dns.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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$";
  6. /**
  7. * \file dns.c
  8. * \brief Implements a farm of 'DNS worker' threads or processes to
  9. * perform DNS lookups for onion routers and cache the results.
  10. * [This needs to be done in the background because of the lack of a
  11. * good, ubiquitous asynchronous DNS implementation.]
  12. **/
  13. /* See http://elvin.dstc.com/ListArchive/elvin-dev/archive/2001/09/msg00027.html
  14. * for some approaches to asynchronous dns. We will want to switch once one of
  15. * them becomes more commonly available.
  16. */
  17. #define NEW_LOG_INTERFACE
  18. #include "or.h"
  19. #include "tree.h"
  20. /** Longest hostname we're willing to resolve. */
  21. #define MAX_ADDRESSLEN 256
  22. /** Maximum DNS processes to spawn. */
  23. #define MAX_DNSWORKERS 100
  24. /** Minimum DNS processes to spawn. */
  25. #define MIN_DNSWORKERS 3
  26. /** If more than this many processes are idle, shut down the extras. */
  27. #define MAX_IDLE_DNSWORKERS 10
  28. /** Possible outcomes from hostname lookup: permanent failure,
  29. * transient (retryable) failure, and success. */
  30. #define DNS_RESOLVE_FAILED_TRANSIENT 1
  31. #define DNS_RESOLVE_FAILED_PERMANENT 2
  32. #define DNS_RESOLVE_SUCCEEDED 3
  33. /** How many dnsworkers we have running right now. */
  34. static int num_dnsworkers=0;
  35. /** How many of the running dnsworkers have an assigned task right now. */
  36. static int num_dnsworkers_busy=0;
  37. /** When did we last rotate the dnsworkers? */
  38. static time_t last_rotation_time=0;
  39. /** Linked list of connections waiting for a DNS answer. */
  40. typedef struct pending_connection_t {
  41. connection_t *conn;
  42. struct pending_connection_t *next;
  43. } pending_connection_t;
  44. /** A DNS request: possibly completed, possibly pending; cached_resolve
  45. * structs are stored at the OR side in a splay tree, and as a linked
  46. * list from oldest to newest.
  47. */
  48. typedef struct cached_resolve_t {
  49. SPLAY_ENTRY(cached_resolve_t) node;
  50. char address[MAX_ADDRESSLEN]; /**< The hostname to be resolved. */
  51. uint32_t addr; /**< IPv4 addr for <b>address</b>. */
  52. char state; /**< 0 is pending; 1 means answer is valid; 2 means resolve failed. */
  53. #define CACHE_STATE_PENDING 0
  54. #define CACHE_STATE_VALID 1
  55. #define CACHE_STATE_FAILED 2
  56. uint32_t expire; /**< Remove items from cache after this time. */
  57. pending_connection_t *pending_connections;
  58. struct cached_resolve_t *next;
  59. } cached_resolve_t;
  60. static void purge_expired_resolves(uint32_t now);
  61. static int assign_to_dnsworker(connection_t *exitconn);
  62. static void dns_purge_resolve(cached_resolve_t *resolve);
  63. static void dns_found_answer(char *address, uint32_t addr, char outcome);
  64. static int dnsworker_main(void *data);
  65. static int spawn_dnsworker(void);
  66. static int spawn_enough_dnsworkers(void);
  67. static void send_resolved_cell(connection_t *conn, uint8_t answer_type);
  68. /** Splay tree of cached_resolve objects. */
  69. static SPLAY_HEAD(cache_tree, cached_resolve_t) cache_root;
  70. /** Function to compare hashed resolves on their addresses; used to
  71. * implement splay trees. */
  72. static int
  73. compare_cached_resolves(cached_resolve_t *a,
  74. cached_resolve_t *b)
  75. {
  76. /* make this smarter one day? */
  77. return strncmp(a->address, b->address, MAX_ADDRESSLEN);
  78. }
  79. SPLAY_PROTOTYPE(cache_tree, cached_resolve_t, node, compare_cached_resolves);
  80. SPLAY_GENERATE(cache_tree, cached_resolve_t, node, compare_cached_resolves);
  81. /** Initialize the DNS cache. */
  82. static void
  83. init_cache_tree(void)
  84. {
  85. SPLAY_INIT(&cache_root);
  86. }
  87. /** Initialize the DNS subsystem; called by the OR process. */
  88. void
  89. dns_init(void)
  90. {
  91. init_cache_tree();
  92. dnsworkers_rotate();
  93. }
  94. /** Helper: free storage held by an entry in the DNS cache. */
  95. static void
  96. _free_cached_resolve(cached_resolve_t *r)
  97. {
  98. while (r->pending_connections) {
  99. pending_connection_t *victim = r->pending_connections;
  100. r->pending_connections = victim->next;
  101. tor_free(victim);
  102. }
  103. tor_free(r);
  104. }
  105. /** Free all storage held in the DNS cache */
  106. void
  107. dns_free_all(void)
  108. {
  109. cached_resolve_t *ptr, *next;
  110. for (ptr = SPLAY_MIN(cache_tree, &cache_root); ptr != NULL; ptr = next) {
  111. next = SPLAY_NEXT(cache_tree, &cache_root, ptr);
  112. SPLAY_REMOVE(cache_tree, &cache_root, ptr);
  113. _free_cached_resolve(ptr);
  114. }
  115. }
  116. /** Linked list of resolved addresses, oldest to newest. */
  117. static cached_resolve_t *oldest_cached_resolve = NULL;
  118. static cached_resolve_t *newest_cached_resolve = NULL;
  119. /** Remove every cached_resolve whose <b>expire</b> time is before <b>now</b>
  120. * from the cache. */
  121. static void
  122. purge_expired_resolves(uint32_t now)
  123. {
  124. cached_resolve_t *resolve;
  125. pending_connection_t *pend;
  126. connection_t *pendconn;
  127. /* this is fast because the linked list
  128. * oldest_cached_resolve is ordered by when they came in.
  129. */
  130. while (oldest_cached_resolve && (oldest_cached_resolve->expire < now)) {
  131. resolve = oldest_cached_resolve;
  132. debug(LD_EXIT,"Forgetting old cached resolve (address %s, expires %lu)",
  133. safe_str(resolve->address), (unsigned long)resolve->expire);
  134. if (resolve->state == CACHE_STATE_PENDING) {
  135. debug(LD_EXIT,"Bug: Expiring a dns resolve ('%s') that's still pending. Forgot to cull it?", safe_str(resolve->address));
  136. tor_fragile_assert();
  137. }
  138. if (resolve->pending_connections) {
  139. debug(LD_EXIT, "Closing pending connections on expiring DNS resolve!");
  140. tor_fragile_assert();
  141. while (resolve->pending_connections) {
  142. pend = resolve->pending_connections;
  143. resolve->pending_connections = pend->next;
  144. /* Connections should only be pending if they have no socket. */
  145. tor_assert(pend->conn->s == -1);
  146. pendconn = pend->conn;
  147. connection_edge_end(pendconn, END_STREAM_REASON_TIMEOUT,
  148. pendconn->cpath_layer);
  149. circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
  150. connection_free(pendconn);
  151. tor_free(pend);
  152. }
  153. }
  154. oldest_cached_resolve = resolve->next;
  155. if (!oldest_cached_resolve) /* if there are no more, */
  156. newest_cached_resolve = NULL; /* then make sure the list's tail knows that too */
  157. SPLAY_REMOVE(cache_tree, &cache_root, resolve);
  158. tor_free(resolve);
  159. }
  160. }
  161. /** Send a response to the RESOVLE request of a connection. answer_type must
  162. * be one of RESOLVED_TYPE_(IPV4|ERROR|ERROR_TRANSIENT) */
  163. static void
  164. send_resolved_cell(connection_t *conn, uint8_t answer_type)
  165. {
  166. char buf[RELAY_PAYLOAD_SIZE];
  167. size_t buflen;
  168. buf[0] = answer_type;
  169. switch (answer_type)
  170. {
  171. case RESOLVED_TYPE_IPV4:
  172. buf[1] = 4;
  173. set_uint32(buf+2, htonl(conn->addr));
  174. set_uint32(buf+6, htonl(MAX_DNS_ENTRY_AGE)); /*XXXX send a real TTL*/
  175. buflen = 10;
  176. break;
  177. case RESOLVED_TYPE_ERROR_TRANSIENT:
  178. case RESOLVED_TYPE_ERROR:
  179. {
  180. const char *errmsg = "Error resolving hostname";
  181. int msglen = strlen(errmsg);
  182. int ttl = (answer_type == RESOLVED_TYPE_ERROR ? MAX_DNS_ENTRY_AGE : 0);
  183. buf[1] = msglen;
  184. strlcpy(buf+2, errmsg, sizeof(buf)-2);
  185. set_uint32(buf+2+msglen, htonl((uint32_t)ttl));
  186. buflen = 6+msglen;
  187. break;
  188. }
  189. default:
  190. tor_assert(0);
  191. }
  192. connection_edge_send_command(conn, circuit_get_by_edge_conn(conn),
  193. RELAY_COMMAND_RESOLVED, buf, buflen,
  194. conn->cpath_layer);
  195. }
  196. /** Link <b>r</b> into the tree of address-to-result mappings, and add it to
  197. * the linked list of resolves-by-age. */
  198. static void
  199. insert_resolve(cached_resolve_t *r)
  200. {
  201. /* add us to the linked list of resolves */
  202. if (!oldest_cached_resolve) {
  203. oldest_cached_resolve = r;
  204. } else {
  205. newest_cached_resolve->next = r;
  206. }
  207. newest_cached_resolve = r;
  208. SPLAY_INSERT(cache_tree, &cache_root, r);
  209. }
  210. /** See if we have a cache entry for <b>exitconn</b>-\>address. if so,
  211. * if resolve valid, put it into <b>exitconn</b>-\>addr and return 1.
  212. * If resolve failed, unlink exitconn if needed, free it, and return -1.
  213. *
  214. * Else, if seen before and pending, add conn to the pending list,
  215. * and return 0.
  216. *
  217. * Else, if not seen before, add conn to pending list, hand to
  218. * dns farm, and return 0.
  219. */
  220. int
  221. dns_resolve(connection_t *exitconn)
  222. {
  223. cached_resolve_t *resolve;
  224. cached_resolve_t search;
  225. pending_connection_t *pending_connection;
  226. struct in_addr in;
  227. circuit_t *circ;
  228. uint32_t now = time(NULL);
  229. assert_connection_ok(exitconn, 0);
  230. tor_assert(exitconn->s == -1);
  231. /* first check if exitconn->address is an IP. If so, we already
  232. * know the answer. */
  233. if (tor_inet_aton(exitconn->address, &in) != 0) {
  234. exitconn->addr = ntohl(in.s_addr);
  235. if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
  236. send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
  237. return 1;
  238. }
  239. /* then take this opportunity to see if there are any expired
  240. * resolves in the tree. */
  241. purge_expired_resolves(now);
  242. /* lower-case exitconn->address, so it's in canonical form */
  243. tor_strlower(exitconn->address);
  244. /* now check the tree to see if 'address' is already there. */
  245. strlcpy(search.address, exitconn->address, sizeof(search.address));
  246. resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
  247. if (resolve) { /* already there */
  248. switch (resolve->state) {
  249. case CACHE_STATE_PENDING:
  250. /* add us to the pending list */
  251. pending_connection = tor_malloc_zero(
  252. sizeof(pending_connection_t));
  253. pending_connection->conn = exitconn;
  254. pending_connection->next = resolve->pending_connections;
  255. resolve->pending_connections = pending_connection;
  256. debug(LD_EXIT,"Connection (fd %d) waiting for pending DNS resolve of '%s'",
  257. exitconn->s, safe_str(exitconn->address));
  258. exitconn->state = EXIT_CONN_STATE_RESOLVING;
  259. return 0;
  260. case CACHE_STATE_VALID:
  261. exitconn->addr = resolve->addr;
  262. debug(LD_EXIT,"Connection (fd %d) found cached answer for '%s'",
  263. exitconn->s, safe_str(exitconn->address));
  264. if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
  265. send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
  266. return 1;
  267. case CACHE_STATE_FAILED:
  268. debug(LD_EXIT,"Connection (fd %d) found cached error for '%s'",
  269. exitconn->s, safe_str(exitconn->address));
  270. if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
  271. send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
  272. circ = circuit_get_by_edge_conn(exitconn);
  273. if (circ)
  274. circuit_detach_stream(circ, exitconn);
  275. if (!exitconn->marked_for_close)
  276. connection_free(exitconn);
  277. return -1;
  278. }
  279. tor_assert(0);
  280. }
  281. /* not there, need to add it */
  282. resolve = tor_malloc_zero(sizeof(cached_resolve_t));
  283. resolve->state = CACHE_STATE_PENDING;
  284. resolve->expire = now + MAX_DNS_ENTRY_AGE;
  285. strlcpy(resolve->address, exitconn->address, sizeof(resolve->address));
  286. /* add us to the pending list */
  287. pending_connection = tor_malloc_zero(sizeof(pending_connection_t));
  288. pending_connection->conn = exitconn;
  289. resolve->pending_connections = pending_connection;
  290. exitconn->state = EXIT_CONN_STATE_RESOLVING;
  291. insert_resolve(resolve);
  292. return assign_to_dnsworker(exitconn);
  293. }
  294. /** Find or spawn a dns worker process to handle resolving
  295. * <b>exitconn</b>-\>address; tell that dns worker to begin resolving.
  296. */
  297. static int
  298. assign_to_dnsworker(connection_t *exitconn)
  299. {
  300. connection_t *dnsconn;
  301. unsigned char len;
  302. tor_assert(exitconn->state == EXIT_CONN_STATE_RESOLVING);
  303. tor_assert(exitconn->s == -1);
  304. /* respawn here, to be sure there are enough */
  305. if (spawn_enough_dnsworkers() < 0) {
  306. goto err;
  307. }
  308. dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE);
  309. if (!dnsconn) {
  310. warn(LD_EXIT,"no idle dns workers. Failing.");
  311. if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
  312. send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
  313. goto err;
  314. }
  315. debug(LD_EXIT,
  316. "Connection (fd %d) needs to resolve '%s'; assigning to DNSWorker (fd %d)",
  317. exitconn->s, safe_str(exitconn->address), dnsconn->s);
  318. tor_free(dnsconn->address);
  319. dnsconn->address = tor_strdup(exitconn->address);
  320. dnsconn->state = DNSWORKER_STATE_BUSY;
  321. num_dnsworkers_busy++;
  322. len = strlen(dnsconn->address);
  323. connection_write_to_buf((char*)&len, 1, dnsconn);
  324. connection_write_to_buf(dnsconn->address, len, dnsconn);
  325. return 0;
  326. err:
  327. dns_cancel_pending_resolve(exitconn->address); /* also sends end and frees! */
  328. return -1;
  329. }
  330. /** Remove <b>conn</b> from the list of connections waiting for conn-\>address.
  331. */
  332. void
  333. connection_dns_remove(connection_t *conn)
  334. {
  335. pending_connection_t *pend, *victim;
  336. cached_resolve_t search;
  337. cached_resolve_t *resolve;
  338. tor_assert(conn->type == CONN_TYPE_EXIT);
  339. tor_assert(conn->state == EXIT_CONN_STATE_RESOLVING);
  340. strlcpy(search.address, conn->address, sizeof(search.address));
  341. resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
  342. if (!resolve) {
  343. /* XXXX RD This *is* a bug, right? -NM */
  344. notice(LD_BUG,"Address '%s' is not pending. Dropping.", safe_str(conn->address));
  345. return;
  346. }
  347. tor_assert(resolve->pending_connections);
  348. assert_connection_ok(conn,0);
  349. pend = resolve->pending_connections;
  350. if (pend->conn == conn) {
  351. resolve->pending_connections = pend->next;
  352. tor_free(pend);
  353. debug(LD_EXIT, "First connection (fd %d) no longer waiting for resolve of '%s'",
  354. conn->s, safe_str(conn->address));
  355. return;
  356. } else {
  357. for ( ; pend->next; pend = pend->next) {
  358. if (pend->next->conn == conn) {
  359. victim = pend->next;
  360. pend->next = victim->next;
  361. tor_free(victim);
  362. debug(LD_EXIT, "Connection (fd %d) no longer waiting for resolve of '%s'",
  363. conn->s, safe_str(conn->address));
  364. return; /* more are pending */
  365. }
  366. }
  367. tor_assert(0); /* not reachable unless onlyconn not in pending list */
  368. }
  369. }
  370. /** Log an error and abort if conn is waiting for a DNS resolve.
  371. */
  372. void
  373. assert_connection_edge_not_dns_pending(connection_t *conn)
  374. {
  375. pending_connection_t *pend;
  376. cached_resolve_t *resolve;
  377. SPLAY_FOREACH(resolve, cache_tree, &cache_root) {
  378. for (pend = resolve->pending_connections;
  379. pend;
  380. pend = pend->next) {
  381. tor_assert(pend->conn != conn);
  382. }
  383. }
  384. }
  385. /** Log an error and abort if any connection waiting for a DNS resolve is
  386. * corrupted. */
  387. void
  388. assert_all_pending_dns_resolves_ok(void)
  389. {
  390. pending_connection_t *pend;
  391. cached_resolve_t *resolve;
  392. SPLAY_FOREACH(resolve, cache_tree, &cache_root) {
  393. for (pend = resolve->pending_connections;
  394. pend;
  395. pend = pend->next) {
  396. assert_connection_ok(pend->conn, 0);
  397. tor_assert(pend->conn->s == -1);
  398. tor_assert(!connection_in_array(pend->conn));
  399. }
  400. }
  401. }
  402. /** Mark all connections waiting for <b>address</b> for close. Then cancel
  403. * the resolve for <b>address</b> itself, and remove any cached results for
  404. * <b>address</b> from the cache.
  405. */
  406. void
  407. dns_cancel_pending_resolve(char *address)
  408. {
  409. pending_connection_t *pend;
  410. cached_resolve_t search;
  411. cached_resolve_t *resolve;
  412. connection_t *pendconn;
  413. circuit_t *circ;
  414. strlcpy(search.address, address, sizeof(search.address));
  415. resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
  416. if (!resolve) {
  417. /* XXXX RD This *is* a bug, right? -NM */
  418. notice(LD_BUG,"Address '%s' is not pending. Dropping.", safe_str(address));
  419. return;
  420. }
  421. if (!resolve->pending_connections) {
  422. /* XXX this should never trigger, but sometimes it does */
  423. warn(LD_BUG,"Bug: Address '%s' is pending but has no pending connections!",
  424. safe_str(address));
  425. tor_fragile_assert();
  426. return;
  427. }
  428. tor_assert(resolve->pending_connections);
  429. /* mark all pending connections to fail */
  430. debug(LD_EXIT, "Failing all connections waiting on DNS resolve of '%s'",
  431. safe_str(address));
  432. while (resolve->pending_connections) {
  433. pend = resolve->pending_connections;
  434. pend->conn->state = EXIT_CONN_STATE_RESOLVEFAILED;
  435. pendconn = pend->conn;
  436. tor_assert(pendconn->s == -1);
  437. if (!pendconn->marked_for_close) {
  438. connection_edge_end(pendconn, END_STREAM_REASON_RESOURCELIMIT,
  439. pendconn->cpath_layer);
  440. }
  441. circ = circuit_get_by_edge_conn(pendconn);
  442. if (circ)
  443. circuit_detach_stream(circ, pendconn);
  444. connection_free(pendconn);
  445. resolve->pending_connections = pend->next;
  446. tor_free(pend);
  447. }
  448. dns_purge_resolve(resolve);
  449. }
  450. /** Remove <b>resolve</b> from the cache.
  451. */
  452. static void
  453. dns_purge_resolve(cached_resolve_t *resolve)
  454. {
  455. cached_resolve_t *tmp;
  456. /* remove resolve from the linked list */
  457. if (resolve == oldest_cached_resolve) {
  458. oldest_cached_resolve = resolve->next;
  459. if (oldest_cached_resolve == NULL)
  460. newest_cached_resolve = NULL;
  461. } else {
  462. /* FFFF make it a doubly linked list if this becomes too slow */
  463. for (tmp=oldest_cached_resolve; tmp && tmp->next != resolve; tmp=tmp->next) ;
  464. tor_assert(tmp); /* it's got to be in the list, or we screwed up somewhere else */
  465. tmp->next = resolve->next; /* unlink it */
  466. if (newest_cached_resolve == resolve)
  467. newest_cached_resolve = tmp;
  468. }
  469. /* remove resolve from the tree */
  470. SPLAY_REMOVE(cache_tree, &cache_root, resolve);
  471. tor_free(resolve);
  472. }
  473. /** Called on the OR side when a DNS worker tells us the outcome of a DNS
  474. * resolve: tell all pending connections about the result of the lookup, and
  475. * cache the value. (<b>address</b> is a NUL-terminated string containing the
  476. * address to look up; <b>addr</b> is an IPv4 address in host order;
  477. * <b>outcome</b> is one of
  478. * DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
  479. */
  480. static void
  481. dns_found_answer(char *address, uint32_t addr, char outcome)
  482. {
  483. pending_connection_t *pend;
  484. cached_resolve_t search;
  485. cached_resolve_t *resolve;
  486. connection_t *pendconn;
  487. circuit_t *circ;
  488. strlcpy(search.address, address, sizeof(search.address));
  489. resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
  490. if (!resolve) {
  491. info(LD_EXIT,"Resolved unasked address '%s'; caching anyway.",
  492. safe_str(address));
  493. resolve = tor_malloc_zero(sizeof(cached_resolve_t));
  494. resolve->state = (outcome == DNS_RESOLVE_SUCCEEDED) ?
  495. CACHE_STATE_VALID : CACHE_STATE_FAILED;
  496. resolve->addr = addr;
  497. resolve->expire = time(NULL) + MAX_DNS_ENTRY_AGE;
  498. insert_resolve(resolve);
  499. return;
  500. }
  501. if (resolve->state != CACHE_STATE_PENDING) {
  502. /* XXXX Maybe update addr? or check addr for consistency? Or let
  503. * VALID replace FAILED? */
  504. notice(LD_EXIT, "Resolved '%s' which was already resolved; ignoring",
  505. safe_str(address));
  506. tor_assert(resolve->pending_connections == NULL);
  507. return;
  508. }
  509. /* Removed this assertion: in fact, we'll sometimes get a double answer
  510. * to the same question. This can happen when we ask one worker to resolve
  511. * X.Y.Z., then we cancel the request, and then we ask another worker to
  512. * resolve X.Y.Z. */
  513. /* tor_assert(resolve->state == CACHE_STATE_PENDING); */
  514. resolve->addr = addr;
  515. if (outcome == DNS_RESOLVE_SUCCEEDED)
  516. resolve->state = CACHE_STATE_VALID;
  517. else
  518. resolve->state = CACHE_STATE_FAILED;
  519. while (resolve->pending_connections) {
  520. pend = resolve->pending_connections;
  521. assert_connection_ok(pend->conn,time(NULL));
  522. pend->conn->addr = resolve->addr;
  523. pendconn = pend->conn; /* don't pass complex things to the
  524. connection_mark_for_close macro */
  525. if (resolve->state == CACHE_STATE_FAILED) {
  526. /* prevent double-remove. */
  527. pendconn->state = EXIT_CONN_STATE_RESOLVEFAILED;
  528. if (pendconn->purpose == EXIT_PURPOSE_CONNECT) {
  529. connection_edge_end(pendconn, END_STREAM_REASON_RESOLVEFAILED, pendconn->cpath_layer);
  530. /* This detach must happen after we send the end cell. */
  531. circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
  532. } else {
  533. send_resolved_cell(pendconn, RESOLVED_TYPE_ERROR);
  534. /* This detach must happen after we send the resolved cell. */
  535. circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
  536. }
  537. connection_free(pendconn);
  538. } else {
  539. if (pendconn->purpose == EXIT_PURPOSE_CONNECT) {
  540. /* prevent double-remove. */
  541. pend->conn->state = EXIT_CONN_STATE_CONNECTING;
  542. circ = circuit_get_by_edge_conn(pend->conn);
  543. tor_assert(circ);
  544. /* unlink pend->conn from resolving_streams, */
  545. circuit_detach_stream(circ, pend->conn);
  546. /* and link it to n_streams */
  547. pend->conn->next_stream = circ->n_streams;
  548. pend->conn->on_circuit = circ;
  549. circ->n_streams = pend->conn;
  550. connection_exit_connect(pend->conn);
  551. } else {
  552. /* prevent double-remove. This isn't really an accurate state,
  553. * but it does the right thing. */
  554. pendconn->state = EXIT_CONN_STATE_RESOLVEFAILED;
  555. send_resolved_cell(pendconn, RESOLVED_TYPE_IPV4);
  556. circ = circuit_get_by_edge_conn(pendconn);
  557. tor_assert(circ);
  558. circuit_detach_stream(circ, pendconn);
  559. connection_free(pendconn);
  560. }
  561. }
  562. resolve->pending_connections = pend->next;
  563. tor_free(pend);
  564. }
  565. if (outcome == DNS_RESOLVE_FAILED_TRANSIENT) { /* remove from cache */
  566. dns_purge_resolve(resolve);
  567. }
  568. }
  569. /******************************************************************/
  570. /*
  571. * Connection between OR and dnsworker
  572. */
  573. /** Write handler: called when we've pushed a request to a dnsworker. */
  574. int
  575. connection_dns_finished_flushing(connection_t *conn)
  576. {
  577. tor_assert(conn);
  578. tor_assert(conn->type == CONN_TYPE_DNSWORKER);
  579. connection_stop_writing(conn);
  580. return 0;
  581. }
  582. int
  583. connection_dns_reached_eof(connection_t *conn)
  584. {
  585. warn(LD_EXIT,"Read eof. Worker died unexpectedly.");
  586. if (conn->state == DNSWORKER_STATE_BUSY) {
  587. /* don't cancel the resolve here -- it would be cancelled in
  588. * connection_about_to_close_connection(), since conn is still
  589. * in state BUSY
  590. */
  591. num_dnsworkers_busy--;
  592. }
  593. num_dnsworkers--;
  594. connection_mark_for_close(conn);
  595. return 0;
  596. }
  597. /** Read handler: called when we get data from a dnsworker. See
  598. * if we have a complete answer. If so, call dns_found_answer on the
  599. * result. If not, wait. Returns 0. */
  600. int
  601. connection_dns_process_inbuf(connection_t *conn)
  602. {
  603. char success;
  604. uint32_t addr;
  605. tor_assert(conn);
  606. tor_assert(conn->type == CONN_TYPE_DNSWORKER);
  607. if (conn->state != DNSWORKER_STATE_BUSY && buf_datalen(conn->inbuf)) {
  608. warn(LD_BUG,"Bug: read data (%d bytes) from an idle dns worker (fd %d, address '%s'). Please report.",
  609. (int)buf_datalen(conn->inbuf), conn->s, safe_str(conn->address));
  610. tor_fragile_assert();
  611. /* Pull it off the buffer anyway, or it will just stay there.
  612. * Keep pulling things off because sometimes we get several
  613. * answers at once (!). */
  614. while (buf_datalen(conn->inbuf)) {
  615. connection_fetch_from_buf(&success,1,conn);
  616. connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
  617. warn(LD_EXIT,"Discarding idle dns answer (success %d, addr %d.)",
  618. success, addr); // XXX safe_str
  619. }
  620. return 0;
  621. }
  622. if (buf_datalen(conn->inbuf) < 5) /* entire answer available? */
  623. return 0; /* not yet */
  624. tor_assert(conn->state == DNSWORKER_STATE_BUSY);
  625. tor_assert(buf_datalen(conn->inbuf) == 5);
  626. connection_fetch_from_buf(&success,1,conn);
  627. connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
  628. debug(LD_EXIT, "DNSWorker (fd %d) returned answer for '%s'",
  629. conn->s, safe_str(conn->address));
  630. tor_assert(success >= DNS_RESOLVE_FAILED_TRANSIENT);
  631. tor_assert(success <= DNS_RESOLVE_SUCCEEDED);
  632. dns_found_answer(conn->address, ntohl(addr), success);
  633. tor_free(conn->address);
  634. conn->address = tor_strdup("<idle>");
  635. conn->state = DNSWORKER_STATE_IDLE;
  636. num_dnsworkers_busy--;
  637. if (conn->timestamp_created < last_rotation_time) {
  638. connection_mark_for_close(conn);
  639. num_dnsworkers--;
  640. spawn_enough_dnsworkers();
  641. }
  642. return 0;
  643. }
  644. /** Close and re-open all idle dnsworkers; schedule busy ones to be closed
  645. * and re-opened once they're no longer busy.
  646. **/
  647. void
  648. dnsworkers_rotate(void)
  649. {
  650. connection_t *dnsconn;
  651. while ((dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
  652. DNSWORKER_STATE_IDLE))) {
  653. connection_mark_for_close(dnsconn);
  654. num_dnsworkers--;
  655. }
  656. last_rotation_time = time(NULL);
  657. if (server_mode(get_options()))
  658. spawn_enough_dnsworkers();
  659. }
  660. /** Implementation for DNS workers; this code runs in a separate
  661. * execution context. It takes as its argument an fdarray as returned
  662. * by socketpair(), and communicates via fdarray[1]. The protocol is
  663. * as follows:
  664. * - The OR says:
  665. * - ADDRESSLEN [1 byte]
  666. * - ADDRESS [ADDRESSLEN bytes]
  667. * - The DNS worker does the lookup, and replies:
  668. * - OUTCOME [1 byte]
  669. * - IP [4 bytes]
  670. *
  671. * OUTCOME is one of DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
  672. * IP is in host order.
  673. *
  674. * The dnsworker runs indefinitely, until its connection is closed or an error
  675. * occurs.
  676. */
  677. static int
  678. dnsworker_main(void *data)
  679. {
  680. char address[MAX_ADDRESSLEN];
  681. unsigned char address_len;
  682. char answer[5];
  683. uint32_t ip;
  684. int *fdarray = data;
  685. int fd;
  686. int result;
  687. /* log_fn(LOG_NOTICE,"After spawn: fdarray @%d has %d:%d", (int)fdarray, fdarray[0],fdarray[1]); */
  688. fd = fdarray[1]; /* this side is ours */
  689. #ifndef TOR_IS_MULTITHREADED
  690. tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
  691. tor_free_all(1); /* so the child doesn't hold the parent's fd's open */
  692. handle_signals(0); /* ignore interrupts from the keyboard, etc */
  693. #endif
  694. tor_free(data);
  695. for (;;) {
  696. int r;
  697. if ((r = recv(fd, &address_len, 1, 0)) != 1) {
  698. if (r == 0) {
  699. info(LD_EXIT,"DNS worker exiting because Tor process closed connection (either pruned idle dnsworker or died).");
  700. } else {
  701. info(LD_EXIT,"DNS worker exiting because of error on connection to Tor process.");
  702. info(LD_EXIT,"(Error on %d was %s)", fd, tor_socket_strerror(tor_socket_errno(fd)));
  703. }
  704. tor_close_socket(fd);
  705. spawn_exit();
  706. }
  707. if (address_len && read_all(fd, address, address_len, 1) != address_len) {
  708. err(LD_BUG,"read hostname failed. Child exiting.");
  709. tor_close_socket(fd);
  710. spawn_exit();
  711. }
  712. address[address_len] = 0; /* null terminate it */
  713. result = tor_lookup_hostname(address, &ip);
  714. /* Make 0.0.0.0 an error, so that we can use "0" to mean "no addr") */
  715. if (!ip)
  716. result = -1;
  717. switch (result) {
  718. case 1:
  719. /* XXX result can never be 1, because we set it to -1 above on error */
  720. info(LD_NET,"Could not resolve dest addr %s (transient).",safe_str(address));
  721. answer[0] = DNS_RESOLVE_FAILED_TRANSIENT;
  722. break;
  723. case -1:
  724. info(LD_NET,"Could not resolve dest addr %s (permanent).",safe_str(address));
  725. answer[0] = DNS_RESOLVE_FAILED_PERMANENT;
  726. break;
  727. case 0:
  728. info(LD_NET,"Resolved address '%s'.",safe_str(address));
  729. answer[0] = DNS_RESOLVE_SUCCEEDED;
  730. break;
  731. }
  732. set_uint32(answer+1, ip);
  733. if (write_all(fd, answer, 5, 1) != 5) {
  734. err(LD_NET,"writing answer failed. Child exiting.");
  735. tor_close_socket(fd);
  736. spawn_exit();
  737. }
  738. }
  739. return 0; /* windows wants this function to return an int */
  740. }
  741. /** Launch a new DNS worker; return 0 on success, -1 on failure.
  742. */
  743. static int
  744. spawn_dnsworker(void)
  745. {
  746. int *fdarray;
  747. int fd;
  748. connection_t *conn;
  749. int err;
  750. fdarray = tor_malloc(sizeof(int)*2);
  751. if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
  752. warn(LD_NET, "Couldn't construct socketpair: %s", tor_socket_strerror(-err));
  753. tor_free(fdarray);
  754. return -1;
  755. }
  756. /* log_fn(LOG_NOTICE,"Before spawn: fdarray @%d has %d:%d", (int)fdarray, fdarray[0],fdarray[1]); */
  757. fd = fdarray[0]; /* We copy this out here, since dnsworker_main may free fdarray */
  758. spawn_func(dnsworker_main, (void*)fdarray);
  759. debug(LD_EXIT,"just spawned a worker.");
  760. #ifndef TOR_IS_MULTITHREADED
  761. tor_close_socket(fdarray[1]); /* we don't need the worker's side of the pipe */
  762. tor_free(fdarray);
  763. #endif
  764. conn = connection_new(CONN_TYPE_DNSWORKER);
  765. set_socket_nonblocking(fd);
  766. /* set up conn so it's got all the data we need to remember */
  767. conn->s = fd;
  768. conn->address = tor_strdup("<unused>");
  769. if (connection_add(conn) < 0) { /* no space, forget it */
  770. warn(LD_NET,"connection_add failed. Giving up.");
  771. connection_free(conn); /* this closes fd */
  772. return -1;
  773. }
  774. conn->state = DNSWORKER_STATE_IDLE;
  775. connection_start_reading(conn);
  776. return 0; /* success */
  777. }
  778. /** If we have too many or too few DNS workers, spawn or kill some.
  779. * Return 0 if we are happy, return -1 if we tried to spawn more but
  780. * we couldn't.
  781. */
  782. static int
  783. spawn_enough_dnsworkers(void)
  784. {
  785. int num_dnsworkers_needed; /* aim to have 1 more than needed,
  786. * but no less than min and no more than max */
  787. connection_t *dnsconn;
  788. /* XXX This may not be the best strategy. Maybe we should queue pending
  789. * requests until the old ones finish or time out: otherwise, if
  790. * the connection requests come fast enough, we never get any DNS done. -NM
  791. * XXX But if we queue them, then the adversary can pile even more
  792. * queries onto us, blocking legitimate requests for even longer.
  793. * Maybe we should compromise and only kill if it's been at it for
  794. * more than, e.g., 2 seconds. -RD
  795. */
  796. if (num_dnsworkers_busy == MAX_DNSWORKERS) {
  797. /* We always want at least one worker idle.
  798. * So find the oldest busy worker and kill it.
  799. */
  800. dnsconn = connection_get_by_type_state_lastwritten(CONN_TYPE_DNSWORKER,
  801. DNSWORKER_STATE_BUSY);
  802. tor_assert(dnsconn);
  803. warn(LD_EXIT, "%d DNS workers are spawned; all are busy. Killing one.",
  804. MAX_DNSWORKERS);
  805. connection_mark_for_close(dnsconn);
  806. num_dnsworkers_busy--;
  807. num_dnsworkers--;
  808. }
  809. if (num_dnsworkers_busy >= MIN_DNSWORKERS)
  810. num_dnsworkers_needed = num_dnsworkers_busy+1;
  811. else
  812. num_dnsworkers_needed = MIN_DNSWORKERS;
  813. while (num_dnsworkers < num_dnsworkers_needed) {
  814. if (spawn_dnsworker() < 0) {
  815. warn(LD_EXIT,"Spawn failed. Will try again later.");
  816. return -1;
  817. }
  818. num_dnsworkers++;
  819. }
  820. while (num_dnsworkers > num_dnsworkers_busy+MAX_IDLE_DNSWORKERS) {
  821. /* too many idle? */
  822. /* cull excess workers */
  823. info(LD_EXIT,"%d of %d dnsworkers are idle. Killing one.",
  824. num_dnsworkers-num_dnsworkers_busy, num_dnsworkers);
  825. dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER, DNSWORKER_STATE_IDLE);
  826. tor_assert(dnsconn);
  827. connection_mark_for_close(dnsconn);
  828. num_dnsworkers--;
  829. }
  830. return 0;
  831. }