update copyright notices.
[tor.git] / src / or / dns.c
blobb595668f5334d72eb7f5c32669b0704acde9da11
1 /* Copyright 2003-2004 Roger Dingledine.
2 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
5 const char dns_c_id[] = "$Id$";
7 /**
8 * \file dns.c
9 * \brief Resolve hostnames in separate processes.
10 **/
12 /* See http://elvin.dstc.com/ListArchive/elvin-dev/archive/2001/09/msg00027.html
13 * for some approaches to asynchronous dns. We will want to switch once one of
14 * them becomes more commonly available.
17 #include "or.h"
18 #include "tree.h"
20 /** Longest hostname we're willing to resolve. */
21 #define MAX_ADDRESSLEN 256
23 /** Maximum DNS processes to spawn. */
24 #define MAX_DNSWORKERS 100
25 /** Minimum DNS processes to spawn. */
26 #define MIN_DNSWORKERS 3
28 /** If more than this many processes are idle, shut down the extras. */
29 #define MAX_IDLE_DNSWORKERS 10
31 /** Possible outcomes from hostname lookup: permanent failure,
32 * transient (retryable) failure, and success. */
33 #define DNS_RESOLVE_FAILED_TRANSIENT 1
34 #define DNS_RESOLVE_FAILED_PERMANENT 2
35 #define DNS_RESOLVE_SUCCEEDED 3
37 /** How many dnsworkers we have running right now. */
38 static int num_dnsworkers=0;
39 /** How many of the running dnsworkers have an assigned task right now. */
40 static int num_dnsworkers_busy=0;
41 /** When did we last rotate the dnsworkers? */
42 static time_t last_rotation_time=0;
44 /** Linked list of connections waiting for a DNS answer. */
45 struct pending_connection_t {
46 struct connection_t *conn;
47 struct pending_connection_t *next;
50 /** A DNS request: possibly completed, possibly pending; cached_resolve
51 * structs are stored at the OR side in a splay tree, and as a linked
52 * list from oldest to newest.
54 struct cached_resolve {
55 SPLAY_ENTRY(cached_resolve) node;
56 char address[MAX_ADDRESSLEN]; /**< The hostname to be resolved. */
57 uint32_t addr; /**< IPv4 addr for <b>address</b>. */
58 char state; /**< 0 is pending; 1 means answer is valid; 2 means resolve failed. */
59 #define CACHE_STATE_PENDING 0
60 #define CACHE_STATE_VALID 1
61 #define CACHE_STATE_FAILED 2
62 uint32_t expire; /**< Remove items from cache after this time. */
63 struct pending_connection_t *pending_connections;
64 struct cached_resolve *next;
67 static void purge_expired_resolves(uint32_t now);
68 static int assign_to_dnsworker(connection_t *exitconn);
69 static void dns_purge_resolve(struct cached_resolve *resolve);
70 static void dns_found_answer(char *address, uint32_t addr, char outcome);
71 static int dnsworker_main(void *data);
72 static int spawn_dnsworker(void);
73 static void spawn_enough_dnsworkers(void);
74 static void send_resolved_cell(connection_t *conn, uint8_t answer_type);
76 /** Splay tree of cached_resolve objects. */
77 static SPLAY_HEAD(cache_tree, cached_resolve) cache_root;
79 /** Function to compare hashed resolves on their addresses; used to
80 * implement splay trees. */
81 static int compare_cached_resolves(struct cached_resolve *a,
82 struct cached_resolve *b) {
83 /* make this smarter one day? */
84 return strncasecmp(a->address, b->address, MAX_ADDRESSLEN);
87 SPLAY_PROTOTYPE(cache_tree, cached_resolve, node, compare_cached_resolves);
88 SPLAY_GENERATE(cache_tree, cached_resolve, node, compare_cached_resolves);
90 /** Initialize the DNS cache. */
91 static void init_cache_tree(void) {
92 SPLAY_INIT(&cache_root);
95 /** Initialize the DNS subsystem; called by the OR process. */
96 void dns_init(void) {
97 init_cache_tree();
98 last_rotation_time=time(NULL);
99 spawn_enough_dnsworkers();
102 static void
103 _free_cached_resolve(struct cached_resolve *r) {
104 while (r->pending_connections) {
105 struct pending_connection_t *victim = r->pending_connections;
106 r->pending_connections = victim->next;
107 tor_free(victim);
109 tor_free(r);
112 void
113 dns_free_all(void)
115 struct cached_resolve *ptr, *next;
116 for (ptr = SPLAY_MIN(cache_tree, &cache_root); ptr != NULL; ptr = next) {
117 next = SPLAY_NEXT(cache_tree, &cache_root, ptr);
118 SPLAY_REMOVE(cache_tree, &cache_root, ptr);
119 _free_cached_resolve(ptr);
123 /** Linked list of resolved addresses, oldest to newest. */
124 static struct cached_resolve *oldest_cached_resolve = NULL;
125 static struct cached_resolve *newest_cached_resolve = NULL;
127 /** Remove every cached_resolve whose <b>expire</b> time is before <b>now</b>
128 * from the cache. */
129 static void purge_expired_resolves(uint32_t now) {
130 struct cached_resolve *resolve;
131 struct pending_connection_t *pend;
132 connection_t *pendconn;
134 /* this is fast because the linked list
135 * oldest_cached_resolve is ordered by when they came in.
137 while (oldest_cached_resolve && (oldest_cached_resolve->expire < now)) {
138 resolve = oldest_cached_resolve;
139 log(LOG_DEBUG,"Forgetting old cached resolve (expires %lu)", (unsigned long)resolve->expire);
140 if (resolve->state == CACHE_STATE_PENDING) {
141 log_fn(LOG_WARN,"Bug: Expiring a dns resolve that's still pending. Forgot to cull it?");
142 #ifdef TOR_FRAGILE
143 tor_assert(0);
144 #endif
146 if (resolve->pending_connections) {
147 log_fn(LOG_WARN, "Closing pending connections on expiring DNS resolve!");
148 #ifdef TOR_FRAGILE
149 tor_assert(0);
150 #endif
151 while (resolve->pending_connections) {
152 pend = resolve->pending_connections;
153 resolve->pending_connections = pend->next;
154 /* Connections should only be pending if they have no socket. */
155 tor_assert(pend->conn->s == -1);
156 pendconn = pend->conn;
157 connection_edge_end(pendconn, END_STREAM_REASON_TIMEOUT,
158 pendconn->cpath_layer);
159 circuit_detach_stream(circuit_get_by_conn(pendconn), pendconn);
160 connection_free(pendconn);
161 tor_free(pend);
164 oldest_cached_resolve = resolve->next;
165 if (!oldest_cached_resolve) /* if there are no more, */
166 newest_cached_resolve = NULL; /* then make sure the list's tail knows that too */
167 SPLAY_REMOVE(cache_tree, &cache_root, resolve);
168 tor_free(resolve);
172 static void send_resolved_cell(connection_t *conn, uint8_t answer_type)
174 char buf[RELAY_PAYLOAD_SIZE];
175 size_t buflen;
177 buf[0] = answer_type;
179 switch (answer_type)
181 case RESOLVED_TYPE_IPV4:
182 buf[1] = 4;
183 set_uint32(buf+2, htonl(conn->addr));
184 buflen = 6;
185 break;
186 case RESOLVED_TYPE_ERROR_TRANSIENT:
187 case RESOLVED_TYPE_ERROR:
188 buf[1] = 24; /* length of "error resolving hostname" */
189 strlcpy(buf+2, "error resolving hostname", sizeof(buf)-2);
190 buflen = 26;
191 break;
192 default:
193 tor_assert(0);
195 connection_edge_send_command(conn, circuit_get_by_conn(conn),
196 RELAY_COMMAND_RESOLVED, buf, buflen,
197 conn->cpath_layer);
200 /** Link <b>r</b> into the tree of address-to-result mappings, and add it to
201 * the linked list of resolves-by-age. */
202 static void
203 insert_resolve(struct cached_resolve *r)
205 /* add us to the linked list of resolves */
206 if (!oldest_cached_resolve) {
207 oldest_cached_resolve = r;
208 } else {
209 newest_cached_resolve->next = r;
211 newest_cached_resolve = r;
213 SPLAY_INSERT(cache_tree, &cache_root, r);
216 /** See if we have a cache entry for <b>exitconn</b>-\>address. if so,
217 * if resolve valid, put it into <b>exitconn</b>-\>addr and return 1.
218 * If resolve failed, unlink exitconn if needed, free it, and return -1.
220 * Else, if seen before and pending, add conn to the pending list,
221 * and return 0.
223 * Else, if not seen before, add conn to pending list, hand to
224 * dns farm, and return 0.
226 int dns_resolve(connection_t *exitconn) {
227 struct cached_resolve *resolve;
228 struct cached_resolve search;
229 struct pending_connection_t *pending_connection;
230 struct in_addr in;
231 circuit_t *circ;
232 uint32_t now = time(NULL);
233 assert_connection_ok(exitconn, 0);
234 tor_assert(exitconn->s == -1);
236 /* first check if exitconn->address is an IP. If so, we already
237 * know the answer. */
238 if (tor_inet_aton(exitconn->address, &in) != 0) {
239 exitconn->addr = ntohl(in.s_addr);
240 if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
241 send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
242 return 1;
245 /* then take this opportunity to see if there are any expired
246 * resolves in the tree. */
247 purge_expired_resolves(now);
249 /* now check the tree to see if 'address' is already there. */
250 strlcpy(search.address, exitconn->address, sizeof(search.address));
251 resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
252 if (resolve) { /* already there */
253 switch (resolve->state) {
254 case CACHE_STATE_PENDING:
255 /* add us to the pending list */
256 pending_connection = tor_malloc_zero(
257 sizeof(struct pending_connection_t));
258 pending_connection->conn = exitconn;
259 pending_connection->next = resolve->pending_connections;
260 resolve->pending_connections = pending_connection;
261 log_fn(LOG_DEBUG,"Connection (fd %d) waiting for pending DNS resolve of '%s'",
262 exitconn->s, exitconn->address);
263 exitconn->state = EXIT_CONN_STATE_RESOLVING;
264 return 0;
265 case CACHE_STATE_VALID:
266 exitconn->addr = resolve->addr;
267 log_fn(LOG_DEBUG,"Connection (fd %d) found cached answer for '%s'",
268 exitconn->s, exitconn->address);
269 if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
270 send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
271 return 1;
272 case CACHE_STATE_FAILED:
273 log_fn(LOG_DEBUG,"Connection (fd %d) found cached error for '%s'",
274 exitconn->s, exitconn->address);
275 if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
276 send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
277 circ = circuit_get_by_conn(exitconn);
278 if (circ)
279 circuit_detach_stream(circ, exitconn);
280 if (!exitconn->marked_for_close)
281 connection_free(exitconn);
282 return -1;
284 tor_assert(0);
286 /* not there, need to add it */
287 resolve = tor_malloc_zero(sizeof(struct cached_resolve));
288 resolve->state = CACHE_STATE_PENDING;
289 resolve->expire = now + MAX_DNS_ENTRY_AGE;
290 strlcpy(resolve->address, exitconn->address, sizeof(resolve->address));
292 /* add us to the pending list */
293 pending_connection = tor_malloc_zero(sizeof(struct pending_connection_t));
294 pending_connection->conn = exitconn;
295 resolve->pending_connections = pending_connection;
296 exitconn->state = EXIT_CONN_STATE_RESOLVING;
298 insert_resolve(resolve);
299 return assign_to_dnsworker(exitconn);
302 /** Find or spawn a dns worker process to handle resolving
303 * <b>exitconn</b>-\>address; tell that dns worker to begin resolving.
305 static int assign_to_dnsworker(connection_t *exitconn) {
306 connection_t *dnsconn;
307 unsigned char len;
309 tor_assert(exitconn->state == EXIT_CONN_STATE_RESOLVING);
310 tor_assert(exitconn->s == -1);
312 spawn_enough_dnsworkers(); /* respawn here, to be sure there are enough */
314 dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE);
316 if (!dnsconn) {
317 log_fn(LOG_WARN,"no idle dns workers. Failing.");
318 if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
319 send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
320 dns_cancel_pending_resolve(exitconn->address); /* also sends end and frees! */
321 return -1;
324 log_fn(LOG_DEBUG, "Connection (fd %d) needs to resolve '%s'; assigning to DNSWorker (fd %d)",
325 exitconn->s, exitconn->address, dnsconn->s);
327 tor_free(dnsconn->address);
328 dnsconn->address = tor_strdup(exitconn->address);
329 dnsconn->state = DNSWORKER_STATE_BUSY;
330 num_dnsworkers_busy++;
332 len = strlen(dnsconn->address);
333 connection_write_to_buf(&len, 1, dnsconn);
334 connection_write_to_buf(dnsconn->address, len, dnsconn);
336 // log_fn(LOG_DEBUG,"submitted '%s'", exitconn->address);
337 return 0;
340 /** Remove <b>conn</b> from the list of connections waiting for conn-\>address.
342 void connection_dns_remove(connection_t *conn)
344 struct pending_connection_t *pend, *victim;
345 struct cached_resolve search;
346 struct cached_resolve *resolve;
348 tor_assert(conn->type == CONN_TYPE_EXIT);
349 tor_assert(conn->state == EXIT_CONN_STATE_RESOLVING);
351 strlcpy(search.address, conn->address, sizeof(search.address));
353 resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
354 if (!resolve) {
355 log_fn(LOG_NOTICE,"Address '%s' is not pending. Dropping.", conn->address);
356 return;
359 tor_assert(resolve->pending_connections);
360 assert_connection_ok(conn,0);
362 pend = resolve->pending_connections;
364 if (pend->conn == conn) {
365 resolve->pending_connections = pend->next;
366 tor_free(pend);
367 log_fn(LOG_DEBUG, "First connection (fd %d) no longer waiting for resolve of '%s'",
368 conn->s, conn->address);
369 return;
370 } else {
371 for ( ; pend->next; pend = pend->next) {
372 if (pend->next->conn == conn) {
373 victim = pend->next;
374 pend->next = victim->next;
375 tor_free(victim);
376 log_fn(LOG_DEBUG, "Connection (fd %d) no longer waiting for resolve of '%s'",
377 conn->s, conn->address);
378 return; /* more are pending */
381 tor_assert(0); /* not reachable unless onlyconn not in pending list */
385 /** Log an error and abort if conn is waiting for a DNS resolve.
387 void assert_connection_edge_not_dns_pending(connection_t *conn) {
388 struct pending_connection_t *pend;
389 struct cached_resolve *resolve;
391 SPLAY_FOREACH(resolve, cache_tree, &cache_root) {
392 for (pend = resolve->pending_connections;
393 pend;
394 pend = pend->next) {
395 tor_assert(pend->conn != conn);
400 /** Log an error and abort if any connection waiting for a DNS resolve is
401 * corrupted. */
402 void assert_all_pending_dns_resolves_ok(void) {
403 struct pending_connection_t *pend;
404 struct cached_resolve *resolve;
406 SPLAY_FOREACH(resolve, cache_tree, &cache_root) {
407 for (pend = resolve->pending_connections;
408 pend;
409 pend = pend->next) {
410 assert_connection_ok(pend->conn, 0);
411 tor_assert(pend->conn->s == -1);
412 tor_assert(!connection_in_array(pend->conn));
417 /** Mark all connections waiting for <b>address</b> for close. Then cancel
418 * the resolve for <b>address</b> itself, and remove any cached results for
419 * <b>address</b> from the cache.
421 void dns_cancel_pending_resolve(char *address) {
422 struct pending_connection_t *pend;
423 struct cached_resolve search;
424 struct cached_resolve *resolve;
425 connection_t *pendconn;
426 circuit_t *circ;
428 strlcpy(search.address, address, sizeof(search.address));
430 resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
431 if (!resolve) {
432 log_fn(LOG_NOTICE,"Address '%s' is not pending. Dropping.", address);
433 return;
436 if (!resolve->pending_connections) {
437 /* XXX this should never trigger, but sometimes it does */
438 log_fn(LOG_WARN,"Bug: Address '%s' is pending but has no pending connections!", address);
439 #ifdef TOR_FRAGILE
440 tor_assert(0);
441 #endif
442 return;
444 tor_assert(resolve->pending_connections);
446 /* mark all pending connections to fail */
447 log_fn(LOG_DEBUG, "Failing all connections waiting on DNS resolve of '%s'",
448 address);
449 while (resolve->pending_connections) {
450 pend = resolve->pending_connections;
451 pend->conn->state = EXIT_CONN_STATE_RESOLVEFAILED;
452 pendconn = pend->conn;
453 tor_assert(pendconn->s == -1);
454 if (!pendconn->marked_for_close) {
455 connection_edge_end(pendconn, END_STREAM_REASON_RESOURCELIMIT,
456 pendconn->cpath_layer);
458 circ = circuit_get_by_conn(pendconn);
459 if (circ)
460 circuit_detach_stream(circ, pendconn);
461 connection_free(pendconn);
462 resolve->pending_connections = pend->next;
463 tor_free(pend);
466 dns_purge_resolve(resolve);
469 /** Remove <b>resolve</b> from the cache.
471 static void dns_purge_resolve(struct cached_resolve *resolve) {
472 struct cached_resolve *tmp;
474 /* remove resolve from the linked list */
475 if (resolve == oldest_cached_resolve) {
476 oldest_cached_resolve = resolve->next;
477 if (oldest_cached_resolve == NULL)
478 newest_cached_resolve = NULL;
479 } else {
480 /* FFFF make it a doubly linked list if this becomes too slow */
481 for (tmp=oldest_cached_resolve; tmp && tmp->next != resolve; tmp=tmp->next) ;
482 tor_assert(tmp); /* it's got to be in the list, or we screwed up somewhere else */
483 tmp->next = resolve->next; /* unlink it */
485 if (newest_cached_resolve == resolve)
486 newest_cached_resolve = tmp;
489 /* remove resolve from the tree */
490 SPLAY_REMOVE(cache_tree, &cache_root, resolve);
492 tor_free(resolve);
495 /** Called on the OR side when a DNS worker tells us the outcome of a DNS
496 * resolve: tell all pending connections about the result of the lookup, and
497 * cache the value. (<b>address</b> is a NUL-terminated string containing the
498 * address to look up; <b>addr</b> is an IPv4 address in host order;
499 * <b>outcome</b> is one of
500 * DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
502 static void dns_found_answer(char *address, uint32_t addr, char outcome) {
503 struct pending_connection_t *pend;
504 struct cached_resolve search;
505 struct cached_resolve *resolve;
506 connection_t *pendconn;
507 circuit_t *circ;
509 strlcpy(search.address, address, sizeof(search.address));
511 resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
512 if (!resolve) {
513 log_fn(LOG_INFO,"Resolved unasked address '%s'; caching anyway.", address);
514 resolve = tor_malloc_zero(sizeof(struct cached_resolve));
515 resolve->state = (outcome == DNS_RESOLVE_SUCCEEDED) ?
516 CACHE_STATE_VALID : CACHE_STATE_FAILED;
517 resolve->addr = addr;
518 resolve->expire = time(NULL) + MAX_DNS_ENTRY_AGE;
519 insert_resolve(resolve);
520 return;
523 if (resolve->state != CACHE_STATE_PENDING) {
524 /* XXXX Maybe update addr? or check addr for consistency? Or let
525 * VALID replace FAILED? */
526 log_fn(LOG_NOTICE, "Resolved '%s' which was already resolved; ignoring",
527 address);
528 tor_assert(resolve->pending_connections == NULL);
529 return;
531 /* Removed this assertion: in fact, we'll sometimes get a double answer
532 * to the same question. This can happen when we ask one worker to resolve
533 * X.Y.Z., then we cancel the request, and then we ask another worker to
534 * resolve X.Y.Z. */
535 /* tor_assert(resolve->state == CACHE_STATE_PENDING); */
537 resolve->addr = addr;
538 if (outcome == DNS_RESOLVE_SUCCEEDED)
539 resolve->state = CACHE_STATE_VALID;
540 else
541 resolve->state = CACHE_STATE_FAILED;
543 while (resolve->pending_connections) {
544 pend = resolve->pending_connections;
545 assert_connection_ok(pend->conn,time(NULL));
546 pend->conn->addr = resolve->addr;
547 pendconn = pend->conn; /* don't pass complex things to the
548 connection_mark_for_close macro */
550 if (resolve->state == CACHE_STATE_FAILED) {
551 /* prevent double-remove. */
552 pendconn->state = EXIT_CONN_STATE_RESOLVEFAILED;
553 if (pendconn->purpose == EXIT_PURPOSE_CONNECT) {
554 connection_edge_end(pendconn, END_STREAM_REASON_RESOLVEFAILED, pendconn->cpath_layer);
555 /* This detach must happen after we send the end cell. */
556 circuit_detach_stream(circuit_get_by_conn(pendconn), pendconn);
557 } else {
558 send_resolved_cell(pendconn, RESOLVED_TYPE_ERROR);
559 /* This detach must happen after we send the resolved cell. */
560 circuit_detach_stream(circuit_get_by_conn(pendconn), pendconn);
562 connection_free(pendconn);
563 } else {
564 if (pendconn->purpose == EXIT_PURPOSE_CONNECT) {
565 /* prevent double-remove. */
566 pend->conn->state = EXIT_CONN_STATE_CONNECTING;
568 circ = circuit_get_by_conn(pend->conn);
569 tor_assert(circ);
570 /* unlink pend->conn from resolving_streams, */
571 circuit_detach_stream(circ, pend->conn);
572 /* and link it to n_streams */
573 pend->conn->next_stream = circ->n_streams;
574 circ->n_streams = pend->conn;
576 connection_exit_connect(pend->conn);
577 } else {
578 /* prevent double-remove. This isn't really an accurate state,
579 * but it does the right thing. */
580 pendconn->state = EXIT_CONN_STATE_RESOLVEFAILED;
581 send_resolved_cell(pendconn, RESOLVED_TYPE_IPV4);
582 circ = circuit_get_by_conn(pendconn);
583 tor_assert(circ);
584 circuit_detach_stream(circ, pendconn);
585 connection_free(pendconn);
588 resolve->pending_connections = pend->next;
589 tor_free(pend);
592 if (outcome == DNS_RESOLVE_FAILED_TRANSIENT) { /* remove from cache */
593 dns_purge_resolve(resolve);
597 /******************************************************************/
600 * Connection between OR and dnsworker
603 /** Write handler: called when we've pushed a request to a dnsworker. */
604 int connection_dns_finished_flushing(connection_t *conn) {
605 tor_assert(conn);
606 tor_assert(conn->type == CONN_TYPE_DNSWORKER);
607 connection_stop_writing(conn);
608 return 0;
611 int connection_dns_reached_eof(connection_t *conn) {
612 log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
613 if (conn->state == DNSWORKER_STATE_BUSY) {
614 /* don't cancel the resolve here -- it would be cancelled in
615 * connection_about_to_close_connection(), since conn is still
616 * in state BUSY
618 num_dnsworkers_busy--;
620 num_dnsworkers--;
621 connection_mark_for_close(conn);
622 return 0;
625 /** Read handler: called when we get data from a dnsworker. See
626 * if we have a complete answer. If so, call dns_found_answer on the
627 * result. If not, wait. Returns 0. */
628 int connection_dns_process_inbuf(connection_t *conn) {
629 char success;
630 uint32_t addr;
632 tor_assert(conn);
633 tor_assert(conn->type == CONN_TYPE_DNSWORKER);
635 if (conn->state != DNSWORKER_STATE_BUSY && buf_datalen(conn->inbuf)) {
636 log_fn(LOG_WARN,"Bug: read data from an idle dns worker. Please report.");
637 #ifdef TOR_FRAGILE
638 tor_assert(0);
639 #endif
640 return 0;
642 if (buf_datalen(conn->inbuf) < 5) /* entire answer available? */
643 return 0; /* not yet */
644 tor_assert(conn->state == DNSWORKER_STATE_BUSY);
645 tor_assert(buf_datalen(conn->inbuf) == 5);
647 connection_fetch_from_buf(&success,1,conn);
648 connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
650 log_fn(LOG_DEBUG, "DNSWorker (fd %d) returned answer for '%s'",
651 conn->s, conn->address);
653 tor_assert(success >= DNS_RESOLVE_FAILED_TRANSIENT);
654 tor_assert(success <= DNS_RESOLVE_SUCCEEDED);
655 dns_found_answer(conn->address, ntohl(addr), success);
657 tor_free(conn->address);
658 conn->address = tor_strdup("<idle>");
659 conn->state = DNSWORKER_STATE_IDLE;
660 num_dnsworkers_busy--;
661 if (conn->timestamp_created < last_rotation_time) {
662 connection_mark_for_close(conn);
663 num_dnsworkers--;
664 spawn_enough_dnsworkers();
666 return 0;
669 /** Close and re-open all idle dnsworkers; schedule busy ones to be closed
670 * and re-opened once they're no longer busy.
672 void dnsworkers_rotate(void)
674 connection_t *dnsconn;
675 log_fn(LOG_INFO, "Rotating DNS workers.");
676 while ((dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
677 DNSWORKER_STATE_IDLE))) {
678 connection_mark_for_close(dnsconn);
679 num_dnsworkers--;
681 last_rotation_time = time(NULL);
682 spawn_enough_dnsworkers();
685 /** Implementation for DNS workers; this code runs in a separate
686 * execution context. It takes as its argument an fdarray as returned
687 * by socketpair(), and communicates via fdarray[1]. The protocol is
688 * as follows:
689 * - The OR says:
690 * - ADDRESSLEN [1 byte]
691 * - ADDRESS [ADDRESSLEN bytes]
692 * - The DNS worker does the lookup, and replies:
693 * - OUTCOME [1 byte]
694 * - IP [4 bytes]
696 * OUTCOME is one of DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
697 * IP is in host order.
699 * The dnsworker runs indefinitely, until its connection is closed or an error
700 * occurs.
702 static int dnsworker_main(void *data) {
703 char address[MAX_ADDRESSLEN];
704 unsigned char address_len;
705 char answer[5];
706 uint32_t ip;
707 int *fdarray = data;
708 int fd;
709 int result;
711 /* log_fn(LOG_NOTICE,"After spawn: fdarray @%d has %d:%d", (int)fdarray, fdarray[0],fdarray[1]); */
713 fd = fdarray[1]; /* this side is ours */
714 #ifndef TOR_IS_MULTITHREADED
715 tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
716 connection_free_all(); /* so the child doesn't hold the parent's fd's open */
717 handle_signals(0); /* ignore interrupts from the keyboard, etc */
718 #endif
719 tor_free(data);
721 for (;;) {
722 int r;
724 if ((r = recv(fd, &address_len, 1, 0)) != 1) {
725 if (r == 0) {
726 log_fn(LOG_INFO,"DNS worker exiting because Tor process closed connection (either pruned idle dnsworker or died).");
727 } else {
728 log_fn(LOG_INFO,"DNS worker exiting because of error on connection to Tor process.");
729 log_fn(LOG_INFO,"(Error on %d was %s)", fd, tor_socket_strerror(tor_socket_errno(fd)));
731 spawn_exit();
734 if (address_len && read_all(fd, address, address_len, 1) != address_len) {
735 log_fn(LOG_ERR,"read hostname failed. Child exiting.");
736 spawn_exit();
738 address[address_len] = 0; /* null terminate it */
740 result = tor_lookup_hostname(address, &ip);
741 /* Make 0.0.0.0 an error, so that we can use "0" to mean "no addr") */
742 if (!ip)
743 result = -1;
744 switch (result) {
745 case 1:
746 /* XXX result can never be 1, because we set it to -1 above on error */
747 log_fn(LOG_INFO,"Could not resolve dest addr %s (transient).",address);
748 answer[0] = DNS_RESOLVE_FAILED_TRANSIENT;
749 break;
750 case -1:
751 log_fn(LOG_INFO,"Could not resolve dest addr %s (permanent).",address);
752 answer[0] = DNS_RESOLVE_FAILED_PERMANENT;
753 break;
754 case 0:
755 log_fn(LOG_INFO,"Resolved address '%s'.",address);
756 answer[0] = DNS_RESOLVE_SUCCEEDED;
757 break;
759 set_uint32(answer+1, ip);
760 if (write_all(fd, answer, 5, 1) != 5) {
761 log_fn(LOG_ERR,"writing answer failed. Child exiting.");
762 spawn_exit();
765 return 0; /* windows wants this function to return an int */
768 /** Launch a new DNS worker; return 0 on success, -1 on failure.
770 static int spawn_dnsworker(void) {
771 int *fdarray;
772 int fd;
773 connection_t *conn;
775 fdarray = tor_malloc(sizeof(int)*2);
776 if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) {
777 log(LOG_ERR, "Couldn't construct socketpair: %s",
778 tor_socket_strerror(tor_socket_errno(-1)));
779 tor_cleanup();
780 tor_free(fdarray);
781 exit(1);
784 /* log_fn(LOG_NOTICE,"Before spawn: fdarray @%d has %d:%d", (int)fdarray, fdarray[0],fdarray[1]); */
786 fd = fdarray[0]; /* We copy this out here, since dnsworker_main may free fdarray */
787 spawn_func(dnsworker_main, (void*)fdarray);
788 log_fn(LOG_DEBUG,"just spawned a worker.");
789 #ifndef TOR_IS_MULTITHREADED
790 tor_close_socket(fdarray[1]); /* we don't need the worker's side of the pipe */
791 tor_free(fdarray);
792 #endif
794 conn = connection_new(CONN_TYPE_DNSWORKER);
796 set_socket_nonblocking(fd);
798 /* set up conn so it's got all the data we need to remember */
799 conn->s = fd;
800 conn->address = tor_strdup("<unused>");
802 if (connection_add(conn) < 0) { /* no space, forget it */
803 log_fn(LOG_WARN,"connection_add failed. Giving up.");
804 connection_free(conn); /* this closes fd */
805 return -1;
808 conn->state = DNSWORKER_STATE_IDLE;
809 connection_start_reading(conn);
811 return 0; /* success */
814 /** If we have too many or too few DNS workers, spawn or kill some.
816 static void spawn_enough_dnsworkers(void) {
817 int num_dnsworkers_needed; /* aim to have 1 more than needed,
818 * but no less than min and no more than max */
819 connection_t *dnsconn;
821 /* XXX This may not be the best strategy. Maybe we should queue pending
822 * requests until the old ones finish or time out: otherwise, if
823 * the connection requests come fast enough, we never get any DNS done. -NM
824 * XXX But if we queue them, then the adversary can pile even more
825 * queries onto us, blocking legitimate requests for even longer.
826 * Maybe we should compromise and only kill if it's been at it for
827 * more than, e.g., 2 seconds. -RD
829 if (num_dnsworkers_busy == MAX_DNSWORKERS) {
830 /* We always want at least one worker idle.
831 * So find the oldest busy worker and kill it.
833 dnsconn = connection_get_by_type_state_lastwritten(CONN_TYPE_DNSWORKER,
834 DNSWORKER_STATE_BUSY);
835 tor_assert(dnsconn);
837 log_fn(LOG_WARN, "%d DNS workers are spawned; all are busy. Killing one.",
838 MAX_DNSWORKERS);
840 connection_mark_for_close(dnsconn);
841 num_dnsworkers_busy--;
842 num_dnsworkers--;
845 if (num_dnsworkers_busy >= MIN_DNSWORKERS)
846 num_dnsworkers_needed = num_dnsworkers_busy+1;
847 else
848 num_dnsworkers_needed = MIN_DNSWORKERS;
850 while (num_dnsworkers < num_dnsworkers_needed) {
851 if (spawn_dnsworker() < 0) {
852 log(LOG_WARN,"spawn_enough_dnsworkers(): spawn failed!");
853 return;
855 num_dnsworkers++;
858 while (num_dnsworkers > num_dnsworkers_busy+MAX_IDLE_DNSWORKERS) { /* too many idle? */
859 /* cull excess workers */
860 log_fn(LOG_NOTICE,"%d of %d dnsworkers are idle. Killing one.",
861 num_dnsworkers-num_dnsworkers_needed, num_dnsworkers);
862 dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE);
863 tor_assert(dnsconn);
864 connection_mark_for_close(dnsconn);
865 num_dnsworkers--;