summaryrefslogtreecommitdiff
path: root/src/lib/net
diff options
context:
space:
mode:
authorteor <teor@torproject.org>2020-04-15 09:52:57 +1000
committerteor <teor@torproject.org>2020-04-29 22:43:09 +1000
commita72e017e7f69581ceb005d05ce8033a6fd05626e (patch)
tree6308dc41fa1b22cb2d019ea55380c73d9d3af10a /src/lib/net
parente9d04b05c6ba3930d55dc6c7e512e082ff67ebfb (diff)
downloadtor-a72e017e7f69581ceb005d05ce8033a6fd05626e.tar.gz
tor-a72e017e7f69581ceb005d05ce8033a6fd05626e.zip
net: Add fmt_addrport_ap() and fmt_addr_family()
Add fmt_addrport_ap(), a macro that takes a tor_addr_port_t, and uses it to call fmt_addrport(). Add fmt_addr_family(), a function that returns a string constant describing the address family. Utility functions for 33817.
Diffstat (limited to 'src/lib/net')
-rw-r--r--src/lib/net/address.c33
-rw-r--r--src/lib/net/address.h2
2 files changed, 35 insertions, 0 deletions
diff --git a/src/lib/net/address.c b/src/lib/net/address.c
index 5fe9abca1b..9a90093aef 100644
--- a/src/lib/net/address.c
+++ b/src/lib/net/address.c
@@ -1206,6 +1206,39 @@ fmt_addr32(uint32_t addr)
return buf;
}
+/** 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)
+{
+ static int default_bug_once = 0;
+
+ IF_BUG_ONCE(!addr)
+ return "NULL pointer";
+
+ switch (tor_addr_family(addr)) {
+ case AF_INET6:
+ return "IPv6";
+ case AF_INET:
+ return "IPv4";
+ case AF_UNIX:
+ return "UNIX socket";
+ case AF_UNSPEC:
+ return "unspecified";
+ default:
+ if (!default_bug_once) {
+ log_warn(LD_BUG, "Called with unknown address family %d",
+ (int)tor_addr_family(addr));
+ default_bug_once = 1;
+ }
+ return "unknown";
+ }
+ //return "(unreachable code)";
+}
+
/** 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 186f1fe86a..611d1ca1ee 100644
--- a/src/lib/net/address.h
+++ b/src/lib/net/address.h
@@ -221,7 +221,9 @@ char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC;
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_addr_family(const tor_addr_t *addr);
MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
tor_addr_t *addr));