diff options
author | Nick Mathewson <nickm@torproject.org> | 2020-06-09 15:44:58 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2020-06-09 15:44:58 -0400 |
commit | 354f085e5f14c2bb4918b015e36cdc72748e0ea6 (patch) | |
tree | 0467dc89507ca7e7cc6a5f109332d693ed6a139a /src/lib/net | |
parent | eaae5625cb1c5fdd26c766157742725aa4b7ad9e (diff) | |
parent | 1df451aba116f77a5a24e4e54cf343ec46d55f9a (diff) | |
download | tor-354f085e5f14c2bb4918b015e36cdc72748e0ea6.tar.gz tor-354f085e5f14c2bb4918b015e36cdc72748e0ea6.zip |
Merge remote-tracking branch 'tor-github/pr/1888/head'
Diffstat (limited to 'src/lib/net')
-rw-r--r-- | src/lib/net/address.c | 36 | ||||
-rw-r--r-- | src/lib/net/address.h | 2 |
2 files changed, 31 insertions, 7 deletions
diff --git a/src/lib/net/address.c b/src/lib/net/address.c index 6d46f9b955..b51fc7cb13 100644 --- a/src/lib/net/address.c +++ b/src/lib/net/address.c @@ -1217,20 +1217,28 @@ fmt_addr32(uint32_t addr) return buf; } -/** Return a string representing the family of <b>addr</b>. +/** 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. * 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: @@ -1242,7 +1250,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"; @@ -1250,6 +1258,20 @@ fmt_addr_family(const tor_addr_t *addr) //return "(unreachable code)"; } +/** Return a string representing the family of <b>addr</b>. + * + * 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 <b>src</b> to a tor_addr_t <b>addr</b>. The string * may be an IPv4 address, or an IPv6 address surrounded by square brackets. * diff --git a/src/lib/net/address.h b/src/lib/net/address.h index e5016ee4fe..5ab654ef1d 100644 --- a/src/lib/net/address.h +++ b/src/lib/net/address.h @@ -236,6 +236,8 @@ 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); MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family, |