[tor-commits] [tor/master] relay: Refactor choosing extend IPs

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


commit bcec1ec071400464f7cefa4f0d493964984905ce
Author: teor <teor at torproject.org>
Date:   Wed Apr 29 21:37:31 2020 +1000

    relay: Refactor choosing extend IPs
    
    Flatten the logic in circuit_choose_ip_ap_for_extend().
    
    Part of 33817.
---
 src/feature/relay/circuitbuild_relay.c | 45 +++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 20 deletions(-)

diff --git a/src/feature/relay/circuitbuild_relay.c b/src/feature/relay/circuitbuild_relay.c
index 2bf08547d..9420ea11a 100644
--- a/src/feature/relay/circuitbuild_relay.c
+++ b/src/feature/relay/circuitbuild_relay.c
@@ -250,8 +250,27 @@ STATIC const tor_addr_port_t *
 circuit_choose_ip_ap_for_extend(const tor_addr_port_t *ipv4_ap,
                                 const tor_addr_port_t *ipv6_ap)
 {
-  /* If we could make an IPv4 or an IPv6 connection, make an IPv6 connection
-   * at random, with probability 1 in N.
+  const bool ipv6_supported = router_has_advertised_ipv6_orport(get_options());
+
+  /* If IPv6 is not supported, we can't use the IPv6 address. */
+  if (!ipv6_supported) {
+    ipv6_ap = NULL;
+  }
+
+  /* If there is no IPv6 address, IPv4 is always supported.
+   * Until clients include IPv6 ORPorts, and most relays support IPv6,
+   * this is the most common case. */
+  if (!ipv6_ap) {
+    return ipv4_ap;
+  }
+
+  /* If there is no IPv4 address, return the (possibly NULL) IPv6 address. */
+  if (!ipv4_ap) {
+    return ipv6_ap;
+  }
+
+  /* Now we have an IPv4 and an IPv6 address, and IPv6 is supported.
+   * So make an IPv6 connection at random, with probability 1 in N.
    *   1 means "always IPv6 (and no IPv4)"
    *   2 means "equal probability of IPv4 or IPv6"
    *   ... (and so on) ...
@@ -260,26 +279,12 @@ circuit_choose_ip_ap_for_extend(const tor_addr_port_t *ipv4_ap,
    */
 #define IPV6_CONNECTION_ONE_IN_N 2
 
-  /* IPv4 is always supported */
-  const bool ipv6_supported = router_has_advertised_ipv6_orport(get_options());
-
-  if (ipv4_ap && ipv6_ap && ipv6_supported) {
-    /* Choose between IPv4 and IPv6 at random */
-    bool choose_ipv6 = crypto_fast_rng_one_in_n(get_thread_fast_rng(),
-                                                IPV6_CONNECTION_ONE_IN_N);
-    if (choose_ipv6) {
-      return ipv6_ap;
-    } else {
-      return ipv4_ap;
-    }
-  } else if (ipv6_ap && ipv6_supported) {
-    /* There's only one valid address: try to use it */
+  bool choose_ipv6 = crypto_fast_rng_one_in_n(get_thread_fast_rng(),
+                                              IPV6_CONNECTION_ONE_IN_N);
+  if (choose_ipv6) {
     return ipv6_ap;
-  } else if (ipv4_ap) {
-    return ipv4_ap;
   } else {
-    /* IPv6-only extend, but IPv6 is not supported. */
-    return NULL;
+    return ipv4_ap;
   }
 }
 





More information about the tor-commits mailing list