[tor-commits] [tor/master] Move the various _describe() functions out of router.c

nickm at torproject.org nickm at torproject.org
Wed Sep 26 13:52:23 UTC 2018


commit 5c86f3c297229515f125c0e06abdc689c90768f4
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Sep 25 16:13:47 2018 -0400

    Move the various _describe() functions out of router.c
    
    Note that I haven't separated the headers yet (there's still an
---
 src/core/include.am             |   2 +
 src/feature/nodelist/describe.c | 178 ++++++++++++++++++++++++++++++++++++++++
 src/feature/nodelist/describe.h |  25 ++++++
 src/feature/relay/router.c      | 171 --------------------------------------
 src/feature/relay/router.h      |   6 +-
 5 files changed, 206 insertions(+), 176 deletions(-)

diff --git a/src/core/include.am b/src/core/include.am
index ae37bef99..d4671bf6c 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -89,6 +89,7 @@ LIBTOR_APP_A_SOURCES = 				\
 	src/feature/keymgt/loadkey.c		\
 	src/feature/dirauth/keypin.c		\
 	src/feature/nodelist/authcert.c		\
+	src/feature/nodelist/describe.c		\
 	src/feature/nodelist/dirlist.c		\
 	src/feature/nodelist/microdesc.c	\
 	src/feature/nodelist/networkstatus.c	\
@@ -297,6 +298,7 @@ noinst_HEADERS +=					\
 	src/feature/keymgt/loadkey.h			\
 	src/feature/nodelist/authcert.h			\
 	src/feature/nodelist/authority_cert_st.h	\
+	src/feature/nodelist/describe.h			\
 	src/feature/nodelist/desc_store_st.h		\
 	src/feature/nodelist/dirlist.h			\
 	src/feature/nodelist/document_signature_st.h	\
