[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Refactor duplicate code in router_check_descriptor_address_consistency



commit 427663428bf91dca0dfa2ede151cdb7eb9007408
Author: teor <teor2345@xxxxxxxxx>
Date:   Tue Aug 16 14:05:46 2016 +1000

    Refactor duplicate code in router_check_descriptor_address_consistency
    
    No behaviour change
---
 src/or/router.c | 107 ++++++++++++++++++++++++--------------------------------
 1 file changed, 45 insertions(+), 62 deletions(-)

diff --git a/src/or/router.c b/src/or/router.c
index c6bbafb..db800b8 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1896,6 +1896,47 @@ router_pick_published_address,(const or_options_t *options, uint32_t *addr))
   return 0;
 }
 
+/* Like router_check_descriptor_address_consistency, but specifically for the
+ * ORPort or DirPort.
+ * listener_type is either CONN_TYPE_OR_LISTENER or CONN_TYPE_DIR_LISTENER. */
+static void
+router_check_descriptor_address_port_consistency(uint32_t ipv4h_desc_addr,
+                                                 int listener_type)
+{
+  /* The first configured Port, which may be the magic constant CFG_AUTO_PORT.
+   */
+  int port_v4_cfg = get_first_advertised_port_by_type_af(listener_type,
+                                                         AF_INET);
+  if (port_v4_cfg != 0 &&
+      !port_exists_by_type_addr32h_port(listener_type,
+                                        ipv4h_desc_addr, port_v4_cfg, 1)) {
+        const tor_addr_t *port_addr = get_first_advertised_addr_by_type_af(
+                                                                listener_type,
+                                                                AF_INET);
+        tor_addr_t desc_addr;
+        char port_addr_str[TOR_ADDR_BUF_LEN];
+        char desc_addr_str[TOR_ADDR_BUF_LEN];
+
+        tor_addr_to_str(port_addr_str, port_addr, TOR_ADDR_BUF_LEN, 0);
+
+        tor_addr_from_ipv4h(&desc_addr, ipv4h_desc_addr);
+        tor_addr_to_str(desc_addr_str, &desc_addr, TOR_ADDR_BUF_LEN, 0);
+
+        log_warn(LD_CONFIG, "The configured IPv4 %sPort address %s does not "
+                 "match the address %s in the descriptor. Please configure "
+                 "the matching IPv4 addresses for this Tor relay as "
+                 "Address <IPv4 address> in the torrc configuration file if "
+                 "you have multiple public IP addresses. If you are behind a "
+                 "NAT and have the right ports forwarded, you can ignore this "
+                 "warning or, to remove it, use 2 %sPort lines with options "
+                 "NoListen (for the public IPv4 address line) and NoAdvertise "
+                 "(for the internal NAT IPv4 address line).",
+                 listener_type ? "OR" : "Dir",
+                 port_addr_str, desc_addr_str,
+                 listener_type ? "OR" : "Dir");
+      }
+}
+
 /* Tor relays only have one IPv4 address in the descriptor, which is derived
  * from the Address torrc option, or guessed using various methods in
  * router_pick_published_address().
@@ -1921,68 +1962,10 @@ router_pick_published_address,(const or_options_t *options, uint32_t *addr))
 static void
 router_check_descriptor_address_consistency(uint32_t ipv4h_desc_addr)
 {
-
-  /* The first configured ORPort and DirPort, which may be CFG_AUTO_PORT. */
-  int orport_v4_cfg = get_first_advertised_port_by_type_af(
-                                                        CONN_TYPE_OR_LISTENER,
-                                                        AF_INET);
-  int dirport_v4_cfg = get_first_advertised_port_by_type_af(
-                                                        CONN_TYPE_DIR_LISTENER,
-                                                        AF_INET);
-
-  if (orport_v4_cfg != 0 &&
-      !port_exists_by_type_addr32h_port(CONN_TYPE_OR_LISTENER,
-                                        ipv4h_desc_addr, orport_v4_cfg, 1)) {
-      const tor_addr_t *port_addr = get_first_advertised_addr_by_type_af(
-                                                        CONN_TYPE_OR_LISTENER,
-                                                        AF_INET);
-      tor_addr_t desc_addr;
-      char port_addr_str[TOR_ADDR_BUF_LEN];
-      char desc_addr_str[TOR_ADDR_BUF_LEN];
-
-      tor_addr_to_str(port_addr_str, port_addr, TOR_ADDR_BUF_LEN, 0);
-
-      tor_addr_from_ipv4h(&desc_addr, ipv4h_desc_addr);
-      tor_addr_to_str(desc_addr_str, &desc_addr, TOR_ADDR_BUF_LEN, 0);
-
-      log_warn(LD_CONFIG, "The configured IPv4 ORPort address %s does not "
-               "match the address %s in the descriptor. Please configure "
-               "the matching IPv4 addresses for this Tor relay as "
-               "Address <IPv4 address> in the torrc configuration file if "
-               "you have multiple public IP addresses. If you are behind a "
-               "NAT and have the right ports forwarded, you can ignore this "
-               "warning or, to remove it, use 2 ORPort lines with options "
-               "NoListen (for the public IPv4 address line) and NoAdvertise "
-               "(for the internal NAT IPv4 address line).",
-               port_addr_str, desc_addr_str);
-    }
-
-  if (dirport_v4_cfg != 0 &&
-      !port_exists_by_type_addr32h_port(CONN_TYPE_DIR_LISTENER,
-                                        ipv4h_desc_addr, dirport_v4_cfg, 1)) {
-      const tor_addr_t *port_addr = get_first_advertised_addr_by_type_af(
-                                                        CONN_TYPE_DIR_LISTENER,
-                                                        AF_INET);
-      tor_addr_t desc_addr;
-      char port_addr_str[TOR_ADDR_BUF_LEN];
-      char desc_addr_str[TOR_ADDR_BUF_LEN];
-
-      tor_addr_to_str(port_addr_str, port_addr, TOR_ADDR_BUF_LEN, 0);
-
-      tor_addr_from_ipv4h(&desc_addr, ipv4h_desc_addr);
-      tor_addr_to_str(desc_addr_str, &desc_addr, TOR_ADDR_BUF_LEN, 0);
-
-      log_warn(LD_CONFIG, "The configured IPv4 DirPort address %s does not "
-               "match the address %s in the descriptor. Please configure "
-               "the matching IPv4 addresses for this Tor relay as "
-               "Address <IPv4 address> in the torrc configuration file if "
-               "you have multiple public IP addresses. If you are behind a "
-               "NAT and have the right ports forwarded, you can ignore this "
-               "warning or, to remove it, use 2 DirPort lines with options "
-               "NoListen (for the public IPv4 address line) and NoAdvertise "
-               "(for the internal NAT IPv4 address line).",
-               port_addr_str, desc_addr_str);
-    }
+  router_check_descriptor_address_port_consistency(ipv4h_desc_addr,
+                                                   CONN_TYPE_OR_LISTENER);
+  router_check_descriptor_address_port_consistency(ipv4h_desc_addr,
+                                                   CONN_TYPE_DIR_LISTENER);
 }
 
 /** Build a fresh routerinfo, signed server descriptor, and extra-info document



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits