From 7536c40e9641a0724f0c9e6f994306d762d37e4d Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 25 Nov 2012 13:45:36 -0500 Subject: [PATCH] Implement option to turn off DNS cache modification by a client port (This is part 3 of making DNS cache use enabled/disabled on a per-client port basis. This implements the UseCacheIPv[46]DNS options) --- src/or/addressmap.c | 29 ++++++++++++++++++++--------- src/or/addressmap.h | 4 ++-- src/or/connection_edge.c | 6 +++--- src/or/relay.c | 4 ++-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/or/addressmap.c b/src/or/addressmap.c index a77e199e15..5815bfba61 100644 --- a/src/or/addressmap.c +++ b/src/or/addressmap.c @@ -594,13 +594,13 @@ client_dns_clear_failures(const char *address) * ttlseconds; otherwise, we use the default. */ static void -client_dns_set_addressmap_impl(origin_circuit_t *on_circ, +client_dns_set_addressmap_impl(entry_connection_t *for_conn, const char *address, const char *name, const char *exitname, int ttl) { char *extendedaddress=NULL, *extendedval=NULL; - (void)on_circ; + (void)for_conn; tor_assert(address); tor_assert(name); @@ -640,7 +640,7 @@ client_dns_set_addressmap_impl(origin_circuit_t *on_circ, * ttlseconds; otherwise, we use the default. */ void -client_dns_set_addressmap(origin_circuit_t *on_circ, +client_dns_set_addressmap(entry_connection_t *for_conn, const char *address, const tor_addr_t *val, const char *exitname, @@ -655,14 +655,18 @@ client_dns_set_addressmap(origin_circuit_t *on_circ, if (tor_addr_parse(&addr_tmp, address) >= 0) return; /* If address was an IP address already, don't add a mapping. */ - /* XXXXX For now, don't cache IPv6 addresses. */ - if (tor_addr_family(val) != AF_INET) - return; + if (tor_addr_family(val) == AF_INET) { + if (! for_conn->cache_ipv4_answers) + return; + } else if (tor_addr_family(val) == AF_INET6) { + if (! for_conn->cache_ipv6_answers) + return; + } if (! tor_addr_to_str(valbuf, val, sizeof(valbuf), 1)) return; - client_dns_set_addressmap_impl(on_circ, address, valbuf, exitname, ttl); + client_dns_set_addressmap_impl(for_conn, address, valbuf, exitname, ttl); } /** Add a cache entry noting that address (ordinarily a dotted quad) @@ -675,14 +679,21 @@ client_dns_set_addressmap(origin_circuit_t *on_circ, * ttlseconds; otherwise, we use the default. */ void -client_dns_set_reverse_addressmap(origin_circuit_t *on_circ, +client_dns_set_reverse_addressmap(entry_connection_t *for_conn, const char *address, const char *v, const char *exitname, int ttl) { char *s = NULL; + { + tor_addr_t tmp_addr; + sa_family_t f = tor_addr_parse(&tmp_addr, address); + if ((f == AF_INET && ! for_conn->cache_ipv4_answers) || + (f == AF_INET6 && ! for_conn->cache_ipv6_answers)) + return; + } tor_asprintf(&s, "REVERSE[%s]", address); - client_dns_set_addressmap_impl(on_circ, s, v, exitname, ttl); + client_dns_set_addressmap_impl(for_conn, s, v, exitname, ttl); tor_free(s); } diff --git a/src/or/addressmap.h b/src/or/addressmap.h index 54b3e0ae7c..0c73201f39 100644 --- a/src/or/addressmap.h +++ b/src/or/addressmap.h @@ -31,7 +31,7 @@ int parse_virtual_addr_network(const char *val, int validate_only, char **msg); int client_dns_incr_failures(const char *address); void client_dns_clear_failures(const char *address); -void client_dns_set_addressmap(origin_circuit_t *on_circ, +void client_dns_set_addressmap(entry_connection_t *for_conn, const char *address, const tor_addr_t *val, const char *exitname, int ttl); const char *addressmap_register_virtual_address(int type, char *new_address); @@ -39,7 +39,7 @@ void addressmap_get_mappings(smartlist_t *sl, time_t min_expires, time_t max_expires, int want_expiry); int address_is_in_virtual_range(const char *addr); void clear_trackexithost_mappings(const char *exitname); -void client_dns_set_reverse_addressmap(origin_circuit_t *on_circ, +void client_dns_set_reverse_addressmap(entry_connection_t *for_conn, const char *address, const char *v, const char *exitname, int ttl); diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index 091c14570a..61e57bfe2e 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -2072,7 +2072,7 @@ connection_ap_handshake_socks_resolved(entry_connection_t *conn, tor_addr_t a; tor_addr_from_ipv4n(&a, get_uint32(answer)); if (! tor_addr_is_null(&a)) { - client_dns_set_addressmap(origin_circ, + client_dns_set_addressmap(conn, conn->socks_request->address, &a, conn->chosen_exit_name, ttl); } @@ -2080,13 +2080,13 @@ connection_ap_handshake_socks_resolved(entry_connection_t *conn, tor_addr_t a; tor_addr_from_ipv6_bytes(&a, (char*)answer); if (! tor_addr_is_null(&a)) { - client_dns_set_addressmap(origin_circ, + client_dns_set_addressmap(conn, conn->socks_request->address, &a, conn->chosen_exit_name, ttl); } } else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) { char *cp = tor_strndup((char*)answer, answer_len); - client_dns_set_reverse_addressmap(origin_circ, + client_dns_set_reverse_addressmap(conn, conn->socks_request->address, cp, conn->chosen_exit_name, ttl); diff --git a/src/or/relay.c b/src/or/relay.c index 608b5fa60e..8955d31bb9 100644 --- a/src/or/relay.c +++ b/src/or/relay.c @@ -742,7 +742,7 @@ connection_ap_process_end_not_open( return 0; } - client_dns_set_addressmap(circ, + client_dns_set_addressmap(conn, conn->socks_request->address, &addr, conn->chosen_exit_name, ttl); } @@ -979,7 +979,7 @@ connection_edge_process_relay_cell_not_open( return 0; } - client_dns_set_addressmap(TO_ORIGIN_CIRCUIT(circ), + client_dns_set_addressmap(entry_conn, entry_conn->socks_request->address, &addr, entry_conn->chosen_exit_name, ttl);