diff --git a/src/feature/nodelist/describe.c b/src/feature/nodelist/describe.c
new file mode 100644
index 000000000..ccf27e02d
--- /dev/null
+++ b/src/feature/nodelist/describe.c
@@ -0,0 +1,178 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "core/or/or.h"
+#include "feature/nodelist/describe.h"
+#include "feature/relay/router.h"
+
+#include "core/or/extend_info_st.h"
+#include "feature/nodelist/node_st.h"
+#include "feature/nodelist/routerinfo_st.h"
+#include "feature/nodelist/routerstatus_st.h"
+
+/**
+ * Longest allowed output of format_node_description, plus 1 character for
+ * NUL.  This allows space for:
+ * "$FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF~xxxxxxxxxxxxxxxxxxx at"
+ * " [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]"
+ * plus a terminating NUL.
+ */
+#define NODE_DESC_BUF_LEN (MAX_VERBOSE_NICKNAME_LEN+4+TOR_ADDR_BUF_LEN)
+
+/** Use <b>buf</b> (which must be at least NODE_DESC_BUF_LEN bytes long) to
+ * hold a human-readable description of a node with identity digest
+ * <b>id_digest</b>, named-status <b>is_named</b>, nickname <b>nickname</b>,
+ * and address <b>addr</b> or <b>addr32h</b>.
+ *
+ * The <b>nickname</b> and <b>addr</b> fields are optional and may be set to
+ * NULL.  The <b>addr32h</b> field is optional and may be set to 0.
+ *
+ * Return a pointer to the front of <b>buf</b>.
+ */
+static const char *
+format_node_description(char *buf,
+                        const char *id_digest,
+                        int is_named,
+                        const char *nickname,
+                        const tor_addr_t *addr,
+                        uint32_t addr32h)
+{
+  char *cp;
+
+  if (!buf)
+    return "<NULL BUFFER>";
+
+  buf[0] = '$';
+  base16_encode(buf+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
+  cp = buf+1+HEX_DIGEST_LEN;
+  if (nickname) {
+    buf[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
+    strlcpy(buf+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
+    cp += strlen(cp);
+  }
+  if (addr32h || addr) {
+    memcpy(cp, " at ", 4);
+    cp += 4;
+    if (addr) {
+      tor_addr_to_str(cp, addr, TOR_ADDR_BUF_LEN, 0);
+    } else {
+      struct in_addr in;
+      in.s_addr = htonl(addr32h);
+      tor_inet_ntoa(&in, cp, INET_NTOA_BUF_LEN);
+    }
+  }
+  return buf;
+}
+
+/** Return a human-readable description of the routerinfo_t <b>ri</b>.
+ *
+ * This function is not thread-safe.  Each call to this function invalidates
+ * previous values returned by this function.
+ */
+const char *
+router_describe(const routerinfo_t *ri)
+{
+  static char buf[NODE_DESC_BUF_LEN];
+
+  if (!ri)
+    return "<null>";
+  return format_node_description(buf,
+                                 ri->cache_info.identity_digest,
+                                 0,
+                                 ri->nickname,
+                                 NULL,
+                                 ri->addr);
+}
+
+/** Return a human-readable description of the node_t <b>node</b>.
+ *
+ * This function is not thread-safe.  Each call to this function invalidates
+ * previous values returned by this function.
+ */
+const char *
+node_describe(const node_t *node)
+{
+  static char buf[NODE_DESC_BUF_LEN];
+  const char *nickname = NULL;
+  uint32_t addr32h = 0;
+  int is_named = 0;
+
+  if (!node)
+    return "<null>";
+
+  if (node->rs) {
+    nickname = node->rs->nickname;
+    is_named = node->rs->is_named;
+    addr32h = node->rs->addr;
+  } else if (node->ri) {
+    nickname = node->ri->nickname;
+    addr32h = node->ri->addr;
+  }
+
+  return format_node_description(buf,
+                                 node->identity,
+                                 is_named,
+                                 nickname,
+                                 NULL,
+                                 addr32h);
+}
+
+/** Return a human-readable description of the routerstatus_t <b>rs</b>.
+ *
+ * This function is not thread-safe.  Each call to this function invalidates
+ * previous values returned by this function.
+ */
+const char *
+routerstatus_describe(const routerstatus_t *rs)
+{
+  static char buf[NODE_DESC_BUF_LEN];
+
+  if (!rs)
+    return "<null>";
+  return format_node_description(buf,
+                                 rs->identity_digest,
+                                 rs->is_named,
+                                 rs->nickname,
+                                 NULL,
+                                 rs->addr);
+}
+
+/** Return a human-readable description of the extend_info_t <b>ei</b>.
+ *
+ * This function is not thread-safe.  Each call to this function invalidates
+ * previous values returned by this function.
+ */
+const char *
+extend_info_describe(const extend_info_t *ei)
+{
+  static char buf[NODE_DESC_BUF_LEN];
+
+  if (!ei)
+    return "<null>";
+  return format_node_description(buf,
+                                 ei->identity_digest,
+                                 0,
+                                 ei->nickname,
+                                 &ei->addr,
+                                 0);
+}
+
+/** Set <b>buf</b> (which must have MAX_VERBOSE_NICKNAME_LEN+1 bytes) to the
+ * verbose representation of the identity of <b>router</b>.  The format is:
+ *  A dollar sign.
+ *  The upper-case hexadecimal encoding of the SHA1 hash of router's identity.
+ *  A "=" if the router is named (no longer implemented); a "~" if it is not.
+ *  The router's nickname.
+ **/
+void
+router_get_verbose_nickname(char *buf, const routerinfo_t *router)
+{
+  buf[0] = '$';
+  base16_encode(buf+1, HEX_DIGEST_LEN+1, router->cache_info.identity_digest,
+                DIGEST_LEN);
+  buf[1+HEX_DIGEST_LEN] = '~';
+  strlcpy(buf+1+HEX_DIGEST_LEN+1, router->nickname, MAX_NICKNAME_LEN+1);
+}
diff --git a/src/feature/nodelist/describe.h b/src/feature/nodelist/describe.h
new file mode 100644
index 000000000..e5723bb93
--- /dev/null
+++ b/src/feature/nodelist/describe.h
@@ -0,0 +1,25 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file describe.h
+ * \brief Header file for describe.c.
+ **/
+
+#ifndef TOR_DESCRIBE_H
+#define TOR_DESCRIBE_H
+
+struct extend_info_t;
+struct node_t;
+struct routerinfo_t;
+struct routerstatus_t;
+
+const char *extend_info_describe(const struct extend_info_t *ei);
+const char *node_describe(const struct node_t *node);
+const char *router_describe(const struct routerinfo_t *ri);
+const char *routerstatus_describe(const struct routerstatus_t *ri);
+
+#endif
diff --git a/src/feature/relay/router.c b/src/feature/relay/router.c
index e145a8e41..494609b5b 100644
--- a/src/feature/relay/router.c
+++ b/src/feature/relay/router.c
@@ -133,13 +133,6 @@ static authority_cert_t *legacy_key_certificate = NULL;
  * used by tor-gencert to sign new signing keys and make new key
  * certificates. */
 
-const char *format_node_description(char *buf,
-                                    const char *id_digest,
-                                    int is_named,
-                                    const char *nickname,
-                                    const tor_addr_t *addr,
-                                    uint32_t addr32h);
-
 /** Return a readonly string with human readable description
  * of <b>err</b>.
  */
@@ -3160,170 +3153,6 @@ is_legal_hexdigest(const char *s)
           strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN);
 }
 
-/**
- * Longest allowed output of format_node_description, plus 1 character for
- * NUL.  This allows space for:
- * "$FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF~xxxxxxxxxxxxxxxxxxx at"
- * " [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]"
- * plus a terminating NUL.
- */
-#define NODE_DESC_BUF_LEN (MAX_VERBOSE_NICKNAME_LEN+4+TOR_ADDR_BUF_LEN)
-
-/** Use <b>buf</b> (which must be at least NODE_DESC_BUF_LEN bytes long) to
- * hold a human-readable description of a node with identity digest
- * <b>id_digest</b>, named-status <b>is_named</b>, nickname <b>nickname</b>,
- * and address <b>addr</b> or <b>addr32h</b>.
- *
- * The <b>nickname</b> and <b>addr</b> fields are optional and may be set to
- * NULL.  The <b>addr32h</b> field is optional and may be set to 0.
- *
- * Return a pointer to the front of <b>buf</b>.
- */
-const char *
-format_node_description(char *buf,
-                        const char *id_digest,
-                        int is_named,
-                        const char *nickname,
-                        const tor_addr_t *addr,
-                        uint32_t addr32h)
-{
-  char *cp;
-
-  if (!buf)
-    return "<NULL BUFFER>";
-
-  buf[0] = '$';
-  base16_encode(buf+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
-  cp = buf+1+HEX_DIGEST_LEN;
-  if (nickname) {
-    buf[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
-    strlcpy(buf+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
-    cp += strlen(cp);
-  }
-  if (addr32h || addr) {
-    memcpy(cp, " at ", 4);
-    cp += 4;
-    if (addr) {
-      tor_addr_to_str(cp, addr, TOR_ADDR_BUF_LEN, 0);
-    } else {
-      struct in_addr in;
-      in.s_addr = htonl(addr32h);
-      tor_inet_ntoa(&in, cp, INET_NTOA_BUF_LEN);
-    }
-  }
-  return buf;
-}
-
-/** Return a human-readable description of the routerinfo_t <b>ri</b>.
- *
- * This function is not thread-safe.  Each call to this function invalidates
- * previous values returned by this function.
- */
-const char *
-router_describe(const routerinfo_t *ri)
-{
-  static char buf[NODE_DESC_BUF_LEN];
-
-  if (!ri)
-    return "<null>";
-  return format_node_description(buf,
-                                 ri->cache_info.identity_digest,
-                                 0,
-                                 ri->nickname,
-                                 NULL,
-                                 ri->addr);
-}
-
-/** Return a human-readable description of the node_t <b>node</b>.
- *
- * This function is not thread-safe.  Each call to this function invalidates
- * previous values returned by this function.
- */
-const char *
-node_describe(const node_t *node)
-{
-  static char buf[NODE_DESC_BUF_LEN];
-  const char *nickname = NULL;
-  uint32_t addr32h = 0;
-  int is_named = 0;
-
-  if (!node)
-    return "<null>";
-
-  if (node->rs) {
-    nickname = node->rs->nickname;
-    is_named = node->rs->is_named;
-    addr32h = node->rs->addr;
-  } else if (node->ri) {
-    nickname = node->ri->nickname;
-    addr32h = node->ri->addr;
-  }
-
-  return format_node_description(buf,
-                                 node->identity,
-                                 is_named,
-                                 nickname,
-                                 NULL,
-                                 addr32h);
-}
-
-/** Return a human-readable description of the routerstatus_t <b>rs</b>.
- *
- * This function is not thread-safe.  Each call to this function invalidates
- * previous values returned by this function.
- */
-const char *
-routerstatus_describe(const routerstatus_t *rs)
-{
-  static char buf[NODE_DESC_BUF_LEN];
-
-  if (!rs)
-    return "<null>";
-  return format_node_description(buf,
-                                 rs->identity_digest,
-                                 rs->is_named,
-                                 rs->nickname,
-                                 NULL,
-                                 rs->addr);
-}
-
-/** Return a human-readable description of the extend_info_t <b>ei</b>.
- *
- * This function is not thread-safe.  Each call to this function invalidates
- * previous values returned by this function.
- */
-const char *
-extend_info_describe(const extend_info_t *ei)
-{
-  static char buf[NODE_DESC_BUF_LEN];
-
-  if (!ei)
-    return "<null>";
-  return format_node_description(buf,
-                                 ei->identity_digest,
-                                 0,
-                                 ei->nickname,
-                                 &ei->addr,
-                                 0);
-}
-
-/** Set <b>buf</b> (which must have MAX_VERBOSE_NICKNAME_LEN+1 bytes) to the
- * verbose representation of the identity of <b>router</b>.  The format is:
- *  A dollar sign.
- *  The upper-case hexadecimal encoding of the SHA1 hash of router's identity.
- *  A "=" if the router is named (no longer implemented); a "~" if it is not.
- *  The router's nickname.
- **/
-void
-router_get_verbose_nickname(char *buf, const routerinfo_t *router)
-{
-  buf[0] = '$';
-  base16_encode(buf+1, HEX_DIGEST_LEN+1, router->cache_info.identity_digest,
-                DIGEST_LEN);
-  buf[1+HEX_DIGEST_LEN] = '~';
-  strlcpy(buf+1+HEX_DIGEST_LEN+1, router->nickname, MAX_NICKNAME_LEN+1);
-}
-
 /** Forget that we have issued any router-related warnings, so that we'll
  * warn again if we see the same errors. */
 void
diff --git a/src/feature/relay/router.h b/src/feature/relay/router.h
index 5e342cc49..d56ddc8a1 100644
--- a/src/feature/relay/router.h
+++ b/src/feature/relay/router.h
@@ -13,6 +13,7 @@
 #define TOR_ROUTER_H
 
 #include "lib/testsupport/testsupport.h"
+#include "feature/nodelist/describe.h"
 
 struct curve25519_keypair_t;
 struct ed25519_keypair_t;
@@ -122,11 +123,6 @@ int is_legal_nickname(const char *s);
 int is_legal_nickname_or_hexdigest(const char *s);
 int is_legal_hexdigest(const char *s);
 
-const char *router_describe(const routerinfo_t *ri);
-const char *node_describe(const node_t *node);
-const char *routerstatus_describe(const routerstatus_t *ri);
-const char *extend_info_describe(const extend_info_t *ei);
-
 const char *routerinfo_err_to_string(int err);
 int routerinfo_err_is_transient(int err);
 





More information about the tor-commits mailing list