relay: Use flags in relay_find_addr_to_publish()

Instead of a boolean saying "cache_only" add the concept of flags so we add
semantic through out the code and allow ourselves to have more options in the
future.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2020-07-17 11:10:56 -04:00
parent 230293c169
commit 75434a1df1
7 changed files with 38 additions and 21 deletions

View File

@ -1652,11 +1652,12 @@ pt_get_extra_info_descriptor_string(void)
tor_addr_t addr;
/* Attempt to find the IPv4 and then attempt to find the IPv6 if we
* can't find it. */
bool found = relay_find_addr_to_publish(get_options(), AF_INET, false,
bool found = relay_find_addr_to_publish(get_options(), AF_INET,
RELAY_FIND_ADDR_NO_FLAG,
&addr);
if (!found) {
found = relay_find_addr_to_publish(get_options(), AF_INET6, false,
&addr);
found = relay_find_addr_to_publish(get_options(), AF_INET6,
RELAY_FIND_ADDR_NO_FLAG, &addr);
}
if (!found) {
log_err(LD_PT, "Unable to find address for transport %s", t->name);

View File

@ -133,7 +133,8 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
*answer = tor_strdup("VERBOSE_NAMES EXTENDED_EVENTS");
} else if (!strcmp(question, "address")) {
tor_addr_t addr;
if (!relay_find_addr_to_publish(get_options(), AF_INET, true, &addr)) {
if (!relay_find_addr_to_publish(get_options(), AF_INET,
RELAY_FIND_ADDR_CACHE_ONLY, &addr)) {
*errmsg = "Address unknown";
return -1;
}

View File

@ -86,18 +86,19 @@ relay_address_new_suggestion(const tor_addr_t *suggested_addr,
* 1. Resolved cache. Populated by find_my_address() during the relay
* periodic event that attempts to learn if our address has changed.
*
* 2. If cache_only is false, attempt to find the address using the
* relay_find_addr.h interface.
* 2. If flags is set with RELAY_FIND_ADDR_CACHE_ONLY, only the resolved
* and suggested cache are looked at. No address discovery will be done.
*
* 3. Finally, if all fails, use the suggested address cache which is
* populated by the NETINFO cell values.
* populated by the NETINFO cell content or HTTP header from a
* directory.
*
* Return true on success and addr_out contains the address to use for the
* given family. On failure to find the address, false is returned and
* addr_out is set to an AF_UNSPEC address. */
MOCK_IMPL(bool,
relay_find_addr_to_publish, (const or_options_t *options, int family,
bool cache_only, tor_addr_t *addr_out))
int flags, tor_addr_t *addr_out))
{
tor_assert(options);
tor_assert(addr_out);
@ -113,7 +114,7 @@ relay_find_addr_to_publish, (const or_options_t *options, int family,
/* Second, attempt to find our address. The following can do a DNS resolve
* thus only do it when the no cache only flag is flipped. */
if (!cache_only) {
if (!(flags & RELAY_FIND_ADDR_CACHE_ONLY)) {
if (find_my_address(options, family, LOG_INFO, addr_out, NULL, NULL)) {
goto found;
}
@ -140,5 +141,6 @@ bool
relay_has_address_set(int family)
{
tor_addr_t addr;
return relay_find_addr_to_publish(get_options(), family, 1, &addr);
return relay_find_addr_to_publish(get_options(), family,
RELAY_FIND_ADDR_CACHE_ONLY, &addr);
}

View File

@ -9,12 +9,17 @@
#ifndef TOR_RELAY_FIND_ADDR_H
#define TOR_RELAY_FIND_ADDR_H
typedef enum {
RELAY_FIND_ADDR_NO_FLAG = (1U << 0),
RELAY_FIND_ADDR_CACHE_ONLY = (1U << 1),
} relay_find_addr_flags_t;
void relay_address_new_suggestion(const tor_addr_t *suggested_addr,
const tor_addr_t *peer_addr,
const char *identity_digest);
MOCK_DECL(bool, relay_find_addr_to_publish,
(const or_options_t *options, int family, bool cache_only,
(const or_options_t *options, int family, int flags,
tor_addr_t *addr_out));
bool relay_has_address_set(int family);

View File

@ -2040,9 +2040,11 @@ router_build_fresh_unsigned_routerinfo,(routerinfo_t **ri_out))
/* Find our resolved address both IPv4 and IPv6. In case the address is not
* found, the object is set to an UNSPEC address. */
bool have_v4 = relay_find_addr_to_publish(options, AF_INET, false,
bool have_v4 = relay_find_addr_to_publish(options, AF_INET,
RELAY_FIND_ADDR_NO_FLAG,
&ipv4_addr);
bool have_v6 = relay_find_addr_to_publish(options, AF_INET6, false,
bool have_v6 = relay_find_addr_to_publish(options, AF_INET6,
RELAY_FIND_ADDR_NO_FLAG,
&ipv6_addr);
/* Tor requires a relay to have an IPv4 so bail if we can't find it. */

View File

@ -3846,11 +3846,11 @@ static bool mock_relay_find_addr_to_publish_result = true;
static bool
mock_relay_find_addr_to_publish(const or_options_t *options, int family,
bool cache_only, tor_addr_t *addr_out)
int flags, tor_addr_t *addr_out)
{
(void) options;
(void) family;
(void) cache_only;
(void) flags;
(void) addr_out;
return mock_relay_find_addr_to_publish_result;
}

View File

@ -316,11 +316,13 @@ test_find_addr_to_publish(void *arg)
tt_assert(tor_addr_eq(&ipv6_addr, &cache_addr));
/* 1. Address located in the resolved cache. */
ret = relay_find_addr_to_publish(&options, AF_INET, true, &cache_addr);
ret = relay_find_addr_to_publish(&options, AF_INET,
RELAY_FIND_ADDR_CACHE_ONLY, &cache_addr);
tt_assert(ret);
tt_assert(tor_addr_eq(&ipv4_addr, &cache_addr));
ret = relay_find_addr_to_publish(&options, AF_INET6, true, &cache_addr);
ret = relay_find_addr_to_publish(&options, AF_INET6,
RELAY_FIND_ADDR_CACHE_ONLY, &cache_addr);
tt_assert(ret);
tt_assert(tor_addr_eq(&ipv6_addr, &cache_addr));
resolved_addr_reset_last(AF_INET);
@ -330,21 +332,25 @@ test_find_addr_to_publish(void *arg)
* the find_my_address() code path because that is extensively tested in
* another unit tests. */
resolved_addr_set_suggested(&ipv4_addr);
ret = relay_find_addr_to_publish(&options, AF_INET, true, &cache_addr);
ret = relay_find_addr_to_publish(&options, AF_INET,
RELAY_FIND_ADDR_CACHE_ONLY, &cache_addr);
tt_assert(ret);
tt_assert(tor_addr_eq(&ipv4_addr, &cache_addr));
resolved_addr_set_suggested(&ipv6_addr);
ret = relay_find_addr_to_publish(&options, AF_INET6, true, &cache_addr);
ret = relay_find_addr_to_publish(&options, AF_INET6,
RELAY_FIND_ADDR_CACHE_ONLY, &cache_addr);
tt_assert(ret);
tt_assert(tor_addr_eq(&ipv6_addr, &cache_addr));
resolve_addr_reset_suggested(AF_INET);
resolve_addr_reset_suggested(AF_INET6);
/* 3. No IP anywhere. */
ret = relay_find_addr_to_publish(&options, AF_INET, true, &cache_addr);
ret = relay_find_addr_to_publish(&options, AF_INET,
RELAY_FIND_ADDR_CACHE_ONLY, &cache_addr);
tt_assert(!ret);
ret = relay_find_addr_to_publish(&options, AF_INET6, true, &cache_addr);
ret = relay_find_addr_to_publish(&options, AF_INET6,
RELAY_FIND_ADDR_CACHE_ONLY, &cache_addr);
tt_assert(!ret);
done: