relay: Refactor can extend over IPv6 checks

Split "can extend over IPv6" and "has advertised IPv6 ORPort" into
separate functions. They currently have the same result, but this may
change in 33818 with ExtendAllowIPv6Addresses.

Part of 33817.
This commit is contained in:
teor 2020-04-30 06:47:46 +10:00
parent 066d2deb3d
commit 15a4180a7e
4 changed files with 52 additions and 43 deletions

View File

@ -251,7 +251,7 @@ 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)
{
const bool ipv6_supported = router_has_advertised_ipv6_orport(get_options());
const bool ipv6_supported = router_can_extend_over_ipv6(get_options());
/* If IPv6 is not supported, we can't use the IPv6 address. */
if (!ipv6_supported) {

View File

@ -1491,14 +1491,23 @@ router_get_advertised_ipv6_or_ap(const or_options_t *options,
}
/** Returns true if this router has an advertised IPv6 ORPort. */
MOCK_IMPL(bool,
router_has_advertised_ipv6_orport,(const or_options_t *options))
bool
router_has_advertised_ipv6_orport(const or_options_t *options)
{
tor_addr_port_t ipv6_ap;
router_get_advertised_ipv6_or_ap(options, &ipv6_ap);
return tor_addr_port_is_valid_ap(&ipv6_ap, 0);
}
/** Returns true if this router has an advertised IPv6 ORPort. */
MOCK_IMPL(bool,
router_can_extend_over_ipv6,(const or_options_t *options))
{
/* We might add some extra checks here, such as ExtendAllowIPv6Addresses
* from ticket 33818. */
return router_has_advertised_ipv6_orport(options);
}
/** Return the port that we should advertise as our DirPort;
* this is one of three possibilities:
* The one that is passed as <b>dirport</b> if the DirPort option is 0, or

View File

@ -68,8 +68,8 @@ uint16_t router_get_active_listener_port_by_type_af(int listener_type,
uint16_t router_get_advertised_or_port(const or_options_t *options);
void router_get_advertised_ipv6_or_ap(const or_options_t *options,
tor_addr_port_t *ipv6_ap_out);
MOCK_DECL(bool, router_has_advertised_ipv6_orport,(
const or_options_t *options));
bool router_has_advertised_ipv6_orport(const or_options_t *options);
MOCK_DECL(bool, router_can_extend_over_ipv6,(const or_options_t *options));
uint16_t router_get_advertised_or_port_by_af(const or_options_t *options,
sa_family_t family);
uint16_t router_get_advertised_dir_port(const or_options_t *options,

View File

@ -821,14 +821,14 @@ test_circuit_extend_lspec_valid(void *arg)
tor_free(p_chan);
}
static bool router_has_ipv6_orport_result = false;
static int mock_router_ipv6_orport_calls = 0;
static bool can_extend_over_ipv6_result = false;
static int mock_router_can_extend_over_ipv6_calls = 0;
static bool
mock_router_has_advertised_ipv6_orport(const or_options_t *options)
mock_router_can_extend_over_ipv6(const or_options_t *options)
{
(void)options;
mock_router_ipv6_orport_calls++;
return router_has_ipv6_orport_result;
mock_router_can_extend_over_ipv6_calls++;
return can_extend_over_ipv6_result;
}
/* Test the different cases in circuit_choose_ip_ap_for_extend(). */
@ -849,65 +849,65 @@ test_circuit_choose_ip_ap_for_extend(void *arg)
MOCK(get_options, mock_get_options);
mocked_options = fake_options;
MOCK(router_has_advertised_ipv6_orport,
mock_router_has_advertised_ipv6_orport);
router_has_ipv6_orport_result = true;
mock_router_ipv6_orport_calls = 0;
MOCK(router_can_extend_over_ipv6,
mock_router_can_extend_over_ipv6);
can_extend_over_ipv6_result = true;
mock_router_can_extend_over_ipv6_calls = 0;
/* No valid addresses */
router_has_ipv6_orport_result = true;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = true;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL, NULL), OP_EQ, NULL);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
router_has_ipv6_orport_result = false;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = false;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL, NULL), OP_EQ, NULL);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
/* One valid address: IPv4 */
router_has_ipv6_orport_result = true;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = true;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(&ipv4_ap, NULL), OP_EQ, &ipv4_ap);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
router_has_ipv6_orport_result = false;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = false;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(&ipv4_ap, NULL), OP_EQ, &ipv4_ap);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
/* One valid address: IPv6 */
router_has_ipv6_orport_result = true;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = true;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL, &ipv6_ap), OP_EQ, &ipv6_ap);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
router_has_ipv6_orport_result = false;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = false;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(NULL, &ipv6_ap), OP_EQ, NULL);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
/* Two valid addresses */
const tor_addr_port_t *chosen_addr = NULL;
router_has_ipv6_orport_result = true;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = true;
mock_router_can_extend_over_ipv6_calls = 0;
chosen_addr = circuit_choose_ip_ap_for_extend(&ipv4_ap, &ipv6_ap);
tt_assert(chosen_addr == &ipv4_ap || chosen_addr == &ipv6_ap);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
router_has_ipv6_orport_result = false;
mock_router_ipv6_orport_calls = 0;
can_extend_over_ipv6_result = false;
mock_router_can_extend_over_ipv6_calls = 0;
tt_ptr_op(circuit_choose_ip_ap_for_extend(&ipv4_ap, &ipv6_ap),
OP_EQ, &ipv4_ap);
tt_int_op(mock_router_ipv6_orport_calls, OP_EQ, 1);
tt_int_op(mock_router_can_extend_over_ipv6_calls, OP_EQ, 1);
done:
UNMOCK(get_options);
or_options_free(fake_options);
mocked_options = NULL;
UNMOCK(router_has_advertised_ipv6_orport);
UNMOCK(router_can_extend_over_ipv6);
tor_free(fake_options);
}
@ -968,9 +968,9 @@ test_circuit_open_connection_for_extend(void *arg)
mock_channel_connect_calls = 0;
mock_channel_connect_nchan = NULL;
MOCK(router_has_advertised_ipv6_orport,
mock_router_has_advertised_ipv6_orport);
router_has_ipv6_orport_result = true;
MOCK(router_can_extend_over_ipv6,
mock_router_can_extend_over_ipv6);
can_extend_over_ipv6_result = true;
setup_full_capture_of_logs(LOG_INFO);
@ -1110,7 +1110,7 @@ test_circuit_open_connection_for_extend(void *arg)
or_options_free(fake_options);
mocked_options = NULL;
UNMOCK(router_has_advertised_ipv6_orport);
UNMOCK(router_can_extend_over_ipv6);
tor_free(ec);
tor_free(circ->n_hop);