xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/evdns.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: evdns.c,v 1.7 2024/08/18 20:47:21 christos Exp $	*/
2 
3 /* Copyright 2006-2007 Niels Provos
4  * Copyright 2007-2012 Nick Mathewson and Niels Provos
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Based on software by Adam Langly. Adam's original message:
30  *
31  * Async DNS Library
32  * Adam Langley <agl@imperialviolet.org>
33  * http://www.imperialviolet.org/eventdns.html
34  * Public Domain code
35  *
36  * This software is Public Domain. To view a copy of the public domain dedication,
37  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
38  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
39  *
40  * I ask and expect, but do not require, that all derivative works contain an
41  * attribution similar to:
42  *	Parts developed by Adam Langley <agl@imperialviolet.org>
43  *
44  * You may wish to replace the word "Parts" with something else depending on
45  * the amount of original code.
46  *
47  * (Derivative works does not include programs which link against, run or include
48  * the source verbatim in their source distributions)
49  *
50  * Version: 0.1b
51  */
52 
53 #include "event2/event-config.h"
54 #include "evconfig-private.h"
55 
56 #include <sys/types.h>
57 
58 #ifndef _FORTIFY_SOURCE
59 #define _FORTIFY_SOURCE 3
60 #endif
61 
62 #include <string.h>
63 #include <fcntl.h>
64 #ifdef EVENT__HAVE_SYS_TIME_H
65 #include <sys/time.h>
66 #endif
67 #ifdef EVENT__HAVE_STDINT_H
68 #include <stdint.h>
69 #endif
70 #include <stdlib.h>
71 #include <string.h>
72 #include <errno.h>
73 #ifdef EVENT__HAVE_UNISTD_H
74 #include <unistd.h>
75 #endif
76 #include <limits.h>
77 #include <sys/stat.h>
78 #include <stdio.h>
79 #include <stdarg.h>
80 #ifdef _WIN32
81 #include <winsock2.h>
82 #include <winerror.h>
83 #include <ws2tcpip.h>
84 #ifndef _WIN32_IE
85 #define _WIN32_IE 0x400
86 #endif
87 #include <shlobj.h>
88 #endif
89 
90 #include "event2/dns.h"
91 #include "event2/dns_struct.h"
92 #include "event2/dns_compat.h"
93 #include "event2/util.h"
94 #include "event2/event.h"
95 #include "event2/event_struct.h"
96 #include "event2/thread.h"
97 
98 #include "defer-internal.h"
99 #include "log-internal.h"
100 #include "mm-internal.h"
101 #include "strlcpy-internal.h"
102 #include "ipv6-internal.h"
103 #include "util-internal.h"
104 #include "evthread-internal.h"
105 #ifdef _WIN32
106 #include <ctype.h>
107 #include <winsock2.h>
108 #include <windows.h>
109 #include <iphlpapi.h>
110 #include <io.h>
111 #else
112 #include <sys/socket.h>
113 #include <netinet/in.h>
114 #include <arpa/inet.h>
115 #endif
116 
117 #ifdef EVENT__HAVE_NETINET_IN6_H
118 #include <netinet/in6.h>
119 #endif
120 
121 #define EVDNS_LOG_DEBUG EVENT_LOG_DEBUG
122 #define EVDNS_LOG_WARN EVENT_LOG_WARN
123 #define EVDNS_LOG_MSG EVENT_LOG_MSG
124 
125 #ifndef HOST_NAME_MAX
126 #define HOST_NAME_MAX 255
127 #endif
128 
129 #include <stdio.h>
130 
131 #undef MIN
132 #define MIN(a,b) ((a)<(b)?(a):(b))
133 
134 #define ASSERT_VALID_REQUEST(req) \
135 	EVUTIL_ASSERT((req)->handle && (req)->handle->current_req == (req))
136 
137 #define u64 ev_uint64_t
138 #define u32 ev_uint32_t
139 #define u16 ev_uint16_t
140 #define u8  ev_uint8_t
141 
142 /* maximum number of addresses from a single packet */
143 /* that we bother recording */
144 #define MAX_V4_ADDRS 32
145 #define MAX_V6_ADDRS 32
146 
147 
148 #define TYPE_A	       EVDNS_TYPE_A
149 #define TYPE_CNAME     5
150 #define TYPE_PTR       EVDNS_TYPE_PTR
151 #define TYPE_SOA       EVDNS_TYPE_SOA
152 #define TYPE_AAAA      EVDNS_TYPE_AAAA
153 
154 #define CLASS_INET     EVDNS_CLASS_INET
155 
156 /* Persistent handle.  We keep this separate from 'struct request' since we
157  * need some object to last for as long as an evdns_request is outstanding so
158  * that it can be canceled, whereas a search request can lead to multiple
159  * 'struct request' instances being created over its lifetime. */
160 struct evdns_request {
161 	struct request *current_req;
162 	struct evdns_base *base;
163 
164 	int pending_cb; /* Waiting for its callback to be invoked; not
165 			 * owned by event base any more. */
166 
167 	/* elements used by the searching code */
168 	int search_index;
169 	struct search_state *search_state;
170 	char *search_origname;	/* needs to be free()ed */
171 	int search_flags;
172 };
173 
174 struct request {
175 	u8 *request;  /* the dns packet data */
176 	u8 request_type; /* TYPE_PTR or TYPE_A or TYPE_AAAA */
177 	unsigned int request_len;
178 	int reissue_count;
179 	int tx_count;  /* the number of times that this packet has been sent */
180 	void *user_pointer;  /* the pointer given to us for this request */
181 	evdns_callback_type user_callback;
182 	struct nameserver *ns;	/* the server which we last sent it */
183 
184 	/* these objects are kept in a circular list */
185 	/* XXX We could turn this into a CIRCLEQ. */
186 	struct request *next, *prev;
187 
188 	struct event timeout_event;
189 
190 	u16 trans_id;  /* the transaction id */
191 	unsigned request_appended :1;	/* true if the request pointer is data which follows this struct */
192 	unsigned transmit_me :1;  /* needs to be transmitted */
193 
194 	/* XXXX This is a horrible hack. */
195 	char **put_cname_in_ptr; /* store the cname here if we get one. */
196 
197 	struct evdns_base *base;
198 
199 	struct evdns_request *handle;
200 };
201 
202 struct reply {
203 	unsigned int type;
204 	unsigned int have_answer : 1;
205 	union {
206 		struct {
207 			u32 addrcount;
208 			u32 addresses[MAX_V4_ADDRS];
209 		} a;
210 		struct {
211 			u32 addrcount;
212 			struct in6_addr addresses[MAX_V6_ADDRS];
213 		} aaaa;
214 		struct {
215 			char name[HOST_NAME_MAX];
216 		} ptr;
217 	} data;
218 };
219 
220 struct nameserver {
221 	evutil_socket_t socket;	 /* a connected UDP socket */
222 	struct sockaddr_storage address;
223 	ev_socklen_t addrlen;
224 	int failed_times;  /* number of times which we have given this server a chance */
225 	int timedout;  /* number of times in a row a request has timed out */
226 	struct event event;
227 	/* these objects are kept in a circular list */
228 	struct nameserver *next, *prev;
229 	struct event timeout_event;  /* used to keep the timeout for */
230 				     /* when we next probe this server. */
231 				     /* Valid if state == 0 */
232 	/* Outstanding probe request for this nameserver, if any */
233 	struct evdns_request *probe_request;
234 	char state;  /* zero if we think that this server is down */
235 	char choked;  /* true if we have an EAGAIN from this server's socket */
236 	char write_waiting;  /* true if we are waiting for EV_WRITE events */
237 	struct evdns_base *base;
238 
239 	/* Number of currently inflight requests: used
240 	 * to track when we should add/del the event. */
241 	int requests_inflight;
242 };
243 
244 
245 /* Represents a local port where we're listening for DNS requests. Right now, */
246 /* only UDP is supported. */
247 struct evdns_server_port {
248 	evutil_socket_t socket; /* socket we use to read queries and write replies. */
249 	int refcnt; /* reference count. */
250 	char choked; /* Are we currently blocked from writing? */
251 	char closing; /* Are we trying to close this port, pending writes? */
252 	evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
253 	void *user_data; /* Opaque pointer passed to user_callback */
254 	struct event event; /* Read/write event */
255 	/* circular list of replies that we want to write. */
256 	struct server_request *pending_replies;
257 	struct event_base *event_base;
258 
259 #ifndef EVENT__DISABLE_THREAD_SUPPORT
260 	void *lock;
261 #endif
262 };
263 
264 /* Represents part of a reply being built.	(That is, a single RR.) */
265 struct server_reply_item {
266 	struct server_reply_item *next; /* next item in sequence. */
267 	char *name; /* name part of the RR */
268 	u16 type; /* The RR type */
269 	u16 class; /* The RR class (usually CLASS_INET) */
270 	u32 ttl; /* The RR TTL */
271 	char is_name; /* True iff data is a label */
272 	u16 datalen; /* Length of data; -1 if data is a label */
273 	void *data; /* The contents of the RR */
274 };
275 
276 /* Represents a request that we've received as a DNS server, and holds */
277 /* the components of the reply as we're constructing it. */
278 struct server_request {
279 	/* Pointers to the next and previous entries on the list of replies */
280 	/* that we're waiting to write.	 Only set if we have tried to respond */
281 	/* and gotten EAGAIN. */
282 	struct server_request *next_pending;
283 	struct server_request *prev_pending;
284 
285 	u16 trans_id; /* Transaction id. */
286 	struct evdns_server_port *port; /* Which port received this request on? */
287 	struct sockaddr_storage addr; /* Where to send the response */
288 	ev_socklen_t addrlen; /* length of addr */
289 
290 	int n_answer; /* how many answer RRs have been set? */
291 	int n_authority; /* how many authority RRs have been set? */
292 	int n_additional; /* how many additional RRs have been set? */
293 
294 	struct server_reply_item *answer; /* linked list of answer RRs */
295 	struct server_reply_item *authority; /* linked list of authority RRs */
296 	struct server_reply_item *additional; /* linked list of additional RRs */
297 
298 	/* Constructed response.  Only set once we're ready to send a reply. */
299 	/* Once this is set, the RR fields are cleared, and no more should be set. */
300 	char *response;
301 	size_t response_len;
302 
303 	/* Caller-visible fields: flags, questions. */
304 	struct evdns_server_request base;
305 };
306 
307 struct evdns_base {
308 	/* An array of n_req_heads circular lists for inflight requests.
309 	 * Each inflight request req is in req_heads[req->trans_id % n_req_heads].
310 	 */
311 	struct request **req_heads;
312 	/* A circular list of requests that we're waiting to send, but haven't
313 	 * sent yet because there are too many requests inflight */
314 	struct request *req_waiting_head;
315 	/* A circular list of nameservers. */
316 	struct nameserver *server_head;
317 	int n_req_heads;
318 
319 	struct event_base *event_base;
320 
321 	/* The number of good nameservers that we have */
322 	int global_good_nameservers;
323 
324 	/* inflight requests are contained in the req_head list */
325 	/* and are actually going out across the network */
326 	int global_requests_inflight;
327 	/* requests which aren't inflight are in the waiting list */
328 	/* and are counted here */
329 	int global_requests_waiting;
330 
331 	int global_max_requests_inflight;
332 
333 	struct timeval global_timeout;	/* 5 seconds by default */
334 	int global_max_reissues;  /* a reissue occurs when we get some errors from the server */
335 	int global_max_retransmits;  /* number of times we'll retransmit a request which timed out */
336 	/* number of timeouts in a row before we consider this server to be down */
337 	int global_max_nameserver_timeout;
338 	/* true iff we will use the 0x20 hack to prevent poisoning attacks. */
339 	int global_randomize_case;
340 
341 	/* The first time that a nameserver fails, how long do we wait before
342 	 * probing to see if it has returned?  */
343 	struct timeval global_nameserver_probe_initial_timeout;
344 
345 	/** Port to bind to for outgoing DNS packets. */
346 	struct sockaddr_storage global_outgoing_address;
347 	/** ev_socklen_t for global_outgoing_address. 0 if it isn't set. */
348 	ev_socklen_t global_outgoing_addrlen;
349 
350 	struct timeval global_getaddrinfo_allow_skew;
351 
352 	int so_rcvbuf;
353 	int so_sndbuf;
354 
355 	int getaddrinfo_ipv4_timeouts;
356 	int getaddrinfo_ipv6_timeouts;
357 	int getaddrinfo_ipv4_answered;
358 	int getaddrinfo_ipv6_answered;
359 
360 	struct search_state *global_search_state;
361 
362 	TAILQ_HEAD(hosts_list, hosts_entry) hostsdb;
363 
364 #ifndef EVENT__DISABLE_THREAD_SUPPORT
365 	void *lock;
366 #endif
367 
368 	int disable_when_inactive;
369 };
370 
371 struct hosts_entry {
372 	TAILQ_ENTRY(hosts_entry) next;
373 	union {
374 		struct sockaddr sa;
375 		struct sockaddr_in sin;
376 		struct sockaddr_in6 sin6;
377 	} addr;
378 	int addrlen;
379 	char hostname[1];
380 };
381 
382 static struct evdns_base *current_base = NULL;
383 
384 struct evdns_base *
385 evdns_get_global_base(void)
386 {
387 	return current_base;
388 }
389 
390 /* Given a pointer to an evdns_server_request, get the corresponding */
391 /* server_request. */
392 #define TO_SERVER_REQUEST(base_ptr)					\
393 	((struct server_request*)					\
394 	  (((char*)(base_ptr) - evutil_offsetof(struct server_request, base))))
395 
396 #define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads])
397 
398 static struct nameserver *nameserver_pick(struct evdns_base *base);
399 static void evdns_request_insert(struct request *req, struct request **head);
400 static void evdns_request_remove(struct request *req, struct request **head);
401 static void nameserver_ready_callback(evutil_socket_t fd, short events, void *arg);
402 static int evdns_transmit(struct evdns_base *base);
403 static int evdns_request_transmit(struct request *req);
404 static void nameserver_send_probe(struct nameserver *const ns);
405 static void search_request_finished(struct evdns_request *const);
406 static int search_try_next(struct evdns_request *const req);
407 static struct request *search_request_new(struct evdns_base *base, struct evdns_request *handle, int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg);
408 static void evdns_requests_pump_waiting_queue(struct evdns_base *base);
409 static u16 transaction_id_pick(struct evdns_base *base);
410 static struct request *request_new(struct evdns_base *base, struct evdns_request *handle, int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
411 static void request_submit(struct request *const req);
412 
413 static int server_request_free(struct server_request *req);
414 static void server_request_free_answers(struct server_request *req);
415 static void server_port_free(struct evdns_server_port *port);
416 static void server_port_ready_callback(evutil_socket_t fd, short events, void *arg);
417 static int evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename);
418 static int evdns_base_set_option_impl(struct evdns_base *base,
419     const char *option, const char *val, int flags);
420 static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests);
421 static void evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg);
422 
423 static int strtoint(const char *const str);
424 
425 #ifdef EVENT__DISABLE_THREAD_SUPPORT
426 #define EVDNS_LOCK(base)  EVUTIL_NIL_STMT_
427 #define EVDNS_UNLOCK(base) EVUTIL_NIL_STMT_
428 #define ASSERT_LOCKED(base) EVUTIL_NIL_STMT_
429 #else
430 #define EVDNS_LOCK(base)			\
431 	EVLOCK_LOCK((base)->lock, 0)
432 #define EVDNS_UNLOCK(base)			\
433 	EVLOCK_UNLOCK((base)->lock, 0)
434 #define ASSERT_LOCKED(base)			\
435 	EVLOCK_ASSERT_LOCKED((base)->lock)
436 #endif
437 
438 static evdns_debug_log_fn_type evdns_log_fn = NULL;
439 
440 void
441 evdns_set_log_fn(evdns_debug_log_fn_type fn)
442 {
443 	evdns_log_fn = fn;
444 }
445 
446 #ifdef __GNUC__
447 #define EVDNS_LOG_CHECK	 __attribute__ ((format(printf, 2, 3)))
448 #else
449 #define EVDNS_LOG_CHECK
450 #endif
451 
452 static void evdns_log_(int severity, const char *fmt, ...) EVDNS_LOG_CHECK;
453 static void
454 evdns_log_(int severity, const char *fmt, ...)
455 {
456 	va_list args;
457 	va_start(args,fmt);
458 	if (evdns_log_fn) {
459 		char buf[512];
460 		int is_warn = (severity == EVDNS_LOG_WARN);
461 		evutil_vsnprintf(buf, sizeof(buf), fmt, args);
462 		evdns_log_fn(is_warn, buf);
463 	} else {
464 		event_logv_(severity, NULL, fmt, args);
465 	}
466 	va_end(args);
467 }
468 
469 #define log evdns_log_
470 
471 /* This walks the list of inflight requests to find the */
472 /* one with a matching transaction id. Returns NULL on */
473 /* failure */
474 static struct request *
475 request_find_from_trans_id(struct evdns_base *base, u16 trans_id) {
476 	struct request *req = REQ_HEAD(base, trans_id);
477 	struct request *const started_at = req;
478 
479 	ASSERT_LOCKED(base);
480 
481 	if (req) {
482 		do {
483 			if (req->trans_id == trans_id) return req;
484 			req = req->next;
485 		} while (req != started_at);
486 	}
487 
488 	return NULL;
489 }
490 
491 /* a libevent callback function which is called when a nameserver */
492 /* has gone down and we want to test if it has came back to life yet */
493 static void
494 nameserver_prod_callback(evutil_socket_t fd, short events, void *arg) {
495 	struct nameserver *const ns = (struct nameserver *) arg;
496 	(void)fd;
497 	(void)events;
498 
499 	EVDNS_LOCK(ns->base);
500 	nameserver_send_probe(ns);
501 	EVDNS_UNLOCK(ns->base);
502 }
503 
504 /* a libevent callback which is called when a nameserver probe (to see if */
505 /* it has come back to life) times out. We increment the count of failed_times */
506 /* and wait longer to send the next probe packet. */
507 static void
508 nameserver_probe_failed(struct nameserver *const ns) {
509 	struct timeval timeout;
510 	int i;
511 
512 	ASSERT_LOCKED(ns->base);
513 	(void) evtimer_del(&ns->timeout_event);
514 	if (ns->state == 1) {
515 		/* This can happen if the nameserver acts in a way which makes us mark */
516 		/* it as bad and then starts sending good replies. */
517 		return;
518 	}
519 
520 #define MAX_PROBE_TIMEOUT 3600
521 #define TIMEOUT_BACKOFF_FACTOR 3
522 
523 	memcpy(&timeout, &ns->base->global_nameserver_probe_initial_timeout,
524 	    sizeof(struct timeval));
525 	for (i=ns->failed_times; i > 0 && timeout.tv_sec < MAX_PROBE_TIMEOUT; --i) {
526 		timeout.tv_sec *= TIMEOUT_BACKOFF_FACTOR;
527 		timeout.tv_usec *= TIMEOUT_BACKOFF_FACTOR;
528 		if (timeout.tv_usec > 1000000) {
529 			timeout.tv_sec += timeout.tv_usec / 1000000;
530 			timeout.tv_usec %= 1000000;
531 		}
532 	}
533 	if (timeout.tv_sec > MAX_PROBE_TIMEOUT) {
534 		timeout.tv_sec = MAX_PROBE_TIMEOUT;
535 		timeout.tv_usec = 0;
536 	}
537 
538 	ns->failed_times++;
539 
540 	if (evtimer_add(&ns->timeout_event, &timeout) < 0) {
541 		char addrbuf[128];
542 		log(EVDNS_LOG_WARN,
543 		    "Error from libevent when adding timer event for %s",
544 		    evutil_format_sockaddr_port_(
545 			    (struct sockaddr *)&ns->address,
546 			    addrbuf, sizeof(addrbuf)));
547 	}
548 }
549 
550 static void
551 request_swap_ns(struct request *req, struct nameserver *ns) {
552 	if (ns && req->ns != ns) {
553 		EVUTIL_ASSERT(req->ns->requests_inflight > 0);
554 		req->ns->requests_inflight--;
555 		ns->requests_inflight++;
556 
557 		req->ns = ns;
558 	}
559 }
560 
561 /* called when a nameserver has been deemed to have failed. For example, too */
562 /* many packets have timed out etc */
563 static void
564 nameserver_failed(struct nameserver *const ns, const char *msg) {
565 	struct request *req, *started_at;
566 	struct evdns_base *base = ns->base;
567 	int i;
568 	char addrbuf[128];
569 
570 	ASSERT_LOCKED(base);
571 	/* if this nameserver has already been marked as failed */
572 	/* then don't do anything */
573 	if (!ns->state) return;
574 
575 	log(EVDNS_LOG_MSG, "Nameserver %s has failed: %s",
576 	    evutil_format_sockaddr_port_(
577 		    (struct sockaddr *)&ns->address,
578 		    addrbuf, sizeof(addrbuf)),
579 	    msg);
580 
581 	base->global_good_nameservers--;
582 	EVUTIL_ASSERT(base->global_good_nameservers >= 0);
583 	if (base->global_good_nameservers == 0) {
584 		log(EVDNS_LOG_MSG, "All nameservers have failed");
585 	}
586 
587 	ns->state = 0;
588 	ns->failed_times = 1;
589 
590 	if (evtimer_add(&ns->timeout_event,
591 		&base->global_nameserver_probe_initial_timeout) < 0) {
592 		log(EVDNS_LOG_WARN,
593 		    "Error from libevent when adding timer event for %s",
594 		    evutil_format_sockaddr_port_(
595 			    (struct sockaddr *)&ns->address,
596 			    addrbuf, sizeof(addrbuf)));
597 		/* ???? Do more? */
598 	}
599 
600 	/* walk the list of inflight requests to see if any can be reassigned to */
601 	/* a different server. Requests in the waiting queue don't have a */
602 	/* nameserver assigned yet */
603 
604 	/* if we don't have *any* good nameservers then there's no point */
605 	/* trying to reassign requests to one */
606 	if (!base->global_good_nameservers) return;
607 
608 	for (i = 0; i < base->n_req_heads; ++i) {
609 		req = started_at = base->req_heads[i];
610 		if (req) {
611 			do {
612 				if (req->tx_count == 0 && req->ns == ns) {
613 					/* still waiting to go out, can be moved */
614 					/* to another server */
615 					request_swap_ns(req, nameserver_pick(base));
616 				}
617 				req = req->next;
618 			} while (req != started_at);
619 		}
620 	}
621 }
622 
623 static void
624 nameserver_up(struct nameserver *const ns)
625 {
626 	char addrbuf[128];
627 	ASSERT_LOCKED(ns->base);
628 	if (ns->state) return;
629 	log(EVDNS_LOG_MSG, "Nameserver %s is back up",
630 	    evutil_format_sockaddr_port_(
631 		    (struct sockaddr *)&ns->address,
632 		    addrbuf, sizeof(addrbuf)));
633 	evtimer_del(&ns->timeout_event);
634 	if (ns->probe_request) {
635 		evdns_cancel_request(ns->base, ns->probe_request);
636 		ns->probe_request = NULL;
637 	}
638 	ns->state = 1;
639 	ns->failed_times = 0;
640 	ns->timedout = 0;
641 	ns->base->global_good_nameservers++;
642 }
643 
644 static void
645 request_trans_id_set(struct request *const req, const u16 trans_id) {
646 	req->trans_id = trans_id;
647 	*((u16 *) req->request) = htons(trans_id);
648 }
649 
650 /* Called to remove a request from a list and dealloc it. */
651 /* head is a pointer to the head of the list it should be */
652 /* removed from or NULL if the request isn't in a list. */
653 /* when free_handle is one, free the handle as well. */
654 static void
655 request_finished(struct request *const req, struct request **head, int free_handle) {
656 	struct evdns_base *base = req->base;
657 	int was_inflight = (head != &base->req_waiting_head);
658 	EVDNS_LOCK(base);
659 	ASSERT_VALID_REQUEST(req);
660 
661 	if (head)
662 		evdns_request_remove(req, head);
663 
664 	log(EVDNS_LOG_DEBUG, "Removing timeout for request %p", req);
665 	if (was_inflight) {
666 		evtimer_del(&req->timeout_event);
667 		base->global_requests_inflight--;
668 		req->ns->requests_inflight--;
669 	} else {
670 		base->global_requests_waiting--;
671 	}
672 	/* it was initialized during request_new / evtimer_assign */
673 	event_debug_unassign(&req->timeout_event);
674 
675 	if (req->ns &&
676 	    req->ns->requests_inflight == 0 &&
677 	    req->base->disable_when_inactive) {
678 		event_del(&req->ns->event);
679 		evtimer_del(&req->ns->timeout_event);
680 	}
681 
682 	if (!req->request_appended) {
683 		/* need to free the request data on it's own */
684 		mm_free(req->request);
685 	} else {
686 		/* the request data is appended onto the header */
687 		/* so everything gets free()ed when we: */
688 	}
689 
690 	if (req->handle) {
691 		EVUTIL_ASSERT(req->handle->current_req == req);
692 
693 		if (free_handle) {
694 			search_request_finished(req->handle);
695 			req->handle->current_req = NULL;
696 			if (! req->handle->pending_cb) {
697 				/* If we're planning to run the callback,
698 				 * don't free the handle until later. */
699 				mm_free(req->handle);
700 			}
701 			req->handle = NULL; /* If we have a bug, let's crash
702 					     * early */
703 		} else {
704 			req->handle->current_req = NULL;
705 		}
706 	}
707 
708 	mm_free(req);
709 
710 	evdns_requests_pump_waiting_queue(base);
711 	EVDNS_UNLOCK(base);
712 }
713 
714 /* This is called when a server returns a funny error code. */
715 /* We try the request again with another server. */
716 /* */
717 /* return: */
718 /*   0 ok */
719 /*   1 failed/reissue is pointless */
720 static int
721 request_reissue(struct request *req) {
722 	const struct nameserver *const last_ns = req->ns;
723 	ASSERT_LOCKED(req->base);
724 	ASSERT_VALID_REQUEST(req);
725 	/* the last nameserver should have been marked as failing */
726 	/* by the caller of this function, therefore pick will try */
727 	/* not to return it */
728 	request_swap_ns(req, nameserver_pick(req->base));
729 	if (req->ns == last_ns) {
730 		/* ... but pick did return it */
731 		/* not a lot of point in trying again with the */
732 		/* same server */
733 		return 1;
734 	}
735 
736 	req->reissue_count++;
737 	req->tx_count = 0;
738 	req->transmit_me = 1;
739 
740 	return 0;
741 }
742 
743 /* this function looks for space on the inflight queue and promotes */
744 /* requests from the waiting queue if it can. */
745 /* */
746 /* TODO: */
747 /* add return code, see at nameserver_pick() and other functions. */
748 static void
749 evdns_requests_pump_waiting_queue(struct evdns_base *base) {
750 	ASSERT_LOCKED(base);
751 	while (base->global_requests_inflight < base->global_max_requests_inflight &&
752 		   base->global_requests_waiting) {
753 		struct request *req;
754 
755 		EVUTIL_ASSERT(base->req_waiting_head);
756 		req = base->req_waiting_head;
757 
758 		req->ns = nameserver_pick(base);
759 		if (!req->ns)
760 			return;
761 
762 		/* move a request from the waiting queue to the inflight queue */
763 		req->ns->requests_inflight++;
764 
765 		evdns_request_remove(req, &base->req_waiting_head);
766 
767 		base->global_requests_waiting--;
768 		base->global_requests_inflight++;
769 
770 		request_trans_id_set(req, transaction_id_pick(base));
771 
772 		evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
773 		evdns_request_transmit(req);
774 		evdns_transmit(base);
775 	}
776 }
777 
778 /* TODO(nickm) document */
779 struct deferred_reply_callback {
780 	struct event_callback deferred;
781 	struct evdns_request *handle;
782 	u8 request_type;
783 	u8 have_reply;
784 	u32 ttl;
785 	u32 err;
786 	evdns_callback_type user_callback;
787 	struct reply reply;
788 };
789 
790 static void
791 reply_run_callback(struct event_callback *d, void *user_pointer)
792 {
793 	struct deferred_reply_callback *cb =
794 	    EVUTIL_UPCAST(d, struct deferred_reply_callback, deferred);
795 
796 	switch (cb->request_type) {
797 	case TYPE_A:
798 		if (cb->have_reply)
799 			cb->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
800 			    cb->reply.data.a.addrcount, cb->ttl,
801 			    cb->reply.data.a.addresses,
802 			    user_pointer);
803 		else
804 			cb->user_callback(cb->err, 0, 0, cb->ttl, NULL, user_pointer);
805 		break;
806 	case TYPE_PTR:
807 		if (cb->have_reply) {
808 			char *name = cb->reply.data.ptr.name;
809 			cb->user_callback(DNS_ERR_NONE, DNS_PTR, 1, cb->ttl,
810 			    &name, user_pointer);
811 		} else {
812 			cb->user_callback(cb->err, 0, 0, cb->ttl, NULL, user_pointer);
813 		}
814 		break;
815 	case TYPE_AAAA:
816 		if (cb->have_reply)
817 			cb->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
818 			    cb->reply.data.aaaa.addrcount, cb->ttl,
819 			    cb->reply.data.aaaa.addresses,
820 			    user_pointer);
821 		else
822 			cb->user_callback(cb->err, 0, 0, cb->ttl, NULL, user_pointer);
823 		break;
824 	default:
825 		EVUTIL_ASSERT(0);
826 	}
827 
828 	if (cb->handle && cb->handle->pending_cb) {
829 		mm_free(cb->handle);
830 	}
831 
832 	mm_free(cb);
833 }
834 
835 static void
836 reply_schedule_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply)
837 {
838 	struct deferred_reply_callback *d = mm_calloc(1, sizeof(*d));
839 
840 	if (!d) {
841 		event_warn("%s: Couldn't allocate space for deferred callback.",
842 		    __func__);
843 		return;
844 	}
845 
846 	ASSERT_LOCKED(req->base);
847 
848 	d->request_type = req->request_type;
849 	d->user_callback = req->user_callback;
850 	d->ttl = ttl;
851 	d->err = err;
852 	if (reply) {
853 		d->have_reply = 1;
854 		memcpy(&d->reply, reply, sizeof(struct reply));
855 	}
856 
857 	if (req->handle) {
858 		req->handle->pending_cb = 1;
859 		d->handle = req->handle;
860 	}
861 
862 	event_deferred_cb_init_(
863 	    &d->deferred,
864 	    event_get_priority(&req->timeout_event),
865 	    reply_run_callback,
866 	    req->user_pointer);
867 	event_deferred_cb_schedule_(
868 		req->base->event_base,
869 		&d->deferred);
870 }
871 
872 
873 #define _QR_MASK    0x8000U
874 #define _OP_MASK    0x7800U
875 #define _AA_MASK    0x0400U
876 #define _TC_MASK    0x0200U
877 #define _RD_MASK    0x0100U
878 #define _RA_MASK    0x0080U
879 #define _Z_MASK     0x0040U
880 #define _AD_MASK    0x0020U
881 #define _CD_MASK    0x0010U
882 #define _RCODE_MASK 0x000fU
883 #define _Z_MASK_DEPRECATED 0x0070U
884 
885 /* this processes a parsed reply packet */
886 static void
887 reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) {
888 	int error;
889 	char addrbuf[128];
890 	static const int error_codes[] = {
891 		DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST,
892 		DNS_ERR_NOTIMPL, DNS_ERR_REFUSED
893 	};
894 
895 	ASSERT_LOCKED(req->base);
896 	ASSERT_VALID_REQUEST(req);
897 
898 	if (flags & (_RCODE_MASK | _TC_MASK) || !reply || !reply->have_answer) {
899 		/* there was an error */
900 		if (flags & _TC_MASK) {
901 			error = DNS_ERR_TRUNCATED;
902 		} else if (flags & _RCODE_MASK) {
903 			u16 error_code = (flags & _RCODE_MASK) - 1;
904 			if (error_code > 4) {
905 				error = DNS_ERR_UNKNOWN;
906 			} else {
907 				error = error_codes[error_code];
908 			}
909 		} else if (reply && !reply->have_answer) {
910 			error = DNS_ERR_NODATA;
911 		} else {
912 			error = DNS_ERR_UNKNOWN;
913 		}
914 
915 		switch (error) {
916 		case DNS_ERR_NOTIMPL:
917 		case DNS_ERR_REFUSED:
918 			/* we regard these errors as marking a bad nameserver */
919 			if (req->reissue_count < req->base->global_max_reissues) {
920 				char msg[64];
921 				evutil_snprintf(msg, sizeof(msg), "Bad response %d (%s)",
922 					 error, evdns_err_to_string(error));
923 				nameserver_failed(req->ns, msg);
924 				if (!request_reissue(req)) return;
925 			}
926 			break;
927 		case DNS_ERR_SERVERFAILED:
928 			/* rcode 2 (servfailed) sometimes means "we
929 			 * are broken" and sometimes (with some binds)
930 			 * means "that request was very confusing."
931 			 * Treat this as a timeout, not a failure.
932 			 */
933 			log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver"
934 				"at %s; will allow the request to time out.",
935 			    evutil_format_sockaddr_port_(
936 				    (struct sockaddr *)&req->ns->address,
937 				    addrbuf, sizeof(addrbuf)));
938 			/* Call the timeout function */
939 			evdns_request_timeout_callback(0, 0, req);
940 			return;
941 		default:
942 			/* we got a good reply from the nameserver: it is up. */
943 			if (req->handle == req->ns->probe_request) {
944 				/* Avoid double-free */
945 				req->ns->probe_request = NULL;
946 			}
947 
948 			nameserver_up(req->ns);
949 		}
950 
951 		if (req->handle->search_state &&
952 		    req->request_type != TYPE_PTR) {
953 			/* if we have a list of domains to search in,
954 			 * try the next one */
955 			if (!search_try_next(req->handle)) {
956 				/* a new request was issued so this
957 				 * request is finished and */
958 				/* the user callback will be made when
959 				 * that request (or a */
960 				/* child of it) finishes. */
961 				return;
962 			}
963 		}
964 
965 		/* all else failed. Pass the failure up */
966 		reply_schedule_callback(req, ttl, error, NULL);
967 		request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
968 	} else {
969 		/* all ok, tell the user */
970 		reply_schedule_callback(req, ttl, 0, reply);
971 		if (req->handle == req->ns->probe_request)
972 			req->ns->probe_request = NULL; /* Avoid double-free */
973 		nameserver_up(req->ns);
974 		request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
975 	}
976 }
977 
978 static int
979 name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
980 	int name_end = -1;
981 	int j = *idx;
982 	int ptr_count = 0;
983 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&t32_, packet + j, 4); j += 4; x = ntohl(t32_); } while (0)
984 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&t_, packet + j, 2); j += 2; x = ntohs(t_); } while (0)
985 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
986 
987 	char *cp = name_out;
988 	const char *const end = name_out + name_out_len;
989 
990 	/* Normally, names are a series of length prefixed strings terminated */
991 	/* with a length of 0 (the lengths are u8's < 63). */
992 	/* However, the length can start with a pair of 1 bits and that */
993 	/* means that the next 14 bits are a pointer within the current */
994 	/* packet. */
995 
996 	for (;;) {
997 		u8 label_len;
998 		GET8(label_len);
999 		if (!label_len) break;
1000 		if (label_len & 0xc0) {
1001 			u8 ptr_low;
1002 			GET8(ptr_low);
1003 			if (name_end < 0) name_end = j;
1004 			j = (((int)label_len & 0x3f) << 8) + ptr_low;
1005 			/* Make sure that the target offset is in-bounds. */
1006 			if (j < 0 || j >= length) return -1;
1007 			/* If we've jumped more times than there are characters in the
1008 			 * message, we must have a loop. */
1009 			if (++ptr_count > length) return -1;
1010 			continue;
1011 		}
1012 		if (label_len > 63) return -1;
1013 		if (cp != name_out) {
1014 			if (cp + 1 >= end) return -1;
1015 			*cp++ = '.';
1016 		}
1017 		if (cp + label_len >= end) return -1;
1018 		if (j + label_len > length) return -1;
1019 		memcpy(cp, packet + j, label_len);
1020 		cp += label_len;
1021 		j += label_len;
1022 	}
1023 	if (cp >= end) return -1;
1024 	*cp = '\0';
1025 	if (name_end < 0)
1026 		*idx = j;
1027 	else
1028 		*idx = name_end;
1029 	return 0;
1030  err:
1031 	return -1;
1032 }
1033 
1034 /* parses a raw request from a nameserver */
1035 static int
1036 reply_parse(struct evdns_base *base, u8 *packet, int length) {
1037 	int j = 0, k = 0;  /* index into packet */
1038 	u16 t_;	 /* used by the macros */
1039 	u32 t32_;  /* used by the macros */
1040 	char tmp_name[256], cmp_name[256]; /* used by the macros */
1041 	int name_matches = 0;
1042 
1043 	u16 trans_id, questions, answers, authority, additional, datalength;
1044 	u16 flags = 0;
1045 	u32 ttl, ttl_r = 0xffffffff;
1046 	struct reply reply;
1047 	struct request *req = NULL;
1048 	unsigned int i;
1049 
1050 	ASSERT_LOCKED(base);
1051 
1052 	GET16(trans_id);
1053 	GET16(flags);
1054 	GET16(questions);
1055 	GET16(answers);
1056 	GET16(authority);
1057 	GET16(additional);
1058 	(void) authority; /* suppress "unused variable" warnings. */
1059 	(void) additional; /* suppress "unused variable" warnings. */
1060 
1061 	req = request_find_from_trans_id(base, trans_id);
1062 	if (!req) return -1;
1063 	EVUTIL_ASSERT(req->base == base);
1064 
1065 	memset(&reply, 0, sizeof(reply));
1066 
1067 	/* If it's not an answer, it doesn't correspond to any request. */
1068 	if (!(flags & _QR_MASK)) return -1;  /* must be an answer */
1069 	if ((flags & (_RCODE_MASK|_TC_MASK)) && (flags & (_RCODE_MASK|_TC_MASK)) != DNS_ERR_NOTEXIST) {
1070 		/* there was an error and it's not NXDOMAIN */
1071 		goto err;
1072 	}
1073 	/* if (!answers) return; */  /* must have an answer of some form */
1074 
1075 	/* This macro skips a name in the DNS reply. */
1076 #define SKIP_NAME						\
1077 	do { tmp_name[0] = '\0';				\
1078 		if (name_parse(packet, length, &j, tmp_name,	\
1079 			sizeof(tmp_name))<0)			\
1080 			goto err;				\
1081 	} while (0)
1082 
1083 	reply.type = req->request_type;
1084 
1085 	/* skip over each question in the reply */
1086 	for (i = 0; i < questions; ++i) {
1087 		/* the question looks like
1088 		 *   <label:name><u16:type><u16:class>
1089 		 */
1090 		tmp_name[0] = '\0';
1091 		cmp_name[0] = '\0';
1092 		k = j;
1093 		if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name)) < 0)
1094 			goto err;
1095 		if (name_parse(req->request, req->request_len, &k,
1096 			cmp_name, sizeof(cmp_name))<0)
1097 			goto err;
1098 		if (!base->global_randomize_case) {
1099 			if (strcmp(tmp_name, cmp_name) == 0)
1100 				name_matches = 1;
1101 		} else {
1102 			if (evutil_ascii_strcasecmp(tmp_name, cmp_name) == 0)
1103 				name_matches = 1;
1104 		}
1105 
1106 		j += 4;
1107 		if (j > length)
1108 			goto err;
1109 	}
1110 
1111 	if (!name_matches)
1112 		goto err;
1113 
1114 	/* now we have the answer section which looks like
1115 	 * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
1116 	 */
1117 
1118 	for (i = 0; i < answers; ++i) {
1119 		u16 type, class;
1120 
1121 		SKIP_NAME;
1122 		GET16(type);
1123 		GET16(class);
1124 		GET32(ttl);
1125 		GET16(datalength);
1126 
1127 		if (type == TYPE_A && class == CLASS_INET) {
1128 			int addrcount, addrtocopy;
1129 			if (req->request_type != TYPE_A) {
1130 				j += datalength; continue;
1131 			}
1132 			if ((datalength & 3) != 0) /* not an even number of As. */
1133 			    goto err;
1134 			addrcount = datalength >> 2;
1135 			addrtocopy = MIN(MAX_V4_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
1136 
1137 			ttl_r = MIN(ttl_r, ttl);
1138 			/* we only bother with the first four addresses. */
1139 			if (j + 4*addrtocopy > length) goto err;
1140 			memcpy(&reply.data.a.addresses[reply.data.a.addrcount],
1141 				   packet + j, 4*addrtocopy);
1142 			j += 4*addrtocopy;
1143 			reply.data.a.addrcount += addrtocopy;
1144 			reply.have_answer = 1;
1145 			if (reply.data.a.addrcount == MAX_V4_ADDRS) break;
1146 		} else if (type == TYPE_PTR && class == CLASS_INET) {
1147 			if (req->request_type != TYPE_PTR) {
1148 				j += datalength; continue;
1149 			}
1150 			if (name_parse(packet, length, &j, reply.data.ptr.name,
1151 						   sizeof(reply.data.ptr.name))<0)
1152 				goto err;
1153 			ttl_r = MIN(ttl_r, ttl);
1154 			reply.have_answer = 1;
1155 			break;
1156 		} else if (type == TYPE_CNAME) {
1157 			char cname[HOST_NAME_MAX];
1158 			if (!req->put_cname_in_ptr || *req->put_cname_in_ptr) {
1159 				j += datalength; continue;
1160 			}
1161 			if (name_parse(packet, length, &j, cname,
1162 				sizeof(cname))<0)
1163 				goto err;
1164 			*req->put_cname_in_ptr = mm_strdup(cname);
1165 		} else if (type == TYPE_AAAA && class == CLASS_INET) {
1166 			int addrcount, addrtocopy;
1167 			if (req->request_type != TYPE_AAAA) {
1168 				j += datalength; continue;
1169 			}
1170 			if ((datalength & 15) != 0) /* not an even number of AAAAs. */
1171 				goto err;
1172 			addrcount = datalength >> 4;  /* each address is 16 bytes long */
1173 			addrtocopy = MIN(MAX_V6_ADDRS - reply.data.aaaa.addrcount, (unsigned)addrcount);
1174 			ttl_r = MIN(ttl_r, ttl);
1175 
1176 			/* we only bother with the first four addresses. */
1177 			if (j + 16*addrtocopy > length) goto err;
1178 			memcpy(&reply.data.aaaa.addresses[reply.data.aaaa.addrcount],
1179 				   packet + j, 16*addrtocopy);
1180 			reply.data.aaaa.addrcount += addrtocopy;
1181 			j += 16*addrtocopy;
1182 			reply.have_answer = 1;
1183 			if (reply.data.aaaa.addrcount == MAX_V6_ADDRS) break;
1184 		} else {
1185 			/* skip over any other type of resource */
1186 			j += datalength;
1187 		}
1188 	}
1189 
1190 	if (!reply.have_answer) {
1191 		for (i = 0; i < authority; ++i) {
1192 			u16 type, class;
1193 			SKIP_NAME;
1194 			GET16(type);
1195 			GET16(class);
1196 			GET32(ttl);
1197 			GET16(datalength);
1198 			if (type == TYPE_SOA && class == CLASS_INET) {
1199 				u32 serial, refresh, retry, expire, minimum;
1200 				SKIP_NAME;
1201 				SKIP_NAME;
1202 				GET32(serial);
1203 				GET32(refresh);
1204 				GET32(retry);
1205 				GET32(expire);
1206 				GET32(minimum);
1207 				(void)expire;
1208 				(void)retry;
1209 				(void)refresh;
1210 				(void)serial;
1211 				ttl_r = MIN(ttl_r, ttl);
1212 				ttl_r = MIN(ttl_r, minimum);
1213 			} else {
1214 				/* skip over any other type of resource */
1215 				j += datalength;
1216 			}
1217 		}
1218 	}
1219 
1220 	if (ttl_r == 0xffffffff)
1221 		ttl_r = 0;
1222 
1223 	reply_handle(req, flags, ttl_r, &reply);
1224 	return 0;
1225  err:
1226 	if (req)
1227 		reply_handle(req, flags, 0, NULL);
1228 	return -1;
1229 }
1230 
1231 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
1232 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
1233 /* callback. */
1234 static int
1235 request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, ev_socklen_t addrlen)
1236 {
1237 	int j = 0;	/* index into packet */
1238 	u16 t_;	 /* used by the macros */
1239 	char tmp_name[256]; /* used by the macros */
1240 
1241 	int i;
1242 	u16 trans_id, flags, questions, answers, authority, additional;
1243 	struct server_request *server_req = NULL;
1244 
1245 	ASSERT_LOCKED(port);
1246 
1247 	/* Get the header fields */
1248 	GET16(trans_id);
1249 	GET16(flags);
1250 	GET16(questions);
1251 	GET16(answers);
1252 	GET16(authority);
1253 	GET16(additional);
1254 	(void)answers;
1255 	(void)additional;
1256 	(void)authority;
1257 
1258 	if (flags & _QR_MASK) return -1; /* Must not be an answer. */
1259 	flags &= (_RD_MASK|_CD_MASK); /* Only RD and CD get preserved. */
1260 
1261 	server_req = mm_malloc(sizeof(struct server_request));
1262 	if (server_req == NULL) return -1;
1263 	memset(server_req, 0, sizeof(struct server_request));
1264 
1265 	server_req->trans_id = trans_id;
1266 	memcpy(&server_req->addr, addr, addrlen);
1267 	server_req->addrlen = addrlen;
1268 
1269 	server_req->base.flags = flags;
1270 	server_req->base.nquestions = 0;
1271 	server_req->base.questions = mm_calloc(sizeof(struct evdns_server_question *), questions);
1272 	if (server_req->base.questions == NULL)
1273 		goto err;
1274 
1275 	for (i = 0; i < questions; ++i) {
1276 		u16 type, class;
1277 		struct evdns_server_question *q;
1278 		int namelen;
1279 		if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
1280 			goto err;
1281 		GET16(type);
1282 		GET16(class);
1283 		namelen = (int)strlen(tmp_name);
1284 		q = mm_malloc(sizeof(struct evdns_server_question) + namelen);
1285 		if (!q)
1286 			goto err;
1287 		q->type = type;
1288 		q->dns_question_class = class;
1289 		memcpy(q->name, tmp_name, namelen+1);
1290 		server_req->base.questions[server_req->base.nquestions++] = q;
1291 	}
1292 
1293 	/* Ignore answers, authority, and additional. */
1294 
1295 	server_req->port = port;
1296 	port->refcnt++;
1297 
1298 	/* Only standard queries are supported. */
1299 	if (flags & _OP_MASK) {
1300 		evdns_server_request_respond(&(server_req->base), DNS_ERR_NOTIMPL);
1301 		return -1;
1302 	}
1303 
1304 	port->user_callback(&(server_req->base), port->user_data);
1305 
1306 	return 0;
1307 err:
1308 	if (server_req->base.questions) {
1309 		for (i = 0; i < server_req->base.nquestions; ++i)
1310 			mm_free(server_req->base.questions[i]);
1311 		mm_free(server_req->base.questions);
1312 	}
1313 	mm_free(server_req);
1314 	return -1;
1315 
1316 #undef SKIP_NAME
1317 #undef GET32
1318 #undef GET16
1319 #undef GET8
1320 }
1321 
1322 
1323 void
1324 evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void))
1325 {
1326 }
1327 
1328 void
1329 evdns_set_random_bytes_fn(void (*fn)(char *, size_t))
1330 {
1331 }
1332 
1333 /* Try to choose a strong transaction id which isn't already in flight */
1334 static u16
1335 transaction_id_pick(struct evdns_base *base) {
1336 	ASSERT_LOCKED(base);
1337 	for (;;) {
1338 		u16 trans_id;
1339 		evutil_secure_rng_get_bytes(&trans_id, sizeof(trans_id));
1340 
1341 		if (trans_id == 0xffff) continue;
1342 		/* now check to see if that id is already inflight */
1343 		if (request_find_from_trans_id(base, trans_id) == NULL)
1344 			return trans_id;
1345 	}
1346 }
1347 
1348 /* choose a namesever to use. This function will try to ignore */
1349 /* nameservers which we think are down and load balance across the rest */
1350 /* by updating the server_head global each time. */
1351 static struct nameserver *
1352 nameserver_pick(struct evdns_base *base) {
1353 	struct nameserver *started_at = base->server_head, *picked;
1354 	ASSERT_LOCKED(base);
1355 	if (!base->server_head) return NULL;
1356 
1357 	/* if we don't have any good nameservers then there's no */
1358 	/* point in trying to find one. */
1359 	if (!base->global_good_nameservers) {
1360 		base->server_head = base->server_head->next;
1361 		return base->server_head;
1362 	}
1363 
1364 	/* remember that nameservers are in a circular list */
1365 	for (;;) {
1366 		if (base->server_head->state) {
1367 			/* we think this server is currently good */
1368 			picked = base->server_head;
1369 			base->server_head = base->server_head->next;
1370 			return picked;
1371 		}
1372 
1373 		base->server_head = base->server_head->next;
1374 		if (base->server_head == started_at) {
1375 			/* all the nameservers seem to be down */
1376 			/* so we just return this one and hope for the */
1377 			/* best */
1378 			EVUTIL_ASSERT(base->global_good_nameservers == 0);
1379 			picked = base->server_head;
1380 			base->server_head = base->server_head->next;
1381 			return picked;
1382 		}
1383 	}
1384 }
1385 
1386 /* this is called when a namesever socket is ready for reading */
1387 static void
1388 nameserver_read(struct nameserver *ns) {
1389 	struct sockaddr_storage ss;
1390 	ev_socklen_t addrlen = sizeof(ss);
1391 	u8 packet[1500];
1392 	char addrbuf[128];
1393 	ASSERT_LOCKED(ns->base);
1394 
1395 	for (;;) {
1396 		const int r = recvfrom(ns->socket, (void*)packet,
1397 		    sizeof(packet), 0,
1398 		    (struct sockaddr*)&ss, &addrlen);
1399 		if (r < 0) {
1400 			int err = evutil_socket_geterror(ns->socket);
1401 			if (EVUTIL_ERR_RW_RETRIABLE(err))
1402 				return;
1403 			nameserver_failed(ns,
1404 			    evutil_socket_error_to_string(err));
1405 			return;
1406 		}
1407 		if (evutil_sockaddr_cmp((struct sockaddr*)&ss,
1408 			(struct sockaddr*)&ns->address, 0)) {
1409 			log(EVDNS_LOG_WARN, "Address mismatch on received "
1410 			    "DNS packet.  Apparent source was %s",
1411 			    evutil_format_sockaddr_port_(
1412 				    (struct sockaddr *)&ss,
1413 				    addrbuf, sizeof(addrbuf)));
1414 			return;
1415 		}
1416 
1417 		ns->timedout = 0;
1418 		reply_parse(ns->base, packet, r);
1419 	}
1420 }
1421 
1422 /* Read a packet from a DNS client on a server port s, parse it, and */
1423 /* act accordingly. */
1424 static void
1425 server_port_read(struct evdns_server_port *s) {
1426 	u8 packet[1500];
1427 	struct sockaddr_storage addr;
1428 	ev_socklen_t addrlen;
1429 	int r;
1430 	ASSERT_LOCKED(s);
1431 
1432 	for (;;) {
1433 		addrlen = sizeof(struct sockaddr_storage);
1434 		r = recvfrom(s->socket, (void*)packet, sizeof(packet), 0,
1435 					 (struct sockaddr*) &addr, &addrlen);
1436 		if (r < 0) {
1437 			int err = evutil_socket_geterror(s->socket);
1438 			if (EVUTIL_ERR_RW_RETRIABLE(err))
1439 				return;
1440 			log(EVDNS_LOG_WARN,
1441 			    "Error %s (%d) while reading request.",
1442 			    evutil_socket_error_to_string(err), err);
1443 			return;
1444 		}
1445 		request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);
1446 	}
1447 }
1448 
1449 /* Try to write all pending replies on a given DNS server port. */
1450 static void
1451 server_port_flush(struct evdns_server_port *port)
1452 {
1453 	struct server_request *req = port->pending_replies;
1454 	ASSERT_LOCKED(port);
1455 	while (req) {
1456 		int r = sendto(port->socket, req->response, (int)req->response_len, 0,
1457 			   (struct sockaddr*) &req->addr, (ev_socklen_t)req->addrlen);
1458 		if (r < 0) {
1459 			int err = evutil_socket_geterror(port->socket);
1460 			if (EVUTIL_ERR_RW_RETRIABLE(err))
1461 				return;
1462 			log(EVDNS_LOG_WARN, "Error %s (%d) while writing response to port; dropping", evutil_socket_error_to_string(err), err);
1463 		}
1464 		if (server_request_free(req)) {
1465 			/* we released the last reference to req->port. */
1466 			return;
1467 		} else {
1468 			EVUTIL_ASSERT(req != port->pending_replies);
1469 			req = port->pending_replies;
1470 		}
1471 	}
1472 
1473 	/* We have no more pending requests; stop listening for 'writeable' events. */
1474 	(void) event_del(&port->event);
1475 	event_assign(&port->event, port->event_base,
1476 				 port->socket, EV_READ | EV_PERSIST,
1477 				 server_port_ready_callback, port);
1478 
1479 	if (event_add(&port->event, NULL) < 0) {
1480 		log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server.");
1481 		/* ???? Do more? */
1482 	}
1483 }
1484 
1485 /* set if we are waiting for the ability to write to this server. */
1486 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1487 /* we stop these events. */
1488 static void
1489 nameserver_write_waiting(struct nameserver *ns, char waiting) {
1490 	ASSERT_LOCKED(ns->base);
1491 	if (ns->write_waiting == waiting) return;
1492 
1493 	ns->write_waiting = waiting;
1494 	(void) event_del(&ns->event);
1495 	event_assign(&ns->event, ns->base->event_base,
1496 	    ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
1497 	    nameserver_ready_callback, ns);
1498 	if (event_add(&ns->event, NULL) < 0) {
1499 		char addrbuf[128];
1500 		log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
1501 		    evutil_format_sockaddr_port_(
1502 			    (struct sockaddr *)&ns->address,
1503 			    addrbuf, sizeof(addrbuf)));
1504 		/* ???? Do more? */
1505 	}
1506 }
1507 
1508 /* a callback function. Called by libevent when the kernel says that */
1509 /* a nameserver socket is ready for writing or reading */
1510 static void
1511 nameserver_ready_callback(evutil_socket_t fd, short events, void *arg) {
1512 	struct nameserver *ns = (struct nameserver *) arg;
1513 	(void)fd;
1514 
1515 	EVDNS_LOCK(ns->base);
1516 	if (events & EV_WRITE) {
1517 		ns->choked = 0;
1518 		if (!evdns_transmit(ns->base)) {
1519 			nameserver_write_waiting(ns, 0);
1520 		}
1521 	}
1522 	if (events & EV_READ) {
1523 		nameserver_read(ns);
1524 	}
1525 	EVDNS_UNLOCK(ns->base);
1526 }
1527 
1528 /* a callback function. Called by libevent when the kernel says that */
1529 /* a server socket is ready for writing or reading. */
1530 static void
1531 server_port_ready_callback(evutil_socket_t fd, short events, void *arg) {
1532 	struct evdns_server_port *port = (struct evdns_server_port *) arg;
1533 	(void) fd;
1534 
1535 	EVDNS_LOCK(port);
1536 	if (events & EV_WRITE) {
1537 		port->choked = 0;
1538 		server_port_flush(port);
1539 	}
1540 	if (events & EV_READ) {
1541 		server_port_read(port);
1542 	}
1543 	EVDNS_UNLOCK(port);
1544 }
1545 
1546 /* This is an inefficient representation; only use it via the dnslabel_table_*
1547  * functions, so that is can be safely replaced with something smarter later. */
1548 #define MAX_LABELS 128
1549 /* Structures used to implement name compression */
1550 struct dnslabel_entry { char *v; off_t pos; };
1551 struct dnslabel_table {
1552 	int n_labels; /* number of current entries */
1553 	/* map from name to position in message */
1554 	struct dnslabel_entry labels[MAX_LABELS];
1555 };
1556 
1557 /* Initialize dnslabel_table. */
1558 static void
1559 dnslabel_table_init(struct dnslabel_table *table)
1560 {
1561 	table->n_labels = 0;
1562 }
1563 
1564 /* Free all storage held by table, but not the table itself. */
1565 static void
1566 dnslabel_clear(struct dnslabel_table *table)
1567 {
1568 	int i;
1569 	for (i = 0; i < table->n_labels; ++i)
1570 		mm_free(table->labels[i].v);
1571 	table->n_labels = 0;
1572 }
1573 
1574 /* return the position of the label in the current message, or -1 if the label */
1575 /* hasn't been used yet. */
1576 static int
1577 dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
1578 {
1579 	int i;
1580 	for (i = 0; i < table->n_labels; ++i) {
1581 		if (!strcmp(label, table->labels[i].v))
1582 			return table->labels[i].pos;
1583 	}
1584 	return -1;
1585 }
1586 
1587 /* remember that we've used the label at position pos */
1588 static int
1589 dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
1590 {
1591 	char *v;
1592 	int p;
1593 	if (table->n_labels == MAX_LABELS)
1594 		return (-1);
1595 	v = mm_strdup(label);
1596 	if (v == NULL)
1597 		return (-1);
1598 	p = table->n_labels++;
1599 	table->labels[p].v = v;
1600 	table->labels[p].pos = pos;
1601 
1602 	return (0);
1603 }
1604 
1605 /* Converts a string to a length-prefixed set of DNS labels, starting */
1606 /* at buf[j]. name and buf must not overlap. name_len should be the length */
1607 /* of name.	 table is optional, and is used for compression. */
1608 /* */
1609 /* Input: abc.def */
1610 /* Output: <3>abc<3>def<0> */
1611 /* */
1612 /* Returns the first index after the encoded name, or negative on error. */
1613 /*	 -1	 label was > 63 bytes */
1614 /*	 -2	 name too long to fit in buffer. */
1615 /* */
1616 static off_t
1617 dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
1618 				  const char *name, const size_t name_len,
1619 				  struct dnslabel_table *table) {
1620 	const char *end = name + name_len;
1621 	int ref = 0;
1622 	u16 t_;
1623 
1624 #define APPEND16(x) do {						\
1625 		if (j + 2 > (off_t)buf_len)				\
1626 			goto overflow;					\
1627 		t_ = htons(x);						\
1628 		memcpy(buf + j, &t_, 2);				\
1629 		j += 2;							\
1630 	} while (0)
1631 #define APPEND32(x) do {						\
1632 		if (j + 4 > (off_t)buf_len)				\
1633 			goto overflow;					\
1634 		t32_ = htonl(x);					\
1635 		memcpy(buf + j, &t32_, 4);				\
1636 		j += 4;							\
1637 	} while (0)
1638 
1639 	if (name_len > 255) return -2;
1640 
1641 	for (;;) {
1642 		const char *const start = name;
1643 		if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
1644 			APPEND16(ref | 0xc000);
1645 			return j;
1646 		}
1647 		name = strchr(name, '.');
1648 		if (!name) {
1649 			const size_t label_len = end - start;
1650 			if (label_len > 63) return -1;
1651 			if ((size_t)(j+label_len+1) > buf_len) return -2;
1652 			if (table) dnslabel_table_add(table, start, j);
1653 			buf[j++] = (ev_uint8_t)label_len;
1654 
1655 			memcpy(buf + j, start, label_len);
1656 			j += (int) label_len;
1657 			break;
1658 		} else {
1659 			/* append length of the label. */
1660 			const size_t label_len = name - start;
1661 			if (label_len > 63) return -1;
1662 			if ((size_t)(j+label_len+1) > buf_len) return -2;
1663 			if (table) dnslabel_table_add(table, start, j);
1664 			buf[j++] = (ev_uint8_t)label_len;
1665 
1666 			memcpy(buf + j, start, label_len);
1667 			j += (int) label_len;
1668 			/* hop over the '.' */
1669 			name++;
1670 		}
1671 	}
1672 
1673 	/* the labels must be terminated by a 0. */
1674 	/* It's possible that the name ended in a . */
1675 	/* in which case the zero is already there */
1676 	if (!j || buf[j-1]) buf[j++] = 0;
1677 	return j;
1678  overflow:
1679 	return (-2);
1680 }
1681 
1682 /* Finds the length of a dns request for a DNS name of the given */
1683 /* length. The actual request may be smaller than the value returned */
1684 /* here */
1685 static size_t
1686 evdns_request_len(const size_t name_len) {
1687 	return 96 + /* length of the DNS standard header */
1688 		name_len + 2 +
1689 		4;  /* space for the resource type */
1690 }
1691 
1692 /* build a dns request packet into buf. buf should be at least as long */
1693 /* as evdns_request_len told you it should be. */
1694 /* */
1695 /* Returns the amount of space used. Negative on error. */
1696 static int
1697 evdns_request_data_build(const char *const name, const size_t name_len,
1698     const u16 trans_id, const u16 type, const u16 class,
1699     u8 *const buf, size_t buf_len) {
1700 	off_t j = 0;  /* current offset into buf */
1701 	u16 t_;	 /* used by the macros */
1702 
1703 	APPEND16(trans_id);
1704 	APPEND16(0x0100);  /* standard query, recusion needed */
1705 	APPEND16(1);  /* one question */
1706 	APPEND16(0);  /* no answers */
1707 	APPEND16(0);  /* no authority */
1708 	APPEND16(0);  /* no additional */
1709 
1710 	j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
1711 	if (j < 0) {
1712 		return (int)j;
1713 	}
1714 
1715 	APPEND16(type);
1716 	APPEND16(class);
1717 
1718 	return (int)j;
1719  overflow:
1720 	return (-1);
1721 }
1722 
1723 /* exported function */
1724 struct evdns_server_port *
1725 evdns_add_server_port_with_base(struct event_base *base, evutil_socket_t socket, int flags, evdns_request_callback_fn_type cb, void *user_data)
1726 {
1727 	struct evdns_server_port *port;
1728 	if (flags)
1729 		return NULL; /* flags not yet implemented */
1730 	if (!(port = mm_malloc(sizeof(struct evdns_server_port))))
1731 		return NULL;
1732 	memset(port, 0, sizeof(struct evdns_server_port));
1733 
1734 
1735 	port->socket = socket;
1736 	port->refcnt = 1;
1737 	port->choked = 0;
1738 	port->closing = 0;
1739 	port->user_callback = cb;
1740 	port->user_data = user_data;
1741 	port->pending_replies = NULL;
1742 	port->event_base = base;
1743 
1744 	event_assign(&port->event, port->event_base,
1745 				 port->socket, EV_READ | EV_PERSIST,
1746 				 server_port_ready_callback, port);
1747 	if (event_add(&port->event, NULL) < 0) {
1748 		mm_free(port);
1749 		return NULL;
1750 	}
1751 	EVTHREAD_ALLOC_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
1752 	return port;
1753 }
1754 
1755 struct evdns_server_port *
1756 evdns_add_server_port(evutil_socket_t socket, int flags, evdns_request_callback_fn_type cb, void *user_data)
1757 {
1758 	return evdns_add_server_port_with_base(NULL, socket, flags, cb, user_data);
1759 }
1760 
1761 /* exported function */
1762 void
1763 evdns_close_server_port(struct evdns_server_port *port)
1764 {
1765 	EVDNS_LOCK(port);
1766 	if (--port->refcnt == 0) {
1767 		EVDNS_UNLOCK(port);
1768 		server_port_free(port);
1769 	} else {
1770 		port->closing = 1;
1771 		EVDNS_UNLOCK(port);
1772 	}
1773 }
1774 
1775 /* exported function */
1776 int
1777 evdns_server_request_add_reply(struct evdns_server_request *req_, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
1778 {
1779 	struct server_request *req = TO_SERVER_REQUEST(req_);
1780 	struct server_reply_item **itemp, *item;
1781 	int *countp;
1782 	int result = -1;
1783 
1784 	EVDNS_LOCK(req->port);
1785 	if (req->response) /* have we already answered? */
1786 		goto done;
1787 
1788 	switch (section) {
1789 	case EVDNS_ANSWER_SECTION:
1790 		itemp = &req->answer;
1791 		countp = &req->n_answer;
1792 		break;
1793 	case EVDNS_AUTHORITY_SECTION:
1794 		itemp = &req->authority;
1795 		countp = &req->n_authority;
1796 		break;
1797 	case EVDNS_ADDITIONAL_SECTION:
1798 		itemp = &req->additional;
1799 		countp = &req->n_additional;
1800 		break;
1801 	default:
1802 		goto done;
1803 	}
1804 	while (*itemp) {
1805 		itemp = &((*itemp)->next);
1806 	}
1807 	item = mm_malloc(sizeof(struct server_reply_item));
1808 	if (!item)
1809 		goto done;
1810 	item->next = NULL;
1811 	if (!(item->name = mm_strdup(name))) {
1812 		mm_free(item);
1813 		goto done;
1814 	}
1815 	item->type = type;
1816 	item->dns_question_class = class;
1817 	item->ttl = ttl;
1818 	item->is_name = is_name != 0;
1819 	item->datalen = 0;
1820 	item->data = NULL;
1821 	if (data) {
1822 		if (item->is_name) {
1823 			if (!(item->data = mm_strdup(data))) {
1824 				mm_free(item->name);
1825 				mm_free(item);
1826 				goto done;
1827 			}
1828 			item->datalen = (u16)-1;
1829 		} else {
1830 			if (!(item->data = mm_malloc(datalen))) {
1831 				mm_free(item->name);
1832 				mm_free(item);
1833 				goto done;
1834 			}
1835 			item->datalen = datalen;
1836 			memcpy(item->data, data, datalen);
1837 		}
1838 	}
1839 
1840 	*itemp = item;
1841 	++(*countp);
1842 	result = 0;
1843 done:
1844 	EVDNS_UNLOCK(req->port);
1845 	return result;
1846 }
1847 
1848 /* exported function */
1849 int
1850 evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl)
1851 {
1852 	return evdns_server_request_add_reply(
1853 		  req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1854 		  ttl, n*4, 0, addrs);
1855 }
1856 
1857 /* exported function */
1858 int
1859 evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl)
1860 {
1861 	return evdns_server_request_add_reply(
1862 		  req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
1863 		  ttl, n*16, 0, addrs);
1864 }
1865 
1866 /* exported function */
1867 int
1868 evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
1869 {
1870 	u32 a;
1871 	char buf[32];
1872 	if (in && inaddr_name)
1873 		return -1;
1874 	else if (!in && !inaddr_name)
1875 		return -1;
1876 	if (in) {
1877 		a = ntohl(in->s_addr);
1878 		evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
1879 				(int)(u8)((a	)&0xff),
1880 				(int)(u8)((a>>8 )&0xff),
1881 				(int)(u8)((a>>16)&0xff),
1882 				(int)(u8)((a>>24)&0xff));
1883 		inaddr_name = buf;
1884 	}
1885 	return evdns_server_request_add_reply(
1886 		  req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
1887 		  ttl, -1, 1, hostname);
1888 }
1889 
1890 /* exported function */
1891 int
1892 evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
1893 {
1894 	return evdns_server_request_add_reply(
1895 		  req, EVDNS_ANSWER_SECTION, name, TYPE_CNAME, CLASS_INET,
1896 		  ttl, -1, 1, cname);
1897 }
1898 
1899 /* exported function */
1900 void
1901 evdns_server_request_set_flags(struct evdns_server_request *exreq, int flags)
1902 {
1903 	struct server_request *req = TO_SERVER_REQUEST(exreq);
1904 	req->base.flags &= ~(EVDNS_FLAGS_AA|EVDNS_FLAGS_RD);
1905 	req->base.flags |= flags;
1906 }
1907 
1908 static int
1909 evdns_server_request_format_response(struct server_request *req, int err)
1910 {
1911 	unsigned char buf[1500];
1912 	size_t buf_len = sizeof(buf);
1913 	off_t j = 0, r;
1914 	u16 t_;
1915 	u32 t32_;
1916 	int i;
1917 	u16 flags;
1918 	struct dnslabel_table table;
1919 
1920 	if (err < 0 || err > 15) return -1;
1921 
1922 	/* Set response bit and error code; copy OPCODE and RD fields from
1923 	 * question; copy RA and AA if set by caller. */
1924 	flags = req->base.flags;
1925 	flags |= (_QR_MASK | err);
1926 
1927 	dnslabel_table_init(&table);
1928 	APPEND16(req->trans_id);
1929 	APPEND16(flags);
1930 	APPEND16(req->base.nquestions);
1931 	APPEND16(req->n_answer);
1932 	APPEND16(req->n_authority);
1933 	APPEND16(req->n_additional);
1934 
1935 	/* Add questions. */
1936 	for (i=0; i < req->base.nquestions; ++i) {
1937 		const char *s = req->base.questions[i]->name;
1938 		j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
1939 		if (j < 0) {
1940 			dnslabel_clear(&table);
1941 			return (int) j;
1942 		}
1943 		APPEND16(req->base.questions[i]->type);
1944 		APPEND16(req->base.questions[i]->dns_question_class);
1945 	}
1946 
1947 	/* Add answer, authority, and additional sections. */
1948 	for (i=0; i<3; ++i) {
1949 		struct server_reply_item *item;
1950 		if (i==0)
1951 			item = req->answer;
1952 		else if (i==1)
1953 			item = req->authority;
1954 		else
1955 			item = req->additional;
1956 		while (item) {
1957 			r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
1958 			if (r < 0)
1959 				goto overflow;
1960 			j = r;
1961 
1962 			APPEND16(item->type);
1963 			APPEND16(item->dns_question_class);
1964 			APPEND32(item->ttl);
1965 			if (item->is_name) {
1966 				off_t len_idx = j, name_start;
1967 				j += 2;
1968 				name_start = j;
1969 				r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
1970 				if (r < 0)
1971 					goto overflow;
1972 				j = r;
1973 				t_ = htons( (short) (j-name_start) );
1974 				memcpy(buf+len_idx, &t_, 2);
1975 			} else {
1976 				APPEND16(item->datalen);
1977 				if (j+item->datalen > (off_t)buf_len)
1978 					goto overflow;
1979 				memcpy(buf+j, item->data, item->datalen);
1980 				j += item->datalen;
1981 			}
1982 			item = item->next;
1983 		}
1984 	}
1985 
1986 	if (j > 512) {
1987 overflow:
1988 		j = 512;
1989 		buf[2] |= 0x02; /* set the truncated bit. */
1990 	}
1991 
1992 	req->response_len = j;
1993 
1994 	if (!(req->response = mm_malloc(req->response_len))) {
1995 		server_request_free_answers(req);
1996 		dnslabel_clear(&table);
1997 		return (-1);
1998 	}
1999 	memcpy(req->response, buf, req->response_len);
2000 	server_request_free_answers(req);
2001 	dnslabel_clear(&table);
2002 	return (0);
2003 }
2004 
2005 /* exported function */
2006 int
2007 evdns_server_request_respond(struct evdns_server_request *req_, int err)
2008 {
2009 	struct server_request *req = TO_SERVER_REQUEST(req_);
2010 	struct evdns_server_port *port = req->port;
2011 	int r = -1;
2012 
2013 	EVDNS_LOCK(port);
2014 	if (!req->response) {
2015 		if ((r = evdns_server_request_format_response(req, err))<0)
2016 			goto done;
2017 	}
2018 
2019 	r = sendto(port->socket, req->response, (int)req->response_len, 0,
2020 			   (struct sockaddr*) &req->addr, (ev_socklen_t)req->addrlen);
2021 	if (r<0) {
2022 		int sock_err = evutil_socket_geterror(port->socket);
2023 		if (EVUTIL_ERR_RW_RETRIABLE(sock_err))
2024 			goto done;
2025 
2026 		if (port->pending_replies) {
2027 			req->prev_pending = port->pending_replies->prev_pending;
2028 			req->next_pending = port->pending_replies;
2029 			req->prev_pending->next_pending =
2030 				req->next_pending->prev_pending = req;
2031 		} else {
2032 			req->prev_pending = req->next_pending = req;
2033 			port->pending_replies = req;
2034 			port->choked = 1;
2035 
2036 			(void) event_del(&port->event);
2037 			event_assign(&port->event, port->event_base, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
2038 
2039 			if (event_add(&port->event, NULL) < 0) {
2040 				log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server");
2041 			}
2042 
2043 		}
2044 
2045 		r = 1;
2046 		goto done;
2047 	}
2048 	if (server_request_free(req)) {
2049 		r = 0;
2050 		goto done;
2051 	}
2052 
2053 	if (port->pending_replies)
2054 		server_port_flush(port);
2055 
2056 	r = 0;
2057 done:
2058 	EVDNS_UNLOCK(port);
2059 	return r;
2060 }
2061 
2062 /* Free all storage held by RRs in req. */
2063 static void
2064 server_request_free_answers(struct server_request *req)
2065 {
2066 	struct server_reply_item *victim, *next, **list;
2067 	int i;
2068 	for (i = 0; i < 3; ++i) {
2069 		if (i==0)
2070 			list = &req->answer;
2071 		else if (i==1)
2072 			list = &req->authority;
2073 		else
2074 			list = &req->additional;
2075 
2076 		victim = *list;
2077 		while (victim) {
2078 			next = victim->next;
2079 			mm_free(victim->name);
2080 			if (victim->data)
2081 				mm_free(victim->data);
2082 			mm_free(victim);
2083 			victim = next;
2084 		}
2085 		*list = NULL;
2086 	}
2087 }
2088 
2089 /* Free all storage held by req, and remove links to it. */
2090 /* return true iff we just wound up freeing the server_port. */
2091 static int
2092 server_request_free(struct server_request *req)
2093 {
2094 	int i, rc=1, lock=0;
2095 	if (req->base.questions) {
2096 		for (i = 0; i < req->base.nquestions; ++i)
2097 			mm_free(req->base.questions[i]);
2098 		mm_free(req->base.questions);
2099 	}
2100 
2101 	if (req->port) {
2102 		EVDNS_LOCK(req->port);
2103 		lock=1;
2104 		if (req->port->pending_replies == req) {
2105 			if (req->next_pending && req->next_pending != req)
2106 				req->port->pending_replies = req->next_pending;
2107 			else
2108 				req->port->pending_replies = NULL;
2109 		}
2110 		rc = --req->port->refcnt;
2111 	}
2112 
2113 	if (req->response) {
2114 		mm_free(req->response);
2115 	}
2116 
2117 	server_request_free_answers(req);
2118 
2119 	if (req->next_pending && req->next_pending != req) {
2120 		req->next_pending->prev_pending = req->prev_pending;
2121 		req->prev_pending->next_pending = req->next_pending;
2122 	}
2123 
2124 	if (rc == 0) {
2125 		EVDNS_UNLOCK(req->port); /* ????? nickm */
2126 		server_port_free(req->port);
2127 		mm_free(req);
2128 		return (1);
2129 	}
2130 	if (lock)
2131 		EVDNS_UNLOCK(req->port);
2132 	mm_free(req);
2133 	return (0);
2134 }
2135 
2136 /* Free all storage held by an evdns_server_port.  Only called when  */
2137 static void
2138 server_port_free(struct evdns_server_port *port)
2139 {
2140 	EVUTIL_ASSERT(port);
2141 	EVUTIL_ASSERT(!port->refcnt);
2142 	EVUTIL_ASSERT(!port->pending_replies);
2143 	if (port->socket > 0) {
2144 		evutil_closesocket(port->socket);
2145 		port->socket = -1;
2146 	}
2147 	(void) event_del(&port->event);
2148 	event_debug_unassign(&port->event);
2149 	EVTHREAD_FREE_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
2150 	mm_free(port);
2151 }
2152 
2153 /* exported function */
2154 int
2155 evdns_server_request_drop(struct evdns_server_request *req_)
2156 {
2157 	struct server_request *req = TO_SERVER_REQUEST(req_);
2158 	server_request_free(req);
2159 	return 0;
2160 }
2161 
2162 /* exported function */
2163 int
2164 evdns_server_request_get_requesting_addr(struct evdns_server_request *req_, struct sockaddr *sa, int addr_len)
2165 {
2166 	struct server_request *req = TO_SERVER_REQUEST(req_);
2167 	if (addr_len < (int)req->addrlen)
2168 		return -1;
2169 	memcpy(sa, &(req->addr), req->addrlen);
2170 	return req->addrlen;
2171 }
2172 
2173 #undef APPEND16
2174 #undef APPEND32
2175 
2176 /* this is a libevent callback function which is called when a request */
2177 /* has timed out. */
2178 static void
2179 evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg) {
2180 	struct request *const req = (struct request *) arg;
2181 	struct evdns_base *base = req->base;
2182 
2183 	(void) fd;
2184 	(void) events;
2185 
2186 	log(EVDNS_LOG_DEBUG, "Request %p timed out", arg);
2187 	EVDNS_LOCK(base);
2188 
2189 	if (req->tx_count >= req->base->global_max_retransmits) {
2190 		struct nameserver *ns = req->ns;
2191 		/* this request has failed */
2192 		log(EVDNS_LOG_DEBUG, "Giving up on request %p; tx_count==%d",
2193 		    arg, req->tx_count);
2194 		reply_schedule_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
2195 
2196 		request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
2197 		nameserver_failed(ns, "request timed out.");
2198 	} else {
2199 		/* retransmit it */
2200 		log(EVDNS_LOG_DEBUG, "Retransmitting request %p; tx_count==%d",
2201 		    arg, req->tx_count);
2202 		(void) evtimer_del(&req->timeout_event);
2203 		request_swap_ns(req, nameserver_pick(base));
2204 		evdns_request_transmit(req);
2205 
2206 		req->ns->timedout++;
2207 		if (req->ns->timedout > req->base->global_max_nameserver_timeout) {
2208 			req->ns->timedout = 0;
2209 			nameserver_failed(req->ns, "request timed out.");
2210 		}
2211 	}
2212 
2213 	EVDNS_UNLOCK(base);
2214 }
2215 
2216 /* try to send a request to a given server. */
2217 /* */
2218 /* return: */
2219 /*   0 ok */
2220 /*   1 temporary failure */
2221 /*   2 other failure */
2222 static int
2223 evdns_request_transmit_to(struct request *req, struct nameserver *server) {
2224 	int r;
2225 	ASSERT_LOCKED(req->base);
2226 	ASSERT_VALID_REQUEST(req);
2227 
2228 	if (server->requests_inflight == 1 &&
2229 		req->base->disable_when_inactive &&
2230 		event_add(&server->event, NULL) < 0) {
2231 		return 1;
2232 	}
2233 
2234 	r = sendto(server->socket, (void*)req->request, req->request_len, 0,
2235 	    (struct sockaddr *)&server->address, server->addrlen);
2236 	if (r < 0) {
2237 		int err = evutil_socket_geterror(server->socket);
2238 		if (EVUTIL_ERR_RW_RETRIABLE(err))
2239 			return 1;
2240 		nameserver_failed(req->ns, evutil_socket_error_to_string(err));
2241 		return 2;
2242 	} else if (r != (int)req->request_len) {
2243 		return 1;  /* short write */
2244 	} else {
2245 		return 0;
2246 	}
2247 }
2248 
2249 /* try to send a request, updating the fields of the request */
2250 /* as needed */
2251 /* */
2252 /* return: */
2253 /*   0 ok */
2254 /*   1 failed */
2255 static int
2256 evdns_request_transmit(struct request *req) {
2257 	int retcode = 0, r;
2258 
2259 	ASSERT_LOCKED(req->base);
2260 	ASSERT_VALID_REQUEST(req);
2261 	/* if we fail to send this packet then this flag marks it */
2262 	/* for evdns_transmit */
2263 	req->transmit_me = 1;
2264 	EVUTIL_ASSERT(req->trans_id != 0xffff);
2265 
2266 	if (!req->ns)
2267 	{
2268 		/* unable to transmit request if no nameservers */
2269 		return 1;
2270 	}
2271 
2272 	if (req->ns->choked) {
2273 		/* don't bother trying to write to a socket */
2274 		/* which we have had EAGAIN from */
2275 		return 1;
2276 	}
2277 
2278 	r = evdns_request_transmit_to(req, req->ns);
2279 	switch (r) {
2280 	case 1:
2281 		/* temp failure */
2282 		req->ns->choked = 1;
2283 		nameserver_write_waiting(req->ns, 1);
2284 		return 1;
2285 	case 2:
2286 		/* failed to transmit the request entirely. we can fallthrough since
2287 		 * we'll set a timeout, which will time out, and make us retransmit the
2288 		 * request anyway. */
2289 		retcode = 1;
2290 		EVUTIL_FALLTHROUGH;
2291 	default:
2292 		/* all ok */
2293 		log(EVDNS_LOG_DEBUG,
2294 		    "Setting timeout for request %p, sent to nameserver %p", req, req->ns);
2295 		if (evtimer_add(&req->timeout_event, &req->base->global_timeout) < 0) {
2296 			log(EVDNS_LOG_WARN,
2297 		      "Error from libevent when adding timer for request %p",
2298 			    req);
2299 			/* ???? Do more? */
2300 		}
2301 		req->tx_count++;
2302 		req->transmit_me = 0;
2303 		return retcode;
2304 	}
2305 }
2306 
2307 static void
2308 nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
2309 	struct nameserver *const ns = (struct nameserver *) arg;
2310 	(void) type;
2311 	(void) count;
2312 	(void) ttl;
2313 	(void) addresses;
2314 
2315 	if (result == DNS_ERR_CANCEL) {
2316 		/* We canceled this request because the nameserver came up
2317 		 * for some other reason.  Do not change our opinion about
2318 		 * the nameserver. */
2319 		return;
2320 	}
2321 
2322 	EVDNS_LOCK(ns->base);
2323 	ns->probe_request = NULL;
2324 	if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
2325 		/* this is a good reply */
2326 		nameserver_up(ns);
2327 	} else {
2328 		nameserver_probe_failed(ns);
2329 	}
2330 	EVDNS_UNLOCK(ns->base);
2331 }
2332 
2333 static void
2334 nameserver_send_probe(struct nameserver *const ns) {
2335 	struct evdns_request *handle;
2336 	struct request *req;
2337 	char addrbuf[128];
2338 	/* here we need to send a probe to a given nameserver */
2339 	/* in the hope that it is up now. */
2340 
2341 	ASSERT_LOCKED(ns->base);
2342 	log(EVDNS_LOG_DEBUG, "Sending probe to %s",
2343 	    evutil_format_sockaddr_port_(
2344 		    (struct sockaddr *)&ns->address,
2345 		    addrbuf, sizeof(addrbuf)));
2346 	handle = mm_calloc(1, sizeof(*handle));
2347 	if (!handle) return;
2348 	req = request_new(ns->base, handle, TYPE_A, "google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
2349 	if (!req) {
2350 		mm_free(handle);
2351 		return;
2352 	}
2353 	ns->probe_request = handle;
2354 	/* we force this into the inflight queue no matter what */
2355 	request_trans_id_set(req, transaction_id_pick(ns->base));
2356 	req->ns = ns;
2357 	request_submit(req);
2358 }
2359 
2360 /* returns: */
2361 /*   0 didn't try to transmit anything */
2362 /*   1 tried to transmit something */
2363 static int
2364 evdns_transmit(struct evdns_base *base) {
2365 	char did_try_to_transmit = 0;
2366 	int i;
2367 
2368 	ASSERT_LOCKED(base);
2369 	for (i = 0; i < base->n_req_heads; ++i) {
2370 		if (base->req_heads[i]) {
2371 			struct request *const started_at = base->req_heads[i], *req = started_at;
2372 			/* first transmit all the requests which are currently waiting */
2373 			do {
2374 				if (req->transmit_me) {
2375 					did_try_to_transmit = 1;
2376 					evdns_request_transmit(req);
2377 				}
2378 
2379 				req = req->next;
2380 			} while (req != started_at);
2381 		}
2382 	}
2383 
2384 	return did_try_to_transmit;
2385 }
2386 
2387 /* exported function */
2388 int
2389 evdns_base_count_nameservers(struct evdns_base *base)
2390 {
2391 	const struct nameserver *server;
2392 	int n = 0;
2393 
2394 	EVDNS_LOCK(base);
2395 	server = base->server_head;
2396 	if (!server)
2397 		goto done;
2398 	do {
2399 		++n;
2400 		server = server->next;
2401 	} while (server != base->server_head);
2402 done:
2403 	EVDNS_UNLOCK(base);
2404 	return n;
2405 }
2406 
2407 int
2408 evdns_count_nameservers(void)
2409 {
2410 	return evdns_base_count_nameservers(current_base);
2411 }
2412 
2413 /* exported function */
2414 int
2415 evdns_base_clear_nameservers_and_suspend(struct evdns_base *base)
2416 {
2417 	struct nameserver *server, *started_at;
2418 	int i;
2419 
2420 	EVDNS_LOCK(base);
2421 	server = base->server_head;
2422 	started_at = base->server_head;
2423 	if (!server) {
2424 		EVDNS_UNLOCK(base);
2425 		return 0;
2426 	}
2427 	while (1) {
2428 		struct nameserver *next = server->next;
2429 		(void) event_del(&server->event);
2430 		if (evtimer_initialized(&server->timeout_event))
2431 			(void) evtimer_del(&server->timeout_event);
2432 		if (server->probe_request) {
2433 			evdns_cancel_request(server->base, server->probe_request);
2434 			server->probe_request = NULL;
2435 		}
2436 		if (server->socket >= 0)
2437 			evutil_closesocket(server->socket);
2438 		mm_free(server);
2439 		if (next == started_at)
2440 			break;
2441 		server = next;
2442 	}
2443 	base->server_head = NULL;
2444 	base->global_good_nameservers = 0;
2445 
2446 	for (i = 0; i < base->n_req_heads; ++i) {
2447 		struct request *req, *req_started_at;
2448 		req = req_started_at = base->req_heads[i];
2449 		while (req) {
2450 			struct request *next = req->next;
2451 			req->tx_count = req->reissue_count = 0;
2452 			req->ns = NULL;
2453 			/* ???? What to do about searches? */
2454 			(void) evtimer_del(&req->timeout_event);
2455 			req->trans_id = 0;
2456 			req->transmit_me = 0;
2457 
2458 			base->global_requests_waiting++;
2459 			evdns_request_insert(req, &base->req_waiting_head);
2460 			/* We want to insert these suspended elements at the front of
2461 			 * the waiting queue, since they were pending before any of
2462 			 * the waiting entries were added.  This is a circular list,
2463 			 * so we can just shift the start back by one.*/
2464 			base->req_waiting_head = base->req_waiting_head->prev;
2465 
2466 			if (next == req_started_at)
2467 				break;
2468 			req = next;
2469 		}
2470 		base->req_heads[i] = NULL;
2471 	}
2472 
2473 	base->global_requests_inflight = 0;
2474 
2475 	EVDNS_UNLOCK(base);
2476 	return 0;
2477 }
2478 
2479 int
2480 evdns_clear_nameservers_and_suspend(void)
2481 {
2482 	return evdns_base_clear_nameservers_and_suspend(current_base);
2483 }
2484 
2485 
2486 /* exported function */
2487 int
2488 evdns_base_resume(struct evdns_base *base)
2489 {
2490 	EVDNS_LOCK(base);
2491 	evdns_requests_pump_waiting_queue(base);
2492 	EVDNS_UNLOCK(base);
2493 
2494 	return 0;
2495 }
2496 
2497 int
2498 evdns_resume(void)
2499 {
2500 	return evdns_base_resume(current_base);
2501 }
2502 
2503 static int
2504 evdns_nameserver_add_impl_(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
2505 	/* first check to see if we already have this nameserver */
2506 
2507 	const struct nameserver *server = base->server_head, *const started_at = base->server_head;
2508 	struct nameserver *ns;
2509 	int err = 0;
2510 	char addrbuf[128];
2511 
2512 	ASSERT_LOCKED(base);
2513 	if (server) {
2514 		do {
2515 			if (!evutil_sockaddr_cmp((struct sockaddr*)&server->address, address, 1)) return 3;
2516 			server = server->next;
2517 		} while (server != started_at);
2518 	}
2519 	if (addrlen > (int)sizeof(ns->address)) {
2520 		log(EVDNS_LOG_DEBUG, "Addrlen %d too long.", (int)addrlen);
2521 		return 2;
2522 	}
2523 
2524 	ns = (struct nameserver *) mm_malloc(sizeof(struct nameserver));
2525 	if (!ns) return -1;
2526 
2527 	memset(ns, 0, sizeof(struct nameserver));
2528 	ns->base = base;
2529 
2530 	evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns);
2531 
2532 	ns->socket = evutil_socket_(address->sa_family,
2533 	    SOCK_DGRAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
2534 	if (ns->socket < 0) { err = 1; goto out1; }
2535 
2536 	if (base->global_outgoing_addrlen &&
2537 	    !evutil_sockaddr_is_loopback_(address)) {
2538 		if (bind(ns->socket,
2539 			(struct sockaddr*)&base->global_outgoing_address,
2540 			base->global_outgoing_addrlen) < 0) {
2541 			log(EVDNS_LOG_WARN,"Couldn't bind to outgoing address");
2542 			err = 2;
2543 			goto out2;
2544 		}
2545 	}
2546 
2547 	if (base->so_rcvbuf) {
2548 		if (setsockopt(ns->socket, SOL_SOCKET, SO_RCVBUF,
2549 		    (void *)&base->so_rcvbuf, sizeof(base->so_rcvbuf))) {
2550 			log(EVDNS_LOG_WARN, "Couldn't set SO_RCVBUF to %i", base->so_rcvbuf);
2551 			err = -SO_RCVBUF;
2552 			goto out2;
2553 		}
2554 	}
2555 	if (base->so_sndbuf) {
2556 		if (setsockopt(ns->socket, SOL_SOCKET, SO_SNDBUF,
2557 		    (void *)&base->so_sndbuf, sizeof(base->so_sndbuf))) {
2558 			log(EVDNS_LOG_WARN, "Couldn't set SO_SNDBUF to %i", base->so_sndbuf);
2559 			err = -SO_SNDBUF;
2560 			goto out2;
2561 		}
2562 	}
2563 
2564 	memcpy(&ns->address, address, addrlen);
2565 	ns->addrlen = addrlen;
2566 	ns->state = 1;
2567 	event_assign(&ns->event, ns->base->event_base, ns->socket,
2568 				 EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
2569 	if (!base->disable_when_inactive && event_add(&ns->event, NULL) < 0) {
2570 		err = 2;
2571 		goto out2;
2572 	}
2573 
2574 	log(EVDNS_LOG_DEBUG, "Added nameserver %s as %p",
2575 	    evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), ns);
2576 
2577 	/* insert this nameserver into the list of them */
2578 	if (!base->server_head) {
2579 		ns->next = ns->prev = ns;
2580 		base->server_head = ns;
2581 	} else {
2582 		ns->next = base->server_head->next;
2583 		ns->prev = base->server_head;
2584 		base->server_head->next = ns;
2585 		ns->next->prev = ns;
2586 	}
2587 
2588 	base->global_good_nameservers++;
2589 
2590 	return 0;
2591 
2592 out2:
2593 	evutil_closesocket(ns->socket);
2594 out1:
2595 	event_debug_unassign(&ns->event);
2596 	mm_free(ns);
2597 	log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d",
2598 	    evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), err);
2599 	return err;
2600 }
2601 
2602 /* exported function */
2603 int
2604 evdns_base_nameserver_add(struct evdns_base *base, unsigned long int address)
2605 {
2606 	struct sockaddr_in sin;
2607 	int res;
2608 	memset(&sin, 0, sizeof(sin));
2609 	sin.sin_addr.s_addr = address;
2610 	sin.sin_port = htons(53);
2611 	sin.sin_family = AF_INET;
2612 	EVDNS_LOCK(base);
2613 	res = evdns_nameserver_add_impl_(base, (struct sockaddr*)&sin, sizeof(sin));
2614 	EVDNS_UNLOCK(base);
2615 	return res;
2616 }
2617 
2618 int
2619 evdns_nameserver_add(unsigned long int address) {
2620 	if (!current_base)
2621 		current_base = evdns_base_new(NULL, 0);
2622 	return evdns_base_nameserver_add(current_base, address);
2623 }
2624 
2625 static void
2626 sockaddr_setport(struct sockaddr *sa, ev_uint16_t port)
2627 {
2628 	if (sa->sa_family == AF_INET) {
2629 		((struct sockaddr_in *)sa)->sin_port = htons(port);
2630 	} else if (sa->sa_family == AF_INET6) {
2631 		((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
2632 	}
2633 }
2634 
2635 static ev_uint16_t
2636 sockaddr_getport(struct sockaddr *sa)
2637 {
2638 	if (sa->sa_family == AF_INET) {
2639 		return ntohs(((struct sockaddr_in *)sa)->sin_port);
2640 	} else if (sa->sa_family == AF_INET6) {
2641 		return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
2642 	} else {
2643 		return 0;
2644 	}
2645 }
2646 
2647 /* exported function */
2648 int
2649 evdns_base_nameserver_ip_add(struct evdns_base *base, const char *ip_as_string) {
2650 	struct sockaddr_storage ss;
2651 	struct sockaddr *sa;
2652 	int len = sizeof(ss);
2653 	int res;
2654 	if (evutil_parse_sockaddr_port(ip_as_string, (struct sockaddr *)&ss,
2655 		&len)) {
2656 		log(EVDNS_LOG_WARN, "Unable to parse nameserver address %s",
2657 			ip_as_string);
2658 		return 4;
2659 	}
2660 	sa = (struct sockaddr *) &ss;
2661 	if (sockaddr_getport(sa) == 0)
2662 		sockaddr_setport(sa, 53);
2663 
2664 	EVDNS_LOCK(base);
2665 	res = evdns_nameserver_add_impl_(base, sa, len);
2666 	EVDNS_UNLOCK(base);
2667 	return res;
2668 }
2669 
2670 int
2671 evdns_nameserver_ip_add(const char *ip_as_string) {
2672 	if (!current_base)
2673 		current_base = evdns_base_new(NULL, 0);
2674 	return evdns_base_nameserver_ip_add(current_base, ip_as_string);
2675 }
2676 
2677 int
2678 evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
2679     const struct sockaddr *sa, ev_socklen_t len, unsigned flags)
2680 {
2681 	int res;
2682 	EVUTIL_ASSERT(base);
2683 	EVDNS_LOCK(base);
2684 	res = evdns_nameserver_add_impl_(base, sa, len);
2685 	EVDNS_UNLOCK(base);
2686 	return res;
2687 }
2688 
2689 int
2690 evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
2691     struct sockaddr *sa, ev_socklen_t len)
2692 {
2693 	int result = -1;
2694 	int i;
2695 	struct nameserver *server;
2696 	EVDNS_LOCK(base);
2697 	server = base->server_head;
2698 	for (i = 0; i < idx && server; ++i, server = server->next) {
2699 		if (server->next == base->server_head)
2700 			goto done;
2701 	}
2702 	if (! server)
2703 		goto done;
2704 
2705 	if (server->addrlen > len) {
2706 		result = (int) server->addrlen;
2707 		goto done;
2708 	}
2709 
2710 	memcpy(sa, &server->address, server->addrlen);
2711 	result = (int) server->addrlen;
2712 done:
2713 	EVDNS_UNLOCK(base);
2714 	return result;
2715 }
2716 
2717 /* remove from the queue */
2718 static void
2719 evdns_request_remove(struct request *req, struct request **head)
2720 {
2721 	ASSERT_LOCKED(req->base);
2722 	ASSERT_VALID_REQUEST(req);
2723 
2724 #if 0
2725 	{
2726 		struct request *ptr;
2727 		int found = 0;
2728 		EVUTIL_ASSERT(*head != NULL);
2729 
2730 		ptr = *head;
2731 		do {
2732 			if (ptr == req) {
2733 				found = 1;
2734 				break;
2735 			}
2736 			ptr = ptr->next;
2737 		} while (ptr != *head);
2738 		EVUTIL_ASSERT(found);
2739 
2740 		EVUTIL_ASSERT(req->next);
2741 	}
2742 #endif
2743 
2744 	if (req->next == req) {
2745 		/* only item in the list */
2746 		*head = NULL;
2747 	} else {
2748 		req->next->prev = req->prev;
2749 		req->prev->next = req->next;
2750 		if (*head == req) *head = req->next;
2751 	}
2752 	req->next = req->prev = NULL;
2753 }
2754 
2755 /* insert into the tail of the queue */
2756 static void
2757 evdns_request_insert(struct request *req, struct request **head) {
2758 	ASSERT_LOCKED(req->base);
2759 	ASSERT_VALID_REQUEST(req);
2760 	if (!*head) {
2761 		*head = req;
2762 		req->next = req->prev = req;
2763 		return;
2764 	}
2765 
2766 	req->prev = (*head)->prev;
2767 	req->prev->next = req;
2768 	req->next = *head;
2769 	(*head)->prev = req;
2770 }
2771 
2772 static int
2773 string_num_dots(const char *s) {
2774 	int count = 0;
2775 	while ((s = strchr(s, '.'))) {
2776 		s++;
2777 		count++;
2778 	}
2779 	return count;
2780 }
2781 
2782 static struct request *
2783 request_new(struct evdns_base *base, struct evdns_request *handle, int type,
2784 	    const char *name, int flags, evdns_callback_type callback,
2785 	    void *user_ptr) {
2786 
2787 	const char issuing_now =
2788 	    (base->global_requests_inflight < base->global_max_requests_inflight) ? 1 : 0;
2789 
2790 	const size_t name_len = strlen(name);
2791 	const size_t request_max_len = evdns_request_len(name_len);
2792 	const u16 trans_id = issuing_now ? transaction_id_pick(base) : 0xffff;
2793 	/* the request data is alloced in a single block with the header */
2794 	struct request *const req =
2795 	    mm_malloc(sizeof(struct request) + request_max_len);
2796 	int rlen;
2797 	char namebuf[256];
2798 	(void) flags;
2799 
2800 	ASSERT_LOCKED(base);
2801 
2802 	if (!req) return NULL;
2803 
2804 	if (name_len >= sizeof(namebuf)) {
2805 		mm_free(req);
2806 		return NULL;
2807 	}
2808 
2809 	memset(req, 0, sizeof(struct request));
2810 	req->base = base;
2811 
2812 	evtimer_assign(&req->timeout_event, req->base->event_base, evdns_request_timeout_callback, req);
2813 
2814 	if (base->global_randomize_case) {
2815 		unsigned i;
2816 		char randbits[(sizeof(namebuf)+7)/8];
2817 		strlcpy(namebuf, name, sizeof(namebuf));
2818 		evutil_secure_rng_get_bytes(randbits, (name_len+7)/8);
2819 		for (i = 0; i < name_len; ++i) {
2820 			if (EVUTIL_ISALPHA_(namebuf[i])) {
2821 				if ((randbits[i >> 3] & (1<<(i & 7))))
2822 					namebuf[i] |= 0x20;
2823 				else
2824 					namebuf[i] &= ~0x20;
2825 			}
2826 		}
2827 		name = namebuf;
2828 	}
2829 
2830 	/* request data lives just after the header */
2831 	req->request = ((u8 *) req) + sizeof(struct request);
2832 	/* denotes that the request data shouldn't be free()ed */
2833 	req->request_appended = 1;
2834 	rlen = evdns_request_data_build(name, name_len, trans_id,
2835 	    type, CLASS_INET, req->request, request_max_len);
2836 	if (rlen < 0)
2837 		goto err1;
2838 
2839 	req->request_len = rlen;
2840 	req->trans_id = trans_id;
2841 	req->tx_count = 0;
2842 	req->request_type = type;
2843 	req->user_pointer = user_ptr;
2844 	req->user_callback = callback;
2845 	req->ns = issuing_now ? nameserver_pick(base) : NULL;
2846 	req->next = req->prev = NULL;
2847 	req->handle = handle;
2848 	if (handle) {
2849 		handle->current_req = req;
2850 		handle->base = base;
2851 	}
2852 
2853 	return req;
2854 err1:
2855 	mm_free(req);
2856 	return NULL;
2857 }
2858 
2859 static void
2860 request_submit(struct request *const req) {
2861 	struct evdns_base *base = req->base;
2862 	ASSERT_LOCKED(base);
2863 	ASSERT_VALID_REQUEST(req);
2864 	if (req->ns) {
2865 		/* if it has a nameserver assigned then this is going */
2866 		/* straight into the inflight queue */
2867 		evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
2868 
2869 		base->global_requests_inflight++;
2870 		req->ns->requests_inflight++;
2871 
2872 		evdns_request_transmit(req);
2873 	} else {
2874 		evdns_request_insert(req, &base->req_waiting_head);
2875 		base->global_requests_waiting++;
2876 	}
2877 }
2878 
2879 /* exported function */
2880 void
2881 evdns_cancel_request(struct evdns_base *base, struct evdns_request *handle)
2882 {
2883 	struct request *req;
2884 
2885 	if (!handle->current_req)
2886 		return;
2887 
2888 	if (!base) {
2889 		/* This redundancy is silly; can we fix it? (Not for 2.0) XXXX */
2890 		base = handle->base;
2891 		if (!base)
2892 			base = handle->current_req->base;
2893 	}
2894 
2895 	EVDNS_LOCK(base);
2896 	if (handle->pending_cb) {
2897 		EVDNS_UNLOCK(base);
2898 		return;
2899 	}
2900 
2901 	req = handle->current_req;
2902 	ASSERT_VALID_REQUEST(req);
2903 
2904 	reply_schedule_callback(req, 0, DNS_ERR_CANCEL, NULL);
2905 	if (req->ns) {
2906 		/* remove from inflight queue */
2907 		request_finished(req, &REQ_HEAD(base, req->trans_id), 1);
2908 	} else {
2909 		/* remove from global_waiting head */
2910 		request_finished(req, &base->req_waiting_head, 1);
2911 	}
2912 	EVDNS_UNLOCK(base);
2913 }
2914 
2915 /* exported function */
2916 struct evdns_request *
2917 evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags,
2918     evdns_callback_type callback, void *ptr) {
2919 	struct evdns_request *handle;
2920 	struct request *req;
2921 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2922 	handle = mm_calloc(1, sizeof(*handle));
2923 	if (handle == NULL)
2924 		return NULL;
2925 	EVDNS_LOCK(base);
2926 	if (flags & DNS_QUERY_NO_SEARCH) {
2927 		req =
2928 			request_new(base, handle, TYPE_A, name, flags,
2929 				    callback, ptr);
2930 		if (req)
2931 			request_submit(req);
2932 	} else {
2933 		search_request_new(base, handle, TYPE_A, name, flags,
2934 		    callback, ptr);
2935 	}
2936 	if (handle->current_req == NULL) {
2937 		mm_free(handle);
2938 		handle = NULL;
2939 	}
2940 	EVDNS_UNLOCK(base);
2941 	return handle;
2942 }
2943 
2944 int evdns_resolve_ipv4(const char *name, int flags,
2945 					   evdns_callback_type callback, void *ptr)
2946 {
2947 	return evdns_base_resolve_ipv4(current_base, name, flags, callback, ptr)
2948 		? 0 : -1;
2949 }
2950 
2951 
2952 /* exported function */
2953 struct evdns_request *
2954 evdns_base_resolve_ipv6(struct evdns_base *base,
2955     const char *name, int flags,
2956     evdns_callback_type callback, void *ptr)
2957 {
2958 	struct evdns_request *handle;
2959 	struct request *req;
2960 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2961 	handle = mm_calloc(1, sizeof(*handle));
2962 	if (handle == NULL)
2963 		return NULL;
2964 	EVDNS_LOCK(base);
2965 	if (flags & DNS_QUERY_NO_SEARCH) {
2966 		req = request_new(base, handle, TYPE_AAAA, name, flags,
2967 				  callback, ptr);
2968 		if (req)
2969 			request_submit(req);
2970 	} else {
2971 		search_request_new(base, handle, TYPE_AAAA, name, flags,
2972 		    callback, ptr);
2973 	}
2974 	if (handle->current_req == NULL) {
2975 		mm_free(handle);
2976 		handle = NULL;
2977 	}
2978 	EVDNS_UNLOCK(base);
2979 	return handle;
2980 }
2981 
2982 int evdns_resolve_ipv6(const char *name, int flags,
2983     evdns_callback_type callback, void *ptr) {
2984 	return evdns_base_resolve_ipv6(current_base, name, flags, callback, ptr)
2985 		? 0 : -1;
2986 }
2987 
2988 struct evdns_request *
2989 evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2990 	char buf[32];
2991 	struct evdns_request *handle;
2992 	struct request *req;
2993 	u32 a;
2994 	EVUTIL_ASSERT(in);
2995 	a = ntohl(in->s_addr);
2996 	evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
2997 			(int)(u8)((a	)&0xff),
2998 			(int)(u8)((a>>8 )&0xff),
2999 			(int)(u8)((a>>16)&0xff),
3000 			(int)(u8)((a>>24)&0xff));
3001 	handle = mm_calloc(1, sizeof(*handle));
3002 	if (handle == NULL)
3003 		return NULL;
3004 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
3005 	EVDNS_LOCK(base);
3006 	req = request_new(base, handle, TYPE_PTR, buf, flags, callback, ptr);
3007 	if (req)
3008 		request_submit(req);
3009 	if (handle->current_req == NULL) {
3010 		mm_free(handle);
3011 		handle = NULL;
3012 	}
3013 	EVDNS_UNLOCK(base);
3014 	return (handle);
3015 }
3016 
3017 int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3018 	return evdns_base_resolve_reverse(current_base, in, flags, callback, ptr)
3019 		? 0 : -1;
3020 }
3021 
3022 struct evdns_request *
3023 evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3024 	/* 32 nybbles, 32 periods, "ip6.arpa", NUL. */
3025 	char buf[73];
3026 	char *cp;
3027 	struct evdns_request *handle;
3028 	struct request *req;
3029 	int i;
3030 	EVUTIL_ASSERT(in);
3031 	cp = buf;
3032 	for (i=15; i >= 0; --i) {
3033 		u8 byte = in->s6_addr[i];
3034 		*cp++ = "0123456789abcdef"[byte & 0x0f];
3035 		*cp++ = '.';
3036 		*cp++ = "0123456789abcdef"[byte >> 4];
3037 		*cp++ = '.';
3038 	}
3039 	EVUTIL_ASSERT(cp + strlen("ip6.arpa") < buf+sizeof(buf));
3040 	memcpy(cp, "ip6.arpa", strlen("ip6.arpa")+1);
3041 	handle = mm_calloc(1, sizeof(*handle));
3042 	if (handle == NULL)
3043 		return NULL;
3044 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
3045 	EVDNS_LOCK(base);
3046 	req = request_new(base, handle, TYPE_PTR, buf, flags, callback, ptr);
3047 	if (req)
3048 		request_submit(req);
3049 	if (handle->current_req == NULL) {
3050 		mm_free(handle);
3051 		handle = NULL;
3052 	}
3053 	EVDNS_UNLOCK(base);
3054 	return (handle);
3055 }
3056 
3057 int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3058 	return evdns_base_resolve_reverse_ipv6(current_base, in, flags, callback, ptr)
3059 		? 0 : -1;
3060 }
3061 
3062 /* ================================================================= */
3063 /* Search support */
3064 /* */
3065 /* the libc resolver has support for searching a number of domains */
3066 /* to find a name. If nothing else then it takes the single domain */
3067 /* from the gethostname() call. */
3068 /* */
3069 /* It can also be configured via the domain and search options in a */
3070 /* resolv.conf. */
3071 /* */
3072 /* The ndots option controls how many dots it takes for the resolver */
3073 /* to decide that a name is non-local and so try a raw lookup first. */
3074 
3075 struct search_domain {
3076 	int len;
3077 	struct search_domain *next;
3078 	/* the text string is appended to this structure */
3079 };
3080 
3081 struct search_state {
3082 	int refcount;
3083 	int ndots;
3084 	int num_domains;
3085 	struct search_domain *head;
3086 };
3087 
3088 static void
3089 search_state_decref(struct search_state *const state) {
3090 	if (!state) return;
3091 	state->refcount--;
3092 	if (!state->refcount) {
3093 		struct search_domain *next, *dom;
3094 		for (dom = state->head; dom; dom = next) {
3095 			next = dom->next;
3096 			mm_free(dom);
3097 		}
3098 		mm_free(state);
3099 	}
3100 }
3101 
3102 static struct search_state *
3103 search_state_new(void) {
3104 	struct search_state *state = (struct search_state *) mm_malloc(sizeof(struct search_state));
3105 	if (!state) return NULL;
3106 	memset(state, 0, sizeof(struct search_state));
3107 	state->refcount = 1;
3108 	state->ndots = 1;
3109 
3110 	return state;
3111 }
3112 
3113 static void
3114 search_postfix_clear(struct evdns_base *base) {
3115 	search_state_decref(base->global_search_state);
3116 
3117 	base->global_search_state = search_state_new();
3118 }
3119 
3120 /* exported function */
3121 void
3122 evdns_base_search_clear(struct evdns_base *base)
3123 {
3124 	EVDNS_LOCK(base);
3125 	search_postfix_clear(base);
3126 	EVDNS_UNLOCK(base);
3127 }
3128 
3129 void
3130 evdns_search_clear(void) {
3131 	evdns_base_search_clear(current_base);
3132 }
3133 
3134 static void
3135 search_postfix_add(struct evdns_base *base, const char *domain) {
3136 	size_t domain_len;
3137 	struct search_domain *sdomain;
3138 	while (domain[0] == '.') domain++;
3139 	domain_len = strlen(domain);
3140 
3141 	ASSERT_LOCKED(base);
3142 	if (!base->global_search_state) base->global_search_state = search_state_new();
3143 	if (!base->global_search_state) return;
3144 	base->global_search_state->num_domains++;
3145 
3146 	sdomain = (struct search_domain *) mm_malloc(sizeof(struct search_domain) + domain_len);
3147 	if (!sdomain) return;
3148 	memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
3149 	sdomain->next = base->global_search_state->head;
3150 	sdomain->len = (int) domain_len;
3151 
3152 	base->global_search_state->head = sdomain;
3153 }
3154 
3155 /* reverse the order of members in the postfix list. This is needed because, */
3156 /* when parsing resolv.conf we push elements in the wrong order */
3157 static void
3158 search_reverse(struct evdns_base *base) {
3159 	struct search_domain *cur, *prev = NULL, *next;
3160 	ASSERT_LOCKED(base);
3161 	cur = base->global_search_state->head;
3162 	while (cur) {
3163 		next = cur->next;
3164 		cur->next = prev;
3165 		prev = cur;
3166 		cur = next;
3167 	}
3168 
3169 	base->global_search_state->head = prev;
3170 }
3171 
3172 /* exported function */
3173 void
3174 evdns_base_search_add(struct evdns_base *base, const char *domain) {
3175 	EVDNS_LOCK(base);
3176 	search_postfix_add(base, domain);
3177 	EVDNS_UNLOCK(base);
3178 }
3179 void
3180 evdns_search_add(const char *domain) {
3181 	evdns_base_search_add(current_base, domain);
3182 }
3183 
3184 /* exported function */
3185 void
3186 evdns_base_search_ndots_set(struct evdns_base *base, const int ndots) {
3187 	EVDNS_LOCK(base);
3188 	if (!base->global_search_state) base->global_search_state = search_state_new();
3189 	if (base->global_search_state)
3190 		base->global_search_state->ndots = ndots;
3191 	EVDNS_UNLOCK(base);
3192 }
3193 void
3194 evdns_search_ndots_set(const int ndots) {
3195 	evdns_base_search_ndots_set(current_base, ndots);
3196 }
3197 
3198 static void
3199 search_set_from_hostname(struct evdns_base *base) {
3200 	char hostname[HOST_NAME_MAX + 1], *domainname;
3201 
3202 	ASSERT_LOCKED(base);
3203 	search_postfix_clear(base);
3204 	if (gethostname(hostname, sizeof(hostname))) return;
3205 	domainname = strchr(hostname, '.');
3206 	if (!domainname) return;
3207 	search_postfix_add(base, domainname);
3208 }
3209 
3210 /* warning: returns malloced string */
3211 static char *
3212 search_make_new(const struct search_state *const state, int n, const char *const base_name) {
3213 	const size_t base_len = strlen(base_name);
3214 	char need_to_append_dot;
3215 	struct search_domain *dom;
3216 
3217 	if (!base_len) return NULL;
3218 	need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
3219 
3220 	for (dom = state->head; dom; dom = dom->next) {
3221 		if (!n--) {
3222 			/* this is the postfix we want */
3223 			/* the actual postfix string is kept at the end of the structure */
3224 			const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
3225 			const int postfix_len = dom->len;
3226 			char *const newname = (char *) mm_malloc(base_len + need_to_append_dot + postfix_len + 1);
3227 			if (!newname) return NULL;
3228 			memcpy(newname, base_name, base_len);
3229 			if (need_to_append_dot) newname[base_len] = '.';
3230 			memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
3231 			newname[base_len + need_to_append_dot + postfix_len] = 0;
3232 			return newname;
3233 		}
3234 	}
3235 
3236 	/* we ran off the end of the list and still didn't find the requested string */
3237 	EVUTIL_ASSERT(0);
3238 	return NULL; /* unreachable; stops warnings in some compilers. */
3239 }
3240 
3241 static struct request *
3242 search_request_new(struct evdns_base *base, struct evdns_request *handle,
3243 		   int type, const char *const name, int flags,
3244 		   evdns_callback_type user_callback, void *user_arg) {
3245 	ASSERT_LOCKED(base);
3246 	EVUTIL_ASSERT(type == TYPE_A || type == TYPE_AAAA);
3247 	EVUTIL_ASSERT(handle->current_req == NULL);
3248 	if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
3249 	     base->global_search_state &&
3250 		 base->global_search_state->num_domains) {
3251 		/* we have some domains to search */
3252 		struct request *req;
3253 		if (string_num_dots(name) >= base->global_search_state->ndots) {
3254 			req = request_new(base, handle, type, name, flags, user_callback, user_arg);
3255 			if (!req) return NULL;
3256 			handle->search_index = -1;
3257 		} else {
3258 			char *const new_name = search_make_new(base->global_search_state, 0, name);
3259 			if (!new_name) return NULL;
3260 			req = request_new(base, handle, type, new_name, flags, user_callback, user_arg);
3261 			mm_free(new_name);
3262 			if (!req) return NULL;
3263 			handle->search_index = 0;
3264 		}
3265 		EVUTIL_ASSERT(handle->search_origname == NULL);
3266 		handle->search_origname = mm_strdup(name);
3267 		if (handle->search_origname == NULL) {
3268 			/* XXX Should we dealloc req? If yes, how? */
3269 			if (req)
3270 				mm_free(req);
3271 			return NULL;
3272 		}
3273 		handle->search_state = base->global_search_state;
3274 		handle->search_flags = flags;
3275 		base->global_search_state->refcount++;
3276 		request_submit(req);
3277 		return req;
3278 	} else {
3279 		struct request *const req = request_new(base, handle, type, name, flags, user_callback, user_arg);
3280 		if (!req) return NULL;
3281 		request_submit(req);
3282 		return req;
3283 	}
3284 }
3285 
3286 /* this is called when a request has failed to find a name. We need to check */
3287 /* if it is part of a search and, if so, try the next name in the list */
3288 /* returns: */
3289 /*   0 another request has been submitted */
3290 /*   1 no more requests needed */
3291 static int
3292 search_try_next(struct evdns_request *const handle) {
3293 	struct request *req = handle->current_req;
3294 	struct evdns_base *base = req->base;
3295 	struct request *newreq;
3296 	ASSERT_LOCKED(base);
3297 	if (handle->search_state) {
3298 		/* it is part of a search */
3299 		char *new_name;
3300 		handle->search_index++;
3301 		if (handle->search_index >= handle->search_state->num_domains) {
3302 			/* no more postfixes to try, however we may need to try */
3303 			/* this name without a postfix */
3304 			if (string_num_dots(handle->search_origname) < handle->search_state->ndots) {
3305 				/* yep, we need to try it raw */
3306 				newreq = request_new(base, NULL, req->request_type, handle->search_origname, handle->search_flags, req->user_callback, req->user_pointer);
3307 				log(EVDNS_LOG_DEBUG, "Search: trying raw query %s", handle->search_origname);
3308 				if (newreq) {
3309 					search_request_finished(handle);
3310 					goto submit_next;
3311 				}
3312 			}
3313 			return 1;
3314 		}
3315 
3316 		new_name = search_make_new(handle->search_state, handle->search_index, handle->search_origname);
3317 		if (!new_name) return 1;
3318 		log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, handle->search_index);
3319 		newreq = request_new(base, NULL, req->request_type, new_name, handle->search_flags, req->user_callback, req->user_pointer);
3320 		mm_free(new_name);
3321 		if (!newreq) return 1;
3322 		goto submit_next;
3323 	}
3324 	return 1;
3325 
3326 submit_next:
3327 	request_finished(req, &REQ_HEAD(req->base, req->trans_id), 0);
3328 	handle->current_req = newreq;
3329 	newreq->handle = handle;
3330 	request_submit(newreq);
3331 	return 0;
3332 }
3333 
3334 static void
3335 search_request_finished(struct evdns_request *const handle) {
3336 	ASSERT_LOCKED(handle->current_req->base);
3337 	if (handle->search_state) {
3338 		search_state_decref(handle->search_state);
3339 		handle->search_state = NULL;
3340 	}
3341 	if (handle->search_origname) {
3342 		mm_free(handle->search_origname);
3343 		handle->search_origname = NULL;
3344 	}
3345 }
3346 
3347 /* ================================================================= */
3348 /* Parsing resolv.conf files */
3349 
3350 static void
3351 evdns_resolv_set_defaults(struct evdns_base *base, int flags) {
3352 	int add_default = flags & DNS_OPTION_NAMESERVERS;
3353 	if (flags & DNS_OPTION_NAMESERVERS_NO_DEFAULT)
3354 		add_default = 0;
3355 
3356 	/* if the file isn't found then we assume a local resolver */
3357 	ASSERT_LOCKED(base);
3358 	if (flags & DNS_OPTION_SEARCH)
3359 		search_set_from_hostname(base);
3360 	if (add_default)
3361 		evdns_base_nameserver_ip_add(base, "127.0.0.1");
3362 }
3363 
3364 #ifndef EVENT__HAVE_STRTOK_R
3365 static char *
3366 strtok_r(char *s, const char *delim, char **state) {
3367 	char *cp, *start;
3368 	start = cp = s ? s : *state;
3369 	if (!cp)
3370 		return NULL;
3371 	while (*cp && !strchr(delim, *cp))
3372 		++cp;
3373 	if (!*cp) {
3374 		if (cp == start)
3375 			return NULL;
3376 		*state = NULL;
3377 		return start;
3378 	} else {
3379 		*cp++ = '\0';
3380 		*state = cp;
3381 		return start;
3382 	}
3383 }
3384 #endif
3385 
3386 /* helper version of atoi which returns -1 on error */
3387 static int
3388 strtoint(const char *const str)
3389 {
3390 	char *endptr;
3391 	const int r = strtol(str, &endptr, 10);
3392 	if (*endptr) return -1;
3393 	return r;
3394 }
3395 
3396 /* Parse a number of seconds into a timeval; return -1 on error. */
3397 static int
3398 evdns_strtotimeval(const char *const str, struct timeval *out)
3399 {
3400 	double d;
3401 	char *endptr;
3402 	d = strtod(str, &endptr);
3403 	if (*endptr) return -1;
3404 	if (d < 0) return -1;
3405 	out->tv_sec = (int) d;
3406 	out->tv_usec = (int) ((d - (int) d)*1000000);
3407 	if (out->tv_sec == 0 && out->tv_usec < 1000) /* less than 1 msec */
3408 		return -1;
3409 	return 0;
3410 }
3411 
3412 /* helper version of atoi that returns -1 on error and clips to bounds. */
3413 static int
3414 strtoint_clipped(const char *const str, int min, int max)
3415 {
3416 	int r = strtoint(str);
3417 	if (r == -1)
3418 		return r;
3419 	else if (r<min)
3420 		return min;
3421 	else if (r>max)
3422 		return max;
3423 	else
3424 		return r;
3425 }
3426 
3427 static int
3428 evdns_base_set_max_requests_inflight(struct evdns_base *base, int maxinflight)
3429 {
3430 	int old_n_heads = base->n_req_heads, n_heads;
3431 	struct request **old_heads = base->req_heads, **new_heads, *req;
3432 	int i;
3433 
3434 	ASSERT_LOCKED(base);
3435 	if (maxinflight < 1)
3436 		maxinflight = 1;
3437 	n_heads = (maxinflight+4) / 5;
3438 	EVUTIL_ASSERT(n_heads > 0);
3439 	new_heads = mm_calloc(n_heads, sizeof(struct request*));
3440 	if (!new_heads)
3441 		return (-1);
3442 	if (old_heads) {
3443 		for (i = 0; i < old_n_heads; ++i) {
3444 			while (old_heads[i]) {
3445 				req = old_heads[i];
3446 				evdns_request_remove(req, &old_heads[i]);
3447 				evdns_request_insert(req, &new_heads[req->trans_id % n_heads]);
3448 			}
3449 		}
3450 		mm_free(old_heads);
3451 	}
3452 	base->req_heads = new_heads;
3453 	base->n_req_heads = n_heads;
3454 	base->global_max_requests_inflight = maxinflight;
3455 	return (0);
3456 }
3457 
3458 /* exported function */
3459 int
3460 evdns_base_set_option(struct evdns_base *base,
3461     const char *option, const char *val)
3462 {
3463 	int res;
3464 	EVDNS_LOCK(base);
3465 	res = evdns_base_set_option_impl(base, option, val, DNS_OPTIONS_ALL);
3466 	EVDNS_UNLOCK(base);
3467 	return res;
3468 }
3469 
3470 static inline int
3471 str_matches_option(const char *s1, const char *optionname)
3472 {
3473 	/* Option names are given as "option:" We accept either 'option' in
3474 	 * s1, or 'option:randomjunk'.  The latter form is to implement the
3475 	 * resolv.conf parser. */
3476 	size_t optlen = strlen(optionname);
3477 	size_t slen = strlen(s1);
3478 	if (slen == optlen || slen == optlen - 1)
3479 		return !strncmp(s1, optionname, slen);
3480 	else if (slen > optlen)
3481 		return !strncmp(s1, optionname, optlen);
3482 	else
3483 		return 0;
3484 }
3485 
3486 static int
3487 evdns_base_set_option_impl(struct evdns_base *base,
3488     const char *option, const char *val, int flags)
3489 {
3490 	ASSERT_LOCKED(base);
3491 	if (str_matches_option(option, "ndots:")) {
3492 		const int ndots = strtoint(val);
3493 		if (ndots == -1) return -1;
3494 		if (!(flags & DNS_OPTION_SEARCH)) return 0;
3495 		log(EVDNS_LOG_DEBUG, "Setting ndots to %d", ndots);
3496 		if (!base->global_search_state) base->global_search_state = search_state_new();
3497 		if (!base->global_search_state) return -1;
3498 		base->global_search_state->ndots = ndots;
3499 	} else if (str_matches_option(option, "timeout:")) {
3500 		struct timeval tv;
3501 		if (evdns_strtotimeval(val, &tv) == -1) return -1;
3502 		if (!(flags & DNS_OPTION_MISC)) return 0;
3503 		log(EVDNS_LOG_DEBUG, "Setting timeout to %s", val);
3504 		memcpy(&base->global_timeout, &tv, sizeof(struct timeval));
3505 	} else if (str_matches_option(option, "getaddrinfo-allow-skew:")) {
3506 		struct timeval tv;
3507 		if (evdns_strtotimeval(val, &tv) == -1) return -1;
3508 		if (!(flags & DNS_OPTION_MISC)) return 0;
3509 		log(EVDNS_LOG_DEBUG, "Setting getaddrinfo-allow-skew to %s",
3510 		    val);
3511 		memcpy(&base->global_getaddrinfo_allow_skew, &tv,
3512 		    sizeof(struct timeval));
3513 	} else if (str_matches_option(option, "max-timeouts:")) {
3514 		const int maxtimeout = strtoint_clipped(val, 1, 255);
3515 		if (maxtimeout == -1) return -1;
3516 		if (!(flags & DNS_OPTION_MISC)) return 0;
3517 		log(EVDNS_LOG_DEBUG, "Setting maximum allowed timeouts to %d",
3518 			maxtimeout);
3519 		base->global_max_nameserver_timeout = maxtimeout;
3520 	} else if (str_matches_option(option, "max-inflight:")) {
3521 		const int maxinflight = strtoint_clipped(val, 1, 65000);
3522 		if (maxinflight == -1) return -1;
3523 		if (!(flags & DNS_OPTION_MISC)) return 0;
3524 		log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d",
3525 			maxinflight);
3526 		evdns_base_set_max_requests_inflight(base, maxinflight);
3527 	} else if (str_matches_option(option, "attempts:")) {
3528 		int retries = strtoint(val);
3529 		if (retries == -1) return -1;
3530 		if (retries > 255) retries = 255;
3531 		if (!(flags & DNS_OPTION_MISC)) return 0;
3532 		log(EVDNS_LOG_DEBUG, "Setting retries to %d", retries);
3533 		base->global_max_retransmits = retries;
3534 	} else if (str_matches_option(option, "randomize-case:")) {
3535 		int randcase = strtoint(val);
3536 		if (randcase == -1) return -1;
3537 		if (!(flags & DNS_OPTION_MISC)) return 0;
3538 		base->global_randomize_case = randcase;
3539 	} else if (str_matches_option(option, "bind-to:")) {
3540 		/* XXX This only applies to successive nameservers, not
3541 		 * to already-configured ones.	We might want to fix that. */
3542 		int len = sizeof(base->global_outgoing_address);
3543 		if (!(flags & DNS_OPTION_NAMESERVERS)) return 0;
3544 		if (evutil_parse_sockaddr_port(val,
3545 			(struct sockaddr*)&base->global_outgoing_address, &len))
3546 			return -1;
3547 		base->global_outgoing_addrlen = len;
3548 	} else if (str_matches_option(option, "initial-probe-timeout:")) {
3549 		struct timeval tv;
3550 		if (evdns_strtotimeval(val, &tv) == -1) return -1;
3551 		if (tv.tv_sec > 3600)
3552 			tv.tv_sec = 3600;
3553 		if (!(flags & DNS_OPTION_MISC)) return 0;
3554 		log(EVDNS_LOG_DEBUG, "Setting initial probe timeout to %s",
3555 		    val);
3556 		memcpy(&base->global_nameserver_probe_initial_timeout, &tv,
3557 		    sizeof(tv));
3558 	} else if (str_matches_option(option, "so-rcvbuf:")) {
3559 		int buf = strtoint(val);
3560 		if (buf == -1) return -1;
3561 		if (!(flags & DNS_OPTION_MISC)) return 0;
3562 		log(EVDNS_LOG_DEBUG, "Setting SO_RCVBUF to %s", val);
3563 		base->so_rcvbuf = buf;
3564 	} else if (str_matches_option(option, "so-sndbuf:")) {
3565 		int buf = strtoint(val);
3566 		if (buf == -1) return -1;
3567 		if (!(flags & DNS_OPTION_MISC)) return 0;
3568 		log(EVDNS_LOG_DEBUG, "Setting SO_SNDBUF to %s", val);
3569 		base->so_sndbuf = buf;
3570 	}
3571 	return 0;
3572 }
3573 
3574 int
3575 evdns_set_option(const char *option, const char *val, int flags)
3576 {
3577 	if (!current_base)
3578 		current_base = evdns_base_new(NULL, 0);
3579 	return evdns_base_set_option(current_base, option, val);
3580 }
3581 
3582 static void
3583 resolv_conf_parse_line(struct evdns_base *base, char *const start, int flags) {
3584 	char *strtok_state;
3585 	static const char *const delims = " \t";
3586 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
3587 
3588 
3589 	char *const first_token = strtok_r(start, delims, &strtok_state);
3590 	ASSERT_LOCKED(base);
3591 	if (!first_token) return;
3592 
3593 	if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
3594 		const char *const nameserver = NEXT_TOKEN;
3595 
3596 		if (nameserver)
3597 			evdns_base_nameserver_ip_add(base, nameserver);
3598 	} else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
3599 		const char *const domain = NEXT_TOKEN;
3600 		if (domain) {
3601 			search_postfix_clear(base);
3602 			search_postfix_add(base, domain);
3603 		}
3604 	} else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
3605 		const char *domain;
3606 		search_postfix_clear(base);
3607 
3608 		while ((domain = NEXT_TOKEN)) {
3609 			search_postfix_add(base, domain);
3610 		}
3611 		search_reverse(base);
3612 	} else if (!strcmp(first_token, "options")) {
3613 		const char *option;
3614 		while ((option = NEXT_TOKEN)) {
3615 			const char *val = strchr(option, ':');
3616 			evdns_base_set_option_impl(base, option, val ? val+1 : "", flags);
3617 		}
3618 	}
3619 #undef NEXT_TOKEN
3620 }
3621 
3622 /* exported function */
3623 /* returns: */
3624 /*   0 no errors */
3625 /*   1 failed to open file */
3626 /*   2 failed to stat file */
3627 /*   3 file too large */
3628 /*   4 out of memory */
3629 /*   5 short read from file */
3630 int
3631 evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename) {
3632 	int res;
3633 	EVDNS_LOCK(base);
3634 	res = evdns_base_resolv_conf_parse_impl(base, flags, filename);
3635 	EVDNS_UNLOCK(base);
3636 	return res;
3637 }
3638 
3639 static char *
3640 evdns_get_default_hosts_filename(void)
3641 {
3642 #ifdef _WIN32
3643 	/* Windows is a little coy about where it puts its configuration
3644 	 * files.  Sure, they're _usually_ in C:\windows\system32, but
3645 	 * there's no reason in principle they couldn't be in
3646 	 * W:\hoboken chicken emergency\
3647 	 */
3648 	char path[MAX_PATH+1];
3649 	static const char hostfile[] = "\\drivers\\etc\\hosts";
3650 	char *path_out;
3651 	size_t len_out;
3652 
3653 	if (! SHGetSpecialFolderPathA(NULL, path, CSIDL_SYSTEM, 0))
3654 		return NULL;
3655 	len_out = strlen(path)+strlen(hostfile)+1;
3656 	path_out = mm_malloc(len_out);
3657 	evutil_snprintf(path_out, len_out, "%s%s", path, hostfile);
3658 	return path_out;
3659 #else
3660 	return mm_strdup("/etc/hosts");
3661 #endif
3662 }
3663 
3664 static int
3665 evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename) {
3666 	size_t n;
3667 	char *resolv;
3668 	char *start;
3669 	int err = 0;
3670 	int add_default;
3671 
3672 	log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
3673 
3674 	add_default = flags & DNS_OPTION_NAMESERVERS;
3675 	if (flags & DNS_OPTION_NAMESERVERS_NO_DEFAULT)
3676 		add_default = 0;
3677 
3678 	if (flags & DNS_OPTION_HOSTSFILE) {
3679 		char *fname = evdns_get_default_hosts_filename();
3680 		evdns_base_load_hosts(base, fname);
3681 		if (fname)
3682 			mm_free(fname);
3683 	}
3684 
3685 	if (!filename) {
3686 		evdns_resolv_set_defaults(base, flags);
3687 		return 1;
3688 	}
3689 
3690 	if ((err = evutil_read_file_(filename, &resolv, &n, 0)) < 0) {
3691 		if (err == -1) {
3692 			/* No file. */
3693 			evdns_resolv_set_defaults(base, flags);
3694 			return 1;
3695 		} else {
3696 			return 2;
3697 		}
3698 	}
3699 
3700 	start = resolv;
3701 	for (;;) {
3702 		char *const newline = strchr(start, '\n');
3703 		if (!newline) {
3704 			resolv_conf_parse_line(base, start, flags);
3705 			break;
3706 		} else {
3707 			*newline = 0;
3708 			resolv_conf_parse_line(base, start, flags);
3709 			start = newline + 1;
3710 		}
3711 	}
3712 
3713 	if (!base->server_head && add_default) {
3714 		/* no nameservers were configured. */
3715 		evdns_base_nameserver_ip_add(base, "127.0.0.1");
3716 		err = 6;
3717 	}
3718 	if (flags & DNS_OPTION_SEARCH && (!base->global_search_state || base->global_search_state->num_domains == 0)) {
3719 		search_set_from_hostname(base);
3720 	}
3721 
3722 	mm_free(resolv);
3723 	return err;
3724 }
3725 
3726 int
3727 evdns_resolv_conf_parse(int flags, const char *const filename) {
3728 	if (!current_base)
3729 		current_base = evdns_base_new(NULL, 0);
3730 	return evdns_base_resolv_conf_parse(current_base, flags, filename);
3731 }
3732 
3733 
3734 #ifdef _WIN32
3735 /* Add multiple nameservers from a space-or-comma-separated list. */
3736 static int
3737 evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) {
3738 	const char *addr;
3739 	char *buf;
3740 	int r;
3741 	ASSERT_LOCKED(base);
3742 	while (*ips) {
3743 		while (isspace(*ips) || *ips == ',' || *ips == '\t')
3744 			++ips;
3745 		addr = ips;
3746 		while (isdigit(*ips) || *ips == '.' || *ips == ':' ||
3747 		    *ips=='[' || *ips==']')
3748 			++ips;
3749 		buf = mm_malloc(ips-addr+1);
3750 		if (!buf) return 4;
3751 		memcpy(buf, addr, ips-addr);
3752 		buf[ips-addr] = '\0';
3753 		r = evdns_base_nameserver_ip_add(base, buf);
3754 		mm_free(buf);
3755 		if (r) return r;
3756 	}
3757 	return 0;
3758 }
3759 
3760 typedef DWORD(WINAPI *GetNetworkParams_fn_t)(FIXED_INFO *, DWORD*);
3761 
3762 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
3763 /* figure out what our nameservers are. */
3764 static int
3765 load_nameservers_with_getnetworkparams(struct evdns_base *base)
3766 {
3767 	/* Based on MSDN examples and inspection of  c-ares code. */
3768 	FIXED_INFO *fixed;
3769 	HMODULE handle = 0;
3770 	ULONG size = sizeof(FIXED_INFO);
3771 	void *buf = NULL;
3772 	int status = 0, r, added_any;
3773 	IP_ADDR_STRING *ns;
3774 	GetNetworkParams_fn_t fn;
3775 
3776 	ASSERT_LOCKED(base);
3777 	if (!(handle = evutil_load_windows_system_library_(
3778 			TEXT("iphlpapi.dll")))) {
3779 		log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
3780 		status = -1;
3781 		goto done;
3782 	}
3783 	if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, "GetNetworkParams"))) {
3784 		log(EVDNS_LOG_WARN, "Could not get address of function.");
3785 		status = -1;
3786 		goto done;
3787 	}
3788 
3789 	buf = mm_malloc(size);
3790 	if (!buf) { status = 4; goto done; }
3791 	fixed = buf;
3792 	r = fn(fixed, &size);
3793 	if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
3794 		status = -1;
3795 		goto done;
3796 	}
3797 	if (r != ERROR_SUCCESS) {
3798 		mm_free(buf);
3799 		buf = mm_malloc(size);
3800 		if (!buf) { status = 4; goto done; }
3801 		fixed = buf;
3802 		r = fn(fixed, &size);
3803 		if (r != ERROR_SUCCESS) {
3804 			log(EVDNS_LOG_DEBUG, "fn() failed.");
3805 			status = -1;
3806 			goto done;
3807 		}
3808 	}
3809 
3810 	EVUTIL_ASSERT(fixed);
3811 	added_any = 0;
3812 	ns = &(fixed->DnsServerList);
3813 	while (ns) {
3814 		r = evdns_nameserver_ip_add_line(base, ns->IpAddress.String);
3815 		if (r) {
3816 			log(EVDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
3817 				(ns->IpAddress.String),(int)GetLastError());
3818 			status = r;
3819 		} else {
3820 			++added_any;
3821 			log(EVDNS_LOG_DEBUG,"Successfully added %s as nameserver",ns->IpAddress.String);
3822 		}
3823 
3824 		ns = ns->Next;
3825 	}
3826 
3827 	if (!added_any) {
3828 		log(EVDNS_LOG_DEBUG, "No nameservers added.");
3829 		if (status == 0)
3830 			status = -1;
3831 	} else {
3832 		status = 0;
3833 	}
3834 
3835  done:
3836 	if (buf)
3837 		mm_free(buf);
3838 	if (handle)
3839 		FreeLibrary(handle);
3840 	return status;
3841 }
3842 
3843 static int
3844 config_nameserver_from_reg_key(struct evdns_base *base, HKEY key, const TCHAR *subkey)
3845 {
3846 	char *buf;
3847 	DWORD bufsz = 0, type = 0;
3848 	int status = 0;
3849 
3850 	ASSERT_LOCKED(base);
3851 	if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
3852 	    != ERROR_MORE_DATA)
3853 		return -1;
3854 	if (!(buf = mm_malloc(bufsz)))
3855 		return -1;
3856 
3857 	if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
3858 	    == ERROR_SUCCESS && bufsz > 1) {
3859 		status = evdns_nameserver_ip_add_line(base,buf);
3860 	}
3861 
3862 	mm_free(buf);
3863 	return status;
3864 }
3865 
3866 #define SERVICES_KEY TEXT("System\\CurrentControlSet\\Services\\")
3867 #define WIN_NS_9X_KEY  SERVICES_KEY TEXT("VxD\\MSTCP")
3868 #define WIN_NS_NT_KEY  SERVICES_KEY TEXT("Tcpip\\Parameters")
3869 
3870 static int
3871 load_nameservers_from_registry(struct evdns_base *base)
3872 {
3873 	int found = 0;
3874 	int r;
3875 #define TRY(k, name) \
3876 	if (!found && config_nameserver_from_reg_key(base,k,TEXT(name)) == 0) { \
3877 		log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
3878 		found = 1;						\
3879 	} else if (!found) {						\
3880 		log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
3881 		    #k,#name);						\
3882 	}
3883 
3884 	ASSERT_LOCKED(base);
3885 
3886 	if (((int)GetVersion()) > 0) { /* NT */
3887 		HKEY nt_key = 0, interfaces_key = 0;
3888 
3889 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
3890 				 KEY_READ, &nt_key) != ERROR_SUCCESS) {
3891 			log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
3892 			return -1;
3893 		}
3894 		r = RegOpenKeyEx(nt_key, TEXT("Interfaces"), 0,
3895 			     KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
3896 			     &interfaces_key);
3897 		if (r != ERROR_SUCCESS) {
3898 			log(EVDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
3899 			return -1;
3900 		}
3901 		TRY(nt_key, "NameServer");
3902 		TRY(nt_key, "DhcpNameServer");
3903 		TRY(interfaces_key, "NameServer");
3904 		TRY(interfaces_key, "DhcpNameServer");
3905 		RegCloseKey(interfaces_key);
3906 		RegCloseKey(nt_key);
3907 	} else {
3908 		HKEY win_key = 0;
3909 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
3910 				 KEY_READ, &win_key) != ERROR_SUCCESS) {
3911 			log(EVDNS_LOG_DEBUG, "Couldn't open registry key, %d", (int)GetLastError());
3912 			return -1;
3913 		}
3914 		TRY(win_key, "NameServer");
3915 		RegCloseKey(win_key);
3916 	}
3917 
3918 	if (found == 0) {
3919 		log(EVDNS_LOG_WARN,"Didn't find any nameservers.");
3920 	}
3921 
3922 	return found ? 0 : -1;
3923 #undef TRY
3924 }
3925 
3926 int
3927 evdns_base_config_windows_nameservers(struct evdns_base *base)
3928 {
3929 	int r;
3930 	char *fname;
3931 	if (base == NULL)
3932 		base = current_base;
3933 	if (base == NULL)
3934 		return -1;
3935 	EVDNS_LOCK(base);
3936 	fname = evdns_get_default_hosts_filename();
3937 	log(EVDNS_LOG_DEBUG, "Loading hosts entries from %s", fname);
3938 	evdns_base_load_hosts(base, fname);
3939 	if (fname)
3940 		mm_free(fname);
3941 
3942 	if (load_nameservers_with_getnetworkparams(base) == 0) {
3943 		EVDNS_UNLOCK(base);
3944 		return 0;
3945 	}
3946 	r = load_nameservers_from_registry(base);
3947 
3948 	EVDNS_UNLOCK(base);
3949 	return r;
3950 }
3951 
3952 int
3953 evdns_config_windows_nameservers(void)
3954 {
3955 	if (!current_base) {
3956 		current_base = evdns_base_new(NULL, 1);
3957 		return current_base == NULL ? -1 : 0;
3958 	} else {
3959 		return evdns_base_config_windows_nameservers(current_base);
3960 	}
3961 }
3962 #endif
3963 
3964 struct evdns_base *
3965 evdns_base_new(struct event_base *event_base, int flags)
3966 {
3967 	struct evdns_base *base;
3968 
3969 	if (evutil_secure_rng_init() < 0) {
3970 		log(EVDNS_LOG_WARN, "Unable to seed random number generator; "
3971 		    "DNS can't run.");
3972 		return NULL;
3973 	}
3974 
3975 	/* Give the evutil library a hook into its evdns-enabled
3976 	 * functionality.  We can't just call evdns_getaddrinfo directly or
3977 	 * else libevent-core will depend on libevent-extras. */
3978 	evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo);
3979 	evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel);
3980 
3981 	base = mm_malloc(sizeof(struct evdns_base));
3982 	if (base == NULL)
3983 		return (NULL);
3984 	memset(base, 0, sizeof(struct evdns_base));
3985 	base->req_waiting_head = NULL;
3986 
3987 	EVTHREAD_ALLOC_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
3988 	EVDNS_LOCK(base);
3989 
3990 	/* Set max requests inflight and allocate req_heads. */
3991 	base->req_heads = NULL;
3992 
3993 	evdns_base_set_max_requests_inflight(base, 64);
3994 
3995 	base->server_head = NULL;
3996 	base->event_base = event_base;
3997 	base->global_good_nameservers = base->global_requests_inflight =
3998 		base->global_requests_waiting = 0;
3999 
4000 	base->global_timeout.tv_sec = 5;
4001 	base->global_timeout.tv_usec = 0;
4002 	base->global_max_reissues = 1;
4003 	base->global_max_retransmits = 3;
4004 	base->global_max_nameserver_timeout = 3;
4005 	base->global_search_state = NULL;
4006 	base->global_randomize_case = 1;
4007 	base->global_getaddrinfo_allow_skew.tv_sec = 3;
4008 	base->global_getaddrinfo_allow_skew.tv_usec = 0;
4009 	base->global_nameserver_probe_initial_timeout.tv_sec = 10;
4010 	base->global_nameserver_probe_initial_timeout.tv_usec = 0;
4011 
4012 	TAILQ_INIT(&base->hostsdb);
4013 
4014 #define EVDNS_BASE_ALL_FLAGS ( \
4015 	EVDNS_BASE_INITIALIZE_NAMESERVERS | \
4016 	EVDNS_BASE_DISABLE_WHEN_INACTIVE  | \
4017 	EVDNS_BASE_NAMESERVERS_NO_DEFAULT | \
4018 	0)
4019 
4020 	if (flags & ~EVDNS_BASE_ALL_FLAGS) {
4021 		flags = EVDNS_BASE_INITIALIZE_NAMESERVERS;
4022 		log(EVDNS_LOG_WARN,
4023 		    "Unrecognized flag passed to evdns_base_new(). Assuming "
4024 		    "you meant EVDNS_BASE_INITIALIZE_NAMESERVERS.");
4025 	}
4026 #undef EVDNS_BASE_ALL_FLAGS
4027 
4028 	if (flags & EVDNS_BASE_INITIALIZE_NAMESERVERS) {
4029 		int r;
4030 		int opts = DNS_OPTIONS_ALL;
4031 		if (flags & EVDNS_BASE_NAMESERVERS_NO_DEFAULT) {
4032 			opts |= DNS_OPTION_NAMESERVERS_NO_DEFAULT;
4033 		}
4034 
4035 #ifdef _WIN32
4036 		r = evdns_base_config_windows_nameservers(base);
4037 #else
4038 		r = evdns_base_resolv_conf_parse(base, opts, "/etc/resolv.conf");
4039 #endif
4040 		if (r) {
4041 			evdns_base_free_and_unlock(base, 0);
4042 			return NULL;
4043 		}
4044 	}
4045 	if (flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE) {
4046 		base->disable_when_inactive = 1;
4047 	}
4048 
4049 	EVDNS_UNLOCK(base);
4050 	return base;
4051 }
4052 
4053 int
4054 evdns_init(void)
4055 {
4056 	struct evdns_base *base = evdns_base_new(NULL, 1);
4057 	if (base) {
4058 		current_base = base;
4059 		return 0;
4060 	} else {
4061 		return -1;
4062 	}
4063 }
4064 
4065 const char *
4066 evdns_err_to_string(int err)
4067 {
4068     switch (err) {
4069 	case DNS_ERR_NONE: return "no error";
4070 	case DNS_ERR_FORMAT: return "misformatted query";
4071 	case DNS_ERR_SERVERFAILED: return "server failed";
4072 	case DNS_ERR_NOTEXIST: return "name does not exist";
4073 	case DNS_ERR_NOTIMPL: return "query not implemented";
4074 	case DNS_ERR_REFUSED: return "refused";
4075 
4076 	case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
4077 	case DNS_ERR_UNKNOWN: return "unknown";
4078 	case DNS_ERR_TIMEOUT: return "request timed out";
4079 	case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
4080 	case DNS_ERR_CANCEL: return "dns request canceled";
4081 	case DNS_ERR_NODATA: return "no records in the reply";
4082 	default: return "[Unknown error code]";
4083     }
4084 }
4085 
4086 static void
4087 evdns_nameserver_free(struct nameserver *server)
4088 {
4089 	if (server->socket >= 0)
4090 		evutil_closesocket(server->socket);
4091 	(void) event_del(&server->event);
4092 	event_debug_unassign(&server->event);
4093 	if (server->state == 0)
4094 		(void) event_del(&server->timeout_event);
4095 	if (server->probe_request) {
4096 		evdns_cancel_request(server->base, server->probe_request);
4097 		server->probe_request = NULL;
4098 	}
4099 	event_debug_unassign(&server->timeout_event);
4100 	mm_free(server);
4101 }
4102 
4103 static void
4104 evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests)
4105 {
4106 	struct nameserver *server, *server_next;
4107 	struct search_domain *dom, *dom_next;
4108 	int i;
4109 
4110 	/* Requires that we hold the lock. */
4111 
4112 	/* TODO(nickm) we might need to refcount here. */
4113 
4114 	while (base->req_waiting_head) {
4115 		if (fail_requests)
4116 			reply_schedule_callback(base->req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
4117 		request_finished(base->req_waiting_head, &base->req_waiting_head, 1);
4118 	}
4119 	for (i = 0; i < base->n_req_heads; ++i) {
4120 		while (base->req_heads[i]) {
4121 			if (fail_requests)
4122 				reply_schedule_callback(base->req_heads[i], 0, DNS_ERR_SHUTDOWN, NULL);
4123 			request_finished(base->req_heads[i], &REQ_HEAD(base, base->req_heads[i]->trans_id), 1);
4124 		}
4125 	}
4126 	base->global_requests_inflight = base->global_requests_waiting = 0;
4127 
4128 	for (server = base->server_head; server; server = server_next) {
4129 		server_next = server->next;
4130 		/** already done something before */
4131 		server->probe_request = NULL;
4132 		evdns_nameserver_free(server);
4133 		if (server_next == base->server_head)
4134 			break;
4135 	}
4136 	base->server_head = NULL;
4137 	base->global_good_nameservers = 0;
4138 
4139 	if (base->global_search_state) {
4140 		for (dom = base->global_search_state->head; dom; dom = dom_next) {
4141 			dom_next = dom->next;
4142 			mm_free(dom);
4143 		}
4144 		mm_free(base->global_search_state);
4145 		base->global_search_state = NULL;
4146 	}
4147 
4148 	{
4149 		struct hosts_entry *victim;
4150 		while ((victim = TAILQ_FIRST(&base->hostsdb))) {
4151 			TAILQ_REMOVE(&base->hostsdb, victim, next);
4152 			mm_free(victim);
4153 		}
4154 	}
4155 
4156 	mm_free(base->req_heads);
4157 
4158 	EVDNS_UNLOCK(base);
4159 	EVTHREAD_FREE_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
4160 
4161 	mm_free(base);
4162 }
4163 
4164 void
4165 evdns_base_free(struct evdns_base *base, int fail_requests)
4166 {
4167 	EVDNS_LOCK(base);
4168 	evdns_base_free_and_unlock(base, fail_requests);
4169 }
4170 
4171 void
4172 evdns_base_clear_host_addresses(struct evdns_base *base)
4173 {
4174 	struct hosts_entry *victim;
4175 	EVDNS_LOCK(base);
4176 	while ((victim = TAILQ_FIRST(&base->hostsdb))) {
4177 		TAILQ_REMOVE(&base->hostsdb, victim, next);
4178 		mm_free(victim);
4179 	}
4180 	EVDNS_UNLOCK(base);
4181 }
4182 
4183 void
4184 evdns_shutdown(int fail_requests)
4185 {
4186 	if (current_base) {
4187 		struct evdns_base *b = current_base;
4188 		current_base = NULL;
4189 		evdns_base_free(b, fail_requests);
4190 	}
4191 	evdns_log_fn = NULL;
4192 }
4193 
4194 static int
4195 evdns_base_parse_hosts_line(struct evdns_base *base, char *line)
4196 {
4197 	char *strtok_state;
4198 	static const char *const delims = " \t";
4199 	char *const addr = strtok_r(line, delims, &strtok_state);
4200 	char *hostname, *hash;
4201 	struct sockaddr_storage ss;
4202 	int socklen = sizeof(ss);
4203 	ASSERT_LOCKED(base);
4204 
4205 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
4206 
4207 	if (!addr || *addr == '#')
4208 		return 0;
4209 
4210 	memset(&ss, 0, sizeof(ss));
4211 	if (evutil_parse_sockaddr_port(addr, (struct sockaddr*)&ss, &socklen)<0)
4212 		return -1;
4213 	if (socklen > (int)sizeof(struct sockaddr_in6))
4214 		return -1;
4215 
4216 	if (sockaddr_getport((struct sockaddr*)&ss))
4217 		return -1;
4218 
4219 	while ((hostname = NEXT_TOKEN)) {
4220 		struct hosts_entry *he;
4221 		size_t namelen;
4222 		if ((hash = strchr(hostname, '#'))) {
4223 			if (hash == hostname)
4224 				return 0;
4225 			*hash = '\0';
4226 		}
4227 
4228 		namelen = strlen(hostname);
4229 
4230 		he = mm_calloc(1, sizeof(struct hosts_entry)+namelen);
4231 		if (!he)
4232 			return -1;
4233 		EVUTIL_ASSERT(socklen <= (int)sizeof(he->addr));
4234 		memcpy(&he->addr, &ss, socklen);
4235 		memcpy(he->hostname, hostname, namelen+1);
4236 		he->addrlen = socklen;
4237 
4238 		TAILQ_INSERT_TAIL(&base->hostsdb, he, next);
4239 
4240 		if (hash)
4241 			return 0;
4242 	}
4243 
4244 	return 0;
4245 #undef NEXT_TOKEN
4246 }
4247 
4248 static int
4249 evdns_base_load_hosts_impl(struct evdns_base *base, const char *hosts_fname)
4250 {
4251 	char *str=NULL, *cp, *eol;
4252 	size_t len;
4253 	int err=0;
4254 
4255 	ASSERT_LOCKED(base);
4256 
4257 	if (hosts_fname == NULL ||
4258 	    (err = evutil_read_file_(hosts_fname, &str, &len, 0)) < 0) {
4259 		char tmp[64];
4260 		strlcpy(tmp, "127.0.0.1   localhost", sizeof(tmp));
4261 		evdns_base_parse_hosts_line(base, tmp);
4262 		strlcpy(tmp, "::1   localhost", sizeof(tmp));
4263 		evdns_base_parse_hosts_line(base, tmp);
4264 		return err ? -1 : 0;
4265 	}
4266 
4267 	/* This will break early if there is a NUL in the hosts file.
4268 	 * Probably not a problem.*/
4269 	cp = str;
4270 	for (;;) {
4271 		eol = strchr(cp, '\n');
4272 
4273 		if (eol) {
4274 			*eol = '\0';
4275 			evdns_base_parse_hosts_line(base, cp);
4276 			cp = eol+1;
4277 		} else {
4278 			evdns_base_parse_hosts_line(base, cp);
4279 			break;
4280 		}
4281 	}
4282 
4283 	mm_free(str);
4284 	return 0;
4285 }
4286 
4287 int
4288 evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname)
4289 {
4290 	int res;
4291 	if (!base)
4292 		base = current_base;
4293 	EVDNS_LOCK(base);
4294 	res = evdns_base_load_hosts_impl(base, hosts_fname);
4295 	EVDNS_UNLOCK(base);
4296 	return res;
4297 }
4298 
4299 /* A single request for a getaddrinfo, either v4 or v6. */
4300 struct getaddrinfo_subrequest {
4301 	struct evdns_request *r;
4302 	ev_uint32_t type;
4303 };
4304 
4305 /* State data used to implement an in-progress getaddrinfo. */
4306 struct evdns_getaddrinfo_request {
4307 	struct evdns_base *evdns_base;
4308 	/* Copy of the modified 'hints' data that we'll use to build
4309 	 * answers. */
4310 	struct evutil_addrinfo hints;
4311 	/* The callback to invoke when we're done */
4312 	evdns_getaddrinfo_cb user_cb;
4313 	/* User-supplied data to give to the callback. */
4314 	void *user_data;
4315 	/* The port to use when building sockaddrs. */
4316 	ev_uint16_t port;
4317 	/* The sub_request for an A record (if any) */
4318 	struct getaddrinfo_subrequest ipv4_request;
4319 	/* The sub_request for an AAAA record (if any) */
4320 	struct getaddrinfo_subrequest ipv6_request;
4321 
4322 	/* The cname result that we were told (if any) */
4323 	char *cname_result;
4324 
4325 	/* If we have one request answered and one request still inflight,
4326 	 * then this field holds the answer from the first request... */
4327 	struct evutil_addrinfo *pending_result;
4328 	/* And this event is a timeout that will tell us to cancel the second
4329 	 * request if it's taking a long time. */
4330 	struct event timeout;
4331 
4332 	/* And this field holds the error code from the first request... */
4333 	int pending_error;
4334 	/* If this is set, the user canceled this request. */
4335 	unsigned user_canceled : 1;
4336 	/* If this is set, the user can no longer cancel this request; we're
4337 	 * just waiting for the free. */
4338 	unsigned request_done : 1;
4339 };
4340 
4341 /* Convert an evdns errors to the equivalent getaddrinfo error. */
4342 static int
4343 evdns_err_to_getaddrinfo_err(int e1)
4344 {
4345 	/* XXX Do this better! */
4346 	if (e1 == DNS_ERR_NONE)
4347 		return 0;
4348 	else if (e1 == DNS_ERR_NOTEXIST)
4349 		return EVUTIL_EAI_NONAME;
4350 	else
4351 		return EVUTIL_EAI_FAIL;
4352 }
4353 
4354 /* Return the more informative of two getaddrinfo errors. */
4355 static int
4356 getaddrinfo_merge_err(int e1, int e2)
4357 {
4358 	/* XXXX be cleverer here. */
4359 	if (e1 == 0)
4360 		return e2;
4361 	else
4362 		return e1;
4363 }
4364 
4365 static void
4366 free_getaddrinfo_request(struct evdns_getaddrinfo_request *data)
4367 {
4368 	/* DO NOT CALL this if either of the requests is pending.  Only once
4369 	 * both callbacks have been invoked is it safe to free the request */
4370 	if (data->pending_result)
4371 		evutil_freeaddrinfo(data->pending_result);
4372 	if (data->cname_result)
4373 		mm_free(data->cname_result);
4374 	event_del(&data->timeout);
4375 	mm_free(data);
4376 	return;
4377 }
4378 
4379 static void
4380 add_cname_to_reply(struct evdns_getaddrinfo_request *data,
4381     struct evutil_addrinfo *ai)
4382 {
4383 	if (data->cname_result && ai) {
4384 		ai->ai_canonname = data->cname_result;
4385 		data->cname_result = NULL;
4386 	}
4387 }
4388 
4389 /* Callback: invoked when one request in a mixed-format A/AAAA getaddrinfo
4390  * request has finished, but the other one took too long to answer. Pass
4391  * along the answer we got, and cancel the other request.
4392  */
4393 static void
4394 evdns_getaddrinfo_timeout_cb(evutil_socket_t fd, short what, void *ptr)
4395 {
4396 	int v4_timedout = 0, v6_timedout = 0;
4397 	struct evdns_getaddrinfo_request *data = ptr;
4398 
4399 	/* Cancel any pending requests, and note which one */
4400 	if (data->ipv4_request.r) {
4401 		/* XXXX This does nothing if the request's callback is already
4402 		 * running (pending_cb is set). */
4403 		evdns_cancel_request(NULL, data->ipv4_request.r);
4404 		v4_timedout = 1;
4405 		EVDNS_LOCK(data->evdns_base);
4406 		++data->evdns_base->getaddrinfo_ipv4_timeouts;
4407 		EVDNS_UNLOCK(data->evdns_base);
4408 	}
4409 	if (data->ipv6_request.r) {
4410 		/* XXXX This does nothing if the request's callback is already
4411 		 * running (pending_cb is set). */
4412 		evdns_cancel_request(NULL, data->ipv6_request.r);
4413 		v6_timedout = 1;
4414 		EVDNS_LOCK(data->evdns_base);
4415 		++data->evdns_base->getaddrinfo_ipv6_timeouts;
4416 		EVDNS_UNLOCK(data->evdns_base);
4417 	}
4418 
4419 	/* We only use this timeout callback when we have an answer for
4420 	 * one address. */
4421 	EVUTIL_ASSERT(!v4_timedout || !v6_timedout);
4422 
4423 	/* Report the outcome of the other request that didn't time out. */
4424 	if (data->pending_result) {
4425 		add_cname_to_reply(data, data->pending_result);
4426 		data->user_cb(0, data->pending_result, data->user_data);
4427 		data->pending_result = NULL;
4428 	} else {
4429 		int e = data->pending_error;
4430 		if (!e)
4431 			e = EVUTIL_EAI_AGAIN;
4432 		data->user_cb(e, NULL, data->user_data);
4433 	}
4434 
4435 	data->user_cb = NULL; /* prevent double-call if evdns callbacks are
4436 			       * in-progress. XXXX It would be better if this
4437 			       * weren't necessary. */
4438 
4439 	if (!v4_timedout && !v6_timedout) {
4440 		/* should be impossible? XXXX */
4441 		free_getaddrinfo_request(data);
4442 	}
4443 }
4444 
4445 static int
4446 evdns_getaddrinfo_set_timeout(struct evdns_base *evdns_base,
4447     struct evdns_getaddrinfo_request *data)
4448 {
4449 	return event_add(&data->timeout, &evdns_base->global_getaddrinfo_allow_skew);
4450 }
4451 
4452 static inline int
4453 evdns_result_is_answer(int result)
4454 {
4455 	return (result != DNS_ERR_NOTIMPL && result != DNS_ERR_REFUSED &&
4456 	    result != DNS_ERR_SERVERFAILED && result != DNS_ERR_CANCEL);
4457 }
4458 
4459 static void
4460 evdns_getaddrinfo_gotresolve(int result, char type, int count,
4461     int ttl, void *addresses, void *arg)
4462 {
4463 	int i;
4464 	struct getaddrinfo_subrequest *req = arg;
4465 	struct getaddrinfo_subrequest *other_req;
4466 	struct evdns_getaddrinfo_request *data;
4467 
4468 	struct evutil_addrinfo *res;
4469 
4470 	struct sockaddr_in sin;
4471 	struct sockaddr_in6 sin6;
4472 	struct sockaddr *sa;
4473 	int socklen, addrlen;
4474 	void *addrp;
4475 	int err;
4476 	int user_canceled;
4477 
4478 	EVUTIL_ASSERT(req->type == DNS_IPv4_A || req->type == DNS_IPv6_AAAA);
4479 	if (req->type == DNS_IPv4_A) {
4480 		data = EVUTIL_UPCAST(req, struct evdns_getaddrinfo_request, ipv4_request);
4481 		other_req = &data->ipv6_request;
4482 	} else {
4483 		data = EVUTIL_UPCAST(req, struct evdns_getaddrinfo_request, ipv6_request);
4484 		other_req = &data->ipv4_request;
4485 	}
4486 
4487 	/** Called from evdns_base_free() with @fail_requests == 1 */
4488 	if (result != DNS_ERR_SHUTDOWN) {
4489 		EVDNS_LOCK(data->evdns_base);
4490 		if (evdns_result_is_answer(result)) {
4491 			if (req->type == DNS_IPv4_A)
4492 				++data->evdns_base->getaddrinfo_ipv4_answered;
4493 			else
4494 				++data->evdns_base->getaddrinfo_ipv6_answered;
4495 		}
4496 		user_canceled = data->user_canceled;
4497 		if (other_req->r == NULL)
4498 			data->request_done = 1;
4499 		EVDNS_UNLOCK(data->evdns_base);
4500 	} else {
4501 		data->evdns_base = NULL;
4502 		user_canceled = data->user_canceled;
4503 	}
4504 
4505 	req->r = NULL;
4506 
4507 	if (result == DNS_ERR_CANCEL && ! user_canceled) {
4508 		/* Internal cancel request from timeout or internal error.
4509 		 * we already answered the user. */
4510 		if (other_req->r == NULL)
4511 			free_getaddrinfo_request(data);
4512 		return;
4513 	}
4514 
4515 	if (data->user_cb == NULL) {
4516 		/* We already answered.  XXXX This shouldn't be needed; see
4517 		 * comments in evdns_getaddrinfo_timeout_cb */
4518 		free_getaddrinfo_request(data);
4519 		return;
4520 	}
4521 
4522 	if (result == DNS_ERR_NONE) {
4523 		if (count == 0)
4524 			err = EVUTIL_EAI_NODATA;
4525 		else
4526 			err = 0;
4527 	} else {
4528 		err = evdns_err_to_getaddrinfo_err(result);
4529 	}
4530 
4531 	if (err) {
4532 		/* Looks like we got an error. */
4533 		if (other_req->r) {
4534 			/* The other request is still working; maybe it will
4535 			 * succeed. */
4536 			/* XXXX handle failure from set_timeout */
4537 			if (result != DNS_ERR_SHUTDOWN) {
4538 				evdns_getaddrinfo_set_timeout(data->evdns_base, data);
4539 			}
4540 			data->pending_error = err;
4541 			return;
4542 		}
4543 
4544 		if (user_canceled) {
4545 			data->user_cb(EVUTIL_EAI_CANCEL, NULL, data->user_data);
4546 		} else if (data->pending_result) {
4547 			/* If we have an answer waiting, and we weren't
4548 			 * canceled, ignore this error. */
4549 			add_cname_to_reply(data, data->pending_result);
4550 			data->user_cb(0, data->pending_result, data->user_data);
4551 			data->pending_result = NULL;
4552 		} else {
4553 			if (data->pending_error)
4554 				err = getaddrinfo_merge_err(err,
4555 				    data->pending_error);
4556 			data->user_cb(err, NULL, data->user_data);
4557 		}
4558 		free_getaddrinfo_request(data);
4559 		return;
4560 	} else if (user_canceled) {
4561 		if (other_req->r) {
4562 			/* The other request is still working; let it hit this
4563 			 * callback with EVUTIL_EAI_CANCEL callback and report
4564 			 * the failure. */
4565 			return;
4566 		}
4567 		data->user_cb(EVUTIL_EAI_CANCEL, NULL, data->user_data);
4568 		free_getaddrinfo_request(data);
4569 		return;
4570 	}
4571 
4572 	/* Looks like we got some answers. We should turn them into addrinfos
4573 	 * and then either queue those or return them all. */
4574 	EVUTIL_ASSERT(type == DNS_IPv4_A || type == DNS_IPv6_AAAA);
4575 
4576 	if (type == DNS_IPv4_A) {
4577 		memset(&sin, 0, sizeof(sin));
4578 		sin.sin_family = AF_INET;
4579 		sin.sin_port = htons(data->port);
4580 
4581 		sa = (struct sockaddr *)&sin;
4582 		socklen = sizeof(sin);
4583 		addrlen = 4;
4584 		addrp = &sin.sin_addr.s_addr;
4585 	} else {
4586 		memset(&sin6, 0, sizeof(sin6));
4587 		sin6.sin6_family = AF_INET6;
4588 		sin6.sin6_port = htons(data->port);
4589 
4590 		sa = (struct sockaddr *)&sin6;
4591 		socklen = sizeof(sin6);
4592 		addrlen = 16;
4593 		addrp = &sin6.sin6_addr.s6_addr;
4594 	}
4595 
4596 	res = NULL;
4597 	for (i=0; i < count; ++i) {
4598 		struct evutil_addrinfo *ai;
4599 		memcpy(addrp, ((char*)addresses)+i*addrlen, addrlen);
4600 		ai = evutil_new_addrinfo_(sa, socklen, &data->hints);
4601 		if (!ai) {
4602 			if (other_req->r) {
4603 				evdns_cancel_request(NULL, other_req->r);
4604 			}
4605 			data->user_cb(EVUTIL_EAI_MEMORY, NULL, data->user_data);
4606 			if (res)
4607 				evutil_freeaddrinfo(res);
4608 
4609 			if (other_req->r == NULL)
4610 				free_getaddrinfo_request(data);
4611 			return;
4612 		}
4613 		res = evutil_addrinfo_append_(res, ai);
4614 	}
4615 
4616 	if (other_req->r) {
4617 		/* The other request is still in progress; wait for it */
4618 		/* XXXX handle failure from set_timeout */
4619 		evdns_getaddrinfo_set_timeout(data->evdns_base, data);
4620 		data->pending_result = res;
4621 		return;
4622 	} else {
4623 		/* The other request is done or never started; append its
4624 		 * results (if any) and return them. */
4625 		if (data->pending_result) {
4626 			if (req->type == DNS_IPv4_A)
4627 				res = evutil_addrinfo_append_(res,
4628 				    data->pending_result);
4629 			else
4630 				res = evutil_addrinfo_append_(
4631 				    data->pending_result, res);
4632 			data->pending_result = NULL;
4633 		}
4634 
4635 		/* Call the user callback. */
4636 		add_cname_to_reply(data, res);
4637 		data->user_cb(0, res, data->user_data);
4638 
4639 		/* Free data. */
4640 		free_getaddrinfo_request(data);
4641 	}
4642 }
4643 
4644 static struct hosts_entry *
4645 find_hosts_entry(struct evdns_base *base, const char *hostname,
4646     struct hosts_entry *find_after)
4647 {
4648 	struct hosts_entry *e;
4649 
4650 	if (find_after)
4651 		e = TAILQ_NEXT(find_after, next);
4652 	else
4653 		e = TAILQ_FIRST(&base->hostsdb);
4654 
4655 	for (; e; e = TAILQ_NEXT(e, next)) {
4656 		if (!evutil_ascii_strcasecmp(e->hostname, hostname))
4657 			return e;
4658 	}
4659 	return NULL;
4660 }
4661 
4662 static int
4663 evdns_getaddrinfo_fromhosts(struct evdns_base *base,
4664     const char *nodename, struct evutil_addrinfo *hints, ev_uint16_t port,
4665     struct evutil_addrinfo **res)
4666 {
4667 	int n_found = 0;
4668 	struct hosts_entry *e;
4669 	struct evutil_addrinfo *ai=NULL;
4670 	int f = hints->ai_family;
4671 
4672 	EVDNS_LOCK(base);
4673 	for (e = find_hosts_entry(base, nodename, NULL); e;
4674 	    e = find_hosts_entry(base, nodename, e)) {
4675 		struct evutil_addrinfo *ai_new;
4676 		++n_found;
4677 		if ((e->addr.sa.sa_family == AF_INET && f == PF_INET6) ||
4678 		    (e->addr.sa.sa_family == AF_INET6 && f == PF_INET))
4679 			continue;
4680 		ai_new = evutil_new_addrinfo_(&e->addr.sa, e->addrlen, hints);
4681 		if (!ai_new) {
4682 			n_found = 0;
4683 			goto out;
4684 		}
4685 		sockaddr_setport(ai_new->ai_addr, port);
4686 		ai = evutil_addrinfo_append_(ai, ai_new);
4687 	}
4688 	EVDNS_UNLOCK(base);
4689 out:
4690 	if (n_found) {
4691 		/* Note that we return an empty answer if we found entries for
4692 		 * this hostname but none were of the right address type. */
4693 		*res = ai;
4694 		return 0;
4695 	} else {
4696 		if (ai)
4697 			evutil_freeaddrinfo(ai);
4698 		return -1;
4699 	}
4700 }
4701 
4702 struct evdns_getaddrinfo_request *
4703 evdns_getaddrinfo(struct evdns_base *dns_base,
4704     const char *nodename, const char *servname,
4705     const struct evutil_addrinfo *hints_in,
4706     evdns_getaddrinfo_cb cb, void *arg)
4707 {
4708 	struct evdns_getaddrinfo_request *data;
4709 	struct evutil_addrinfo hints;
4710 	struct evutil_addrinfo *res = NULL;
4711 	int err;
4712 	int port = 0;
4713 	int want_cname = 0;
4714 	int started = 0;
4715 
4716 	if (!dns_base) {
4717 		dns_base = current_base;
4718 		if (!dns_base) {
4719 			log(EVDNS_LOG_WARN,
4720 			    "Call to getaddrinfo_async with no "
4721 			    "evdns_base configured.");
4722 			cb(EVUTIL_EAI_FAIL, NULL, arg); /* ??? better error? */
4723 			return NULL;
4724 		}
4725 	}
4726 
4727 	/* If we _must_ answer this immediately, do so. */
4728 	if ((hints_in && (hints_in->ai_flags & EVUTIL_AI_NUMERICHOST))) {
4729 		res = NULL;
4730 		err = evutil_getaddrinfo(nodename, servname, hints_in, &res);
4731 		cb(err, res, arg);
4732 		return NULL;
4733 	}
4734 
4735 	if (hints_in) {
4736 		memcpy(&hints, hints_in, sizeof(hints));
4737 	} else {
4738 		memset(&hints, 0, sizeof(hints));
4739 		hints.ai_family = PF_UNSPEC;
4740 	}
4741 
4742 	evutil_adjust_hints_for_addrconfig_(&hints);
4743 
4744 	/* Now try to see if we _can_ answer immediately. */
4745 	/* (It would be nice to do this by calling getaddrinfo directly, with
4746 	 * AI_NUMERICHOST, on plaforms that have it, but we can't: there isn't
4747 	 * a reliable way to distinguish the "that wasn't a numeric host!" case
4748 	 * from any other EAI_NONAME cases.) */
4749 	err = evutil_getaddrinfo_common_(nodename, servname, &hints, &res, &port);
4750 	if (err != EVUTIL_EAI_NEED_RESOLVE) {
4751 		cb(err, res, arg);
4752 		return NULL;
4753 	}
4754 
4755 	/* If there is an entry in the hosts file, we should give it now. */
4756 	if (!evdns_getaddrinfo_fromhosts(dns_base, nodename, &hints, port, &res)) {
4757 		cb(0, res, arg);
4758 		return NULL;
4759 	}
4760 
4761 	/* Okay, things are serious now. We're going to need to actually
4762 	 * launch a request.
4763 	 */
4764 	data = mm_calloc(1,sizeof(struct evdns_getaddrinfo_request));
4765 	if (!data) {
4766 		cb(EVUTIL_EAI_MEMORY, NULL, arg);
4767 		return NULL;
4768 	}
4769 
4770 	memcpy(&data->hints, &hints, sizeof(data->hints));
4771 	data->port = (ev_uint16_t)port;
4772 	data->ipv4_request.type = DNS_IPv4_A;
4773 	data->ipv6_request.type = DNS_IPv6_AAAA;
4774 	data->user_cb = cb;
4775 	data->user_data = arg;
4776 	data->evdns_base = dns_base;
4777 
4778 	want_cname = (hints.ai_flags & EVUTIL_AI_CANONNAME);
4779 
4780 	/* If we are asked for a PF_UNSPEC address, we launch two requests in
4781 	 * parallel: one for an A address and one for an AAAA address.  We
4782 	 * can't send just one request, since many servers only answer one
4783 	 * question per DNS request.
4784 	 *
4785 	 * Once we have the answer to one request, we allow for a short
4786 	 * timeout before we report it, to see if the other one arrives.  If
4787 	 * they both show up in time, then we report both the answers.
4788 	 *
4789 	 * If too many addresses of one type time out or fail, we should stop
4790 	 * launching those requests. (XXX we don't do that yet.)
4791 	 */
4792 
4793 	EVDNS_LOCK(dns_base);
4794 
4795 	if (hints.ai_family != PF_INET6) {
4796 		log(EVDNS_LOG_DEBUG, "Sending request for %s on ipv4 as %p",
4797 		    nodename, &data->ipv4_request);
4798 
4799 		data->ipv4_request.r = evdns_base_resolve_ipv4(dns_base,
4800 		    nodename, 0, evdns_getaddrinfo_gotresolve,
4801 		    &data->ipv4_request);
4802 		if (want_cname && data->ipv4_request.r)
4803 			data->ipv4_request.r->current_req->put_cname_in_ptr =
4804 			    &data->cname_result;
4805 	}
4806 	if (hints.ai_family != PF_INET) {
4807 		log(EVDNS_LOG_DEBUG, "Sending request for %s on ipv6 as %p",
4808 		    nodename, &data->ipv6_request);
4809 
4810 		data->ipv6_request.r = evdns_base_resolve_ipv6(dns_base,
4811 		    nodename, 0, evdns_getaddrinfo_gotresolve,
4812 		    &data->ipv6_request);
4813 		if (want_cname && data->ipv6_request.r)
4814 			data->ipv6_request.r->current_req->put_cname_in_ptr =
4815 			    &data->cname_result;
4816 	}
4817 
4818 	evtimer_assign(&data->timeout, dns_base->event_base,
4819 	    evdns_getaddrinfo_timeout_cb, data);
4820 
4821 	started = (data->ipv4_request.r || data->ipv6_request.r);
4822 
4823 	EVDNS_UNLOCK(dns_base);
4824 
4825 	if (started) {
4826 		return data;
4827 	} else {
4828 		mm_free(data);
4829 		cb(EVUTIL_EAI_FAIL, NULL, arg);
4830 		return NULL;
4831 	}
4832 }
4833 
4834 void
4835 evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *data)
4836 {
4837 	EVDNS_LOCK(data->evdns_base);
4838 	if (data->request_done) {
4839 		EVDNS_UNLOCK(data->evdns_base);
4840 		return;
4841 	}
4842 	event_del(&data->timeout);
4843 	data->user_canceled = 1;
4844 	if (data->ipv4_request.r)
4845 		evdns_cancel_request(data->evdns_base, data->ipv4_request.r);
4846 	if (data->ipv6_request.r)
4847 		evdns_cancel_request(data->evdns_base, data->ipv6_request.r);
4848 	EVDNS_UNLOCK(data->evdns_base);
4849 }
4850