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