From 2c40138210fe934f687470fc4bd20513982bf424 Mon Sep 17 00:00:00 2001 From: Desoxy Date: Wed, 3 Apr 2013 18:50:27 +0200 Subject: [PATCH 1/2] Controller: Always send ADDRMAP event after RESOLVE command (#8596 part 1/2) Since 7536c40 only DNS results for real SOCKS requests are added to the cache, but not DNS results for DNSPort queries or control connection RESOLVE queries. Only cache additions would trigger ADDRMAP events on successful resolve. Change it so that DNS results received after a RESOLVE command also generate ADDRMAP events. --- src/or/connection_edge.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index 5075c474a3..0519708810 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -2040,11 +2040,8 @@ tell_controller_about_resolved_result(entry_connection_t *conn, int ttl, time_t expires) { - - if (ttl >= 0 && (answer_type == RESOLVED_TYPE_IPV4 || - answer_type == RESOLVED_TYPE_HOSTNAME)) { - return; /* we already told the controller. */ - } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) { + expires = time(NULL) + ttl; + if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) { char *cp = tor_dup_ip(ntohl(get_uint32(answer))); control_event_address_mapped(conn->socks_request->address, cp, expires, NULL); @@ -2116,8 +2113,9 @@ connection_ap_handshake_socks_resolved(entry_connection_t *conn, conn->socks_request->has_finished = 1; return; } else { - /* This must be a request from the controller. We already sent - * a mapaddress if there's a ttl. */ + /* This must be a request from the controller. Since answers to those + * requests are not cached, they do not generate an ADDRMAP event on + * their own. */ tell_controller_about_resolved_result(conn, answer_type, answer_len, (char*)answer, ttl, expires); conn->socks_request->has_finished = 1; From 74f5e304e49b8ccde2b97232c49e5025dba21f59 Mon Sep 17 00:00:00 2001 From: Desoxy Date: Wed, 3 Apr 2013 18:50:51 +0200 Subject: [PATCH 2/2] Controller: Add CACHED keyword to ADDRMAP events (#8596 part 2/2) Add keyword CACHED="YES"/"NO" to ADDRMAP control events to indicate whether the DNS response will be cached or not. --- changes/bug8596 | 3 +++ src/or/addressmap.c | 2 +- src/or/connection_edge.c | 9 ++++----- src/or/control.c | 14 ++++++++------ src/or/control.h | 3 ++- 5 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 changes/bug8596 diff --git a/changes/bug8596 b/changes/bug8596 new file mode 100644 index 0000000000..dd36bad855 --- /dev/null +++ b/changes/bug8596 @@ -0,0 +1,3 @@ + o Minor features: + - Add CACHED keyword to ADDRMAP events in the control protocol to indicate + whether a DNS result will be cached or not. diff --git a/src/or/addressmap.c b/src/or/addressmap.c index 826eb301db..79e4b7c5e2 100644 --- a/src/or/addressmap.c +++ b/src/or/addressmap.c @@ -560,7 +560,7 @@ addressmap_register(const char *address, char *new_address, time_t expires, 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); + control_event_address_mapped(address, ent->new_address, expires, NULL, 1); } /** An attempt to resolve address failed at some OR. diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index 0519708810..926fcab90c 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -2044,18 +2044,17 @@ tell_controller_about_resolved_result(entry_connection_t *conn, if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) { char *cp = tor_dup_ip(ntohl(get_uint32(answer))); control_event_address_mapped(conn->socks_request->address, - cp, expires, NULL); + cp, expires, NULL, 0); tor_free(cp); } else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) { char *cp = tor_strndup(answer, answer_len); control_event_address_mapped(conn->socks_request->address, - cp, expires, NULL); + cp, expires, NULL, 0); tor_free(cp); } else { control_event_address_mapped(conn->socks_request->address, - "", - time(NULL)+ttl, - "error=yes"); + "", time(NULL)+ttl, + "error=yes", 0); } } diff --git a/src/or/control.c b/src/or/control.c index 2a68464189..f50b87711c 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -2947,7 +2947,7 @@ handle_control_resolve(control_connection_t *conn, uint32_t len, send_control_done(conn); SMARTLIST_FOREACH(failed, const char *, arg, { control_event_address_mapped(arg, arg, time(NULL), - "internal"); + "internal", 0); }); SMARTLIST_FOREACH(args, char *, cp, tor_free(cp)); @@ -4024,15 +4024,17 @@ control_event_descriptors_changed(smartlist_t *routers) */ int control_event_address_mapped(const char *from, const char *to, time_t expires, - const char *error) + const char *error, const int cached) { if (!EVENT_IS_INTERESTING(EVENT_ADDRMAP)) return 0; if (expires < 3 || expires == TIME_MAX) send_control_event(EVENT_ADDRMAP, ALL_FORMATS, - "650 ADDRMAP %s %s NEVER %s\r\n", from, to, - error?error:""); + "650 ADDRMAP %s %s NEVER %s%s" + "CACHED=\"%s\"\r\n", + from, to, error?error:"", error?" ":"", + cached?"YES":"NO"); else { char buf[ISO_TIME_LEN+1]; char buf2[ISO_TIME_LEN+1]; @@ -4040,10 +4042,10 @@ control_event_address_mapped(const char *from, const char *to, time_t expires, format_iso_time(buf2,expires); send_control_event(EVENT_ADDRMAP, ALL_FORMATS, "650 ADDRMAP %s %s \"%s\"" - " %s%sEXPIRES=\"%s\"\r\n", + " %s%sEXPIRES=\"%s\" CACHED=\"%s\"\r\n", from, to, buf, error?error:"", error?" ":"", - buf2); + buf2, cached?"YES":"NO"); } return 0; diff --git a/src/or/control.h b/src/or/control.h index 51ae230b09..0ea7941b13 100644 --- a/src/or/control.h +++ b/src/or/control.h @@ -53,7 +53,8 @@ int control_event_stream_bandwidth_used(void); void control_event_logmsg(int severity, uint32_t domain, const char *msg); int control_event_descriptors_changed(smartlist_t *routers); int control_event_address_mapped(const char *from, const char *to, - time_t expires, const char *error); + time_t expires, const char *error, + const int cached); int control_event_or_authdir_new_descriptor(const char *action, const char *desc, size_t desclen,