dns.c 30 KB

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