Преглед на файлове

Merge node_get_{prim,pref,pref_ipv6}_addr with their _orport counterparts.

This keeps the IP address and TCP for a given OR port together,
reducing the risk of using an address for one address family with a
port of another.

Make node_get_addr() a wrapper function for compatibility.
Linus Nordberg преди 13 години
родител
ревизия
2376a6ade4
променени са 3 файла, в които са добавени 41 реда и са изтрити 73 реда
  1. 6 6
      src/or/connection_or.c
  2. 29 58
      src/or/nodelist.c
  3. 6 9
      src/or/nodelist.h

+ 6 - 6
src/or/connection_or.c

@@ -647,11 +647,11 @@ connection_or_init_conn_from_address(or_connection_t *conn,
   tor_addr_copy(&conn->_base.addr, addr);
   tor_addr_copy(&conn->real_addr, addr);
   if (r) {
-    tor_addr_t node_addr;
-    node_get_pref_addr(r, &node_addr);
+    tor_addr_port_t node_ap;
+    node_get_pref_orport(r, &node_ap);
     /* XXXX proposal 186 is making this more complex.  For now, a conn
        is canonical when it uses the _preferred_ address. */
-    if (tor_addr_eq(&conn->_base.addr, &node_addr))
+    if (tor_addr_eq(&conn->_base.addr, &node_ap.addr))
       conn->is_canonical = 1;
     if (!started_here) {
       /* Override the addr/port, so our log messages will make sense.
@@ -664,12 +664,12 @@ connection_or_init_conn_from_address(or_connection_t *conn,
        * right IP address and port 56244, that wouldn't be as helpful. now we
        * log the "right" port too, so we know if it's moria1 or moria2.
        */
-      tor_addr_copy(&conn->_base.addr, &node_addr);
-      conn->_base.port = node_get_pref_orport(r);
+      tor_addr_copy(&conn->_base.addr, &node_ap.addr);
+      conn->_base.port = node_ap.port;
     }
     conn->nickname = tor_strdup(node_get_nickname(r));
     tor_free(conn->_base.address);
-    conn->_base.address = tor_dup_addr(&node_addr);
+    conn->_base.address = tor_dup_addr(&node_ap.addr);
   } else {
     const char *n;
     /* If we're an authoritative directory server, we may know a

+ 29 - 58
src/or/nodelist.c

@@ -682,19 +682,30 @@ node_get_all_orports(const node_t *node)
   return sl;
 }
 
-/** Copy the primary, IPv4, address for <b>node</b> into
- * *<b>addr_out</b>.  */
+/** Copy the primary (IPv4) OR port (IP address and TCP port) for
+ * <b>node</b> into *<b>ap_out</b>.  */
 void
-node_get_prim_addr(const node_t *node, tor_addr_t *addr_out)
+node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
 {
   if (node->ri) {
-    router_get_prim_addr_port(node->ri, addr_out, NULL);
+    router_get_prim_addr_port(node->ri, &ap_out->addr, &ap_out->port);
   }
   else if (node->rs) {
-    tor_addr_from_ipv4h(addr_out, node->rs->addr);
+    tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
+    ap_out->port = node->rs->or_port;
   }
 }
 
+/** Wrapper around node_get_prim_orport for backward
+    compatibility.  */
+void
+node_get_addr(const node_t *node, tor_addr_t *addr_out)
+{
+  tor_addr_port_t ap;
+  node_get_prim_orport(node, &ap);
+  tor_addr_copy(addr_out, &ap.addr);
+}
+
 /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  * seem to have one.  */
 uint32_t
@@ -708,40 +719,34 @@ node_get_prim_addr_ipv4h(const node_t *node)
   return 0;
 }
 
-/** Return 1 if we prefer the IPv6 address of <b>node</b>, else 0.  */
-static int
-node_ipv6_preferred(const node_t *node)
-{
-  if (node->ri != NULL)
-    return router_ipv6_preferred(node->ri);
-  return 0;
-}
-
-/** Copy the preferred address for <b>node</b> into
- * <b>addr_out</b>.  */
+/** Copy the preferred OR port (IP address and TCP port) for
+ * <b>node</b> into <b>ap_out</b>.  */
 void
-node_get_pref_addr(const node_t *node, tor_addr_t *addr_out)
+node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
 {
   if (node->ri) {
-    router_get_pref_addr_port(node->ri, addr_out, NULL);
+    router_get_pref_addr_port(node->ri, &ap_out->addr, &ap_out->port);
   } else if (node->rs) {
     /* No IPv6 in routerstatus_t yet.  XXXprop186 ok for private
        bridges but needs fixing */
-    tor_addr_from_ipv4h(addr_out, node->rs->addr);
+    tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
+    ap_out->port = node->rs->or_port;
   }
 }
 
-/** Copy the preferred IPv6 address for <b>node</b> into
- * *<b>addr_out</b>. */
+/** Copy the preferred IPv6 OR port (address and TCP port) for
+ * <b>node</b> into *<b>ap_out</b>. */
 void
-node_get_pref_ipv6_addr(const node_t *node, tor_addr_t *addr_out)
+node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
 {
   if (node->ri) {
-    tor_addr_copy(addr_out, &node->ri->ipv6_addr);
+    tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
+    ap_out->port = node->ri->ipv6_orport;
   } else if (node->rs) {
     /* No IPv6 in routerstatus_t yet.  XXXprop186 ok for private
        bridges but needs fixing */
-    tor_addr_make_unspec(addr_out);
+    tor_addr_make_unspec(&ap_out->addr);
+    ap_out->port = 0;
   }
 }
 
@@ -772,40 +777,6 @@ node_get_declared_uptime(const node_t *node)
     return -1;
 }
 
