From 2a0e48385d01ddb2c8a332faed1445110131d723 Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 30 Apr 2020 21:51:45 +1000 Subject: relay: Launch IPv4 and IPv6 ORPort self-test circuits When launching relay ORPort reachability self-tests, launch tests to the IPv4 and IPv6 ORPorts (if available). Part of 33222. --- src/lib/net/address.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/lib/net/address.c') diff --git a/src/lib/net/address.c b/src/lib/net/address.c index 4193053ee1..e112da9479 100644 --- a/src/lib/net/address.c +++ b/src/lib/net/address.c @@ -1207,20 +1207,17 @@ fmt_addr32(uint32_t addr) return buf; } -/** Return a string representing the family of addr. +/** Return a string representing family. * * This string is a string constant, and must not be freed. * This function is thread-safe. */ const char * -fmt_addr_family(const tor_addr_t *addr) +fmt_af_family(sa_family_t family) { static int default_bug_once = 0; - IF_BUG_ONCE(!addr) - return "NULL pointer"; - - switch (tor_addr_family(addr)) { + switch (family) { case AF_INET6: return "IPv6"; case AF_INET: @@ -1232,7 +1229,7 @@ fmt_addr_family(const tor_addr_t *addr) default: if (!default_bug_once) { log_warn(LD_BUG, "Called with unknown address family %d", - (int)tor_addr_family(addr)); + (int)family); default_bug_once = 1; } return "unknown"; @@ -1240,6 +1237,20 @@ fmt_addr_family(const tor_addr_t *addr) //return "(unreachable code)"; } +/** Return a string representing the family of addr. + * + * This string is a string constant, and must not be freed. + * This function is thread-safe. + */ +const char * +fmt_addr_family(const tor_addr_t *addr) +{ + IF_BUG_ONCE(!addr) + return "NULL pointer"; + + return fmt_af_family(tor_addr_family(addr)); +} + /** Convert the string in src to a tor_addr_t addr. The string * may be an IPv4 address, or an IPv6 address surrounded by square brackets. * -- cgit v1.2.3-54-g00ecf From 0daa1da3ba0ba1dbf2892e042573887f430b32fd Mon Sep 17 00:00:00 2001 From: Neel Chauhan Date: Sun, 26 Apr 2020 13:06:11 -0700 Subject: Define and use TOR_ADDRPORT_BUF_LEN --- changes/ticket33956 | 5 +++++ src/core/or/channeltls.c | 10 +++------- src/lib/net/address.c | 3 +-- src/lib/net/address.h | 9 +++++++++ 4 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 changes/ticket33956 (limited to 'src/lib/net/address.c') diff --git a/changes/ticket33956 b/changes/ticket33956 new file mode 100644 index 0000000000..7ad802797d --- /dev/null +++ b/changes/ticket33956 @@ -0,0 +1,5 @@ + o Code simplification and refactoring: + - Define and use a new constant TOR_ADDRPORT_BUF_LEN which is like + TOR_ADDR_BUF_LEN but includes enough space for an IP address, + brackets, seperating colon, and port number. Closes ticket 33956. + Patch by Neel Chauhan. diff --git a/src/core/or/channeltls.c b/src/core/or/channeltls.c index be941c1762..484727268c 100644 --- a/src/core/or/channeltls.c +++ b/src/core/or/channeltls.c @@ -564,10 +564,7 @@ channel_tls_get_transport_name_method(channel_t *chan, char **transport_out) static const char * channel_tls_get_remote_descr_method(channel_t *chan, int flags) { - /* IPv6 address, colon, port */ -#define MAX_DESCR_LEN (TOR_ADDR_BUF_LEN + 1 + 5) - - static char buf[MAX_DESCR_LEN + 1]; + static char buf[TOR_ADDRPORT_BUF_LEN]; channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan); connection_t *conn; const char *answer = NULL; @@ -580,15 +577,14 @@ channel_tls_get_remote_descr_method(channel_t *chan, int flags) switch (flags) { case 0: /* Canonical address with port*/ - tor_snprintf(buf, MAX_DESCR_LEN + 1, + tor_snprintf(buf, TOR_ADDRPORT_BUF_LEN, "%s:%u", conn->address, conn->port); answer = buf; break; case GRD_FLAG_ORIGINAL: /* Actual address with port */ addr_str = tor_addr_to_str_dup(&(tlschan->conn->real_addr)); - tor_snprintf(buf, MAX_DESCR_LEN + 1, - "%s:%u", addr_str, conn->port); + tor_snprintf(buf, TOR_ADDRPORT_BUF_LEN, "%s:%u", addr_str, conn->port); tor_free(addr_str); answer = buf; break; diff --git a/src/lib/net/address.c b/src/lib/net/address.c index e112da9479..7f63593875 100644 --- a/src/lib/net/address.c +++ b/src/lib/net/address.c @@ -1188,8 +1188,7 @@ fmt_addr_impl(const tor_addr_t *addr, int decorate) const char * fmt_addrport(const tor_addr_t *addr, uint16_t port) { - /* Add space for a colon and up to 5 digits. */ - static char buf[TOR_ADDR_BUF_LEN + 6]; + static char buf[TOR_ADDRPORT_BUF_LEN]; tor_snprintf(buf, sizeof(buf), "%s:%u", fmt_and_decorate_addr(addr), port); return buf; } diff --git a/src/lib/net/address.h b/src/lib/net/address.h index 1cf78beca1..eca5ddab75 100644 --- a/src/lib/net/address.h +++ b/src/lib/net/address.h @@ -213,6 +213,15 @@ tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u) */ #define TOR_ADDR_BUF_LEN 48 +/** Length of a buffer containing an IP address along with a port number and + * a seperating colon. + * + * This allows enough space for + * "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]:12345", + * plus a terminating NUL. + */ +#define TOR_ADDRPORT_BUF_LEN (TOR_ADDR_BUF_LEN + 6) + char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC; /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6 -- cgit v1.2.3-54-g00ecf From 504b16fb757d14f58e20294f7aca338dc6826140 Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 7 May 2020 20:59:10 +1000 Subject: relay: Rewrite inform_testing_reachability() Rewrite inform_testing_reachability() to use separate buffers for IPv4 ORPort, IPv6 ORPort, and IPv4 DirPort. And use consistent APIs to fill those buffers. Part of 33222. --- src/feature/relay/selftest.c | 42 +++++++++++++++++++++++++----------------- src/lib/net/address.c | 11 +++++++++++ src/lib/net/address.h | 1 + 3 files changed, 37 insertions(+), 17 deletions(-) (limited to 'src/lib/net/address.c') diff --git a/src/feature/relay/selftest.c b/src/feature/relay/selftest.c index 4bbdeb3b98..3035c252dd 100644 --- a/src/feature/relay/selftest.c +++ b/src/feature/relay/selftest.c @@ -275,39 +275,47 @@ router_do_reachability_checks(int test_or, int test_dir) int inform_testing_reachability(void) { - char extra_addrbuf[128]; - char *address; + char ipv4_or_buf[TOR_ADDRPORT_BUF_LEN]; + char ipv6_or_buf[TOR_ADDRPORT_BUF_LEN]; + char ipv4_dir_buf[TOR_ADDRPORT_BUF_LEN]; + const routerinfo_t *me = router_get_my_routerinfo(); if (!me) return 0; - address = tor_dup_ip(me->addr); + /* IPv4 ORPort */ + strlcpy(ipv4_or_buf, fmt_addr32_port(me->addr, me->or_port), + sizeof(ipv4_or_buf)); control_event_server_status(LOG_NOTICE, - "CHECKING_REACHABILITY ORADDRESS=%s:%d", - address, me->or_port); + "CHECKING_REACHABILITY ORADDRESS=%s", + ipv4_or_buf); + /* IPv6 ORPort */ const bool has_ipv6 = tor_addr_port_is_valid(&me->ipv6_addr, me->ipv6_orport, 0); if (has_ipv6) { - tor_snprintf(extra_addrbuf, sizeof(extra_addrbuf), " and %s", - fmt_addrport(&me->ipv6_addr, me->ipv6_orport)); + strlcpy(ipv6_or_buf, fmt_addrport(&me->ipv6_addr, me->ipv6_orport), + sizeof(ipv6_or_buf)); /* We'll add an IPv6 control event in #34068. */ } + /* IPv4 DirPort (there are no advertised IPv6 DirPorts) */ if (me->dir_port) { - tor_snprintf(extra_addrbuf, sizeof(extra_addrbuf), " and DirPort %s:%d", - address, me->dir_port); + strlcpy(ipv4_dir_buf, fmt_addr32_port(me->addr, me->dir_port), + sizeof(ipv4_dir_buf)); control_event_server_status(LOG_NOTICE, - "CHECKING_REACHABILITY DIRADDRESS=%s:%d", - address, me->dir_port); + "CHECKING_REACHABILITY DIRADDRESS=%s", + ipv4_dir_buf); } - log_notice(LD_OR, "Now checking whether ORPort%s %s:%d%s %s reachable... " - "(this may take up to %d minutes -- look for log " - "messages indicating success)", + log_notice(LD_OR, "Now checking whether ORPort%s %s%s%s%s%s %s reachable... " + "(this may take up to %d minutes -- look for log " + "messages indicating success)", has_ipv6 ? "s" : "", - address, me->or_port, - has_ipv6 || me->dir_port ? extra_addrbuf : "", + ipv4_or_buf, + has_ipv6 ? " and " : "", + has_ipv6 ? ipv6_or_buf : "", + me->dir_port ? " and DirPort " : "", + me->dir_port ? ipv4_dir_buf : "", has_ipv6 || me->dir_port ? "are" : "is", TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT/60); - tor_free(address); return 1; } diff --git a/src/lib/net/address.c b/src/lib/net/address.c index 7f63593875..76c7aadd22 100644 --- a/src/lib/net/address.c +++ b/src/lib/net/address.c @@ -1206,6 +1206,17 @@ fmt_addr32(uint32_t addr) return buf; } +/** Like fmt_addrport(), but takes addr as a host-order IPv4 + * addresses. Also not thread-safe, also clobbers its return buffer on + * repeated calls. */ +const char * +fmt_addr32_port(uint32_t addr, uint16_t port) +{ + static char buf[INET_NTOA_BUF_LEN + 6]; + snprintf(buf, sizeof(buf), "%s:%u", fmt_addr32(addr), port); + return buf; +} + /** Return a string representing family. * * This string is a string constant, and must not be freed. diff --git a/src/lib/net/address.h b/src/lib/net/address.h index eca5ddab75..5ab654ef1d 100644 --- a/src/lib/net/address.h +++ b/src/lib/net/address.h @@ -236,6 +236,7 @@ const char *fmt_addr_impl(const tor_addr_t *addr, int decorate); const char *fmt_addrport(const tor_addr_t *addr, uint16_t port); #define fmt_addrport_ap(ap) fmt_addrport(&(ap)->addr, (ap)->port) const char *fmt_addr32(uint32_t addr); +const char *fmt_addr32_port(uint32_t addr, uint16_t port); const char *fmt_af_family(sa_family_t family); const char *fmt_addr_family(const tor_addr_t *addr); -- cgit v1.2.3-54-g00ecf