1 /* Copyright 2006-2007 Niels Provos
2 * Copyright 2007-2012 Nick Mathewson and Niels Provos
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Based on software by Adam Langly. Adam's original message:
30 * Adam Langley <agl@imperialviolet.org>
31 * http://www.imperialviolet.org/eventdns.html
34 * This software is Public Domain. To view a copy of the public domain dedication,
35 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
36 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
38 * I ask and expect, but do not require, that all derivative works contain an
39 * attribution similar to:
40 * Parts developed by Adam Langley <agl@imperialviolet.org>
42 * You may wish to replace the word "Parts" with something else depending on
43 * the amount of original code.
45 * (Derivative works does not include programs which link against, run or include
46 * the source verbatim in their source distributions)
51 #include <sys/types.h>
52 #include "event2/event-config.h"
54 #ifndef _FORTIFY_SOURCE
55 #define _FORTIFY_SOURCE 3
60 #ifdef _EVENT_HAVE_SYS_TIME_H
63 #ifdef _EVENT_HAVE_STDINT_H
69 #ifdef _EVENT_HAVE_UNISTD_H
80 #define _WIN32_IE 0x400
85 #include "event2/dns.h"
86 #include "event2/dns_struct.h"
87 #include "event2/dns_compat.h"
88 #include "event2/util.h"
89 #include "event2/event.h"
90 #include "event2/event_struct.h"
91 #include "event2/thread.h"
93 #include "event2/bufferevent.h"
94 #include "event2/bufferevent_struct.h"
95 #include "bufferevent-internal.h"
97 #include "defer-internal.h"
98 #include "log-internal.h"
99 #include "mm-internal.h"
100 #include "strlcpy-internal.h"
101 #include "ipv6-internal.h"
102 #include "util-internal.h"
103 #include "evthread-internal.h"
106 #include <winsock2.h>
108 #include <iphlpapi.h>
111 #include <sys/socket.h>
112 #include <netinet/in.h>
113 #include <arpa/inet.h>
116 #ifdef _EVENT_HAVE_NETINET_IN6_H
117 #include <netinet/in6.h>
120 #define EVDNS_LOG_DEBUG 0
121 #define EVDNS_LOG_WARN 1
122 #define EVDNS_LOG_MSG 2
124 #ifndef HOST_NAME_MAX
125 #define HOST_NAME_MAX 255
131 #define MIN(a,b) ((a)<(b)?(a):(b))
133 #define ASSERT_VALID_REQUEST(req) \
134 EVUTIL_ASSERT((req)->handle && (req)->handle->current_req == (req))
136 #define u64 ev_uint64_t
137 #define u32 ev_uint32_t
138 #define u16 ev_uint16_t
139 #define u8 ev_uint8_t
141 /* maximum number of addresses from a single packet */
142 /* that we bother recording */
143 #define MAX_V4_ADDRS 32
144 #define MAX_V6_ADDRS 32
147 #define TYPE_A EVDNS_TYPE_A
149 #define TYPE_PTR EVDNS_TYPE_PTR
150 #define TYPE_SOA EVDNS_TYPE_SOA
151 #define TYPE_AAAA EVDNS_TYPE_AAAA
153 #define CLASS_INET EVDNS_CLASS_INET
155 /* Persistent handle. We keep this separate from 'struct request' since we
156 * need some object to last for as long as an evdns_request is outstanding so
157 * that it can be canceled, whereas a search request can lead to multiple
158 * 'struct request' instances being created over its lifetime. */
159 struct evdns_request
{
160 struct request
*current_req
;
161 struct evdns_base
*base
;
163 int pending_cb
; /* Waiting for its callback to be invoked; not
164 * owned by event base any more. */
166 /* elements used by the searching code */
168 struct search_state
*search_state
;
169 char *search_origname
; /* needs to be free()ed */
174 u8
*request
; /* the dns packet data */
175 u8 request_type
; /* TYPE_PTR or TYPE_A or TYPE_AAAA */
176 unsigned int request_len
;
178 int tx_count
; /* the number of times that this packet has been sent */
179 void *user_pointer
; /* the pointer given to us for this request */
180 evdns_callback_type user_callback
;
181 struct nameserver
*ns
; /* the server which we last sent it */
183 /* these objects are kept in a circular list */
184 /* XXX We could turn this into a CIRCLEQ. */
185 struct request
*next
, *prev
;
187 struct event timeout_event
;
189 u16 trans_id
; /* the transaction id */
190 unsigned request_appended
:1; /* true if the request pointer is data which follows this struct */
191 unsigned transmit_me
:1; /* needs to be transmitted */
193 /* XXXX This is a horrible hack. */
194 char **put_cname_in_ptr
; /* store the cname here if we get one. */
196 struct evdns_base
*base
;
198 struct evdns_request
*handle
;
203 unsigned int have_answer
: 1;
207 u32 addresses
[MAX_V4_ADDRS
];
211 struct in6_addr addresses
[MAX_V6_ADDRS
];
214 char name
[HOST_NAME_MAX
];
220 evutil_socket_t socket
; /* a connected UDP socket */
221 struct sockaddr_storage address
;
222 ev_socklen_t addrlen
;
223 int failed_times
; /* number of times which we have given this server a chance */
224 int timedout
; /* number of times in a row a request has timed out */
226 /* these objects are kept in a circular list */
227 struct nameserver
*next
, *prev
;
228 struct event timeout_event
; /* used to keep the timeout for */
229 /* when we next probe this server. */
230 /* Valid if state == 0 */
231 /* Outstanding probe request for this nameserver, if any */
232 struct evdns_request
*probe_request
;
233 char state
; /* zero if we think that this server is down */
234 char choked
; /* true if we have an EAGAIN from this server's socket */
235 char write_waiting
; /* true if we are waiting for EV_WRITE events */
236 struct evdns_base
*base
;
240 /* Represents a local port where we're listening for DNS requests. Right now, */
241 /* only UDP is supported. */
242 struct evdns_server_port
{
243 evutil_socket_t socket
; /* socket we use to read queries and write replies. */
244 int refcnt
; /* reference count. */
245 char choked
; /* Are we currently blocked from writing? */
246 char closing
; /* Are we trying to close this port, pending writes? */
247 evdns_request_callback_fn_type user_callback
; /* Fn to handle requests */
248 void *user_data
; /* Opaque pointer passed to user_callback */
249 struct event event
; /* Read/write event */
250 /* circular list of replies that we want to write. */
251 struct server_request
*pending_replies
;
252 struct event_base
*event_base
;
254 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
259 /* Represents part of a reply being built. (That is, a single RR.) */
260 struct server_reply_item
{
261 struct server_reply_item
*next
; /* next item in sequence. */
262 char *name
; /* name part of the RR */
263 u16 type
; /* The RR type */
264 u16
class; /* The RR class (usually CLASS_INET) */
265 u32 ttl
; /* The RR TTL */
266 char is_name
; /* True iff data is a label */
267 u16 datalen
; /* Length of data; -1 if data is a label */
268 void *data
; /* The contents of the RR */
271 /* Represents a request that we've received as a DNS server, and holds */
272 /* the components of the reply as we're constructing it. */
273 struct server_request
{
274 /* Pointers to the next and previous entries on the list of replies */
275 /* that we're waiting to write. Only set if we have tried to respond */
276 /* and gotten EAGAIN. */
277 struct server_request
*next_pending
;
278 struct server_request
*prev_pending
;
280 u16 trans_id
; /* Transaction id. */
281 struct evdns_server_port
*port
; /* Which port received this request on? */
282 struct sockaddr_storage addr
; /* Where to send the response */
283 ev_socklen_t addrlen
; /* length of addr */
285 int n_answer
; /* how many answer RRs have been set? */
286 int n_authority
; /* how many authority RRs have been set? */
287 int n_additional
; /* how many additional RRs have been set? */
289 struct server_reply_item
*answer
; /* linked list of answer RRs */
290 struct server_reply_item
*authority
; /* linked list of authority RRs */
291 struct server_reply_item
*additional
; /* linked list of additional RRs */
293 /* Constructed response. Only set once we're ready to send a reply. */
294 /* Once this is set, the RR fields are cleared, and no more should be set. */
298 /* Caller-visible fields: flags, questions. */
299 struct evdns_server_request base
;
303 /* An array of n_req_heads circular lists for inflight requests.
304 * Each inflight request req is in req_heads[req->trans_id % n_req_heads].
306 struct request
**req_heads
;
307 /* A circular list of requests that we're waiting to send, but haven't
308 * sent yet because there are too many requests inflight */
309 struct request
*req_waiting_head
;
310 /* A circular list of nameservers. */
311 struct nameserver
*server_head
;
314 struct event_base
*event_base
;
316 /* The number of good nameservers that we have */
317 int global_good_nameservers
;
319 /* inflight requests are contained in the req_head list */
320 /* and are actually going out across the network */
321 int global_requests_inflight
;
322 /* requests which aren't inflight are in the waiting list */
323 /* and are counted here */
324 int global_requests_waiting
;
326 int global_max_requests_inflight
;
328 struct timeval global_timeout
; /* 5 seconds by default */
329 int global_max_reissues
; /* a reissue occurs when we get some errors from the server */
330 int global_max_retransmits
; /* number of times we'll retransmit a request which timed out */
331 /* number of timeouts in a row before we consider this server to be down */
332 int global_max_nameserver_timeout
;
333 /* true iff we will use the 0x20 hack to prevent poisoning attacks. */
334 int global_randomize_case
;
336 /* The first time that a nameserver fails, how long do we wait before
337 * probing to see if it has returned? */
338 struct timeval global_nameserver_probe_initial_timeout
;
340 /** Port to bind to for outgoing DNS packets. */
341 struct sockaddr_storage global_outgoing_address
;
342 /** ev_socklen_t for global_outgoing_address. 0 if it isn't set. */
343 ev_socklen_t global_outgoing_addrlen
;
345 struct timeval global_getaddrinfo_allow_skew
;
347 int getaddrinfo_ipv4_timeouts
;
348 int getaddrinfo_ipv6_timeouts
;
349 int getaddrinfo_ipv4_answered
;
350 int getaddrinfo_ipv6_answered
;
352 struct search_state
*global_search_state
;
354 TAILQ_HEAD(hosts_list
, hosts_entry
) hostsdb
;
356 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
362 TAILQ_ENTRY(hosts_entry
) next
;
365 struct sockaddr_in sin
;
366 struct sockaddr_in6 sin6
;
372 static struct evdns_base
*current_base
= NULL
;
375 evdns_get_global_base(void)
380 /* Given a pointer to an evdns_server_request, get the corresponding */
381 /* server_request. */
382 #define TO_SERVER_REQUEST(base_ptr) \
383 ((struct server_request*) \
384 (((char*)(base_ptr) - evutil_offsetof(struct server_request, base))))
386 #define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads])
388 static struct nameserver
*nameserver_pick(struct evdns_base
*base
);
389 static void evdns_request_insert(struct request
*req
, struct request
**head
);
390 static void evdns_request_remove(struct request
*req
, struct request
**head
);
391 static void nameserver_ready_callback(evutil_socket_t fd
, short events
, void *arg
);
392 static int evdns_transmit(struct evdns_base
*base
);
393 static int evdns_request_transmit(struct request
*req
);
394 static void nameserver_send_probe(struct nameserver
*const ns
);
395 static void search_request_finished(struct evdns_request
*const);
396 static int search_try_next(struct evdns_request
*const req
);
397 static struct request
*search_request_new(struct evdns_base
*base
, struct evdns_request
*handle
, int type
, const char *const name
, int flags
, evdns_callback_type user_callback
, void *user_arg
);
398 static void evdns_requests_pump_waiting_queue(struct evdns_base
*base
);
399 static u16
transaction_id_pick(struct evdns_base
*base
);
400 static struct request
*request_new(struct evdns_base
*base
, struct evdns_request
*handle
, int type
, const char *name
, int flags
, evdns_callback_type callback
, void *ptr
);
401 static void request_submit(struct request
*const req
);
403 static int server_request_free(struct server_request
*req
);
404 static void server_request_free_answers(struct server_request
*req
);
405 static void server_port_free(struct evdns_server_port
*port
);
406 static void server_port_ready_callback(evutil_socket_t fd
, short events
, void *arg
);
407 static int evdns_base_resolv_conf_parse_impl(struct evdns_base
*base
, int flags
, const char *const filename
);
408 static int evdns_base_set_option_impl(struct evdns_base
*base
,
409 const char *option
, const char *val
, int flags
);
410 static void evdns_base_free_and_unlock(struct evdns_base
*base
, int fail_requests
);
412 static int strtoint(const char *const str
);
414 #ifdef _EVENT_DISABLE_THREAD_SUPPORT
415 #define EVDNS_LOCK(base) _EVUTIL_NIL_STMT
416 #define EVDNS_UNLOCK(base) _EVUTIL_NIL_STMT
417 #define ASSERT_LOCKED(base) _EVUTIL_NIL_STMT
419 #define EVDNS_LOCK(base) \
420 EVLOCK_LOCK((base)->lock, 0)
421 #define EVDNS_UNLOCK(base) \
422 EVLOCK_UNLOCK((base)->lock, 0)
423 #define ASSERT_LOCKED(base) \
424 EVLOCK_ASSERT_LOCKED((base)->lock)
428 default_evdns_log_fn(int warning
, const char *buf
)
430 if (warning
== EVDNS_LOG_WARN
)
431 event_warnx("[evdns] %s", buf
);
432 else if (warning
== EVDNS_LOG_MSG
)
433 event_msgx("[evdns] %s", buf
);
435 event_debug(("[evdns] %s", buf
));
438 static evdns_debug_log_fn_type evdns_log_fn
= NULL
;
441 evdns_set_log_fn(evdns_debug_log_fn_type fn
)
447 #define EVDNS_LOG_CHECK __attribute__ ((format(printf, 2, 3)))
449 #define EVDNS_LOG_CHECK
452 static void _evdns_log(int warn
, const char *fmt
, ...) EVDNS_LOG_CHECK
;
454 _evdns_log(int warn
, const char *fmt
, ...)
461 evutil_vsnprintf(buf
, sizeof(buf
), fmt
, args
);
464 if (warn
== EVDNS_LOG_MSG
)
465 warn
= EVDNS_LOG_WARN
;
466 evdns_log_fn(warn
, buf
);
468 default_evdns_log_fn(warn
, buf
);
473 #define log _evdns_log
475 /* This walks the list of inflight requests to find the */
476 /* one with a matching transaction id. Returns NULL on */
478 static struct request
*
479 request_find_from_trans_id(struct evdns_base
*base
, u16 trans_id
) {
480 struct request
*req
= REQ_HEAD(base
, trans_id
);
481 struct request
*const started_at
= req
;
487 if (req
->trans_id
== trans_id
) return req
;
489 } while (req
!= started_at
);
495 /* a libevent callback function which is called when a nameserver */
496 /* has gone down and we want to test if it has came back to life yet */
498 nameserver_prod_callback(evutil_socket_t fd
, short events
, void *arg
) {
499 struct nameserver
*const ns
= (struct nameserver
*) arg
;
503 EVDNS_LOCK(ns
->base
);
504 nameserver_send_probe(ns
);
505 EVDNS_UNLOCK(ns
->base
);
508 /* a libevent callback which is called when a nameserver probe (to see if */
509 /* it has come back to life) times out. We increment the count of failed_times */
510 /* and wait longer to send the next probe packet. */
512 nameserver_probe_failed(struct nameserver
*const ns
) {
513 struct timeval timeout
;
516 ASSERT_LOCKED(ns
->base
);
517 (void) evtimer_del(&ns
->timeout_event
);
518 if (ns
->state
== 1) {
519 /* This can happen if the nameserver acts in a way which makes us mark */
520 /* it as bad and then starts sending good replies. */
524 #define MAX_PROBE_TIMEOUT 3600
525 #define TIMEOUT_BACKOFF_FACTOR 3
527 memcpy(&timeout
, &ns
->base
->global_nameserver_probe_initial_timeout
,
528 sizeof(struct timeval
));
529 for (i
=ns
->failed_times
; i
> 0 && timeout
.tv_sec
< MAX_PROBE_TIMEOUT
; --i
) {
530 timeout
.tv_sec
*= TIMEOUT_BACKOFF_FACTOR
;
531 timeout
.tv_usec
*= TIMEOUT_BACKOFF_FACTOR
;
532 if (timeout
.tv_usec
> 1000000) {
533 timeout
.tv_sec
+= timeout
.tv_usec
/ 1000000;
534 timeout
.tv_usec
%= 1000000;
537 if (timeout
.tv_sec
> MAX_PROBE_TIMEOUT
) {
538 timeout
.tv_sec
= MAX_PROBE_TIMEOUT
;
544 if (evtimer_add(&ns
->timeout_event
, &timeout
) < 0) {
547 "Error from libevent when adding timer event for %s",
548 evutil_format_sockaddr_port(
549 (struct sockaddr
*)&ns
->address
,
550 addrbuf
, sizeof(addrbuf
)));
554 /* called when a nameserver has been deemed to have failed. For example, too */
555 /* many packets have timed out etc */
557 nameserver_failed(struct nameserver
*const ns
, const char *msg
) {
558 struct request
*req
, *started_at
;
559 struct evdns_base
*base
= ns
->base
;
564 /* if this nameserver has already been marked as failed */
565 /* then don't do anything */
566 if (!ns
->state
) return;
568 log(EVDNS_LOG_MSG
, "Nameserver %s has failed: %s",
569 evutil_format_sockaddr_port(
570 (struct sockaddr
*)&ns
->address
,
571 addrbuf
, sizeof(addrbuf
)),
574 base
->global_good_nameservers
--;
575 EVUTIL_ASSERT(base
->global_good_nameservers
>= 0);
576 if (base
->global_good_nameservers
== 0) {
577 log(EVDNS_LOG_MSG
, "All nameservers have failed");
581 ns
->failed_times
= 1;
583 if (evtimer_add(&ns
->timeout_event
,
584 &base
->global_nameserver_probe_initial_timeout
) < 0) {
586 "Error from libevent when adding timer event for %s",
587 evutil_format_sockaddr_port(
588 (struct sockaddr
*)&ns
->address
,
589 addrbuf
, sizeof(addrbuf
)));
593 /* walk the list of inflight requests to see if any can be reassigned to */
594 /* a different server. Requests in the waiting queue don't have a */
595 /* nameserver assigned yet */
597 /* if we don't have *any* good nameservers then there's no point */
598 /* trying to reassign requests to one */
599 if (!base
->global_good_nameservers
) return;
601 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
602 req
= started_at
= base
->req_heads
[i
];
605 if (req
->tx_count
== 0 && req
->ns
== ns
) {
606 /* still waiting to go out, can be moved */
607 /* to another server */
608 req
->ns
= nameserver_pick(base
);
611 } while (req
!= started_at
);
617 nameserver_up(struct nameserver
*const ns
)
620 ASSERT_LOCKED(ns
->base
);
621 if (ns
->state
) return;
622 log(EVDNS_LOG_MSG
, "Nameserver %s is back up",
623 evutil_format_sockaddr_port(
624 (struct sockaddr
*)&ns
->address
,
625 addrbuf
, sizeof(addrbuf
)));
626 evtimer_del(&ns
->timeout_event
);
627 if (ns
->probe_request
) {
628 evdns_cancel_request(ns
->base
, ns
->probe_request
);
629 ns
->probe_request
= NULL
;
632 ns
->failed_times
= 0;
634 ns
->base
->global_good_nameservers
++;
638 request_trans_id_set(struct request
*const req
, const u16 trans_id
) {
639 req
->trans_id
= trans_id
;
640 *((u16
*) req
->request
) = htons(trans_id
);
643 /* Called to remove a request from a list and dealloc it. */
644 /* head is a pointer to the head of the list it should be */
645 /* removed from or NULL if the request isn't in a list. */
646 /* when free_handle is one, free the handle as well. */
648 request_finished(struct request
*const req
, struct request
**head
, int free_handle
) {
649 struct evdns_base
*base
= req
->base
;
650 int was_inflight
= (head
!= &base
->req_waiting_head
);
652 ASSERT_VALID_REQUEST(req
);
655 evdns_request_remove(req
, head
);
657 log(EVDNS_LOG_DEBUG
, "Removing timeout for request %p", req
);
659 evtimer_del(&req
->timeout_event
);
660 base
->global_requests_inflight
--;
662 base
->global_requests_waiting
--;
664 /* it was initialized during request_new / evtimer_assign */
665 event_debug_unassign(&req
->timeout_event
);
667 if (!req
->request_appended
) {
668 /* need to free the request data on it's own */
669 mm_free(req
->request
);
671 /* the request data is appended onto the header */
672 /* so everything gets free()ed when we: */
676 EVUTIL_ASSERT(req
->handle
->current_req
== req
);
679 search_request_finished(req
->handle
);
680 req
->handle
->current_req
= NULL
;
681 if (! req
->handle
->pending_cb
) {
682 /* If we're planning to run the callback,
683 * don't free the handle until later. */
684 mm_free(req
->handle
);
686 req
->handle
= NULL
; /* If we have a bug, let's crash
689 req
->handle
->current_req
= NULL
;
695 evdns_requests_pump_waiting_queue(base
);
699 /* This is called when a server returns a funny error code. */
700 /* We try the request again with another server. */
704 /* 1 failed/reissue is pointless */
706 request_reissue(struct request
*req
) {
707 const struct nameserver
*const last_ns
= req
->ns
;
708 ASSERT_LOCKED(req
->base
);
709 ASSERT_VALID_REQUEST(req
);
710 /* the last nameserver should have been marked as failing */
711 /* by the caller of this function, therefore pick will try */
712 /* not to return it */
713 req
->ns
= nameserver_pick(req
->base
);
714 if (req
->ns
== last_ns
) {
715 /* ... but pick did return it */
716 /* not a lot of point in trying again with the */
721 req
->reissue_count
++;
723 req
->transmit_me
= 1;
728 /* this function looks for space on the inflight queue and promotes */
729 /* requests from the waiting queue if it can. */
731 evdns_requests_pump_waiting_queue(struct evdns_base
*base
) {
733 while (base
->global_requests_inflight
< base
->global_max_requests_inflight
&&
734 base
->global_requests_waiting
) {
736 /* move a request from the waiting queue to the inflight queue */
737 EVUTIL_ASSERT(base
->req_waiting_head
);
738 req
= base
->req_waiting_head
;
739 evdns_request_remove(req
, &base
->req_waiting_head
);
741 base
->global_requests_waiting
--;
742 base
->global_requests_inflight
++;
744 req
->ns
= nameserver_pick(base
);
745 request_trans_id_set(req
, transaction_id_pick(base
));
747 evdns_request_insert(req
, &REQ_HEAD(base
, req
->trans_id
));
748 evdns_request_transmit(req
);
749 evdns_transmit(base
);
753 /* TODO(nickm) document */
754 struct deferred_reply_callback
{
755 struct deferred_cb deferred
;
756 struct evdns_request
*handle
;
761 evdns_callback_type user_callback
;
766 reply_run_callback(struct deferred_cb
*d
, void *user_pointer
)
768 struct deferred_reply_callback
*cb
=
769 EVUTIL_UPCAST(d
, struct deferred_reply_callback
, deferred
);
771 switch (cb
->request_type
) {
774 cb
->user_callback(DNS_ERR_NONE
, DNS_IPv4_A
,
775 cb
->reply
.data
.a
.addrcount
, cb
->ttl
,
776 cb
->reply
.data
.a
.addresses
,
779 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
782 if (cb
->have_reply
) {
783 char *name
= cb
->reply
.data
.ptr
.name
;
784 cb
->user_callback(DNS_ERR_NONE
, DNS_PTR
, 1, cb
->ttl
,
785 &name
, user_pointer
);
787 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
792 cb
->user_callback(DNS_ERR_NONE
, DNS_IPv6_AAAA
,
793 cb
->reply
.data
.aaaa
.addrcount
, cb
->ttl
,
794 cb
->reply
.data
.aaaa
.addresses
,
797 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
803 if (cb
->handle
&& cb
->handle
->pending_cb
) {
811 reply_schedule_callback(struct request
*const req
, u32 ttl
, u32 err
, struct reply
*reply
)
813 struct deferred_reply_callback
*d
= mm_calloc(1, sizeof(*d
));
816 event_warn("%s: Couldn't allocate space for deferred callback.",
821 ASSERT_LOCKED(req
->base
);
823 d
->request_type
= req
->request_type
;
824 d
->user_callback
= req
->user_callback
;
829 memcpy(&d
->reply
, reply
, sizeof(struct reply
));
833 req
->handle
->pending_cb
= 1;
834 d
->handle
= req
->handle
;
837 event_deferred_cb_init(&d
->deferred
, reply_run_callback
,
839 event_deferred_cb_schedule(
840 event_base_get_deferred_cb_queue(req
->base
->event_base
),
844 /* this processes a parsed reply packet */
846 reply_handle(struct request
*const req
, u16 flags
, u32 ttl
, struct reply
*reply
) {
849 static const int error_codes
[] = {
850 DNS_ERR_FORMAT
, DNS_ERR_SERVERFAILED
, DNS_ERR_NOTEXIST
,
851 DNS_ERR_NOTIMPL
, DNS_ERR_REFUSED
854 ASSERT_LOCKED(req
->base
);
855 ASSERT_VALID_REQUEST(req
);
857 if (flags
& 0x020f || !reply
|| !reply
->have_answer
) {
858 /* there was an error */
859 if (flags
& 0x0200) {
860 error
= DNS_ERR_TRUNCATED
;
861 } else if (flags
& 0x000f) {
862 u16 error_code
= (flags
& 0x000f) - 1;
863 if (error_code
> 4) {
864 error
= DNS_ERR_UNKNOWN
;
866 error
= error_codes
[error_code
];
868 } else if (reply
&& !reply
->have_answer
) {
869 error
= DNS_ERR_NODATA
;
871 error
= DNS_ERR_UNKNOWN
;
875 case DNS_ERR_NOTIMPL
:
876 case DNS_ERR_REFUSED
:
877 /* we regard these errors as marking a bad nameserver */
878 if (req
->reissue_count
< req
->base
->global_max_reissues
) {
880 evutil_snprintf(msg
, sizeof(msg
), "Bad response %d (%s)",
881 error
, evdns_err_to_string(error
));
882 nameserver_failed(req
->ns
, msg
);
883 if (!request_reissue(req
)) return;
886 case DNS_ERR_SERVERFAILED
:
887 /* rcode 2 (servfailed) sometimes means "we
888 * are broken" and sometimes (with some binds)
889 * means "that request was very confusing."
890 * Treat this as a timeout, not a failure.
892 log(EVDNS_LOG_DEBUG
, "Got a SERVERFAILED from nameserver"
893 "at %s; will allow the request to time out.",
894 evutil_format_sockaddr_port(
895 (struct sockaddr
*)&req
->ns
->address
,
896 addrbuf
, sizeof(addrbuf
)));
899 /* we got a good reply from the nameserver: it is up. */
900 if (req
->handle
== req
->ns
->probe_request
) {
901 /* Avoid double-free */
902 req
->ns
->probe_request
= NULL
;
905 nameserver_up(req
->ns
);
908 if (req
->handle
->search_state
&&
909 req
->request_type
!= TYPE_PTR
) {
910 /* if we have a list of domains to search in,
911 * try the next one */
912 if (!search_try_next(req
->handle
)) {
913 /* a new request was issued so this
914 * request is finished and */
915 /* the user callback will be made when
916 * that request (or a */
917 /* child of it) finishes. */
922 /* all else failed. Pass the failure up */
923 reply_schedule_callback(req
, ttl
, error
, NULL
);
924 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 1);
926 /* all ok, tell the user */
927 reply_schedule_callback(req
, ttl
, 0, reply
);
928 if (req
->handle
== req
->ns
->probe_request
)
929 req
->ns
->probe_request
= NULL
; /* Avoid double-free */
930 nameserver_up(req
->ns
);
931 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 1);
936 name_parse(u8
*packet
, int length
, int *idx
, char *name_out
, int name_out_len
) {
940 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while (0)
941 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while (0)
942 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
945 const char *const end
= name_out
+ name_out_len
;
947 /* Normally, names are a series of length prefixed strings terminated */
948 /* with a length of 0 (the lengths are u8's < 63). */
949 /* However, the length can start with a pair of 1 bits and that */
950 /* means that the next 14 bits are a pointer within the current */
955 if (j
>= length
) return -1;
957 if (!label_len
) break;
958 if (label_len
& 0xc0) {
961 if (name_end
< 0) name_end
= j
;
962 j
= (((int)label_len
& 0x3f) << 8) + ptr_low
;
963 /* Make sure that the target offset is in-bounds. */
964 if (j
< 0 || j
>= length
) return -1;
965 /* If we've jumped more times than there are characters in the
966 * message, we must have a loop. */
967 if (++ptr_count
> length
) return -1;
970 if (label_len
> 63) return -1;
971 if (cp
!= name_out
) {
972 if (cp
+ 1 >= end
) return -1;
975 if (cp
+ label_len
>= end
) return -1;
976 memcpy(cp
, packet
+ j
, label_len
);
980 if (cp
>= end
) return -1;
991 /* parses a raw request from a nameserver */
993 reply_parse(struct evdns_base
*base
, u8
*packet
, int length
) {
994 int j
= 0, k
= 0; /* index into packet */
995 u16 _t
; /* used by the macros */
996 u32 _t32
; /* used by the macros */
997 char tmp_name
[256], cmp_name
[256]; /* used by the macros */
998 int name_matches
= 0;
1000 u16 trans_id
, questions
, answers
, authority
, additional
, datalength
;
1002 u32 ttl
, ttl_r
= 0xffffffff;
1004 struct request
*req
= NULL
;
1007 ASSERT_LOCKED(base
);
1015 (void) authority
; /* suppress "unused variable" warnings. */
1016 (void) additional
; /* suppress "unused variable" warnings. */
1018 req
= request_find_from_trans_id(base
, trans_id
);
1019 if (!req
) return -1;
1020 EVUTIL_ASSERT(req
->base
== base
);
1022 memset(&reply
, 0, sizeof(reply
));
1024 /* If it's not an answer, it doesn't correspond to any request. */
1025 if (!(flags
& 0x8000)) return -1; /* must be an answer */
1026 if ((flags
& 0x020f) && (flags
& 0x020f) != DNS_ERR_NOTEXIST
) {
1027 /* there was an error and it's not NXDOMAIN */
1030 /* if (!answers) return; */ /* must have an answer of some form */
1032 /* This macro skips a name in the DNS reply. */
1034 do { tmp_name[0] = '\0'; \
1035 if (name_parse(packet, length, &j, tmp_name, \
1036 sizeof(tmp_name))<0) \
1040 do { tmp_name[0] = '\0'; \
1041 cmp_name[0] = '\0'; \
1043 if (name_parse(packet, length, &j, tmp_name, \
1044 sizeof(tmp_name))<0) \
1046 if (name_parse(req->request, req->request_len, &k, \
1047 cmp_name, sizeof(cmp_name))<0) \
1049 if (base->global_randomize_case) { \
1050 if (strcmp(tmp_name, cmp_name) == 0) \
1053 if (evutil_ascii_strcasecmp(tmp_name, cmp_name) == 0) \
1058 reply
.type
= req
->request_type
;
1060 /* skip over each question in the reply */
1061 for (i
= 0; i
< questions
; ++i
) {
1062 /* the question looks like
1063 * <label:name><u16:type><u16:class>
1067 if (j
> length
) goto err
;
1073 /* now we have the answer section which looks like
1074 * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
1077 for (i
= 0; i
< answers
; ++i
) {
1086 if (type
== TYPE_A
&& class == CLASS_INET
) {
1087 int addrcount
, addrtocopy
;
1088 if (req
->request_type
!= TYPE_A
) {
1089 j
+= datalength
; continue;
1091 if ((datalength
& 3) != 0) /* not an even number of As. */
1093 addrcount
= datalength
>> 2;
1094 addrtocopy
= MIN(MAX_V4_ADDRS
- reply
.data
.a
.addrcount
, (unsigned)addrcount
);
1096 ttl_r
= MIN(ttl_r
, ttl
);
1097 /* we only bother with the first four addresses. */
1098 if (j
+ 4*addrtocopy
> length
) goto err
;
1099 memcpy(&reply
.data
.a
.addresses
[reply
.data
.a
.addrcount
],
1100 packet
+ j
, 4*addrtocopy
);
1102 reply
.data
.a
.addrcount
+= addrtocopy
;
1103 reply
.have_answer
= 1;
1104 if (reply
.data
.a
.addrcount
== MAX_V4_ADDRS
) break;
1105 } else if (type
== TYPE_PTR
&& class == CLASS_INET
) {
1106 if (req
->request_type
!= TYPE_PTR
) {
1107 j
+= datalength
; continue;
1109 if (name_parse(packet
, length
, &j
, reply
.data
.ptr
.name
,
1110 sizeof(reply
.data
.ptr
.name
))<0)
1112 ttl_r
= MIN(ttl_r
, ttl
);
1113 reply
.have_answer
= 1;
1115 } else if (type
== TYPE_CNAME
) {
1116 char cname
[HOST_NAME_MAX
];
1117 if (!req
->put_cname_in_ptr
|| *req
->put_cname_in_ptr
) {
1118 j
+= datalength
; continue;
1120 if (name_parse(packet
, length
, &j
, cname
,
1123 *req
->put_cname_in_ptr
= mm_strdup(cname
);
1124 } else if (type
== TYPE_AAAA
&& class == CLASS_INET
) {
1125 int addrcount
, addrtocopy
;
1126 if (req
->request_type
!= TYPE_AAAA
) {
1127 j
+= datalength
; continue;
1129 if ((datalength
& 15) != 0) /* not an even number of AAAAs. */
1131 addrcount
= datalength
>> 4; /* each address is 16 bytes long */
1132 addrtocopy
= MIN(MAX_V6_ADDRS
- reply
.data
.aaaa
.addrcount
, (unsigned)addrcount
);
1133 ttl_r
= MIN(ttl_r
, ttl
);
1135 /* we only bother with the first four addresses. */
1136 if (j
+ 16*addrtocopy
> length
) goto err
;
1137 memcpy(&reply
.data
.aaaa
.addresses
[reply
.data
.aaaa
.addrcount
],
1138 packet
+ j
, 16*addrtocopy
);
1139 reply
.data
.aaaa
.addrcount
+= addrtocopy
;
1141 reply
.have_answer
= 1;
1142 if (reply
.data
.aaaa
.addrcount
== MAX_V6_ADDRS
) break;
1144 /* skip over any other type of resource */
1149 if (!reply
.have_answer
) {
1150 for (i
= 0; i
< authority
; ++i
) {
1157 if (type
== TYPE_SOA
&& class == CLASS_INET
) {
1158 u32 serial
, refresh
, retry
, expire
, minimum
;
1170 ttl_r
= MIN(ttl_r
, ttl
);
1171 ttl_r
= MIN(ttl_r
, minimum
);
1173 /* skip over any other type of resource */
1179 if (ttl_r
== 0xffffffff)
1182 reply_handle(req
, flags
, ttl_r
, &reply
);
1186 reply_handle(req
, flags
, 0, NULL
);
1190 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
1191 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
1194 request_parse(u8
*packet
, int length
, struct evdns_server_port
*port
, struct sockaddr
*addr
, ev_socklen_t addrlen
)
1196 int j
= 0; /* index into packet */
1197 u16 _t
; /* used by the macros */
1198 char tmp_name
[256]; /* used by the macros */
1201 u16 trans_id
, flags
, questions
, answers
, authority
, additional
;
1202 struct server_request
*server_req
= NULL
;
1204 ASSERT_LOCKED(port
);
1206 /* Get the header fields */
1217 if (flags
& 0x8000) return -1; /* Must not be an answer. */
1218 flags
&= 0x0110; /* Only RD and CD get preserved. */
1220 server_req
= mm_malloc(sizeof(struct server_request
));
1221 if (server_req
== NULL
) return -1;
1222 memset(server_req
, 0, sizeof(struct server_request
));
1224 server_req
->trans_id
= trans_id
;
1225 memcpy(&server_req
->addr
, addr
, addrlen
);
1226 server_req
->addrlen
= addrlen
;
1228 server_req
->base
.flags
= flags
;
1229 server_req
->base
.nquestions
= 0;
1230 server_req
->base
.questions
= mm_calloc(sizeof(struct evdns_server_question
*), questions
);
1231 if (server_req
->base
.questions
== NULL
)
1234 for (i
= 0; i
< questions
; ++i
) {
1236 struct evdns_server_question
*q
;
1238 if (name_parse(packet
, length
, &j
, tmp_name
, sizeof(tmp_name
))<0)
1242 namelen
= (int)strlen(tmp_name
);
1243 q
= mm_malloc(sizeof(struct evdns_server_question
) + namelen
);
1247 q
->dns_question_class
= class;
1248 memcpy(q
->name
, tmp_name
, namelen
+1);
1249 server_req
->base
.questions
[server_req
->base
.nquestions
++] = q
;
1252 /* Ignore answers, authority, and additional. */
1254 server_req
->port
= port
;
1257 /* Only standard queries are supported. */
1258 if (flags
& 0x7800) {
1259 evdns_server_request_respond(&(server_req
->base
), DNS_ERR_NOTIMPL
);
1263 port
->user_callback(&(server_req
->base
), port
->user_data
);
1268 if (server_req
->base
.questions
) {
1269 for (i
= 0; i
< server_req
->base
.nquestions
; ++i
)
1270 mm_free(server_req
->base
.questions
[i
]);
1271 mm_free(server_req
->base
.questions
);
1273 mm_free(server_req
);
1285 evdns_set_transaction_id_fn(ev_uint16_t (*fn
)(void))
1290 evdns_set_random_bytes_fn(void (*fn
)(char *, size_t))
1294 /* Try to choose a strong transaction id which isn't already in flight */
1296 transaction_id_pick(struct evdns_base
*base
) {
1297 ASSERT_LOCKED(base
);
1300 evutil_secure_rng_get_bytes(&trans_id
, sizeof(trans_id
));
1302 if (trans_id
== 0xffff) continue;
1303 /* now check to see if that id is already inflight */
1304 if (request_find_from_trans_id(base
, trans_id
) == NULL
)
1309 /* choose a namesever to use. This function will try to ignore */
1310 /* nameservers which we think are down and load balance across the rest */
1311 /* by updating the server_head global each time. */
1312 static struct nameserver
*
1313 nameserver_pick(struct evdns_base
*base
) {
1314 struct nameserver
*started_at
= base
->server_head
, *picked
;
1315 ASSERT_LOCKED(base
);
1316 if (!base
->server_head
) return NULL
;
1318 /* if we don't have any good nameservers then there's no */
1319 /* point in trying to find one. */
1320 if (!base
->global_good_nameservers
) {
1321 base
->server_head
= base
->server_head
->next
;
1322 return base
->server_head
;
1325 /* remember that nameservers are in a circular list */
1327 if (base
->server_head
->state
) {
1328 /* we think this server is currently good */
1329 picked
= base
->server_head
;
1330 base
->server_head
= base
->server_head
->next
;
1334 base
->server_head
= base
->server_head
->next
;
1335 if (base
->server_head
== started_at
) {
1336 /* all the nameservers seem to be down */
1337 /* so we just return this one and hope for the */
1339 EVUTIL_ASSERT(base
->global_good_nameservers
== 0);
1340 picked
= base
->server_head
;
1341 base
->server_head
= base
->server_head
->next
;
1347 /* this is called when a namesever socket is ready for reading */
1349 nameserver_read(struct nameserver
*ns
) {
1350 struct sockaddr_storage ss
;
1351 ev_socklen_t addrlen
= sizeof(ss
);
1354 ASSERT_LOCKED(ns
->base
);
1357 const int r
= recvfrom(ns
->socket
, (void*)packet
,
1359 (struct sockaddr
*)&ss
, &addrlen
);
1361 int err
= evutil_socket_geterror(ns
->socket
);
1362 if (EVUTIL_ERR_RW_RETRIABLE(err
))
1364 nameserver_failed(ns
,
1365 evutil_socket_error_to_string(err
));
1368 if (evutil_sockaddr_cmp((struct sockaddr
*)&ss
,
1369 (struct sockaddr
*)&ns
->address
, 0)) {
1370 log(EVDNS_LOG_WARN
, "Address mismatch on received "
1371 "DNS packet. Apparent source was %s",
1372 evutil_format_sockaddr_port(
1373 (struct sockaddr
*)&ss
,
1374 addrbuf
, sizeof(addrbuf
)));
1379 reply_parse(ns
->base
, packet
, r
);
1383 /* Read a packet from a DNS client on a server port s, parse it, and */
1384 /* act accordingly. */
1386 server_port_read(struct evdns_server_port
*s
) {
1388 struct sockaddr_storage addr
;
1389 ev_socklen_t addrlen
;
1394 addrlen
= sizeof(struct sockaddr_storage
);
1395 r
= recvfrom(s
->socket
, (void*)packet
, sizeof(packet
), 0,
1396 (struct sockaddr
*) &addr
, &addrlen
);
1398 int err
= evutil_socket_geterror(s
->socket
);
1399 if (EVUTIL_ERR_RW_RETRIABLE(err
))
1402 "Error %s (%d) while reading request.",
1403 evutil_socket_error_to_string(err
), err
);
1406 request_parse(packet
, r
, s
, (struct sockaddr
*) &addr
, addrlen
);
1410 /* Try to write all pending replies on a given DNS server port. */
1412 server_port_flush(struct evdns_server_port
*port
)
1414 struct server_request
*req
= port
->pending_replies
;
1415 ASSERT_LOCKED(port
);
1417 int r
= sendto(port
->socket
, req
->response
, (int)req
->response_len
, 0,
1418 (struct sockaddr
*) &req
->addr
, (ev_socklen_t
)req
->addrlen
);
1420 int err
= evutil_socket_geterror(port
->socket
);
1421 if (EVUTIL_ERR_RW_RETRIABLE(err
))
1423 log(EVDNS_LOG_WARN
, "Error %s (%d) while writing response to port; dropping", evutil_socket_error_to_string(err
), err
);
1425 if (server_request_free(req
)) {
1426 /* we released the last reference to req->port. */
1429 EVUTIL_ASSERT(req
!= port
->pending_replies
);
1430 req
= port
->pending_replies
;
1434 /* We have no more pending requests; stop listening for 'writeable' events. */
1435 (void) event_del(&port
->event
);
1436 event_assign(&port
->event
, port
->event_base
,
1437 port
->socket
, EV_READ
| EV_PERSIST
,
1438 server_port_ready_callback
, port
);
1440 if (event_add(&port
->event
, NULL
) < 0) {
1441 log(EVDNS_LOG_WARN
, "Error from libevent when adding event for DNS server.");
1446 /* set if we are waiting for the ability to write to this server. */
1447 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1448 /* we stop these events. */
1450 nameserver_write_waiting(struct nameserver
*ns
, char waiting
) {
1451 ASSERT_LOCKED(ns
->base
);
1452 if (ns
->write_waiting
== waiting
) return;
1454 ns
->write_waiting
= waiting
;
1455 (void) event_del(&ns
->event
);
1456 event_assign(&ns
->event
, ns
->base
->event_base
,
1457 ns
->socket
, EV_READ
| (waiting
? EV_WRITE
: 0) | EV_PERSIST
,
1458 nameserver_ready_callback
, ns
);
1459 if (event_add(&ns
->event
, NULL
) < 0) {
1461 log(EVDNS_LOG_WARN
, "Error from libevent when adding event for %s",
1462 evutil_format_sockaddr_port(
1463 (struct sockaddr
*)&ns
->address
,
1464 addrbuf
, sizeof(addrbuf
)));
1469 /* a callback function. Called by libevent when the kernel says that */
1470 /* a nameserver socket is ready for writing or reading */
1472 nameserver_ready_callback(evutil_socket_t fd
, short events
, void *arg
) {
1473 struct nameserver
*ns
= (struct nameserver
*) arg
;
1476 EVDNS_LOCK(ns
->base
);
1477 if (events
& EV_WRITE
) {
1479 if (!evdns_transmit(ns
->base
)) {
1480 nameserver_write_waiting(ns
, 0);
1483 if (events
& EV_READ
) {
1484 nameserver_read(ns
);
1486 EVDNS_UNLOCK(ns
->base
);
1489 /* a callback function. Called by libevent when the kernel says that */
1490 /* a server socket is ready for writing or reading. */
1492 server_port_ready_callback(evutil_socket_t fd
, short events
, void *arg
) {
1493 struct evdns_server_port
*port
= (struct evdns_server_port
*) arg
;
1497 if (events
& EV_WRITE
) {
1499 server_port_flush(port
);
1501 if (events
& EV_READ
) {
1502 server_port_read(port
);
1507 /* This is an inefficient representation; only use it via the dnslabel_table_*
1508 * functions, so that is can be safely replaced with something smarter later. */
1509 #define MAX_LABELS 128
1510 /* Structures used to implement name compression */
1511 struct dnslabel_entry
{ char *v
; off_t pos
; };
1512 struct dnslabel_table
{
1513 int n_labels
; /* number of current entries */
1514 /* map from name to position in message */
1515 struct dnslabel_entry labels
[MAX_LABELS
];
1518 /* Initialize dnslabel_table. */
1520 dnslabel_table_init(struct dnslabel_table
*table
)
1522 table
->n_labels
= 0;
1525 /* Free all storage held by table, but not the table itself. */
1527 dnslabel_clear(struct dnslabel_table
*table
)
1530 for (i
= 0; i
< table
->n_labels
; ++i
)
1531 mm_free(table
->labels
[i
].v
);
1532 table
->n_labels
= 0;
1535 /* return the position of the label in the current message, or -1 if the label */
1536 /* hasn't been used yet. */
1538 dnslabel_table_get_pos(const struct dnslabel_table
*table
, const char *label
)
1541 for (i
= 0; i
< table
->n_labels
; ++i
) {
1542 if (!strcmp(label
, table
->labels
[i
].v
))
1543 return table
->labels
[i
].pos
;
1548 /* remember that we've used the label at position pos */
1550 dnslabel_table_add(struct dnslabel_table
*table
, const char *label
, off_t pos
)
1554 if (table
->n_labels
== MAX_LABELS
)
1556 v
= mm_strdup(label
);
1559 p
= table
->n_labels
++;
1560 table
->labels
[p
].v
= v
;
1561 table
->labels
[p
].pos
= pos
;
1566 /* Converts a string to a length-prefixed set of DNS labels, starting */
1567 /* at buf[j]. name and buf must not overlap. name_len should be the length */
1568 /* of name. table is optional, and is used for compression. */
1570 /* Input: abc.def */
1571 /* Output: <3>abc<3>def<0> */
1573 /* Returns the first index after the encoded name, or negative on error. */
1574 /* -1 label was > 63 bytes */
1575 /* -2 name too long to fit in buffer. */
1578 dnsname_to_labels(u8
*const buf
, size_t buf_len
, off_t j
,
1579 const char *name
, const size_t name_len
,
1580 struct dnslabel_table
*table
) {
1581 const char *end
= name
+ name_len
;
1585 #define APPEND16(x) do { \
1586 if (j + 2 > (off_t)buf_len) \
1589 memcpy(buf + j, &_t, 2); \
1592 #define APPEND32(x) do { \
1593 if (j + 4 > (off_t)buf_len) \
1596 memcpy(buf + j, &_t32, 4); \
1600 if (name_len
> 255) return -2;
1603 const char *const start
= name
;
1604 if (table
&& (ref
= dnslabel_table_get_pos(table
, name
)) >= 0) {
1605 APPEND16(ref
| 0xc000);
1608 name
= strchr(name
, '.');
1610 const size_t label_len
= end
- start
;
1611 if (label_len
> 63) return -1;
1612 if ((size_t)(j
+label_len
+1) > buf_len
) return -2;
1613 if (table
) dnslabel_table_add(table
, start
, j
);
1614 buf
[j
++] = (ev_uint8_t
)label_len
;
1616 memcpy(buf
+ j
, start
, label_len
);
1617 j
+= (int) label_len
;
1620 /* append length of the label. */
1621 const size_t label_len
= name
- start
;
1622 if (label_len
> 63) return -1;
1623 if ((size_t)(j
+label_len
+1) > buf_len
) return -2;
1624 if (table
) dnslabel_table_add(table
, start
, j
);
1625 buf
[j
++] = (ev_uint8_t
)label_len
;
1627 memcpy(buf
+ j
, start
, label_len
);
1628 j
+= (int) label_len
;
1629 /* hop over the '.' */
1634 /* the labels must be terminated by a 0. */
1635 /* It's possible that the name ended in a . */
1636 /* in which case the zero is already there */
1637 if (!j
|| buf
[j
-1]) buf
[j
++] = 0;
1643 /* Finds the length of a dns request for a DNS name of the given */
1644 /* length. The actual request may be smaller than the value returned */
1647 evdns_request_len(const size_t name_len
) {
1648 return 96 + /* length of the DNS standard header */
1650 4; /* space for the resource type */
1653 /* build a dns request packet into buf. buf should be at least as long */
1654 /* as evdns_request_len told you it should be. */
1656 /* Returns the amount of space used. Negative on error. */
1658 evdns_request_data_build(const char *const name
, const size_t name_len
,
1659 const u16 trans_id
, const u16 type
, const u16
class,
1660 u8
*const buf
, size_t buf_len
) {
1661 off_t j
= 0; /* current offset into buf */
1662 u16 _t
; /* used by the macros */
1665 APPEND16(0x0100); /* standard query, recusion needed */
1666 APPEND16(1); /* one question */
1667 APPEND16(0); /* no answers */
1668 APPEND16(0); /* no authority */
1669 APPEND16(0); /* no additional */
1671 j
= dnsname_to_labels(buf
, buf_len
, j
, name
, name_len
, NULL
);
1684 /* exported function */
1685 struct evdns_server_port
*
1686 evdns_add_server_port_with_base(struct event_base
*base
, evutil_socket_t socket
, int flags
, evdns_request_callback_fn_type cb
, void *user_data
)
1688 struct evdns_server_port
*port
;
1690 return NULL
; /* flags not yet implemented */
1691 if (!(port
= mm_malloc(sizeof(struct evdns_server_port
))))
1693 memset(port
, 0, sizeof(struct evdns_server_port
));
1696 port
->socket
= socket
;
1700 port
->user_callback
= cb
;
1701 port
->user_data
= user_data
;
1702 port
->pending_replies
= NULL
;
1703 port
->event_base
= base
;
1705 event_assign(&port
->event
, port
->event_base
,
1706 port
->socket
, EV_READ
| EV_PERSIST
,
1707 server_port_ready_callback
, port
);
1708 if (event_add(&port
->event
, NULL
) < 0) {
1712 EVTHREAD_ALLOC_LOCK(port
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
1716 struct evdns_server_port
*
1717 evdns_add_server_port(evutil_socket_t socket
, int flags
, evdns_request_callback_fn_type cb
, void *user_data
)
1719 return evdns_add_server_port_with_base(NULL
, socket
, flags
, cb
, user_data
);
1722 /* exported function */
1724 evdns_close_server_port(struct evdns_server_port
*port
)
1727 if (--port
->refcnt
== 0) {
1729 server_port_free(port
);
1735 /* exported function */
1737 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
)
1739 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
1740 struct server_reply_item
**itemp
, *item
;
1744 EVDNS_LOCK(req
->port
);
1745 if (req
->response
) /* have we already answered? */
1749 case EVDNS_ANSWER_SECTION
:
1750 itemp
= &req
->answer
;
1751 countp
= &req
->n_answer
;
1753 case EVDNS_AUTHORITY_SECTION
:
1754 itemp
= &req
->authority
;
1755 countp
= &req
->n_authority
;
1757 case EVDNS_ADDITIONAL_SECTION
:
1758 itemp
= &req
->additional
;
1759 countp
= &req
->n_additional
;
1765 itemp
= &((*itemp
)->next
);
1767 item
= mm_malloc(sizeof(struct server_reply_item
));
1771 if (!(item
->name
= mm_strdup(name
))) {
1776 item
->dns_question_class
= class;
1778 item
->is_name
= is_name
!= 0;
1782 if (item
->is_name
) {
1783 if (!(item
->data
= mm_strdup(data
))) {
1784 mm_free(item
->name
);
1788 item
->datalen
= (u16
)-1;
1790 if (!(item
->data
= mm_malloc(datalen
))) {
1791 mm_free(item
->name
);
1795 item
->datalen
= datalen
;
1796 memcpy(item
->data
, data
, datalen
);
1804 EVDNS_UNLOCK(req
->port
);
1808 /* exported function */
1810 evdns_server_request_add_a_reply(struct evdns_server_request
*req
, const char *name
, int n
, const void *addrs
, int ttl
)
1812 return evdns_server_request_add_reply(
1813 req
, EVDNS_ANSWER_SECTION
, name
, TYPE_A
, CLASS_INET
,
1814 ttl
, n
*4, 0, addrs
);
1817 /* exported function */
1819 evdns_server_request_add_aaaa_reply(struct evdns_server_request
*req
, const char *name
, int n
, const void *addrs
, int ttl
)
1821 return evdns_server_request_add_reply(
1822 req
, EVDNS_ANSWER_SECTION
, name
, TYPE_AAAA
, CLASS_INET
,
1823 ttl
, n
*16, 0, addrs
);
1826 /* exported function */
1828 evdns_server_request_add_ptr_reply(struct evdns_server_request
*req
, struct in_addr
*in
, const char *inaddr_name
, const char *hostname
, int ttl
)
1832 if (in
&& inaddr_name
)
1834 else if (!in
&& !inaddr_name
)
1837 a
= ntohl(in
->s_addr
);
1838 evutil_snprintf(buf
, sizeof(buf
), "%d.%d.%d.%d.in-addr.arpa",
1839 (int)(u8
)((a
)&0xff),
1840 (int)(u8
)((a
>>8 )&0xff),
1841 (int)(u8
)((a
>>16)&0xff),
1842 (int)(u8
)((a
>>24)&0xff));
1845 return evdns_server_request_add_reply(
1846 req
, EVDNS_ANSWER_SECTION
, inaddr_name
, TYPE_PTR
, CLASS_INET
,
1847 ttl
, -1, 1, hostname
);
1850 /* exported function */
1852 evdns_server_request_add_cname_reply(struct evdns_server_request
*req
, const char *name
, const char *cname
, int ttl
)
1854 return evdns_server_request_add_reply(
1855 req
, EVDNS_ANSWER_SECTION
, name
, TYPE_CNAME
, CLASS_INET
,
1859 /* exported function */
1861 evdns_server_request_set_flags(struct evdns_server_request
*exreq
, int flags
)
1863 struct server_request
*req
= TO_SERVER_REQUEST(exreq
);
1864 req
->base
.flags
&= ~(EVDNS_FLAGS_AA
|EVDNS_FLAGS_RD
);
1865 req
->base
.flags
|= flags
;
1869 evdns_server_request_format_response(struct server_request
*req
, int err
)
1871 unsigned char buf
[1500];
1872 size_t buf_len
= sizeof(buf
);
1878 struct dnslabel_table table
;
1880 if (err
< 0 || err
> 15) return -1;
1882 /* Set response bit and error code; copy OPCODE and RD fields from
1883 * question; copy RA and AA if set by caller. */
1884 flags
= req
->base
.flags
;
1885 flags
|= (0x8000 | err
);
1887 dnslabel_table_init(&table
);
1888 APPEND16(req
->trans_id
);
1890 APPEND16(req
->base
.nquestions
);
1891 APPEND16(req
->n_answer
);
1892 APPEND16(req
->n_authority
);
1893 APPEND16(req
->n_additional
);
1895 /* Add questions. */
1896 for (i
=0; i
< req
->base
.nquestions
; ++i
) {
1897 const char *s
= req
->base
.questions
[i
]->name
;
1898 j
= dnsname_to_labels(buf
, buf_len
, j
, s
, strlen(s
), &table
);
1900 dnslabel_clear(&table
);
1903 APPEND16(req
->base
.questions
[i
]->type
);
1904 APPEND16(req
->base
.questions
[i
]->dns_question_class
);
1907 /* Add answer, authority, and additional sections. */
1908 for (i
=0; i
<3; ++i
) {
1909 struct server_reply_item
*item
;
1913 item
= req
->authority
;
1915 item
= req
->additional
;
1917 r
= dnsname_to_labels(buf
, buf_len
, j
, item
->name
, strlen(item
->name
), &table
);
1922 APPEND16(item
->type
);
1923 APPEND16(item
->dns_question_class
);
1924 APPEND32(item
->ttl
);
1925 if (item
->is_name
) {
1926 off_t len_idx
= j
, name_start
;
1929 r
= dnsname_to_labels(buf
, buf_len
, j
, item
->data
, strlen(item
->data
), &table
);
1933 _t
= htons( (short) (j
-name_start
) );
1934 memcpy(buf
+len_idx
, &_t
, 2);
1936 APPEND16(item
->datalen
);
1937 if (j
+item
->datalen
> (off_t
)buf_len
)
1939 memcpy(buf
+j
, item
->data
, item
->datalen
);
1949 buf
[2] |= 0x02; /* set the truncated bit. */
1952 req
->response_len
= j
;
1954 if (!(req
->response
= mm_malloc(req
->response_len
))) {
1955 server_request_free_answers(req
);
1956 dnslabel_clear(&table
);
1959 memcpy(req
->response
, buf
, req
->response_len
);
1960 server_request_free_answers(req
);
1961 dnslabel_clear(&table
);
1965 /* exported function */
1967 evdns_server_request_respond(struct evdns_server_request
*_req
, int err
)
1969 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
1970 struct evdns_server_port
*port
= req
->port
;
1974 if (!req
->response
) {
1975 if ((r
= evdns_server_request_format_response(req
, err
))<0)
1979 r
= sendto(port
->socket
, req
->response
, (int)req
->response_len
, 0,
1980 (struct sockaddr
*) &req
->addr
, (ev_socklen_t
)req
->addrlen
);
1982 int sock_err
= evutil_socket_geterror(port
->socket
);
1983 if (EVUTIL_ERR_RW_RETRIABLE(sock_err
))
1986 if (port
->pending_replies
) {
1987 req
->prev_pending
= port
->pending_replies
->prev_pending
;
1988 req
->next_pending
= port
->pending_replies
;
1989 req
->prev_pending
->next_pending
=
1990 req
->next_pending
->prev_pending
= req
;
1992 req
->prev_pending
= req
->next_pending
= req
;
1993 port
->pending_replies
= req
;
1996 (void) event_del(&port
->event
);
1997 event_assign(&port
->event
, port
->event_base
, port
->socket
, (port
->closing
?0:EV_READ
) | EV_WRITE
| EV_PERSIST
, server_port_ready_callback
, port
);
1999 if (event_add(&port
->event
, NULL
) < 0) {
2000 log(EVDNS_LOG_WARN
, "Error from libevent when adding event for DNS server");
2008 if (server_request_free(req
)) {
2013 if (port
->pending_replies
)
2014 server_port_flush(port
);
2022 /* Free all storage held by RRs in req. */
2024 server_request_free_answers(struct server_request
*req
)
2026 struct server_reply_item
*victim
, *next
, **list
;
2028 for (i
= 0; i
< 3; ++i
) {
2030 list
= &req
->answer
;
2032 list
= &req
->authority
;
2034 list
= &req
->additional
;
2038 next
= victim
->next
;
2039 mm_free(victim
->name
);
2041 mm_free(victim
->data
);
2049 /* Free all storage held by req, and remove links to it. */
2050 /* return true iff we just wound up freeing the server_port. */
2052 server_request_free(struct server_request
*req
)
2054 int i
, rc
=1, lock
=0;
2055 if (req
->base
.questions
) {
2056 for (i
= 0; i
< req
->base
.nquestions
; ++i
)
2057 mm_free(req
->base
.questions
[i
]);
2058 mm_free(req
->base
.questions
);
2062 EVDNS_LOCK(req
->port
);
2064 if (req
->port
->pending_replies
== req
) {
2065 if (req
->next_pending
&& req
->next_pending
!= req
)
2066 req
->port
->pending_replies
= req
->next_pending
;
2068 req
->port
->pending_replies
= NULL
;
2070 rc
= --req
->port
->refcnt
;
2073 if (req
->response
) {
2074 mm_free(req
->response
);
2077 server_request_free_answers(req
);
2079 if (req
->next_pending
&& req
->next_pending
!= req
) {
2080 req
->next_pending
->prev_pending
= req
->prev_pending
;
2081 req
->prev_pending
->next_pending
= req
->next_pending
;
2085 EVDNS_UNLOCK(req
->port
); /* ????? nickm */
2086 server_port_free(req
->port
);
2091 EVDNS_UNLOCK(req
->port
);
2096 /* Free all storage held by an evdns_server_port. Only called when */
2098 server_port_free(struct evdns_server_port
*port
)
2100 EVUTIL_ASSERT(port
);
2101 EVUTIL_ASSERT(!port
->refcnt
);
2102 EVUTIL_ASSERT(!port
->pending_replies
);
2103 if (port
->socket
> 0) {
2104 evutil_closesocket(port
->socket
);
2107 (void) event_del(&port
->event
);
2108 event_debug_unassign(&port
->event
);
2109 EVTHREAD_FREE_LOCK(port
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
2113 /* exported function */
2115 evdns_server_request_drop(struct evdns_server_request
*_req
)
2117 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
2118 server_request_free(req
);
2122 /* exported function */
2124 evdns_server_request_get_requesting_addr(struct evdns_server_request
*_req
, struct sockaddr
*sa
, int addr_len
)
2126 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
2127 if (addr_len
< (int)req
->addrlen
)
2129 memcpy(sa
, &(req
->addr
), req
->addrlen
);
2130 return req
->addrlen
;
2136 /* this is a libevent callback function which is called when a request */
2137 /* has timed out. */
2139 evdns_request_timeout_callback(evutil_socket_t fd
, short events
, void *arg
) {
2140 struct request
*const req
= (struct request
*) arg
;
2141 struct evdns_base
*base
= req
->base
;
2146 log(EVDNS_LOG_DEBUG
, "Request %p timed out", arg
);
2149 req
->ns
->timedout
++;
2150 if (req
->ns
->timedout
> req
->base
->global_max_nameserver_timeout
) {
2151 req
->ns
->timedout
= 0;
2152 nameserver_failed(req
->ns
, "request timed out.");
2155 if (req
->tx_count
>= req
->base
->global_max_retransmits
) {
2156 /* this request has failed */
2157 log(EVDNS_LOG_DEBUG
, "Giving up on request %p; tx_count==%d",
2158 arg
, req
->tx_count
);
2159 reply_schedule_callback(req
, 0, DNS_ERR_TIMEOUT
, NULL
);
2160 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 1);
2163 struct nameserver
*new_ns
;
2164 log(EVDNS_LOG_DEBUG
, "Retransmitting request %p; tx_count==%d",
2165 arg
, req
->tx_count
);
2166 (void) evtimer_del(&req
->timeout_event
);
2167 new_ns
= nameserver_pick(base
);
2170 evdns_request_transmit(req
);
2175 /* try to send a request to a given server. */
2179 /* 1 temporary failure */
2180 /* 2 other failure */
2182 evdns_request_transmit_to(struct request
*req
, struct nameserver
*server
) {
2184 ASSERT_LOCKED(req
->base
);
2185 ASSERT_VALID_REQUEST(req
);
2186 r
= sendto(server
->socket
, (void*)req
->request
, req
->request_len
, 0,
2187 (struct sockaddr
*)&server
->address
, server
->addrlen
);
2189 int err
= evutil_socket_geterror(server
->socket
);
2190 if (EVUTIL_ERR_RW_RETRIABLE(err
))
2192 nameserver_failed(req
->ns
, evutil_socket_error_to_string(err
));
2194 } else if (r
!= (int)req
->request_len
) {
2195 return 1; /* short write */
2201 /* try to send a request, updating the fields of the request */
2208 evdns_request_transmit(struct request
*req
) {
2211 ASSERT_LOCKED(req
->base
);
2212 ASSERT_VALID_REQUEST(req
);
2213 /* if we fail to send this packet then this flag marks it */
2214 /* for evdns_transmit */
2215 req
->transmit_me
= 1;
2216 EVUTIL_ASSERT(req
->trans_id
!= 0xffff);
2218 if (req
->ns
->choked
) {
2219 /* don't bother trying to write to a socket */
2220 /* which we have had EAGAIN from */
2224 r
= evdns_request_transmit_to(req
, req
->ns
);
2228 req
->ns
->choked
= 1;
2229 nameserver_write_waiting(req
->ns
, 1);
2232 /* failed to transmit the request entirely. */
2234 /* fall through: we'll set a timeout, which will time out,
2235 * and make us retransmit the request anyway. */
2238 log(EVDNS_LOG_DEBUG
,
2239 "Setting timeout for request %p, sent to nameserver %p", req
, req
->ns
);
2240 if (evtimer_add(&req
->timeout_event
, &req
->base
->global_timeout
) < 0) {
2242 "Error from libevent when adding timer for request %p",
2247 req
->transmit_me
= 0;
2253 nameserver_probe_callback(int result
, char type
, int count
, int ttl
, void *addresses
, void *arg
) {
2254 struct nameserver
*const ns
= (struct nameserver
*) arg
;
2260 if (result
== DNS_ERR_CANCEL
) {
2261 /* We canceled this request because the nameserver came up
2262 * for some other reason. Do not change our opinion about
2263 * the nameserver. */
2267 EVDNS_LOCK(ns
->base
);
2268 ns
->probe_request
= NULL
;
2269 if (result
== DNS_ERR_NONE
|| result
== DNS_ERR_NOTEXIST
) {
2270 /* this is a good reply */
2273 nameserver_probe_failed(ns
);
2275 EVDNS_UNLOCK(ns
->base
);
2279 nameserver_send_probe(struct nameserver
*const ns
) {
2280 struct evdns_request
*handle
;
2281 struct request
*req
;
2283 /* here we need to send a probe to a given nameserver */
2284 /* in the hope that it is up now. */
2286 ASSERT_LOCKED(ns
->base
);
2287 log(EVDNS_LOG_DEBUG
, "Sending probe to %s",
2288 evutil_format_sockaddr_port(
2289 (struct sockaddr
*)&ns
->address
,
2290 addrbuf
, sizeof(addrbuf
)));
2291 handle
= mm_calloc(1, sizeof(*handle
));
2292 if (!handle
) return;
2293 req
= request_new(ns
->base
, handle
, TYPE_A
, "google.com", DNS_QUERY_NO_SEARCH
, nameserver_probe_callback
, ns
);
2298 ns
->probe_request
= handle
;
2299 /* we force this into the inflight queue no matter what */
2300 request_trans_id_set(req
, transaction_id_pick(ns
->base
));
2302 request_submit(req
);
2306 /* 0 didn't try to transmit anything */
2307 /* 1 tried to transmit something */
2309 evdns_transmit(struct evdns_base
*base
) {
2310 char did_try_to_transmit
= 0;
2313 ASSERT_LOCKED(base
);
2314 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
2315 if (base
->req_heads
[i
]) {
2316 struct request
*const started_at
= base
->req_heads
[i
], *req
= started_at
;
2317 /* first transmit all the requests which are currently waiting */
2319 if (req
->transmit_me
) {
2320 did_try_to_transmit
= 1;
2321 evdns_request_transmit(req
);
2325 } while (req
!= started_at
);
2329 return did_try_to_transmit
;
2332 /* exported function */
2334 evdns_base_count_nameservers(struct evdns_base
*base
)
2336 const struct nameserver
*server
;
2340 server
= base
->server_head
;
2345 server
= server
->next
;
2346 } while (server
!= base
->server_head
);
2353 evdns_count_nameservers(void)
2355 return evdns_base_count_nameservers(current_base
);
2358 /* exported function */
2360 evdns_base_clear_nameservers_and_suspend(struct evdns_base
*base
)
2362 struct nameserver
*server
, *started_at
;
2366 server
= base
->server_head
;
2367 started_at
= base
->server_head
;
2373 struct nameserver
*next
= server
->next
;
2374 (void) event_del(&server
->event
);
2375 if (evtimer_initialized(&server
->timeout_event
))
2376 (void) evtimer_del(&server
->timeout_event
);
2377 if (server
->probe_request
) {
2378 evdns_cancel_request(server
->base
, server
->probe_request
);
2379 server
->probe_request
= NULL
;
2381 if (server
->socket
>= 0)
2382 evutil_closesocket(server
->socket
);
2384 if (next
== started_at
)
2388 base
->server_head
= NULL
;
2389 base
->global_good_nameservers
= 0;
2391 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
2392 struct request
*req
, *req_started_at
;
2393 req
= req_started_at
= base
->req_heads
[i
];
2395 struct request
*next
= req
->next
;
2396 req
->tx_count
= req
->reissue_count
= 0;
2398 /* ???? What to do about searches? */
2399 (void) evtimer_del(&req
->timeout_event
);
2401 req
->transmit_me
= 0;
2403 base
->global_requests_waiting
++;
2404 evdns_request_insert(req
, &base
->req_waiting_head
);
2405 /* We want to insert these suspended elements at the front of
2406 * the waiting queue, since they were pending before any of
2407 * the waiting entries were added. This is a circular list,
2408 * so we can just shift the start back by one.*/
2409 base
->req_waiting_head
= base
->req_waiting_head
->prev
;
2411 if (next
== req_started_at
)
2415 base
->req_heads
[i
] = NULL
;
2418 base
->global_requests_inflight
= 0;
2425 evdns_clear_nameservers_and_suspend(void)
2427 return evdns_base_clear_nameservers_and_suspend(current_base
);
2431 /* exported function */
2433 evdns_base_resume(struct evdns_base
*base
)
2436 evdns_requests_pump_waiting_queue(base
);
2444 return evdns_base_resume(current_base
);
2448 _evdns_nameserver_add_impl(struct evdns_base
*base
, const struct sockaddr
*address
, int addrlen
) {
2449 /* first check to see if we already have this nameserver */
2451 const struct nameserver
*server
= base
->server_head
, *const started_at
= base
->server_head
;
2452 struct nameserver
*ns
;
2456 ASSERT_LOCKED(base
);
2459 if (!evutil_sockaddr_cmp((struct sockaddr
*)&server
->address
, address
, 1)) return 3;
2460 server
= server
->next
;
2461 } while (server
!= started_at
);
2463 if (addrlen
> (int)sizeof(ns
->address
)) {
2464 log(EVDNS_LOG_DEBUG
, "Addrlen %d too long.", (int)addrlen
);
2468 ns
= (struct nameserver
*) mm_malloc(sizeof(struct nameserver
));
2471 memset(ns
, 0, sizeof(struct nameserver
));
2474 evtimer_assign(&ns
->timeout_event
, ns
->base
->event_base
, nameserver_prod_callback
, ns
);
2476 ns
->socket
= socket(address
->sa_family
, SOCK_DGRAM
, 0);
2477 if (ns
->socket
< 0) { err
= 1; goto out1
; }
2478 evutil_make_socket_closeonexec(ns
->socket
);
2479 evutil_make_socket_nonblocking(ns
->socket
);
2481 if (base
->global_outgoing_addrlen
&&
2482 !evutil_sockaddr_is_loopback(address
)) {
2483 if (bind(ns
->socket
,
2484 (struct sockaddr
*)&base
->global_outgoing_address
,
2485 base
->global_outgoing_addrlen
) < 0) {
2486 log(EVDNS_LOG_WARN
,"Couldn't bind to outgoing address");
2492 memcpy(&ns
->address
, address
, addrlen
);
2493 ns
->addrlen
= addrlen
;
2495 event_assign(&ns
->event
, ns
->base
->event_base
, ns
->socket
, EV_READ
| EV_PERSIST
, nameserver_ready_callback
, ns
);
2496 if (event_add(&ns
->event
, NULL
) < 0) {
2501 log(EVDNS_LOG_DEBUG
, "Added nameserver %s as %p",
2502 evutil_format_sockaddr_port(address
, addrbuf
, sizeof(addrbuf
)), ns
);
2504 /* insert this nameserver into the list of them */
2505 if (!base
->server_head
) {
2506 ns
->next
= ns
->prev
= ns
;
2507 base
->server_head
= ns
;
2509 ns
->next
= base
->server_head
->next
;
2510 ns
->prev
= base
->server_head
;
2511 base
->server_head
->next
= ns
;
2512 ns
->next
->prev
= ns
;
2515 base
->global_good_nameservers
++;
2520 evutil_closesocket(ns
->socket
);
2522 event_debug_unassign(&ns
->event
);
2524 log(EVDNS_LOG_WARN
, "Unable to add nameserver %s: error %d",
2525 evutil_format_sockaddr_port(address
, addrbuf
, sizeof(addrbuf
)), err
);
2529 /* exported function */
2531 evdns_base_nameserver_add(struct evdns_base
*base
, unsigned long int address
)
2533 struct sockaddr_in sin
;
2535 memset(&sin
, 0, sizeof(sin
));
2536 sin
.sin_addr
.s_addr
= address
;
2537 sin
.sin_port
= htons(53);
2538 sin
.sin_family
= AF_INET
;
2540 res
= _evdns_nameserver_add_impl(base
, (struct sockaddr
*)&sin
, sizeof(sin
));
2546 evdns_nameserver_add(unsigned long int address
) {
2548 current_base
= evdns_base_new(NULL
, 0);
2549 return evdns_base_nameserver_add(current_base
, address
);
2553 sockaddr_setport(struct sockaddr
*sa
, ev_uint16_t port
)
2555 if (sa
->sa_family
== AF_INET
) {
2556 ((struct sockaddr_in
*)sa
)->sin_port
= htons(port
);
2557 } else if (sa
->sa_family
== AF_INET6
) {
2558 ((struct sockaddr_in6
*)sa
)->sin6_port
= htons(port
);
2563 sockaddr_getport(struct sockaddr
*sa
)
2565 if (sa
->sa_family
== AF_INET
) {
2566 return ntohs(((struct sockaddr_in
*)sa
)->sin_port
);
2567 } else if (sa
->sa_family
== AF_INET6
) {
2568 return ntohs(((struct sockaddr_in6
*)sa
)->sin6_port
);
2574 /* exported function */
2576 evdns_base_nameserver_ip_add(struct evdns_base
*base
, const char *ip_as_string
) {
2577 struct sockaddr_storage ss
;
2578 struct sockaddr
*sa
;
2579 int len
= sizeof(ss
);
2581 if (evutil_parse_sockaddr_port(ip_as_string
, (struct sockaddr
*)&ss
,
2583 log(EVDNS_LOG_WARN
, "Unable to parse nameserver address %s",
2587 sa
= (struct sockaddr
*) &ss
;
2588 if (sockaddr_getport(sa
) == 0)
2589 sockaddr_setport(sa
, 53);
2592 res
= _evdns_nameserver_add_impl(base
, sa
, len
);
2598 evdns_nameserver_ip_add(const char *ip_as_string
) {
2600 current_base
= evdns_base_new(NULL
, 0);
2601 return evdns_base_nameserver_ip_add(current_base
, ip_as_string
);
2605 evdns_base_nameserver_sockaddr_add(struct evdns_base
*base
,
2606 const struct sockaddr
*sa
, ev_socklen_t len
, unsigned flags
)
2609 EVUTIL_ASSERT(base
);
2611 res
= _evdns_nameserver_add_impl(base
, sa
, len
);
2616 /* remove from the queue */
2618 evdns_request_remove(struct request
*req
, struct request
**head
)
2620 ASSERT_LOCKED(req
->base
);
2621 ASSERT_VALID_REQUEST(req
);
2625 struct request
*ptr
;
2627 EVUTIL_ASSERT(*head
!= NULL
);
2636 } while (ptr
!= *head
);
2637 EVUTIL_ASSERT(found
);
2639 EVUTIL_ASSERT(req
->next
);
2643 if (req
->next
== req
) {
2644 /* only item in the list */
2647 req
->next
->prev
= req
->prev
;
2648 req
->prev
->next
= req
->next
;
2649 if (*head
== req
) *head
= req
->next
;
2651 req
->next
= req
->prev
= NULL
;
2654 /* insert into the tail of the queue */
2656 evdns_request_insert(struct request
*req
, struct request
**head
) {
2657 ASSERT_LOCKED(req
->base
);
2658 ASSERT_VALID_REQUEST(req
);
2661 req
->next
= req
->prev
= req
;
2665 req
->prev
= (*head
)->prev
;
2666 req
->prev
->next
= req
;
2668 (*head
)->prev
= req
;
2672 string_num_dots(const char *s
) {
2674 while ((s
= strchr(s
, '.'))) {
2681 static struct request
*
2682 request_new(struct evdns_base
*base
, struct evdns_request
*handle
, int type
,
2683 const char *name
, int flags
, evdns_callback_type callback
,
2686 const char issuing_now
=
2687 (base
->global_requests_inflight
< base
->global_max_requests_inflight
) ? 1 : 0;
2689 const size_t name_len
= strlen(name
);
2690 const size_t request_max_len
= evdns_request_len(name_len
);
2691 const u16 trans_id
= issuing_now
? transaction_id_pick(base
) : 0xffff;
2692 /* the request data is alloced in a single block with the header */
2693 struct request
*const req
=
2694 mm_malloc(sizeof(struct request
) + request_max_len
);
2699 ASSERT_LOCKED(base
);
2701 if (!req
) return NULL
;
2703 if (name_len
>= sizeof(namebuf
)) {
2708 memset(req
, 0, sizeof(struct request
));
2711 evtimer_assign(&req
->timeout_event
, req
->base
->event_base
, evdns_request_timeout_callback
, req
);
2713 if (base
->global_randomize_case
) {
2715 char randbits
[(sizeof(namebuf
)+7)/8];
2716 strlcpy(namebuf
, name
, sizeof(namebuf
));
2717 evutil_secure_rng_get_bytes(randbits
, (name_len
+7)/8);
2718 for (i
= 0; i
< name_len
; ++i
) {
2719 if (EVUTIL_ISALPHA(namebuf
[i
])) {
2720 if ((randbits
[i
>> 3] & (1<<(i
& 7))))
2723 namebuf
[i
] &= ~0x20;
2729 /* request data lives just after the header */
2730 req
->request
= ((u8
*) req
) + sizeof(struct request
);
2731 /* denotes that the request data shouldn't be free()ed */
2732 req
->request_appended
= 1;
2733 rlen
= evdns_request_data_build(name
, name_len
, trans_id
,
2734 type
, CLASS_INET
, req
->request
, request_max_len
);
2738 req
->request_len
= rlen
;
2739 req
->trans_id
= trans_id
;
2741 req
->request_type
= type
;
2742 req
->user_pointer
= user_ptr
;
2743 req
->user_callback
= callback
;
2744 req
->ns
= issuing_now
? nameserver_pick(base
) : NULL
;
2745 req
->next
= req
->prev
= NULL
;
2746 req
->handle
= handle
;
2748 handle
->current_req
= req
;
2749 handle
->base
= base
;
2759 request_submit(struct request
*const req
) {
2760 struct evdns_base
*base
= req
->base
;
2761 ASSERT_LOCKED(base
);
2762 ASSERT_VALID_REQUEST(req
);
2764 /* if it has a nameserver assigned then this is going */
2765 /* straight into the inflight queue */
2766 evdns_request_insert(req
, &REQ_HEAD(base
, req
->trans_id
));
2767 base
->global_requests_inflight
++;
2768 evdns_request_transmit(req
);
2770 evdns_request_insert(req
, &base
->req_waiting_head
);
2771 base
->global_requests_waiting
++;
2775 /* exported function */
2777 evdns_cancel_request(struct evdns_base
*base
, struct evdns_request
*handle
)
2779 struct request
*req
;
2781 if (!handle
->current_req
)
2785 /* This redundancy is silly; can we fix it? (Not for 2.0) XXXX */
2786 base
= handle
->base
;
2788 base
= handle
->current_req
->base
;
2792 if (handle
->pending_cb
) {
2797 req
= handle
->current_req
;
2798 ASSERT_VALID_REQUEST(req
);
2800 reply_schedule_callback(req
, 0, DNS_ERR_CANCEL
, NULL
);
2802 /* remove from inflight queue */
2803 request_finished(req
, &REQ_HEAD(base
, req
->trans_id
), 1);
2805 /* remove from global_waiting head */
2806 request_finished(req
, &base
->req_waiting_head
, 1);
2811 /* exported function */
2812 struct evdns_request
*
2813 evdns_base_resolve_ipv4(struct evdns_base
*base
, const char *name
, int flags
,
2814 evdns_callback_type callback
, void *ptr
) {
2815 struct evdns_request
*handle
;
2816 struct request
*req
;
2817 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s", name
);
2818 handle
= mm_calloc(1, sizeof(*handle
));
2822 if (flags
& DNS_QUERY_NO_SEARCH
) {
2824 request_new(base
, handle
, TYPE_A
, name
, flags
,
2827 request_submit(req
);
2829 search_request_new(base
, handle
, TYPE_A
, name
, flags
,
2832 if (handle
->current_req
== NULL
) {
2840 int evdns_resolve_ipv4(const char *name
, int flags
,
2841 evdns_callback_type callback
, void *ptr
)
2843 return evdns_base_resolve_ipv4(current_base
, name
, flags
, callback
, ptr
)
2848 /* exported function */
2849 struct evdns_request
*
2850 evdns_base_resolve_ipv6(struct evdns_base
*base
,
2851 const char *name
, int flags
,
2852 evdns_callback_type callback
, void *ptr
)
2854 struct evdns_request
*handle
;
2855 struct request
*req
;
2856 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s", name
);
2857 handle
= mm_calloc(1, sizeof(*handle
));
2861 if (flags
& DNS_QUERY_NO_SEARCH
) {
2862 req
= request_new(base
, handle
, TYPE_AAAA
, name
, flags
,
2865 request_submit(req
);
2867 search_request_new(base
, handle
, TYPE_AAAA
, name
, flags
,
2870 if (handle
->current_req
== NULL
) {
2878 int evdns_resolve_ipv6(const char *name
, int flags
,
2879 evdns_callback_type callback
, void *ptr
) {
2880 return evdns_base_resolve_ipv6(current_base
, name
, flags
, callback
, ptr
)
2884 struct evdns_request
*
2885 evdns_base_resolve_reverse(struct evdns_base
*base
, const struct in_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2887 struct evdns_request
*handle
;
2888 struct request
*req
;
2891 a
= ntohl(in
->s_addr
);
2892 evutil_snprintf(buf
, sizeof(buf
), "%d.%d.%d.%d.in-addr.arpa",
2893 (int)(u8
)((a
)&0xff),
2894 (int)(u8
)((a
>>8 )&0xff),
2895 (int)(u8
)((a
>>16)&0xff),
2896 (int)(u8
)((a
>>24)&0xff));
2897 handle
= mm_calloc(1, sizeof(*handle
));
2900 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s (reverse)", buf
);
2902 req
= request_new(base
, handle
, TYPE_PTR
, buf
, flags
, callback
, ptr
);
2904 request_submit(req
);
2905 if (handle
->current_req
== NULL
) {
2913 int evdns_resolve_reverse(const struct in_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2914 return evdns_base_resolve_reverse(current_base
, in
, flags
, callback
, ptr
)
2918 struct evdns_request
*
2919 evdns_base_resolve_reverse_ipv6(struct evdns_base
*base
, const struct in6_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2920 /* 32 nybbles, 32 periods, "ip6.arpa", NUL. */
2923 struct evdns_request
*handle
;
2924 struct request
*req
;
2928 for (i
=15; i
>= 0; --i
) {
2929 u8 byte
= in
->s6_addr
[i
];
2930 *cp
++ = "0123456789abcdef"[byte
& 0x0f];
2932 *cp
++ = "0123456789abcdef"[byte
>> 4];
2935 EVUTIL_ASSERT(cp
+ strlen("ip6.arpa") < buf
+sizeof(buf
));
2936 memcpy(cp
, "ip6.arpa", strlen("ip6.arpa")+1);
2937 handle
= mm_calloc(1, sizeof(*handle
));
2940 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s (reverse)", buf
);
2942 req
= request_new(base
, handle
, TYPE_PTR
, buf
, flags
, callback
, ptr
);
2944 request_submit(req
);
2945 if (handle
->current_req
== NULL
) {
2953 int evdns_resolve_reverse_ipv6(const struct in6_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2954 return evdns_base_resolve_reverse_ipv6(current_base
, in
, flags
, callback
, ptr
)
2958 /* ================================================================= */
2959 /* Search support */
2961 /* the libc resolver has support for searching a number of domains */
2962 /* to find a name. If nothing else then it takes the single domain */
2963 /* from the gethostname() call. */
2965 /* It can also be configured via the domain and search options in a */
2968 /* The ndots option controls how many dots it takes for the resolver */
2969 /* to decide that a name is non-local and so try a raw lookup first. */
2971 struct search_domain
{
2973 struct search_domain
*next
;
2974 /* the text string is appended to this structure */
2977 struct search_state
{
2981 struct search_domain
*head
;
2985 search_state_decref(struct search_state
*const state
) {
2988 if (!state
->refcount
) {
2989 struct search_domain
*next
, *dom
;
2990 for (dom
= state
->head
; dom
; dom
= next
) {
2998 static struct search_state
*
2999 search_state_new(void) {
3000 struct search_state
*state
= (struct search_state
*) mm_malloc(sizeof(struct search_state
));
3001 if (!state
) return NULL
;
3002 memset(state
, 0, sizeof(struct search_state
));
3003 state
->refcount
= 1;
3010 search_postfix_clear(struct evdns_base
*base
) {
3011 search_state_decref(base
->global_search_state
);
3013 base
->global_search_state
= search_state_new();
3016 /* exported function */
3018 evdns_base_search_clear(struct evdns_base
*base
)
3021 search_postfix_clear(base
);
3026 evdns_search_clear(void) {
3027 evdns_base_search_clear(current_base
);
3031 search_postfix_add(struct evdns_base
*base
, const char *domain
) {
3033 struct search_domain
*sdomain
;
3034 while (domain
[0] == '.') domain
++;
3035 domain_len
= strlen(domain
);
3037 ASSERT_LOCKED(base
);
3038 if (!base
->global_search_state
) base
->global_search_state
= search_state_new();
3039 if (!base
->global_search_state
) return;
3040 base
->global_search_state
->num_domains
++;
3042 sdomain
= (struct search_domain
*) mm_malloc(sizeof(struct search_domain
) + domain_len
);
3043 if (!sdomain
) return;
3044 memcpy( ((u8
*) sdomain
) + sizeof(struct search_domain
), domain
, domain_len
);
3045 sdomain
->next
= base
->global_search_state
->head
;
3046 sdomain
->len
= (int) domain_len
;
3048 base
->global_search_state
->head
= sdomain
;
3051 /* reverse the order of members in the postfix list. This is needed because, */
3052 /* when parsing resolv.conf we push elements in the wrong order */
3054 search_reverse(struct evdns_base
*base
) {
3055 struct search_domain
*cur
, *prev
= NULL
, *next
;
3056 ASSERT_LOCKED(base
);
3057 cur
= base
->global_search_state
->head
;
3065 base
->global_search_state
->head
= prev
;
3068 /* exported function */
3070 evdns_base_search_add(struct evdns_base
*base
, const char *domain
) {
3072 search_postfix_add(base
, domain
);
3076 evdns_search_add(const char *domain
) {
3077 evdns_base_search_add(current_base
, domain
);
3080 /* exported function */
3082 evdns_base_search_ndots_set(struct evdns_base
*base
, const int ndots
) {
3084 if (!base
->global_search_state
) base
->global_search_state
= search_state_new();
3085 if (base
->global_search_state
)
3086 base
->global_search_state
->ndots
= ndots
;
3090 evdns_search_ndots_set(const int ndots
) {
3091 evdns_base_search_ndots_set(current_base
, ndots
);
3095 search_set_from_hostname(struct evdns_base
*base
) {
3096 char hostname
[HOST_NAME_MAX
+ 1], *domainname
;
3098 ASSERT_LOCKED(base
);
3099 search_postfix_clear(base
);
3100 if (gethostname(hostname
, sizeof(hostname
))) return;
3101 domainname
= strchr(hostname
, '.');
3102 if (!domainname
) return;
3103 search_postfix_add(base
, domainname
);
3106 /* warning: returns malloced string */
3108 search_make_new(const struct search_state
*const state
, int n
, const char *const base_name
) {
3109 const size_t base_len
= strlen(base_name
);
3110 const char need_to_append_dot
= base_name
[base_len
- 1] == '.' ? 0 : 1;
3111 struct search_domain
*dom
;
3113 for (dom
= state
->head
; dom
; dom
= dom
->next
) {
3115 /* this is the postfix we want */
3116 /* the actual postfix string is kept at the end of the structure */
3117 const u8
*const postfix
= ((u8
*) dom
) + sizeof(struct search_domain
);
3118 const int postfix_len
= dom
->len
;
3119 char *const newname
= (char *) mm_malloc(base_len
+ need_to_append_dot
+ postfix_len
+ 1);
3120 if (!newname
) return NULL
;
3121 memcpy(newname
, base_name
, base_len
);
3122 if (need_to_append_dot
) newname
[base_len
] = '.';
3123 memcpy(newname
+ base_len
+ need_to_append_dot
, postfix
, postfix_len
);
3124 newname
[base_len
+ need_to_append_dot
+ postfix_len
] = 0;
3129 /* we ran off the end of the list and still didn't find the requested string */
3131 return NULL
; /* unreachable; stops warnings in some compilers. */
3134 static struct request
*
3135 search_request_new(struct evdns_base
*base
, struct evdns_request
*handle
,
3136 int type
, const char *const name
, int flags
,
3137 evdns_callback_type user_callback
, void *user_arg
) {
3138 ASSERT_LOCKED(base
);
3139 EVUTIL_ASSERT(type
== TYPE_A
|| type
== TYPE_AAAA
);
3140 EVUTIL_ASSERT(handle
->current_req
== NULL
);
3141 if ( ((flags
& DNS_QUERY_NO_SEARCH
) == 0) &&
3142 base
->global_search_state
&&
3143 base
->global_search_state
->num_domains
) {
3144 /* we have some domains to search */
3145 struct request
*req
;
3146 if (string_num_dots(name
) >= base
->global_search_state
->ndots
) {
3147 req
= request_new(base
, handle
, type
, name
, flags
, user_callback
, user_arg
);
3148 if (!req
) return NULL
;
3149 handle
->search_index
= -1;
3151 char *const new_name
= search_make_new(base
->global_search_state
, 0, name
);
3152 if (!new_name
) return NULL
;
3153 req
= request_new(base
, handle
, type
, new_name
, flags
, user_callback
, user_arg
);
3155 if (!req
) return NULL
;
3156 handle
->search_index
= 0;
3158 EVUTIL_ASSERT(handle
->search_origname
== NULL
);
3159 handle
->search_origname
= mm_strdup(name
);
3160 if (handle
->search_origname
== NULL
) {
3161 /* XXX Should we dealloc req? If yes, how? */
3166 handle
->search_state
= base
->global_search_state
;
3167 handle
->search_flags
= flags
;
3168 base
->global_search_state
->refcount
++;
3169 request_submit(req
);
3172 struct request
*const req
= request_new(base
, handle
, type
, name
, flags
, user_callback
, user_arg
);
3173 if (!req
) return NULL
;
3174 request_submit(req
);
3179 /* this is called when a request has failed to find a name. We need to check */
3180 /* if it is part of a search and, if so, try the next name in the list */
3182 /* 0 another request has been submitted */
3183 /* 1 no more requests needed */
3185 search_try_next(struct evdns_request
*const handle
) {
3186 struct request
*req
= handle
->current_req
;
3187 struct evdns_base
*base
= req
->base
;
3188 struct request
*newreq
;
3189 ASSERT_LOCKED(base
);
3190 if (handle
->search_state
) {
3191 /* it is part of a search */
3193 handle
->search_index
++;
3194 if (handle
->search_index
>= handle
->search_state
->num_domains
) {
3195 /* no more postfixes to try, however we may need to try */
3196 /* this name without a postfix */
3197 if (string_num_dots(handle
->search_origname
) < handle
->search_state
->ndots
) {
3198 /* yep, we need to try it raw */
3199 newreq
= request_new(base
, NULL
, req
->request_type
, handle
->search_origname
, handle
->search_flags
, req
->user_callback
, req
->user_pointer
);
3200 log(EVDNS_LOG_DEBUG
, "Search: trying raw query %s", handle
->search_origname
);
3202 search_request_finished(handle
);
3209 new_name
= search_make_new(handle
->search_state
, handle
->search_index
, handle
->search_origname
);
3210 if (!new_name
) return 1;
3211 log(EVDNS_LOG_DEBUG
, "Search: now trying %s (%d)", new_name
, handle
->search_index
);
3212 newreq
= request_new(base
, NULL
, req
->request_type
, new_name
, handle
->search_flags
, req
->user_callback
, req
->user_pointer
);
3214 if (!newreq
) return 1;
3220 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 0);
3221 handle
->current_req
= newreq
;
3222 newreq
->handle
= handle
;
3223 request_submit(newreq
);
3228 search_request_finished(struct evdns_request
*const handle
) {
3229 ASSERT_LOCKED(handle
->current_req
->base
);
3230 if (handle
->search_state
) {
3231 search_state_decref(handle
->search_state
);
3232 handle
->search_state
= NULL
;
3234 if (handle
->search_origname
) {
3235 mm_free(handle
->search_origname
);
3236 handle
->search_origname
= NULL
;
3240 /* ================================================================= */
3241 /* Parsing resolv.conf files */
3244 evdns_resolv_set_defaults(struct evdns_base
*base
, int flags
) {
3245 /* if the file isn't found then we assume a local resolver */
3246 ASSERT_LOCKED(base
);
3247 if (flags
& DNS_OPTION_SEARCH
) search_set_from_hostname(base
);
3248 if (flags
& DNS_OPTION_NAMESERVERS
) evdns_base_nameserver_ip_add(base
,"127.0.0.1");
3251 #ifndef _EVENT_HAVE_STRTOK_R
3253 strtok_r(char *s
, const char *delim
, char **state
) {
3255 start
= cp
= s
? s
: *state
;
3258 while (*cp
&& !strchr(delim
, *cp
))
3273 /* helper version of atoi which returns -1 on error */
3275 strtoint(const char *const str
)
3278 const int r
= strtol(str
, &endptr
, 10);
3279 if (*endptr
) return -1;
3283 /* Parse a number of seconds into a timeval; return -1 on error. */
3285 strtotimeval(const char *const str
, struct timeval
*out
)
3289 d
= strtod(str
, &endptr
);
3290 if (*endptr
) return -1;
3291 if (d
< 0) return -1;
3292 out
->tv_sec
= (int) d
;
3293 out
->tv_usec
= (int) ((d
- (int) d
)*1000000);
3294 if (out
->tv_sec
== 0 && out
->tv_usec
< 1000) /* less than 1 msec */
3299 /* helper version of atoi that returns -1 on error and clips to bounds. */
3301 strtoint_clipped(const char *const str
, int min
, int max
)
3303 int r
= strtoint(str
);
3315 evdns_base_set_max_requests_inflight(struct evdns_base
*base
, int maxinflight
)
3317 int old_n_heads
= base
->n_req_heads
, n_heads
;
3318 struct request
**old_heads
= base
->req_heads
, **new_heads
, *req
;
3321 ASSERT_LOCKED(base
);
3322 if (maxinflight
< 1)
3324 n_heads
= (maxinflight
+4) / 5;
3325 EVUTIL_ASSERT(n_heads
> 0);
3326 new_heads
= mm_calloc(n_heads
, sizeof(struct request
*));
3330 for (i
= 0; i
< old_n_heads
; ++i
) {
3331 while (old_heads
[i
]) {
3333 evdns_request_remove(req
, &old_heads
[i
]);
3334 evdns_request_insert(req
, &new_heads
[req
->trans_id
% n_heads
]);
3339 base
->req_heads
= new_heads
;
3340 base
->n_req_heads
= n_heads
;
3341 base
->global_max_requests_inflight
= maxinflight
;
3345 /* exported function */
3347 evdns_base_set_option(struct evdns_base
*base
,
3348 const char *option
, const char *val
)
3352 res
= evdns_base_set_option_impl(base
, option
, val
, DNS_OPTIONS_ALL
);
3358 str_matches_option(const char *s1
, const char *optionname
)
3360 /* Option names are given as "option:" We accept either 'option' in
3361 * s1, or 'option:randomjunk'. The latter form is to implement the
3362 * resolv.conf parser. */
3363 size_t optlen
= strlen(optionname
);
3364 size_t slen
= strlen(s1
);
3365 if (slen
== optlen
|| slen
== optlen
- 1)
3366 return !strncmp(s1
, optionname
, slen
);
3367 else if (slen
> optlen
)
3368 return !strncmp(s1
, optionname
, optlen
);
3374 evdns_base_set_option_impl(struct evdns_base
*base
,
3375 const char *option
, const char *val
, int flags
)
3377 ASSERT_LOCKED(base
);
3378 if (str_matches_option(option
, "ndots:")) {
3379 const int ndots
= strtoint(val
);
3380 if (ndots
== -1) return -1;
3381 if (!(flags
& DNS_OPTION_SEARCH
)) return 0;
3382 log(EVDNS_LOG_DEBUG
, "Setting ndots to %d", ndots
);
3383 if (!base
->global_search_state
) base
->global_search_state
= search_state_new();
3384 if (!base
->global_search_state
) return -1;
3385 base
->global_search_state
->ndots
= ndots
;
3386 } else if (str_matches_option(option
, "timeout:")) {
3388 if (strtotimeval(val
, &tv
) == -1) return -1;
3389 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3390 log(EVDNS_LOG_DEBUG
, "Setting timeout to %s", val
);
3391 memcpy(&base
->global_timeout
, &tv
, sizeof(struct timeval
));
3392 } else if (str_matches_option(option
, "getaddrinfo-allow-skew:")) {
3394 if (strtotimeval(val
, &tv
) == -1) return -1;
3395 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3396 log(EVDNS_LOG_DEBUG
, "Setting getaddrinfo-allow-skew to %s",
3398 memcpy(&base
->global_getaddrinfo_allow_skew
, &tv
,
3399 sizeof(struct timeval
));
3400 } else if (str_matches_option(option
, "max-timeouts:")) {
3401 const int maxtimeout
= strtoint_clipped(val
, 1, 255);
3402 if (maxtimeout
== -1) return -1;
3403 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3404 log(EVDNS_LOG_DEBUG
, "Setting maximum allowed timeouts to %d",
3406 base
->global_max_nameserver_timeout
= maxtimeout
;
3407 } else if (str_matches_option(option
, "max-inflight:")) {
3408 const int maxinflight
= strtoint_clipped(val
, 1, 65000);
3409 if (maxinflight
== -1) return -1;
3410 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3411 log(EVDNS_LOG_DEBUG
, "Setting maximum inflight requests to %d",
3413 evdns_base_set_max_requests_inflight(base
, maxinflight
);
3414 } else if (str_matches_option(option
, "attempts:")) {
3415 int retries
= strtoint(val
);
3416 if (retries
== -1) return -1;
3417 if (retries
> 255) retries
= 255;
3418 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3419 log(EVDNS_LOG_DEBUG
, "Setting retries to %d", retries
);
3420 base
->global_max_retransmits
= retries
;
3421 } else if (str_matches_option(option
, "randomize-case:")) {
3422 int randcase
= strtoint(val
);
3423 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3424 base
->global_randomize_case
= randcase
;
3425 } else if (str_matches_option(option
, "bind-to:")) {
3426 /* XXX This only applies to successive nameservers, not
3427 * to already-configured ones. We might want to fix that. */
3428 int len
= sizeof(base
->global_outgoing_address
);
3429 if (!(flags
& DNS_OPTION_NAMESERVERS
)) return 0;
3430 if (evutil_parse_sockaddr_port(val
,
3431 (struct sockaddr
*)&base
->global_outgoing_address
, &len
))
3433 base
->global_outgoing_addrlen
= len
;
3434 } else if (str_matches_option(option
, "initial-probe-timeout:")) {
3436 if (strtotimeval(val
, &tv
) == -1) return -1;
3437 if (tv
.tv_sec
> 3600)
3439 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3440 log(EVDNS_LOG_DEBUG
, "Setting initial probe timeout to %s",
3442 memcpy(&base
->global_nameserver_probe_initial_timeout
, &tv
,
3449 evdns_set_option(const char *option
, const char *val
, int flags
)
3452 current_base
= evdns_base_new(NULL
, 0);
3453 return evdns_base_set_option(current_base
, option
, val
);
3457 resolv_conf_parse_line(struct evdns_base
*base
, char *const start
, int flags
) {
3459 static const char *const delims
= " \t";
3460 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
3463 char *const first_token
= strtok_r(start
, delims
, &strtok_state
);
3464 ASSERT_LOCKED(base
);
3465 if (!first_token
) return;
3467 if (!strcmp(first_token
, "nameserver") && (flags
& DNS_OPTION_NAMESERVERS
)) {
3468 const char *const nameserver
= NEXT_TOKEN
;
3471 evdns_base_nameserver_ip_add(base
, nameserver
);
3472 } else if (!strcmp(first_token
, "domain") && (flags
& DNS_OPTION_SEARCH
)) {
3473 const char *const domain
= NEXT_TOKEN
;
3475 search_postfix_clear(base
);
3476 search_postfix_add(base
, domain
);
3478 } else if (!strcmp(first_token
, "search") && (flags
& DNS_OPTION_SEARCH
)) {
3480 search_postfix_clear(base
);
3482 while ((domain
= NEXT_TOKEN
)) {
3483 search_postfix_add(base
, domain
);
3485 search_reverse(base
);
3486 } else if (!strcmp(first_token
, "options")) {
3488 while ((option
= NEXT_TOKEN
)) {
3489 const char *val
= strchr(option
, ':');
3490 evdns_base_set_option_impl(base
, option
, val
? val
+1 : "", flags
);
3496 /* exported function */
3499 /* 1 failed to open file */
3500 /* 2 failed to stat file */
3501 /* 3 file too large */
3502 /* 4 out of memory */
3503 /* 5 short read from file */
3505 evdns_base_resolv_conf_parse(struct evdns_base
*base
, int flags
, const char *const filename
) {
3508 res
= evdns_base_resolv_conf_parse_impl(base
, flags
, filename
);
3514 evdns_get_default_hosts_filename(void)
3517 /* Windows is a little coy about where it puts its configuration
3518 * files. Sure, they're _usually_ in C:\windows\system32, but
3519 * there's no reason in principle they couldn't be in
3520 * W:\hoboken chicken emergency\
3522 char path
[MAX_PATH
+1];
3523 static const char hostfile
[] = "\\drivers\\etc\\hosts";
3527 if (! SHGetSpecialFolderPathA(NULL
, path
, CSIDL_SYSTEM
, 0))
3529 len_out
= strlen(path
)+strlen(hostfile
);
3530 path_out
= mm_malloc(len_out
+1);
3531 evutil_snprintf(path_out
, len_out
, "%s%s", path
, hostfile
);
3534 return mm_strdup("/etc/hosts");
3539 evdns_base_resolv_conf_parse_impl(struct evdns_base
*base
, int flags
, const char *const filename
) {
3545 log(EVDNS_LOG_DEBUG
, "Parsing resolv.conf file %s", filename
);
3547 if (flags
& DNS_OPTION_HOSTSFILE
) {
3548 char *fname
= evdns_get_default_hosts_filename();
3549 evdns_base_load_hosts(base
, fname
);
3554 if ((err
= evutil_read_file(filename
, &resolv
, &n
, 0)) < 0) {
3557 evdns_resolv_set_defaults(base
, flags
);
3566 char *const newline
= strchr(start
, '\n');
3568 resolv_conf_parse_line(base
, start
, flags
);
3572 resolv_conf_parse_line(base
, start
, flags
);
3573 start
= newline
+ 1;
3577 if (!base
->server_head
&& (flags
& DNS_OPTION_NAMESERVERS
)) {
3578 /* no nameservers were configured. */
3579 evdns_base_nameserver_ip_add(base
, "127.0.0.1");
3582 if (flags
& DNS_OPTION_SEARCH
&& (!base
->global_search_state
|| base
->global_search_state
->num_domains
== 0)) {
3583 search_set_from_hostname(base
);
3591 evdns_resolv_conf_parse(int flags
, const char *const filename
) {
3593 current_base
= evdns_base_new(NULL
, 0);
3594 return evdns_base_resolv_conf_parse(current_base
, flags
, filename
);
3599 /* Add multiple nameservers from a space-or-comma-separated list. */
3601 evdns_nameserver_ip_add_line(struct evdns_base
*base
, const char *ips
) {
3605 ASSERT_LOCKED(base
);
3607 while (isspace(*ips
) || *ips
== ',' || *ips
== '\t')
3610 while (isdigit(*ips
) || *ips
== '.' || *ips
== ':' ||
3611 *ips
=='[' || *ips
==']')
3613 buf
= mm_malloc(ips
-addr
+1);
3615 memcpy(buf
, addr
, ips
-addr
);
3616 buf
[ips
-addr
] = '\0';
3617 r
= evdns_base_nameserver_ip_add(base
, buf
);
3624 typedef DWORD(WINAPI
*GetNetworkParams_fn_t
)(FIXED_INFO
*, DWORD
*);
3626 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
3627 /* figure out what our nameservers are. */
3629 load_nameservers_with_getnetworkparams(struct evdns_base
*base
)
3631 /* Based on MSDN examples and inspection of c-ares code. */
3634 ULONG size
= sizeof(FIXED_INFO
);
3636 int status
= 0, r
, added_any
;
3638 GetNetworkParams_fn_t fn
;
3640 ASSERT_LOCKED(base
);
3641 if (!(handle
= evutil_load_windows_system_library(
3642 TEXT("iphlpapi.dll")))) {
3643 log(EVDNS_LOG_WARN
, "Could not open iphlpapi.dll");
3647 if (!(fn
= (GetNetworkParams_fn_t
) GetProcAddress(handle
, "GetNetworkParams"))) {
3648 log(EVDNS_LOG_WARN
, "Could not get address of function.");
3653 buf
= mm_malloc(size
);
3654 if (!buf
) { status
= 4; goto done
; }
3656 r
= fn(fixed
, &size
);
3657 if (r
!= ERROR_SUCCESS
&& r
!= ERROR_BUFFER_OVERFLOW
) {
3661 if (r
!= ERROR_SUCCESS
) {
3663 buf
= mm_malloc(size
);
3664 if (!buf
) { status
= 4; goto done
; }
3666 r
= fn(fixed
, &size
);
3667 if (r
!= ERROR_SUCCESS
) {
3668 log(EVDNS_LOG_DEBUG
, "fn() failed.");
3674 EVUTIL_ASSERT(fixed
);
3676 ns
= &(fixed
->DnsServerList
);
3678 r
= evdns_nameserver_ip_add_line(base
, ns
->IpAddress
.String
);
3680 log(EVDNS_LOG_DEBUG
,"Could not add nameserver %s to list,error: %d",
3681 (ns
->IpAddress
.String
),(int)GetLastError());
3685 log(EVDNS_LOG_DEBUG
,"Successfully added %s as nameserver",ns
->IpAddress
.String
);
3692 log(EVDNS_LOG_DEBUG
, "No nameservers added.");
3703 FreeLibrary(handle
);
3708 config_nameserver_from_reg_key(struct evdns_base
*base
, HKEY key
, const TCHAR
*subkey
)
3711 DWORD bufsz
= 0, type
= 0;
3714 ASSERT_LOCKED(base
);
3715 if (RegQueryValueEx(key
, subkey
, 0, &type
, NULL
, &bufsz
)
3718 if (!(buf
= mm_malloc(bufsz
)))
3721 if (RegQueryValueEx(key
, subkey
, 0, &type
, (LPBYTE
)buf
, &bufsz
)
3722 == ERROR_SUCCESS
&& bufsz
> 1) {
3723 status
= evdns_nameserver_ip_add_line(base
,buf
);
3730 #define SERVICES_KEY TEXT("System\\CurrentControlSet\\Services\\")
3731 #define WIN_NS_9X_KEY SERVICES_KEY TEXT("VxD\\MSTCP")
3732 #define WIN_NS_NT_KEY SERVICES_KEY TEXT("Tcpip\\Parameters")
3735 load_nameservers_from_registry(struct evdns_base
*base
)
3739 #define TRY(k, name) \
3740 if (!found && config_nameserver_from_reg_key(base,k,TEXT(name)) == 0) { \
3741 log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
3743 } else if (!found) { \
3744 log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
3748 ASSERT_LOCKED(base
);
3750 if (((int)GetVersion()) > 0) { /* NT */
3751 HKEY nt_key
= 0, interfaces_key
= 0;
3753 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, WIN_NS_NT_KEY
, 0,
3754 KEY_READ
, &nt_key
) != ERROR_SUCCESS
) {
3755 log(EVDNS_LOG_DEBUG
,"Couldn't open nt key, %d",(int)GetLastError());
3758 r
= RegOpenKeyEx(nt_key
, TEXT("Interfaces"), 0,
3759 KEY_QUERY_VALUE
|KEY_ENUMERATE_SUB_KEYS
,
3761 if (r
!= ERROR_SUCCESS
) {
3762 log(EVDNS_LOG_DEBUG
,"Couldn't open interfaces key, %d",(int)GetLastError());
3765 TRY(nt_key
, "NameServer");
3766 TRY(nt_key
, "DhcpNameServer");
3767 TRY(interfaces_key
, "NameServer");
3768 TRY(interfaces_key
, "DhcpNameServer");
3769 RegCloseKey(interfaces_key
);
3770 RegCloseKey(nt_key
);
3773 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, WIN_NS_9X_KEY
, 0,
3774 KEY_READ
, &win_key
) != ERROR_SUCCESS
) {
3775 log(EVDNS_LOG_DEBUG
, "Couldn't open registry key, %d", (int)GetLastError());
3778 TRY(win_key
, "NameServer");
3779 RegCloseKey(win_key
);
3783 log(EVDNS_LOG_WARN
,"Didn't find any nameservers.");
3786 return found
? 0 : -1;
3791 evdns_base_config_windows_nameservers(struct evdns_base
*base
)
3796 base
= current_base
;
3800 if (load_nameservers_with_getnetworkparams(base
) == 0) {
3804 r
= load_nameservers_from_registry(base
);
3806 fname
= evdns_get_default_hosts_filename();
3807 evdns_base_load_hosts(base
, fname
);
3816 evdns_config_windows_nameservers(void)
3818 if (!current_base
) {
3819 current_base
= evdns_base_new(NULL
, 1);
3820 return current_base
== NULL
? -1 : 0;
3822 return evdns_base_config_windows_nameservers(current_base
);
3828 evdns_base_new(struct event_base
*event_base
, int initialize_nameservers
)
3830 struct evdns_base
*base
;
3832 if (evutil_secure_rng_init() < 0) {
3833 log(EVDNS_LOG_WARN
, "Unable to seed random number generator; "
3838 /* Give the evutil library a hook into its evdns-enabled
3839 * functionality. We can't just call evdns_getaddrinfo directly or
3840 * else libevent-core will depend on libevent-extras. */
3841 evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo
);
3843 base
= mm_malloc(sizeof(struct evdns_base
));
3846 memset(base
, 0, sizeof(struct evdns_base
));
3847 base
->req_waiting_head
= NULL
;
3849 EVTHREAD_ALLOC_LOCK(base
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
3852 /* Set max requests inflight and allocate req_heads. */
3853 base
->req_heads
= NULL
;
3855 evdns_base_set_max_requests_inflight(base
, 64);
3857 base
->server_head
= NULL
;
3858 base
->event_base
= event_base
;
3859 base
->global_good_nameservers
= base
->global_requests_inflight
=
3860 base
->global_requests_waiting
= 0;
3862 base
->global_timeout
.tv_sec
= 5;
3863 base
->global_timeout
.tv_usec
= 0;
3864 base
->global_max_reissues
= 1;
3865 base
->global_max_retransmits
= 3;
3866 base
->global_max_nameserver_timeout
= 3;
3867 base
->global_search_state
= NULL
;
3868 base
->global_randomize_case
= 1;
3869 base
->global_getaddrinfo_allow_skew
.tv_sec
= 3;
3870 base
->global_getaddrinfo_allow_skew
.tv_usec
= 0;
3871 base
->global_nameserver_probe_initial_timeout
.tv_sec
= 10;
3872 base
->global_nameserver_probe_initial_timeout
.tv_usec
= 0;
3874 TAILQ_INIT(&base
->hostsdb
);
3876 if (initialize_nameservers
) {
3879 r
= evdns_base_config_windows_nameservers(base
);
3881 r
= evdns_base_resolv_conf_parse(base
, DNS_OPTIONS_ALL
, "/etc/resolv.conf");
3884 evdns_base_free_and_unlock(base
, 0);
3895 struct evdns_base
*base
= evdns_base_new(NULL
, 1);
3897 current_base
= base
;
3905 evdns_err_to_string(int err
)
3908 case DNS_ERR_NONE
: return "no error";
3909 case DNS_ERR_FORMAT
: return "misformatted query";
3910 case DNS_ERR_SERVERFAILED
: return "server failed";
3911 case DNS_ERR_NOTEXIST
: return "name does not exist";
3912 case DNS_ERR_NOTIMPL
: return "query not implemented";
3913 case DNS_ERR_REFUSED
: return "refused";
3915 case DNS_ERR_TRUNCATED
: return "reply truncated or ill-formed";
3916 case DNS_ERR_UNKNOWN
: return "unknown";
3917 case DNS_ERR_TIMEOUT
: return "request timed out";
3918 case DNS_ERR_SHUTDOWN
: return "dns subsystem shut down";
3919 case DNS_ERR_CANCEL
: return "dns request canceled";
3920 case DNS_ERR_NODATA
: return "no records in the reply";
3921 default: return "[Unknown error code]";
3926 evdns_nameserver_free(struct nameserver
*server
)
3928 if (server
->socket
>= 0)
3929 evutil_closesocket(server
->socket
);
3930 (void) event_del(&server
->event
);
3931 event_debug_unassign(&server
->event
);
3932 if (server
->state
== 0)
3933 (void) event_del(&server
->timeout_event
);
3934 event_debug_unassign(&server
->timeout_event
);
3939 evdns_base_free_and_unlock(struct evdns_base
*base
, int fail_requests
)
3941 struct nameserver
*server
, *server_next
;
3942 struct search_domain
*dom
, *dom_next
;
3945 /* Requires that we hold the lock. */
3947 /* TODO(nickm) we might need to refcount here. */
3949 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
3950 while (base
->req_heads
[i
]) {
3952 reply_schedule_callback(base
->req_heads
[i
], 0, DNS_ERR_SHUTDOWN
, NULL
);
3953 request_finished(base
->req_heads
[i
], &REQ_HEAD(base
, base
->req_heads
[i
]->trans_id
), 1);
3956 while (base
->req_waiting_head
) {
3958 reply_schedule_callback(base
->req_waiting_head
, 0, DNS_ERR_SHUTDOWN
, NULL
);
3959 request_finished(base
->req_waiting_head
, &base
->req_waiting_head
, 1);
3961 base
->global_requests_inflight
= base
->global_requests_waiting
= 0;
3963 for (server
= base
->server_head
; server
; server
= server_next
) {
3964 server_next
= server
->next
;
3965 evdns_nameserver_free(server
);
3966 if (server_next
== base
->server_head
)
3969 base
->server_head
= NULL
;
3970 base
->global_good_nameservers
= 0;
3972 if (base
->global_search_state
) {
3973 for (dom
= base
->global_search_state
->head
; dom
; dom
= dom_next
) {
3974 dom_next
= dom
->next
;
3977 mm_free(base
->global_search_state
);
3978 base
->global_search_state
= NULL
;
3982 struct hosts_entry
*victim
;
3983 while ((victim
= TAILQ_FIRST(&base
->hostsdb
))) {
3984 TAILQ_REMOVE(&base
->hostsdb
, victim
, next
);
3989 mm_free(base
->req_heads
);
3992 EVTHREAD_FREE_LOCK(base
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
3998 evdns_base_free(struct evdns_base
*base
, int fail_requests
)
4001 evdns_base_free_and_unlock(base
, fail_requests
);
4005 evdns_shutdown(int fail_requests
)
4008 struct evdns_base
*b
= current_base
;
4009 current_base
= NULL
;
4010 evdns_base_free(b
, fail_requests
);
4012 evdns_log_fn
= NULL
;
4016 evdns_base_parse_hosts_line(struct evdns_base
*base
, char *line
)
4019 static const char *const delims
= " \t";
4020 char *const addr
= strtok_r(line
, delims
, &strtok_state
);
4021 char *hostname
, *hash
;
4022 struct sockaddr_storage ss
;
4023 int socklen
= sizeof(ss
);
4024 ASSERT_LOCKED(base
);
4026 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
4028 if (!addr
|| *addr
== '#')
4031 memset(&ss
, 0, sizeof(ss
));
4032 if (evutil_parse_sockaddr_port(addr
, (struct sockaddr
*)&ss
, &socklen
)<0)
4034 if (socklen
> (int)sizeof(struct sockaddr_in6
))
4037 if (sockaddr_getport((struct sockaddr
*)&ss
))
4040 while ((hostname
= NEXT_TOKEN
)) {
4041 struct hosts_entry
*he
;
4043 if ((hash
= strchr(hostname
, '#'))) {
4044 if (hash
== hostname
)
4049 namelen
= strlen(hostname
);
4051 he
= mm_calloc(1, sizeof(struct hosts_entry
)+namelen
);
4054 EVUTIL_ASSERT(socklen
<= (int)sizeof(he
->addr
));
4055 memcpy(&he
->addr
, &ss
, socklen
);
4056 memcpy(he
->hostname
, hostname
, namelen
+1);
4057 he
->addrlen
= socklen
;
4059 TAILQ_INSERT_TAIL(&base
->hostsdb
, he
, next
);
4070 evdns_base_load_hosts_impl(struct evdns_base
*base
, const char *hosts_fname
)
4072 char *str
=NULL
, *cp
, *eol
;
4076 ASSERT_LOCKED(base
);
4078 if (hosts_fname
== NULL
||
4079 (err
= evutil_read_file(hosts_fname
, &str
, &len
, 0)) < 0) {
4081 strlcpy(tmp
, "127.0.0.1 localhost", sizeof(tmp
));
4082 evdns_base_parse_hosts_line(base
, tmp
);
4083 strlcpy(tmp
, "::1 localhost", sizeof(tmp
));
4084 evdns_base_parse_hosts_line(base
, tmp
);
4085 return err
? -1 : 0;
4088 /* This will break early if there is a NUL in the hosts file.
4089 * Probably not a problem.*/
4092 eol
= strchr(cp
, '\n');
4096 evdns_base_parse_hosts_line(base
, cp
);
4099 evdns_base_parse_hosts_line(base
, cp
);
4109 evdns_base_load_hosts(struct evdns_base
*base
, const char *hosts_fname
)
4113 base
= current_base
;
4115 res
= evdns_base_load_hosts_impl(base
, hosts_fname
);
4120 /* A single request for a getaddrinfo, either v4 or v6. */
4121 struct getaddrinfo_subrequest
{
4122 struct evdns_request
*r
;
4126 /* State data used to implement an in-progress getaddrinfo. */
4127 struct evdns_getaddrinfo_request
{
4128 struct evdns_base
*evdns_base
;
4129 /* Copy of the modified 'hints' data that we'll use to build
4131 struct evutil_addrinfo hints
;
4132 /* The callback to invoke when we're done */
4133 evdns_getaddrinfo_cb user_cb
;
4134 /* User-supplied data to give to the callback. */
4136 /* The port to use when building sockaddrs. */
4138 /* The sub_request for an A record (if any) */
4139 struct getaddrinfo_subrequest ipv4_request
;
4140 /* The sub_request for an AAAA record (if any) */
4141 struct getaddrinfo_subrequest ipv6_request
;
4143 /* The cname result that we were told (if any) */
4146 /* If we have one request answered and one request still inflight,
4147 * then this field holds the answer from the first request... */
4148 struct evutil_addrinfo
*pending_result
;
4149 /* And this event is a timeout that will tell us to cancel the second
4150 * request if it's taking a long time. */
4151 struct event timeout
;
4153 /* And this field holds the error code from the first request... */
4155 /* If this is set, the user canceled this request. */
4156 unsigned user_canceled
: 1;
4157 /* If this is set, the user can no longer cancel this request; we're
4158 * just waiting for the free. */
4159 unsigned request_done
: 1;
4162 /* Convert an evdns errors to the equivalent getaddrinfo error. */
4164 evdns_err_to_getaddrinfo_err(int e1
)
4166 /* XXX Do this better! */
4167 if (e1
== DNS_ERR_NONE
)
4169 else if (e1
== DNS_ERR_NOTEXIST
)
4170 return EVUTIL_EAI_NONAME
;
4172 return EVUTIL_EAI_FAIL
;
4175 /* Return the more informative of two getaddrinfo errors. */
4177 getaddrinfo_merge_err(int e1
, int e2
)
4179 /* XXXX be cleverer here. */
4187 free_getaddrinfo_request(struct evdns_getaddrinfo_request
*data
)
4189 /* DO NOT CALL this if either of the requests is pending. Only once
4190 * both callbacks have been invoked is it safe to free the request */
4191 if (data
->pending_result
)
4192 evutil_freeaddrinfo(data
->pending_result
);
4193 if (data
->cname_result
)
4194 mm_free(data
->cname_result
);
4195 event_del(&data
->timeout
);
4201 add_cname_to_reply(struct evdns_getaddrinfo_request
*data
,
4202 struct evutil_addrinfo
*ai
)
4204 if (data
->cname_result
&& ai
) {
4205 ai
->ai_canonname
= data
->cname_result
;
4206 data
->cname_result
= NULL
;
4210 /* Callback: invoked when one request in a mixed-format A/AAAA getaddrinfo
4211 * request has finished, but the other one took too long to answer. Pass
4212 * along the answer we got, and cancel the other request.
4215 evdns_getaddrinfo_timeout_cb(evutil_socket_t fd
, short what
, void *ptr
)
4217 int v4_timedout
= 0, v6_timedout
= 0;
4218 struct evdns_getaddrinfo_request
*data
= ptr
;
4220 /* Cancel any pending requests, and note which one */
4221 if (data
->ipv4_request
.r
) {
4222 /* XXXX This does nothing if the request's callback is already
4223 * running (pending_cb is set). */
4224 evdns_cancel_request(NULL
, data
->ipv4_request
.r
);
4226 EVDNS_LOCK(data
->evdns_base
);
4227 ++data
->evdns_base
->getaddrinfo_ipv4_timeouts
;
4228 EVDNS_UNLOCK(data
->evdns_base
);
4230 if (data
->ipv6_request
.r
) {
4231 /* XXXX This does nothing if the request's callback is already
4232 * running (pending_cb is set). */
4233 evdns_cancel_request(NULL
, data
->ipv6_request
.r
);
4235 EVDNS_LOCK(data
->evdns_base
);
4236 ++data
->evdns_base
->getaddrinfo_ipv6_timeouts
;
4237 EVDNS_UNLOCK(data
->evdns_base
);
4240 /* We only use this timeout callback when we have an answer for
4242 EVUTIL_ASSERT(!v4_timedout
|| !v6_timedout
);
4244 /* Report the outcome of the other request that didn't time out. */
4245 if (data
->pending_result
) {
4246 add_cname_to_reply(data
, data
->pending_result
);
4247 data
->user_cb(0, data
->pending_result
, data
->user_data
);
4248 data
->pending_result
= NULL
;
4250 int e
= data
->pending_error
;
4252 e
= EVUTIL_EAI_AGAIN
;
4253 data
->user_cb(e
, NULL
, data
->user_data
);
4256 data
->user_cb
= NULL
; /* prevent double-call if evdns callbacks are
4257 * in-progress. XXXX It would be better if this
4258 * weren't necessary. */
4260 if (!v4_timedout
&& !v6_timedout
) {
4261 /* should be impossible? XXXX */
4262 free_getaddrinfo_request(data
);
4267 evdns_getaddrinfo_set_timeout(struct evdns_base
*evdns_base
,
4268 struct evdns_getaddrinfo_request
*data
)
4270 return event_add(&data
->timeout
, &evdns_base
->global_getaddrinfo_allow_skew
);
4274 evdns_result_is_answer(int result
)
4276 return (result
!= DNS_ERR_NOTIMPL
&& result
!= DNS_ERR_REFUSED
&&
4277 result
!= DNS_ERR_SERVERFAILED
&& result
!= DNS_ERR_CANCEL
);
4281 evdns_getaddrinfo_gotresolve(int result
, char type
, int count
,
4282 int ttl
, void *addresses
, void *arg
)
4285 struct getaddrinfo_subrequest
*req
= arg
;
4286 struct getaddrinfo_subrequest
*other_req
;
4287 struct evdns_getaddrinfo_request
*data
;
4289 struct evutil_addrinfo
*res
;
4291 struct sockaddr_in sin
;
4292 struct sockaddr_in6 sin6
;
4293 struct sockaddr
*sa
;
4294 int socklen
, addrlen
;
4299 EVUTIL_ASSERT(req
->type
== DNS_IPv4_A
|| req
->type
== DNS_IPv6_AAAA
);
4300 if (req
->type
== DNS_IPv4_A
) {
4301 data
= EVUTIL_UPCAST(req
, struct evdns_getaddrinfo_request
, ipv4_request
);
4302 other_req
= &data
->ipv6_request
;
4304 data
= EVUTIL_UPCAST(req
, struct evdns_getaddrinfo_request
, ipv6_request
);
4305 other_req
= &data
->ipv4_request
;
4308 EVDNS_LOCK(data
->evdns_base
);
4309 if (evdns_result_is_answer(result
)) {
4310 if (req
->type
== DNS_IPv4_A
)
4311 ++data
->evdns_base
->getaddrinfo_ipv4_answered
;
4313 ++data
->evdns_base
->getaddrinfo_ipv6_answered
;
4315 user_canceled
= data
->user_canceled
;
4316 if (other_req
->r
== NULL
)
4317 data
->request_done
= 1;
4318 EVDNS_UNLOCK(data
->evdns_base
);
4322 if (result
== DNS_ERR_CANCEL
&& ! user_canceled
) {
4323 /* Internal cancel request from timeout or internal error.
4324 * we already answered the user. */
4325 if (other_req
->r
== NULL
)
4326 free_getaddrinfo_request(data
);
4330 if (data
->user_cb
== NULL
) {
4331 /* We already answered. XXXX This shouldn't be needed; see
4332 * comments in evdns_getaddrinfo_timeout_cb */
4333 free_getaddrinfo_request(data
);
4337 if (result
== DNS_ERR_NONE
) {
4339 err
= EVUTIL_EAI_NODATA
;
4343 err
= evdns_err_to_getaddrinfo_err(result
);
4347 /* Looks like we got an error. */
4349 /* The other request is still working; maybe it will
4351 /* XXXX handle failure from set_timeout */
4352 evdns_getaddrinfo_set_timeout(data
->evdns_base
, data
);
4353 data
->pending_error
= err
;
4357 if (user_canceled
) {
4358 data
->user_cb(EVUTIL_EAI_CANCEL
, NULL
, data
->user_data
);
4359 } else if (data
->pending_result
) {
4360 /* If we have an answer waiting, and we weren't
4361 * canceled, ignore this error. */
4362 add_cname_to_reply(data
, data
->pending_result
);
4363 data
->user_cb(0, data
->pending_result
, data
->user_data
);
4364 data
->pending_result
= NULL
;
4366 if (data
->pending_error
)
4367 err
= getaddrinfo_merge_err(err
,
4368 data
->pending_error
);
4369 data
->user_cb(err
, NULL
, data
->user_data
);
4371 free_getaddrinfo_request(data
);
4373 } else if (user_canceled
) {
4375 /* The other request is still working; let it hit this
4376 * callback with EVUTIL_EAI_CANCEL callback and report
4380 data
->user_cb(EVUTIL_EAI_CANCEL
, NULL
, data
->user_data
);
4381 free_getaddrinfo_request(data
);
4385 /* Looks like we got some answers. We should turn them into addrinfos
4386 * and then either queue those or return them all. */
4387 EVUTIL_ASSERT(type
== DNS_IPv4_A
|| type
== DNS_IPv6_AAAA
);
4389 if (type
== DNS_IPv4_A
) {
4390 memset(&sin
, 0, sizeof(sin
));
4391 sin
.sin_family
= AF_INET
;
4392 sin
.sin_port
= htons(data
->port
);
4394 sa
= (struct sockaddr
*)&sin
;
4395 socklen
= sizeof(sin
);
4397 addrp
= &sin
.sin_addr
.s_addr
;
4399 memset(&sin6
, 0, sizeof(sin6
));
4400 sin6
.sin6_family
= AF_INET6
;
4401 sin6
.sin6_port
= htons(data
->port
);
4403 sa
= (struct sockaddr
*)&sin6
;
4404 socklen
= sizeof(sin6
);
4406 addrp
= &sin6
.sin6_addr
.s6_addr
;
4410 for (i
=0; i
< count
; ++i
) {
4411 struct evutil_addrinfo
*ai
;
4412 memcpy(addrp
, ((char*)addresses
)+i
*addrlen
, addrlen
);
4413 ai
= evutil_new_addrinfo(sa
, socklen
, &data
->hints
);
4416 evdns_cancel_request(NULL
, other_req
->r
);
4418 data
->user_cb(EVUTIL_EAI_MEMORY
, NULL
, data
->user_data
);
4420 evutil_freeaddrinfo(res
);
4422 if (other_req
->r
== NULL
)
4423 free_getaddrinfo_request(data
);
4426 res
= evutil_addrinfo_append(res
, ai
);
4430 /* The other request is still in progress; wait for it */
4431 /* XXXX handle failure from set_timeout */
4432 evdns_getaddrinfo_set_timeout(data
->evdns_base
, data
);
4433 data
->pending_result
= res
;
4436 /* The other request is done or never started; append its
4437 * results (if any) and return them. */
4438 if (data
->pending_result
) {
4439 if (req
->type
== DNS_IPv4_A
)
4440 res
= evutil_addrinfo_append(res
,
4441 data
->pending_result
);
4443 res
= evutil_addrinfo_append(
4444 data
->pending_result
, res
);
4445 data
->pending_result
= NULL
;
4448 /* Call the user callback. */
4449 add_cname_to_reply(data
, res
);
4450 data
->user_cb(0, res
, data
->user_data
);
4453 free_getaddrinfo_request(data
);
4457 static struct hosts_entry
*
4458 find_hosts_entry(struct evdns_base
*base
, const char *hostname
,
4459 struct hosts_entry
*find_after
)
4461 struct hosts_entry
*e
;
4464 e
= TAILQ_NEXT(find_after
, next
);
4466 e
= TAILQ_FIRST(&base
->hostsdb
);
4468 for (; e
; e
= TAILQ_NEXT(e
, next
)) {
4469 if (!evutil_ascii_strcasecmp(e
->hostname
, hostname
))
4476 evdns_getaddrinfo_fromhosts(struct evdns_base
*base
,
4477 const char *nodename
, struct evutil_addrinfo
*hints
, ev_uint16_t port
,
4478 struct evutil_addrinfo
**res
)
4481 struct hosts_entry
*e
;
4482 struct evutil_addrinfo
*ai
=NULL
;
4483 int f
= hints
->ai_family
;
4486 for (e
= find_hosts_entry(base
, nodename
, NULL
); e
;
4487 e
= find_hosts_entry(base
, nodename
, e
)) {
4488 struct evutil_addrinfo
*ai_new
;
4490 if ((e
->addr
.sa
.sa_family
== AF_INET
&& f
== PF_INET6
) ||
4491 (e
->addr
.sa
.sa_family
== AF_INET6
&& f
== PF_INET
))
4493 ai_new
= evutil_new_addrinfo(&e
->addr
.sa
, e
->addrlen
, hints
);
4498 sockaddr_setport(ai_new
->ai_addr
, port
);
4499 ai
= evutil_addrinfo_append(ai
, ai_new
);
4504 /* Note that we return an empty answer if we found entries for
4505 * this hostname but none were of the right address type. */
4510 evutil_freeaddrinfo(ai
);
4515 struct evdns_getaddrinfo_request
*
4516 evdns_getaddrinfo(struct evdns_base
*dns_base
,
4517 const char *nodename
, const char *servname
,
4518 const struct evutil_addrinfo
*hints_in
,
4519 evdns_getaddrinfo_cb cb
, void *arg
)
4521 struct evdns_getaddrinfo_request
*data
;
4522 struct evutil_addrinfo hints
;
4523 struct evutil_addrinfo
*res
= NULL
;
4529 dns_base
= current_base
;
4532 "Call to getaddrinfo_async with no "
4533 "evdns_base configured.");
4534 cb(EVUTIL_EAI_FAIL
, NULL
, arg
); /* ??? better error? */
4539 /* If we _must_ answer this immediately, do so. */
4540 if ((hints_in
&& (hints_in
->ai_flags
& EVUTIL_AI_NUMERICHOST
))) {
4542 err
= evutil_getaddrinfo(nodename
, servname
, hints_in
, &res
);
4548 memcpy(&hints
, hints_in
, sizeof(hints
));
4550 memset(&hints
, 0, sizeof(hints
));
4551 hints
.ai_family
= PF_UNSPEC
;
4554 evutil_adjust_hints_for_addrconfig(&hints
);
4556 /* Now try to see if we _can_ answer immediately. */
4557 /* (It would be nice to do this by calling getaddrinfo directly, with
4558 * AI_NUMERICHOST, on plaforms that have it, but we can't: there isn't
4559 * a reliable way to distinguish the "that wasn't a numeric host!" case
4560 * from any other EAI_NONAME cases.) */
4561 err
= evutil_getaddrinfo_common(nodename
, servname
, &hints
, &res
, &port
);
4562 if (err
!= EVUTIL_EAI_NEED_RESOLVE
) {
4567 /* If there is an entry in the hosts file, we should give it now. */
4568 if (!evdns_getaddrinfo_fromhosts(dns_base
, nodename
, &hints
, port
, &res
)) {
4573 /* Okay, things are serious now. We're going to need to actually
4576 data
= mm_calloc(1,sizeof(struct evdns_getaddrinfo_request
));
4578 cb(EVUTIL_EAI_MEMORY
, NULL
, arg
);
4582 memcpy(&data
->hints
, &hints
, sizeof(data
->hints
));
4583 data
->port
= (ev_uint16_t
)port
;
4584 data
->ipv4_request
.type
= DNS_IPv4_A
;
4585 data
->ipv6_request
.type
= DNS_IPv6_AAAA
;
4587 data
->user_data
= arg
;
4588 data
->evdns_base
= dns_base
;
4590 want_cname
= (hints
.ai_flags
& EVUTIL_AI_CANONNAME
);
4592 /* If we are asked for a PF_UNSPEC address, we launch two requests in
4593 * parallel: one for an A address and one for an AAAA address. We
4594 * can't send just one request, since many servers only answer one
4595 * question per DNS request.
4597 * Once we have the answer to one request, we allow for a short
4598 * timeout before we report it, to see if the other one arrives. If
4599 * they both show up in time, then we report both the answers.
4601 * If too many addresses of one type time out or fail, we should stop
4602 * launching those requests. (XXX we don't do that yet.)
4605 if (hints
.ai_family
!= PF_INET6
) {
4606 log(EVDNS_LOG_DEBUG
, "Sending request for %s on ipv4 as %p",
4607 nodename
, &data
->ipv4_request
);
4609 data
->ipv4_request
.r
= evdns_base_resolve_ipv4(dns_base
,
4610 nodename
, 0, evdns_getaddrinfo_gotresolve
,
4611 &data
->ipv4_request
);
4613 data
->ipv4_request
.r
->current_req
->put_cname_in_ptr
=
4614 &data
->cname_result
;
4616 if (hints
.ai_family
!= PF_INET
) {
4617 log(EVDNS_LOG_DEBUG
, "Sending request for %s on ipv6 as %p",
4618 nodename
, &data
->ipv6_request
);
4620 data
->ipv6_request
.r
= evdns_base_resolve_ipv6(dns_base
,
4621 nodename
, 0, evdns_getaddrinfo_gotresolve
,
4622 &data
->ipv6_request
);
4624 data
->ipv6_request
.r
->current_req
->put_cname_in_ptr
=
4625 &data
->cname_result
;
4628 evtimer_assign(&data
->timeout
, dns_base
->event_base
,
4629 evdns_getaddrinfo_timeout_cb
, data
);
4631 if (data
->ipv4_request
.r
|| data
->ipv6_request
.r
) {
4635 cb(EVUTIL_EAI_FAIL
, NULL
, arg
);
4641 evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request
*data
)
4643 EVDNS_LOCK(data
->evdns_base
);
4644 if (data
->request_done
) {
4645 EVDNS_UNLOCK(data
->evdns_base
);
4648 event_del(&data
->timeout
);
4649 data
->user_canceled
= 1;
4650 if (data
->ipv4_request
.r
)
4651 evdns_cancel_request(data
->evdns_base
, data
->ipv4_request
.r
);
4652 if (data
->ipv6_request
.r
)
4653 evdns_cancel_request(data
->evdns_base
, data
->ipv6_request
.r
);
4654 EVDNS_UNLOCK(data
->evdns_base
);