[tor-commits] [tor/master] net: Add fmt_addrport_ap() and fmt_addr_family()

nickm at torproject.org nickm at torproject.org
Wed Apr 29 23:23:42 UTC 2020


commit a72e017e7f69581ceb005d05ce8033a6fd05626e
Author: teor <teor at torproject.org>
Date:   Wed Apr 15 09:52:57 2020 +1000

    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.
---
 src/lib/net/address.c | 33 +++++++++++++++++++++++++++++++++
 src/lib/net/address.h |  2 ++
 2 files changed, 35 insertions(+)

diff --git a/src/lib/net/address.c b/src/lib/net/address.c
index 5fe9abca1..9a90093ae 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 186f1fe86..611d1ca1e 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));





More information about the tor-commits mailing list