diff options
author | Nick Mathewson <nickm@torproject.org> | 2007-01-29 18:13:42 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2007-01-29 18:13:42 +0000 |
commit | cd374f810cd35dddf102e5b3c0e301895884f0c2 (patch) | |
tree | b5138a76d2483bfa4bfa06f121ee2f8bb9ba6ea0 /src/or | |
parent | ac0dbc3d9b8f6abcf28637eaa20bf5fac33999ba (diff) | |
download | tor-cd374f810cd35dddf102e5b3c0e301895884f0c2.tar.gz tor-cd374f810cd35dddf102e5b3c0e301895884f0c2.zip |
r11586@catbus: nickm | 2007-01-29 13:13:27 -0500
Add a couple of fixes I turned up while writing regression tests for libevent: Allow DNS servers on ports other than 53, and handle TTLs correctly on reverse hostname lookups.
svn:r9458
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/eventdns.c | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/or/eventdns.c b/src/or/eventdns.c index 02b8c9a3e4..6bf195b4da 100644 --- a/src/or/eventdns.c +++ b/src/or/eventdns.c @@ -322,6 +322,8 @@ static void server_request_free_answers(struct server_request *req); static void server_port_free(struct evdns_server_port *port); static void server_port_ready_callback(int fd, short events, void *arg); +static int strtoint(const char *const str); + #ifdef WIN32 static int last_error(int sock) @@ -884,6 +886,7 @@ reply_parse(u8 *packet, int length) { if (name_parse(packet, length, &j, reply.data.ptr.name, sizeof(reply.data.ptr.name))<0) return -1; + ttl_r = MIN(ttl_r, ttl); reply.have_answer = 1; break; } else if (type == TYPE_AAAA && class == CLASS_INET) { @@ -2019,9 +2022,8 @@ evdns_resume(void) return 0; } -// exported function -int -evdns_nameserver_add(unsigned long int address) { +static int +_evdns_nameserver_add_impl(unsigned long int address, int port) { // first check to see if we already have this nameserver const struct nameserver *server = server_head, *const started_at = server_head; @@ -2051,7 +2053,7 @@ evdns_nameserver_add(unsigned long int address) { fcntl(ns->socket, F_SETFL, O_NONBLOCK); #endif sin.sin_addr.s_addr = address; - sin.sin_port = htons(53); + sin.sin_port = htons(port); sin.sin_family = AF_INET; if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) { err = 2; @@ -2096,11 +2098,37 @@ out1: // exported function int +evdns_nameserver_add(unsigned long int address) { + return _evdns_nameserver_add_impl(address, 53); +} + +// exported function +int evdns_nameserver_ip_add(const char *ip_as_string) { struct in_addr ina; - if (!inet_aton(ip_as_string, &ina)) + int port; + char buf[20]; + const char *cp; + cp = strchr(ip_as_string, ':'); + if (! cp) { + cp = ip_as_string; + port = 53; + } else { + port = strtoint(cp+1); + if (port < 0 || port > 65535) { + return 4; + } + if ((cp-ip_as_string) >= (int)sizeof(buf)) { + return 4; + } + memcpy(buf, ip_as_string, cp-ip_as_string); + buf[cp-ip_as_string] = '\0'; + cp = buf; + } + if (!inet_aton(cp, &ina)) { return 4; - return evdns_nameserver_add(ina.s_addr); + } + return _evdns_nameserver_add_impl(ina.s_addr, port); } // insert into the tail of the queue @@ -2711,7 +2739,7 @@ evdns_nameserver_ip_add_line(const char *ips) { while (ISSPACE(*ips) || *ips == ',' || *ips == '\t') ++ips; addr = ips; - while (ISDIGIT(*ips) || *ips == '.') + while (ISDIGIT(*ips) || *ips == '.' || *ips == ':') ++ips; buf = malloc(ips-addr+1); if (!buf) return 4; |