aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoger Dingledine <arma@torproject.org>2013-02-11 17:09:10 -0500
committerRoger Dingledine <arma@torproject.org>2013-02-11 17:09:10 -0500
commitcc896f7c84c221d7dcfff1e0155533dce3ac5518 (patch)
tree55ff5874885e1208f15f728274fa80694897a45c /src
parent719940df2bdfbd0f5ee02a9ca404f345d2fc49e8 (diff)
downloadtor-cc896f7c84c221d7dcfff1e0155533dce3ac5518.tar
tor-cc896f7c84c221d7dcfff1e0155533dce3ac5518.tar.gz
Teach resolve_my_address() to return a cached answer
I didn't make any of the callers use this feature yet.
Diffstat (limited to 'src')
-rw-r--r--src/or/config.c20
-rw-r--r--src/or/config.h3
-rw-r--r--src/or/dirserv.c4
-rw-r--r--src/or/router.c6
-rw-r--r--src/or/routerlist.c3
5 files changed, 27 insertions, 9 deletions
diff --git a/src/or/config.c b/src/or/config.c
index 7ca20e46b..e37b148b7 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1908,12 +1908,16 @@ static uint32_t last_resolved_addr = 0;
* holding that hostname. (If we didn't get our address by resolving a
* hostname, set *<b>hostname_out</b> to NULL.)
*
+ * If <b>use_cached_addr</b> is true, and we have a plausible answer,
+ * provide that answer and return.
+ *
* XXXX ipv6
*/
int
resolve_my_address(int warn_severity, const or_options_t *options,
uint32_t *addr_out,
- const char **method_out, char **hostname_out)
+ const char **method_out, char **hostname_out,
+ int use_cached_addr)
{
struct in_addr in;
uint32_t addr; /* host order */
@@ -1931,6 +1935,18 @@ resolve_my_address(int warn_severity, const or_options_t *options,
tor_assert(addr_out);
/*
+ * Step zero: if used_cached_addr is true, and we have a cached answer,
+ * just return it and be done.
+ */
+
+ if (use_cached_addr && last_resolved_addr) {
+ *addr_out = last_resolved_addr;
+ if (method_out)
+ *method_out = "CACHED";
+ return 0;
+ }
+
+ /*
* Step one: Fill in 'hostname' to be our best guess.
*/
@@ -2343,7 +2359,7 @@ options_validate(or_options_t *old_options, or_options_t *options,
if (authdir_mode(options)) {
/* confirm that our address isn't broken, so we can complain now */
uint32_t tmp;
- if (resolve_my_address(LOG_WARN, options, &tmp, NULL, NULL) < 0)
+ if (resolve_my_address(LOG_WARN, options, &tmp, NULL, NULL, 0) < 0)
REJECT("Failed to resolve/guess local address. See logs for details.");
}
diff --git a/src/or/config.h b/src/or/config.h
index e0748a07b..7ec52e39d 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -28,7 +28,8 @@ setopt_err_t options_trial_assign(config_line_t *list, int use_defaults,
int resolve_my_address(int warn_severity, const or_options_t *options,
uint32_t *addr_out,
- const char **method_out, char **hostname_out);
+ const char **method_out, char **hostname_out,
+ int use_cached_addr);
int is_local_addr(const tor_addr_t *addr);
void options_init(or_options_t *options);
char *options_dump(const or_options_t *options, int minimal);
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 0819d4bd2..280c6b429 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -2750,7 +2750,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
tor_assert(private_key);
tor_assert(cert);
- if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname)<0) {
+ if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname, 0)<0) {
log_warn(LD_NET, "Couldn't resolve my hostname");
return NULL;
}
@@ -2960,7 +2960,7 @@ generate_v2_networkstatus_opinion(void)
private_key = get_server_identity_key();
- if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname)<0) {
+ if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname, 0)<0) {
log_warn(LD_NET, "Couldn't resolve my hostname");
goto done;
}
diff --git a/src/or/router.c b/src/or/router.c
index 4492ed271..bc1c6d215 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1712,7 +1712,7 @@ static int router_guess_address_from_dir_headers(uint32_t *guess);
int
router_pick_published_address(const or_options_t *options, uint32_t *addr)
{
- if (resolve_my_address(LOG_INFO, options, addr, NULL, NULL) < 0) {
+ if (resolve_my_address(LOG_INFO, options, addr, NULL, NULL, 0) < 0) {
log_info(LD_CONFIG, "Could not determine our address locally. "
"Checking if directory headers provide any hints.");
if (router_guess_address_from_dir_headers(addr) < 0) {
@@ -2103,7 +2103,7 @@ check_descriptor_ipaddress_changed(time_t now)
/* XXXX ipv6 */
prev = desc_routerinfo->addr;
- if (resolve_my_address(LOG_INFO, options, &cur, &method, &hostname) < 0) {
+ if (resolve_my_address(LOG_INFO, options, &cur, &method, &hostname, 0) < 0) {
log_info(LD_CONFIG,"options->Address didn't resolve into an IP.");
return;
}
@@ -2159,7 +2159,7 @@ router_new_address_suggestion(const char *suggestion,
}
/* XXXX ipv6 */
- if (resolve_my_address(LOG_INFO, options, &cur, NULL, NULL) >= 0) {
+ if (resolve_my_address(LOG_INFO, options, &cur, NULL, NULL, 0) >= 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 */
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 90b707bcd..2dde89b7e 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -3951,7 +3951,8 @@ trusted_dir_server_new(const char *nickname, const char *address,
dir_server_t *result;
if (!address) { /* The address is us; we should guess. */
- if (resolve_my_address(LOG_WARN, get_options(), &a, NULL, &hostname) < 0) {
+ if (resolve_my_address(LOG_WARN, get_options(),
+ &a, NULL, &hostname, 0) < 0) {
log_warn(LD_CONFIG,
"Couldn't find a suitable address when adding ourself as a "
"trusted directory server.");