From b95dd03e5f6505ce2e78fe34a20bf5e5c970e6eb Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 15 May 2011 21:58:46 -0400 Subject: Log descriptions of nodes, not just nicknames. This patch introduces a few new functions in router.c to produce a more helpful description of a node than its nickame, and then tweaks nearly all log messages taking a nickname as an argument to call these functions instead. There are a few cases where I left the old log messages alone: in these cases, the nickname was that of an authority (whose nicknames are useful and unique), or the message already included an identity and/or an address. I might have missed a couple more too. This is a fix for bug 3045. --- src/or/router.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) (limited to 'src/or/router.c') diff --git a/src/or/router.c b/src/or/router.c index 616a290d8a..a11d3a5923 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -2217,6 +2217,133 @@ is_legal_hexdigest(const char *s) strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN); } +/** Use buf (which must be at least NODE_DESC_BUF_LEN bytes long) to + * hold a human-readable description of a node with identity digest + * id_digest, named-status is_named, nickname nickname, + * and address addr or addr32h. + * + * The nickname and addr fields are optional and may be set to + * NULL. The addr32h field is optional and may be set to + * addr32h. + * + * Return a pointer to the front of buf. + */ +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; + 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; +} + +/** Use buf (which must be at least NODE_DESC_BUF_LEN bytes long) to + * hold a human-readable description of ri. + * + * + * Return a pointer to the front of buf. + */ +const char * +router_get_description(char *buf, const routerinfo_t *ri) +{ + return format_node_description(buf, + ri->cache_info.identity_digest, + ri->is_named, + ri->nickname, + NULL, + ri->addr); +} + +/** Use buf (which must be at least NODE_DESC_BUF_LEN bytes long) to + * hold a human-readable description of rs. + * + * Return a pointer to the front of buf. + */ +const char * +routerstatus_get_description(char *buf, const routerstatus_t *rs) +{ + return format_node_description(buf, + rs->identity_digest, + rs->is_named, + rs->nickname, + NULL, + rs->addr); +} + +/** Use buf (which must be at least NODE_DESC_BUF_LEN bytes long) to + * hold a human-readable description of ei. + * + * Return a pointer to the front of buf. + */ +const char * +extend_info_get_description(char *buf, const extend_info_t *ei) +{ + return format_node_description(buf, + ei->identity_digest, + 0, + ei->nickname, + &ei->addr, + 0); +} + +/** Return a human-readable description of the routerinfo_t ri. + * + * 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]; + return router_get_description(buf, ri); +} + +/** Return a human-readable description of the routerstatus_t rs. + * + * 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]; + return routerstatus_get_description(buf, rs); +} + +/** Return a human-readable description of the extend_info_t ri. + * + * 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]; + return extend_info_get_description(buf, ei); +} + /** Set buf (which must have MAX_VERBOSE_NICKNAME_LEN+1 bytes) to the * verbose representation of the identity of router. The format is: * A dollar sign. -- cgit v1.2.3-54-g00ecf From 3c0d944b076aa552216ca598a6cdc50e77ad1a58 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 30 May 2011 15:15:10 -0400 Subject: Improve comments and defensive programming for 3045 The comment fixes are trivial. The defensive programming trick is to tolerate receiving NULL inputs on the describe functions. That should never actually happen, but it seems like the likeliest mistake for us to make in the future. --- src/or/router.c | 13 +++++++++++-- src/or/router.h | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'src/or/router.c') diff --git a/src/or/router.c b/src/or/router.c index a11d3a5923..0bd4c55026 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -2223,8 +2223,7 @@ is_legal_hexdigest(const char *s) * and address addr or addr32h. * * The nickname and addr fields are optional and may be set to - * NULL. The addr32h field is optional and may be set to - * addr32h. + * NULL. The addr32h field is optional and may be set to 0. * * Return a pointer to the front of buf. */ @@ -2237,6 +2236,10 @@ format_node_description(char *buf, uint32_t addr32h) { char *cp; + + if (!buf) + return ""; + buf[0] = '$'; base16_encode(buf+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN); cp = buf+1+HEX_DIGEST_LEN; @@ -2268,6 +2271,8 @@ format_node_description(char *buf, const char * router_get_description(char *buf, const routerinfo_t *ri) { + if (!ri) + return ""; return format_node_description(buf, ri->cache_info.identity_digest, ri->is_named, @@ -2284,6 +2289,8 @@ router_get_description(char *buf, const routerinfo_t *ri) const char * routerstatus_get_description(char *buf, const routerstatus_t *rs) { + if (!rs) + return ""; return format_node_description(buf, rs->identity_digest, rs->is_named, @@ -2300,6 +2307,8 @@ routerstatus_get_description(char *buf, const routerstatus_t *rs) const char * extend_info_get_description(char *buf, const extend_info_t *ei) { + if (!ei) + return ""; return format_node_description(buf, ei->identity_digest, 0, diff --git a/src/or/router.h b/src/or/router.h index 6cabfd56e8..a285a3e773 100644 --- a/src/or/router.h +++ b/src/or/router.h @@ -86,6 +86,13 @@ int is_legal_nickname(const char *s); int is_legal_nickname_or_hexdigest(const char *s); int is_legal_hexdigest(const char *s); +/** + * 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) const char *format_node_description(char *buf, const char *id_digest, -- cgit v1.2.3-54-g00ecf