-/** Return <b>node</b>'s declared primary (IPv4) or_port. */
-uint16_t
-node_get_prim_orport(const node_t *node)
-{
-  if (node->ri)
-    return node->ri->or_port;
-  else if (node->rs)
-    return node->rs->or_port;
-  else
-    return 0;
-}
-
-/** Return <b>node</b>'s preferred or_port. */
-uint16_t
-node_get_pref_orport(const node_t *node)
-{
-  if (node_ipv6_preferred(node))
-    return node_get_pref_ipv6_orport(node);
-  else
-    return node_get_prim_orport(node);
-}
-
-/** Return <b>node</b>'s preferred IPv6 or_port. */
-uint16_t
-node_get_pref_ipv6_orport(const node_t *node)
-{
-  if (node->ri)
-    return node->ri->ipv6_orport;
-  else if (node->rs)
-    return 0;			/* No IPv6 in routerstatus_t yet. */
-  else
-    return 0;
-}
-
 /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
 const char *
 node_get_platform(const node_t *node)

+ 6 - 9
src/or/nodelist.h

@@ -38,14 +38,12 @@ int node_get_purpose(const node_t *node);
 int node_is_me(const node_t *node);
 int node_exit_policy_rejects_all(const node_t *node);
 smartlist_t *node_get_all_orports(const node_t *node);
-void node_get_prim_addr(const node_t *node, tor_addr_t *addr_out);
-void node_get_pref_addr(const node_t *node, tor_addr_t *addr_out);
-void node_get_pref_ipv6_addr(const node_t *node, tor_addr_t *addr_out);
+void node_get_prim_orport(const node_t *node, tor_addr_port_t *addr_port_out);
+void node_get_pref_orport(const node_t *node, tor_addr_port_t *addr_port_out);
+void node_get_pref_ipv6_orport(const node_t *node,
+                               tor_addr_port_t *addr_port_out);
 uint32_t node_get_prim_addr_ipv4h(const node_t *node);
 int node_allows_single_hop_exits(const node_t *node);
-uint16_t node_get_prim_orport(const node_t *node);
-uint16_t node_get_pref_orport(const node_t *node);
-uint16_t node_get_pref_ipv6_orport(const node_t *node);
 const char *node_get_nickname(const node_t *node);
 const char *node_get_platform(const node_t *node);
 void node_get_address_string(const node_t *node, char *cp, size_t len);
@@ -55,10 +53,9 @@ const smartlist_t *node_get_declared_family(const node_t *node);
 
 smartlist_t *nodelist_get_list(void);
 
-/* Temporary aliases during transition to multiple addresses.  */
-#define node_get_addr(n,a) node_get_prim_addr((n),(a))
+/* Temporary during transition to multiple addresses.  */
+void node_get_addr(const node_t *node, tor_addr_t *addr_out);
 #define node_get_addr_ipv4h(n) node_get_prim_addr_ipv4h((n))
-#define node_get_orport(n) node_get_prim_orport((n))
 
 /* XXXX These need to move out of routerlist.c */
 void nodelist_refresh_countries(void);