aboutsummaryrefslogtreecommitdiff
path: root/src/feature/relay
diff options
context:
space:
mode:
authorteor <teor@torproject.org>2020-04-29 21:37:31 +1000
committerteor <teor@torproject.org>2020-04-29 22:43:09 +1000
commitbcec1ec071400464f7cefa4f0d493964984905ce (patch)
treec9eee9bec767ef4d98773ad96003184d9c417b26 /src/feature/relay
parentdf5ea297751d68dac7986f827494f2be87fe954f (diff)
downloadtor-bcec1ec071400464f7cefa4f0d493964984905ce.tar.gz
tor-bcec1ec071400464f7cefa4f0d493964984905ce.zip
relay: Refactor choosing extend IPs
Flatten the logic in circuit_choose_ip_ap_for_extend(). Part of 33817.
Diffstat (limited to 'src/feature/relay')
-rw-r--r--src/feature/relay/circuitbuild_relay.c45
1 files changed, 25 insertions, 20 deletions
diff --git a/src/feature/relay/circuitbuild_relay.c b/src/feature/relay/circuitbuild_relay.c
index 2bf08547d8..9420ea11a5 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;
}
}