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