addr: Refactor last resolved address cache accessors

Series of things done in this commit:

  1. Rename the functions to better reflect the namespace of the file.

  2. Make both reset and get function to operate on the last_resolved_addrs
     cache that is per family.

  3. Make the get function to take a tor_addr_t.

  4. Change all callsite to use the new convention.

Part of #33233

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2020-06-23 10:06:19 -04:00
parent b8042c9d9a
commit 7795dd7ef6
4 changed files with 34 additions and 17 deletions

View File

@ -47,18 +47,23 @@ static tor_addr_t last_resolved_addrs[IDX_SIZE];
/** Last value actually set by resolve_my_address. */
static uint32_t last_resolved_addr_v4 = 0;
/** Accessor for last_resolved_addr_v4 from outside this file. */
uint32_t
get_last_resolved_addr_v4(void)
/** Copy the last resolved address of family into addr_out.
*
* If not last resolved address existed, the addr_out is a null address (use
* tor_addr_is_null()). */
void
resolved_addr_get_last(int family, tor_addr_t *addr_out)
{
return last_resolved_addr_v4;
tor_addr_copy(addr_out, &last_resolved_addrs[family]);
}
/** Reset last_resolved_addr_v4 from outside this file. */
/** Reset the last resolved address of family.
*
* This makes it null address. */
void
reset_last_resolved_addr_v4(void)
resolved_addr_reset_last(int family)
{
last_resolved_addr_v4 = 0;
tor_addr_make_null(&last_resolved_addrs[family], family);
}
/** @brief Return true iff the given IP address can be used as a valid

View File

@ -19,8 +19,8 @@ bool find_my_address(const or_options_t *options, int family,
int warn_severity, tor_addr_t *addr_out,
const char **method_out, char **hostname_out);
uint32_t get_last_resolved_addr_v4(void);
void reset_last_resolved_addr_v4(void);
void resolved_addr_get_last(int family, tor_addr_t *addr_out);
void resolved_addr_reset_last(int family);
MOCK_DECL(int, is_local_addr, (const tor_addr_t *addr));

View File

@ -4870,7 +4870,7 @@ client_check_address_changed(tor_socket_t sock)
smartlist_clear(outgoing_addrs);
smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
/* We'll need to resolve ourselves again. */
reset_last_resolved_addr_v4();
resolved_addr_reset_last(AF_INET);
/* Okay, now change our keys. */
ip_address_changed(1);
}

View File

@ -45,7 +45,7 @@ void
router_new_address_suggestion(const char *suggestion,
const dir_connection_t *d_conn)
{
tor_addr_t addr;
tor_addr_t addr, last_resolved_addr;
uint32_t cur = 0; /* Current IPv4 address. */
const or_options_t *options = get_options();
@ -64,14 +64,22 @@ router_new_address_suggestion(const char *suggestion,
}
/* XXXX ipv6 */
cur = get_last_resolved_addr_v4();
if (cur ||
resolve_my_address_v4(LOG_INFO, options, &cur, NULL, NULL) >= 0) {
resolved_addr_get_last(AF_INET, &last_resolved_addr);
if (!tor_addr_is_null(&last_resolved_addr)) {
/* Lets use this one. */
tor_addr_copy(&last_guessed_ip, &last_resolved_addr);
return;
}
/* Attempt to find our address. */
if (resolve_my_address_v4(LOG_INFO, options, &cur, NULL, NULL) >= 0) {
/* We're all set -- we already know our address. Great. */
tor_addr_from_ipv4h(&last_guessed_ip, cur); /* store it in case we
need it later */
return;
}
/* Consider the suggestion from the directory. */
if (tor_addr_is_internal(&addr, 0)) {
/* Don't believe anybody who says our IP is, say, 127.0.0.1. */
return;
@ -111,10 +119,14 @@ MOCK_IMPL(int,
router_pick_published_address, (const or_options_t *options, uint32_t *addr,
int cache_only))
{
/* First, check the cached output from resolve_my_address(). */
*addr = get_last_resolved_addr_v4();
if (*addr)
tor_addr_t last_resolved_addr;
/* First, check the cached output from resolve_my_address_v4(). */
resolved_addr_get_last(AF_INET, &last_resolved_addr);
if (!tor_addr_is_null(&last_resolved_addr)) {
*addr = tor_addr_to_ipv4h(&last_resolved_addr);
return 0;
}
/* Second, consider doing a resolve attempt right here. */
if (!cache_only) {