tor-commits
Threads by month
- ----- 2025 -----
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
November 2012
- 18 participants
- 1508 discussions

15 Nov '12
commit c64ee7099f32c2630d8ed65febf92357cd5e9487
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Wed Oct 24 23:39:28 2012 -0400
Record, send, and receive flags in BEGIN cells
---
src/or/connection_edge.c | 8 ++++++--
src/or/or.h | 2 ++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index a840b43..9a4f274 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -1656,6 +1656,10 @@ connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
ap_conn->socks_request->address : "",
ap_conn->socks_request->port);
payload_len = (int)strlen(payload)+1;
+ if (payload_len <= RELAY_PAYLOAD_SIZE - 4) {
+ set_uint32(payload + payload_len, htonl(edge_conn->begincell_flags));
+ payload_len += 4;
+ }
log_info(LD_APP,
"Sending relay cell %d to begin stream %d.",
@@ -2235,14 +2239,14 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
}
log_debug(LD_EXIT,"Creating new exit connection.");
- n_stream = edge_connection_new(CONN_TYPE_EXIT, AF_INET);
+ n_stream = edge_connection_new(CONN_TYPE_EXIT, AF_INET);/*XXXX IPv6*/
/* Remember the tunneled request ID in the new edge connection, so that
* we can measure download times. */
n_stream->dirreq_id = circ->dirreq_id;
n_stream->base_.purpose = EXIT_PURPOSE_CONNECT;
-
+ n_stream->begincell_flags = bcell.flags;
n_stream->stream_id = rh.stream_id;
n_stream->base_.port = port;
/* leave n_stream->s at -1, because it's not yet valid */
diff --git a/src/or/or.h b/src/or/or.h
index 05d27b1..fa90aae 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1414,6 +1414,8 @@ typedef struct edge_connection_t {
uint32_t address_ttl; /**< TTL for address-to-addr mapping on exit
* connection. Exit connections only. */
+ uint32_t begincell_flags; /** Flags sent or received in the BEGIN cell
+ * for this connection */
streamid_t stream_id; /**< The stream ID used for this edge connection on its
* circuit */
1
0

15 Nov '12
commit b35a0d113233fa29439500555201b85c1dec64b1
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Wed Oct 24 23:05:44 2012 -0400
Add IPv6 support to compare_to_addr_to_node_policy
---
src/or/policies.c | 19 ++++++++++++++++---
src/or/router.c | 19 +++++++++++++++++++
2 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/src/or/policies.c b/src/or/policies.c
index 8367446..16b3e4f 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -1616,16 +1616,29 @@ compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port,
if (node->rejects_all)
return ADDR_POLICY_REJECTED;
- if (node->ri)
+ if (addr && tor_addr_family(addr) == AF_INET6) {
+ const short_policy_t *p = NULL;
+ if (node->ri)
+ p = node->ri->ipv6_exit_policy;
+ else if (node->md)
+ p = node->md->ipv6_exit_policy;
+ if (p)
+ return compare_tor_addr_to_short_policy(addr, port, p);
+ else
+ return ADDR_POLICY_REJECTED;
+ }
+
+ if (node->ri) {
return compare_tor_addr_to_addr_policy(addr, port, node->ri->exit_policy);
- else if (node->md) {
+ } else if (node->md) {
if (node->md->exit_policy == NULL)
return ADDR_POLICY_REJECTED;
else
return compare_tor_addr_to_short_policy(addr, port,
node->md->exit_policy);
- } else
+ } else {
return ADDR_POLICY_PROBABLY_REJECTED;
+ }
}
/** Implementation for GETINFO control command: knows the answer for questions
diff --git a/src/or/router.c b/src/or/router.c
index efe24d7..cdd33bb 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1609,6 +1609,11 @@ router_rebuild_descriptor(int force)
}
ri->policy_is_reject_star =
policy_is_reject_star(ri->exit_policy);
+#if 0
+ /* XXXX024 Don't actually enable this code until exiting to an IPv6
+ * address works. */
+ ri->ipv6_exit_policy = policy_summarize(&ri->exit_policy, AF_INET6);
+#endif
#if 0
/* XXXX NM NM I belive this is safe to remove */
@@ -2150,6 +2155,20 @@ router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
}
}
+ if (router->ipv6_exit_policy) {
+ char *p6 = write_short_policy(router->ipv6_exit_policy);
+ if (p6 && strcmp(p6, "reject 1-65535")) {
+ result = tor_snprintf(s+written, maxlen-written,
+ "ipv6-policy %s\n", p6);
+ if (result<0) {
+ log_warn(LD_BUG,"Descriptor printf of policy ran out of room");
+ return -1;
+ }
+ written += result;
+ }
+ tor_free(p6);
+ }
+
if (written + DIROBJ_MAX_SIG_LEN > maxlen) {
/* Not enough room for signature. */
log_warn(LD_BUG,"not enough room left in descriptor for signature!");
1
0

[tor/master] Simplest version of server-side IPv6 support (no dns)
by nickm@torproject.org 15 Nov '12
by nickm@torproject.org 15 Nov '12
15 Nov '12
commit c3faa0ebd55f7de80db1a3aa5e64a79c72708722
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Wed Oct 24 23:45:24 2012 -0400
Simplest version of server-side IPv6 support (no dns)
This is a relatively simple set of changes: we mostly need to
remove a few "but not for IPv6" changes. We also needed to tweak
the handling of DNS code to generate RESOLVED cells that could get
an IPv6 answer in return.
---
src/or/connection_edge.c | 3 +++
src/or/dns.c | 38 +++++++++++++++++++++++++++++++-------
src/or/router.c | 4 ++--
3 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index 9a4f274..d97bea7 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -2408,6 +2408,9 @@ connection_exit_connect(edge_connection_t *edge_conn)
addr = &conn->addr;
port = conn->port;
+ if (tor_addr_family(addr) == AF_INET6)
+ conn->socket_family = AF_INET6;
+
log_debug(LD_EXIT,"about to try connecting");
switch (connection_connect(conn, conn->address, addr, port, &socket_error)) {
case -1: {
diff --git a/src/or/dns.c b/src/or/dns.c
index 5e1d0b4..d26314a 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -160,7 +160,8 @@ typedef struct cached_resolve_t {
static void purge_expired_resolves(time_t now);
static void dns_found_answer(const char *address, uint8_t is_reverse,
- uint32_t addr, const char *hostname, char outcome,
+ uint32_t addr,
+ const char *hostname, char outcome,
uint32_t ttl);
static void send_resolved_cell(edge_connection_t *conn, uint8_t answer_type);
static int launch_resolve(edge_connection_t *exitconn);
@@ -490,9 +491,13 @@ purge_expired_resolves(time_t now)
assert_cache_ok();
}
+/* argument for send_resolved_cell only, meaning "let the answer type be ipv4
+ * or ipv6 depending on the connection's address". */
+#define RESOLVED_TYPE_AUTO 0xff
+
/** Send a response to the RESOLVE request of a connection.
* <b>answer_type</b> must be one of
- * RESOLVED_TYPE_(IPV4|ERROR|ERROR_TRANSIENT).
+ * RESOLVED_TYPE_(IPV4|IPV6|ERROR|ERROR_TRANSIENT|AUTO).
*
* If <b>circ</b> is provided, and we have a cached answer, send the
* answer back along circ; otherwise, send the answer back along
@@ -505,6 +510,16 @@ send_resolved_cell(edge_connection_t *conn, uint8_t answer_type)
size_t buflen;
uint32_t ttl;
+ if (answer_type == RESOLVED_TYPE_AUTO) {
+ sa_family_t family = tor_addr_family(&conn->base_.addr);
+ if (family == AF_INET)
+ answer_type = RESOLVED_TYPE_IPV4;
+ else if (family == AF_INET6)
+ answer_type = RESOLVED_TYPE_IPV6;
+ else
+ answer_type = RESOLVED_TYPE_ERROR_TRANSIENT;
+ }
+
buf[0] = answer_type;
ttl = dns_clip_ttl(conn->address_ttl);
@@ -516,7 +531,15 @@ send_resolved_cell(edge_connection_t *conn, uint8_t answer_type)
set_uint32(buf+6, htonl(ttl));
buflen = 10;
break;
- /*XXXX IP6 need ipv6 implementation */
+ case RESOLVED_TYPE_IPV6:
+ {
+ const uint8_t *bytes = tor_addr_to_in6_addr8(&conn->base_.addr);
+ buf[1] = 16;
+ memcpy(buf+2, bytes, 16);
+ set_uint32(buf+18, htonl(ttl));
+ buflen = 22;
+ }
+ break;
case RESOLVED_TYPE_ERROR_TRANSIENT:
case RESOLVED_TYPE_ERROR:
{
@@ -614,7 +637,7 @@ dns_resolve(edge_connection_t *exitconn)
if (hostname)
send_resolved_hostname_cell(exitconn, hostname);
else
- send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
+ send_resolved_cell(exitconn, RESOLVED_TYPE_AUTO);
exitconn->on_circuit = NULL;
} else {
/* Add to the n_streams list; the calling function will send back a
@@ -693,12 +716,13 @@ dns_resolve_impl(edge_connection_t *exitconn, int is_resolve,
/* first check if exitconn->base_.address is an IP. If so, we already
* know the answer. */
if (tor_addr_parse(&addr, exitconn->base_.address) >= 0) {
- if (tor_addr_family(&addr) == AF_INET) {
+ if (tor_addr_family(&addr) == AF_INET ||
+ tor_addr_family(&addr) == AF_INET6) {
tor_addr_copy(&exitconn->base_.addr, &addr);
exitconn->address_ttl = DEFAULT_DNS_TTL;
return 1;
} else {
- /* XXXX IPv6 */
+ /* XXXX unspec? Bogus? */
return -1;
}
}
@@ -1140,7 +1164,7 @@ dns_found_answer(const char *address, uint8_t is_reverse, uint32_t addr,
if (is_reverse)
send_resolved_hostname_cell(pendconn, hostname);
else
- send_resolved_cell(pendconn, RESOLVED_TYPE_IPV4);
+ send_resolved_cell(pendconn, RESOLVED_TYPE_AUTO);
circ = circuit_get_by_edge_conn(pendconn);
tor_assert(circ);
circuit_detach_stream(circ, pendconn);
diff --git a/src/or/router.c b/src/or/router.c
index cdd33bb..642656f 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1380,8 +1380,8 @@ router_compare_to_my_exit_policy(edge_connection_t *conn)
if (tor_addr_is_null(&conn->base_.addr))
return -1;
- /* XXXX IPv6 */
- if (tor_addr_family(&conn->base_.addr) != AF_INET)
+ if (tor_addr_family(&conn->base_.addr) != AF_INET &&
+ tor_addr_family(&conn->base_.addr) != AF_INET6)
return -1;
return compare_tor_addr_to_addr_policy(&conn->base_.addr, conn->base_.port,
1
0

15 Nov '12
commit 04ea55014182776871841279f6917d1a03c4e905
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Wed Oct 24 22:34:55 2012 -0400
Authorities put p6 lines into microdescriptors.
---
src/or/dirvote.c | 11 +++++++++++
src/or/dirvote.h | 5 ++++-
2 files changed, 15 insertions(+), 1 deletions(-)
diff --git a/src/or/dirvote.c b/src/or/dirvote.c
index f8b8d37..9810c82 100644
--- a/src/or/dirvote.c
+++ b/src/or/dirvote.c
@@ -3569,6 +3569,17 @@ dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
if (summary && strcmp(summary, "reject 1-65535"))
smartlist_add_asprintf(chunks, "p %s\n", summary);
+ if (consensus_method >= MIN_METHOD_FOR_P6_LINES &&
+ ri->ipv6_exit_policy) {
+ /* XXXX024 This doesn't match proposal 208, which says these should
+ * be taken unchanged from the routerinfo. That's bogosity, IMO:
+ * the proposal should have said to do this instead.*/
+ char *p6 = write_short_policy(ri->ipv6_exit_policy);
+ if (p6 && strcmp(p6, "reject 1-65535"))
+ smartlist_add_asprintf(chunks, "p6 %s\n", p6);
+ tor_free(p6);
+ }
+
output = smartlist_join_strings(chunks, "", 0, NULL);
{
diff --git a/src/or/dirvote.h b/src/or/dirvote.h
index 04cf2f9..d14a375 100644
--- a/src/or/dirvote.h
+++ b/src/or/dirvote.h
@@ -20,7 +20,7 @@
#define MIN_VOTE_INTERVAL 300
/** The highest consensus method that we currently support. */
-#define MAX_SUPPORTED_CONSENSUS_METHOD 14
+#define MAX_SUPPORTED_CONSENSUS_METHOD 15
/** Lowest consensus method that contains a 'directory-footer' marker */
#define MIN_METHOD_FOR_FOOTER 9
@@ -45,6 +45,9 @@
/** Lowest consensus method that contains "a" lines. */
#define MIN_METHOD_FOR_A_LINES 14
+/** Lowest consensus method where microdescs may include a "p6" line. */
+#define MIN_METHOD_FOR_P6_LINES 15
+
void dirvote_free_all(void);
/* vote manipulation */
1
0
commit 9016d9e8294a35295851d12b62de4ac9a5549709
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Thu Oct 25 00:20:41 2012 -0400
Add an IPv6Exit configuration option
Don't advertise an IPv6 exit policy, or accept IPv6 exit requests,
if IPv6Exit is not true.
---
src/or/config.c | 3 ++-
src/or/connection_edge.c | 17 +++++++++++++++--
src/or/connection_edge.h | 5 +++++
src/or/or.h | 2 ++
src/or/policies.c | 5 +++++
src/or/policies.h | 1 +
src/or/router.c | 1 +
src/test/test.c | 6 +++---
8 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index f8ac67e..76038d5 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -276,7 +276,7 @@ static config_var_t option_vars_[] = {
V(HTTPProxyAuthenticator, STRING, NULL),
V(HTTPSProxy, STRING, NULL),
V(HTTPSProxyAuthenticator, STRING, NULL),
- // V(IPv6EXit, BOOL, "0"),
+ V(IPv6Exit, BOOL, "0"),
VAR("ServerTransportPlugin", LINELIST, ServerTransportPlugin, NULL),
V(Socks4Proxy, STRING, NULL),
V(Socks5Proxy, STRING, NULL),
@@ -3170,6 +3170,7 @@ options_transition_affects_descriptor(const or_options_t *old_options,
!config_lines_eq(old_options->ExitPolicy,new_options->ExitPolicy) ||
old_options->ExitPolicyRejectPrivate !=
new_options->ExitPolicyRejectPrivate ||
+ old_options->IPv6Exit != new_options->IPv6Exit ||
!config_lines_eq(old_options->ORPort_lines,
new_options->ORPort_lines) ||
!config_lines_eq(old_options->DirPort_lines,
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index d97bea7..373edf7 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -2238,6 +2238,17 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
return 0;
}
+ if (! options->IPv6Exit) {
+ /* I don't care if you prefer IPv6; I can't give you any. */
+ bcell.flags &= ~BEGIN_FLAG_IPV6_PREFERRED;
+ /* If you don't want IPv4, I can't help. */
+ if (bcell.flags & BEGIN_FLAG_IPV4_NOT_OK) {
+ tor_free(address);
+ relay_send_end_cell_from_edge(rh.stream_id, circ,
+ END_STREAM_REASON_EXITPOLICY, NULL);
+ }
+ }
+
log_debug(LD_EXIT,"Creating new exit connection.");
n_stream = edge_connection_new(CONN_TYPE_EXIT, AF_INET);/*XXXX IPv6*/
@@ -2395,8 +2406,10 @@ connection_exit_connect(edge_connection_t *edge_conn)
connection_t *conn = TO_CONN(edge_conn);
int socket_error = 0;
- if (!connection_edge_is_rendezvous_stream(edge_conn) &&
- router_compare_to_my_exit_policy(edge_conn)) {
+ if ( (!connection_edge_is_rendezvous_stream(edge_conn) &&
+ router_compare_to_my_exit_policy(edge_conn)) ||
+ (tor_addr_family(&conn->addr) == AF_INET6 &&
+ ! get_options()->IPv6Exit)) {
log_info(LD_EXIT,"%s:%d failed exit policy. Closing.",
escaped_safe_str_client(conn->address), conn->port);
connection_edge_end(edge_conn, END_STREAM_REASON_EXITPOLICY);
diff --git a/src/or/connection_edge.h b/src/or/connection_edge.h
index f3d1038..39f492d 100644
--- a/src/or/connection_edge.h
+++ b/src/or/connection_edge.h
@@ -92,6 +92,11 @@ int connection_edge_update_circuit_isolation(const entry_connection_t *conn,
void circuit_clear_isolation(origin_circuit_t *circ);
#ifdef CONNECTION_EDGE_PRIVATE
+
+#define BEGIN_FLAG_IPV6_OK (1u<<0)
+#define BEGIN_FLAG_IPV4_NOT_OK (1u<<1)
+#define BEGIN_FLAG_IPV6_PREFERRED (1u<<2)
+
/*DOCDOC*/
typedef struct begin_cell_t {
char *address;
diff --git a/src/or/or.h b/src/or/or.h
index fa90aae..9d22e11 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -3745,6 +3745,8 @@ typedef struct {
int PathBiasScaleFactor;
/** @} */
+ int IPv6Exit; /**< DOCDOC*/
+
} or_options_t;
/** Persistent state for an onion router, as saved to disk. */
diff --git a/src/or/policies.c b/src/or/policies.c
index 16b3e4f..f9646f8 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -437,6 +437,7 @@ validate_addr_policies(const or_options_t *options, char **msg)
*msg = NULL;
if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
+ options->IPv6Exit,
options->ExitPolicyRejectPrivate, NULL,
!options->BridgeRelay))
REJECT("Error in ExitPolicy entry.");
@@ -938,9 +939,13 @@ exit_policy_remove_redundancies(smartlist_t *dest)
*/
int
policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
+ int ipv6_exit,
int rejectprivate, const char *local_address,
int add_default_policy)
{
+ if (!ipv6_exit) {
+ append_exit_policy_string(dest, "reject *6:*");
+ }
if (rejectprivate) {
append_exit_policy_string(dest, "reject private:*");
if (local_address) {
diff --git a/src/or/policies.h b/src/or/policies.h
index e9d214d..f8de911 100644
--- a/src/or/policies.h
+++ b/src/or/policies.h
@@ -43,6 +43,7 @@ addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr,
uint16_t port, const node_t *node);
int policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
+ int ipv6exit,
int rejectprivate, const char *local_address,
int add_default_policy);
void policies_exit_policy_append_reject_star(smartlist_t **dest);
diff --git a/src/or/router.c b/src/or/router.c
index 642656f..5e9f3d5 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1604,6 +1604,7 @@ router_rebuild_descriptor(int force)
policies_exit_policy_append_reject_star(&ri->exit_policy);
} else {
policies_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
+ options->IPv6Exit,
options->ExitPolicyRejectPrivate,
ri->address, !options->BridgeRelay);
}
diff --git a/src/test/test.c b/src/test/test.c
index 6652e77..d70f255 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1044,7 +1044,7 @@ test_policy_summary_helper(const char *policy_str,
line.value = (char *)policy_str;
line.next = NULL;
- r = policies_parse_exit_policy(&line, &policy, 0, NULL, 1);
+ r = policies_parse_exit_policy(&line, &policy, 1, 0, NULL, 1);
test_eq(r, 0);
summary = policy_summarize(policy, AF_INET);
@@ -1101,7 +1101,7 @@ test_policies(void)
test_assert(ADDR_POLICY_REJECTED ==
compare_tor_addr_to_addr_policy(&tar, 2, policy));
- test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, NULL, 1));
+ test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, 1, NULL, 1));
test_assert(policy2);
policy3 = smartlist_new();
@@ -1188,7 +1188,7 @@ test_policies(void)
line.key = (char*)"foo";
line.value = (char*)"accept *:80,reject private:*,reject *:*";
line.next = NULL;
- test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL, 1));
+ test_assert(0 == policies_parse_exit_policy(&line, &policy, 1, 0, NULL, 1));
test_assert(policy);
//test_streq(policy->string, "accept *:80");
//test_streq(policy->next->string, "reject *:*");
1
0

[tor/master] Refactor begin cell parsing into its own function, with tests.
by nickm@torproject.org 15 Nov '12
by nickm@torproject.org 15 Nov '12
15 Nov '12
commit 462ebb270a10f02573b1847649db45b94c0e0fc3
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Mon Oct 22 11:28:37 2012 -0400
Refactor begin cell parsing into its own function, with tests.
Add 'flags' argument to begin cells, per proposal 208.
---
src/or/connection_edge.c | 105 ++++++++++++++-----
src/or/connection_edge.h | 15 +++
src/test/include.am | 1 +
src/test/test.c | 2 +
src/test/test_cell_formats.c | 234 ++++++++++++++++++++++++++++++++++++++++++
5 files changed, 330 insertions(+), 27 deletions(-)
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index f548576..a840b43 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -8,6 +8,7 @@
* \file connection_edge.c
* \brief Handle edge streams.
**/
+#define CONNECTION_EDGE_PRIVATE
#include "or.h"
#include "addressmap.h"
@@ -2059,6 +2060,64 @@ connection_ap_handshake_socks_reply(entry_connection_t *conn, char *reply,
return;
}
+/* DOCDOC */
+/* static */ int
+begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
+ uint8_t *end_reason_out)
+{
+ relay_header_t rh;
+ const uint8_t *body, *nul;
+
+ memset(bcell, 0, sizeof(*bcell));
+ *end_reason_out = END_STREAM_REASON_MISC;
+
+ relay_header_unpack(&rh, cell->payload);
+ if (rh.length > RELAY_PAYLOAD_SIZE) {
+ return -2; /*XXXX why not TORPROTOL? */
+ }
+
+ bcell->stream_id = rh.stream_id;
+
+ if (rh.command == RELAY_COMMAND_BEGIN_DIR) {
+ bcell->is_begindir = 1;
+ return 0;
+ } else if (rh.command != RELAY_COMMAND_BEGIN) {
+ log_warn(LD_BUG, "Got an unexpected command %d", (int)rh.command);
+ *end_reason_out = END_STREAM_REASON_INTERNAL;
+ return -1;
+ }
+
+ body = cell->payload + RELAY_HEADER_SIZE;
+ nul = memchr(body, 0, rh.length);
+ if (! nul) {
+ log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
+ "Relay begin cell has no \\0. Closing.");
+ *end_reason_out = END_STREAM_REASON_TORPROTOCOL;
+ return -1;
+ }
+
+ if (tor_addr_port_split(LOG_PROTOCOL_WARN,
+ (char*)(body),
+ &bcell->address,&bcell->port)<0) {
+ log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
+ "Unable to parse addr:port in relay begin cell. Closing.");
+ *end_reason_out = END_STREAM_REASON_TORPROTOCOL;
+ return -1;
+ }
+ if (bcell->port == 0) {
+ log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
+ "Missing port in relay begin cell. Closing.");
+ tor_free(bcell->address);
+ *end_reason_out = END_STREAM_REASON_TORPROTOCOL;
+ return -1;
+ }
+ if (body + rh.length >= nul + 4)
+ bcell->flags = ntohl(get_uint32(nul+1));
+
+ return 0;
+}
+
+
/** A relay 'begin' or 'begin_dir' cell has arrived, and either we are
* an exit hop for the circuit, or we are the origin and it is a
* rendezvous begin.
@@ -2082,10 +2141,13 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
{
edge_connection_t *n_stream;
relay_header_t rh;
- char *address=NULL;
- uint16_t port;
+ char *address = NULL;
+ uint16_t port = 0;
or_circuit_t *or_circ = NULL;
const or_options_t *options = get_options();
+ begin_cell_t bcell;
+ int r;
+ uint8_t end_reason=0;
assert_circuit_ok(circ);
if (!CIRCUIT_IS_ORIGIN(circ))
@@ -2109,31 +2171,20 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
return 0;
}
- if (rh.command == RELAY_COMMAND_BEGIN) {
- if (!memchr(cell->payload+RELAY_HEADER_SIZE, 0, rh.length)) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Relay begin cell has no \\0. Closing.");
- relay_send_end_cell_from_edge(rh.stream_id, circ,
- END_STREAM_REASON_TORPROTOCOL, NULL);
- return 0;
- }
- if (tor_addr_port_split(LOG_PROTOCOL_WARN,
- (char*)(cell->payload+RELAY_HEADER_SIZE),
- &address,&port)<0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Unable to parse addr:port in relay begin cell. Closing.");
- relay_send_end_cell_from_edge(rh.stream_id, circ,
- END_STREAM_REASON_TORPROTOCOL, NULL);
- return 0;
- }
- if (port==0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Missing port in relay begin cell. Closing.");
- relay_send_end_cell_from_edge(rh.stream_id, circ,
- END_STREAM_REASON_TORPROTOCOL, NULL);
- tor_free(address);
- return 0;
- }
+ r = begin_cell_parse(cell, &bcell, &end_reason);
+ if (r < -1) {
+ return -1;
+ } else if (r == -1) {
+ tor_free(bcell.address);
+ relay_send_end_cell_from_edge(rh.stream_id, circ, end_reason, NULL);
+ return 0;
+ }
+
+ if (! bcell.is_begindir) {
+ /* Steal reference */
+ address = bcell.address;
+ port = bcell.port;
+
if (or_circ && or_circ->p_chan) {
if (!options->AllowSingleHopExits &&
(or_circ->is_first_hop ||
diff --git a/src/or/connection_edge.h b/src/or/connection_edge.h
index b5938b3..f3d1038 100644
--- a/src/or/connection_edge.h
+++ b/src/or/connection_edge.h
@@ -91,5 +91,20 @@ int connection_edge_update_circuit_isolation(const entry_connection_t *conn,
int dry_run);
void circuit_clear_isolation(origin_circuit_t *circ);
+#ifdef CONNECTION_EDGE_PRIVATE
+/*DOCDOC*/
+typedef struct begin_cell_t {
+ char *address;
+ uint32_t flags;
+ uint16_t port;
+ uint16_t stream_id;
+ unsigned is_begindir : 1;
+} begin_cell_t;
+
+int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
+ uint8_t *end_reason_out);
+#endif
+
+
#endif
diff --git a/src/test/include.am b/src/test/include.am
index bdfe498..075df36 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -14,6 +14,7 @@ src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
src_test_test_SOURCES = \
src/test/test.c \
src/test/test_addr.c \
+ src/test/test_cell_formats.c \
src/test/test_containers.c \
src/test/test_crypto.c \
src/test/test_data.c \
diff --git a/src/test/test.c b/src/test/test.c
index 1eaa65c..fc3c36e 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1983,6 +1983,7 @@ extern struct testcase_t pt_tests[];
extern struct testcase_t config_tests[];
extern struct testcase_t introduce_tests[];
extern struct testcase_t replaycache_tests[];
+extern struct testcase_t cell_format_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
@@ -1991,6 +1992,7 @@ static struct testgroup_t testgroups[] = {
{ "crypto/", crypto_tests },
{ "container/", container_tests },
{ "util/", util_tests },
+ { "cellfmt/", cell_format_tests },
{ "dir/", dir_tests },
{ "dir/md/", microdesc_tests },
{ "pt/", pt_tests },
diff --git a/src/test/test_cell_formats.c b/src/test/test_cell_formats.c
new file mode 100644
index 0000000..2199a05
--- /dev/null
+++ b/src/test/test_cell_formats.c
@@ -0,0 +1,234 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2012, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+
+#define CONNECTION_EDGE_PRIVATE
+#include "or.h"
+#include "connection_edge.h"
+#include "relay.h"
+#include "test.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+static void
+test_cfmt_relay_header(void *arg)
+{
+ relay_header_t rh;
+ const uint8_t hdr_1[RELAY_HEADER_SIZE] =
+ "\x03" "\x00\x00" "\x21\x22" "ABCD" "\x01\x03";
+ uint8_t hdr_out[RELAY_HEADER_SIZE];
+ (void)arg;
+
+ tt_int_op(sizeof(hdr_1), ==, RELAY_HEADER_SIZE);
+ relay_header_unpack(&rh, hdr_1);
+ tt_int_op(rh.command, ==, 3);
+ tt_int_op(rh.recognized, ==, 0);
+ tt_int_op(rh.stream_id, ==, 0x2122);
+ test_mem_op(rh.integrity, ==, "ABCD", 4);
+ tt_int_op(rh.length, ==, 0x103);
+
+ relay_header_pack(hdr_out, &rh);
+ test_mem_op(hdr_out, ==, hdr_1, RELAY_HEADER_SIZE);
+
+ done:
+ ;
+}
+
+static void
+make_relay_cell(cell_t *out, uint8_t command,
+ const void *body, size_t bodylen)
+{
+ relay_header_t rh;
+
+ memset(&rh, 0, sizeof(rh));
+ rh.stream_id = 5;
+ rh.command = command;
+ rh.length = bodylen;
+
+ out->command = CELL_RELAY;
+ out->circ_id = 10;
+ relay_header_pack(out->payload, &rh);
+
+ memcpy(out->payload + RELAY_HEADER_SIZE, body, bodylen);
+}
+
+static void
+test_cfmt_begin_cells(void *arg)
+{
+ cell_t cell;
+ begin_cell_t bcell;
+ uint8_t end_reason;
+ (void)arg;
+
+ /* Try begindir. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "", 0);
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_ptr_op(NULL, ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(0, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(1, ==, bcell.is_begindir);
+
+ /* A Begindir with extra stuff. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "12345", 5);
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_ptr_op(NULL, ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(0, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(1, ==, bcell.is_begindir);
+
+ /* A short but valid begin cell */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:9", 6);
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("a.b", ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(9, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* A significantly loner begin cell */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ {
+ const char c[] = "here-is-a-nice-long.hostname.com:65535";
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, strlen(c)+1);
+ }
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("here-is-a-nice-long.hostname.com", ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(65535, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* An IPv4 begin cell. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "18.9.22.169:80", 15);
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("18.9.22.169", ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(80, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* An IPv6 begin cell. Let's make sure we handle colons*/
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN,
+ "[2620::6b0:b:1a1a:0:26e5:480e]:80", 34);
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("[2620::6b0:b:1a1a:0:26e5:480e]", ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(80, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* a begin cell with extra junk but not enough for flags. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ {
+ const char c[] = "another.example.com:80\x00\x01\x02";
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
+ }
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("another.example.com", ==, bcell.address);
+ tt_int_op(0, ==, bcell.flags);
+ tt_int_op(80, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* a begin cell with flags. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ {
+ const char c[] = "another.example.com:443\x00\x01\x02\x03\x04";
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
+ }
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("another.example.com", ==, bcell.address);
+ tt_int_op(0x1020304, ==, bcell.flags);
+ tt_int_op(443, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* a begin cell with flags and even more cruft after that. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ {
+ const char c[] = "a-further.example.com:22\x00\xee\xaa\x00\xffHi mom";
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
+ }
+ tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ tt_str_op("a-further.example.com", ==, bcell.address);
+ tt_int_op(0xeeaa00ff, ==, bcell.flags);
+ tt_int_op(22, ==, bcell.port);
+ tt_int_op(5, ==, bcell.stream_id);
+ tt_int_op(0, ==, bcell.is_begindir);
+ tor_free(bcell.address);
+
+ /* bad begin cell: impossible length. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 7);
+ cell.payload[9] = 0x01; /* Set length to 510 */
+ cell.payload[10] = 0xfe;
+ {
+ relay_header_t rh;
+ relay_header_unpack(&rh, cell.payload);
+ tt_int_op(rh.length, ==, 510);
+ }
+ tt_int_op(-2, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ /* Bad begin cell: no body. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ /* bad begin cell: no body. */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ /* bad begin cell: no colon */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b", 4);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ /* bad begin cell: no ports */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:", 5);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ /* bad begin cell: bad port */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:xyz", 8);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:100000", 11);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ /* bad begin cell: no nul */
+ memset(&bcell, 0x7f, sizeof(bcell));
+ make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 6);
+ tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
+
+ done:
+ tor_free(bcell.address);
+}
+
+#define TEST(name, flags) \
+ { #name, test_cfmt_ ## name, flags, 0, NULL }
+
+struct testcase_t cell_format_tests[] = {
+ TEST(relay_header, 0),
+ TEST(begin_cells, 0),
+ END_OF_TESTCASES
+};
+
1
0

[tor/master] Make DNS callback pass IPv6 answers to dns_answer_found
by nickm@torproject.org 15 Nov '12
by nickm@torproject.org 15 Nov '12
15 Nov '12
commit b7843ca5544e741e74fe5f93fb49579e01b468c8
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sat Oct 27 16:34:49 2012 -0400
Make DNS callback pass IPv6 answers to dns_answer_found
Also, count ipv6 timeouts vs others. If we have too many ipv6
requests time out, then we could be degrading performance because of a
broken DNS server that ignores AAAA requests. Other cases in which
we never learn an AAAA address aren't so bad, since they don't slow
A (ipv4) answers down very much.
---
src/or/dns.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
src/or/dns.h | 1 +
2 files changed, 68 insertions(+), 7 deletions(-)
diff --git a/src/or/dns.c b/src/or/dns.c
index c590e01..3832421 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -1342,6 +1342,10 @@ configure_nameservers(int force)
return -1;
}
+static uint64_t n_ipv6_requests_made = 0;
+static uint64_t n_ipv6_timeouts = 0;
+static int dns_is_broken_for_ipv6 = 0;
+
/** For eventdns: Called when we get an answer for a request we launched.
* See eventdns.h for arguments; 'arg' holds the address we tried to resolve.
*/
@@ -1352,20 +1356,39 @@ evdns_callback(int result, char type, int count, int ttl, void *addresses,
char *string_address = arg;
uint8_t is_reverse = 0;
int status = DNS_RESOLVE_FAILED_PERMANENT;
- uint32_t addr = 0;
+ tor_addr_t addr;
const char *hostname = NULL;
int was_wildcarded = 0;
+ tor_addr_make_unspec(&addr); /*WRONG WRONG WRONG XXXX XXXXX IPV6 prop208*/
+
+ /* Keep track of whether IPv6 is working */
+ if (type == DNS_IPv6_AAAA) {
+ if (result == DNS_ERR_TIMEOUT) {
+ ++n_ipv6_timeouts;
+ }
+
+ if (n_ipv6_timeouts > 10 &&
+ n_ipv6_timeouts > n_ipv6_requests_made / 2) {
+ if (! dns_is_broken_for_ipv6) {
+ log_notice(LD_EXIT, "More than half of our IPv6 requests seem to "
+ "have timed out. I'm going to assume I can't get AAAA "
+ "responses.");
+ dns_is_broken_for_ipv6 = 1;
+ }
+ }
+ }
+
if (result == DNS_ERR_NONE) {
if (type == DNS_IPv4_A && count) {
char answer_buf[INET_NTOA_BUF_LEN+1];
struct in_addr in;
char *escaped_address;
uint32_t *addrs = addresses;
- in.s_addr = addrs[0];
- addr = ntohl(addrs[0]);
+ tor_addr_from_ipv4n(&addr, addrs[0]);
status = DNS_RESOLVE_SUCCEEDED;
- tor_inet_ntoa(&in, answer_buf, sizeof(answer_buf));
+
+ tor_addr_to_str(answer_buf, &addr, sizeof(answer_buf), 0);
escaped_address = esc_for_log(string_address);
if (answer_is_wildcarded(answer_buf)) {
@@ -1382,6 +1405,29 @@ evdns_callback(int result, char type, int count, int ttl, void *addresses,
escaped_safe_str(answer_buf));
}
tor_free(escaped_address);
+ } else if (type == DNS_IPv6_AAAA && count) {
+ char answer_buf[TOR_ADDR_BUF_LEN];
+ char *escaped_address;
+ struct in6_addr *addrs = addresses;
+ tor_addr_from_in6(&addr, &addrs[0]);
+ status = DNS_RESOLVE_SUCCEEDED;
+ tor_inet_ntop(AF_INET6, &addrs[0], answer_buf, sizeof(answer_buf));
+ escaped_address = esc_for_log(string_address);
+
+ if (answer_is_wildcarded(answer_buf)) {
+ log_debug(LD_EXIT, "eventdns said that %s resolves to ISP-hijacked "
+ "address %s; treating as a failure.",
+ safe_str(escaped_address),
+ escaped_safe_str(answer_buf));
+ was_wildcarded = 1;
+ tor_addr_make_unspec(&addr); /* WRONG WRONG ETC XXXXXXXX */
+ status = DNS_RESOLVE_FAILED_PERMANENT;
+ } else {
+ log_debug(LD_EXIT, "eventdns said that %s resolves to %s",
+ safe_str(escaped_address),
+ escaped_safe_str(answer_buf));
+ }
+ tor_free(escaped_address);
} else if (type == DNS_PTR && count) {
char *escaped_address;
is_reverse = 1;
@@ -1666,8 +1712,8 @@ launch_test_addresses(int fd, short event, void *args)
/* This situation is worse than the failure-hijacking situation. When this
* happens, we're no good for DNS requests at all, and we shouldn't really
* be an exit server.*/
- if (!options->ServerDNSTestAddresses)
- return;
+ if (options->ServerDNSTestAddresses) {
+
tor_assert(the_evdns_base);
SMARTLIST_FOREACH_BEGIN(options->ServerDNSTestAddresses,
const char *, address) {
@@ -1683,12 +1729,16 @@ launch_test_addresses(int fd, short event, void *args)
a = tor_strdup(address);
req = evdns_base_resolve_ipv6(the_evdns_base,
address, DNS_QUERY_NO_SEARCH, evdns_callback, a);
+ ++n_ipv6_requests_made;
if (!req) {
log_info(LD_EXIT, "eventdns rejected test address %s",
escaped_safe_str(address));
tor_free(a);
}
} SMARTLIST_FOREACH_END(address);
+
+ } /*XXXX REINDENT */
+
}
#define N_WILDCARD_CHECKS 2
@@ -1756,6 +1806,13 @@ dns_seems_to_be_broken(void)
return dns_is_completely_invalid;
}
+/** DOCDOC */
+int
+dns_seems_to_be_broken_for_ipv6(void)
+{
+ return dns_is_broken_for_ipv6;
+}
+
/** Forget what we've previously learned about our DNS servers' correctness. */
void
dns_reset_correctness_checks(void)
@@ -1765,6 +1822,8 @@ dns_reset_correctness_checks(void)
n_wildcard_requests = 0;
+ n_ipv6_requests_made = n_ipv6_timeouts = 0;
+
if (dns_wildcard_list) {
SMARTLIST_FOREACH(dns_wildcard_list, char *, cp, tor_free(cp));
smartlist_clear(dns_wildcard_list);
@@ -1775,7 +1834,8 @@ dns_reset_correctness_checks(void)
smartlist_clear(dns_wildcarded_test_address_list);
}
dns_wildcard_one_notice_given = dns_wildcard_notice_given =
- dns_wildcarded_test_address_notice_given = dns_is_completely_invalid = 0;
+ dns_wildcarded_test_address_notice_given = dns_is_completely_invalid =
+ dns_is_broken_for_ipv6 = 0;
}
/** Return true iff we have noticed that the dotted-quad <b>ip</b> has been
diff --git a/src/or/dns.h b/src/or/dns.h
index 441a6c3..d2f6614 100644
--- a/src/or/dns.h
+++ b/src/or/dns.h
@@ -24,6 +24,7 @@ void dns_cancel_pending_resolve(const char *question);
int dns_resolve(edge_connection_t *exitconn);
void dns_launch_correctness_checks(void);
int dns_seems_to_be_broken(void);
+int dns_seems_to_be_broken_for_ipv6(void);
void dns_reset_correctness_checks(void);
void dump_dns_mem_usage(int severity);
1
0
commit 7908ab209350782646dfc112d346ea75974b9dec
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sun Oct 28 16:13:58 2012 -0400
Move address map into its own file.
---
changes/split_addressmap | 3 +
src/or/addressmap.c | 958 ++++++++++++++++++++++++++++++++++++++++++++++
src/or/addressmap.h | 42 ++
src/or/buffers.c | 1 +
src/or/circuituse.c | 1 +
src/or/config.c | 1 +
src/or/connection_edge.c | 944 +---------------------------------------------
src/or/connection_edge.h | 24 --
src/or/control.c | 1 +
src/or/include.am | 2 +
src/or/main.c | 1 +
src/or/relay.c | 1 +
src/test/test_config.c | 1 +
13 files changed, 1013 insertions(+), 967 deletions(-)
diff --git a/changes/split_addressmap b/changes/split_addressmap
new file mode 100644
index 0000000..9f377c5
--- /dev/null
+++ b/changes/split_addressmap
@@ -0,0 +1,3 @@
+ o Code simplification and refactoring:
+ - Move the client-side address-map/virtual-address/DNS-cache code
+ out of connection_edge.c into a new addressmap.c module.
diff --git a/src/or/addressmap.c b/src/or/addressmap.c
new file mode 100644
index 0000000..fec83a7
--- /dev/null
+++ b/src/or/addressmap.c
@@ -0,0 +1,958 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2012, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+#include "addressmap.h"
+#include "circuituse.h"
+#include "config.h"
+#include "connection_edge.h"
+#include "control.h"
+#include "dns.h"
+#include "routerset.h"
+#include "nodelist.h"
+
+/** A client-side struct to remember requests to rewrite addresses
+ * to new addresses. These structs are stored in the hash table
+ * "addressmap" below.
+ *
+ * There are 5 ways to set an address mapping:
+ * - A MapAddress command from the controller [permanent]
+ * - An AddressMap directive in the torrc [permanent]
+ * - When a TrackHostExits torrc directive is triggered [temporary]
+ * - When a DNS resolve succeeds [temporary]
+ * - When a DNS resolve fails [temporary]
+ *
+ * When an addressmap request is made but one is already registered,
+ * the new one is replaced only if the currently registered one has
+ * no "new_address" (that is, it's in the process of DNS resolve),
+ * or if the new one is permanent (expires==0 or 1).
+ *
+ * (We overload the 'expires' field, using "0" for mappings set via
+ * the configuration file, "1" for mappings set from the control
+ * interface, and other values for DNS and TrackHostExit mappings that can
+ * expire.)
+ *
+ * A mapping may be 'wildcarded'. If "src_wildcard" is true, then
+ * any address that ends with a . followed by the key for this entry will
+ * get remapped by it. If "dst_wildcard" is also true, then only the
+ * matching suffix of such addresses will get replaced by new_address.
+ */
+typedef struct {
+ char *new_address;
+ time_t expires;
+ addressmap_entry_source_t source:3;
+ unsigned src_wildcard:1;
+ unsigned dst_wildcard:1;
+ short num_resolve_failures;
+} addressmap_entry_t;
+
+/** Entry for mapping addresses to which virtual address we mapped them to. */
+typedef struct {
+ char *ipv4_address;
+ char *hostname_address;
+} virtaddress_entry_t;
+
+/** A hash table to store client-side address rewrite instructions. */
+static strmap_t *addressmap=NULL;
+/**
+ * Table mapping addresses to which virtual address, if any, we
+ * assigned them to.
+ *
+ * We maintain the following invariant: if [A,B] is in
+ * virtaddress_reversemap, then B must be a virtual address, and [A,B]
+ * must be in addressmap. We do not require that the converse hold:
+ * if it fails, then we could end up mapping two virtual addresses to
+ * the same address, which is no disaster.
+ **/
+static strmap_t *virtaddress_reversemap=NULL;
+
+/** Initialize addressmap. */
+void
+addressmap_init(void)
+{
+ addressmap = strmap_new();
+ virtaddress_reversemap = strmap_new();
+}
+
+/** Free the memory associated with the addressmap entry <b>_ent</b>. */
+static void
+addressmap_ent_free(void *_ent)
+{
+ addressmap_entry_t *ent;
+ if (!_ent)
+ return;
+
+ ent = _ent;
+ tor_free(ent->new_address);
+ tor_free(ent);
+}
+
+/** Free storage held by a virtaddress_entry_t* entry in <b>ent</b>. */
+static void
+addressmap_virtaddress_ent_free(void *_ent)
+{
+ virtaddress_entry_t *ent;
+ if (!_ent)
+ return;
+
+ ent = _ent;
+ tor_free(ent->ipv4_address);
+ tor_free(ent->hostname_address);
+ tor_free(ent);
+}
+
+/** Free storage held by a virtaddress_entry_t* entry in <b>ent</b>. */
+static void
+addressmap_virtaddress_remove(const char *address, addressmap_entry_t *ent)
+{
+ if (ent && ent->new_address &&
+ address_is_in_virtual_range(ent->new_address)) {
+ virtaddress_entry_t *ve =
+ strmap_get(virtaddress_reversemap, ent->new_address);
+ /*log_fn(LOG_NOTICE,"remove reverse mapping for %s",ent->new_address);*/
+ if (ve) {
+ if (!strcmp(address, ve->ipv4_address))
+ tor_free(ve->ipv4_address);
+ if (!strcmp(address, ve->hostname_address))
+ tor_free(ve->hostname_address);
+ if (!ve->ipv4_address && !ve->hostname_address) {
+ tor_free(ve);
+ strmap_remove(virtaddress_reversemap, ent->new_address);
+ }
+ }
+ }
+}
+
+/** Remove <b>ent</b> (which must be mapped to by <b>address</b>) from the
+ * client address maps. */
+static void
+addressmap_ent_remove(const char *address, addressmap_entry_t *ent)
+{
+ addressmap_virtaddress_remove(address, ent);
+ addressmap_ent_free(ent);
+}
+
+/** Unregister all TrackHostExits mappings from any address to
+ * *.exitname.exit. */
+void
+clear_trackexithost_mappings(const char *exitname)
+{
+ char *suffix = NULL;
+ if (!addressmap || !exitname)
+ return;
+ tor_asprintf(&suffix, ".%s.exit", exitname);
+ tor_strlower(suffix);
+
+ STRMAP_FOREACH_MODIFY(addressmap, address, addressmap_entry_t *, ent) {
+ if (ent->source == ADDRMAPSRC_TRACKEXIT &&
+ !strcmpend(ent->new_address, suffix)) {
+ addressmap_ent_remove(address, ent);
+ MAP_DEL_CURRENT(address);
+ }
+ } STRMAP_FOREACH_END;
+
+ tor_free(suffix);
+}
+
+/** Remove all TRACKEXIT mappings from the addressmap for which the target
+ * host is unknown or no longer allowed, or for which the source address
+ * is no longer in trackexithosts. */
+void
+addressmap_clear_excluded_trackexithosts(const or_options_t *options)
+{
+ const routerset_t *allow_nodes = options->ExitNodes;
+ const routerset_t *exclude_nodes = options->ExcludeExitNodesUnion_;
+
+ if (!addressmap)
+ return;
+ if (routerset_is_empty(allow_nodes))
+ allow_nodes = NULL;
+ if (allow_nodes == NULL && routerset_is_empty(exclude_nodes))
+ return;
+
+ STRMAP_FOREACH_MODIFY(addressmap, address, addressmap_entry_t *, ent) {
+ size_t len;
+ const char *target = ent->new_address, *dot;
+ char *nodename;
+ const node_t *node;
+
+ if (!target) {
+ /* DNS resolving in progress */
+ continue;
+ } else if (strcmpend(target, ".exit")) {
+ /* Not a .exit mapping */
+ continue;
+ } else if (ent->source != ADDRMAPSRC_TRACKEXIT) {
+ /* Not a trackexit mapping. */
+ continue;
+ }
+ len = strlen(target);
+ if (len < 6)
+ continue; /* malformed. */
+ dot = target + len - 6; /* dot now points to just before .exit */
+ while (dot > target && *dot != '.')
+ dot--;
+ if (*dot == '.') dot++;
+ nodename = tor_strndup(dot, len-5-(dot-target));;
+ node = node_get_by_nickname(nodename, 0);
+ tor_free(nodename);
+ if (!node ||
+ (allow_nodes && !routerset_contains_node(allow_nodes, node)) ||
+ routerset_contains_node(exclude_nodes, node) ||
+ !hostname_in_track_host_exits(options, address)) {
+ /* We don't know this one, or we want to be rid of it. */
+ addressmap_ent_remove(address, ent);
+ MAP_DEL_CURRENT(address);
+ }
+ } STRMAP_FOREACH_END;
+}
+
+/** Remove all AUTOMAP mappings from the addressmap for which the
+ * source address no longer matches AutomapHostsSuffixes, which is
+ * no longer allowed by AutomapHostsOnResolve, or for which the
+ * target address is no longer in the virtual network. */
+void
+addressmap_clear_invalid_automaps(const or_options_t *options)
+{
+ int clear_all = !options->AutomapHostsOnResolve;
+ const smartlist_t *suffixes = options->AutomapHostsSuffixes;
+
+ if (!addressmap)
+ return;
+
+ if (!suffixes)
+ clear_all = 1; /* This should be impossible, but let's be sure. */
+
+ STRMAP_FOREACH_MODIFY(addressmap, src_address, addressmap_entry_t *, ent) {
+ int remove = clear_all;
+ if (ent->source != ADDRMAPSRC_AUTOMAP)
+ continue; /* not an automap mapping. */
+
+ if (!remove) {
+ int suffix_found = 0;
+ SMARTLIST_FOREACH(suffixes, const char *, suffix, {
+ if (!strcasecmpend(src_address, suffix)) {
+ suffix_found = 1;
+ break;
+ }
+ });
+ if (!suffix_found)
+ remove = 1;
+ }
+
+ if (!remove && ! address_is_in_virtual_range(ent->new_address))
+ remove = 1;
+
+ if (remove) {
+ addressmap_ent_remove(src_address, ent);
+ MAP_DEL_CURRENT(src_address);
+ }
+ } STRMAP_FOREACH_END;
+}
+
+/** Remove all entries from the addressmap that were set via the
+ * configuration file or the command line. */
+void
+addressmap_clear_configured(void)
+{
+ addressmap_get_mappings(NULL, 0, 0, 0);
+}
+
+/** Remove all entries from the addressmap that are set to expire, ever. */
+void
+addressmap_clear_transient(void)
+{
+ addressmap_get_mappings(NULL, 2, TIME_MAX, 0);
+}
+
+/** Clean out entries from the addressmap cache that were
+ * added long enough ago that they are no longer valid.
+ */
+void
+addressmap_clean(time_t now)
+{
+ addressmap_get_mappings(NULL, 2, now, 0);
+}
+
+/** Free all the elements in the addressmap, and free the addressmap
+ * itself. */
+void
+addressmap_free_all(void)
+{
+ strmap_free(addressmap, addressmap_ent_free);
+ addressmap = NULL;
+
+ strmap_free(virtaddress_reversemap, addressmap_virtaddress_ent_free);
+ virtaddress_reversemap = NULL;
+}
+
+/** Try to find a match for AddressMap expressions that use
+ * wildcard notation such as '*.c.d *.e.f' (so 'a.c.d' will map to 'a.e.f') or
+ * '*.c.d a.b.c' (so 'a.c.d' will map to a.b.c).
+ * Return the matching entry in AddressMap or NULL if no match is found.
+ * For expressions such as '*.c.d *.e.f', truncate <b>address</b> 'a.c.d'
+ * to 'a' before we return the matching AddressMap entry.
+ *
+ * This function does not handle the case where a pattern of the form "*.c.d"
+ * matches the address c.d -- that's done by the main addressmap_rewrite
+ * function.
+ */
+static addressmap_entry_t *
+addressmap_match_superdomains(char *address)
+{
+ addressmap_entry_t *val;
+ char *cp;
+
+ cp = address;
+ while ((cp = strchr(cp, '.'))) {
+ /* cp now points to a suffix of address that begins with a . */
+ val = strmap_get_lc(addressmap, cp+1);
+ if (val && val->src_wildcard) {
+ if (val->dst_wildcard)
+ *cp = '\0';
+ return val;
+ }
+ ++cp;
+ }
+ return NULL;
+}
+
+/** Look at address, and rewrite it until it doesn't want any
+ * more rewrites; but don't get into an infinite loop.
+ * Don't write more than maxlen chars into address. Return true if the
+ * address changed; false otherwise. Set *<b>expires_out</b> to the
+ * expiry time of the result, or to <b>time_max</b> if the result does
+ * not expire.
+ *
+ * If <b>exit_source_out</b> is non-null, we set it as follows. If we the
+ * address starts out as a non-exit address, and we remap it to an .exit
+ * address at any point, then set *<b>exit_source_out</b> to the
+ * address_entry_source_t of the first such rule. Set *<b>exit_source_out</b>
+ * to ADDRMAPSRC_NONE if there is no such rewrite, or if the original address
+ * was a .exit.
+ */
+int
+addressmap_rewrite(char *address, size_t maxlen, time_t *expires_out,
+ addressmap_entry_source_t *exit_source_out)
+{
+ addressmap_entry_t *ent;
+ int rewrites;
+ time_t expires = TIME_MAX;
+ addressmap_entry_source_t exit_source = ADDRMAPSRC_NONE;
+ char *addr_orig = tor_strdup(address);
+ char *log_addr_orig = NULL;
+
+ for (rewrites = 0; rewrites < 16; rewrites++) {
+ int exact_match = 0;
+ log_addr_orig = tor_strdup(escaped_safe_str_client(address));
+
+ ent = strmap_get(addressmap, address);
+
+ if (!ent || !ent->new_address) {
+ ent = addressmap_match_superdomains(address);
+ } else {
+ if (ent->src_wildcard && !ent->dst_wildcard &&
+ !strcasecmp(address, ent->new_address)) {
+ /* This is a rule like *.example.com example.com, and we just got
+ * "example.com" */
+ goto done;
+ }
+
+ exact_match = 1;
+ }
+
+ if (!ent || !ent->new_address) {
+ goto done;
+ }
+
+ if (ent->dst_wildcard && !exact_match) {
+ strlcat(address, ".", maxlen);
+ strlcat(address, ent->new_address, maxlen);
+ } else {
+ strlcpy(address, ent->new_address, maxlen);
+ }
+
+ if (!strcmpend(address, ".exit") &&
+ strcmpend(addr_orig, ".exit") &&
+ exit_source == ADDRMAPSRC_NONE) {
+ exit_source = ent->source;
+ }
+
+ log_info(LD_APP, "Addressmap: rewriting %s to %s",
+ log_addr_orig, escaped_safe_str_client(address));
+ if (ent->expires > 1 && ent->expires < expires)
+ expires = ent->expires;
+
+ tor_free(log_addr_orig);
+ }
+ log_warn(LD_CONFIG,
+ "Loop detected: we've rewritten %s 16 times! Using it as-is.",
+ escaped_safe_str_client(address));
+ /* it's fine to rewrite a rewrite, but don't loop forever */
+
+ done:
+ tor_free(addr_orig);
+ tor_free(log_addr_orig);
+ if (exit_source_out)
+ *exit_source_out = exit_source;
+ if (expires_out)
+ *expires_out = TIME_MAX;
+ return (rewrites > 0);
+}
+
+/** If we have a cached reverse DNS entry for the address stored in the
+ * <b>maxlen</b>-byte buffer <b>address</b> (typically, a dotted quad) then
+ * rewrite to the cached value and return 1. Otherwise return 0. Set
+ * *<b>expires_out</b> to the expiry time of the result, or to <b>time_max</b>
+ * if the result does not expire. */
+int
+addressmap_rewrite_reverse(char *address, size_t maxlen, time_t *expires_out)
+{
+ char *s, *cp;
+ addressmap_entry_t *ent;
+ int r = 0;
+ tor_asprintf(&s, "REVERSE[%s]", address);
+ ent = strmap_get(addressmap, s);
+ if (ent) {
+ cp = tor_strdup(escaped_safe_str_client(ent->new_address));
+ log_info(LD_APP, "Rewrote reverse lookup %s -> %s",
+ escaped_safe_str_client(s), cp);
+ tor_free(cp);
+ strlcpy(address, ent->new_address, maxlen);
+ r = 1;
+ }
+
+ if (expires_out)
+ *expires_out = (ent && ent->expires > 1) ? ent->expires : TIME_MAX;
+
+ tor_free(s);
+ return r;
+}
+
+/** Return 1 if <b>address</b> is already registered, else return 0. If address
+ * is already registered, and <b>update_expires</b> is non-zero, then update
+ * the expiry time on the mapping with update_expires if it is a
+ * mapping created by TrackHostExits. */
+int
+addressmap_have_mapping(const char *address, int update_expiry)
+{
+ addressmap_entry_t *ent;
+ if (!(ent=strmap_get_lc(addressmap, address)))
+ return 0;
+ if (update_expiry && ent->source==ADDRMAPSRC_TRACKEXIT)
+ ent->expires=time(NULL) + update_expiry;
+ return 1;
+}
+
+/** Register a request to map <b>address</b> to <b>new_address</b>,
+ * which will expire on <b>expires</b> (or 0 if never expires from
+ * config file, 1 if never expires from controller, 2 if never expires
+ * (virtual address mapping) from the controller.)
+ *
+ * <b>new_address</b> should be a newly dup'ed string, which we'll use or
+ * free as appropriate. We will leave address alone.
+ *
+ * If <b>wildcard_addr</b> is true, then the mapping will match any address
+ * equal to <b>address</b>, or any address ending with a period followed by
+ * <b>address</b>. If <b>wildcard_addr</b> and <b>wildcard_new_addr</b> are
+ * both true, the mapping will rewrite addresses that end with
+ * ".<b>address</b>" into ones that end with ".<b>new_address</b>."
+ *
+ * If <b>new_address</b> is NULL, or <b>new_address</b> is equal to
+ * <b>address</b> and <b>wildcard_addr</b> is equal to
+ * <b>wildcard_new_addr</b>, remove any mappings that exist from
+ * <b>address</b>.
+ *
+ *
+ * It is an error to set <b>wildcard_new_addr</b> if <b>wildcard_addr</b> is
+ * not set. */
+void
+addressmap_register(const char *address, char *new_address, time_t expires,
+ addressmap_entry_source_t source,
+ const int wildcard_addr,
+ const int wildcard_new_addr)
+{
+ addressmap_entry_t *ent;
+
+ if (wildcard_new_addr)
+ tor_assert(wildcard_addr);
+
+ ent = strmap_get(addressmap, address);
+ if (!new_address || (!strcasecmp(address,new_address) &&
+ wildcard_addr == wildcard_new_addr)) {
+ /* Remove the mapping, if any. */
+ tor_free(new_address);
+ if (ent) {
+ addressmap_ent_remove(address,ent);
+ strmap_remove(addressmap, address);
+ }
+ return;
+ }
+ if (!ent) { /* make a new one and register it */
+ ent = tor_malloc_zero(sizeof(addressmap_entry_t));
+ strmap_set(addressmap, address, ent);
+ } else if (ent->new_address) { /* we need to clean up the old mapping. */
+ if (expires > 1) {
+ log_info(LD_APP,"Temporary addressmap ('%s' to '%s') not performed, "
+ "since it's already mapped to '%s'",
+ safe_str_client(address),
+ safe_str_client(new_address),
+ safe_str_client(ent->new_address));
+ tor_free(new_address);
+ return;
+ }
+ if (address_is_in_virtual_range(ent->new_address) &&
+ expires != 2) {
+ /* XXX This isn't the perfect test; we want to avoid removing
+ * mappings set from the control interface _as virtual mapping */
+ addressmap_virtaddress_remove(address, ent);
+ }
+ tor_free(ent->new_address);
+ } /* else { we have an in-progress resolve with no mapping. } */
+
+ ent->new_address = new_address;
+ ent->expires = expires==2 ? 1 : expires;
+ ent->num_resolve_failures = 0;
+ ent->source = source;
+ ent->src_wildcard = wildcard_addr ? 1 : 0;
+ ent->dst_wildcard = wildcard_new_addr ? 1 : 0;
+
+ log_info(LD_CONFIG, "Addressmap: (re)mapped '%s' to '%s'",
+ safe_str_client(address),
+ safe_str_client(ent->new_address));
+ control_event_address_mapped(address, ent->new_address, expires, NULL);
+}
+
+/** An attempt to resolve <b>address</b> failed at some OR.
+ * Increment the number of resolve failures we have on record
+ * for it, and then return that number.
+ */
+int
+client_dns_incr_failures(const char *address)
+{
+ addressmap_entry_t *ent = strmap_get(addressmap, address);
+ if (!ent) {
+ ent = tor_malloc_zero(sizeof(addressmap_entry_t));
+ ent->expires = time(NULL) + MAX_DNS_ENTRY_AGE;
+ strmap_set(addressmap,address,ent);
+ }
+ if (ent->num_resolve_failures < SHORT_MAX)
+ ++ent->num_resolve_failures; /* don't overflow */
+ log_info(LD_APP, "Address %s now has %d resolve failures.",
+ safe_str_client(address),
+ ent->num_resolve_failures);
+ return ent->num_resolve_failures;
+}
+
+/** If <b>address</b> is in the client DNS addressmap, reset
+ * the number of resolve failures we have on record for it.
+ * This is used when we fail a stream because it won't resolve:
+ * otherwise future attempts on that address will only try once.
+ */
+void
+client_dns_clear_failures(const char *address)
+{
+ addressmap_entry_t *ent = strmap_get(addressmap, address);
+ if (ent)
+ ent->num_resolve_failures = 0;
+}
+
+/** Record the fact that <b>address</b> resolved to <b>name</b>.
+ * We can now use this in subsequent streams via addressmap_rewrite()
+ * so we can more correctly choose an exit that will allow <b>address</b>.
+ *
+ * If <b>exitname</b> is defined, then append the addresses with
+ * ".exitname.exit" before registering the mapping.
+ *
+ * If <b>ttl</b> is nonnegative, the mapping will be valid for
+ * <b>ttl</b>seconds; otherwise, we use the default.
+ */
+static void
+client_dns_set_addressmap_impl(const char *address, const char *name,
+ const char *exitname,
+ int ttl)
+{
+ /* <address>.<hex or nickname>.exit\0 or just <address>\0 */
+ char extendedaddress[MAX_SOCKS_ADDR_LEN+MAX_VERBOSE_NICKNAME_LEN+10];
+ /* 123.123.123.123.<hex or nickname>.exit\0 or just 123.123.123.123\0 */
+ char extendedval[INET_NTOA_BUF_LEN+MAX_VERBOSE_NICKNAME_LEN+10];
+
+ tor_assert(address);
+ tor_assert(name);
+
+ if (ttl<0)
+ ttl = DEFAULT_DNS_TTL;
+ else
+ ttl = dns_clip_ttl(ttl);
+
+ if (exitname) {
+ /* XXXX fails to ever get attempts to get an exit address of
+ * google.com.digest[=~]nickname.exit; we need a syntax for this that
+ * won't make strict RFC952-compliant applications (like us) barf. */
+ tor_snprintf(extendedaddress, sizeof(extendedaddress),
+ "%s.%s.exit", address, exitname);
+ tor_snprintf(extendedval, sizeof(extendedval),
+ "%s.%s.exit", name, exitname);
+ } else {
+ tor_snprintf(extendedaddress, sizeof(extendedaddress),
+ "%s", address);
+ tor_snprintf(extendedval, sizeof(extendedval),
+ "%s", name);
+ }
+ addressmap_register(extendedaddress, tor_strdup(extendedval),
+ time(NULL) + ttl, ADDRMAPSRC_DNS, 0, 0);
+}
+
+/** Record the fact that <b>address</b> resolved to <b>val</b>.
+ * We can now use this in subsequent streams via addressmap_rewrite()
+ * so we can more correctly choose an exit that will allow <b>address</b>.
+ *
+ * If <b>exitname</b> is defined, then append the addresses with
+ * ".exitname.exit" before registering the mapping.
+ *
+ * If <b>ttl</b> is nonnegative, the mapping will be valid for
+ * <b>ttl</b>seconds; otherwise, we use the default.
+ */
+void
+client_dns_set_addressmap(const char *address, uint32_t val,
+ const char *exitname,
+ int ttl)
+{
+ struct in_addr in;
+ char valbuf[INET_NTOA_BUF_LEN];
+
+ tor_assert(address);
+
+ if (tor_inet_aton(address, &in))
+ return; /* If address was an IP address already, don't add a mapping. */
+ in.s_addr = htonl(val);
+ tor_inet_ntoa(&in,valbuf,sizeof(valbuf));
+
+ client_dns_set_addressmap_impl(address, valbuf, exitname, ttl);
+}
+
+/** Add a cache entry noting that <b>address</b> (ordinarily a dotted quad)
+ * resolved via a RESOLVE_PTR request to the hostname <b>v</b>.
+ *
+ * If <b>exitname</b> is defined, then append the addresses with
+ * ".exitname.exit" before registering the mapping.
+ *
+ * If <b>ttl</b> is nonnegative, the mapping will be valid for
+ * <b>ttl</b>seconds; otherwise, we use the default.
+ */
+void
+client_dns_set_reverse_addressmap(const char *address, const char *v,
+ const char *exitname,
+ int ttl)
+{
+ char *s = NULL;
+ tor_asprintf(&s, "REVERSE[%s]", address);
+ client_dns_set_addressmap_impl(s, v, exitname, ttl);
+ tor_free(s);
+}
+
+/* By default, we hand out 127.192.0.1 through 127.254.254.254.
+ * These addresses should map to localhost, so even if the
+ * application accidentally tried to connect to them directly (not
+ * via Tor), it wouldn't get too far astray.
+ *
+ * These options are configured by parse_virtual_addr_network().
+ */
+/** Which network should we use for virtual IPv4 addresses? Only the first
+ * bits of this value are fixed. */
+static uint32_t virtual_addr_network = 0x7fc00000u;
+/** How many bits of <b>virtual_addr_network</b> are fixed? */
+static maskbits_t virtual_addr_netmask_bits = 10;
+/** What's the next virtual address we will hand out? */
+static uint32_t next_virtual_addr = 0x7fc00000u;
+
+/** Read a netmask of the form 127.192.0.0/10 from "val", and check whether
+ * it's a valid set of virtual addresses to hand out in response to MAPADDRESS
+ * requests. Return 0 on success; set *msg (if provided) to a newly allocated
+ * string and return -1 on failure. If validate_only is false, sets the
+ * actual virtual address range to the parsed value. */
+int
+parse_virtual_addr_network(const char *val, int validate_only,
+ char **msg)
+{
+ uint32_t addr;
+ uint16_t port_min, port_max;
+ maskbits_t bits;
+
+ if (parse_addr_and_port_range(val, &addr, &bits, &port_min, &port_max)) {
+ if (msg) *msg = tor_strdup("Error parsing VirtualAddressNetwork");
+ return -1;
+ }
+
+ if (port_min != 1 || port_max != 65535) {
+ if (msg) *msg = tor_strdup("Can't specify ports on VirtualAddressNetwork");
+ return -1;
+ }
+
+ if (bits > 16) {
+ if (msg) *msg = tor_strdup("VirtualAddressNetwork expects a /16 "
+ "network or larger");
+ return -1;
+ }
+
+ if (validate_only)
+ return 0;
+
+ virtual_addr_network = (uint32_t)( addr & (0xfffffffful << (32-bits)) );
+ virtual_addr_netmask_bits = bits;
+
+ if (addr_mask_cmp_bits(next_virtual_addr, addr, bits))
+ next_virtual_addr = addr;
+
+ return 0;
+}
+
+/**
+ * Return true iff <b>addr</b> is likely to have been returned by
+ * client_dns_get_unused_address.
+ **/
+int
+address_is_in_virtual_range(const char *address)
+{
+ struct in_addr in;
+ tor_assert(address);
+ if (!strcasecmpend(address, ".virtual")) {
+ return 1;
+ } else if (tor_inet_aton(address, &in)) {
+ uint32_t addr = ntohl(in.s_addr);
+ if (!addr_mask_cmp_bits(addr, virtual_addr_network,
+ virtual_addr_netmask_bits))
+ return 1;
+ }
+ return 0;
+}
+
+/** Increment the value of next_virtual_addr; reset it to the start of the
+ * virtual address range if it wraps around.
+ */
+static INLINE void
+increment_virtual_addr(void)
+{
+ ++next_virtual_addr;
+ if (addr_mask_cmp_bits(next_virtual_addr, virtual_addr_network,
+ virtual_addr_netmask_bits))
+ next_virtual_addr = virtual_addr_network;
+}
+
+/** Return a newly allocated string holding an address of <b>type</b>
+ * (one of RESOLVED_TYPE_{IPV4|HOSTNAME}) that has not yet been mapped,
+ * and that is very unlikely to be the address of any real host.
+ *
+ * May return NULL if we have run out of virtual addresses.
+ */
+static char *
+addressmap_get_virtual_address(int type)
+{
+ char buf[64];
+ tor_assert(addressmap);
+
+ if (type == RESOLVED_TYPE_HOSTNAME) {
+ char rand[10];
+ do {
+ crypto_rand(rand, sizeof(rand));
+ base32_encode(buf,sizeof(buf),rand,sizeof(rand));
+ strlcat(buf, ".virtual", sizeof(buf));
+ } while (strmap_get(addressmap, buf));
+ return tor_strdup(buf);
+ } else if (type == RESOLVED_TYPE_IPV4) {
+ // This is an imperfect estimate of how many addresses are available, but
+ // that's ok.
+ struct in_addr in;
+ uint32_t available = 1u << (32-virtual_addr_netmask_bits);
+ while (available) {
+ /* Don't hand out any .0 or .255 address. */
+ while ((next_virtual_addr & 0xff) == 0 ||
+ (next_virtual_addr & 0xff) == 0xff) {
+ increment_virtual_addr();
+ if (! --available) {
+ log_warn(LD_CONFIG, "Ran out of virtual addresses!");
+ return NULL;
+ }
+ }
+ in.s_addr = htonl(next_virtual_addr);
+ tor_inet_ntoa(&in, buf, sizeof(buf));
+ if (!strmap_get(addressmap, buf)) {
+ increment_virtual_addr();
+ break;
+ }
+
+ increment_virtual_addr();
+ --available;
+ // log_info(LD_CONFIG, "%d addrs available", (int)available);
+ if (! available) {
+ log_warn(LD_CONFIG, "Ran out of virtual addresses!");
+ return NULL;
+ }
+ }
+ return tor_strdup(buf);
+ } else {
+ log_warn(LD_BUG, "Called with unsupported address type (%d)", type);
+ return NULL;
+ }
+}
+
+/** A controller has requested that we map some address of type
+ * <b>type</b> to the address <b>new_address</b>. Choose an address
+ * that is unlikely to be used, and map it, and return it in a newly
+ * allocated string. If another address of the same type is already
+ * mapped to <b>new_address</b>, try to return a copy of that address.
+ *
+ * The string in <b>new_address</b> may be freed or inserted into a map
+ * as appropriate. May return NULL if are out of virtual addresses.
+ **/
+const char *
+addressmap_register_virtual_address(int type, char *new_address)
+{
+ char **addrp;
+ virtaddress_entry_t *vent;
+ int vent_needs_to_be_added = 0;
+
+ tor_assert(new_address);
+ tor_assert(addressmap);
+ tor_assert(virtaddress_reversemap);
+
+ vent = strmap_get(virtaddress_reversemap, new_address);
+ if (!vent) {
+ vent = tor_malloc_zero(sizeof(virtaddress_entry_t));
+ vent_needs_to_be_added = 1;
+ }
+
+ addrp = (type == RESOLVED_TYPE_IPV4) ?
+ &vent->ipv4_address : &vent->hostname_address;
+ if (*addrp) {
+ addressmap_entry_t *ent = strmap_get(addressmap, *addrp);
+ if (ent && ent->new_address &&
+ !strcasecmp(new_address, ent->new_address)) {
+ tor_free(new_address);
+ tor_assert(!vent_needs_to_be_added);
+ return tor_strdup(*addrp);
+ } else
+ log_warn(LD_BUG,
+ "Internal confusion: I thought that '%s' was mapped to by "
+ "'%s', but '%s' really maps to '%s'. This is a harmless bug.",
+ safe_str_client(new_address),
+ safe_str_client(*addrp),
+ safe_str_client(*addrp),
+ ent?safe_str_client(ent->new_address):"(nothing)");
+ }
+
+ tor_free(*addrp);
+ *addrp = addressmap_get_virtual_address(type);
+ if (!*addrp) {
+ tor_free(vent);
+ tor_free(new_address);
+ return NULL;
+ }
+ log_info(LD_APP, "Registering map from %s to %s", *addrp, new_address);
+ if (vent_needs_to_be_added)
+ strmap_set(virtaddress_reversemap, new_address, vent);
+ addressmap_register(*addrp, new_address, 2, ADDRMAPSRC_AUTOMAP, 0, 0);
+
+#if 0
+ {
+ /* Try to catch possible bugs */
+ addressmap_entry_t *ent;
+ ent = strmap_get(addressmap, *addrp);
+ tor_assert(ent);
+ tor_assert(!strcasecmp(ent->new_address,new_address));
+ vent = strmap_get(virtaddress_reversemap, new_address);
+ tor_assert(vent);
+ tor_assert(!strcasecmp(*addrp,
+ (type == RESOLVED_TYPE_IPV4) ?
+ vent->ipv4_address : vent->hostname_address));
+ log_info(LD_APP, "Map from %s to %s okay.",
+ safe_str_client(*addrp),
+ safe_str_client(new_address));
+ }
+#endif
+
+ return *addrp;
+}
+
+/** Return 1 if <b>address</b> has funny characters in it like colons. Return
+ * 0 if it's fine, or if we're configured to allow it anyway. <b>client</b>
+ * should be true if we're using this address as a client; false if we're
+ * using it as a server.
+ */
+int
+address_is_invalid_destination(const char *address, int client)
+{
+ if (client) {
+ if (get_options()->AllowNonRFC953Hostnames)
+ return 0;
+ } else {
+ if (get_options()->ServerDNSAllowNonRFC953Hostnames)
+ return 0;
+ }
+
+ while (*address) {
+ if (TOR_ISALNUM(*address) ||
+ *address == '-' ||
+ *address == '.' ||
+ *address == '_') /* Underscore is not allowed, but Windows does it
+ * sometimes, just to thumb its nose at the IETF. */
+ ++address;
+ else
+ return 1;
+ }
+ return 0;
+}
+
+/** Iterate over all address mappings which have expiry times between
+ * min_expires and max_expires, inclusive. If sl is provided, add an
+ * "old-addr new-addr expiry" string to sl for each mapping, omitting
+ * the expiry time if want_expiry is false. If sl is NULL, remove the
+ * mappings.
+ */
+void
+addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
+ time_t max_expires, int want_expiry)
+{
+ strmap_iter_t *iter;
+ const char *key;
+ void *val_;
+ addressmap_entry_t *val;
+
+ if (!addressmap)
+ addressmap_init();
+
+ for (iter = strmap_iter_init(addressmap); !strmap_iter_done(iter); ) {
+ strmap_iter_get(iter, &key, &val_);
+ val = val_;
+ if (val->expires >= min_expires && val->expires <= max_expires) {
+ if (!sl) {
+ iter = strmap_iter_next_rmv(addressmap,iter);
+ addressmap_ent_remove(key, val);
+ continue;
+ } else if (val->new_address) {
+ const char *src_wc = val->src_wildcard ? "*." : "";
+ const char *dst_wc = val->dst_wildcard ? "*." : "";
+ if (want_expiry) {
+ if (val->expires < 3 || val->expires == TIME_MAX)
+ smartlist_add_asprintf(sl, "%s%s %s%s NEVER",
+ src_wc, key, dst_wc, val->new_address);
+ else {
+ char time[ISO_TIME_LEN+1];
+ format_iso_time(time, val->expires);
+ smartlist_add_asprintf(sl, "%s%s %s%s \"%s\"",
+ src_wc, key, dst_wc, val->new_address,
+ time);
+ }
+ } else {
+ smartlist_add_asprintf(sl, "%s%s %s%s",
+ src_wc, key, dst_wc, val->new_address);
+ }
+ }
+ }
+ iter = strmap_iter_next(addressmap,iter);
+ }
+}
+
diff --git a/src/or/addressmap.h b/src/or/addressmap.h
new file mode 100644
index 0000000..911c7ac
--- /dev/null
+++ b/src/or/addressmap.h
@@ -0,0 +1,42 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2012, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_ADDRESSMAP_H
+#define TOR_ADDRESSMAP_H
+
+void addressmap_init(void);
+void addressmap_clear_excluded_trackexithosts(const or_options_t *options);
+void addressmap_clear_invalid_automaps(const or_options_t *options);
+void addressmap_clean(time_t now);
+void addressmap_clear_configured(void);
+void addressmap_clear_transient(void);
+void addressmap_free_all(void);
+int addressmap_rewrite(char *address, size_t maxlen, time_t *expires_out,
+ addressmap_entry_source_t *exit_source_out);
+int addressmap_rewrite_reverse(char *address, size_t maxlen,
+ time_t *expires_out);
+int addressmap_have_mapping(const char *address, int update_timeout);
+
+void addressmap_register(const char *address, char *new_address,
+ time_t expires, addressmap_entry_source_t source,
+ const int address_wildcard,
+ const int new_address_wildcard);
+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(const char *address, uint32_t val,
+ const char *exitname, int ttl);
+const char *addressmap_register_virtual_address(int type, char *new_address);
+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(const char *address, const char *v,
+ const char *exitname, int ttl);
+
+#endif
+
diff --git a/src/or/buffers.c b/src/or/buffers.c
index f03047a..3a1b4d5 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -12,6 +12,7 @@
**/
#define BUFFERS_PRIVATE
#include "or.h"
+#include "addressmap.h"
#include "buffers.h"
#include "config.h"
#include "connection_edge.h"
diff --git a/src/or/circuituse.c b/src/or/circuituse.c
index ded7855..d3cde1d 100644
--- a/src/or/circuituse.c
+++ b/src/or/circuituse.c
@@ -10,6 +10,7 @@
**/
#include "or.h"
+#include "addressmap.h"
#include "channel.h"
#include "circuitbuild.h"
#include "circuitlist.h"
diff --git a/src/or/config.c b/src/or/config.c
index bf32fae..9af55e9 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -12,6 +12,7 @@
#define CONFIG_PRIVATE
#include "or.h"
+#include "addressmap.h"
#include "channel.h"
#include "circuitbuild.h"
#include "circuitlist.h"
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index 162de2e..1ee88f3 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -10,6 +10,7 @@
**/
#include "or.h"
+#include "addressmap.h"
#include "buffers.h"
#include "channel.h"
#include "circuitlist.h"
@@ -56,9 +57,7 @@
static int connection_ap_handshake_process_socks(entry_connection_t *conn);
static int connection_ap_process_natd(entry_connection_t *conn);
static int connection_exit_connect_dir(edge_connection_t *exitconn);
-static int address_is_in_virtual_range(const char *addr);
static int consider_plaintext_ports(entry_connection_t *conn, uint16_t port);
-static void clear_trackexithost_mappings(const char *exitname);
static int connection_ap_supports_optimistic_data(const entry_connection_t *);
/** An AP stream has failed/finished. If it hasn't already sent back
@@ -784,947 +783,6 @@ connection_ap_detach_retriable(entry_connection_t *conn,
}
}
-/** A client-side struct to remember requests to rewrite addresses
- * to new addresses. These structs are stored in the hash table
- * "addressmap" below.
- *
- * There are 5 ways to set an address mapping:
- * - A MapAddress command from the controller [permanent]
- * - An AddressMap directive in the torrc [permanent]
- * - When a TrackHostExits torrc directive is triggered [temporary]
- * - When a DNS resolve succeeds [temporary]
- * - When a DNS resolve fails [temporary]
- *
- * When an addressmap request is made but one is already registered,
- * the new one is replaced only if the currently registered one has
- * no "new_address" (that is, it's in the process of DNS resolve),
- * or if the new one is permanent (expires==0 or 1).
- *
- * (We overload the 'expires' field, using "0" for mappings set via
- * the configuration file, "1" for mappings set from the control
- * interface, and other values for DNS and TrackHostExit mappings that can
- * expire.)
- *
- * A mapping may be 'wildcarded'. If "src_wildcard" is true, then
- * any address that ends with a . followed by the key for this entry will
- * get remapped by it. If "dst_wildcard" is also true, then only the
- * matching suffix of such addresses will get replaced by new_address.
- */
-typedef struct {
- char *new_address;
- time_t expires;
- addressmap_entry_source_t source:3;
- unsigned src_wildcard:1;
- unsigned dst_wildcard:1;
- short num_resolve_failures;
-} addressmap_entry_t;
-
-/** Entry for mapping addresses to which virtual address we mapped them to. */
-typedef struct {
- char *ipv4_address;
- char *hostname_address;
-} virtaddress_entry_t;
-
-/** A hash table to store client-side address rewrite instructions. */
-static strmap_t *addressmap=NULL;
-/**
- * Table mapping addresses to which virtual address, if any, we
- * assigned them to.
- *
- * We maintain the following invariant: if [A,B] is in
- * virtaddress_reversemap, then B must be a virtual address, and [A,B]
- * must be in addressmap. We do not require that the converse hold:
- * if it fails, then we could end up mapping two virtual addresses to
- * the same address, which is no disaster.
- **/
-static strmap_t *virtaddress_reversemap=NULL;
-
-/** Initialize addressmap. */
-void
-addressmap_init(void)
-{
- addressmap = strmap_new();
- virtaddress_reversemap = strmap_new();
-}
-
-/** Free the memory associated with the addressmap entry <b>_ent</b>. */
-static void
-addressmap_ent_free(void *_ent)
-{
- addressmap_entry_t *ent;
- if (!_ent)
- return;
-
- ent = _ent;
- tor_free(ent->new_address);
- tor_free(ent);
-}
-
-/** Free storage held by a virtaddress_entry_t* entry in <b>ent</b>. */
-static void
-addressmap_virtaddress_ent_free(void *_ent)
-{
- virtaddress_entry_t *ent;
- if (!_ent)
- return;
-
- ent = _ent;
- tor_free(ent->ipv4_address);
- tor_free(ent->hostname_address);
- tor_free(ent);
-}
-
-/** Free storage held by a virtaddress_entry_t* entry in <b>ent</b>. */
-static void
-addressmap_virtaddress_remove(const char *address, addressmap_entry_t *ent)
-{
- if (ent && ent->new_address &&
- address_is_in_virtual_range(ent->new_address)) {
- virtaddress_entry_t *ve =
- strmap_get(virtaddress_reversemap, ent->new_address);
- /*log_fn(LOG_NOTICE,"remove reverse mapping for %s",ent->new_address);*/
- if (ve) {
- if (!strcmp(address, ve->ipv4_address))
- tor_free(ve->ipv4_address);
- if (!strcmp(address, ve->hostname_address))
- tor_free(ve->hostname_address);
- if (!ve->ipv4_address && !ve->hostname_address) {
- tor_free(ve);
- strmap_remove(virtaddress_reversemap, ent->new_address);
- }
- }
- }
-}
-
-/** Remove <b>ent</b> (which must be mapped to by <b>address</b>) from the
- * client address maps. */
-static void
-addressmap_ent_remove(const char *address, addressmap_entry_t *ent)
-{
- addressmap_virtaddress_remove(address, ent);
- addressmap_ent_free(ent);
-}
-
-/** Unregister all TrackHostExits mappings from any address to
- * *.exitname.exit. */
-static void
-clear_trackexithost_mappings(const char *exitname)
-{
- char *suffix = NULL;
- if (!addressmap || !exitname)
- return;
- tor_asprintf(&suffix, ".%s.exit", exitname);
- tor_strlower(suffix);
-
- STRMAP_FOREACH_MODIFY(addressmap, address, addressmap_entry_t *, ent) {
- if (ent->source == ADDRMAPSRC_TRACKEXIT &&
- !strcmpend(ent->new_address, suffix)) {
- addressmap_ent_remove(address, ent);
- MAP_DEL_CURRENT(address);
- }
- } STRMAP_FOREACH_END;
-
- tor_free(suffix);
-}
-
-/** Remove all TRACKEXIT mappings from the addressmap for which the target
- * host is unknown or no longer allowed, or for which the source address
- * is no longer in trackexithosts. */
-void
-addressmap_clear_excluded_trackexithosts(const or_options_t *options)
-{
- const routerset_t *allow_nodes = options->ExitNodes;
- const routerset_t *exclude_nodes = options->ExcludeExitNodesUnion_;
-
- if (!addressmap)
- return;
- if (routerset_is_empty(allow_nodes))
- allow_nodes = NULL;
- if (allow_nodes == NULL && routerset_is_empty(exclude_nodes))
- return;
-
- STRMAP_FOREACH_MODIFY(addressmap, address, addressmap_entry_t *, ent) {
- size_t len;
- const char *target = ent->new_address, *dot;
- char *nodename;
- const node_t *node;
-
- if (!target) {
- /* DNS resolving in progress */
- continue;
- } else if (strcmpend(target, ".exit")) {
- /* Not a .exit mapping */
- continue;
- } else if (ent->source != ADDRMAPSRC_TRACKEXIT) {
- /* Not a trackexit mapping. */
- continue;
- }
- len = strlen(target);
- if (len < 6)
- continue; /* malformed. */
- dot = target + len - 6; /* dot now points to just before .exit */
- while (dot > target && *dot != '.')
- dot--;
- if (*dot == '.') dot++;
- nodename = tor_strndup(dot, len-5-(dot-target));;
- node = node_get_by_nickname(nodename, 0);
- tor_free(nodename);
- if (!node ||
- (allow_nodes && !routerset_contains_node(allow_nodes, node)) ||
- routerset_contains_node(exclude_nodes, node) ||
- !hostname_in_track_host_exits(options, address)) {
- /* We don't know this one, or we want to be rid of it. */
- addressmap_ent_remove(address, ent);
- MAP_DEL_CURRENT(address);
- }
- } STRMAP_FOREACH_END;
-}
-
-/** Remove all AUTOMAP mappings from the addressmap for which the
- * source address no longer matches AutomapHostsSuffixes, which is
- * no longer allowed by AutomapHostsOnResolve, or for which the
- * target address is no longer in the virtual network. */
-void
-addressmap_clear_invalid_automaps(const or_options_t *options)
-{
- int clear_all = !options->AutomapHostsOnResolve;
- const smartlist_t *suffixes = options->AutomapHostsSuffixes;
-
- if (!addressmap)
- return;
-
- if (!suffixes)
- clear_all = 1; /* This should be impossible, but let's be sure. */
-
- STRMAP_FOREACH_MODIFY(addressmap, src_address, addressmap_entry_t *, ent) {
- int remove = clear_all;
- if (ent->source != ADDRMAPSRC_AUTOMAP)
- continue; /* not an automap mapping. */
-
- if (!remove) {
- int suffix_found = 0;
- SMARTLIST_FOREACH(suffixes, const char *, suffix, {
- if (!strcasecmpend(src_address, suffix)) {
- suffix_found = 1;
- break;
- }
- });
- if (!suffix_found)
- remove = 1;
- }
-
- if (!remove && ! address_is_in_virtual_range(ent->new_address))
- remove = 1;
-
- if (remove) {
- addressmap_ent_remove(src_address, ent);
- MAP_DEL_CURRENT(src_address);
- }
- } STRMAP_FOREACH_END;
-}
-
-/** Remove all entries from the addressmap that were set via the
- * configuration file or the command line. */
-void
-addressmap_clear_configured(void)
-{
- addressmap_get_mappings(NULL, 0, 0, 0);
-}
-
-/** Remove all entries from the addressmap that are set to expire, ever. */
-void
-addressmap_clear_transient(void)
-{
- addressmap_get_mappings(NULL, 2, TIME_MAX, 0);
-}
-
-/** Clean out entries from the addressmap cache that were
- * added long enough ago that they are no longer valid.
- */
-void
-addressmap_clean(time_t now)
-{
- addressmap_get_mappings(NULL, 2, now, 0);
-}
-
-/** Free all the elements in the addressmap, and free the addressmap
- * itself. */
-void
-addressmap_free_all(void)
-{
- strmap_free(addressmap, addressmap_ent_free);
- addressmap = NULL;
-
- strmap_free(virtaddress_reversemap, addressmap_virtaddress_ent_free);
- virtaddress_reversemap = NULL;
-}
-
-/** Try to find a match for AddressMap expressions that use
- * wildcard notation such as '*.c.d *.e.f' (so 'a.c.d' will map to 'a.e.f') or
- * '*.c.d a.b.c' (so 'a.c.d' will map to a.b.c).
- * Return the matching entry in AddressMap or NULL if no match is found.
- * For expressions such as '*.c.d *.e.f', truncate <b>address</b> 'a.c.d'
- * to 'a' before we return the matching AddressMap entry.
- *
- * This function does not handle the case where a pattern of the form "*.c.d"
- * matches the address c.d -- that's done by the main addressmap_rewrite
- * function.
- */
-static addressmap_entry_t *
-addressmap_match_superdomains(char *address)
-{
- addressmap_entry_t *val;
- char *cp;
-
- cp = address;
- while ((cp = strchr(cp, '.'))) {
- /* cp now points to a suffix of address that begins with a . */
- val = strmap_get_lc(addressmap, cp+1);
- if (val && val->src_wildcard) {
- if (val->dst_wildcard)
- *cp = '\0';
- return val;
- }
- ++cp;
- }
- return NULL;
-}
-
-/** Look at address, and rewrite it until it doesn't want any
- * more rewrites; but don't get into an infinite loop.
- * Don't write more than maxlen chars into address. Return true if the
- * address changed; false otherwise. Set *<b>expires_out</b> to the
- * expiry time of the result, or to <b>time_max</b> if the result does
- * not expire.
- *
- * If <b>exit_source_out</b> is non-null, we set it as follows. If we the
- * address starts out as a non-exit address, and we remap it to an .exit
- * address at any point, then set *<b>exit_source_out</b> to the
- * address_entry_source_t of the first such rule. Set *<b>exit_source_out</b>
- * to ADDRMAPSRC_NONE if there is no such rewrite, or if the original address
- * was a .exit.
- */
-int
-addressmap_rewrite(char *address, size_t maxlen, time_t *expires_out,
- addressmap_entry_source_t *exit_source_out)
-{
- addressmap_entry_t *ent;
- int rewrites;
- time_t expires = TIME_MAX;
- addressmap_entry_source_t exit_source = ADDRMAPSRC_NONE;
- char *addr_orig = tor_strdup(address);
- char *log_addr_orig = NULL;
-
- for (rewrites = 0; rewrites < 16; rewrites++) {
- int exact_match = 0;
- log_addr_orig = tor_strdup(escaped_safe_str_client(address));
-
- ent = strmap_get(addressmap, address);
-
- if (!ent || !ent->new_address) {
- ent = addressmap_match_superdomains(address);
- } else {
- if (ent->src_wildcard && !ent->dst_wildcard &&
- !strcasecmp(address, ent->new_address)) {
- /* This is a rule like *.example.com example.com, and we just got
- * "example.com" */
- goto done;
- }
-
- exact_match = 1;
- }
-
- if (!ent || !ent->new_address) {
- goto done;
- }
-
- if (ent->dst_wildcard && !exact_match) {
- strlcat(address, ".", maxlen);
- strlcat(address, ent->new_address, maxlen);
- } else {
- strlcpy(address, ent->new_address, maxlen);
- }
-
- if (!strcmpend(address, ".exit") &&
- strcmpend(addr_orig, ".exit") &&
- exit_source == ADDRMAPSRC_NONE) {
- exit_source = ent->source;
- }
-
- log_info(LD_APP, "Addressmap: rewriting %s to %s",
- log_addr_orig, escaped_safe_str_client(address));
- if (ent->expires > 1 && ent->expires < expires)
- expires = ent->expires;
-
- tor_free(log_addr_orig);
- }
- log_warn(LD_CONFIG,
- "Loop detected: we've rewritten %s 16 times! Using it as-is.",
- escaped_safe_str_client(address));
- /* it's fine to rewrite a rewrite, but don't loop forever */
-
- done:
- tor_free(addr_orig);
- tor_free(log_addr_orig);
- if (exit_source_out)
- *exit_source_out = exit_source;
- if (expires_out)
- *expires_out = TIME_MAX;
- return (rewrites > 0);
-}
-
-/** If we have a cached reverse DNS entry for the address stored in the
- * <b>maxlen</b>-byte buffer <b>address</b> (typically, a dotted quad) then
- * rewrite to the cached value and return 1. Otherwise return 0. Set
- * *<b>expires_out</b> to the expiry time of the result, or to <b>time_max</b>
- * if the result does not expire. */
-static int
-addressmap_rewrite_reverse(char *address, size_t maxlen, time_t *expires_out)
-{
- char *s, *cp;
- addressmap_entry_t *ent;
- int r = 0;
- tor_asprintf(&s, "REVERSE[%s]", address);
- ent = strmap_get(addressmap, s);
- if (ent) {
- cp = tor_strdup(escaped_safe_str_client(ent->new_address));
- log_info(LD_APP, "Rewrote reverse lookup %s -> %s",
- escaped_safe_str_client(s), cp);
- tor_free(cp);
- strlcpy(address, ent->new_address, maxlen);
- r = 1;
- }
-
- if (expires_out)
- *expires_out = (ent && ent->expires > 1) ? ent->expires : TIME_MAX;
-
- tor_free(s);
- return r;
-}
-
-/** Return 1 if <b>address</b> is already registered, else return 0. If address
- * is already registered, and <b>update_expires</b> is non-zero, then update
- * the expiry time on the mapping with update_expires if it is a
- * mapping created by TrackHostExits. */
-int
-addressmap_have_mapping(const char *address, int update_expiry)
-{
- addressmap_entry_t *ent;
- if (!(ent=strmap_get_lc(addressmap, address)))
- return 0;
- if (update_expiry && ent->source==ADDRMAPSRC_TRACKEXIT)
- ent->expires=time(NULL) + update_expiry;
- return 1;
-}
-
-/** Register a request to map <b>address</b> to <b>new_address</b>,
- * which will expire on <b>expires</b> (or 0 if never expires from
- * config file, 1 if never expires from controller, 2 if never expires
- * (virtual address mapping) from the controller.)
- *
- * <b>new_address</b> should be a newly dup'ed string, which we'll use or
- * free as appropriate. We will leave address alone.
- *
- * If <b>wildcard_addr</b> is true, then the mapping will match any address
- * equal to <b>address</b>, or any address ending with a period followed by
- * <b>address</b>. If <b>wildcard_addr</b> and <b>wildcard_new_addr</b> are
- * both true, the mapping will rewrite addresses that end with
- * ".<b>address</b>" into ones that end with ".<b>new_address</b>."
- *
- * If <b>new_address</b> is NULL, or <b>new_address</b> is equal to
- * <b>address</b> and <b>wildcard_addr</b> is equal to
- * <b>wildcard_new_addr</b>, remove any mappings that exist from
- * <b>address</b>.
- *
- *
- * It is an error to set <b>wildcard_new_addr</b> if <b>wildcard_addr</b> is
- * not set. */
-void
-addressmap_register(const char *address, char *new_address, time_t expires,
- addressmap_entry_source_t source,
- const int wildcard_addr,
- const int wildcard_new_addr)
-{
- addressmap_entry_t *ent;
-
- if (wildcard_new_addr)
- tor_assert(wildcard_addr);
-
- ent = strmap_get(addressmap, address);
- if (!new_address || (!strcasecmp(address,new_address) &&
- wildcard_addr == wildcard_new_addr)) {
- /* Remove the mapping, if any. */
- tor_free(new_address);
- if (ent) {
- addressmap_ent_remove(address,ent);
- strmap_remove(addressmap, address);
- }
- return;
- }
- if (!ent) { /* make a new one and register it */
- ent = tor_malloc_zero(sizeof(addressmap_entry_t));
- strmap_set(addressmap, address, ent);
- } else if (ent->new_address) { /* we need to clean up the old mapping. */
- if (expires > 1) {
- log_info(LD_APP,"Temporary addressmap ('%s' to '%s') not performed, "
- "since it's already mapped to '%s'",
- safe_str_client(address),
- safe_str_client(new_address),
- safe_str_client(ent->new_address));
- tor_free(new_address);
- return;
- }
- if (address_is_in_virtual_range(ent->new_address) &&
- expires != 2) {
- /* XXX This isn't the perfect test; we want to avoid removing
- * mappings set from the control interface _as virtual mapping */
- addressmap_virtaddress_remove(address, ent);
- }
- tor_free(ent->new_address);
- } /* else { we have an in-progress resolve with no mapping. } */
-
- ent->new_address = new_address;
- ent->expires = expires==2 ? 1 : expires;
- ent->num_resolve_failures = 0;
- ent->source = source;
- ent->src_wildcard = wildcard_addr ? 1 : 0;
- ent->dst_wildcard = wildcard_new_addr ? 1 : 0;
-
- log_info(LD_CONFIG, "Addressmap: (re)mapped '%s' to '%s'",
- safe_str_client(address),
- safe_str_client(ent->new_address));
- control_event_address_mapped(address, ent->new_address, expires, NULL);
-}
-
-/** An attempt to resolve <b>address</b> failed at some OR.
- * Increment the number of resolve failures we have on record
- * for it, and then return that number.
- */
-int
-client_dns_incr_failures(const char *address)
-{
- addressmap_entry_t *ent = strmap_get(addressmap, address);
- if (!ent) {
- ent = tor_malloc_zero(sizeof(addressmap_entry_t));
- ent->expires = time(NULL) + MAX_DNS_ENTRY_AGE;
- strmap_set(addressmap,address,ent);
- }
- if (ent->num_resolve_failures < SHORT_MAX)
- ++ent->num_resolve_failures; /* don't overflow */
- log_info(LD_APP, "Address %s now has %d resolve failures.",
- safe_str_client(address),
- ent->num_resolve_failures);
- return ent->num_resolve_failures;
-}
-
-/** If <b>address</b> is in the client DNS addressmap, reset
- * the number of resolve failures we have on record for it.
- * This is used when we fail a stream because it won't resolve:
- * otherwise future attempts on that address will only try once.
- */
-void
-client_dns_clear_failures(const char *address)
-{
- addressmap_entry_t *ent = strmap_get(addressmap, address);
- if (ent)
- ent->num_resolve_failures = 0;
-}
-
-/** Record the fact that <b>address</b> resolved to <b>name</b>.
- * We can now use this in subsequent streams via addressmap_rewrite()
- * so we can more correctly choose an exit that will allow <b>address</b>.
- *
- * If <b>exitname</b> is defined, then append the addresses with
- * ".exitname.exit" before registering the mapping.
- *
- * If <b>ttl</b> is nonnegative, the mapping will be valid for
- * <b>ttl</b>seconds; otherwise, we use the default.
- */
-static void
-client_dns_set_addressmap_impl(const char *address, const char *name,
- const char *exitname,
- int ttl)
-{
- /* <address>.<hex or nickname>.exit\0 or just <address>\0 */
- char extendedaddress[MAX_SOCKS_ADDR_LEN+MAX_VERBOSE_NICKNAME_LEN+10];
- /* 123.123.123.123.<hex or nickname>.exit\0 or just 123.123.123.123\0 */
- char extendedval[INET_NTOA_BUF_LEN+MAX_VERBOSE_NICKNAME_LEN+10];
-
- tor_assert(address);
- tor_assert(name);
-
- if (ttl<0)
- ttl = DEFAULT_DNS_TTL;
- else
- ttl = dns_clip_ttl(ttl);
-
- if (exitname) {
- /* XXXX fails to ever get attempts to get an exit address of
- * google.com.digest[=~]nickname.exit; we need a syntax for this that
- * won't make strict RFC952-compliant applications (like us) barf. */
- tor_snprintf(extendedaddress, sizeof(extendedaddress),
- "%s.%s.exit", address, exitname);
- tor_snprintf(extendedval, sizeof(extendedval),
- "%s.%s.exit", name, exitname);
- } else {
- tor_snprintf(extendedaddress, sizeof(extendedaddress),
- "%s", address);
- tor_snprintf(extendedval, sizeof(extendedval),
- "%s", name);
- }
- addressmap_register(extendedaddress, tor_strdup(extendedval),
- time(NULL) + ttl, ADDRMAPSRC_DNS, 0, 0);
-}
-
-/** Record the fact that <b>address</b> resolved to <b>val</b>.
- * We can now use this in subsequent streams via addressmap_rewrite()
- * so we can more correctly choose an exit that will allow <b>address</b>.
- *
- * If <b>exitname</b> is defined, then append the addresses with
- * ".exitname.exit" before registering the mapping.
- *
- * If <b>ttl</b> is nonnegative, the mapping will be valid for
- * <b>ttl</b>seconds; otherwise, we use the default.
- */
-void
-client_dns_set_addressmap(const char *address, uint32_t val,
- const char *exitname,
- int ttl)
-{
- struct in_addr in;
- char valbuf[INET_NTOA_BUF_LEN];
-
- tor_assert(address);
-
- if (tor_inet_aton(address, &in))
- return; /* If address was an IP address already, don't add a mapping. */
- in.s_addr = htonl(val);
- tor_inet_ntoa(&in,valbuf,sizeof(valbuf));
-
- client_dns_set_addressmap_impl(address, valbuf, exitname, ttl);
-}
-
-/** Add a cache entry noting that <b>address</b> (ordinarily a dotted quad)
- * resolved via a RESOLVE_PTR request to the hostname <b>v</b>.
- *
- * If <b>exitname</b> is defined, then append the addresses with
- * ".exitname.exit" before registering the mapping.
- *
- * If <b>ttl</b> is nonnegative, the mapping will be valid for
- * <b>ttl</b>seconds; otherwise, we use the default.
- */
-static void
-client_dns_set_reverse_addressmap(const char *address, const char *v,
- const char *exitname,
- int ttl)
-{
- char *s = NULL;
- tor_asprintf(&s, "REVERSE[%s]", address);
- client_dns_set_addressmap_impl(s, v, exitname, ttl);
- tor_free(s);
-}
-
-/* By default, we hand out 127.192.0.1 through 127.254.254.254.
- * These addresses should map to localhost, so even if the
- * application accidentally tried to connect to them directly (not
- * via Tor), it wouldn't get too far astray.
- *
- * These options are configured by parse_virtual_addr_network().
- */
-/** Which network should we use for virtual IPv4 addresses? Only the first
- * bits of this value are fixed. */
-static uint32_t virtual_addr_network = 0x7fc00000u;
-/** How many bits of <b>virtual_addr_network</b> are fixed? */
-static maskbits_t virtual_addr_netmask_bits = 10;
-/** What's the next virtual address we will hand out? */
-static uint32_t next_virtual_addr = 0x7fc00000u;
-
-/** Read a netmask of the form 127.192.0.0/10 from "val", and check whether
- * it's a valid set of virtual addresses to hand out in response to MAPADDRESS
- * requests. Return 0 on success; set *msg (if provided) to a newly allocated
- * string and return -1 on failure. If validate_only is false, sets the
- * actual virtual address range to the parsed value. */
-int
-parse_virtual_addr_network(const char *val, int validate_only,
- char **msg)
-{
- uint32_t addr;
- uint16_t port_min, port_max;
- maskbits_t bits;
-
- if (parse_addr_and_port_range(val, &addr, &bits, &port_min, &port_max)) {
- if (msg) *msg = tor_strdup("Error parsing VirtualAddressNetwork");
- return -1;
- }
-
- if (port_min != 1 || port_max != 65535) {
- if (msg) *msg = tor_strdup("Can't specify ports on VirtualAddressNetwork");
- return -1;
- }
-
- if (bits > 16) {
- if (msg) *msg = tor_strdup("VirtualAddressNetwork expects a /16 "
- "network or larger");
- return -1;
- }
-
- if (validate_only)
- return 0;
-
- virtual_addr_network = (uint32_t)( addr & (0xfffffffful << (32-bits)) );
- virtual_addr_netmask_bits = bits;
-
- if (addr_mask_cmp_bits(next_virtual_addr, addr, bits))
- next_virtual_addr = addr;
-
- return 0;
-}
-
-/**
- * Return true iff <b>addr</b> is likely to have been returned by
- * client_dns_get_unused_address.
- **/
-static int
-address_is_in_virtual_range(const char *address)
-{
- struct in_addr in;
- tor_assert(address);
- if (!strcasecmpend(address, ".virtual")) {
- return 1;
- } else if (tor_inet_aton(address, &in)) {
- uint32_t addr = ntohl(in.s_addr);
- if (!addr_mask_cmp_bits(addr, virtual_addr_network,
- virtual_addr_netmask_bits))
- return 1;
- }
- return 0;
-}
-
-/** Increment the value of next_virtual_addr; reset it to the start of the
- * virtual address range if it wraps around.
- */
-static INLINE void
-increment_virtual_addr(void)
-{
- ++next_virtual_addr;
- if (addr_mask_cmp_bits(next_virtual_addr, virtual_addr_network,
- virtual_addr_netmask_bits))
- next_virtual_addr = virtual_addr_network;
-}
-
-/** Return a newly allocated string holding an address of <b>type</b>
- * (one of RESOLVED_TYPE_{IPV4|HOSTNAME}) that has not yet been mapped,
- * and that is very unlikely to be the address of any real host.
- *
- * May return NULL if we have run out of virtual addresses.
- */
-static char *
-addressmap_get_virtual_address(int type)
-{
- char buf[64];
- tor_assert(addressmap);
-
- if (type == RESOLVED_TYPE_HOSTNAME) {
- char rand[10];
- do {
- crypto_rand(rand, sizeof(rand));
- base32_encode(buf,sizeof(buf),rand,sizeof(rand));
- strlcat(buf, ".virtual", sizeof(buf));
- } while (strmap_get(addressmap, buf));
- return tor_strdup(buf);
- } else if (type == RESOLVED_TYPE_IPV4) {
- // This is an imperfect estimate of how many addresses are available, but
- // that's ok.
- struct in_addr in;
- uint32_t available = 1u << (32-virtual_addr_netmask_bits);
- while (available) {
- /* Don't hand out any .0 or .255 address. */
- while ((next_virtual_addr & 0xff) == 0 ||
- (next_virtual_addr & 0xff) == 0xff) {
- increment_virtual_addr();
- if (! --available) {
- log_warn(LD_CONFIG, "Ran out of virtual addresses!");
- return NULL;
- }
- }
- in.s_addr = htonl(next_virtual_addr);
- tor_inet_ntoa(&in, buf, sizeof(buf));
- if (!strmap_get(addressmap, buf)) {
- increment_virtual_addr();
- break;
- }
-
- increment_virtual_addr();
- --available;
- // log_info(LD_CONFIG, "%d addrs available", (int)available);
- if (! available) {
- log_warn(LD_CONFIG, "Ran out of virtual addresses!");
- return NULL;
- }
- }
- return tor_strdup(buf);
- } else {
- log_warn(LD_BUG, "Called with unsupported address type (%d)", type);
- return NULL;
- }
-}
-
-/** A controller has requested that we map some address of type
- * <b>type</b> to the address <b>new_address</b>. Choose an address
- * that is unlikely to be used, and map it, and return it in a newly
- * allocated string. If another address of the same type is already
- * mapped to <b>new_address</b>, try to return a copy of that address.
- *
- * The string in <b>new_address</b> may be freed or inserted into a map
- * as appropriate. May return NULL if are out of virtual addresses.
- **/
-const char *
-addressmap_register_virtual_address(int type, char *new_address)
-{
- char **addrp;
- virtaddress_entry_t *vent;
- int vent_needs_to_be_added = 0;
-
- tor_assert(new_address);
- tor_assert(addressmap);
- tor_assert(virtaddress_reversemap);
-
- vent = strmap_get(virtaddress_reversemap, new_address);
- if (!vent) {
- vent = tor_malloc_zero(sizeof(virtaddress_entry_t));
- vent_needs_to_be_added = 1;
- }
-
- addrp = (type == RESOLVED_TYPE_IPV4) ?
- &vent->ipv4_address : &vent->hostname_address;
- if (*addrp) {
- addressmap_entry_t *ent = strmap_get(addressmap, *addrp);
- if (ent && ent->new_address &&
- !strcasecmp(new_address, ent->new_address)) {
- tor_free(new_address);
- tor_assert(!vent_needs_to_be_added);
- return tor_strdup(*addrp);
- } else
- log_warn(LD_BUG,
- "Internal confusion: I thought that '%s' was mapped to by "
- "'%s', but '%s' really maps to '%s'. This is a harmless bug.",
- safe_str_client(new_address),
- safe_str_client(*addrp),
- safe_str_client(*addrp),
- ent?safe_str_client(ent->new_address):"(nothing)");
- }
-
- tor_free(*addrp);
- *addrp = addressmap_get_virtual_address(type);
- if (!*addrp) {
- tor_free(vent);
- tor_free(new_address);
- return NULL;
- }
- log_info(LD_APP, "Registering map from %s to %s", *addrp, new_address);
- if (vent_needs_to_be_added)
- strmap_set(virtaddress_reversemap, new_address, vent);
- addressmap_register(*addrp, new_address, 2, ADDRMAPSRC_AUTOMAP, 0, 0);
-
-#if 0
- {
- /* Try to catch possible bugs */
- addressmap_entry_t *ent;
- ent = strmap_get(addressmap, *addrp);
- tor_assert(ent);
- tor_assert(!strcasecmp(ent->new_address,new_address));
- vent = strmap_get(virtaddress_reversemap, new_address);
- tor_assert(vent);
- tor_assert(!strcasecmp(*addrp,
- (type == RESOLVED_TYPE_IPV4) ?
- vent->ipv4_address : vent->hostname_address));
- log_info(LD_APP, "Map from %s to %s okay.",
- safe_str_client(*addrp),
- safe_str_client(new_address));
- }
-#endif
-
- return *addrp;
-}
-
-/** Return 1 if <b>address</b> has funny characters in it like colons. Return
- * 0 if it's fine, or if we're configured to allow it anyway. <b>client</b>
- * should be true if we're using this address as a client; false if we're
- * using it as a server.
- */
-int
-address_is_invalid_destination(const char *address, int client)
-{
- if (client) {
- if (get_options()->AllowNonRFC953Hostnames)
- return 0;
- } else {
- if (get_options()->ServerDNSAllowNonRFC953Hostnames)
- return 0;
- }
-
- while (*address) {
- if (TOR_ISALNUM(*address) ||
- *address == '-' ||
- *address == '.' ||
- *address == '_') /* Underscore is not allowed, but Windows does it
- * sometimes, just to thumb its nose at the IETF. */
- ++address;
- else
- return 1;
- }
- return 0;
-}
-
-/** Iterate over all address mappings which have expiry times between
- * min_expires and max_expires, inclusive. If sl is provided, add an
- * "old-addr new-addr expiry" string to sl for each mapping, omitting
- * the expiry time if want_expiry is false. If sl is NULL, remove the
- * mappings.
- */
-void
-addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
- time_t max_expires, int want_expiry)
-{
- strmap_iter_t *iter;
- const char *key;
- void *val_;
- addressmap_entry_t *val;
-
- if (!addressmap)
- addressmap_init();
-
- for (iter = strmap_iter_init(addressmap); !strmap_iter_done(iter); ) {
- strmap_iter_get(iter, &key, &val_);
- val = val_;
- if (val->expires >= min_expires && val->expires <= max_expires) {
- if (!sl) {
- iter = strmap_iter_next_rmv(addressmap,iter);
- addressmap_ent_remove(key, val);
- continue;
- } else if (val->new_address) {
- const char *src_wc = val->src_wildcard ? "*." : "";
- const char *dst_wc = val->dst_wildcard ? "*." : "";
- if (want_expiry) {
- if (val->expires < 3 || val->expires == TIME_MAX)
- smartlist_add_asprintf(sl, "%s%s %s%s NEVER",
- src_wc, key, dst_wc, val->new_address);
- else {
- char time[ISO_TIME_LEN+1];
- format_iso_time(time, val->expires);
- smartlist_add_asprintf(sl, "%s%s %s%s \"%s\"",
- src_wc, key, dst_wc, val->new_address,
- time);
- }
- } else {
- smartlist_add_asprintf(sl, "%s%s %s%s",
- src_wc, key, dst_wc, val->new_address);
- }
- }
- }
- iter = strmap_iter_next(addressmap,iter);
- }
-}
/** Check if <b>conn</b> is using a dangerous port. Then warn and/or
* reject depending on our config options. */
diff --git a/src/or/connection_edge.h b/src/or/connection_edge.h
index 42fb73c..b5938b3 100644
--- a/src/or/connection_edge.h
+++ b/src/or/connection_edge.h
@@ -67,30 +67,6 @@ int connection_ap_process_transparent(entry_connection_t *conn);
int address_is_invalid_destination(const char *address, int client);
-void addressmap_init(void);
-void addressmap_clear_excluded_trackexithosts(const or_options_t *options);
-void addressmap_clear_invalid_automaps(const or_options_t *options);
-void addressmap_clean(time_t now);
-void addressmap_clear_configured(void);
-void addressmap_clear_transient(void);
-void addressmap_free_all(void);
-int addressmap_rewrite(char *address, size_t maxlen, time_t *expires_out,
- addressmap_entry_source_t *exit_source_out);
-int addressmap_have_mapping(const char *address, int update_timeout);
-
-void addressmap_register(const char *address, char *new_address,
- time_t expires, addressmap_entry_source_t source,
- const int address_wildcard,
- const int new_address_wildcard);
-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(const char *address, uint32_t val,
- const char *exitname, int ttl);
-const char *addressmap_register_virtual_address(int type, char *new_address);
-void addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
- time_t max_expires, int want_expiry);
int connection_ap_rewrite_and_attach_if_allowed(entry_connection_t *conn,
origin_circuit_t *circ,
crypt_path_t *cpath);
diff --git a/src/or/control.c b/src/or/control.c
index ad2f278..c6a66ae 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -11,6 +11,7 @@
#define CONTROL_PRIVATE
#include "or.h"
+#include "addressmap.h"
#include "buffers.h"
#include "channel.h"
#include "channeltls.h"
diff --git a/src/or/include.am b/src/or/include.am
index 01f4784..405cbd0 100644
--- a/src/or/include.am
+++ b/src/or/include.am
@@ -16,6 +16,7 @@ evdns_source=src/ext/eventdns.c
endif
src_or_libtor_a_SOURCES = \
+ src/or/addressmap.c \
src/or/buffers.c \
src/or/channel.c \
src/or/channeltls.c \
@@ -91,6 +92,7 @@ src_or_tor_LDADD = src/or/libtor.a src/common/libor.a src/common/libor-crypto.a
@TOR_LIB_WS32@ @TOR_LIB_GDI@
ORHEADERS = \
+ src/or/addressmap.h \
src/or/buffers.h \
src/or/channel.h \
src/or/channeltls.h \
diff --git a/src/or/main.c b/src/or/main.c
index fd8f941..446836a 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -12,6 +12,7 @@
#define MAIN_PRIVATE
#include "or.h"
+#include "addressmap.h"
#include "buffers.h"
#include "channel.h"
#include "channeltls.h"
diff --git a/src/or/relay.c b/src/or/relay.c
index 0f0d1df..25fc2e7 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -12,6 +12,7 @@
#define RELAY_PRIVATE
#include "or.h"
+#include "addressmap.h"
#include "buffers.h"
#include "channel.h"
#include "circuitbuild.h"
diff --git a/src/test/test_config.c b/src/test/test_config.c
index d9fcd8b..e04b9df 100644
--- a/src/test/test_config.c
+++ b/src/test/test_config.c
@@ -5,6 +5,7 @@
#include "orconfig.h"
#include "or.h"
+#include "addressmap.h"
#include "config.h"
#include "confparse.h"
#include "connection_edge.h"
1
0

15 Nov '12
commit 00633b944672458bdfd3d79bab4e1230cee4a522
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Sat Oct 27 16:07:25 2012 -0400
Make dns wildcarding checks work for ipv6
---
src/or/dns.c | 93 ++++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 61 insertions(+), 32 deletions(-)
diff --git a/src/or/dns.c b/src/or/dns.c
index d26314a..c590e01 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -1506,8 +1506,8 @@ static int dns_wildcarded_test_address_notice_given = 0;
/** True iff all addresses seem to be getting wildcarded. */
static int dns_is_completely_invalid = 0;
-/** Called when we see <b>id</b> (a dotted quad) in response to a request for
- * a hopefully bogus address. */
+/** Called when we see <b>id</b> (a dotted quad or IPv6 address) in response
+ * to a request for a hopefully bogus address. */
static void
wildcard_increment_answer(const char *id)
{
@@ -1579,17 +1579,27 @@ evdns_wildcard_check_callback(int result, char type, int count, int ttl,
{
(void)ttl;
++n_wildcard_requests;
- if (result == DNS_ERR_NONE && type == DNS_IPv4_A && count) {
- uint32_t *addrs = addresses;
- int i;
+ if (result == DNS_ERR_NONE && count) {
char *string_address = arg;
- for (i = 0; i < count; ++i) {
- char answer_buf[INET_NTOA_BUF_LEN+1];
- struct in_addr in;
- in.s_addr = addrs[i];
- tor_inet_ntoa(&in, answer_buf, sizeof(answer_buf));
- wildcard_increment_answer(answer_buf);
+ int i;
+ if (type == DNS_IPv4_A) {
+ const uint32_t *addrs = addresses;
+ for (i = 0; i < count; ++i) {
+ char answer_buf[INET_NTOA_BUF_LEN+1];
+ struct in_addr in;
+ in.s_addr = addrs[i];
+ tor_inet_ntoa(&in, answer_buf, sizeof(answer_buf));
+ wildcard_increment_answer(answer_buf);
+ }
+ } else if (type == DNS_IPv6_AAAA) {
+ const struct in6_addr *addrs = addresses;
+ for (i = 0; i < count; ++i) {
+ char answer_buf[TOR_ADDR_BUF_LEN+1];
+ tor_inet_ntop(AF_INET6, &addrs[i], answer_buf, sizeof(answer_buf));
+ wildcard_increment_answer(answer_buf);
+ }
}
+
log(dns_wildcard_one_notice_given ? LOG_INFO : LOG_NOTICE, LD_EXIT,
"Your DNS provider gave an answer for \"%s\", which "
"is not supposed to exist. Apparently they are hijacking "
@@ -1606,7 +1616,8 @@ evdns_wildcard_check_callback(int result, char type, int count, int ttl,
* <b>min_len</b> and <b>max_len</b> random (plausible) characters followed by
* <b>suffix</b> */
static void
-launch_wildcard_check(int min_len, int max_len, const char *suffix)
+launch_wildcard_check(int min_len, int max_len, int is_ipv6,
+ const char *suffix)
{
char *addr;
struct evdns_request *req;
@@ -1616,7 +1627,15 @@ launch_wildcard_check(int min_len, int max_len, const char *suffix)
"domains with request for bogus hostname \"%s\"", addr);
tor_assert(the_evdns_base);
- req = evdns_base_resolve_ipv4(
+ if (is_ipv6)
+ req = evdns_base_resolve_ipv6(
+ the_evdns_base,
+ /* This "addr" tells us which address to resolve */
+ addr,
+ DNS_QUERY_NO_SEARCH, evdns_wildcard_check_callback,
+ /* This "addr" is an argument to the callback*/ addr);
+ else
+ req = evdns_base_resolve_ipv4(
the_evdns_base,
/* This "addr" tells us which address to resolve */
addr,
@@ -1661,6 +1680,14 @@ launch_test_addresses(int fd, short event, void *args)
escaped_safe_str(address));
tor_free(a);
}
+ a = tor_strdup(address);
+ req = evdns_base_resolve_ipv6(the_evdns_base,
+ address, DNS_QUERY_NO_SEARCH, evdns_callback, a);
+ if (!req) {
+ log_info(LD_EXIT, "eventdns rejected test address %s",
+ escaped_safe_str(address));
+ tor_free(a);
+ }
} SMARTLIST_FOREACH_END(address);
}
@@ -1673,27 +1700,29 @@ launch_test_addresses(int fd, short event, void *args)
static void
dns_launch_wildcard_checks(void)
{
- int i;
+ int i, ipv6;
log_info(LD_EXIT, "Launching checks to see whether our nameservers like "
"to hijack DNS failures.");
- for (i = 0; i < N_WILDCARD_CHECKS; ++i) {
- /* RFC2606 reserves these. Sadly, some DNS hijackers, in a silly attempt
- * to 'comply' with rfc2606, refrain from giving A records for these.
- * This is the standards-compliance equivalent of making sure that your
- * crackhouse's elevator inspection certificate is up to date.
- */
- launch_wildcard_check(2, 16, ".invalid");
- launch_wildcard_check(2, 16, ".test");
-
- /* These will break specs if there are ever any number of
- * 8+-character top-level domains. */
- launch_wildcard_check(8, 16, "");
-
- /* Try some random .com/org/net domains. This will work fine so long as
- * not too many resolve to the same place. */
- launch_wildcard_check(8, 16, ".com");
- launch_wildcard_check(8, 16, ".org");
- launch_wildcard_check(8, 16, ".net");
+ for (ipv6 = 0; ipv6 <= 1; ++ipv6) {
+ for (i = 0; i < N_WILDCARD_CHECKS; ++i) {
+ /* RFC2606 reserves these. Sadly, some DNS hijackers, in a silly
+ * attempt to 'comply' with rfc2606, refrain from giving A records for
+ * these. This is the standards-compliance equivalent of making sure
+ * that your crackhouse's elevator inspection certificate is up to date.
+ */
+ launch_wildcard_check(2, 16, ipv6, ".invalid");
+ launch_wildcard_check(2, 16, ipv6, ".test");
+
+ /* These will break specs if there are ever any number of
+ * 8+-character top-level domains. */
+ launch_wildcard_check(8, 16, ipv6, "");
+
+ /* Try some random .com/org/net domains. This will work fine so long as
+ * not too many resolve to the same place. */
+ launch_wildcard_check(8, 16, ipv6, ".com");
+ launch_wildcard_check(8, 16, ipv6, ".org");
+ launch_wildcard_check(8, 16, ipv6, ".net");
+ }
}
}
1
0

[tor/master] Get the client side of receiving an IPv6 address to work
by nickm@torproject.org 15 Nov '12
by nickm@torproject.org 15 Nov '12
15 Nov '12
commit cac5335195d3bb9a39af77886992ffa98b8c7817
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Wed Oct 31 18:27:48 2012 -0400
Get the client side of receiving an IPv6 address to work
This makes it so we can handle getting an IPv6 in the 3 different
formats we specified it for in RESOLVED cells,
END_STREAM_REASON_EXITPOLICY cells, and CONNECTED cells.
We don't cache IPv6 addresses yet, since proposal 205 isn't
implemented.
There's a refactored function for parsing connected cells; it has unit
tests.
---
src/or/addressmap.c | 35 ++++++-----
src/or/addressmap.h | 2 +-
src/or/connection_edge.c | 15 ++++-
src/or/connection_edge.h | 1 +
src/or/relay.c | 134 ++++++++++++++++++++++++++++++++---------
src/or/relay.h | 2 +
src/test/test_cell_formats.c | 114 +++++++++++++++++++++++++++++++++++
7 files changed, 254 insertions(+), 49 deletions(-)
diff --git a/src/or/addressmap.c b/src/or/addressmap.c
index 4aa46fc..45e29c1 100644
--- a/src/or/addressmap.c
+++ b/src/or/addressmap.c
@@ -576,10 +576,7 @@ client_dns_set_addressmap_impl(origin_circuit_t *on_circ,
const char *exitname,
int ttl)
{
- /* <address>.<hex or nickname>.exit\0 or just <address>\0 */
- char extendedaddress[MAX_SOCKS_ADDR_LEN+MAX_VERBOSE_NICKNAME_LEN+10];
- /* 123.123.123.123.<hex or nickname>.exit\0 or just 123.123.123.123\0 */
- char extendedval[INET_NTOA_BUF_LEN+MAX_VERBOSE_NICKNAME_LEN+10];
+ char *extendedaddress=NULL, *extendedval=NULL;
(void)on_circ;
tor_assert(address);
@@ -594,18 +591,19 @@ client_dns_set_addressmap_impl(origin_circuit_t *on_circ,
/* XXXX fails to ever get attempts to get an exit address of
* google.com.digest[=~]nickname.exit; we need a syntax for this that
* won't make strict RFC952-compliant applications (like us) barf. */
- tor_snprintf(extendedaddress, sizeof(extendedaddress),
+ tor_asprintf(&extendedaddress,
"%s.%s.exit", address, exitname);
- tor_snprintf(extendedval, sizeof(extendedval),
+ tor_asprintf(&extendedval,
"%s.%s.exit", name, exitname);
} else {
- tor_snprintf(extendedaddress, sizeof(extendedaddress),
+ tor_asprintf(&extendedaddress,
"%s", address);
- tor_snprintf(extendedval, sizeof(extendedval),
+ tor_asprintf(&extendedval,
"%s", name);
}
- addressmap_register(extendedaddress, tor_strdup(extendedval),
+ addressmap_register(extendedaddress, extendedval,
time(NULL) + ttl, ADDRMAPSRC_DNS, 0, 0);
+ tor_free(extendedaddress);
}
/** Record the fact that <b>address</b> resolved to <b>val</b>.
@@ -620,19 +618,26 @@ client_dns_set_addressmap_impl(origin_circuit_t *on_circ,
*/
void
client_dns_set_addressmap(origin_circuit_t *on_circ,
- const char *address, uint32_t val,
+ const char *address,
+ const tor_addr_t *val,
const char *exitname,
int ttl)
{
- struct in_addr in;
- char valbuf[INET_NTOA_BUF_LEN];
+ tor_addr_t addr_tmp;
+ char valbuf[TOR_ADDR_BUF_LEN];
tor_assert(address);
+ tor_assert(val);
- if (tor_inet_aton(address, &in))
+ if (tor_addr_parse(&addr_tmp, address) == 0)
return; /* If address was an IP address already, don't add a mapping. */
- in.s_addr = htonl(val);
- tor_inet_ntoa(&in,valbuf,sizeof(valbuf));
+
+ /* XXXXX For now, don't cache IPv6 addresses. */
+ if (tor_addr_family(val) != AF_INET)
+ return;
+
+ if (! tor_addr_to_str(valbuf, val, sizeof(valbuf), 0)) /* XXXX decorate? */
+ return;
client_dns_set_addressmap_impl(on_circ, address, valbuf, exitname, ttl);
}
diff --git a/src/or/addressmap.h b/src/or/addressmap.h
index dd8fc9b..9b07341 100644
--- a/src/or/addressmap.h
+++ b/src/or/addressmap.h
@@ -29,7 +29,7 @@ int parse_virtual_addr_network(const char *val, int validate_only,
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,
- const char *address, uint32_t val,
+ const char *address, const tor_addr_t *val,
const char *exitname, int ttl);
const char *addressmap_register_virtual_address(int type, char *new_address);
void addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index dc7d863..969128f 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -1949,10 +1949,19 @@ connection_ap_handshake_socks_resolved(entry_connection_t *conn,
if (CIRCUIT_IS_ORIGIN(circ)) /* should always be true */
origin_circ = TO_ORIGIN_CIRCUIT(circ);
if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
- uint32_t a = ntohl(get_uint32(answer));
- if (a) {
+ tor_addr_t a;
+ tor_addr_from_ipv4n(&a, get_uint32(answer));
+ if (! tor_addr_is_null(&a)) {
client_dns_set_addressmap(origin_circ,
- conn->socks_request->address, a,
+ conn->socks_request->address, &a,
+ conn->chosen_exit_name, ttl);
+ }
+ } else if (answer_type == RESOLVED_TYPE_IPV6 && answer_len == 16) {
+ tor_addr_t a;
+ tor_addr_from_ipv6_bytes(&a, (char*)answer);
+ if (! tor_addr_is_null(&a)) {
+ client_dns_set_addressmap(origin_circ,
+ conn->socks_request->address, &a,
conn->chosen_exit_name, ttl);
}
} else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
diff --git a/src/or/connection_edge.h b/src/or/connection_edge.h
index 39f492d..9c4318c 100644
--- a/src/or/connection_edge.h
+++ b/src/or/connection_edge.h
@@ -108,6 +108,7 @@ typedef struct begin_cell_t {
int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
uint8_t *end_reason_out);
+
#endif
diff --git a/src/or/relay.c b/src/or/relay.c
index 76e9d25..a05e955 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -705,21 +705,37 @@ connection_ap_process_end_not_open(
switch (reason) {
case END_STREAM_REASON_EXITPOLICY:
if (rh->length >= 5) {
- uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
- int ttl;
- if (!addr) {
+ tor_addr_t addr;
+ int ttl = -1;
+ tor_addr_make_unspec(&addr);
+ if (rh->length == 5 || rh->length == 9) {
+ tor_addr_from_ipv4n(&addr,
+ get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
+ if (rh->length == 9)
+ ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
+ } else if (rh->length == 17 || rh->length == 21) {
+ tor_addr_from_ipv6_bytes(&addr,
+ (char*)(cell->payload+RELAY_HEADER_SIZE+1));
+ if (rh->length == 21)
+ ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+17));
+ }
+ if (tor_addr_is_null(&addr)) {
log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
safe_str(conn->socks_request->address));
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
return 0;
}
- if (rh->length >= 9)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
- else
- ttl = -1;
+ if ((tor_addr_family(&addr) == AF_INET && !conn->ipv4_traffic_ok) ||
+ (tor_addr_family(&addr) == AF_INET6 && !conn->ipv6_traffic_ok)) {
+ log_fn(LOG_PROTOCOL_WARN, LD_APP,
+ "Got an EXITPOLICY failure on a connection with a "
+ "mismatched family. Closing.");
+ connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
+ return 0;
+ }
if (get_options()->ClientDNSRejectInternalAddresses &&
- is_internal_IP(addr, 0)) {
+ tor_addr_is_internal(&addr, 0)) {
log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
safe_str(conn->socks_request->address));
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
@@ -727,7 +743,7 @@ connection_ap_process_end_not_open(
}
client_dns_set_addressmap(circ,
- conn->socks_request->address, addr,
+ conn->socks_request->address, &addr,
conn->chosen_exit_name, ttl);
}
/* check if he *ought* to have allowed it */
@@ -830,20 +846,56 @@ connection_ap_process_end_not_open(
}
/** Helper: change the socks_request->address field on conn to the
- * dotted-quad representation of <b>new_addr</b> (given in host order),
+ * dotted-quad representation of <b>new_addr</b>,
* and send an appropriate REMAP event. */
static void
-remap_event_helper(entry_connection_t *conn, uint32_t new_addr)
+remap_event_helper(entry_connection_t *conn, const tor_addr_t *new_addr)
{
- struct in_addr in;
-
- in.s_addr = htonl(new_addr);
- tor_inet_ntoa(&in, conn->socks_request->address,
- sizeof(conn->socks_request->address));
+ tor_addr_to_str(conn->socks_request->address, new_addr,
+ sizeof(conn->socks_request->address),
+ 0); /* XXXXX Should decorate be 1? */
control_event_stream_status(conn, STREAM_EVENT_REMAP,
REMAP_STREAM_SOURCE_EXIT);
}
+/**DOCDOC*/
+int
+connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
+ tor_addr_t *addr_out, int *ttl_out)
+{
+ uint32_t bytes;
+ const uint8_t *payload = cell->payload + RELAY_HEADER_SIZE;
+
+ tor_addr_make_unspec(addr_out);
+ *ttl_out = -1;
+ if (rh->length == 0)
+ return 0;
+ if (rh->length < 4)
+ return -1;
+ bytes = ntohl(get_uint32(payload));
+
+ /* If bytes is 0, this is maybe a v6 address. Otherwise it's a v4 address */
+ if (bytes != 0) {
+ /* v4 address */
+ tor_addr_from_ipv4h(addr_out, bytes);
+ if (rh->length >= 8) {
+ bytes = ntohl(get_uint32(payload + 4));
+ if (bytes <= INT32_MAX)
+ *ttl_out = bytes;
+ }
+ } else {
+ if (rh->length < 25) /* 4 bytes of 0s, 1 addr, 16 ipv4, 4 ttl. */
+ return -1;
+ if (get_uint8(payload + 4) != 6)
+ return -1;
+ tor_addr_from_ipv6_bytes(addr_out, (char*)(payload + 5));
+ bytes = ntohl(get_uint32(payload + 21));
+ if (bytes <= INT32_MAX)
+ *ttl_out = (int) bytes;
+ }
+ return 0;
+}
+
/** An incoming relay cell has arrived from circuit <b>circ</b> to
* stream <b>conn</b>.
*
@@ -874,6 +926,8 @@ connection_edge_process_relay_cell_not_open(
if (conn->base_.type == CONN_TYPE_AP &&
rh->command == RELAY_COMMAND_CONNECTED) {
+ tor_addr_t addr;
+ int ttl;
entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
tor_assert(CIRCUIT_IS_ORIGIN(circ));
if (conn->base_.state != AP_CONN_STATE_CONNECT_WAIT) {
@@ -884,27 +938,41 @@ connection_edge_process_relay_cell_not_open(
conn->base_.state = AP_CONN_STATE_OPEN;
log_info(LD_APP,"'connected' received after %d seconds.",
(int)(time(NULL) - conn->base_.timestamp_lastread));
- if (rh->length >= 4) {
- uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
- int ttl;
- if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
- is_internal_IP(addr, 0))) {
+ if (connected_cell_parse(rh, cell, &addr, &ttl) < 0) {
+ log_fn(LOG_PROTOCOL_WARN, LD_APP,
+ "Got a badly formatted connected cell. Closing.");
+ connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
+ connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
+ }
+ if (tor_addr_family(&addr) != AF_UNSPEC) {
+ const sa_family_t family = tor_addr_family(&addr);
+ if (tor_addr_is_null(&addr) ||
+ (get_options()->ClientDNSRejectInternalAddresses &&
+ tor_addr_is_internal(&addr, 0))) {
log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
- fmt_addr32(addr));
+ fmt_addr(&addr));
+ connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
+ connection_mark_unattached_ap(entry_conn,
+ END_STREAM_REASON_TORPROTOCOL);
+ return 0;
+ }
+
+ if ((family == AF_INET && ! entry_conn->ipv4_traffic_ok) ||
+ (family == AF_INET6 && ! entry_conn->ipv6_traffic_ok)) {
+ log_fn(LOG_PROTOCOL_WARN, LD_APP,
+ "Got a connected cell to %s with unsupported address family."
+ " Closing.", fmt_addr(&addr));
connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
connection_mark_unattached_ap(entry_conn,
END_STREAM_REASON_TORPROTOCOL);
return 0;
}
- if (rh->length >= 8)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+4));
- else
- ttl = -1;
+
client_dns_set_addressmap(TO_ORIGIN_CIRCUIT(circ),
- entry_conn->socks_request->address, addr,
+ entry_conn->socks_request->address, &addr,
entry_conn->chosen_exit_name, ttl);
- remap_event_helper(entry_conn, addr);
+ remap_event_helper(entry_conn, &addr);
}
circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
/* don't send a socks reply to transparent conns */
@@ -994,8 +1062,14 @@ connection_edge_process_relay_cell_not_open(
ttl,
-1);
if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
- uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
- remap_event_helper(entry_conn, addr);
+ tor_addr_t addr;
+ tor_addr_from_ipv4n(&addr, get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
+ remap_event_helper(entry_conn, &addr);
+ } else if (answer_type == RESOLVED_TYPE_IPV6 && answer_len == 16) {
+ tor_addr_t addr;
+ tor_addr_from_ipv6_bytes(&addr,
+ (char*)(cell->payload+RELAY_HEADER_SIZE+2));
+ remap_event_helper(entry_conn, &addr);
}
connection_mark_unattached_ap(entry_conn,
END_STREAM_REASON_DONE |
diff --git a/src/or/relay.h b/src/or/relay.h
index 0f7b45f..57400fd 100644
--- a/src/or/relay.h
+++ b/src/or/relay.h
@@ -68,6 +68,8 @@ void circuit_clear_cell_queue(circuit_t *circ, channel_t *chan);
#ifdef RELAY_PRIVATE
int relay_crypt(circuit_t *circ, cell_t *cell, cell_direction_t cell_direction,
crypt_path_t **layer_hint, char *recognized);
+int connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
+ tor_addr_t *addr_out, int *ttl_out);
#endif
#endif
diff --git a/src/test/test_cell_formats.c b/src/test/test_cell_formats.c
index 2199a05..3fac745 100644
--- a/src/test/test_cell_formats.c
+++ b/src/test/test_cell_formats.c
@@ -6,6 +6,7 @@
#include "orconfig.h"
#define CONNECTION_EDGE_PRIVATE
+#define RELAY_PRIVATE
#include "or.h"
#include "connection_edge.h"
#include "relay.h"
@@ -223,12 +224,125 @@ test_cfmt_begin_cells(void *arg)
tor_free(bcell.address);
}
+static void
+test_cfmt_connected_cells(void *arg)
+{
+ relay_header_t rh;
+ cell_t cell;
+ tor_addr_t addr;
+ int ttl, r;
+ (void)arg;
+
+ /* Let's try an oldschool one with nothing in it. */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "", 0);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_UNSPEC);
+ tt_int_op(ttl, ==, -1);
+
+ /* A slightly less oldschool one: only an IPv4 address */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x20\x30\x40\x50", 4);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_INET);
+ tt_str_op(fmt_addr(&addr), ==, "32.48.64.80");
+ tt_int_op(ttl, ==, -1);
+
+ /* Bogus but understandable: truncated TTL */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x11\x12\x13\x14\x15", 5);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_INET);
+ tt_str_op(fmt_addr(&addr), ==, "17.18.19.20");
+ tt_int_op(ttl, ==, -1);
+
+ /* Regular IPv4 one: address and TTL */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x02\x03\x04\x05\x00\x00\x0e\x10", 8);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_INET);
+ tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
+ tt_int_op(ttl, ==, 3600);
+
+ /* IPv4 with too-big TTL */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x02\x03\x04\x05\xf0\x00\x00\x00", 8);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_INET);
+ tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
+ tt_int_op(ttl, ==, -1);
+
+ /* IPv6 (ttl is mandatory) */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x00\x00\x00\x00\x06"
+ "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
+ "\x00\x00\x00\x00\x00\x00\x00\x68"
+ "\x00\x00\x02\x58", 25);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
+ tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
+ tt_int_op(ttl, ==, 600);
+
+ /* IPv6 (ttl too big) */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x00\x00\x00\x00\x06"
+ "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
+ "\x00\x00\x00\x00\x00\x00\x00\x68"
+ "\x90\x00\x02\x58", 25);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, 0);
+ tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
+ tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
+ tt_int_op(ttl, ==, -1);
+
+ /* Bogus size: 3. */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x00\x01\x02", 3);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, -1);
+
+ /* Bogus family: 7. */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x00\x00\x00\x00\x07"
+ "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
+ "\x00\x00\x00\x00\x00\x00\x00\x68"
+ "\x90\x00\x02\x58", 25);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, -1);
+
+ /* Truncated IPv6. */
+ make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
+ "\x00\x00\x00\x00\x06"
+ "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
+ "\x00\x00\x00\x00\x00\x00\x00\x68"
+ "\x00\x00\x02", 24);
+ relay_header_unpack(&rh, cell.payload);
+ r = connected_cell_parse(&rh, &cell, &addr, &ttl);
+ tt_int_op(r, ==, -1);
+
+ done:
+ ;
+}
+
#define TEST(name, flags) \
{ #name, test_cfmt_ ## name, flags, 0, NULL }
struct testcase_t cell_format_tests[] = {
TEST(relay_header, 0),
TEST(begin_cells, 0),
+ TEST(connected_cells, 0),
END_OF_TESTCASES
};
1
0