]> git.llucax.com Git - software/libev.git/blob - evdns.c
*** empty log message ***
[software/libev.git] / evdns.c
1 /* $Id: evdns.c,v 1.19 2007-11-06 18:49:32 root Exp $ */
2
3 /* The original version of this module was written by Adam Langley; for
4  * a history of modifications, check out the subversion logs.
5  *
6  * When editing this module, try to keep it re-mergeable by Adam.  Don't
7  * reformat the whitespace, add Tor dependencies, or so on.
8  *
9  * TODO:
10  *   - Support IPv6 and PTR records.
11  *   - Replace all externally visible magic numbers with #defined constants.
12  *   - Write doccumentation for APIs of all external functions.
13  */
14
15 /* Async DNS Library
16  * Adam Langley <agl@imperialviolet.org>
17  * http://www.imperialviolet.org/eventdns.html
18  * Public Domain code
19  *
20  * This software is Public Domain. To view a copy of the public domain dedication,
21  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
22  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
23  *
24  * I ask and expect, but do not require, that all derivative works contain an
25  * attribution similar to:
26  *      Parts developed by Adam Langley <agl@imperialviolet.org>
27  *
28  * You may wish to replace the word "Parts" with something else depending on
29  * the amount of original code.
30  *
31  * (Derivative works does not include programs which link against, run or include
32  * the source verbatim in their source distributions)
33  *
34  * Version: 0.1b
35  */
36
37 #include <sys/types.h>
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #ifdef WIN32
43 #ifndef EV_STANDALONE
44 #include "misc.h"
45 #endif
46 #endif
47
48 /* #define NDEBUG */
49
50 #ifndef DNS_USE_CPU_CLOCK_FOR_ID
51 #ifndef DNS_USE_GETTIMEOFDAY_FOR_ID
52 #ifndef DNS_USE_OPENSSL_FOR_ID
53 #error Must configure at least one id generation method.
54 #error Please see the documentation.
55 #endif
56 #endif
57 #endif
58
59 /* #define _POSIX_C_SOURCE 200507 */
60 #define _GNU_SOURCE
61
62 #ifdef DNS_USE_CPU_CLOCK_FOR_ID
63 #ifdef DNS_USE_OPENSSL_FOR_ID
64 #error Multiple id options selected
65 #endif
66 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
67 #error Multiple id options selected
68 #endif
69 #include <time.h>
70 #endif
71
72 #ifdef DNS_USE_OPENSSL_FOR_ID
73 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
74 #error Multiple id options selected
75 #endif
76 #include <openssl/rand.h>
77 #endif
78
79 #define _FORTIFY_SOURCE 3
80
81 #include <string.h>
82 #include <fcntl.h>
83 #include <sys/time.h>
84 #ifdef HAVE_STDINT_H
85 #include <stdint.h>
86 #endif
87 #include <stdlib.h>
88 #include <string.h>
89 #include <errno.h>
90 #include <assert.h>
91 #include <unistd.h>
92 #include <limits.h>
93 #include <sys/stat.h>
94 #include <ctype.h>
95 #include <stdio.h>
96 #include <stdarg.h>
97
98 #include "evdns.h"
99 #ifdef WIN32
100 #include <windows.h>
101 #include <winsock2.h>
102 #include <iphlpapi.h>
103 #else
104 #include <sys/socket.h>
105 #include <netinet/in.h>
106 #include <arpa/inet.h>
107 #endif
108
109 #ifdef HAVE_NETINET_IN6_H
110 #include <netinet/in6.h>
111 #endif
112
113 #ifdef WIN32
114 typedef int socklen_t;
115 #endif
116
117 #define EVDNS_LOG_DEBUG 0
118 #define EVDNS_LOG_WARN 1
119
120 #ifndef HOST_NAME_MAX
121 #define HOST_NAME_MAX 255
122 #endif
123
124 #ifndef NDEBUG
125 #include <stdio.h>
126 #endif
127
128 #undef MIN
129 #define MIN(a,b) ((a)<(b)?(a):(b))
130
131 #ifdef __USE_ISOC99B
132 /* libevent doesn't work without this */
133 typedef uint8_t u_char;
134 typedef unsigned int uint;
135 #endif
136 #include <event.h>
137
138 #define u64 uint64_t
139 #define u32 uint32_t
140 #define u16 uint16_t
141 #define u8  uint8_t
142
143 #define MAX_ADDRS 4  /* maximum number of addresses from a single packet */
144 /* which we bother recording */
145
146 #define TYPE_A         EVDNS_TYPE_A
147 #define TYPE_CNAME     5
148 #define TYPE_PTR       EVDNS_TYPE_PTR
149 #define TYPE_AAAA      EVDNS_TYPE_AAAA
150
151 #define CLASS_INET     EVDNS_CLASS_INET
152
153 struct request {
154         u8 *request;  /* the dns packet data */
155         unsigned int request_len;
156         int reissue_count;
157         int tx_count;  /* the number of times that this packet has been sent */
158         unsigned int request_type; /* TYPE_PTR or TYPE_A */
159         void *user_pointer;  /* the pointer given to us for this request */
160         evdns_callback_type user_callback;
161         struct nameserver *ns;  /* the server which we last sent it */
162
163         /* elements used by the searching code */
164         int search_index;
165         struct search_state *search_state;
166         char *search_origname;  /* needs to be free()ed */
167         int search_flags;
168
169         /* these objects are kept in a circular list */
170         struct request *next, *prev;
171
172         struct event timeout_event;
173
174         u16 trans_id;  /* the transaction id */
175         char request_appended;  /* true if the request pointer is data which follows this struct */
176         char transmit_me;  /* needs to be transmitted */
177 };
178
179 #ifndef HAVE_STRUCT_IN6_ADDR
180 struct in6_addr {
181         u8 s6_addr[16];
182 };
183 #endif
184
185 struct reply {
186         unsigned int type;
187         unsigned int have_answer;
188         union {
189                 struct {
190                         u32 addrcount;
191                         u32 addresses[MAX_ADDRS];
192                 } a;
193                 struct {
194                         u32 addrcount;
195                         struct in6_addr addresses[MAX_ADDRS];
196                 } aaaa;
197                 struct {
198                         char name[HOST_NAME_MAX];
199                 } ptr;
200         } data;
201 };
202
203 struct nameserver {
204         int socket;  /* a connected UDP socket */
205         u32 address;
206         int failed_times;  /* number of times which we have given this server a chance */
207         int timedout;  /* number of times in a row a request has timed out */
208         struct event event;
209         /* these objects are kept in a circular list */
210         struct nameserver *next, *prev;
211         struct event timeout_event;  /* used to keep the timeout for */
212                                      /* when we next probe this server. */
213                                      /* Valid if state == 0 */
214         char state;  /* zero if we think that this server is down */
215         char choked;  /* true if we have an EAGAIN from this server's socket */
216         char write_waiting;  /* true if we are waiting for EV_WRITE events */
217 };
218
219 static struct request *req_head = NULL, *req_waiting_head = NULL;
220 static struct nameserver *server_head = NULL;
221
222 /* Represents a local port where we're listening for DNS requests. Right now, */
223 /* only UDP is supported. */
224 struct evdns_server_port {
225         int socket; /* socket we use to read queries and write replies. */
226         int refcnt; /* reference count. */
227         char choked; /* Are we currently blocked from writing? */
228         char closing; /* Are we trying to close this port, pending writes? */
229         evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
230         void *user_data; /* Opaque pointer passed to user_callback */
231         struct event event; /* Read/write event */
232         /* circular list of replies that we want to write. */
233         struct server_request *pending_replies;
234 };
235
236 /* Represents part of a reply being built.      (That is, a single RR.) */
237 struct server_reply_item {
238         struct server_reply_item *next; /* next item in sequence. */
239         char *name; /* name part of the RR */
240         u16 type : 16; /* The RR type */
241         u16 class : 16; /* The RR class (usually CLASS_INET) */
242         u32 ttl; /* The RR TTL */
243         char is_name; /* True iff data is a label */
244         u16 datalen; /* Length of data; -1 if data is a label */
245         void *data; /* The contents of the RR */
246 };
247
248 /* Represents a request that we've received as a DNS server, and holds */
249 /* the components of the reply as we're constructing it. */
250 struct server_request {
251         /* Pointers to the next and previous entries on the list of replies */
252         /* that we're waiting to write.  Only set if we have tried to respond */
253         /* and gotten EAGAIN. */
254         struct server_request *next_pending;
255         struct server_request *prev_pending;
256
257         u16 trans_id; /* Transaction id. */
258         struct evdns_server_port *port; /* Which port received this request on? */
259         struct sockaddr_storage addr; /* Where to send the response */
260         socklen_t addrlen; /* length of addr */
261
262         int n_answer; /* how many answer RRs have been set? */
263         int n_authority; /* how many authority RRs have been set? */
264         int n_additional; /* how many additional RRs have been set? */
265
266         struct server_reply_item *answer; /* linked list of answer RRs */
267         struct server_reply_item *authority; /* linked list of authority RRs */
268         struct server_reply_item *additional; /* linked list of additional RRs */
269
270         /* Constructed response.  Only set once we're ready to send a reply. */
271         /* Once this is set, the RR fields are cleared, and no more should be set. */
272         char *response;
273         size_t response_len;
274
275         /* Caller-visible fields: flags, questions. */
276         struct evdns_server_request base;
277 };
278
279 /* helper macro */
280 #define OFFSET_OF(st, member) ((off_t) (((char*)&((st*)0)->member)-(char*)0))
281
282 /* Given a pointer to an evdns_server_request, get the corresponding */
283 /* server_request. */
284 #define TO_SERVER_REQUEST(base_ptr)                                                                             \
285         ((struct server_request*)                                                                                       \
286          (((char*)(base_ptr) - OFFSET_OF(struct server_request, base))))
287
288 /* The number of good nameservers that we have */
289 static int global_good_nameservers = 0;
290
291 /* inflight requests are contained in the req_head list */
292 /* and are actually going out across the network */
293 static int global_requests_inflight = 0;
294 /* requests which aren't inflight are in the waiting list */
295 /* and are counted here */
296 static int global_requests_waiting = 0;
297
298 static int global_max_requests_inflight = 64;
299
300 static struct timeval global_timeout = {5, 0};  /* 5 seconds */
301 static int global_max_reissues = 1;  /* a reissue occurs when we get some errors from the server */
302 static int global_max_retransmits = 3;  /* number of times we'll retransmit a request which timed out */
303 /* number of timeouts in a row before we consider this server to be down */
304 static int global_max_nameserver_timeout = 3;
305
306 /* These are the timeout values for nameservers. If we find a nameserver is down */
307 /* we try to probe it at intervals as given below. Values are in seconds. */
308 static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}};
309 static const int global_nameserver_timeouts_length = sizeof(global_nameserver_timeouts)/sizeof(struct timeval);
310
311 static struct nameserver *nameserver_pick(void);
312 static void evdns_request_insert(struct request *req, struct request **head);
313 static void nameserver_ready_callback(int fd, short events, void *arg);
314 static int evdns_transmit(void);
315 static int evdns_request_transmit(struct request *req);
316 static void nameserver_send_probe(struct nameserver *const ns);
317 static void search_request_finished(struct request *const);
318 static int search_try_next(struct request *const req);
319 static int search_request_new(int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg);
320 static void evdns_requests_pump_waiting_queue(void);
321 static u16 transaction_id_pick(void);
322 static struct request *request_new(int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
323 static void request_submit(struct request *req);
324
325 static int server_request_free(struct server_request *req);
326 static void server_request_free_answers(struct server_request *req);
327 static void server_port_free(struct evdns_server_port *port);
328 static void server_port_ready_callback(int fd, short events, void *arg);
329
330 static int strtoint(const char *const str);
331
332 #ifdef WIN32
333 static int
334 last_error(int sock)
335 {
336         int optval, optvallen=sizeof(optval);
337         int err = WSAGetLastError();
338         if (err == WSAEWOULDBLOCK && sock >= 0) {
339                 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval,
340                                &optvallen))
341                         return err;
342                 if (optval)
343                         return optval;
344         }
345         return err;
346
347 }
348 static int
349 error_is_eagain(int err)
350 {
351         return err == EAGAIN || err == WSAEWOULDBLOCK;
352 }
353 static int
354 inet_aton(const char *c, struct in_addr *addr)
355 {
356         uint32_t r;
357         if (strcmp(c, "255.255.255.255") == 0) {
358                 addr->s_addr = 0xffffffffu;
359         } else {
360                 r = inet_addr(c);
361                 if (r == INADDR_NONE)
362                         return 0;
363                 addr->s_addr = r;
364         }
365         return 1;
366 }
367 #define CLOSE_SOCKET(x) closesocket(x)
368 #else
369 #define last_error(sock) (errno)
370 #define error_is_eagain(err) ((err) == EAGAIN)
371 #define CLOSE_SOCKET(x) close(x)
372 #endif
373
374 #define ISSPACE(c) isspace((int)(unsigned char)(c))
375 #define ISDIGIT(c) isdigit((int)(unsigned char)(c))
376
377 #ifndef NDEBUG
378 static const char *
379 debug_ntoa(u32 address)
380 {
381         static char buf[32];
382         u32 a = ntohl(address);
383         snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
384                       (int)(u8)((a>>24)&0xff),
385                       (int)(u8)((a>>16)&0xff),
386                       (int)(u8)((a>>8 )&0xff),
387                       (int)(u8)((a    )&0xff));
388         return buf;
389 }
390 #endif
391
392 static evdns_debug_log_fn_type evdns_log_fn = NULL;
393
394 void
395 evdns_set_log_fn(evdns_debug_log_fn_type fn)
396 {
397   evdns_log_fn = fn;
398 }
399
400 #ifdef __GNUC__
401 #define EVDNS_LOG_CHECK  __attribute__ ((format(printf, 2, 3)))
402 #else
403 #define EVDNS_LOG_CHECK
404 #endif
405
406 static void _evdns_log(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
407 static void
408 _evdns_log(int warn, const char *fmt, ...)
409 {
410   va_list args;
411   static char buf[512];
412   if (!evdns_log_fn)
413     return;
414   va_start(args,fmt);
415 #ifdef WIN32
416   _vsnprintf(buf, sizeof(buf), fmt, args);
417 #else
418   vsnprintf(buf, sizeof(buf), fmt, args);
419 #endif
420   buf[sizeof(buf)-1] = '\0';
421   evdns_log_fn(warn, buf);
422   va_end(args);
423 }
424
425 #define log _evdns_log
426
427 /* This walks the list of inflight requests to find the */
428 /* one with a matching transaction id. Returns NULL on */
429 /* failure */
430 static struct request *
431 request_find_from_trans_id(u16 trans_id) {
432         struct request *req = req_head, *const started_at = req_head;
433
434         if (req) {
435                 do {
436                         if (req->trans_id == trans_id) return req;
437                         req = req->next;
438                 } while (req != started_at);
439         }
440
441         return NULL;
442 }
443
444 /* a libevent callback function which is called when a nameserver */
445 /* has gone down and we want to test if it has came back to life yet */
446 static void
447 nameserver_prod_callback(int fd, short events, void *arg) {
448         struct nameserver *const ns = (struct nameserver *) arg;
449         (void)fd;
450         (void)events;
451
452         nameserver_send_probe(ns);
453 }
454
455 /* a libevent callback which is called when a nameserver probe (to see if */
456 /* it has come back to life) times out. We increment the count of failed_times */
457 /* and wait longer to send the next probe packet. */
458 static void
459 nameserver_probe_failed(struct nameserver *const ns) {
460         const struct timeval * timeout;
461         (void) evtimer_del(&ns->timeout_event);
462         if (ns->state == 1) {
463                 /* This can happen if the nameserver acts in a way which makes us mark */
464                 /* it as bad and then starts sending good replies. */
465                 return;
466         }
467
468         timeout =
469           &global_nameserver_timeouts[MIN(ns->failed_times,
470                                           global_nameserver_timeouts_length - 1)];
471         ns->failed_times++;
472
473         evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
474         if (evtimer_add(&ns->timeout_event, (struct timeval *) timeout) < 0) {
475           log(EVDNS_LOG_WARN,
476               "Error from libevent when adding timer event for %s",
477               debug_ntoa(ns->address));
478           /* ???? Do more? */
479         }
480 }
481
482 /* called when a nameserver has been deemed to have failed. For example, too */
483 /* many packets have timed out etc */
484 static void
485 nameserver_failed(struct nameserver *const ns, const char *msg) {
486         struct request *req, *started_at;
487         /* if this nameserver has already been marked as failed */
488         /* then don't do anything */
489         if (!ns->state) return;
490
491         log(EVDNS_LOG_WARN, "Nameserver %s has failed: %s",
492             debug_ntoa(ns->address), msg);
493         global_good_nameservers--;
494         assert(global_good_nameservers >= 0);
495         if (global_good_nameservers == 0) {
496                 log(EVDNS_LOG_WARN, "All nameservers have failed");
497         }
498
499         ns->state = 0;
500         ns->failed_times = 1;
501
502         evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
503         if (evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]) < 0) {
504                 log(EVDNS_LOG_WARN,
505                     "Error from libevent when adding timer event for %s",
506                     debug_ntoa(ns->address));
507                 /* ???? Do more? */
508         }
509
510         /* walk the list of inflight requests to see if any can be reassigned to */
511         /* a different server. Requests in the waiting queue don't have a */
512         /* nameserver assigned yet */
513
514         /* if we don't have *any* good nameservers then there's no point */
515         /* trying to reassign requests to one */
516         if (!global_good_nameservers) return;
517
518         req = req_head;
519         started_at = req_head;
520         if (req) {
521                 do {
522                         if (req->tx_count == 0 && req->ns == ns) {
523                                 /* still waiting to go out, can be moved */
524                                 /* to another server */
525                                 req->ns = nameserver_pick();
526                         }
527                         req = req->next;
528                 } while (req != started_at);
529         }
530 }
531
532 static void
533 nameserver_up(struct nameserver *const ns) {
534         if (ns->state) return;
535         log(EVDNS_LOG_WARN, "Nameserver %s is back up",
536             debug_ntoa(ns->address));
537         evtimer_del(&ns->timeout_event);
538         ns->state = 1;
539         ns->failed_times = 0;
540         ns->timedout = 0;
541         global_good_nameservers++;
542 }
543
544 static void
545 request_trans_id_set(struct request *const req, const u16 trans_id) {
546         req->trans_id = trans_id;
547         *((u16 *) req->request) = htons(trans_id);
548 }
549
550 /* Called to remove a request from a list and dealloc it. */
551 /* head is a pointer to the head of the list it should be */
552 /* removed from or NULL if the request isn't in a list. */
553 static void
554 request_finished(struct request *const req, struct request **head) {
555         if (head) {
556                 if (req->next == req) {
557                         /* only item in the list */
558                         *head = NULL;
559                 } else {
560                         req->next->prev = req->prev;
561                         req->prev->next = req->next;
562                         if (*head == req) *head = req->next;
563                 }
564         }
565
566         log(EVDNS_LOG_DEBUG, "Removing timeout for request %lx",
567             (unsigned long) req);
568         evtimer_del(&req->timeout_event);
569
570         search_request_finished(req);
571         global_requests_inflight--;
572
573         if (!req->request_appended) {
574                 /* need to free the request data on it's own */
575                 free(req->request);
576         } else {
577                 /* the request data is appended onto the header */
578                 /* so everything gets free()ed when we: */
579         }
580
581         free(req);
582
583         evdns_requests_pump_waiting_queue();
584 }
585
586 /* This is called when a server returns a funny error code. */
587 /* We try the request again with another server. */
588 /* */
589 /* return: */
590 /*   0 ok */
591 /*   1 failed/reissue is pointless */
592 static int
593 request_reissue(struct request *req) {
594         const struct nameserver *const last_ns = req->ns;
595         /* the last nameserver should have been marked as failing */
596         /* by the caller of this function, therefore pick will try */
597         /* not to return it */
598         req->ns = nameserver_pick();
599         if (req->ns == last_ns) {
600                 /* ... but pick did return it */
601                 /* not a lot of point in trying again with the */
602                 /* same server */
603                 return 1;
604         }
605
606         req->reissue_count++;
607         req->tx_count = 0;
608         req->transmit_me = 1;
609
610         return 0;
611 }
612
613 /* this function looks for space on the inflight queue and promotes */
614 /* requests from the waiting queue if it can. */
615 static void
616 evdns_requests_pump_waiting_queue(void) {
617         while (global_requests_inflight < global_max_requests_inflight &&
618             global_requests_waiting) {
619                 struct request *req;
620                 /* move a request from the waiting queue to the inflight queue */
621                 assert(req_waiting_head);
622                 if (req_waiting_head->next == req_waiting_head) {
623                         /* only one item in the queue */
624                         req = req_waiting_head;
625                         req_waiting_head = NULL;
626                 } else {
627                         req = req_waiting_head;
628                         req->next->prev = req->prev;
629                         req->prev->next = req->next;
630                         req_waiting_head = req->next;
631                 }
632
633                 global_requests_waiting--;
634                 global_requests_inflight++;
635
636                 req->ns = nameserver_pick();
637                 request_trans_id_set(req, transaction_id_pick());
638
639                 evdns_request_insert(req, &req_head);
640                 evdns_request_transmit(req);
641                 evdns_transmit();
642         }
643 }
644
645 static void
646 reply_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply) {
647         switch (req->request_type) {
648         case TYPE_A:
649                 if (reply)
650                         req->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
651                                                            reply->data.a.addrcount, ttl,
652                                                  reply->data.a.addresses,
653                                                            req->user_pointer);
654                 else
655                         req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
656                 return;
657         case TYPE_PTR:
658                 if (reply) {
659                         char *name = reply->data.ptr.name;
660                         req->user_callback(DNS_ERR_NONE, DNS_PTR, 1, ttl,
661                                                            &name, req->user_pointer);
662                 } else {
663                         req->user_callback(err, 0, 0, 0, NULL,
664                                                            req->user_pointer);
665                 }
666                 return;
667         case TYPE_AAAA:
668                 if (reply)
669                         req->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
670                                                            reply->data.aaaa.addrcount, ttl,
671                                                            reply->data.aaaa.addresses,
672                                                            req->user_pointer);
673                 else
674                         req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
675                 return;
676         }
677         assert(0);
678 }
679
680 /* this processes a parsed reply packet */
681 static void
682 reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) {
683         int error;
684         static const int error_codes[] = {DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST, DNS_ERR_NOTIMPL, DNS_ERR_REFUSED};
685
686         if (flags & 0x020f || !reply || !reply->have_answer) {
687                 /* there was an error */
688                 if (flags & 0x0200) {
689                         error = DNS_ERR_TRUNCATED;
690                 } else {
691                         u16 error_code = (flags & 0x000f) - 1;
692                         if (error_code > 4) {
693                                 error = DNS_ERR_UNKNOWN;
694                         } else {
695                                 error = error_codes[error_code];
696                         }
697                 }
698
699                 switch(error) {
700                 case DNS_ERR_NOTIMPL:
701                 case DNS_ERR_REFUSED:
702                         /* we regard these errors as marking a bad nameserver */
703                         if (req->reissue_count < global_max_reissues) {
704                                 char msg[64];
705                                 snprintf(msg, sizeof(msg), "Bad response %d (%s)",
706                                          error, evdns_err_to_string(error));
707                                 nameserver_failed(req->ns, msg);
708                                 if (!request_reissue(req)) return;
709                         }
710                         break;
711                 case DNS_ERR_SERVERFAILED:
712                         /* rcode 2 (servfailed) sometimes means "we are broken" and
713                          * sometimes (with some binds) means "that request was very
714                          * confusing."  Treat this as a timeout, not a failure. 
715                          */
716                         log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver %s; "
717                                 "will allow the request to time out.",
718                                 debug_ntoa(req->ns->address));
719                         break;
720                 default:
721                         /* we got a good reply from the nameserver */
722                         nameserver_up(req->ns);
723                 }
724
725                 if (req->search_state && req->request_type != TYPE_PTR) {
726                         /* if we have a list of domains to search in, try the next one */
727                         if (!search_try_next(req)) {
728                                 /* a new request was issued so this request is finished and */
729                                 /* the user callback will be made when that request (or a */
730                                 /* child of it) finishes. */
731                                 request_finished(req, &req_head);
732                                 return;
733                         }
734                 }
735
736                 /* all else failed. Pass the failure up */
737                 reply_callback(req, 0, error, NULL);
738                 request_finished(req, &req_head);
739         } else {
740                 /* all ok, tell the user */
741                 reply_callback(req, ttl, 0, reply);
742                 nameserver_up(req->ns);
743                 request_finished(req, &req_head);
744         }
745 }
746
747 static int
748 name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
749         int name_end = -1;
750         int j = *idx;
751         int ptr_count = 0;
752 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0)
753 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while(0)
754 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while(0)
755
756         char *cp = name_out;
757         const char *const end = name_out + name_out_len;
758
759         /* Normally, names are a series of length prefixed strings terminated */
760         /* with a length of 0 (the lengths are u8's < 63). */
761         /* However, the length can start with a pair of 1 bits and that */
762         /* means that the next 14 bits are a pointer within the current */
763         /* packet. */
764
765         for(;;) {
766                 u8 label_len;
767                 if (j >= length) return -1;
768                 GET8(label_len);
769                 if (!label_len) break;
770                 if (label_len & 0xc0) {
771                         u8 ptr_low;
772                         GET8(ptr_low);
773                         if (name_end < 0) name_end = j;
774                         j = (((int)label_len & 0x3f) << 8) + ptr_low;
775                         /* Make sure that the target offset is in-bounds. */
776                         if (j < 0 || j >= length) return -1;
777                         /* If we've jumped more times than there are characters in the
778                          * message, we must have a loop. */
779                         if (++ptr_count > length) return -1;
780                         continue;
781                 }
782                 if (label_len > 63) return -1;
783                 if (cp != name_out) {
784                         if (cp + 1 >= end) return -1;
785                         *cp++ = '.';
786                 }
787                 if (cp + label_len >= end) return -1;
788                 memcpy(cp, packet + j, label_len);
789                 cp += label_len;
790                 j += label_len;
791         }
792         if (cp >= end) return -1;
793         *cp = '\0';
794         if (name_end < 0)
795                 *idx = j;
796         else
797                 *idx = name_end;
798         return 0;
799  err:
800         return -1;
801 }
802
803 /* parses a raw request from a nameserver */
804 static int
805 reply_parse(u8 *packet, int length) {
806         int j = 0;  /* index into packet */
807         u16 _t;  /* used by the macros */
808         u32 _t32;  /* used by the macros */
809         char tmp_name[256]; /* used by the macros */
810
811         u16 trans_id, questions, answers, authority, additional, datalength;
812         u16 flags = 0;
813         u32 ttl, ttl_r = 0xffffffff;
814         struct reply reply;
815         struct request *req = NULL;
816         unsigned int i;
817
818         GET16(trans_id);
819         GET16(flags);
820         GET16(questions);
821         GET16(answers);
822         GET16(authority);
823         GET16(additional);
824         (void) authority; /* suppress "unused variable" warnings. */
825         (void) additional; /* suppress "unused variable" warnings. */
826
827         req = request_find_from_trans_id(trans_id);
828         if (!req) return -1;
829
830         memset(&reply, 0, sizeof(reply));
831
832         /* If it's not an answer, it doesn't correspond to any request. */
833         if (!(flags & 0x8000)) return -1;  /* must be an answer */
834         if (flags & 0x020f) {
835                 /* there was an error */
836                 goto err;
837         }
838         /* if (!answers) return; */  /* must have an answer of some form */
839
840         /* This macro skips a name in the DNS reply. */
841 #define SKIP_NAME \
842         do { tmp_name[0] = '\0';                                        \
843                 if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0) \
844                         goto err;                                                                                                       \
845         } while(0);
846
847         reply.type = req->request_type;
848
849         /* skip over each question in the reply */
850         for (i = 0; i < questions; ++i) {
851                 /* the question looks like
852                  *   <label:name><u16:type><u16:class>
853                  */
854                 SKIP_NAME;
855                 j += 4;
856                 if (j >= length) goto err;
857         }
858
859         /* now we have the answer section which looks like
860          * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
861          */
862
863         for (i = 0; i < answers; ++i) {
864                 u16 type, class;
865
866                 SKIP_NAME;
867                 GET16(type);
868                 GET16(class);
869                 GET32(ttl);
870                 GET16(datalength);
871
872                 if (type == TYPE_A && class == CLASS_INET) {
873                         int addrcount, addrtocopy;
874                         if (req->request_type != TYPE_A) {
875                                 j += datalength; continue;
876                         }
877                         if ((datalength & 3) != 0) /* not an even number of As. */
878                             goto err;
879                         addrcount = datalength >> 2;
880                         addrtocopy = MIN(MAX_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
881
882                         ttl_r = MIN(ttl_r, ttl);
883                         /* we only bother with the first four addresses. */
884                         if (j + 4*addrtocopy > length) goto err;
885                         memcpy(&reply.data.a.addresses[reply.data.a.addrcount],
886                                    packet + j, 4*addrtocopy);
887                         j += 4*addrtocopy;
888                         reply.data.a.addrcount += addrtocopy;
889                         reply.have_answer = 1;
890                         if (reply.data.a.addrcount == MAX_ADDRS) break;
891                 } else if (type == TYPE_PTR && class == CLASS_INET) {
892                         if (req->request_type != TYPE_PTR) {
893                                 j += datalength; continue;
894                         }
895                         if (name_parse(packet, length, &j, reply.data.ptr.name,
896                                                    sizeof(reply.data.ptr.name))<0)
897                                 goto err;
898                         ttl_r = MIN(ttl_r, ttl);
899                         reply.have_answer = 1;
900                         break;
901                 } else if (type == TYPE_AAAA && class == CLASS_INET) {
902                         int addrcount, addrtocopy;
903                         if (req->request_type != TYPE_AAAA) {
904                                 j += datalength; continue;
905                         }
906                         if ((datalength & 15) != 0) /* not an even number of AAAAs. */
907                                 goto err;
908                         addrcount = datalength >> 4;  /* each address is 16 bytes long */
909                         addrtocopy = MIN(MAX_ADDRS - reply.data.aaaa.addrcount, (unsigned)addrcount);
910                         ttl_r = MIN(ttl_r, ttl);
911
912                         /* we only bother with the first four addresses. */
913                         if (j + 16*addrtocopy > length) goto err;
914                         memcpy(&reply.data.aaaa.addresses[reply.data.aaaa.addrcount],
915                                    packet + j, 16*addrtocopy);
916                         reply.data.aaaa.addrcount += addrtocopy;
917                         j += 16*addrtocopy;
918                         reply.have_answer = 1;
919                         if (reply.data.aaaa.addrcount == MAX_ADDRS) break;
920                 } else {
921                         /* skip over any other type of resource */
922                         j += datalength;
923                 }
924         }
925
926         reply_handle(req, flags, ttl_r, &reply);
927         return 0;
928  err:
929         if (req)
930                 reply_handle(req, flags, 0, NULL);
931         return -1;
932 }
933
934 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
935 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
936 /* callback. */
937 static int
938 request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, socklen_t addrlen)
939 {
940         int j = 0;      /* index into packet */
941         u16 _t;  /* used by the macros */
942         char tmp_name[256]; /* used by the macros */
943
944         int i;
945         u16 trans_id, flags, questions, answers, authority, additional;
946         struct server_request *server_req = NULL;
947
948         /* Get the header fields */
949         GET16(trans_id);
950         GET16(flags);
951         GET16(questions);
952         GET16(answers);
953         GET16(authority);
954         GET16(additional);
955
956         if (flags & 0x8000) return -1; /* Must not be an answer. */
957         if (flags & 0x7800) return -1; /* only standard queries are supported */
958         flags &= 0x0300; /* Only TC and RD get preserved. */
959
960         server_req = malloc(sizeof(struct server_request));
961         if (server_req == NULL) return -1;
962         memset(server_req, 0, sizeof(struct server_request));
963
964         server_req->trans_id = trans_id;
965         memcpy(&server_req->addr, addr, addrlen);
966         server_req->addrlen = addrlen;
967
968         server_req->base.flags = flags;
969         server_req->base.nquestions = 0;
970         server_req->base.questions = malloc(sizeof(struct evdns_server_question *) * questions);
971         if (server_req->base.questions == NULL)
972                 goto err;
973
974         for (i = 0; i < questions; ++i) {
975                 u16 type, class;
976                 struct evdns_server_question *q;
977                 int namelen;
978                 if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
979                         goto err;
980                 GET16(type);
981                 GET16(class);
982                 namelen = strlen(tmp_name);
983                 q = malloc(sizeof(struct evdns_server_question) + namelen);
984                 if (!q)
985                         goto err;
986                 q->type = type;
987                 q->class = class;
988                 memcpy(q->name, tmp_name, namelen+1);
989                 server_req->base.questions[server_req->base.nquestions++] = q;
990         }
991
992         /* Ignore answers, authority, and additional. */
993
994         server_req->port = port;
995         port->refcnt++;
996         port->user_callback(&(server_req->base), port->user_data);
997
998         return 0;
999 err:
1000         if (server_req) {
1001                 if (server_req->base.questions) {
1002                         for (i = 0; i < server_req->base.nquestions; ++i)
1003                                 free(server_req->base.questions[i]);
1004                         free(server_req->base.questions);
1005                 }
1006                 free(server_req);
1007         }
1008         return -1;
1009
1010 #undef SKIP_NAME
1011 #undef GET32
1012 #undef GET16
1013 #undef GET8
1014 }
1015
1016 /* Try to choose a strong transaction id which isn't already in flight */
1017 static u16
1018 transaction_id_pick(void) {
1019         for (;;) {
1020                 const struct request *req = req_head, *started_at;
1021 #ifdef DNS_USE_CPU_CLOCK_FOR_ID
1022                 struct timespec ts;
1023                 u16 trans_id;
1024 #ifdef CLOCK_MONOTONIC
1025                 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
1026 #else
1027                 if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
1028 #endif
1029                         event_err(1, "clock_gettime");
1030                 trans_id = ts.tv_nsec & 0xffff;
1031 #endif
1032
1033 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
1034                 struct timeval tv;
1035                 u16 trans_id;
1036                 gettimeofday(&tv, NULL);
1037                 trans_id = tv.tv_usec & 0xffff;
1038 #endif
1039
1040 #ifdef DNS_USE_OPENSSL_FOR_ID
1041                 u16 trans_id;
1042                 if (RAND_pseudo_bytes((u8 *) &trans_id, 2) == -1) {
1043                         /* in the case that the RAND call fails we back */
1044                         /* down to using gettimeofday. */
1045                         struct timeval tv;
1046                         gettimeofday(&tv, NULL);
1047                         trans_id = tv.tv_usec & 0xffff; */
1048                         abort();
1049                 }
1050 #endif
1051
1052                 if (trans_id == 0xffff) continue;
1053                 /* now check to see if that id is already inflight */
1054                 req = started_at = req_head;
1055                 if (req) {
1056                         do {
1057                                 if (req->trans_id == trans_id) break;
1058                                 req = req->next;
1059                         } while (req != started_at);
1060                 }
1061                 /* we didn't find it, so this is a good id */
1062                 if (req == started_at) return trans_id;
1063         }
1064 }
1065
1066 /* choose a namesever to use. This function will try to ignore */
1067 /* nameservers which we think are down and load balance across the rest */
1068 /* by updating the server_head global each time. */
1069 static struct nameserver *
1070 nameserver_pick(void) {
1071         struct nameserver *started_at = server_head, *picked;
1072         if (!server_head) return NULL;
1073
1074         /* if we don't have any good nameservers then there's no */
1075         /* point in trying to find one. */
1076         if (!global_good_nameservers) {
1077                 server_head = server_head->next;
1078                 return server_head;
1079         }
1080
1081         /* remember that nameservers are in a circular list */
1082         for (;;) {
1083                 if (server_head->state) {
1084                         /* we think this server is currently good */
1085                         picked = server_head;
1086                         server_head = server_head->next;
1087                         return picked;
1088                 }
1089
1090                 server_head = server_head->next;
1091                 if (server_head == started_at) {
1092                         /* all the nameservers seem to be down */
1093                         /* so we just return this one and hope for the */
1094                         /* best */
1095                         assert(global_good_nameservers == 0);
1096                         picked = server_head;
1097                         server_head = server_head->next;
1098                         return picked;
1099                 }
1100         }
1101 }
1102
1103 /* this is called when a namesever socket is ready for reading */
1104 static void
1105 nameserver_read(struct nameserver *ns) {
1106         u8 packet[1500];
1107
1108         for (;;) {
1109                 const int r = recv(ns->socket, packet, sizeof(packet), 0);
1110                 if (r < 0) {
1111                         int err = last_error(ns->socket);
1112                         if (error_is_eagain(err)) return;
1113                         nameserver_failed(ns, strerror(err));
1114                         return;
1115                 }
1116                 ns->timedout = 0;
1117                 reply_parse(packet, r);
1118         }
1119 }
1120
1121 /* Read a packet from a DNS client on a server port s, parse it, and */
1122 /* act accordingly. */
1123 static void
1124 server_port_read(struct evdns_server_port *s) {
1125         u8 packet[1500];
1126         struct sockaddr_storage addr;
1127         socklen_t addrlen;
1128         int r;
1129
1130         for (;;) {
1131                 addrlen = sizeof(struct sockaddr_storage);
1132                 r = recvfrom(s->socket, packet, sizeof(packet), 0,
1133                                          (struct sockaddr*) &addr, &addrlen);
1134                 if (r < 0) {
1135                         int err = last_error(s->socket);
1136                         if (error_is_eagain(err)) return;
1137                         log(EVDNS_LOG_WARN, "Error %s (%d) while reading request.",
1138                                 strerror(err), err);
1139                         return;
1140                 }
1141                 request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);
1142         }
1143 }
1144
1145 /* Try to write all pending replies on a given DNS server port. */
1146 static void
1147 server_port_flush(struct evdns_server_port *port)
1148 {
1149         while (port->pending_replies) {
1150                 struct server_request *req = port->pending_replies;
1151                 int r = sendto(port->socket, req->response, req->response_len, 0,
1152                            (struct sockaddr*) &req->addr, req->addrlen);
1153                 if (r < 0) {
1154                         int err = last_error(port->socket);
1155                         if (error_is_eagain(err))
1156                                 return;
1157                         log(EVDNS_LOG_WARN, "Error %s (%d) while writing response to port; dropping", strerror(err), err);
1158                 }
1159                 if (server_request_free(req)) {
1160                         /* we released the last reference to req->port. */
1161                         return;
1162                 }
1163         }
1164
1165         /* We have no more pending requests; stop listening for 'writeable' events. */
1166         (void) event_del(&port->event);
1167         event_set(&port->event, port->socket, EV_READ | EV_PERSIST,
1168                           server_port_ready_callback, port);
1169         if (event_add(&port->event, NULL) < 0) {
1170                 log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server.");
1171                 /* ???? Do more? */
1172         }
1173 }
1174
1175 /* set if we are waiting for the ability to write to this server. */
1176 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1177 /* we stop these events. */
1178 static void
1179 nameserver_write_waiting(struct nameserver *ns, char waiting) {
1180         if (ns->write_waiting == waiting) return;
1181
1182         ns->write_waiting = waiting;
1183         (void) event_del(&ns->event);
1184         event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
1185                         nameserver_ready_callback, ns);
1186         if (event_add(&ns->event, NULL) < 0) {
1187           log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
1188               debug_ntoa(ns->address));
1189           /* ???? Do more? */
1190         }
1191 }
1192
1193 /* a callback function. Called by libevent when the kernel says that */
1194 /* a nameserver socket is ready for writing or reading */
1195 static void
1196 nameserver_ready_callback(int fd, short events, void *arg) {
1197         struct nameserver *ns = (struct nameserver *) arg;
1198         (void)fd;
1199
1200         if (events & EV_WRITE) {
1201                 ns->choked = 0;
1202                 if (!evdns_transmit()) {
1203                         nameserver_write_waiting(ns, 0);
1204                 }
1205         }
1206         if (events & EV_READ) {
1207                 nameserver_read(ns);
1208         }
1209 }
1210
1211 /* a callback function. Called by libevent when the kernel says that */
1212 /* a server socket is ready for writing or reading. */
1213 static void
1214 server_port_ready_callback(int fd, short events, void *arg) {
1215         struct evdns_server_port *port = (struct evdns_server_port *) arg;
1216         (void) fd;
1217
1218         if (events & EV_WRITE) {
1219                 port->choked = 0;
1220                 server_port_flush(port);
1221         }
1222         if (events & EV_READ) {
1223                 server_port_read(port);
1224         }
1225 }
1226
1227 /* This is an inefficient representation; only use it via the dnslabel_table_*
1228  * functions, so that is can be safely replaced with something smarter later. */
1229 #define MAX_LABELS 128
1230 /* Structures used to implement name compression */
1231 struct dnslabel_entry { char *v; off_t pos; };
1232 struct dnslabel_table {
1233         int n_labels; /* number of current entries */
1234         /* map from name to position in message */
1235         struct dnslabel_entry labels[MAX_LABELS];
1236 };
1237
1238 /* Initialize dnslabel_table. */
1239 static void
1240 dnslabel_table_init(struct dnslabel_table *table)
1241 {
1242         table->n_labels = 0;
1243 }
1244
1245 /* Free all storage held by table, but not the table itself. */
1246 static void
1247 dnslabel_clear(struct dnslabel_table *table)
1248 {
1249         int i;
1250         for (i = 0; i < table->n_labels; ++i)
1251                 free(table->labels[i].v);
1252         table->n_labels = 0;
1253 }
1254
1255 /* return the position of the label in the current message, or -1 if the label */
1256 /* hasn't been used yet. */
1257 static int
1258 dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
1259 {
1260         int i;
1261         for (i = 0; i < table->n_labels; ++i) {
1262                 if (!strcmp(label, table->labels[i].v))
1263                         return table->labels[i].pos;
1264         }
1265         return -1;
1266 }
1267
1268 /* remember that we've used the label at position pos */
1269 static int
1270 dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
1271 {
1272         char *v;
1273         int p;
1274         if (table->n_labels == MAX_LABELS)
1275                 return (-1);
1276         v = strdup(label);
1277         if (v == NULL)
1278                 return (-1);
1279         p = table->n_labels++;
1280         table->labels[p].v = v;
1281         table->labels[p].pos = pos;
1282
1283         return (0);
1284 }
1285
1286 /* Converts a string to a length-prefixed set of DNS labels, starting */
1287 /* at buf[j]. name and buf must not overlap. name_len should be the length */
1288 /* of name.      table is optional, and is used for compression. */
1289 /* */
1290 /* Input: abc.def */
1291 /* Output: <3>abc<3>def<0> */
1292 /* */
1293 /* Returns the first index after the encoded name, or negative on error. */
1294 /*       -1      label was > 63 bytes */
1295 /*       -2      name too long to fit in buffer. */
1296 /* */
1297 static off_t
1298 dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
1299                                   const char *name, const int name_len,
1300                                   struct dnslabel_table *table) {
1301         const char *end = name + name_len;
1302         int ref = 0;
1303         u16 _t;
1304
1305 #define APPEND16(x) do {                                                   \
1306                 if (j + 2 > (off_t)buf_len)                                \
1307                         goto overflow;                                             \
1308                 _t = htons(x);                                                     \
1309                 memcpy(buf + j, &_t, 2);                                   \
1310                 j += 2;                                                                    \
1311         } while (0)
1312 #define APPEND32(x) do {                                                   \
1313                 if (j + 4 > (off_t)buf_len)                                \
1314                         goto overflow;                                             \
1315                 _t32 = htonl(x);                                                   \
1316                 memcpy(buf + j, &_t32, 4);                                 \
1317                 j += 4;                                                                    \
1318         } while (0)
1319
1320         if (name_len > 255) return -2;
1321
1322         for (;;) {
1323                 const char *const start = name;
1324                 if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
1325                         APPEND16(ref | 0xc000);
1326                         return j;
1327                 }
1328                 name = strchr(name, '.');
1329                 if (!name) {
1330                         const unsigned int label_len = end - start;
1331                         if (label_len > 63) return -1;
1332                         if ((size_t)(j+label_len+1) > buf_len) return -2;
1333                         if (table) dnslabel_table_add(table, start, j);
1334                         buf[j++] = label_len;
1335
1336                         memcpy(buf + j, start, end - start);
1337                         j += end - start;
1338                         break;
1339                 } else {
1340                         /* append length of the label. */
1341                         const unsigned int label_len = name - start;
1342                         if (label_len > 63) return -1;
1343                         if ((size_t)(j+label_len+1) > buf_len) return -2;
1344                         if (table) dnslabel_table_add(table, start, j);
1345                         buf[j++] = label_len;
1346
1347                         memcpy(buf + j, start, name - start);
1348                         j += name - start;
1349                         /* hop over the '.' */
1350                         name++;
1351                 }
1352         }
1353
1354         /* the labels must be terminated by a 0. */
1355         /* It's possible that the name ended in a . */
1356         /* in which case the zero is already there */
1357         if (!j || buf[j-1]) buf[j++] = 0;
1358         return j;
1359  overflow:
1360         return (-2);
1361 }
1362
1363 /* Finds the length of a dns request for a DNS name of the given */
1364 /* length. The actual request may be smaller than the value returned */
1365 /* here */
1366 static int
1367 evdns_request_len(const int name_len) {
1368         return 96 + /* length of the DNS standard header */
1369                 name_len + 2 +
1370                 4;  /* space for the resource type */
1371 }
1372
1373 /* build a dns request packet into buf. buf should be at least as long */
1374 /* as evdns_request_len told you it should be. */
1375 /* */
1376 /* Returns the amount of space used. Negative on error. */
1377 static int
1378 evdns_request_data_build(const char *const name, const int name_len,
1379     const u16 trans_id, const u16 type, const u16 class,
1380     u8 *const buf, size_t buf_len) {
1381         off_t j = 0;  /* current offset into buf */
1382         u16 _t;  /* used by the macros */
1383
1384         APPEND16(trans_id);
1385         APPEND16(0x0100);  /* standard query, recusion needed */
1386         APPEND16(1);  /* one question */
1387         APPEND16(0);  /* no answers */
1388         APPEND16(0);  /* no authority */
1389         APPEND16(0);  /* no additional */
1390
1391         j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
1392         if (j < 0) {
1393                 return (int)j;
1394         }
1395         
1396         APPEND16(type);
1397         APPEND16(class);
1398
1399         return (int)j;
1400  overflow:
1401         return (-1);
1402 }
1403
1404 /* exported function */
1405 struct evdns_server_port *
1406 evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
1407 {
1408         struct evdns_server_port *port;
1409         if (!(port = malloc(sizeof(struct evdns_server_port))))
1410                 return NULL;
1411         memset(port, 0, sizeof(struct evdns_server_port));
1412
1413         assert(!is_tcp); /* TCP sockets not yet implemented */
1414         port->socket = socket;
1415         port->refcnt = 1;
1416         port->choked = 0;
1417         port->closing = 0;
1418         port->user_callback = cb;
1419         port->user_data = user_data;
1420         port->pending_replies = NULL;
1421
1422         event_set(&port->event, port->socket, EV_READ | EV_PERSIST,
1423                           server_port_ready_callback, port);
1424         event_add(&port->event, NULL); /* check return. */
1425         return port;
1426 }
1427
1428 /* exported function */
1429 void
1430 evdns_close_server_port(struct evdns_server_port *port)
1431 {
1432         if (--port->refcnt == 0)
1433                 server_port_free(port);
1434         port->closing = 1;
1435 }
1436
1437 /* exported function */
1438 int
1439 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)
1440 {
1441         struct server_request *req = TO_SERVER_REQUEST(_req);
1442         struct server_reply_item **itemp, *item;
1443         int *countp;
1444
1445         if (req->response) /* have we already answered? */
1446                 return (-1);
1447
1448         switch (section) {
1449         case EVDNS_ANSWER_SECTION:
1450                 itemp = &req->answer;
1451                 countp = &req->n_answer;
1452                 break;
1453         case EVDNS_AUTHORITY_SECTION:
1454                 itemp = &req->authority;
1455                 countp = &req->n_authority;
1456                 break;
1457         case EVDNS_ADDITIONAL_SECTION:
1458                 itemp = &req->additional;
1459                 countp = &req->n_additional;
1460                 break;
1461         default:
1462                 return (-1);
1463         }
1464         while (*itemp) {
1465                 itemp = &((*itemp)->next);
1466         }
1467         item = malloc(sizeof(struct server_reply_item));
1468         if (!item)
1469                 return -1;
1470         item->next = NULL;
1471         if (!(item->name = strdup(name))) {
1472                 free(item);
1473                 return -1;
1474         }
1475         item->type = type;
1476         item->class = class;
1477         item->ttl = ttl;
1478         item->is_name = is_name != 0;
1479         item->datalen = 0;
1480         item->data = NULL;
1481         if (data) {
1482                 if (item->is_name) {
1483                         if (!(item->data = strdup(data))) {
1484                                 free(item->name);
1485                                 free(item);
1486                                 return -1;
1487                         }
1488                         item->datalen = (u16)-1;
1489                 } else {
1490                         if (!(item->data = malloc(datalen))) {
1491                                 free(item->name);
1492                                 free(item);
1493                                 return -1;
1494                         }
1495                         item->datalen = datalen;
1496                         memcpy(item->data, data, datalen);
1497                 }
1498         }
1499
1500         *itemp = item;
1501         ++(*countp);
1502         return 0;
1503 }
1504
1505 /* exported function */
1506 int
1507 evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
1508 {
1509         return evdns_server_request_add_reply(
1510                   req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1511                   ttl, n*4, 0, addrs);
1512 }
1513
1514 /* exported function */
1515 int
1516 evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
1517 {
1518         return evdns_server_request_add_reply(
1519                   req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
1520                   ttl, n*16, 0, addrs);
1521 }
1522
1523 /* exported function */
1524 int
1525 evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
1526 {
1527         u32 a;
1528         char buf[32];
1529         assert(in || inaddr_name);
1530         assert(!(in && inaddr_name));
1531         if (in) {
1532                 a = ntohl(in->s_addr);
1533                 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
1534                                 (int)(u8)((a    )&0xff),
1535                                 (int)(u8)((a>>8 )&0xff),
1536                                 (int)(u8)((a>>16)&0xff),
1537                                 (int)(u8)((a>>24)&0xff));
1538                 inaddr_name = buf;
1539         }
1540         return evdns_server_request_add_reply(
1541                   req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
1542                   ttl, -1, 1, hostname);
1543 }
1544
1545 /* exported function */
1546 int
1547 evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
1548 {
1549         return evdns_server_request_add_reply(
1550                   req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1551                   ttl, -1, 1, cname);
1552 }
1553
1554
1555 static int
1556 evdns_server_request_format_response(struct server_request *req, int err)
1557 {
1558         unsigned char buf[1500];
1559         size_t buf_len = sizeof(buf);
1560         off_t j = 0, r;
1561         u16 _t;
1562         u32 _t32;
1563         int i;
1564         u16 flags;
1565         struct dnslabel_table table;
1566
1567         if (err < 0 || err > 15) return -1;
1568
1569         /* Set response bit and error code; copy OPCODE and RD fields from
1570          * question; copy RA and AA if set by caller. */
1571         flags = req->base.flags;
1572         flags |= (0x8000 | err);
1573
1574         dnslabel_table_init(&table);
1575         APPEND16(req->trans_id);
1576         APPEND16(flags);
1577         APPEND16(req->base.nquestions);
1578         APPEND16(req->n_answer);
1579         APPEND16(req->n_authority);
1580         APPEND16(req->n_additional);
1581
1582         /* Add questions. */
1583         for (i=0; i < req->base.nquestions; ++i) {
1584                 const char *s = req->base.questions[i]->name;
1585                 j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
1586                 if (j < 0) {
1587                         dnslabel_clear(&table);
1588                         return (int) j;
1589                 }
1590                 APPEND16(req->base.questions[i]->type);
1591                 APPEND16(req->base.questions[i]->class);
1592         }
1593
1594         /* Add answer, authority, and additional sections. */
1595         for (i=0; i<3; ++i) {
1596                 struct server_reply_item *item;
1597                 if (i==0)
1598                         item = req->answer;
1599                 else if (i==1)
1600                         item = req->authority;
1601                 else
1602                         item = req->additional;
1603                 while (item) {
1604                         r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
1605                         if (r < 0)
1606                                 goto overflow;
1607                         j = r;
1608
1609                         APPEND16(item->type);
1610                         APPEND16(item->class);
1611                         APPEND32(item->ttl);
1612                         if (item->is_name) {
1613                                 off_t len_idx = j, name_start;
1614                                 j += 2;
1615                                 name_start = j;
1616                                 r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
1617                                 if (r < 0)
1618                                         goto overflow;
1619                                 j = r;
1620                                 _t = htons( (j-name_start) );
1621                                 memcpy(buf+len_idx, &_t, 2);
1622                         } else {
1623                                 APPEND16(item->datalen);
1624                                 if (j+item->datalen > (off_t)buf_len)
1625                                         goto overflow;
1626                                 memcpy(buf+j, item->data, item->datalen);
1627                                 j += item->datalen;
1628                         }
1629                         item = item->next;
1630                 }
1631         }
1632
1633         if (j > 512) {
1634 overflow:
1635                 j = 512;
1636                 buf[3] |= 0x02; /* set the truncated bit. */
1637         }
1638
1639         req->response_len = j;
1640
1641         if (!(req->response = malloc(req->response_len))) {
1642                 server_request_free_answers(req);
1643                 dnslabel_clear(&table);
1644                 return (-1);
1645         }
1646         memcpy(req->response, buf, req->response_len);
1647         server_request_free_answers(req);
1648         dnslabel_clear(&table);
1649         return (0);
1650 }
1651
1652 /* exported function */
1653 int
1654 evdns_server_request_respond(struct evdns_server_request *_req, int err)
1655 {
1656         struct server_request *req = TO_SERVER_REQUEST(_req);
1657         struct evdns_server_port *port = req->port;
1658         int r;
1659         if (!req->response) {
1660                 if ((r = evdns_server_request_format_response(req, err))<0)
1661                         return r;
1662         }
1663
1664         r = sendto(port->socket, req->response, req->response_len, 0,
1665                            (struct sockaddr*) &req->addr, req->addrlen);
1666         if (r<0) {
1667                 int err = last_error(port->socket);
1668                 if (! error_is_eagain(err))
1669                         return -1;
1670
1671                 if (port->pending_replies) {
1672                         req->prev_pending = port->pending_replies->prev_pending;
1673                         req->next_pending = port->pending_replies;
1674                         req->prev_pending->next_pending =
1675                                 req->next_pending->prev_pending = req;
1676                 } else {
1677                         req->prev_pending = req->next_pending = req;
1678                         port->pending_replies = req;
1679                         port->choked = 1;
1680
1681                         (void) event_del(&port->event);
1682                         event_set(&port->event, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
1683
1684                         if (event_add(&port->event, NULL) < 0) {
1685                                 log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server");
1686                         }
1687
1688                 }
1689
1690                 return 1;
1691         }
1692         if (server_request_free(req))
1693                 return 0;
1694
1695         if (port->pending_replies)
1696                 server_port_flush(port);
1697
1698         return 0;
1699 }
1700
1701 /* Free all storage held by RRs in req. */
1702 static void
1703 server_request_free_answers(struct server_request *req)
1704 {
1705         struct server_reply_item *victim, *next, **list;
1706         int i;
1707         for (i = 0; i < 3; ++i) {
1708                 if (i==0)
1709                         list = &req->answer;
1710                 else if (i==1)
1711                         list = &req->authority;
1712                 else
1713                         list = &req->additional;
1714
1715                 victim = *list;
1716                 while (victim) {
1717                         next = victim->next;
1718                         free(victim->name);
1719                         if (victim->data)
1720                                 free(victim->data);
1721                         free(victim);
1722                         victim = next;
1723                 }
1724                 *list = NULL;
1725         }
1726 }
1727
1728 /* Free all storage held by req, and remove links to it. */
1729 /* return true iff we just wound up freeing the server_port. */
1730 static int
1731 server_request_free(struct server_request *req)
1732 {
1733         int i, rc=1;
1734         if (req->base.questions) {
1735                 for (i = 0; i < req->base.nquestions; ++i)
1736                         free(req->base.questions[i]);
1737                 free(req->base.questions);
1738         }
1739
1740         if (req->port) {
1741                 if (req->port->pending_replies == req) {
1742                         if (req->next_pending)
1743                                 req->port->pending_replies = req->next_pending;
1744                         else
1745                                 req->port->pending_replies = NULL;
1746                 }
1747                 rc = --req->port->refcnt;
1748         }
1749
1750         if (req->response) {
1751                 free(req->response);
1752         }
1753
1754         server_request_free_answers(req);
1755
1756         if (req->next_pending && req->next_pending != req) {
1757                 req->next_pending->prev_pending = req->prev_pending;
1758                 req->prev_pending->next_pending = req->next_pending;
1759         }
1760
1761         if (rc == 0) {
1762                 server_port_free(req->port);
1763                 free(req);
1764                 return (1);
1765         }
1766         free(req);
1767         return (0);
1768 }
1769
1770 /* Free all storage held by an evdns_server_port.  Only called when  */
1771 static void
1772 server_port_free(struct evdns_server_port *port)
1773 {
1774         assert(port);
1775         assert(!port->refcnt);
1776         assert(!port->pending_replies);
1777         if (port->socket > 0) {
1778                 CLOSE_SOCKET(port->socket);
1779                 port->socket = -1;
1780         }
1781         (void) event_del(&port->event);
1782         /* XXXX actually free the port? -NM */
1783 }
1784
1785 /* exported function */
1786 int
1787 evdns_server_request_drop(struct evdns_server_request *_req)
1788 {
1789         struct server_request *req = TO_SERVER_REQUEST(_req);
1790         server_request_free(req);
1791         return 0;
1792 }
1793
1794 /* exported function */
1795 int
1796 evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
1797 {
1798         struct server_request *req = TO_SERVER_REQUEST(_req);
1799         if (addr_len < (int)req->addrlen)
1800                 return -1;
1801         memcpy(sa, &(req->addr), req->addrlen);
1802         return req->addrlen;
1803 }
1804
1805 #undef APPEND16
1806 #undef APPEND32
1807
1808 /* this is a libevent callback function which is called when a request */
1809 /* has timed out. */
1810 static void
1811 evdns_request_timeout_callback(int fd, short events, void *arg) {
1812         struct request *const req = (struct request *) arg;
1813         (void) fd;
1814         (void) events;
1815
1816         log(EVDNS_LOG_DEBUG, "Request %lx timed out", (unsigned long) arg);
1817
1818         req->ns->timedout++;
1819         if (req->ns->timedout > global_max_nameserver_timeout) {
1820                 req->ns->timedout = 0;
1821                 nameserver_failed(req->ns, "request timed out.");
1822         }
1823
1824         (void) evtimer_del(&req->timeout_event);
1825         if (req->tx_count >= global_max_retransmits) {
1826                 /* this request has failed */
1827                 reply_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
1828                 request_finished(req, &req_head);
1829         } else {
1830                 /* retransmit it */
1831                 evdns_request_transmit(req);
1832         }
1833 }
1834
1835 /* try to send a request to a given server. */
1836 /* */
1837 /* return: */
1838 /*   0 ok */
1839 /*   1 temporary failure */
1840 /*   2 other failure */
1841 static int
1842 evdns_request_transmit_to(struct request *req, struct nameserver *server) {
1843         const int r = send(server->socket, req->request, req->request_len, 0);
1844         if (r < 0) {
1845                 int err = last_error(server->socket);
1846                 if (error_is_eagain(err)) return 1;
1847                 nameserver_failed(req->ns, strerror(err));
1848                 return 2;
1849         } else if (r != (int)req->request_len) {
1850                 return 1;  /* short write */
1851         } else {
1852                 return 0;
1853         }
1854 }
1855
1856 /* try to send a request, updating the fields of the request */
1857 /* as needed */
1858 /* */
1859 /* return: */
1860 /*   0 ok */
1861 /*   1 failed */
1862 static int
1863 evdns_request_transmit(struct request *req) {
1864         int retcode = 0, r;
1865
1866         /* if we fail to send this packet then this flag marks it */
1867         /* for evdns_transmit */
1868         req->transmit_me = 1;
1869         if (req->trans_id == 0xffff) abort();
1870
1871         if (req->ns->choked) {
1872                 /* don't bother trying to write to a socket */
1873                 /* which we have had EAGAIN from */
1874                 return 1;
1875         }
1876
1877         r = evdns_request_transmit_to(req, req->ns);
1878         switch (r) {
1879         case 1:
1880                 /* temp failure */
1881                 req->ns->choked = 1;
1882                 nameserver_write_waiting(req->ns, 1);
1883                 return 1;
1884         case 2:
1885                 /* failed in some other way */
1886                 retcode = 1;
1887                 /* fall through */
1888         default:
1889                 /* all ok */
1890                 log(EVDNS_LOG_DEBUG,
1891                     "Setting timeout for request %lx", (unsigned long) req);
1892                 evtimer_set(&req->timeout_event, evdns_request_timeout_callback, req);
1893                 if (evtimer_add(&req->timeout_event, &global_timeout) < 0) {
1894                   log(EVDNS_LOG_WARN,
1895                       "Error from libevent when adding timer for request %lx",
1896                       (unsigned long) req);
1897                   /* ???? Do more? */
1898                 }
1899                 req->tx_count++;
1900                 req->transmit_me = 0;
1901                 return retcode;
1902         }
1903 }
1904
1905 static void
1906 nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
1907         struct nameserver *const ns = (struct nameserver *) arg;
1908         (void) type;
1909         (void) count;
1910         (void) ttl;
1911         (void) addresses;
1912
1913         if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
1914                 /* this is a good reply */
1915                 nameserver_up(ns);
1916         } else nameserver_probe_failed(ns);
1917 }
1918
1919 static void
1920 nameserver_send_probe(struct nameserver *const ns) {
1921         struct request *req;
1922         /* here we need to send a probe to a given nameserver */
1923         /* in the hope that it is up now. */
1924
1925         log(EVDNS_LOG_DEBUG, "Sending probe to %s", debug_ntoa(ns->address));
1926
1927         req = request_new(TYPE_A, "www.google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
1928         if (!req) return;
1929         /* we force this into the inflight queue no matter what */
1930         request_trans_id_set(req, transaction_id_pick());
1931         req->ns = ns;
1932         request_submit(req);
1933 }
1934
1935 /* returns: */
1936 /*   0 didn't try to transmit anything */
1937 /*   1 tried to transmit something */
1938 static int
1939 evdns_transmit(void) {
1940         char did_try_to_transmit = 0;
1941
1942         if (req_head) {
1943                 struct request *const started_at = req_head, *req = req_head;
1944                 /* first transmit all the requests which are currently waiting */
1945                 do {
1946                         if (req->transmit_me) {
1947                                 did_try_to_transmit = 1;
1948                                 evdns_request_transmit(req);
1949                         }
1950
1951                         req = req->next;
1952                 } while (req != started_at);
1953         }
1954
1955         return did_try_to_transmit;
1956 }
1957
1958 /* exported function */
1959 int
1960 evdns_count_nameservers(void)
1961 {
1962         const struct nameserver *server = server_head;
1963         int n = 0;
1964         if (!server)
1965                 return 0;
1966         do {
1967                 ++n;
1968                 server = server->next;
1969         } while (server != server_head);
1970         return n;
1971 }
1972
1973 /* exported function */
1974 int
1975 evdns_clear_nameservers_and_suspend(void)
1976 {
1977         struct nameserver *server = server_head, *started_at = server_head;
1978         struct request *req = req_head, *req_started_at = req_head;
1979
1980         if (!server)
1981                 return 0;
1982         while (1) {
1983                 struct nameserver *next = server->next;
1984                 (void) event_del(&server->event);
1985                 (void) evtimer_del(&server->timeout_event);
1986                 if (server->socket >= 0)
1987                         CLOSE_SOCKET(server->socket);
1988                 free(server);
1989                 if (next == started_at)
1990                         break;
1991                 server = next;
1992         }
1993         server_head = NULL;
1994         global_good_nameservers = 0;
1995
1996         while (req) {
1997                 struct request *next = req->next;
1998                 req->tx_count = req->reissue_count = 0;
1999                 req->ns = NULL;
2000                 /* ???? What to do about searches? */
2001                 (void) evtimer_del(&req->timeout_event);
2002                 req->trans_id = 0;
2003                 req->transmit_me = 0;
2004
2005                 global_requests_waiting++;
2006                 evdns_request_insert(req, &req_waiting_head);
2007                 /* We want to insert these suspended elements at the front of
2008                  * the waiting queue, since they were pending before any of
2009                  * the waiting entries were added.  This is a circular list,
2010                  * so we can just shift the start back by one.*/
2011                 req_waiting_head = req_waiting_head->prev;
2012
2013                 if (next == req_started_at)
2014                         break;
2015                 req = next;
2016         }
2017         req_head = NULL;
2018         global_requests_inflight = 0;
2019
2020         return 0;
2021 }
2022
2023
2024 /* exported function */
2025 int
2026 evdns_resume(void)
2027 {
2028         evdns_requests_pump_waiting_queue();
2029         return 0;
2030 }
2031
2032 static int
2033 _evdns_nameserver_add_impl(unsigned long int address, int port) {
2034         /* first check to see if we already have this nameserver */
2035
2036         const struct nameserver *server = server_head, *const started_at = server_head;
2037         struct nameserver *ns;
2038         struct sockaddr_in sin;
2039         int err = 0;
2040         if (server) {
2041                 do {
2042                         if (server->address == address) return 3;
2043                         server = server->next;
2044                 } while (server != started_at);
2045         }
2046
2047         ns = (struct nameserver *) malloc(sizeof(struct nameserver));
2048         if (!ns) return -1;
2049
2050         memset(ns, 0, sizeof(struct nameserver));
2051
2052         ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
2053         if (ns->socket < 0) { err = 1; goto out1; }
2054 #ifdef WIN32
2055         {
2056                 u_long nonblocking = 1;
2057                 ioctlsocket(ns->socket, FIONBIO, &nonblocking);
2058         }
2059 #else
2060         fcntl(ns->socket, F_SETFL, O_NONBLOCK);
2061 #endif
2062         sin.sin_addr.s_addr = address;
2063         sin.sin_port = htons(port);
2064         sin.sin_family = AF_INET;
2065         if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
2066                 err = 2;
2067                 goto out2;
2068         }
2069
2070         ns->address = address;
2071         ns->state = 1;
2072         event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
2073         if (event_add(&ns->event, NULL) < 0) {
2074           err = 2;
2075           goto out2;
2076         }
2077
2078         log(EVDNS_LOG_DEBUG, "Added nameserver %s", debug_ntoa(address));
2079
2080         /* insert this nameserver into the list of them */
2081         if (!server_head) {
2082                 ns->next = ns->prev = ns;
2083                 server_head = ns;
2084         } else {
2085                 ns->next = server_head->next;
2086                 ns->prev = server_head;
2087                 server_head->next = ns;
2088                 if (server_head->prev == server_head) {
2089                         server_head->prev = ns;
2090                 }
2091         }
2092
2093         global_good_nameservers++;
2094
2095         return 0;
2096
2097 out2:
2098         CLOSE_SOCKET(ns->socket);
2099 out1:
2100         free(ns);
2101         log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d", debug_ntoa(address), err);
2102         return err;
2103 }
2104
2105 /* exported function */
2106 int
2107 evdns_nameserver_add(unsigned long int address) {
2108         return _evdns_nameserver_add_impl(address, 53);
2109 }
2110
2111 /* exported function */
2112 int
2113 evdns_nameserver_ip_add(const char *ip_as_string) {
2114         struct in_addr ina;
2115         int port;
2116         char buf[20];
2117         const char *cp;
2118         cp = strchr(ip_as_string, ':');
2119         if (! cp) {
2120                 cp = ip_as_string;
2121                 port = 53;
2122         } else {
2123                 port = strtoint(cp+1);
2124                 if (port < 0 || port > 65535) {
2125                         return 4;
2126                 }
2127                 if ((cp-ip_as_string) >= (int)sizeof(buf)) {
2128                         return 4;
2129                 }
2130                 memcpy(buf, ip_as_string, cp-ip_as_string);
2131                 buf[cp-ip_as_string] = '\0';
2132                 cp = buf;
2133         }
2134         if (!inet_aton(cp, &ina)) {
2135                 return 4;
2136         }
2137         return _evdns_nameserver_add_impl(ina.s_addr, port);
2138 }
2139
2140 /* insert into the tail of the queue */
2141 static void
2142 evdns_request_insert(struct request *req, struct request **head) {
2143         if (!*head) {
2144                 *head = req;
2145                 req->next = req->prev = req;
2146                 return;
2147         }
2148
2149         req->prev = (*head)->prev;
2150         req->prev->next = req;
2151         req->next = *head;
2152         (*head)->prev = req;
2153 }
2154
2155 static int
2156 string_num_dots(const char *s) {
2157         int count = 0;
2158         while ((s = strchr(s, '.'))) {
2159                 s++;
2160                 count++;
2161         }
2162         return count;
2163 }
2164
2165 static struct request *
2166 request_new(int type, const char *name, int flags,
2167     evdns_callback_type callback, void *user_ptr) {
2168         const char issuing_now =
2169             (global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
2170
2171         const int name_len = strlen(name);
2172         const int request_max_len = evdns_request_len(name_len);
2173         const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
2174         /* the request data is alloced in a single block with the header */
2175         struct request *const req =
2176             (struct request *) malloc(sizeof(struct request) + request_max_len);
2177         int rlen;
2178         (void) flags;
2179
2180         if (!req) return NULL;
2181         memset(req, 0, sizeof(struct request));
2182
2183         /* request data lives just after the header */
2184         req->request = ((u8 *) req) + sizeof(struct request);
2185         /* denotes that the request data shouldn't be free()ed */
2186         req->request_appended = 1;
2187         rlen = evdns_request_data_build(name, name_len, trans_id,
2188             type, CLASS_INET, req->request, request_max_len);
2189         if (rlen < 0)
2190                 goto err1;
2191         req->request_len = rlen;
2192         req->trans_id = trans_id;
2193         req->tx_count = 0;
2194         req->request_type = type;
2195         req->user_pointer = user_ptr;
2196         req->user_callback = callback;
2197         req->ns = issuing_now ? nameserver_pick() : NULL;
2198         req->next = req->prev = NULL;
2199
2200         return req;
2201 err1:
2202         free(req);
2203         return NULL;
2204 }
2205
2206 static void
2207 request_submit(struct request *const req) {
2208         if (req->ns) {
2209                 /* if it has a nameserver assigned then this is going */
2210                 /* straight into the inflight queue */
2211                 evdns_request_insert(req, &req_head);
2212                 global_requests_inflight++;
2213                 evdns_request_transmit(req);
2214         } else {
2215                 evdns_request_insert(req, &req_waiting_head);
2216                 global_requests_waiting++;
2217         }
2218 }
2219
2220 /* exported function */
2221 int evdns_resolve_ipv4(const char *name, int flags,
2222     evdns_callback_type callback, void *ptr) {
2223         log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2224         if (flags & DNS_QUERY_NO_SEARCH) {
2225                 struct request *const req =
2226                         request_new(TYPE_A, name, flags, callback, ptr);
2227                 if (req == NULL)
2228                         return (1);
2229                 request_submit(req);
2230                 return (0);
2231         } else {
2232                 return (search_request_new(TYPE_A, name, flags, callback, ptr));
2233         }
2234 }
2235
2236 /* exported function */
2237 int evdns_resolve_ipv6(const char *name, int flags,
2238                                            evdns_callback_type callback, void *ptr) {
2239         log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2240         if (flags & DNS_QUERY_NO_SEARCH) {
2241                 struct request *const req =
2242                         request_new(TYPE_AAAA, name, flags, callback, ptr);
2243                 if (req == NULL)
2244                         return (1);
2245                 request_submit(req);
2246                 return (0);
2247         } else {
2248                 return (search_request_new(TYPE_AAAA, name, flags, callback, ptr));
2249         }
2250 }
2251
2252 int evdns_resolve_reverse(struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2253         char buf[32];
2254         struct request *req;
2255         u32 a;
2256         assert(in);
2257         a = ntohl(in->s_addr);
2258         snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
2259                         (int)(u8)((a    )&0xff),
2260                         (int)(u8)((a>>8 )&0xff),
2261                         (int)(u8)((a>>16)&0xff),
2262                         (int)(u8)((a>>24)&0xff));
2263         log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
2264         req = request_new(TYPE_PTR, buf, flags, callback, ptr);
2265         if (!req) return 1;
2266         request_submit(req);
2267         return 0;
2268 }
2269
2270 int evdns_resolve_reverse_ipv6(struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2271         char buf[96];
2272         char *cp;
2273         struct request *req;
2274         int i;
2275         assert(in);
2276         cp = buf;
2277         for (i=15; i >= 0; --i) {
2278                 u8 byte = in->s6_addr[i];
2279                 *cp++ = "0123456789abcdef"[byte & 0x0f];
2280                 *cp++ = '.';
2281                 *cp++ = "0123456789abcdef"[byte >> 4];
2282                 *cp++ = '.';
2283         }
2284         assert(cp + strlen(".ip6.arpa") < buf+sizeof(buf));
2285         memcpy(cp, ".ip6.arpa", strlen(".ip6.arpa")+1);
2286         log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
2287         req = request_new(TYPE_PTR, buf, flags, callback, ptr);
2288         if (!req) return 1;
2289         request_submit(req);
2290         return 0;
2291 }
2292
2293 /*/////////////////////////////////////////////////////////////////// */
2294 /* Search support */
2295 /* */
2296 /* the libc resolver has support for searching a number of domains */
2297 /* to find a name. If nothing else then it takes the single domain */
2298 /* from the gethostname() call. */
2299 /* */
2300 /* It can also be configured via the domain and search options in a */
2301 /* resolv.conf. */
2302 /* */
2303 /* The ndots option controls how many dots it takes for the resolver */
2304 /* to decide that a name is non-local and so try a raw lookup first. */
2305
2306 struct search_domain {
2307         int len;
2308         struct search_domain *next;
2309         /* the text string is appended to this structure */
2310 };
2311
2312 struct search_state {
2313         int refcount;
2314         int ndots;
2315         int num_domains;
2316         struct search_domain *head;
2317 };
2318
2319 static struct search_state *global_search_state = NULL;
2320
2321 static void
2322 search_state_decref(struct search_state *const state) {
2323         if (!state) return;
2324         state->refcount--;
2325         if (!state->refcount) {
2326                 struct search_domain *next, *dom;
2327                 for (dom = state->head; dom; dom = next) {
2328                         next = dom->next;
2329                         free(dom);
2330                 }
2331                 free(state);
2332         }
2333 }
2334
2335 static struct search_state *
2336 search_state_new(void) {
2337         struct search_state *state = (struct search_state *) malloc(sizeof(struct search_state));
2338         if (!state) return NULL;
2339         memset(state, 0, sizeof(struct search_state));
2340         state->refcount = 1;
2341         state->ndots = 1;
2342
2343         return state;
2344 }
2345
2346 static void
2347 search_postfix_clear(void) {
2348         search_state_decref(global_search_state);
2349
2350         global_search_state = search_state_new();
2351 }
2352
2353 /* exported function */
2354 void
2355 evdns_search_clear(void) {
2356         search_postfix_clear();
2357 }
2358
2359 static void
2360 search_postfix_add(const char *domain) {
2361         int domain_len;
2362         struct search_domain *sdomain;
2363         while (domain[0] == '.') domain++;
2364         domain_len = strlen(domain);
2365
2366         if (!global_search_state) global_search_state = search_state_new();
2367         if (!global_search_state) return;
2368         global_search_state->num_domains++;
2369
2370         sdomain = (struct search_domain *) malloc(sizeof(struct search_domain) + domain_len);
2371         if (!sdomain) return;
2372         memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
2373         sdomain->next = global_search_state->head;
2374         sdomain->len = domain_len;
2375
2376         global_search_state->head = sdomain;
2377 }
2378
2379 /* reverse the order of members in the postfix list. This is needed because, */
2380 /* when parsing resolv.conf we push elements in the wrong order */
2381 static void
2382 search_reverse(void) {
2383         struct search_domain *cur, *prev = NULL, *next;
2384         cur = global_search_state->head;
2385         while (cur) {
2386                 next = cur->next;
2387                 cur->next = prev;
2388                 prev = cur;
2389                 cur = next;
2390         }
2391
2392         global_search_state->head = prev;
2393 }
2394
2395 /* exported function */
2396 void
2397 evdns_search_add(const char *domain) {
2398         search_postfix_add(domain);
2399 }
2400
2401 /* exported function */
2402 void
2403 evdns_search_ndots_set(const int ndots) {
2404         if (!global_search_state) global_search_state = search_state_new();
2405         if (!global_search_state) return;
2406         global_search_state->ndots = ndots;
2407 }
2408
2409 static void
2410 search_set_from_hostname(void) {
2411         char hostname[HOST_NAME_MAX + 1], *domainname;
2412
2413         search_postfix_clear();
2414         if (gethostname(hostname, sizeof(hostname))) return;
2415         domainname = strchr(hostname, '.');
2416         if (!domainname) return;
2417         search_postfix_add(domainname);
2418 }
2419
2420 /* warning: returns malloced string */
2421 static char *
2422 search_make_new(const struct search_state *const state, int n, const char *const base_name) {
2423         const int base_len = strlen(base_name);
2424         const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
2425         struct search_domain *dom;
2426
2427         for (dom = state->head; dom; dom = dom->next) {
2428                 if (!n--) {
2429                         /* this is the postfix we want */
2430                         /* the actual postfix string is kept at the end of the structure */
2431                         const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
2432                         const int postfix_len = dom->len;
2433                         char *const newname = (char *) malloc(base_len + need_to_append_dot + postfix_len + 1);
2434                         if (!newname) return NULL;
2435                         memcpy(newname, base_name, base_len);
2436                         if (need_to_append_dot) newname[base_len] = '.';
2437                         memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
2438                         newname[base_len + need_to_append_dot + postfix_len] = 0;
2439                         return newname;
2440                 }
2441         }
2442
2443         /* we ran off the end of the list and still didn't find the requested string */
2444         abort();
2445         return NULL; /* unreachable; stops warnings in some compilers. */
2446 }
2447
2448 static int
2449 search_request_new(int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg) {
2450         assert(type == TYPE_A || type == TYPE_AAAA);
2451         if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
2452              global_search_state &&
2453                  global_search_state->num_domains) {
2454                 /* we have some domains to search */
2455                 struct request *req;
2456                 if (string_num_dots(name) >= global_search_state->ndots) {
2457                         req = request_new(type, name, flags, user_callback, user_arg);
2458                         if (!req) return 1;
2459                         req->search_index = -1;
2460                 } else {
2461                         char *const new_name = search_make_new(global_search_state, 0, name);
2462                         if (!new_name) return 1;
2463                         req = request_new(type, new_name, flags, user_callback, user_arg);
2464                         free(new_name);
2465                         if (!req) return 1;
2466                         req->search_index = 0;
2467                 }
2468                 req->search_origname = strdup(name);
2469                 req->search_state = global_search_state;
2470                 req->search_flags = flags;
2471                 global_search_state->refcount++;
2472                 request_submit(req);
2473                 return 0;
2474         } else {
2475                 struct request *const req = request_new(type, name, flags, user_callback, user_arg);
2476                 if (!req) return 1;
2477                 request_submit(req);
2478                 return 0;
2479         }
2480 }
2481
2482 /* this is called when a request has failed to find a name. We need to check */
2483 /* if it is part of a search and, if so, try the next name in the list */
2484 /* returns: */
2485 /*   0 another request has been submitted */
2486 /*   1 no more requests needed */
2487 static int
2488 search_try_next(struct request *const req) {
2489         if (req->search_state) {
2490                 /* it is part of a search */
2491                 char *new_name;
2492                 struct request *newreq;
2493                 req->search_index++;
2494                 if (req->search_index >= req->search_state->num_domains) {
2495                         /* no more postfixes to try, however we may need to try */
2496                         /* this name without a postfix */
2497                         if (string_num_dots(req->search_origname) < req->search_state->ndots) {
2498                                 /* yep, we need to try it raw */
2499                                 struct request *const newreq = request_new(req->request_type, req->search_origname, req->search_flags, req->user_callback, req->user_pointer);
2500                                 log(EVDNS_LOG_DEBUG, "Search: trying raw query %s", req->search_origname);
2501                                 if (newreq) {
2502                                         request_submit(newreq);
2503                                         return 0;
2504                                 }
2505                         }
2506                         return 1;
2507                 }
2508
2509                 new_name = search_make_new(req->search_state, req->search_index, req->search_origname);
2510                 if (!new_name) return 1;
2511                 log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, req->search_index);
2512                 newreq = request_new(req->request_type, new_name, req->search_flags, req->user_callback, req->user_pointer);
2513                 free(new_name);
2514                 if (!newreq) return 1;
2515                 newreq->search_origname = req->search_origname;
2516                 req->search_origname = NULL;
2517                 newreq->search_state = req->search_state;
2518                 newreq->search_flags = req->search_flags;
2519                 newreq->search_index = req->search_index;
2520                 newreq->search_state->refcount++;
2521                 request_submit(newreq);
2522                 return 0;
2523         }
2524         return 1;
2525 }
2526
2527 static void
2528 search_request_finished(struct request *const req) {
2529         if (req->search_state) {
2530                 search_state_decref(req->search_state);
2531                 req->search_state = NULL;
2532         }
2533         if (req->search_origname) {
2534                 free(req->search_origname);
2535                 req->search_origname = NULL;
2536         }
2537 }
2538
2539 /*/////////////////////////////////////////////////////////////////// */
2540 /* Parsing resolv.conf files */
2541
2542 static void
2543 evdns_resolv_set_defaults(int flags) {
2544         /* if the file isn't found then we assume a local resolver */
2545         if (flags & DNS_OPTION_SEARCH) search_set_from_hostname();
2546         if (flags & DNS_OPTION_NAMESERVERS) evdns_nameserver_ip_add("127.0.0.1");
2547 }
2548
2549 #ifndef HAVE_STRTOK_R
2550 static char *
2551 strtok_r(char *s, const char *delim, char **state) {
2552         return strtok(s, delim);
2553 }
2554 #endif
2555
2556 /* helper version of atoi which returns -1 on error */
2557 static int
2558 strtoint(const char *const str) {
2559         char *endptr;
2560         const int r = strtol(str, &endptr, 10);
2561         if (*endptr) return -1;
2562         return r;
2563 }
2564
2565 /* helper version of atoi that returns -1 on error and clips to bounds. */
2566 static int
2567 strtoint_clipped(const char *const str, int min, int max)
2568 {
2569         int r = strtoint(str);
2570         if (r == -1)
2571                 return r;
2572         else if (r<min)
2573                 return min;
2574         else if (r>max)
2575                 return max;
2576         else
2577                 return r;
2578 }
2579
2580 /* exported function */
2581 int
2582 evdns_set_option(const char *option, const char *val, int flags)
2583 {
2584         if (!strncmp(option, "ndots:", 6)) {
2585                 const int ndots = strtoint(val);
2586                 if (ndots == -1) return -1;
2587                 if (!(flags & DNS_OPTION_SEARCH)) return 0;
2588                 log(EVDNS_LOG_DEBUG, "Setting ndots to %d", ndots);
2589                 if (!global_search_state) global_search_state = search_state_new();
2590                 if (!global_search_state) return -1;
2591                 global_search_state->ndots = ndots;
2592         } else if (!strncmp(option, "timeout:", 8)) {
2593                 const int timeout = strtoint(val);
2594                 if (timeout == -1) return -1;
2595                 if (!(flags & DNS_OPTION_MISC)) return 0;
2596                 log(EVDNS_LOG_DEBUG, "Setting timeout to %d", timeout);
2597                 global_timeout.tv_sec = timeout;
2598         } else if (!strncmp(option, "max-timeouts:", 12)) {
2599                 const int maxtimeout = strtoint_clipped(val, 1, 255);
2600                 if (maxtimeout == -1) return -1;
2601                 if (!(flags & DNS_OPTION_MISC)) return 0;
2602                 log(EVDNS_LOG_DEBUG, "Setting maximum allowed timeouts to %d",
2603                         maxtimeout);
2604                 global_max_nameserver_timeout = maxtimeout;
2605         } else if (!strncmp(option, "max-inflight:", 13)) {
2606                 const int maxinflight = strtoint_clipped(val, 1, 65000);
2607                 if (maxinflight == -1) return -1;
2608                 if (!(flags & DNS_OPTION_MISC)) return 0;
2609                 log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d",
2610                         maxinflight);
2611                 global_max_requests_inflight = maxinflight;
2612         } else if (!strncmp(option, "attempts:", 9)) {
2613                 int retries = strtoint(val);
2614                 if (retries == -1) return -1;
2615                 if (retries > 255) retries = 255;
2616                 if (!(flags & DNS_OPTION_MISC)) return 0;
2617                 log(EVDNS_LOG_DEBUG, "Setting retries to %d", retries);
2618                 global_max_retransmits = retries;
2619         }
2620         return 0;
2621 }
2622
2623 static void
2624 resolv_conf_parse_line(char *const start, int flags) {
2625         char *strtok_state;
2626         static const char *const delims = " \t";
2627 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
2628
2629         char *const first_token = strtok_r(start, delims, &strtok_state);
2630         if (!first_token) return;
2631
2632         if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
2633                 const char *const nameserver = NEXT_TOKEN;
2634                 struct in_addr ina;
2635
2636                 if (inet_aton(nameserver, &ina)) {
2637                         /* address is valid */
2638                         evdns_nameserver_add(ina.s_addr);
2639                 }
2640         } else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
2641                 const char *const domain = NEXT_TOKEN;
2642                 if (domain) {
2643                         search_postfix_clear();
2644                         search_postfix_add(domain);
2645                 }
2646         } else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
2647                 const char *domain;
2648                 search_postfix_clear();
2649
2650                 while ((domain = NEXT_TOKEN)) {
2651                         search_postfix_add(domain);
2652                 }
2653                 search_reverse();
2654         } else if (!strcmp(first_token, "options")) {
2655                 const char *option;
2656                 while ((option = NEXT_TOKEN)) {
2657                         const char *val = strchr(option, ':');
2658                         evdns_set_option(option, val ? val+1 : "", flags);
2659                 }
2660         }
2661 #undef NEXT_TOKEN
2662 }
2663
2664 /* exported function */
2665 /* returns: */
2666 /*   0 no errors */
2667 /*   1 failed to open file */
2668 /*   2 failed to stat file */
2669 /*   3 file too large */
2670 /*   4 out of memory */
2671 /*   5 short read from file */
2672 int
2673 evdns_resolv_conf_parse(int flags, const char *const filename) {
2674         struct stat st;
2675         int fd, n, r;
2676         u8 *resolv;
2677         char *start;
2678         int err = 0;
2679
2680         log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
2681
2682         fd = open(filename, O_RDONLY);
2683         if (fd < 0) {
2684                 evdns_resolv_set_defaults(flags);
2685                 return 1;
2686         }
2687
2688         if (fstat(fd, &st)) { err = 2; goto out1; }
2689         if (!st.st_size) {
2690                 evdns_resolv_set_defaults(flags);
2691                 err = (flags & DNS_OPTION_NAMESERVERS) ? 6 : 0;
2692                 goto out1;
2693         }
2694         if (st.st_size > 65535) { err = 3; goto out1; }  /* no resolv.conf should be any bigger */
2695
2696         resolv = (u8 *) malloc((size_t)st.st_size + 1);
2697         if (!resolv) { err = 4; goto out1; }
2698
2699         n = 0;
2700         while ((r = read(fd, resolv+n, (size_t)st.st_size-n)) > 0) {
2701                 n += r;
2702                 if (n == st.st_size)
2703                         break;
2704                 assert(n < st.st_size);
2705         }
2706         if (r < 0) { err = 5; goto out2; }
2707         resolv[n] = 0;   /* we malloced an extra byte; this should be fine. */
2708
2709         start = (char *) resolv;
2710         for (;;) {
2711                 char *const newline = strchr(start, '\n');
2712                 if (!newline) {
2713                         resolv_conf_parse_line(start, flags);
2714                         break;
2715                 } else {
2716                         *newline = 0;
2717                         resolv_conf_parse_line(start, flags);
2718                         start = newline + 1;
2719                 }
2720         }
2721
2722         if (!server_head && (flags & DNS_OPTION_NAMESERVERS)) {
2723                 /* no nameservers were configured. */
2724                 evdns_nameserver_ip_add("127.0.0.1");
2725                 err = 6;
2726         }
2727         if (flags & DNS_OPTION_SEARCH && (!global_search_state || global_search_state->num_domains == 0)) {
2728                 search_set_from_hostname();
2729         }
2730
2731 out2:
2732         free(resolv);
2733 out1:
2734         close(fd);
2735         return err;
2736 }
2737
2738 #ifdef WIN32
2739 /* Add multiple nameservers from a space-or-comma-separated list. */
2740 static int
2741 evdns_nameserver_ip_add_line(const char *ips) {
2742         const char *addr;
2743         char *buf;
2744         int r;
2745         while (*ips) {
2746                 while (ISSPACE(*ips) || *ips == ',' || *ips == '\t')
2747                         ++ips;
2748                 addr = ips;
2749                 while (ISDIGIT(*ips) || *ips == '.' || *ips == ':')
2750                         ++ips;
2751                 buf = malloc(ips-addr+1);
2752                 if (!buf) return 4;
2753                 memcpy(buf, addr, ips-addr);
2754                 buf[ips-addr] = '\0';
2755                 r = evdns_nameserver_ip_add(buf);
2756                 free(buf);
2757                 if (r) return r;
2758         }
2759         return 0;
2760 }
2761
2762 typedef DWORD(WINAPI *GetNetworkParams_fn_t)(FIXED_INFO *, DWORD*);
2763
2764 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
2765 /* figure out what our nameservers are. */
2766 static int
2767 load_nameservers_with_getnetworkparams(void)
2768 {
2769         /* Based on MSDN examples and inspection of  c-ares code. */
2770         FIXED_INFO *fixed;
2771         HMODULE handle = 0;
2772         ULONG size = sizeof(FIXED_INFO);
2773         void *buf = NULL;
2774         int status = 0, r, added_any;
2775         IP_ADDR_STRING *ns;
2776         GetNetworkParams_fn_t fn;
2777
2778         if (!(handle = LoadLibrary("iphlpapi.dll"))) {
2779                 log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
2780                 status = -1;
2781                 goto done;
2782         }
2783         if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, "GetNetworkParams"))) {
2784                 log(EVDNS_LOG_WARN, "Could not get address of function.");
2785                 status = -1;
2786                 goto done;
2787         }
2788
2789         buf = malloc(size);
2790         if (!buf) { status = 4; goto done; }
2791         fixed = buf;
2792         r = fn(fixed, &size);
2793         if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
2794                 status = -1;
2795                 goto done;
2796         }
2797         if (r != ERROR_SUCCESS) {
2798                 free(buf);
2799                 buf = malloc(size);
2800                 if (!buf) { status = 4; goto done; }
2801                 fixed = buf;
2802                 r = fn(fixed, &size);
2803                 if (r != ERROR_SUCCESS) {
2804                         log(EVDNS_LOG_DEBUG, "fn() failed.");
2805                         status = -1;
2806                         goto done;
2807                 }
2808         }
2809
2810         assert(fixed);
2811         added_any = 0;
2812         ns = &(fixed->DnsServerList);
2813         while (ns) {
2814                 r = evdns_nameserver_ip_add_line(ns->IpAddress.String);
2815                 if (r) {
2816                         log(EVDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
2817                                 (ns->IpAddress.String),(int)GetLastError());
2818                         status = r;
2819                         goto done;
2820                 } else {
2821                         log(EVDNS_LOG_DEBUG,"Succesfully added %s as nameserver",ns->IpAddress.String);
2822                 }
2823
2824                 added_any++;
2825                 ns = ns->Next;
2826         }
2827
2828         if (!added_any) {
2829                 log(EVDNS_LOG_DEBUG, "No nameservers added.");
2830                 status = -1;
2831         }
2832
2833  done:
2834         if (buf)
2835                 free(buf);
2836         if (handle)
2837                 FreeLibrary(handle);
2838         return status;
2839 }
2840
2841 static int
2842 config_nameserver_from_reg_key(HKEY key, const char *subkey)
2843 {
2844         char *buf;
2845         DWORD bufsz = 0, type = 0;
2846         int status = 0;
2847
2848         if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
2849             != ERROR_MORE_DATA)
2850                 return -1;
2851         if (!(buf = malloc(bufsz)))
2852                 return -1;
2853
2854         if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
2855             == ERROR_SUCCESS && bufsz > 1) {
2856                 status = evdns_nameserver_ip_add_line(buf);
2857         }
2858
2859         free(buf);
2860         return status;
2861 }
2862
2863 #define SERVICES_KEY "System\\CurrentControlSet\\Services\\"
2864 #define WIN_NS_9X_KEY  SERVICES_KEY "VxD\\MSTCP"
2865 #define WIN_NS_NT_KEY  SERVICES_KEY "Tcpip\\Parameters"
2866
2867 static int
2868 load_nameservers_from_registry(void)
2869 {
2870         int found = 0;
2871         int r;
2872 #define TRY(k, name) \
2873         if (!found && config_nameserver_from_reg_key(k,name) == 0) {    \
2874                 log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
2875                 found = 1;                                              \
2876         } else if (!found) {                                            \
2877                 log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
2878                     #k,#name);                                          \
2879         }
2880
2881         if (((int)GetVersion()) > 0) { /* NT */
2882                 HKEY nt_key = 0, interfaces_key = 0;
2883
2884                 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
2885                                  KEY_READ, &nt_key) != ERROR_SUCCESS) {
2886                         log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
2887                         return -1;
2888                 }
2889                 r = RegOpenKeyEx(nt_key, "Interfaces", 0,
2890                              KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
2891                              &interfaces_key);
2892                 if (r != ERROR_SUCCESS) {
2893                         log(EVDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
2894                         return -1;
2895                 }
2896                 TRY(nt_key, "NameServer");
2897                 TRY(nt_key, "DhcpNameServer");
2898                 TRY(interfaces_key, "NameServer");
2899                 TRY(interfaces_key, "DhcpNameServer");
2900                 RegCloseKey(interfaces_key);
2901                 RegCloseKey(nt_key);
2902         } else {
2903                 HKEY win_key = 0;
2904                 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
2905                                  KEY_READ, &win_key) != ERROR_SUCCESS) {
2906                         log(EVDNS_LOG_DEBUG, "Couldn't open registry key, %d", (int)GetLastError());
2907                         return -1;
2908                 }
2909                 TRY(win_key, "NameServer");
2910                 RegCloseKey(win_key);
2911         }
2912
2913         if (found == 0) {
2914                 log(EVDNS_LOG_WARN,"Didn't find any nameservers.");
2915         }
2916
2917         return found ? 0 : -1;
2918 #undef TRY
2919 }
2920
2921 int
2922 evdns_config_windows_nameservers(void)
2923 {
2924         if (load_nameservers_with_getnetworkparams() == 0)
2925                 return 0;
2926         return load_nameservers_from_registry();
2927 }
2928 #endif
2929
2930 int
2931 evdns_init(void)
2932 {
2933         int res = 0;
2934 #ifdef WIN32
2935         evdns_config_windows_nameservers();
2936 #else
2937         res = evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
2938 #endif
2939
2940         return (res);
2941 }
2942
2943 const char *
2944 evdns_err_to_string(int err)
2945 {
2946     switch (err) {
2947         case DNS_ERR_NONE: return "no error";
2948         case DNS_ERR_FORMAT: return "misformatted query";
2949         case DNS_ERR_SERVERFAILED: return "server failed";
2950         case DNS_ERR_NOTEXIST: return "name does not exist";
2951         case DNS_ERR_NOTIMPL: return "query not implemented";
2952         case DNS_ERR_REFUSED: return "refused";
2953
2954         case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
2955         case DNS_ERR_UNKNOWN: return "unknown";
2956         case DNS_ERR_TIMEOUT: return "request timed out";
2957         case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
2958         default: return "[Unknown error code]";
2959     }
2960 }
2961
2962 void
2963 evdns_shutdown(int fail_requests)
2964 {
2965         struct nameserver *server, *server_next;
2966         struct search_domain *dom, *dom_next;
2967
2968         while (req_head) {
2969                 if (fail_requests)
2970                         reply_callback(req_head, 0, DNS_ERR_SHUTDOWN, NULL);
2971                 request_finished(req_head, &req_head);
2972         }
2973         while (req_waiting_head) {
2974                 if (fail_requests)
2975                         reply_callback(req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
2976                 request_finished(req_waiting_head, &req_waiting_head);
2977         }
2978         global_requests_inflight = global_requests_waiting = 0;
2979
2980         for (server = server_head; server; server = server_next) {
2981                 server_next = server->next;
2982                 if (server->socket >= 0)
2983                         CLOSE_SOCKET(server->socket);
2984                 (void) event_del(&server->event);
2985                 if (server->state == 0)
2986                         (void) event_del(&server->timeout_event);
2987                 free(server);
2988                 if (server_next == server_head)
2989                         break;
2990         }
2991         server_head = NULL;
2992         global_good_nameservers = 0;
2993
2994         if (global_search_state) {
2995                 for (dom = global_search_state->head; dom; dom = dom_next) {
2996                         dom_next = dom->next;
2997                         free(dom);
2998                 }
2999                 free(global_search_state);
3000                 global_search_state = NULL;
3001         }
3002         evdns_log_fn = NULL;
3003 }
3004
3005 #ifdef EVDNS_MAIN
3006 void
3007 main_callback(int result, char type, int count, int ttl,
3008                           void *addrs, void *orig) {
3009         char *n = (char*)orig;
3010         int i;
3011         for (i = 0; i < count; ++i) {
3012                 if (type == DNS_IPv4_A) {
3013                         printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
3014                 } else if (type == DNS_PTR) {
3015                         printf("%s: %s\n", n, ((char**)addrs)[i]);
3016                 }
3017         }
3018         if (!count) {
3019                 printf("%s: No answer (%d)\n", n, result);
3020         }
3021         fflush(stdout);
3022 }
3023 void
3024 evdns_server_callback(struct evdns_server_request *req, void *data)
3025 {
3026         int i, r;
3027         (void)data;
3028         /* dummy; give 192.168.11.11 as an answer for all A questions,
3029          *      give foo.bar.example.com as an answer for all PTR questions. */
3030         for (i = 0; i < req->nquestions; ++i) {
3031                 u32 ans = htonl(0xc0a80b0bUL);
3032                 if (req->questions[i]->type == EVDNS_TYPE_A &&
3033                         req->questions[i]->class == EVDNS_CLASS_INET) {
3034                         printf(" -- replying for %s (A)\n", req->questions[i]->name);
3035                         r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
3036                                                                                   1, &ans, 10);
3037                         if (r<0)
3038                                 printf("eeep, didn't work.\n");
3039                 } else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
3040                                    req->questions[i]->class == EVDNS_CLASS_INET) {
3041                         printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
3042                         r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
3043                                                                                         "foo.bar.example.com", 10);
3044                 } else {
3045                         printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
3046                                    req->questions[i]->type, req->questions[i]->class);
3047                 }
3048         }
3049
3050         r = evdns_request_respond(req, 0);
3051         if (r<0)
3052                 printf("eeek, couldn't send reply.\n");
3053 }
3054
3055 void
3056 logfn(int is_warn, const char *msg) {
3057   (void) is_warn;
3058   fprintf(stderr, "%s\n", msg);
3059 }
3060 int
3061 main(int c, char **v) {
3062         int idx;
3063         int reverse = 0, verbose = 1, servertest = 0;
3064         if (c<2) {
3065                 fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
3066                 fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
3067                 return 1;
3068         }
3069         idx = 1;
3070         while (idx < c && v[idx][0] == '-') {
3071                 if (!strcmp(v[idx], "-x"))
3072                         reverse = 1;
3073                 else if (!strcmp(v[idx], "-v"))
3074                         verbose = 1;
3075                 else if (!strcmp(v[idx], "-servertest"))
3076                         servertest = 1;
3077                 else
3078                         fprintf(stderr, "Unknown option %s\n", v[idx]);
3079                 ++idx;
3080         }
3081         event_init();
3082         if (verbose)
3083                 evdns_set_log_fn(logfn);
3084         evdns_resolv_conf_parse(DNS_OPTION_NAMESERVERS, "/etc/resolv.conf");
3085         if (servertest) {
3086                 int sock;
3087                 struct sockaddr_in my_addr;
3088                 sock = socket(PF_INET, SOCK_DGRAM, 0);
3089                 fcntl(sock, F_SETFL, O_NONBLOCK);
3090                 my_addr.sin_family = AF_INET;
3091                 my_addr.sin_port = htons(10053);
3092                 my_addr.sin_addr.s_addr = INADDR_ANY;
3093                 if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) {
3094                         perror("bind");
3095                         exit(1);
3096                 }
3097                 evdns_add_server_port(sock, 0, evdns_server_callback, NULL);
3098         }
3099         for (; idx < c; ++idx) {
3100                 if (reverse) {
3101                         struct in_addr addr;
3102                         if (!inet_aton(v[idx], &addr)) {
3103                                 fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
3104                                 continue;
3105                         }
3106                         fprintf(stderr, "resolving %s...\n",v[idx]);
3107                         evdns_resolve_reverse(&addr, 0, main_callback, v[idx]);
3108                 } else {
3109                         fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
3110                         evdns_resolve_ipv4(v[idx], 0, main_callback, v[idx]);
3111                 }
3112         }
3113         fflush(stdout);
3114         event_dispatch();
3115         return 0;
3116 }
3117 #endif