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