12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430 |
- const char dns_c_id[] =
- "$Id$";
- #include "or.h"
- #include "../common/ht.h"
- #ifdef USE_EVENTDNS
- #include "eventdns.h"
- #endif
- #define MAX_ADDRESSLEN 256
- #define MAX_DNSWORKERS 100
- #define MIN_DNSWORKERS 3
- #define MAX_IDLE_DNSWORKERS 10
- #define RESOLVE_MAX_TIMEOUT 300
- #define DNS_RESOLVE_FAILED_TRANSIENT 1
- #define DNS_RESOLVE_FAILED_PERMANENT 2
- #define DNS_RESOLVE_SUCCEEDED 3
- #ifndef USE_EVENTDNS
- static int num_dnsworkers=0;
- static int num_dnsworkers_busy=0;
- static time_t last_rotation_time=0;
- #else
- static int nameservers_configured = 0;
- static char *resolv_conf_fname = NULL;
- static time_t resolv_conf_mtime = 0;
- #endif
- typedef struct pending_connection_t {
- edge_connection_t *conn;
- struct pending_connection_t *next;
- } pending_connection_t;
- #define CACHED_RESOLVE_MAGIC 0x1234F00D
- #define CACHE_STATE_PENDING 0
- #define CACHE_STATE_DONE 1
- #define CACHE_STATE_CACHED_VALID 2
- #define CACHE_STATE_CACHED_FAILED 3
- typedef struct cached_resolve_t {
- HT_ENTRY(cached_resolve_t) node;
- uint32_t magic;
- char address[MAX_ADDRESSLEN];
- uint32_t addr;
- uint8_t state;
- time_t expire;
- uint32_t ttl;
-
- pending_connection_t *pending_connections;
- } cached_resolve_t;
- static void purge_expired_resolves(time_t now);
- static void dns_found_answer(const char *address, uint32_t addr, char outcome,
- uint32_t ttl);
- static void send_resolved_cell(edge_connection_t *conn, uint8_t answer_type);
- static int launch_resolve(edge_connection_t *exitconn);
- #ifndef USE_EVENTDNS
- static void dnsworkers_rotate(void);
- static void dnsworker_main(void *data);
- static int spawn_dnsworker(void);
- static int spawn_enough_dnsworkers(void);
- #else
- static int configure_nameservers(int force);
- #endif
- #ifdef DEBUG_DNS_CACHE
- static void _assert_cache_ok(void);
- #define assert_cache_ok() _assert_cache_ok()
- #else
- #define assert_cache_ok() do {} while (0)
- #endif
- static void assert_resolve_ok(cached_resolve_t *resolve);
- static HT_HEAD(cache_map, cached_resolve_t) cache_root;
- static INLINE int
- cached_resolves_eq(cached_resolve_t *a, cached_resolve_t *b)
- {
-
- assert_resolve_ok(a);
- return !strncmp(a->address, b->address, MAX_ADDRESSLEN);
- }
- static INLINE unsigned int
- cached_resolve_hash(cached_resolve_t *a)
- {
- return ht_string_hash(a->address);
- }
- HT_PROTOTYPE(cache_map, cached_resolve_t, node, cached_resolve_hash,
- cached_resolves_eq);
- HT_GENERATE(cache_map, cached_resolve_t, node, cached_resolve_hash,
- cached_resolves_eq, 0.6, malloc, realloc, free);
- static void
- init_cache_map(void)
- {
- HT_INIT(&cache_root);
- }
- #ifdef USE_EVENTDNS
- static void
- eventdns_log_cb(int warn, const char *msg)
- {
- if (!strcmpstart(msg, "Resolve requested for") &&
- get_options()->SafeLogging) {
- log(LOG_INFO, LD_EXIT, "eventdns: Resolve requested.");
- return;
- } else if (!strcmpstart(msg, "Search: ")) {
- return;
- }
- log(warn?LOG_WARN:LOG_INFO, LD_EXIT, "eventdns: %s", msg);
- }
- #endif
- int
- dns_init(void)
- {
- init_cache_map();
- #ifdef USE_EVENTDNS
- if (server_mode(get_options()))
- return configure_nameservers(1);
- #else
- dnsworkers_rotate();
- #endif
- return 0;
- }
- void
- dns_reset(void)
- {
- #ifdef USE_EVENTDNS
- or_options_t *options = get_options();
- if (! server_mode(options)) {
- eventdns_clear_nameservers_and_suspend();
- eventdns_search_clear();
- nameservers_configured = 0;
- tor_free(resolv_conf_fname);
- resolv_conf_mtime = 0;
- } else {
- if (configure_nameservers(0) < 0)
-
- return;
- }
- #else
- dnsworkers_rotate();
- #endif
- }
- uint32_t
- dns_clip_ttl(uint32_t ttl)
- {
- if (ttl < MIN_DNS_TTL)
- return MIN_DNS_TTL;
- else if (ttl > MAX_DNS_TTL)
- return MAX_DNS_TTL;
- else
- return ttl;
- }
- static uint32_t
- dns_get_expiry_ttl(uint32_t ttl)
- {
- if (ttl < MIN_DNS_TTL)
- return MIN_DNS_TTL;
- else if (ttl > MAX_DNS_ENTRY_AGE)
- return MAX_DNS_ENTRY_AGE;
- else
- return ttl;
- }
- static void
- _free_cached_resolve(cached_resolve_t *r)
- {
- while (r->pending_connections) {
- pending_connection_t *victim = r->pending_connections;
- r->pending_connections = victim->next;
- tor_free(victim);
- }
- r->magic = 0xFF00FF00;
- tor_free(r);
- }
- static int
- _compare_cached_resolves_by_expiry(const void *_a, const void *_b)
- {
- const cached_resolve_t *a = _a, *b = _b;
- return a->expire - b->expire;
- }
- static smartlist_t *cached_resolve_pqueue = NULL;
- static void
- set_expiry(cached_resolve_t *resolve, time_t expires)
- {
- tor_assert(resolve && resolve->expire == 0);
- if (!cached_resolve_pqueue)
- cached_resolve_pqueue = smartlist_create();
- resolve->expire = expires;
- smartlist_pqueue_add(cached_resolve_pqueue,
- _compare_cached_resolves_by_expiry,
- resolve);
- }
- void
- dns_free_all(void)
- {
- cached_resolve_t **ptr, **next, *item;
- for (ptr = HT_START(cache_map, &cache_root); ptr != NULL; ptr = next) {
- item = *ptr;
- next = HT_NEXT_RMV(cache_map, &cache_root, ptr);
- _free_cached_resolve(item);
- }
- HT_CLEAR(cache_map, &cache_root);
- if (cached_resolve_pqueue)
- smartlist_free(cached_resolve_pqueue);
- cached_resolve_pqueue = NULL;
- #ifdef USE_EVENTDNS
- tor_free(resolv_conf_fname);
- #endif
- }
- static void
- purge_expired_resolves(time_t now)
- {
- cached_resolve_t *resolve, *removed;
- pending_connection_t *pend;
- edge_connection_t *pendconn;
- assert_cache_ok();
- if (!cached_resolve_pqueue)
- return;
- while (smartlist_len(cached_resolve_pqueue)) {
- resolve = smartlist_get(cached_resolve_pqueue, 0);
- if (resolve->expire > now)
- break;
- smartlist_pqueue_pop(cached_resolve_pqueue,
- _compare_cached_resolves_by_expiry);
- if (resolve->state == CACHE_STATE_PENDING) {
- log_debug(LD_EXIT,
- "Expiring a dns resolve %s that's still pending. Forgot to "
- "cull it? DNS resolve didn't tell us about the timeout?",
- escaped_safe_str(resolve->address));
- } else if (resolve->state == CACHE_STATE_CACHED_VALID ||
- resolve->state == CACHE_STATE_CACHED_FAILED) {
- log_debug(LD_EXIT,
- "Forgetting old cached resolve (address %s, expires %lu)",
- escaped_safe_str(resolve->address),
- (unsigned long)resolve->expire);
- tor_assert(!resolve->pending_connections);
- } else {
- tor_assert(resolve->state == CACHE_STATE_DONE);
- tor_assert(!resolve->pending_connections);
- }
- if (resolve->pending_connections) {
- log_debug(LD_EXIT,
- "Closing pending connections on timed-out DNS resolve!");
- tor_fragile_assert();
- while (resolve->pending_connections) {
- pend = resolve->pending_connections;
- resolve->pending_connections = pend->next;
-
- tor_assert(pend->conn->_base.s == -1);
- pendconn = pend->conn;
- connection_edge_end(pendconn, END_STREAM_REASON_TIMEOUT,
- pendconn->cpath_layer);
- circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
- connection_free(TO_CONN(pendconn));
- tor_free(pend);
- }
- }
- if (resolve->state == CACHE_STATE_CACHED_VALID ||
- resolve->state == CACHE_STATE_CACHED_FAILED ||
- resolve->state == CACHE_STATE_PENDING) {
- removed = HT_REMOVE(cache_map, &cache_root, resolve);
- if (removed != resolve) {
- log_err(LD_BUG, "The expired resolve we purged didn't match any in"
- " the cache. Tried to purge %s (%p); instead got %s (%p).",
- resolve->address, (void*)resolve,
- removed ? removed->address : "NULL", (void*)remove);
- }
- tor_assert(removed == resolve);
- resolve->magic = 0xF0BBF0BB;
- tor_free(resolve);
- } else {
-
- cached_resolve_t *tmp = HT_FIND(cache_map, &cache_root, resolve);
- tor_assert(tmp != resolve);
- }
- }
- assert_cache_ok();
- }
- static void
- send_resolved_cell(edge_connection_t *conn, uint8_t answer_type)
- {
- char buf[RELAY_PAYLOAD_SIZE];
- size_t buflen;
- uint32_t ttl;
- buf[0] = answer_type;
- ttl = dns_clip_ttl(conn->address_ttl);
- switch (answer_type)
- {
- case RESOLVED_TYPE_IPV4:
- buf[1] = 4;
- set_uint32(buf+2, htonl(conn->_base.addr));
- set_uint32(buf+6, htonl(ttl));
- buflen = 10;
- break;
- case RESOLVED_TYPE_ERROR_TRANSIENT:
- case RESOLVED_TYPE_ERROR:
- {
- const char *errmsg = "Error resolving hostname";
- int msglen = strlen(errmsg);
- buf[1] = msglen;
- strlcpy(buf+2, errmsg, sizeof(buf)-2);
- set_uint32(buf+2+msglen, htonl(ttl));
- buflen = 6+msglen;
- break;
- }
- default:
- tor_assert(0);
- return;
- }
- connection_edge_send_command(conn, circuit_get_by_edge_conn(conn),
- RELAY_COMMAND_RESOLVED, buf, buflen,
- conn->cpath_layer);
- }
- int
- dns_resolve(edge_connection_t *exitconn)
- {
- cached_resolve_t *resolve;
- cached_resolve_t search;
- pending_connection_t *pending_connection;
- struct in_addr in;
- circuit_t *circ;
- time_t now = time(NULL);
- assert_connection_ok(TO_CONN(exitconn), 0);
- tor_assert(exitconn->_base.s == -1);
- assert_cache_ok();
-
- if (tor_inet_aton(exitconn->_base.address, &in) != 0) {
- exitconn->_base.addr = ntohl(in.s_addr);
- exitconn->address_ttl = DEFAULT_DNS_TTL;
- if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
- send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
- return 1;
- }
-
- purge_expired_resolves(now);
-
- tor_strlower(exitconn->_base.address);
-
- strlcpy(search.address, exitconn->_base.address, sizeof(search.address));
- resolve = HT_FIND(cache_map, &cache_root, &search);
- if (resolve && resolve->expire > now) {
- switch (resolve->state) {
- case CACHE_STATE_PENDING:
-
- pending_connection = tor_malloc_zero(
- sizeof(pending_connection_t));
- pending_connection->conn = exitconn;
- pending_connection->next = resolve->pending_connections;
- resolve->pending_connections = pending_connection;
- log_debug(LD_EXIT,"Connection (fd %d) waiting for pending DNS "
- "resolve of %s", exitconn->_base.s,
- escaped_safe_str(exitconn->_base.address));
- exitconn->_base.state = EXIT_CONN_STATE_RESOLVING;
- return 0;
- case CACHE_STATE_CACHED_VALID:
- exitconn->_base.addr = resolve->addr;
- exitconn->address_ttl = resolve->ttl;
- log_debug(LD_EXIT,"Connection (fd %d) found cached answer for %s",
- exitconn->_base.s,
- escaped_safe_str(exitconn->_base.address));
- if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
- send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
- return 1;
- case CACHE_STATE_CACHED_FAILED:
- log_debug(LD_EXIT,"Connection (fd %d) found cached error for %s",
- exitconn->_base.s,
- escaped_safe_str(exitconn->_base.address));
- if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
- send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
- circ = circuit_get_by_edge_conn(exitconn);
- if (circ)
- circuit_detach_stream(circ, exitconn);
- if (!exitconn->_base.marked_for_close)
- connection_free(TO_CONN(exitconn));
- return -1;
- case CACHE_STATE_DONE:
- log_err(LD_BUG, "Found a 'DONE' dns resolve still in the cache.");
- tor_fragile_assert();
- }
- tor_assert(0);
- }
-
- resolve = tor_malloc_zero(sizeof(cached_resolve_t));
- resolve->magic = CACHED_RESOLVE_MAGIC;
- resolve->state = CACHE_STATE_PENDING;
- strlcpy(resolve->address, exitconn->_base.address, sizeof(resolve->address));
-
- pending_connection = tor_malloc_zero(sizeof(pending_connection_t));
- pending_connection->conn = exitconn;
- resolve->pending_connections = pending_connection;
- exitconn->_base.state = EXIT_CONN_STATE_RESOLVING;
-
- HT_INSERT(cache_map, &cache_root, resolve);
- set_expiry(resolve, now + RESOLVE_MAX_TIMEOUT);
- log_debug(LD_EXIT,"Launching %s.",
- escaped_safe_str(exitconn->_base.address));
- assert_cache_ok();
- return launch_resolve(exitconn);
- }
- void
- assert_connection_edge_not_dns_pending(edge_connection_t *conn)
- {
- pending_connection_t *pend;
- cached_resolve_t **resolve;
- HT_FOREACH(resolve, cache_map, &cache_root) {
- for (pend = (*resolve)->pending_connections;
- pend;
- pend = pend->next) {
- tor_assert(pend->conn != conn);
- }
- }
- }
- void
- assert_all_pending_dns_resolves_ok(void)
- {
- pending_connection_t *pend;
- cached_resolve_t **resolve;
- HT_FOREACH(resolve, cache_map, &cache_root) {
- for (pend = (*resolve)->pending_connections;
- pend;
- pend = pend->next) {
- assert_connection_ok(TO_CONN(pend->conn), 0);
- tor_assert(pend->conn->_base.s == -1);
- tor_assert(!connection_in_array(TO_CONN(pend->conn)));
- }
- }
- }
- void
- connection_dns_remove(edge_connection_t *conn)
- {
- pending_connection_t *pend, *victim;
- cached_resolve_t search;
- cached_resolve_t *resolve;
- tor_assert(conn->_base.type == CONN_TYPE_EXIT);
- tor_assert(conn->_base.state == EXIT_CONN_STATE_RESOLVING);
- strlcpy(search.address, conn->_base.address, sizeof(search.address));
- resolve = HT_FIND(cache_map, &cache_root, &search);
- if (!resolve) {
- log_notice(LD_BUG, "Address %s is not pending. Dropping.",
- escaped_safe_str(conn->_base.address));
- return;
- }
- tor_assert(resolve->pending_connections);
- assert_connection_ok(TO_CONN(conn),0);
- pend = resolve->pending_connections;
- if (pend->conn == conn) {
- resolve->pending_connections = pend->next;
- tor_free(pend);
- log_debug(LD_EXIT, "First connection (fd %d) no longer waiting "
- "for resolve of %s",
- conn->_base.s, escaped_safe_str(conn->_base.address));
- return;
- } else {
- for ( ; pend->next; pend = pend->next) {
- if (pend->next->conn == conn) {
- victim = pend->next;
- pend->next = victim->next;
- tor_free(victim);
- log_debug(LD_EXIT,
- "Connection (fd %d) no longer waiting for resolve of %s",
- conn->_base.s, escaped_safe_str(conn->_base.address));
- return;
- }
- }
- tor_assert(0);
- }
- }
- void
- dns_cancel_pending_resolve(char *address)
- {
- pending_connection_t *pend;
- cached_resolve_t search;
- cached_resolve_t *resolve, *tmp;
- edge_connection_t *pendconn;
- circuit_t *circ;
- strlcpy(search.address, address, sizeof(search.address));
- resolve = HT_FIND(cache_map, &cache_root, &search);
- if (!resolve || resolve->state != CACHE_STATE_PENDING) {
- log_notice(LD_BUG,"Address %s is not pending. Dropping.",
- escaped_safe_str(address));
- return;
- }
- if (!resolve->pending_connections) {
-
- log_warn(LD_BUG,
- "Bug: Address %s is pending but has no pending connections!",
- escaped_safe_str(address));
- tor_fragile_assert();
- return;
- }
- tor_assert(resolve->pending_connections);
-
- log_debug(LD_EXIT,
- "Failing all connections waiting on DNS resolve of %s",
- escaped_safe_str(address));
- while (resolve->pending_connections) {
- pend = resolve->pending_connections;
- pend->conn->_base.state = EXIT_CONN_STATE_RESOLVEFAILED;
- pendconn = pend->conn;
- assert_connection_ok(TO_CONN(pendconn), 0);
- tor_assert(pendconn->_base.s == -1);
- if (!pendconn->_base.marked_for_close) {
- connection_edge_end(pendconn, END_STREAM_REASON_RESOURCELIMIT,
- pendconn->cpath_layer);
- }
- circ = circuit_get_by_edge_conn(pendconn);
- if (circ)
- circuit_detach_stream(circ, pendconn);
- connection_free(TO_CONN(pendconn));
- resolve->pending_connections = pend->next;
- tor_free(pend);
- }
- tmp = HT_REMOVE(cache_map, &cache_root, resolve);
- if (tmp != resolve) {
- log_err(LD_BUG, "The cancelled resolve we purged didn't match any in"
- " the cache. Tried to purge %s (%p); instead got %s (%p).",
- resolve->address, (void*)resolve,
- tmp ? tmp->address : "NULL", (void*)tmp);
- }
- tor_assert(tmp == resolve);
- resolve->state = CACHE_STATE_DONE;
- }
- static void
- add_answer_to_cache(const char *address, uint32_t addr, char outcome,
- uint32_t ttl)
- {
- cached_resolve_t *resolve;
- if (outcome == DNS_RESOLVE_FAILED_TRANSIENT)
- return;
- resolve = tor_malloc_zero(sizeof(cached_resolve_t));
- resolve->magic = CACHED_RESOLVE_MAGIC;
- resolve->state = (outcome == DNS_RESOLVE_SUCCEEDED) ?
- CACHE_STATE_CACHED_VALID : CACHE_STATE_CACHED_FAILED;
- strlcpy(resolve->address, address, sizeof(resolve->address));
- resolve->addr = addr;
- resolve->ttl = ttl;
- assert_resolve_ok(resolve);
- HT_INSERT(cache_map, &cache_root, resolve);
- set_expiry(resolve, time(NULL) + dns_get_expiry_ttl(ttl));
- }
- static void
- dns_found_answer(const char *address, uint32_t addr, char outcome,
- uint32_t ttl)
- {
- pending_connection_t *pend;
- cached_resolve_t search;
- cached_resolve_t *resolve, *removed;
- edge_connection_t *pendconn;
- circuit_t *circ;
- assert_cache_ok();
- strlcpy(search.address, address, sizeof(search.address));
- resolve = HT_FIND(cache_map, &cache_root, &search);
- if (!resolve) {
- log_info(LD_EXIT,"Resolved unasked address %s; caching anyway.",
- escaped_safe_str(address));
- add_answer_to_cache(address, addr, outcome, ttl);
- return;
- }
- assert_resolve_ok(resolve);
- if (resolve->state != CACHE_STATE_PENDING) {
-
- log_notice(LD_EXIT, "Resolved %s which was already resolved; ignoring",
- escaped_safe_str(address));
- tor_assert(resolve->pending_connections == NULL);
- return;
- }
-
-
- while (resolve->pending_connections) {
- pend = resolve->pending_connections;
- pendconn = pend->conn;
- assert_connection_ok(TO_CONN(pendconn),time(NULL));
- pendconn->_base.addr = addr;
- pendconn->address_ttl = ttl;
- if (outcome != DNS_RESOLVE_SUCCEEDED) {
-
- pendconn->_base.state = EXIT_CONN_STATE_RESOLVEFAILED;
- if (pendconn->_base.purpose == EXIT_PURPOSE_CONNECT) {
- connection_edge_end(pendconn, END_STREAM_REASON_RESOLVEFAILED,
- pendconn->cpath_layer);
-
- circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
- } else {
- send_resolved_cell(pendconn, RESOLVED_TYPE_ERROR);
-
- circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
- }
- connection_free(TO_CONN(pendconn));
- } else {
- if (pendconn->_base.purpose == EXIT_PURPOSE_CONNECT) {
-
- pend->conn->_base.state = EXIT_CONN_STATE_CONNECTING;
- circ = circuit_get_by_edge_conn(pend->conn);
- tor_assert(circ);
- tor_assert(!CIRCUIT_IS_ORIGIN(circ));
-
- circuit_detach_stream(circ, pend->conn);
-
- pend->conn->next_stream = TO_OR_CIRCUIT(circ)->n_streams;
- pend->conn->on_circuit = circ;
- TO_OR_CIRCUIT(circ)->n_streams = pend->conn;
- connection_exit_connect(pend->conn);
- } else {
-
- pendconn->_base.state = EXIT_CONN_STATE_RESOLVEFAILED;
- send_resolved_cell(pendconn, RESOLVED_TYPE_IPV4);
- circ = circuit_get_by_edge_conn(pendconn);
- tor_assert(circ);
- circuit_detach_stream(circ, pendconn);
- connection_free(TO_CONN(pendconn));
- }
- }
- resolve->pending_connections = pend->next;
- tor_free(pend);
- }
- resolve->state = CACHE_STATE_DONE;
- removed = HT_REMOVE(cache_map, &cache_root, &search);
- if (removed != resolve) {
- log_err(LD_BUG, "The pending resolve we found wasn't removable from"
- " the cache. Tried to purge %s (%p); instead got %s (%p).",
- resolve->address, (void*)resolve,
- removed ? removed->address : "NULL", (void*)removed);
- }
- assert_resolve_ok(resolve);
- assert_cache_ok();
- add_answer_to_cache(address, addr, outcome, ttl);
- assert_cache_ok();
- }
- #ifndef USE_EVENTDNS
- static int
- launch_resolve(edge_connection_t *exitconn)
- {
- connection_t *dnsconn;
- unsigned char len;
- tor_assert(exitconn->_base.state == EXIT_CONN_STATE_RESOLVING);
- assert_connection_ok(TO_CONN(exitconn), 0);
- tor_assert(exitconn->_base.s == -1);
-
- if (spawn_enough_dnsworkers() < 0) {
- goto err;
- }
- dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
- DNSWORKER_STATE_IDLE);
- if (!dnsconn) {
- log_warn(LD_EXIT,"no idle dns workers. Failing.");
- if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
- send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
- goto err;
- }
- log_debug(LD_EXIT,
- "Connection (fd %d) needs to resolve %s; assigning "
- "to DNSWorker (fd %d)", exitconn->_base.s,
- escaped_safe_str(exitconn->_base.address), dnsconn->s);
- tor_free(dnsconn->address);
- dnsconn->address = tor_strdup(exitconn->_base.address);
- dnsconn->state = DNSWORKER_STATE_BUSY;
-
- dnsconn->timestamp_lastwritten = time(NULL);
- num_dnsworkers_busy++;
- len = strlen(dnsconn->address);
- connection_write_to_buf((char*)&len, 1, dnsconn);
- connection_write_to_buf(dnsconn->address, len, dnsconn);
- return 0;
- err:
-
- dns_cancel_pending_resolve(exitconn->_base.address);
- return -1;
- }
- int
- connection_dns_finished_flushing(connection_t *conn)
- {
- tor_assert(conn);
- tor_assert(conn->type == CONN_TYPE_DNSWORKER);
- connection_stop_writing(conn);
- return 0;
- }
- int
- connection_dns_reached_eof(connection_t *conn)
- {
- log_warn(LD_EXIT,"Read eof. Worker died unexpectedly.");
- if (conn->state == DNSWORKER_STATE_BUSY) {
-
- num_dnsworkers_busy--;
- }
- num_dnsworkers--;
- connection_mark_for_close(conn);
- return 0;
- }
- int
- connection_dns_process_inbuf(connection_t *conn)
- {
- char success;
- uint32_t addr;
- int ttl;
- tor_assert(conn);
- tor_assert(conn->type == CONN_TYPE_DNSWORKER);
- if (conn->state != DNSWORKER_STATE_BUSY && buf_datalen(conn->inbuf)) {
- log_warn(LD_BUG,
- "Bug: read data (%d bytes) from an idle dns worker (fd %d, "
- "address %s). Please report.", (int)buf_datalen(conn->inbuf),
- conn->s, escaped_safe_str(conn->address));
- tor_fragile_assert();
-
- while (buf_datalen(conn->inbuf)) {
- connection_fetch_from_buf(&success,1,conn);
- connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
- log_warn(LD_EXIT,"Discarding idle dns answer (success %d, addr %d.)",
- success, addr);
- }
- return 0;
- }
- if (buf_datalen(conn->inbuf) < 5)
- return 0;
- tor_assert(conn->state == DNSWORKER_STATE_BUSY);
- tor_assert(buf_datalen(conn->inbuf) == 5);
- connection_fetch_from_buf(&success,1,conn);
- connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
- log_debug(LD_EXIT, "DNSWorker (fd %d) returned answer for %s",
- conn->s, escaped_safe_str(conn->address));
- tor_assert(success >= DNS_RESOLVE_FAILED_TRANSIENT);
- tor_assert(success <= DNS_RESOLVE_SUCCEEDED);
- ttl = (success == DNS_RESOLVE_FAILED_TRANSIENT) ? 0 : MAX_DNS_ENTRY_AGE;
- dns_found_answer(conn->address, ntohl(addr), success, ttl);
- tor_free(conn->address);
- conn->address = tor_strdup("<idle>");
- conn->state = DNSWORKER_STATE_IDLE;
- num_dnsworkers_busy--;
- if (conn->timestamp_created < last_rotation_time) {
- connection_mark_for_close(conn);
- num_dnsworkers--;
- spawn_enough_dnsworkers();
- }
- return 0;
- }
- static void
- dnsworkers_rotate(void)
- {
- connection_t *dnsconn;
- while ((dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
- DNSWORKER_STATE_IDLE))) {
- connection_mark_for_close(dnsconn);
- num_dnsworkers--;
- }
- last_rotation_time = time(NULL);
- if (server_mode(get_options()))
- spawn_enough_dnsworkers();
- }
- static void
- dnsworker_main(void *data)
- {
- char address[MAX_ADDRESSLEN+1];
- unsigned char address_len;
- char *log_address;
- char answer[5];
- uint32_t ip;
- int *fdarray = data;
- int fd;
- int result;
- int search = get_options()->SearchDomains;
-
- fd = fdarray[1];
- #ifndef TOR_IS_MULTITHREADED
- tor_close_socket(fdarray[0]);
- tor_free_all(1);
- handle_signals(0);
- #endif
- tor_free(data);
- for (;;) {
- int r;
- if ((r = recv(fd, &address_len, 1, 0)) != 1) {
- if (r == 0) {
- log_info(LD_EXIT,"DNS worker exiting because Tor process closed "
- "connection (either pruned idle dnsworker or died).");
- } else {
- log_info(LD_EXIT,"DNS worker exiting because of error on connection "
- "to Tor process.");
- log_info(LD_EXIT,"(Error on %d was %s)", fd,
- tor_socket_strerror(tor_socket_errno(fd)));
- }
- tor_close_socket(fd);
- crypto_thread_cleanup();
- spawn_exit();
- }
- if (address_len && read_all(fd, address, address_len, 1) != address_len) {
- log_err(LD_BUG,"read hostname failed. Child exiting.");
- tor_close_socket(fd);
- crypto_thread_cleanup();
- spawn_exit();
- }
-
- if (address[address_len-1] != '.' && !search) {
- address[address_len] = '.';
- address[address_len+1] = '\0';
- } else {
- address[address_len] = '\0';
- }
- log_address = esc_for_log(safe_str(address));
- result = tor_lookup_hostname(address, &ip);
-
- if (!ip)
- result = -1;
- switch (result) {
- case 1:
-
- log_info(LD_NET,"Could not resolve dest addr %s (transient)",
- log_address);
- answer[0] = DNS_RESOLVE_FAILED_TRANSIENT;
- break;
- case -1:
- log_info(LD_NET,"Could not resolve dest addr %s (permanent)",
- log_address);
- answer[0] = DNS_RESOLVE_FAILED_PERMANENT;
- break;
- case 0:
- log_info(LD_NET,"Resolved address %s", log_address);
- answer[0] = DNS_RESOLVE_SUCCEEDED;
- break;
- }
- tor_free(log_address);
- set_uint32(answer+1, ip);
- if (write_all(fd, answer, 5, 1) != 5) {
- log_err(LD_NET,"writing answer failed. Child exiting.");
- tor_close_socket(fd);
- crypto_thread_cleanup();
- spawn_exit();
- }
- }
- }
- static int
- spawn_dnsworker(void)
- {
- int *fdarray;
- int fd;
- connection_t *conn;
- int err;
- fdarray = tor_malloc(sizeof(int)*2);
- if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
- log_warn(LD_NET, "Couldn't construct socketpair: %s",
- tor_socket_strerror(-err));
- tor_free(fdarray);
- return -1;
- }
- tor_assert(fdarray[0] >= 0);
- tor_assert(fdarray[1] >= 0);
-
- fd = fdarray[0];
- spawn_func((void*) dnsworker_main, (void*)fdarray);
- log_debug(LD_EXIT,"just spawned a dns worker.");
- #ifndef TOR_IS_MULTITHREADED
- tor_close_socket(fdarray[1]);
- tor_free(fdarray);
- #endif
- conn = connection_new(CONN_TYPE_DNSWORKER);
- set_socket_nonblocking(fd);
-
- conn->s = fd;
- conn->address = tor_strdup("<unused>");
- if (connection_add(conn) < 0) {
- log_warn(LD_NET,"connection_add failed. Giving up.");
- connection_free(conn);
- return -1;
- }
- conn->state = DNSWORKER_STATE_IDLE;
- connection_start_reading(conn);
- return 0;
- }
- static int
- spawn_enough_dnsworkers(void)
- {
- int num_dnsworkers_needed;
- connection_t *dnsconn;
-
- if (num_dnsworkers_busy == MAX_DNSWORKERS) {
-
- dnsconn = connection_get_by_type_state_lastwritten(CONN_TYPE_DNSWORKER,
- DNSWORKER_STATE_BUSY);
- tor_assert(dnsconn);
- log_warn(LD_EXIT, "%d DNS workers are spawned; all are busy. Killing one.",
- MAX_DNSWORKERS);
- connection_mark_for_close(dnsconn);
- num_dnsworkers_busy--;
- num_dnsworkers--;
- }
- if (num_dnsworkers_busy >= MIN_DNSWORKERS)
- num_dnsworkers_needed = num_dnsworkers_busy+1;
- else
- num_dnsworkers_needed = MIN_DNSWORKERS;
- while (num_dnsworkers < num_dnsworkers_needed) {
- if (spawn_dnsworker() < 0) {
- log_warn(LD_EXIT,"Spawn failed. Will try again later.");
- return -1;
- }
- num_dnsworkers++;
- }
- while (num_dnsworkers > num_dnsworkers_busy+MAX_IDLE_DNSWORKERS) {
-
-
- log_info(LD_EXIT,"%d of %d dnsworkers are idle. Killing one.",
- num_dnsworkers-num_dnsworkers_busy, num_dnsworkers);
- dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
- DNSWORKER_STATE_IDLE);
- tor_assert(dnsconn);
- connection_mark_for_close(dnsconn);
- num_dnsworkers--;
- }
- return 0;
- }
- #else
- static int
- eventdns_err_is_transient(int err)
- {
- switch (err)
- {
- case DNS_ERR_SERVERFAILED:
- case DNS_ERR_TRUNCATED:
- case DNS_ERR_TIMEOUT:
- return 1;
- default:
- return 0;
- }
- }
- int
- connection_dns_finished_flushing(connection_t *conn)
- {
- (void)conn;
- tor_assert(0);
- return 0;
- }
- int
- connection_dns_process_inbuf(connection_t *conn)
- {
- (void)conn;
- tor_assert(0);
- return 0;
- }
- int
- connection_dns_reached_eof(connection_t *conn)
- {
- (void)conn;
- tor_assert(0);
- return 0;
- }
- static int
- configure_nameservers(int force)
- {
- or_options_t *options;
- const char *conf_fname;
- struct stat st;
- options = get_options();
- conf_fname = options->ResolvConf;
- #ifndef MS_WINDOWS
- if (!conf_fname)
- conf_fname = "/etc/resolv.conf";
- #endif
- eventdns_set_log_fn(eventdns_log_cb);
- if (conf_fname) {
- if (stat(conf_fname, &st)) {
- log_warn(LD_EXIT, "Unable to stat resolver configuration in '%s'",
- conf_fname);
- return -1;
- }
- if (!force && resolv_conf_fname && !strcmp(conf_fname,resolv_conf_fname)
- && st.st_mtime == resolv_conf_mtime) {
- log_info(LD_EXIT, "No change to '%s'", conf_fname);
- return 0;
- }
- if (nameservers_configured) {
- eventdns_search_clear();
- eventdns_clear_nameservers_and_suspend();
- }
- log_info(LD_EXIT, "Parsing resolver configuration in '%s'", conf_fname);
- if (eventdns_resolv_conf_parse(DNS_OPTIONS_ALL, conf_fname))
- return -1;
- if (eventdns_count_nameservers() == 0) {
- log_warn(LD_EXIT, "Unable to find any nameservers in '%s'.", conf_fname);
- return -1;
- }
- tor_free(resolv_conf_fname);
- resolv_conf_fname = tor_strdup(conf_fname);
- resolv_conf_mtime = st.st_mtime;
- if (nameservers_configured)
- eventdns_resume();
- }
- #ifdef MS_WINDOWS
- else {
- if (nameservers_configured) {
- eventdns_search_clear();
- eventdns_clear_nameservers_and_suspend();
- }
- if (eventdns_config_windows_nameservers()) {
- log_warn(LD_EXIT,"Could not config nameservers.");
- return -1;
- }
- if (eventdns_count_nameservers() == 0) {
- log_warn(LD_EXIT, "Unable to find any platform nameservers in "
- "your Windows configuration. Perhaps you should list a "
- "ResolvConf file in your torrc?");
- return -1;
- }
- if (nameservers_configured)
- eventdns_resume();
- tor_free(resolv_conf_fname);
- resolv_conf_mtime = 0;
- }
- #endif
- nameservers_configured = 1;
- return 0;
- }
- static void
- eventdns_callback(int result, char type, int count, int ttl, void *addresses,
- void *arg)
- {
- char *string_address = arg;
- int status = DNS_RESOLVE_FAILED_PERMANENT;
- uint32_t addr = 0;
- if (result == DNS_ERR_NONE) {
- if (type == DNS_IPv4_A && count) {
- char answer_buf[INET_NTOA_BUF_LEN+1];
- struct in_addr in;
- uint32_t *addrs = addresses;
- in.s_addr = addrs[0];
- addr = ntohl(addrs[0]);
- status = DNS_RESOLVE_SUCCEEDED;
- tor_inet_ntoa(&in, answer_buf, sizeof(answer_buf));
- log_debug(LD_EXIT, "eventdns said that %s resolves to %s",
- escaped_safe_str(string_address),
- escaped_safe_str(answer_buf));
- } else if (count) {
- log_warn(LD_EXIT, "eventdns returned only non-IPv4 answers for %s.",
- escaped_safe_str(string_address));
- } else {
- log_warn(LD_BUG, "eventdns returned no addresses or error for %s!",
- escaped_safe_str(string_address));
- }
- } else {
- if (eventdns_err_is_transient(result))
- status = DNS_RESOLVE_FAILED_TRANSIENT;
- }
- dns_found_answer(string_address, addr, status, ttl);
- tor_free(string_address);
- }
- static int
- launch_resolve(edge_connection_t *exitconn)
- {
- char *addr = tor_strdup(exitconn->_base.address);
- int r;
- int options = get_options()->SearchDomains ? 0 : DNS_QUERY_NO_SEARCH;
-
- if (!nameservers_configured) {
- log_warn(LD_EXIT, "Harmless bug: nameservers not configured, but resolve "
- "launched. Configuring.");
- if (configure_nameservers(1) < 0)
- return -1;
- }
- log_info(LD_EXIT, "Launching eventdns request for %s",
- escaped_safe_str(exitconn->_base.address));
- r = eventdns_resolve_ipv4(exitconn->_base.address, options,
- eventdns_callback, addr);
- if (r) {
- log_warn(LD_EXIT, "eventdns rejected address %s: error %d.",
- escaped_safe_str(addr), r);
- if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE) {
- if (eventdns_err_is_transient(r))
- send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
- else {
- exitconn->address_ttl = DEFAULT_DNS_TTL;
- send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
- }
- }
- dns_cancel_pending_resolve(addr);
- tor_free(addr);
- }
- return r ? -1 : 0;
- }
- #endif
- static void
- assert_resolve_ok(cached_resolve_t *resolve)
- {
- tor_assert(resolve);
- tor_assert(resolve->magic == CACHED_RESOLVE_MAGIC);
- tor_assert(strlen(resolve->address) < MAX_ADDRESSLEN);
- tor_assert(tor_strisnonupper(resolve->address));
- if (resolve->state != CACHE_STATE_PENDING) {
- tor_assert(!resolve->pending_connections);
- }
- if (resolve->state == CACHE_STATE_PENDING ||
- resolve->state == CACHE_STATE_DONE) {
- tor_assert(!resolve->ttl);
- tor_assert(!resolve->addr);
- }
- }
- #ifdef DEBUG_DNS_CACHE
- static void
- _assert_cache_ok(void)
- {
- cached_resolve_t **resolve;
- int bad_rep = _cache_map_HT_REP_IS_BAD(&cache_root);
- if (bad_rep) {
- log_err(LD_BUG, "Bad rep type %d on dns cache hash table", bad_rep);
- tor_assert(!bad_rep);
- }
- HT_FOREACH(resolve, cache_map, &cache_root) {
- assert_resolve_ok(*resolve);
- tor_assert((*resolve)->state != CACHE_STATE_DONE);
- }
- if (!cached_resolve_pqueue)
- return;
- smartlist_pqueue_assert_ok(cached_resolve_pqueue,
- _compare_cached_resolves_by_expiry);
- }
- #endif
|