[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [tor/master] relay: Rewrite inform_testing_reachability()
commit 504b16fb757d14f58e20294f7aca338dc6826140
Author: teor <teor@xxxxxxxxxx>
Date: Thu May 7 20:59:10 2020 +1000
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(-)
diff --git a/src/feature/relay/selftest.c b/src/feature/relay/selftest.c
index 4bbdeb3b9..3035c252d 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 7f6359387..76c7aadd2 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 <b>addr</b> 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 <b>family</b>.
*
* 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 eca5ddab7..5ab654ef1 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);
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits