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
145 #define MAX_TXT_RRS 32
146 #define MAX_TXT_SIZE 255
149 #define TYPE_A EVDNS_TYPE_A
151 #define TYPE_PTR EVDNS_TYPE_PTR
152 #define TYPE_SOA EVDNS_TYPE_SOA
153 #define TYPE_AAAA EVDNS_TYPE_AAAA
154 #define TYPE_TXT EVDNS_TYPE_TXT
156 #define CLASS_INET EVDNS_CLASS_INET
158 /* Persistent handle. We keep this separate from 'struct request' since we
159 * need some object to last for as long as an evdns_request is outstanding so
160 * that it can be canceled, whereas a search request can lead to multiple
161 * 'struct request' instances being created over its lifetime. */
162 struct evdns_request
{
163 struct request
*current_req
;
164 struct evdns_base
*base
;
166 int pending_cb
; /* Waiting for its callback to be invoked; not
167 * owned by event base any more. */
169 /* elements used by the searching code */
171 struct search_state
*search_state
;
172 char *search_origname
; /* needs to be free()ed */
177 u8
*request
; /* the dns packet data */
178 u8 request_type
; /* TYPE_PTR or TYPE_A or TYPE_AAAA */
179 unsigned int request_len
;
181 int tx_count
; /* the number of times that this packet has been sent */
182 void *user_pointer
; /* the pointer given to us for this request */
183 evdns_callback_type user_callback
;
184 struct nameserver
*ns
; /* the server which we last sent it */
186 /* these objects are kept in a circular list */
187 /* XXX We could turn this into a CIRCLEQ. */
188 struct request
*next
, *prev
;
190 struct event timeout_event
;
192 u16 trans_id
; /* the transaction id */
193 unsigned request_appended
:1; /* true if the request pointer is data which follows this struct */
194 unsigned transmit_me
:1; /* needs to be transmitted */
196 /* XXXX This is a horrible hack. */
197 char **put_cname_in_ptr
; /* store the cname here if we get one. */
199 struct evdns_base
*base
;
201 struct evdns_request
*handle
;
206 unsigned int have_answer
: 1;
210 u32 addresses
[MAX_V4_ADDRS
];
214 struct in6_addr addresses
[MAX_V6_ADDRS
];
217 char name
[HOST_NAME_MAX
];
221 struct txt_record records
[MAX_TXT_RRS
];
227 evutil_socket_t socket
; /* a connected UDP socket */
228 struct sockaddr_storage address
;
229 ev_socklen_t addrlen
;
230 int failed_times
; /* number of times which we have given this server a chance */
231 int timedout
; /* number of times in a row a request has timed out */
233 /* these objects are kept in a circular list */
234 struct nameserver
*next
, *prev
;
235 struct event timeout_event
; /* used to keep the timeout for */
236 /* when we next probe this server. */
237 /* Valid if state == 0 */
238 /* Outstanding probe request for this nameserver, if any */
239 struct evdns_request
*probe_request
;
240 char state
; /* zero if we think that this server is down */
241 char choked
; /* true if we have an EAGAIN from this server's socket */
242 char write_waiting
; /* true if we are waiting for EV_WRITE events */
243 struct evdns_base
*base
;
247 /* Represents a local port where we're listening for DNS requests. Right now, */
248 /* only UDP is supported. */
249 struct evdns_server_port
{
250 evutil_socket_t socket
; /* socket we use to read queries and write replies. */
251 int refcnt
; /* reference count. */
252 char choked
; /* Are we currently blocked from writing? */
253 char closing
; /* Are we trying to close this port, pending writes? */
254 evdns_request_callback_fn_type user_callback
; /* Fn to handle requests */
255 void *user_data
; /* Opaque pointer passed to user_callback */
256 struct event event
; /* Read/write event */
257 /* circular list of replies that we want to write. */
258 struct server_request
*pending_replies
;
259 struct event_base
*event_base
;
261 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
266 /* Represents part of a reply being built. (That is, a single RR.) */
267 struct server_reply_item
{
268 struct server_reply_item
*next
; /* next item in sequence. */
269 char *name
; /* name part of the RR */
270 u16 type
; /* The RR type */
271 u16
class; /* The RR class (usually CLASS_INET) */
272 u32 ttl
; /* The RR TTL */
273 char is_name
; /* True iff data is a label */
274 u16 datalen
; /* Length of data; -1 if data is a label */
275 void *data
; /* The contents of the RR */
278 /* Represents a request that we've received as a DNS server, and holds */
279 /* the components of the reply as we're constructing it. */
280 struct server_request
{
281 /* Pointers to the next and previous entries on the list of replies */
282 /* that we're waiting to write. Only set if we have tried to respond */
283 /* and gotten EAGAIN. */
284 struct server_request
*next_pending
;
285 struct server_request
*prev_pending
;
287 u16 trans_id
; /* Transaction id. */
288 struct evdns_server_port
*port
; /* Which port received this request on? */
289 struct sockaddr_storage addr
; /* Where to send the response */
290 ev_socklen_t addrlen
; /* length of addr */
292 int n_answer
; /* how many answer RRs have been set? */
293 int n_authority
; /* how many authority RRs have been set? */
294 int n_additional
; /* how many additional RRs have been set? */
296 struct server_reply_item
*answer
; /* linked list of answer RRs */
297 struct server_reply_item
*authority
; /* linked list of authority RRs */
298 struct server_reply_item
*additional
; /* linked list of additional RRs */
300 /* Constructed response. Only set once we're ready to send a reply. */
301 /* Once this is set, the RR fields are cleared, and no more should be set. */
305 /* Caller-visible fields: flags, questions. */
306 struct evdns_server_request base
;
310 /* An array of n_req_heads circular lists for inflight requests.
311 * Each inflight request req is in req_heads[req->trans_id % n_req_heads].
313 struct request
**req_heads
;
314 /* A circular list of requests that we're waiting to send, but haven't
315 * sent yet because there are too many requests inflight */
316 struct request
*req_waiting_head
;
317 /* A circular list of nameservers. */
318 struct nameserver
*server_head
;
321 struct event_base
*event_base
;
323 /* The number of good nameservers that we have */
324 int global_good_nameservers
;
326 /* inflight requests are contained in the req_head list */
327 /* and are actually going out across the network */
328 int global_requests_inflight
;
329 /* requests which aren't inflight are in the waiting list */
330 /* and are counted here */
331 int global_requests_waiting
;
333 int global_max_requests_inflight
;
335 struct timeval global_timeout
; /* 5 seconds by default */
336 int global_max_reissues
; /* a reissue occurs when we get some errors from the server */
337 int global_max_retransmits
; /* number of times we'll retransmit a request which timed out */
338 /* number of timeouts in a row before we consider this server to be down */
339 int global_max_nameserver_timeout
;
340 /* true iff we will use the 0x20 hack to prevent poisoning attacks. */
341 int global_randomize_case
;
343 /* The first time that a nameserver fails, how long do we wait before
344 * probing to see if it has returned? */
345 struct timeval global_nameserver_probe_initial_timeout
;
347 /** Port to bind to for outgoing DNS packets. */
348 struct sockaddr_storage global_outgoing_address
;
349 /** ev_socklen_t for global_outgoing_address. 0 if it isn't set. */
350 ev_socklen_t global_outgoing_addrlen
;
352 struct timeval global_getaddrinfo_allow_skew
;
354 int getaddrinfo_ipv4_timeouts
;
355 int getaddrinfo_ipv6_timeouts
;
356 int getaddrinfo_ipv4_answered
;
357 int getaddrinfo_ipv6_answered
;
359 struct search_state
*global_search_state
;
361 TAILQ_HEAD(hosts_list
, hosts_entry
) hostsdb
;
363 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
369 TAILQ_ENTRY(hosts_entry
) next
;
372 struct sockaddr_in sin
;
373 struct sockaddr_in6 sin6
;
379 static struct evdns_base
*current_base
= NULL
;
382 evdns_get_global_base(void)
387 /* Given a pointer to an evdns_server_request, get the corresponding */
388 /* server_request. */
389 #define TO_SERVER_REQUEST(base_ptr) \
390 ((struct server_request*) \
391 (((char*)(base_ptr) - evutil_offsetof(struct server_request, base))))
393 #define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads])
395 static struct nameserver
*nameserver_pick(struct evdns_base
*base
);
396 static void evdns_request_insert(struct request
*req
, struct request
**head
);
397 static void evdns_request_remove(struct request
*req
, struct request
**head
);
398 static void nameserver_ready_callback(evutil_socket_t fd
, short events
, void *arg
);
399 static int evdns_transmit(struct evdns_base
*base
);
400 static int evdns_request_transmit(struct request
*req
);
401 static void nameserver_send_probe(struct nameserver
*const ns
);
402 static void search_request_finished(struct evdns_request
*const);
403 static int search_try_next(struct evdns_request
*const req
);
404 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
);
405 static void evdns_requests_pump_waiting_queue(struct evdns_base
*base
);
406 static u16
transaction_id_pick(struct evdns_base
*base
);
407 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
);
408 static void request_submit(struct request
*const req
);
410 static int server_request_free(struct server_request
*req
);
411 static void server_request_free_answers(struct server_request
*req
);
412 static void server_port_free(struct evdns_server_port
*port
);
413 static void server_port_ready_callback(evutil_socket_t fd
, short events
, void *arg
);
414 static int evdns_base_resolv_conf_parse_impl(struct evdns_base
*base
, int flags
, const char *const filename
);
415 static int evdns_base_set_option_impl(struct evdns_base
*base
,
416 const char *option
, const char *val
, int flags
);
417 static void evdns_base_free_and_unlock(struct evdns_base
*base
, int fail_requests
);
419 static int strtoint(const char *const str
);
421 #ifdef _EVENT_DISABLE_THREAD_SUPPORT
422 #define EVDNS_LOCK(base) _EVUTIL_NIL_STMT
423 #define EVDNS_UNLOCK(base) _EVUTIL_NIL_STMT
424 #define ASSERT_LOCKED(base) _EVUTIL_NIL_STMT
426 #define EVDNS_LOCK(base) \
427 EVLOCK_LOCK((base)->lock, 0)
428 #define EVDNS_UNLOCK(base) \
429 EVLOCK_UNLOCK((base)->lock, 0)
430 #define ASSERT_LOCKED(base) \
431 EVLOCK_ASSERT_LOCKED((base)->lock)
435 default_evdns_log_fn(int warning
, const char *buf
)
437 if (warning
== EVDNS_LOG_WARN
)
438 event_warnx("[evdns] %s", buf
);
439 else if (warning
== EVDNS_LOG_MSG
)
440 event_msgx("[evdns] %s", buf
);
442 event_debug(("[evdns] %s", buf
));
445 static evdns_debug_log_fn_type evdns_log_fn
= NULL
;
448 evdns_set_log_fn(evdns_debug_log_fn_type fn
)
454 #define EVDNS_LOG_CHECK __attribute__ ((format(printf, 2, 3)))
456 #define EVDNS_LOG_CHECK
459 static void _evdns_log(int warn
, const char *fmt
, ...) EVDNS_LOG_CHECK
;
461 _evdns_log(int warn
, const char *fmt
, ...)
468 evutil_vsnprintf(buf
, sizeof(buf
), fmt
, args
);
471 if (warn
== EVDNS_LOG_MSG
)
472 warn
= EVDNS_LOG_WARN
;
473 evdns_log_fn(warn
, buf
);
475 default_evdns_log_fn(warn
, buf
);
480 #define log _evdns_log
482 /* This walks the list of inflight requests to find the */
483 /* one with a matching transaction id. Returns NULL on */
485 static struct request
*
486 request_find_from_trans_id(struct evdns_base
*base
, u16 trans_id
) {
487 struct request
*req
= REQ_HEAD(base
, trans_id
);
488 struct request
*const started_at
= req
;
494 if (req
->trans_id
== trans_id
) return req
;
496 } while (req
!= started_at
);
502 /* a libevent callback function which is called when a nameserver */
503 /* has gone down and we want to test if it has came back to life yet */
505 nameserver_prod_callback(evutil_socket_t fd
, short events
, void *arg
) {
506 struct nameserver
*const ns
= (struct nameserver
*) arg
;
510 EVDNS_LOCK(ns
->base
);
511 nameserver_send_probe(ns
);
512 EVDNS_UNLOCK(ns
->base
);
515 /* a libevent callback which is called when a nameserver probe (to see if */
516 /* it has come back to life) times out. We increment the count of failed_times */
517 /* and wait longer to send the next probe packet. */
519 nameserver_probe_failed(struct nameserver
*const ns
) {
520 struct timeval timeout
;
523 ASSERT_LOCKED(ns
->base
);
524 (void) evtimer_del(&ns
->timeout_event
);
525 if (ns
->state
== 1) {
526 /* This can happen if the nameserver acts in a way which makes us mark */
527 /* it as bad and then starts sending good replies. */
531 #define MAX_PROBE_TIMEOUT 3600
532 #define TIMEOUT_BACKOFF_FACTOR 3
534 memcpy(&timeout
, &ns
->base
->global_nameserver_probe_initial_timeout
,
535 sizeof(struct timeval
));
536 for (i
=ns
->failed_times
; i
> 0 && timeout
.tv_sec
< MAX_PROBE_TIMEOUT
; --i
) {
537 timeout
.tv_sec
*= TIMEOUT_BACKOFF_FACTOR
;
538 timeout
.tv_usec
*= TIMEOUT_BACKOFF_FACTOR
;
539 if (timeout
.tv_usec
> 1000000) {
540 timeout
.tv_sec
+= timeout
.tv_usec
/ 1000000;
541 timeout
.tv_usec
%= 1000000;
544 if (timeout
.tv_sec
> MAX_PROBE_TIMEOUT
) {
545 timeout
.tv_sec
= MAX_PROBE_TIMEOUT
;
551 if (evtimer_add(&ns
->timeout_event
, &timeout
) < 0) {
554 "Error from libevent when adding timer event for %s",
555 evutil_format_sockaddr_port(
556 (struct sockaddr
*)&ns
->address
,
557 addrbuf
, sizeof(addrbuf
)));
561 /* called when a nameserver has been deemed to have failed. For example, too */
562 /* many packets have timed out etc */
564 nameserver_failed(struct nameserver
*const ns
, const char *msg
) {
565 struct request
*req
, *started_at
;
566 struct evdns_base
*base
= ns
->base
;
571 /* if this nameserver has already been marked as failed */
572 /* then don't do anything */
573 if (!ns
->state
) return;
575 log(EVDNS_LOG_MSG
, "Nameserver %s has failed: %s",
576 evutil_format_sockaddr_port(
577 (struct sockaddr
*)&ns
->address
,
578 addrbuf
, sizeof(addrbuf
)),
581 base
->global_good_nameservers
--;
582 EVUTIL_ASSERT(base
->global_good_nameservers
>= 0);
583 if (base
->global_good_nameservers
== 0) {
584 log(EVDNS_LOG_MSG
, "All nameservers have failed");
588 ns
->failed_times
= 1;
590 if (evtimer_add(&ns
->timeout_event
,
591 &base
->global_nameserver_probe_initial_timeout
) < 0) {
593 "Error from libevent when adding timer event for %s",
594 evutil_format_sockaddr_port(
595 (struct sockaddr
*)&ns
->address
,
596 addrbuf
, sizeof(addrbuf
)));
600 /* walk the list of inflight requests to see if any can be reassigned to */
601 /* a different server. Requests in the waiting queue don't have a */
602 /* nameserver assigned yet */
604 /* if we don't have *any* good nameservers then there's no point */
605 /* trying to reassign requests to one */
606 if (!base
->global_good_nameservers
) return;
608 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
609 req
= started_at
= base
->req_heads
[i
];
612 if (req
->tx_count
== 0 && req
->ns
== ns
) {
613 /* still waiting to go out, can be moved */
614 /* to another server */
615 req
->ns
= nameserver_pick(base
);
618 } while (req
!= started_at
);
624 nameserver_up(struct nameserver
*const ns
)
627 ASSERT_LOCKED(ns
->base
);
628 if (ns
->state
) return;
629 log(EVDNS_LOG_MSG
, "Nameserver %s is back up",
630 evutil_format_sockaddr_port(
631 (struct sockaddr
*)&ns
->address
,
632 addrbuf
, sizeof(addrbuf
)));
633 evtimer_del(&ns
->timeout_event
);
634 if (ns
->probe_request
) {
635 evdns_cancel_request(ns
->base
, ns
->probe_request
);
636 ns
->probe_request
= NULL
;
639 ns
->failed_times
= 0;
641 ns
->base
->global_good_nameservers
++;
645 request_trans_id_set(struct request
*const req
, const u16 trans_id
) {
646 req
->trans_id
= trans_id
;
647 *((u16
*) req
->request
) = htons(trans_id
);
650 /* Called to remove a request from a list and dealloc it. */
651 /* head is a pointer to the head of the list it should be */
652 /* removed from or NULL if the request isn't in a list. */
653 /* when free_handle is one, free the handle as well. */
655 request_finished(struct request
*const req
, struct request
**head
, int free_handle
) {
656 struct evdns_base
*base
= req
->base
;
657 int was_inflight
= (head
!= &base
->req_waiting_head
);
659 ASSERT_VALID_REQUEST(req
);
662 evdns_request_remove(req
, head
);
664 log(EVDNS_LOG_DEBUG
, "Removing timeout for request %p", req
);
666 evtimer_del(&req
->timeout_event
);
667 base
->global_requests_inflight
--;
669 base
->global_requests_waiting
--;
671 /* it was initialized during request_new / evtimer_assign */
672 event_debug_unassign(&req
->timeout_event
);
674 if (!req
->request_appended
) {
675 /* need to free the request data on it's own */
676 mm_free(req
->request
);
678 /* the request data is appended onto the header */
679 /* so everything gets free()ed when we: */
683 EVUTIL_ASSERT(req
->handle
->current_req
== req
);
686 search_request_finished(req
->handle
);
687 req
->handle
->current_req
= NULL
;
688 if (! req
->handle
->pending_cb
) {
689 /* If we're planning to run the callback,
690 * don't free the handle until later. */
691 mm_free(req
->handle
);
693 req
->handle
= NULL
; /* If we have a bug, let's crash
696 req
->handle
->current_req
= NULL
;
702 evdns_requests_pump_waiting_queue(base
);
706 /* This is called when a server returns a funny error code. */
707 /* We try the request again with another server. */
711 /* 1 failed/reissue is pointless */
713 request_reissue(struct request
*req
) {
714 const struct nameserver
*const last_ns
= req
->ns
;
715 ASSERT_LOCKED(req
->base
);
716 ASSERT_VALID_REQUEST(req
);
717 /* the last nameserver should have been marked as failing */
718 /* by the caller of this function, therefore pick will try */
719 /* not to return it */
720 req
->ns
= nameserver_pick(req
->base
);
721 if (req
->ns
== last_ns
) {
722 /* ... but pick did return it */
723 /* not a lot of point in trying again with the */
728 req
->reissue_count
++;
730 req
->transmit_me
= 1;
735 /* this function looks for space on the inflight queue and promotes */
736 /* requests from the waiting queue if it can. */
739 /* add return code, see at nameserver_pick() and other functions. */
741 evdns_requests_pump_waiting_queue(struct evdns_base
*base
) {
743 while (base
->global_requests_inflight
< base
->global_max_requests_inflight
&&
744 base
->global_requests_waiting
) {
747 EVUTIL_ASSERT(base
->req_waiting_head
);
748 req
= base
->req_waiting_head
;
750 req
->ns
= nameserver_pick(base
);
754 /* move a request from the waiting queue to the inflight queue */
755 evdns_request_remove(req
, &base
->req_waiting_head
);
757 base
->global_requests_waiting
--;
758 base
->global_requests_inflight
++;
760 request_trans_id_set(req
, transaction_id_pick(base
));
762 evdns_request_insert(req
, &REQ_HEAD(base
, req
->trans_id
));
763 evdns_request_transmit(req
);
764 evdns_transmit(base
);
768 /* TODO(nickm) document */
769 struct deferred_reply_callback
{
770 struct deferred_cb deferred
;
771 struct evdns_request
*handle
;
776 evdns_callback_type user_callback
;
781 reply_run_callback(struct deferred_cb
*d
, void *user_pointer
)
783 struct deferred_reply_callback
*cb
=
784 EVUTIL_UPCAST(d
, struct deferred_reply_callback
, deferred
);
786 switch (cb
->request_type
) {
789 cb
->user_callback(DNS_ERR_NONE
, DNS_IPv4_A
,
790 cb
->reply
.data
.a
.addrcount
, cb
->ttl
,
791 cb
->reply
.data
.a
.addresses
,
794 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
797 if (cb
->have_reply
) {
798 char *name
= cb
->reply
.data
.ptr
.name
;
799 cb
->user_callback(DNS_ERR_NONE
, DNS_PTR
, 1, cb
->ttl
,
800 &name
, user_pointer
);
802 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
807 cb
->user_callback(DNS_ERR_NONE
, DNS_IPv6_AAAA
,
808 cb
->reply
.data
.aaaa
.addrcount
, cb
->ttl
,
809 cb
->reply
.data
.aaaa
.addresses
,
812 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
816 cb
->user_callback(DNS_ERR_NONE
, DNS_TXT
,
817 cb
->reply
.data
.txt
.recordscount
, cb
->ttl
,
818 cb
->reply
.data
.txt
.records
,
821 cb
->user_callback(cb
->err
, 0, 0, cb
->ttl
, NULL
, user_pointer
);
827 if (cb
->handle
&& cb
->handle
->pending_cb
) {
835 reply_schedule_callback(struct request
*const req
, u32 ttl
, u32 err
, struct reply
*reply
)
837 struct deferred_reply_callback
*d
= mm_calloc(1, sizeof(*d
));
840 event_warn("%s: Couldn't allocate space for deferred callback.",
845 ASSERT_LOCKED(req
->base
);
847 d
->request_type
= req
->request_type
;
848 d
->user_callback
= req
->user_callback
;
853 memcpy(&d
->reply
, reply
, sizeof(struct reply
));
857 req
->handle
->pending_cb
= 1;
858 d
->handle
= req
->handle
;
861 event_deferred_cb_init(&d
->deferred
, reply_run_callback
,
863 event_deferred_cb_schedule(
864 event_base_get_deferred_cb_queue(req
->base
->event_base
),
868 /* this processes a parsed reply packet */
870 reply_handle(struct request
*const req
, u16 flags
, u32 ttl
, struct reply
*reply
) {
873 static const int error_codes
[] = {
874 DNS_ERR_FORMAT
, DNS_ERR_SERVERFAILED
, DNS_ERR_NOTEXIST
,
875 DNS_ERR_NOTIMPL
, DNS_ERR_REFUSED
878 ASSERT_LOCKED(req
->base
);
879 ASSERT_VALID_REQUEST(req
);
881 if (flags
& 0x020f || !reply
|| !reply
->have_answer
) {
882 /* there was an error */
883 if (flags
& 0x0200) {
884 error
= DNS_ERR_TRUNCATED
;
885 } else if (flags
& 0x000f) {
886 u16 error_code
= (flags
& 0x000f) - 1;
887 if (error_code
> 4) {
888 error
= DNS_ERR_UNKNOWN
;
890 error
= error_codes
[error_code
];
892 } else if (reply
&& !reply
->have_answer
) {
893 error
= DNS_ERR_NODATA
;
895 error
= DNS_ERR_UNKNOWN
;
899 case DNS_ERR_NOTIMPL
:
900 case DNS_ERR_REFUSED
:
901 /* we regard these errors as marking a bad nameserver */
902 if (req
->reissue_count
< req
->base
->global_max_reissues
) {
904 evutil_snprintf(msg
, sizeof(msg
), "Bad response %d (%s)",
905 error
, evdns_err_to_string(error
));
906 nameserver_failed(req
->ns
, msg
);
907 if (!request_reissue(req
)) return;
910 case DNS_ERR_SERVERFAILED
:
911 /* rcode 2 (servfailed) sometimes means "we
912 * are broken" and sometimes (with some binds)
913 * means "that request was very confusing."
914 * Treat this as a timeout, not a failure.
916 log(EVDNS_LOG_DEBUG
, "Got a SERVERFAILED from nameserver"
917 "at %s; will allow the request to time out.",
918 evutil_format_sockaddr_port(
919 (struct sockaddr
*)&req
->ns
->address
,
920 addrbuf
, sizeof(addrbuf
)));
923 /* we got a good reply from the nameserver: it is up. */
924 if (req
->handle
== req
->ns
->probe_request
) {
925 /* Avoid double-free */
926 req
->ns
->probe_request
= NULL
;
929 nameserver_up(req
->ns
);
932 if (req
->handle
->search_state
&&
933 req
->request_type
!= TYPE_PTR
) {
934 /* if we have a list of domains to search in,
935 * try the next one */
936 if (!search_try_next(req
->handle
)) {
937 /* a new request was issued so this
938 * request is finished and */
939 /* the user callback will be made when
940 * that request (or a */
941 /* child of it) finishes. */
946 /* all else failed. Pass the failure up */
947 reply_schedule_callback(req
, ttl
, error
, NULL
);
948 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 1);
950 /* all ok, tell the user */
951 reply_schedule_callback(req
, ttl
, 0, reply
);
952 if (req
->handle
== req
->ns
->probe_request
)
953 req
->ns
->probe_request
= NULL
; /* Avoid double-free */
954 nameserver_up(req
->ns
);
955 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 1);
960 name_parse(u8
*packet
, int length
, int *idx
, char *name_out
, int name_out_len
) {
964 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while (0)
965 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while (0)
966 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
969 const char *const end
= name_out
+ name_out_len
;
971 /* Normally, names are a series of length prefixed strings terminated */
972 /* with a length of 0 (the lengths are u8's < 63). */
973 /* However, the length can start with a pair of 1 bits and that */
974 /* means that the next 14 bits are a pointer within the current */
979 if (j
>= length
) return -1;
981 if (!label_len
) break;
982 if (label_len
& 0xc0) {
985 if (name_end
< 0) name_end
= j
;
986 j
= (((int)label_len
& 0x3f) << 8) + ptr_low
;
987 /* Make sure that the target offset is in-bounds. */
988 if (j
< 0 || j
>= length
) return -1;
989 /* If we've jumped more times than there are characters in the
990 * message, we must have a loop. */
991 if (++ptr_count
> length
) return -1;
994 if (label_len
> 63) return -1;
995 if (cp
!= name_out
) {
996 if (cp
+ 1 >= end
) return -1;
999 if (cp
+ label_len
>= end
) return -1;
1000 memcpy(cp
, packet
+ j
, label_len
);
1004 if (cp
>= end
) return -1;
1015 /* parses a raw request from a nameserver */
1017 reply_parse(struct evdns_base
*base
, u8
*packet
, int length
) {
1018 int j
= 0, k
= 0; /* index into packet */
1019 u16 _t
; /* used by the macros */
1020 u32 _t32
; /* used by the macros */
1021 char tmp_name
[256], cmp_name
[256]; /* used by the macros */
1022 int name_matches
= 0;
1024 u16 trans_id
, questions
, answers
, authority
, additional
, datalength
;
1026 u32 ttl
, ttl_r
= 0xffffffff;
1028 struct request
*req
= NULL
;
1031 ASSERT_LOCKED(base
);
1039 (void) authority
; /* suppress "unused variable" warnings. */
1040 (void) additional
; /* suppress "unused variable" warnings. */
1042 req
= request_find_from_trans_id(base
, trans_id
);
1043 if (!req
) return -1;
1044 EVUTIL_ASSERT(req
->base
== base
);
1046 memset(&reply
, 0, sizeof(reply
));
1048 /* If it's not an answer, it doesn't correspond to any request. */
1049 if (!(flags
& 0x8000)) return -1; /* must be an answer */
1050 if ((flags
& 0x020f) && (flags
& 0x020f) != DNS_ERR_NOTEXIST
) {
1051 /* there was an error and it's not NXDOMAIN */
1054 /* if (!answers) return; */ /* must have an answer of some form */
1056 /* This macro skips a name in the DNS reply. */
1058 do { tmp_name[0] = '\0'; \
1059 if (name_parse(packet, length, &j, tmp_name, \
1060 sizeof(tmp_name))<0) \
1064 do { tmp_name[0] = '\0'; \
1065 cmp_name[0] = '\0'; \
1067 if (name_parse(packet, length, &j, tmp_name, \
1068 sizeof(tmp_name))<0) \
1070 if (name_parse(req->request, req->request_len, &k, \
1071 cmp_name, sizeof(cmp_name))<0) \
1073 if (base->global_randomize_case) { \
1074 if (strcmp(tmp_name, cmp_name) == 0) \
1077 if (evutil_ascii_strcasecmp(tmp_name, cmp_name) == 0) \
1082 reply
.type
= req
->request_type
;
1084 /* skip over each question in the reply */
1085 for (i
= 0; i
< questions
; ++i
) {
1086 /* the question looks like
1087 * <label:name><u16:type><u16:class>
1091 if (j
> length
) goto err
;
1097 /* now we have the answer section which looks like
1098 * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
1101 for (i
= 0; i
< answers
; ++i
) {
1110 EVUTIL_ASSERT(INT_MAX
> 0xFFFF + 0xFFFF);
1111 if (datalength
<= 0U || j
+ datalength
> length
)
1113 if (type
== TYPE_A
&& class == CLASS_INET
) {
1114 int addrcount
, addrtocopy
;
1115 if (req
->request_type
!= TYPE_A
) {
1116 j
+= datalength
; continue;
1118 if ((datalength
& 3) != 0) /* not an even number of As. */
1120 addrcount
= datalength
>> 2;
1121 addrtocopy
= MIN(MAX_V4_ADDRS
- reply
.data
.a
.addrcount
, (unsigned)addrcount
);
1123 ttl_r
= MIN(ttl_r
, ttl
);
1124 /* we only bother with the first four addresses. */
1125 if (j
+ 4*addrtocopy
> length
) goto err
;
1126 memcpy(&reply
.data
.a
.addresses
[reply
.data
.a
.addrcount
],
1127 packet
+ j
, 4*addrtocopy
);
1129 reply
.data
.a
.addrcount
+= addrtocopy
;
1130 reply
.have_answer
= 1;
1131 if (reply
.data
.a
.addrcount
== MAX_V4_ADDRS
) break;
1132 } else if (type
== TYPE_PTR
&& class == CLASS_INET
) {
1133 if (req
->request_type
!= TYPE_PTR
) {
1134 j
+= datalength
; continue;
1136 if (name_parse(packet
, length
, &j
, reply
.data
.ptr
.name
,
1137 sizeof(reply
.data
.ptr
.name
))<0)
1139 ttl_r
= MIN(ttl_r
, ttl
);
1140 reply
.have_answer
= 1;
1142 } else if (type
== TYPE_CNAME
) {
1143 char cname
[HOST_NAME_MAX
];
1144 if (!req
->put_cname_in_ptr
|| *req
->put_cname_in_ptr
) {
1145 j
+= datalength
; continue;
1147 if (name_parse(packet
, length
, &j
, cname
,
1150 *req
->put_cname_in_ptr
= mm_strdup(cname
);
1151 } else if (type
== TYPE_AAAA
&& class == CLASS_INET
) {
1152 int addrcount
, addrtocopy
;
1153 if (req
->request_type
!= TYPE_AAAA
) {
1154 j
+= datalength
; continue;
1156 if ((datalength
& 15) != 0) /* not an even number of AAAAs. */
1158 addrcount
= datalength
>> 4; /* each address is 16 bytes long */
1159 addrtocopy
= MIN(MAX_V6_ADDRS
- reply
.data
.aaaa
.addrcount
, (unsigned)addrcount
);
1160 ttl_r
= MIN(ttl_r
, ttl
);
1162 /* we only bother with the first four addresses. */
1163 if (j
+ 16*addrtocopy
> length
) goto err
;
1164 memcpy(&reply
.data
.aaaa
.addresses
[reply
.data
.aaaa
.addrcount
],
1165 packet
+ j
, 16*addrtocopy
);
1166 reply
.data
.aaaa
.addrcount
+= addrtocopy
;
1168 reply
.have_answer
= 1;
1169 if (reply
.data
.aaaa
.addrcount
== MAX_V6_ADDRS
) break;
1170 } else if (type
== TYPE_TXT
) {
1171 if (req
->request_type
!= TYPE_TXT
) {
1172 j
+= datalength
; continue;
1174 size_t mergedlen
= 0U;
1175 size_t mergenum
= 0U;
1176 while (mergedlen
+ mergenum
< datalength
) {
1178 txtlen
= packet
[j
+ mergedlen
+ mergenum
];
1179 if (mergedlen
+ txtlen
> sizeof reply
.data
.txt
.records
[0].txt
||
1180 datalength
<= mergedlen
+ txtlen
|| j
+ mergedlen
+ txtlen
>= length
) {
1183 memcpy(reply
.data
.txt
.records
[reply
.data
.txt
.recordscount
].txt
+ mergedlen
,
1184 &packet
[j
+ mergedlen
+ mergenum
+ 1], txtlen
);
1185 mergedlen
+= txtlen
;
1188 reply
.data
.txt
.records
[reply
.data
.txt
.recordscount
].len
= mergedlen
;
1189 reply
.data
.txt
.recordscount
++;
1190 ttl_r
= MIN(ttl_r
, ttl
);
1191 reply
.have_answer
= 1;
1193 if (reply
.data
.txt
.recordscount
>= MAX_TXT_RRS
) {
1197 /* skip over any other type of resource */
1202 if (!reply
.have_answer
) {
1203 for (i
= 0; i
< authority
; ++i
) {
1210 if (type
== TYPE_SOA
&& class == CLASS_INET
) {
1211 u32 serial
, refresh
, retry
, expire
, minimum
;
1223 ttl_r
= MIN(ttl_r
, ttl
);
1224 ttl_r
= MIN(ttl_r
, minimum
);
1226 /* skip over any other type of resource */
1232 if (ttl_r
== 0xffffffff)
1235 reply_handle(req
, flags
, ttl_r
, &reply
);
1239 reply_handle(req
, flags
, 0, NULL
);
1243 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
1244 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
1247 request_parse(u8
*packet
, int length
, struct evdns_server_port
*port
, struct sockaddr
*addr
, ev_socklen_t addrlen
)
1249 int j
= 0; /* index into packet */
1250 u16 _t
; /* used by the macros */
1251 char tmp_name
[256]; /* used by the macros */
1254 u16 trans_id
, flags
, questions
, answers
, authority
, additional
;
1255 struct server_request
*server_req
= NULL
;
1257 ASSERT_LOCKED(port
);
1259 /* Get the header fields */
1270 if (flags
& 0x8000) return -1; /* Must not be an answer. */
1271 flags
&= 0x0110; /* Only RD and CD get preserved. */
1273 server_req
= mm_malloc(sizeof(struct server_request
));
1274 if (server_req
== NULL
) return -1;
1275 memset(server_req
, 0, sizeof(struct server_request
));
1277 server_req
->trans_id
= trans_id
;
1278 memcpy(&server_req
->addr
, addr
, addrlen
);
1279 server_req
->addrlen
= addrlen
;
1281 server_req
->base
.flags
= flags
;
1282 server_req
->base
.nquestions
= 0;
1283 server_req
->base
.questions
= mm_calloc(sizeof(struct evdns_server_question
*), questions
);
1284 if (server_req
->base
.questions
== NULL
)
1287 for (i
= 0; i
< questions
; ++i
) {
1289 struct evdns_server_question
*q
;
1291 if (name_parse(packet
, length
, &j
, tmp_name
, sizeof(tmp_name
))<0)
1295 namelen
= (int)strlen(tmp_name
);
1296 q
= mm_malloc(sizeof(struct evdns_server_question
) + namelen
);
1300 q
->dns_question_class
= class;
1301 memcpy(q
->name
, tmp_name
, namelen
+1);
1302 server_req
->base
.questions
[server_req
->base
.nquestions
++] = q
;
1305 /* Ignore answers, authority, and additional. */
1307 server_req
->port
= port
;
1310 /* Only standard queries are supported. */
1311 if (flags
& 0x7800) {
1312 evdns_server_request_respond(&(server_req
->base
), DNS_ERR_NOTIMPL
);
1316 port
->user_callback(&(server_req
->base
), port
->user_data
);
1321 if (server_req
->base
.questions
) {
1322 for (i
= 0; i
< server_req
->base
.nquestions
; ++i
)
1323 mm_free(server_req
->base
.questions
[i
]);
1324 mm_free(server_req
->base
.questions
);
1326 mm_free(server_req
);
1336 static int (*rand_init_function
)(void) = evutil_secure_rng_init
;
1338 static void (*rand_bytes_function
)(void *buf
, size_t n
) =
1339 evutil_secure_rng_get_bytes
;
1342 evdns_set_transaction_id_fn(ev_uint16_t (*fn
)(void))
1347 evdns_set_random_init_fn(int (*fn
)(void))
1349 rand_init_function
= fn
;
1353 evdns_set_random_bytes_fn(void (*fn
)(void *, size_t))
1355 rand_bytes_function
= fn
;
1358 /* Try to choose a strong transaction id which isn't already in flight */
1360 transaction_id_pick(struct evdns_base
*base
) {
1361 ASSERT_LOCKED(base
);
1364 rand_bytes_function(&trans_id
, sizeof(trans_id
));
1366 if (trans_id
== 0xffff) continue;
1367 /* now check to see if that id is already inflight */
1368 if (request_find_from_trans_id(base
, trans_id
) == NULL
)
1373 /* choose a namesever to use. This function will try to ignore */
1374 /* nameservers which we think are down and load balance across the rest */
1375 /* by updating the server_head global each time. */
1376 static struct nameserver
*
1377 nameserver_pick(struct evdns_base
*base
) {
1378 struct nameserver
*started_at
= base
->server_head
, *picked
;
1379 ASSERT_LOCKED(base
);
1380 if (!base
->server_head
) return NULL
;
1382 /* if we don't have any good nameservers then there's no */
1383 /* point in trying to find one. */
1384 if (!base
->global_good_nameservers
) {
1385 base
->server_head
= base
->server_head
->next
;
1386 return base
->server_head
;
1389 /* remember that nameservers are in a circular list */
1391 if (base
->server_head
->state
) {
1392 /* we think this server is currently good */
1393 picked
= base
->server_head
;
1394 base
->server_head
= base
->server_head
->next
;
1398 base
->server_head
= base
->server_head
->next
;
1399 if (base
->server_head
== started_at
) {
1400 /* all the nameservers seem to be down */
1401 /* so we just return this one and hope for the */
1403 EVUTIL_ASSERT(base
->global_good_nameservers
== 0);
1404 picked
= base
->server_head
;
1405 base
->server_head
= base
->server_head
->next
;
1411 /* this is called when a namesever socket is ready for reading */
1413 nameserver_read(struct nameserver
*ns
) {
1414 struct sockaddr_storage ss
;
1415 ev_socklen_t addrlen
= sizeof(ss
);
1418 ASSERT_LOCKED(ns
->base
);
1421 const int r
= recvfrom(ns
->socket
, (void*)packet
,
1423 (struct sockaddr
*)&ss
, &addrlen
);
1425 int err
= evutil_socket_geterror(ns
->socket
);
1426 if (EVUTIL_ERR_RW_RETRIABLE(err
))
1428 nameserver_failed(ns
,
1429 evutil_socket_error_to_string(err
));
1432 if (evutil_sockaddr_cmp((struct sockaddr
*)&ss
,
1433 (struct sockaddr
*)&ns
->address
, 0)) {
1434 log(EVDNS_LOG_WARN
, "Address mismatch on received "
1435 "DNS packet. Apparent source was %s",
1436 evutil_format_sockaddr_port(
1437 (struct sockaddr
*)&ss
,
1438 addrbuf
, sizeof(addrbuf
)));
1443 reply_parse(ns
->base
, packet
, r
);
1447 /* Read a packet from a DNS client on a server port s, parse it, and */
1448 /* act accordingly. */
1450 server_port_read(struct evdns_server_port
*s
) {
1452 struct sockaddr_storage addr
;
1453 ev_socklen_t addrlen
;
1458 addrlen
= sizeof(struct sockaddr_storage
);
1459 r
= recvfrom(s
->socket
, (void*)packet
, sizeof(packet
), 0,
1460 (struct sockaddr
*) &addr
, &addrlen
);
1462 int err
= evutil_socket_geterror(s
->socket
);
1463 if (EVUTIL_ERR_RW_RETRIABLE(err
))
1466 "Error %s (%d) while reading request.",
1467 evutil_socket_error_to_string(err
), err
);
1470 request_parse(packet
, r
, s
, (struct sockaddr
*) &addr
, addrlen
);
1474 /* Try to write all pending replies on a given DNS server port. */
1476 server_port_flush(struct evdns_server_port
*port
)
1478 struct server_request
*req
= port
->pending_replies
;
1479 ASSERT_LOCKED(port
);
1481 int r
= sendto(port
->socket
, req
->response
, (int)req
->response_len
, 0,
1482 (struct sockaddr
*) &req
->addr
, (ev_socklen_t
)req
->addrlen
);
1484 int err
= evutil_socket_geterror(port
->socket
);
1485 if (EVUTIL_ERR_RW_RETRIABLE(err
))
1487 log(EVDNS_LOG_WARN
, "Error %s (%d) while writing response to port; dropping", evutil_socket_error_to_string(err
), err
);
1489 if (server_request_free(req
)) {
1490 /* we released the last reference to req->port. */
1493 EVUTIL_ASSERT(req
!= port
->pending_replies
);
1494 req
= port
->pending_replies
;
1498 /* We have no more pending requests; stop listening for 'writeable' events. */
1499 (void) event_del(&port
->event
);
1500 event_assign(&port
->event
, port
->event_base
,
1501 port
->socket
, EV_READ
| EV_PERSIST
,
1502 server_port_ready_callback
, port
);
1504 if (event_add(&port
->event
, NULL
) < 0) {
1505 log(EVDNS_LOG_WARN
, "Error from libevent when adding event for DNS server.");
1510 /* set if we are waiting for the ability to write to this server. */
1511 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1512 /* we stop these events. */
1514 nameserver_write_waiting(struct nameserver
*ns
, char waiting
) {
1515 ASSERT_LOCKED(ns
->base
);
1516 if (ns
->write_waiting
== waiting
) return;
1518 ns
->write_waiting
= waiting
;
1519 (void) event_del(&ns
->event
);
1520 event_assign(&ns
->event
, ns
->base
->event_base
,
1521 ns
->socket
, EV_READ
| (waiting
? EV_WRITE
: 0) | EV_PERSIST
,
1522 nameserver_ready_callback
, ns
);
1523 if (event_add(&ns
->event
, NULL
) < 0) {
1525 log(EVDNS_LOG_WARN
, "Error from libevent when adding event for %s",
1526 evutil_format_sockaddr_port(
1527 (struct sockaddr
*)&ns
->address
,
1528 addrbuf
, sizeof(addrbuf
)));
1533 /* a callback function. Called by libevent when the kernel says that */
1534 /* a nameserver socket is ready for writing or reading */
1536 nameserver_ready_callback(evutil_socket_t fd
, short events
, void *arg
) {
1537 struct nameserver
*ns
= (struct nameserver
*) arg
;
1540 EVDNS_LOCK(ns
->base
);
1541 if (events
& EV_WRITE
) {
1543 if (!evdns_transmit(ns
->base
)) {
1544 nameserver_write_waiting(ns
, 0);
1547 if (events
& EV_READ
) {
1548 nameserver_read(ns
);
1550 EVDNS_UNLOCK(ns
->base
);
1553 /* a callback function. Called by libevent when the kernel says that */
1554 /* a server socket is ready for writing or reading. */
1556 server_port_ready_callback(evutil_socket_t fd
, short events
, void *arg
) {
1557 struct evdns_server_port
*port
= (struct evdns_server_port
*) arg
;
1561 if (events
& EV_WRITE
) {
1563 server_port_flush(port
);
1565 if (events
& EV_READ
) {
1566 server_port_read(port
);
1571 /* This is an inefficient representation; only use it via the dnslabel_table_*
1572 * functions, so that is can be safely replaced with something smarter later. */
1573 #define MAX_LABELS 128
1574 /* Structures used to implement name compression */
1575 struct dnslabel_entry
{ char *v
; off_t pos
; };
1576 struct dnslabel_table
{
1577 int n_labels
; /* number of current entries */
1578 /* map from name to position in message */
1579 struct dnslabel_entry labels
[MAX_LABELS
];
1582 /* Initialize dnslabel_table. */
1584 dnslabel_table_init(struct dnslabel_table
*table
)
1586 table
->n_labels
= 0;
1589 /* Free all storage held by table, but not the table itself. */
1591 dnslabel_clear(struct dnslabel_table
*table
)
1594 for (i
= 0; i
< table
->n_labels
; ++i
)
1595 mm_free(table
->labels
[i
].v
);
1596 table
->n_labels
= 0;
1599 /* return the position of the label in the current message, or -1 if the label */
1600 /* hasn't been used yet. */
1602 dnslabel_table_get_pos(const struct dnslabel_table
*table
, const char *label
)
1605 for (i
= 0; i
< table
->n_labels
; ++i
) {
1606 if (!strcmp(label
, table
->labels
[i
].v
))
1607 return table
->labels
[i
].pos
;
1612 /* remember that we've used the label at position pos */
1614 dnslabel_table_add(struct dnslabel_table
*table
, const char *label
, off_t pos
)
1618 if (table
->n_labels
== MAX_LABELS
)
1620 v
= mm_strdup(label
);
1623 p
= table
->n_labels
++;
1624 table
->labels
[p
].v
= v
;
1625 table
->labels
[p
].pos
= pos
;
1630 /* Converts a string to a length-prefixed set of DNS labels, starting */
1631 /* at buf[j]. name and buf must not overlap. name_len should be the length */
1632 /* of name. table is optional, and is used for compression. */
1634 /* Input: abc.def */
1635 /* Output: <3>abc<3>def<0> */
1637 /* Returns the first index after the encoded name, or negative on error. */
1638 /* -1 label was > 63 bytes */
1639 /* -2 name too long to fit in buffer. */
1642 dnsname_to_labels(u8
*const buf
, size_t buf_len
, off_t j
,
1643 const char *name
, const size_t name_len
,
1644 struct dnslabel_table
*table
) {
1645 const char *end
= name
+ name_len
;
1649 #define APPEND16(x) do { \
1650 if (j + 2 > (off_t)buf_len) \
1653 memcpy(buf + j, &_t, 2); \
1656 #define APPEND32(x) do { \
1657 if (j + 4 > (off_t)buf_len) \
1660 memcpy(buf + j, &_t32, 4); \
1664 if (name_len
> 255) return -2;
1667 const char *const start
= name
;
1668 if (table
&& (ref
= dnslabel_table_get_pos(table
, name
)) >= 0) {
1669 APPEND16(ref
| 0xc000);
1672 name
= strchr(name
, '.');
1674 const size_t label_len
= end
- start
;
1675 if (label_len
> 63) return -1;
1676 if ((size_t)(j
+label_len
+1) > buf_len
) return -2;
1677 if (table
) dnslabel_table_add(table
, start
, j
);
1678 buf
[j
++] = (ev_uint8_t
)label_len
;
1680 memcpy(buf
+ j
, start
, label_len
);
1681 j
+= (int) label_len
;
1684 /* append length of the label. */
1685 const size_t label_len
= name
- start
;
1686 if (label_len
> 63) return -1;
1687 if ((size_t)(j
+label_len
+1) > buf_len
) return -2;
1688 if (table
) dnslabel_table_add(table
, start
, j
);
1689 buf
[j
++] = (ev_uint8_t
)label_len
;
1691 memcpy(buf
+ j
, start
, label_len
);
1692 j
+= (int) label_len
;
1693 /* hop over the '.' */
1698 /* the labels must be terminated by a 0. */
1699 /* It's possible that the name ended in a . */
1700 /* in which case the zero is already there */
1701 if (!j
|| buf
[j
-1]) buf
[j
++] = 0;
1707 /* Finds the length of a dns request for a DNS name of the given */
1708 /* length. The actual request may be smaller than the value returned */
1711 evdns_request_len(const size_t name_len
) {
1712 return 96 + /* length of the DNS standard header */
1714 4; /* space for the resource type */
1717 /* build a dns request packet into buf. buf should be at least as long */
1718 /* as evdns_request_len told you it should be. */
1720 /* Returns the amount of space used. Negative on error. */
1722 evdns_request_data_build(const char *const name
, const size_t name_len
,
1723 const u16 trans_id
, const u16 type
, const u16
class,
1724 u8
*const buf
, size_t buf_len
) {
1725 off_t j
= 0; /* current offset into buf */
1726 u16 _t
; /* used by the macros */
1729 APPEND16(0x0100); /* standard query, recusion needed */
1730 APPEND16(1); /* one question */
1731 APPEND16(0); /* no answers */
1732 APPEND16(0); /* no authority */
1733 APPEND16(0); /* no additional */
1735 j
= dnsname_to_labels(buf
, buf_len
, j
, name
, name_len
, NULL
);
1748 /* exported function */
1749 struct evdns_server_port
*
1750 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
)
1752 struct evdns_server_port
*port
;
1754 return NULL
; /* flags not yet implemented */
1755 if (!(port
= mm_malloc(sizeof(struct evdns_server_port
))))
1757 memset(port
, 0, sizeof(struct evdns_server_port
));
1760 port
->socket
= socket
;
1764 port
->user_callback
= cb
;
1765 port
->user_data
= user_data
;
1766 port
->pending_replies
= NULL
;
1767 port
->event_base
= base
;
1769 event_assign(&port
->event
, port
->event_base
,
1770 port
->socket
, EV_READ
| EV_PERSIST
,
1771 server_port_ready_callback
, port
);
1772 if (event_add(&port
->event
, NULL
) < 0) {
1776 EVTHREAD_ALLOC_LOCK(port
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
1780 struct evdns_server_port
*
1781 evdns_add_server_port(evutil_socket_t socket
, int flags
, evdns_request_callback_fn_type cb
, void *user_data
)
1783 return evdns_add_server_port_with_base(NULL
, socket
, flags
, cb
, user_data
);
1786 /* exported function */
1788 evdns_close_server_port(struct evdns_server_port
*port
)
1791 if (--port
->refcnt
== 0) {
1793 server_port_free(port
);
1799 /* exported function */
1801 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
)
1803 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
1804 struct server_reply_item
**itemp
, *item
;
1808 EVDNS_LOCK(req
->port
);
1809 if (req
->response
) /* have we already answered? */
1813 case EVDNS_ANSWER_SECTION
:
1814 itemp
= &req
->answer
;
1815 countp
= &req
->n_answer
;
1817 case EVDNS_AUTHORITY_SECTION
:
1818 itemp
= &req
->authority
;
1819 countp
= &req
->n_authority
;
1821 case EVDNS_ADDITIONAL_SECTION
:
1822 itemp
= &req
->additional
;
1823 countp
= &req
->n_additional
;
1829 itemp
= &((*itemp
)->next
);
1831 item
= mm_malloc(sizeof(struct server_reply_item
));
1835 if (!(item
->name
= mm_strdup(name
))) {
1840 item
->dns_question_class
= class;
1842 item
->is_name
= is_name
!= 0;
1846 if (item
->is_name
) {
1847 if (!(item
->data
= mm_strdup(data
))) {
1848 mm_free(item
->name
);
1852 item
->datalen
= (u16
)-1;
1854 if (!(item
->data
= mm_malloc(datalen
))) {
1855 mm_free(item
->name
);
1859 item
->datalen
= datalen
;
1860 memcpy(item
->data
, data
, datalen
);
1868 EVDNS_UNLOCK(req
->port
);
1872 /* exported function */
1874 evdns_server_request_add_a_reply(struct evdns_server_request
*req
, const char *name
, int n
, const void *addrs
, int ttl
)
1876 return evdns_server_request_add_reply(
1877 req
, EVDNS_ANSWER_SECTION
, name
, TYPE_A
, CLASS_INET
,
1878 ttl
, n
*4, 0, addrs
);
1881 /* exported function */
1883 evdns_server_request_add_aaaa_reply(struct evdns_server_request
*req
, const char *name
, int n
, const void *addrs
, int ttl
)
1885 return evdns_server_request_add_reply(
1886 req
, EVDNS_ANSWER_SECTION
, name
, TYPE_AAAA
, CLASS_INET
,
1887 ttl
, n
*16, 0, addrs
);
1890 /* exported function */
1892 evdns_server_request_add_ptr_reply(struct evdns_server_request
*req
, struct in_addr
*in
, const char *inaddr_name
, const char *hostname
, int ttl
)
1896 if (in
&& inaddr_name
)
1898 else if (!in
&& !inaddr_name
)
1901 a
= ntohl(in
->s_addr
);
1902 evutil_snprintf(buf
, sizeof(buf
), "%d.%d.%d.%d.in-addr.arpa",
1903 (int)(u8
)((a
)&0xff),
1904 (int)(u8
)((a
>>8 )&0xff),
1905 (int)(u8
)((a
>>16)&0xff),
1906 (int)(u8
)((a
>>24)&0xff));
1909 return evdns_server_request_add_reply(
1910 req
, EVDNS_ANSWER_SECTION
, inaddr_name
, TYPE_PTR
, CLASS_INET
,
1911 ttl
, -1, 1, hostname
);
1914 /* exported function */
1916 evdns_server_request_add_cname_reply(struct evdns_server_request
*req
, const char *name
, const char *cname
, int ttl
)
1918 return evdns_server_request_add_reply(
1919 req
, EVDNS_ANSWER_SECTION
, name
, TYPE_CNAME
, CLASS_INET
,
1923 /* exported function */
1925 evdns_server_request_set_flags(struct evdns_server_request
*exreq
, int flags
)
1927 struct server_request
*req
= TO_SERVER_REQUEST(exreq
);
1928 req
->base
.flags
&= ~(EVDNS_FLAGS_AA
|EVDNS_FLAGS_RD
);
1929 req
->base
.flags
|= flags
;
1933 evdns_server_request_format_response(struct server_request
*req
, int err
)
1935 unsigned char buf
[1500];
1936 size_t buf_len
= sizeof(buf
);
1942 struct dnslabel_table table
;
1944 if (err
< 0 || err
> 15) return -1;
1946 /* Set response bit and error code; copy OPCODE and RD fields from
1947 * question; copy RA and AA if set by caller. */
1948 flags
= req
->base
.flags
;
1949 flags
|= (0x8000 | err
);
1951 dnslabel_table_init(&table
);
1952 APPEND16(req
->trans_id
);
1954 APPEND16(req
->base
.nquestions
);
1955 APPEND16(req
->n_answer
);
1956 APPEND16(req
->n_authority
);
1957 APPEND16(req
->n_additional
);
1959 /* Add questions. */
1960 for (i
=0; i
< req
->base
.nquestions
; ++i
) {
1961 const char *s
= req
->base
.questions
[i
]->name
;
1962 j
= dnsname_to_labels(buf
, buf_len
, j
, s
, strlen(s
), &table
);
1964 dnslabel_clear(&table
);
1967 APPEND16(req
->base
.questions
[i
]->type
);
1968 APPEND16(req
->base
.questions
[i
]->dns_question_class
);
1971 /* Add answer, authority, and additional sections. */
1972 for (i
=0; i
<3; ++i
) {
1973 struct server_reply_item
*item
;
1977 item
= req
->authority
;
1979 item
= req
->additional
;
1981 r
= dnsname_to_labels(buf
, buf_len
, j
, item
->name
, strlen(item
->name
), &table
);
1986 APPEND16(item
->type
);
1987 APPEND16(item
->dns_question_class
);
1988 APPEND32(item
->ttl
);
1989 if (item
->is_name
) {
1990 off_t len_idx
= j
, name_start
;
1993 r
= dnsname_to_labels(buf
, buf_len
, j
, item
->data
, strlen(item
->data
), &table
);
1997 _t
= htons( (short) (j
-name_start
) );
1998 memcpy(buf
+len_idx
, &_t
, 2);
2000 APPEND16(item
->datalen
);
2001 if (j
+item
->datalen
> (off_t
)buf_len
)
2003 memcpy(buf
+j
, item
->data
, item
->datalen
);
2013 buf
[2] |= 0x02; /* set the truncated bit. */
2016 req
->response_len
= j
;
2018 if (!(req
->response
= mm_malloc(req
->response_len
))) {
2019 server_request_free_answers(req
);
2020 dnslabel_clear(&table
);
2023 memcpy(req
->response
, buf
, req
->response_len
);
2024 server_request_free_answers(req
);
2025 dnslabel_clear(&table
);
2029 /* exported function */
2031 evdns_server_request_respond(struct evdns_server_request
*_req
, int err
)
2033 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
2034 struct evdns_server_port
*port
= req
->port
;
2038 if (!req
->response
) {
2039 if ((r
= evdns_server_request_format_response(req
, err
))<0)
2043 r
= sendto(port
->socket
, req
->response
, (int)req
->response_len
, 0,
2044 (struct sockaddr
*) &req
->addr
, (ev_socklen_t
)req
->addrlen
);
2046 int sock_err
= evutil_socket_geterror(port
->socket
);
2047 if (EVUTIL_ERR_RW_RETRIABLE(sock_err
))
2050 if (port
->pending_replies
) {
2051 req
->prev_pending
= port
->pending_replies
->prev_pending
;
2052 req
->next_pending
= port
->pending_replies
;
2053 req
->prev_pending
->next_pending
=
2054 req
->next_pending
->prev_pending
= req
;
2056 req
->prev_pending
= req
->next_pending
= req
;
2057 port
->pending_replies
= req
;
2060 (void) event_del(&port
->event
);
2061 event_assign(&port
->event
, port
->event_base
, port
->socket
, (port
->closing
?0:EV_READ
) | EV_WRITE
| EV_PERSIST
, server_port_ready_callback
, port
);
2063 if (event_add(&port
->event
, NULL
) < 0) {
2064 log(EVDNS_LOG_WARN
, "Error from libevent when adding event for DNS server");
2072 if (server_request_free(req
)) {
2077 if (port
->pending_replies
)
2078 server_port_flush(port
);
2086 /* Free all storage held by RRs in req. */
2088 server_request_free_answers(struct server_request
*req
)
2090 struct server_reply_item
*victim
, *next
, **list
;
2092 for (i
= 0; i
< 3; ++i
) {
2094 list
= &req
->answer
;
2096 list
= &req
->authority
;
2098 list
= &req
->additional
;
2102 next
= victim
->next
;
2103 mm_free(victim
->name
);
2105 mm_free(victim
->data
);
2113 /* Free all storage held by req, and remove links to it. */
2114 /* return true iff we just wound up freeing the server_port. */
2116 server_request_free(struct server_request
*req
)
2118 int i
, rc
=1, lock
=0;
2119 if (req
->base
.questions
) {
2120 for (i
= 0; i
< req
->base
.nquestions
; ++i
)
2121 mm_free(req
->base
.questions
[i
]);
2122 mm_free(req
->base
.questions
);
2126 EVDNS_LOCK(req
->port
);
2128 if (req
->port
->pending_replies
== req
) {
2129 if (req
->next_pending
&& req
->next_pending
!= req
)
2130 req
->port
->pending_replies
= req
->next_pending
;
2132 req
->port
->pending_replies
= NULL
;
2134 rc
= --req
->port
->refcnt
;
2137 if (req
->response
) {
2138 mm_free(req
->response
);
2141 server_request_free_answers(req
);
2143 if (req
->next_pending
&& req
->next_pending
!= req
) {
2144 req
->next_pending
->prev_pending
= req
->prev_pending
;
2145 req
->prev_pending
->next_pending
= req
->next_pending
;
2149 EVDNS_UNLOCK(req
->port
); /* ????? nickm */
2150 server_port_free(req
->port
);
2155 EVDNS_UNLOCK(req
->port
);
2160 /* Free all storage held by an evdns_server_port. Only called when */
2162 server_port_free(struct evdns_server_port
*port
)
2164 EVUTIL_ASSERT(port
);
2165 EVUTIL_ASSERT(!port
->refcnt
);
2166 EVUTIL_ASSERT(!port
->pending_replies
);
2167 if (port
->socket
> 0) {
2168 evutil_closesocket(port
->socket
);
2171 (void) event_del(&port
->event
);
2172 event_debug_unassign(&port
->event
);
2173 EVTHREAD_FREE_LOCK(port
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
2177 /* exported function */
2179 evdns_server_request_drop(struct evdns_server_request
*_req
)
2181 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
2182 server_request_free(req
);
2186 /* exported function */
2188 evdns_server_request_get_requesting_addr(struct evdns_server_request
*_req
, struct sockaddr
*sa
, int addr_len
)
2190 struct server_request
*req
= TO_SERVER_REQUEST(_req
);
2191 if (addr_len
< (int)req
->addrlen
)
2193 memcpy(sa
, &(req
->addr
), req
->addrlen
);
2194 return req
->addrlen
;
2200 /* this is a libevent callback function which is called when a request */
2201 /* has timed out. */
2203 evdns_request_timeout_callback(evutil_socket_t fd
, short events
, void *arg
) {
2204 struct request
*const req
= (struct request
*) arg
;
2205 struct evdns_base
*base
= req
->base
;
2210 log(EVDNS_LOG_DEBUG
, "Request %p timed out", arg
);
2213 req
->ns
->timedout
++;
2214 if (req
->ns
->timedout
> req
->base
->global_max_nameserver_timeout
) {
2215 req
->ns
->timedout
= 0;
2216 nameserver_failed(req
->ns
, "request timed out.");
2219 if (req
->tx_count
>= req
->base
->global_max_retransmits
) {
2220 /* this request has failed */
2221 log(EVDNS_LOG_DEBUG
, "Giving up on request %p; tx_count==%d",
2222 arg
, req
->tx_count
);
2223 reply_schedule_callback(req
, 0, DNS_ERR_TIMEOUT
, NULL
);
2224 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 1);
2227 struct nameserver
*new_ns
;
2228 log(EVDNS_LOG_DEBUG
, "Retransmitting request %p; tx_count==%d",
2229 arg
, req
->tx_count
);
2230 (void) evtimer_del(&req
->timeout_event
);
2231 new_ns
= nameserver_pick(base
);
2234 evdns_request_transmit(req
);
2239 /* try to send a request to a given server. */
2243 /* 1 temporary failure */
2244 /* 2 other failure */
2246 evdns_request_transmit_to(struct request
*req
, struct nameserver
*server
) {
2248 ASSERT_LOCKED(req
->base
);
2249 ASSERT_VALID_REQUEST(req
);
2250 r
= sendto(server
->socket
, (void*)req
->request
, req
->request_len
, 0,
2251 (struct sockaddr
*)&server
->address
, server
->addrlen
);
2253 int err
= evutil_socket_geterror(server
->socket
);
2254 if (EVUTIL_ERR_RW_RETRIABLE(err
))
2256 nameserver_failed(req
->ns
, evutil_socket_error_to_string(err
));
2258 } else if (r
!= (int)req
->request_len
) {
2259 return 1; /* short write */
2265 /* try to send a request, updating the fields of the request */
2272 evdns_request_transmit(struct request
*req
) {
2275 ASSERT_LOCKED(req
->base
);
2276 ASSERT_VALID_REQUEST(req
);
2277 /* if we fail to send this packet then this flag marks it */
2278 /* for evdns_transmit */
2279 req
->transmit_me
= 1;
2280 EVUTIL_ASSERT(req
->trans_id
!= 0xffff);
2284 /* unable to transmit request if no nameservers */
2288 if (req
->ns
->choked
) {
2289 /* don't bother trying to write to a socket */
2290 /* which we have had EAGAIN from */
2294 r
= evdns_request_transmit_to(req
, req
->ns
);
2298 req
->ns
->choked
= 1;
2299 nameserver_write_waiting(req
->ns
, 1);
2302 /* failed to transmit the request entirely. */
2304 /* fall through: we'll set a timeout, which will time out,
2305 * and make us retransmit the request anyway. */
2308 log(EVDNS_LOG_DEBUG
,
2309 "Setting timeout for request %p, sent to nameserver %p", req
, req
->ns
);
2310 if (evtimer_add(&req
->timeout_event
, &req
->base
->global_timeout
) < 0) {
2312 "Error from libevent when adding timer for request %p",
2317 req
->transmit_me
= 0;
2323 nameserver_probe_callback(int result
, char type
, int count
, int ttl
, void *addresses
, void *arg
) {
2324 struct nameserver
*const ns
= (struct nameserver
*) arg
;
2330 if (result
== DNS_ERR_CANCEL
) {
2331 /* We canceled this request because the nameserver came up
2332 * for some other reason. Do not change our opinion about
2333 * the nameserver. */
2337 EVDNS_LOCK(ns
->base
);
2338 ns
->probe_request
= NULL
;
2339 if (result
== DNS_ERR_NONE
|| result
== DNS_ERR_NOTEXIST
) {
2340 /* this is a good reply */
2343 nameserver_probe_failed(ns
);
2345 EVDNS_UNLOCK(ns
->base
);
2349 nameserver_send_probe(struct nameserver
*const ns
) {
2350 struct evdns_request
*handle
;
2351 struct request
*req
;
2353 /* here we need to send a probe to a given nameserver */
2354 /* in the hope that it is up now. */
2356 ASSERT_LOCKED(ns
->base
);
2357 log(EVDNS_LOG_DEBUG
, "Sending probe to %s",
2358 evutil_format_sockaddr_port(
2359 (struct sockaddr
*)&ns
->address
,
2360 addrbuf
, sizeof(addrbuf
)));
2361 handle
= mm_calloc(1, sizeof(*handle
));
2362 if (!handle
) return;
2363 req
= request_new(ns
->base
, handle
, TYPE_A
, "google.com", DNS_QUERY_NO_SEARCH
, nameserver_probe_callback
, ns
);
2368 ns
->probe_request
= handle
;
2369 /* we force this into the inflight queue no matter what */
2370 request_trans_id_set(req
, transaction_id_pick(ns
->base
));
2372 request_submit(req
);
2376 /* 0 didn't try to transmit anything */
2377 /* 1 tried to transmit something */
2379 evdns_transmit(struct evdns_base
*base
) {
2380 char did_try_to_transmit
= 0;
2383 ASSERT_LOCKED(base
);
2384 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
2385 if (base
->req_heads
[i
]) {
2386 struct request
*const started_at
= base
->req_heads
[i
], *req
= started_at
;
2387 /* first transmit all the requests which are currently waiting */
2389 if (req
->transmit_me
) {
2390 did_try_to_transmit
= 1;
2391 evdns_request_transmit(req
);
2395 } while (req
!= started_at
);
2399 return did_try_to_transmit
;
2402 /* exported function */
2404 evdns_base_count_nameservers(struct evdns_base
*base
)
2406 const struct nameserver
*server
;
2410 server
= base
->server_head
;
2415 server
= server
->next
;
2416 } while (server
!= base
->server_head
);
2423 evdns_count_nameservers(void)
2425 return evdns_base_count_nameservers(current_base
);
2428 /* exported function */
2430 evdns_base_clear_nameservers_and_suspend(struct evdns_base
*base
)
2432 struct nameserver
*server
, *started_at
;
2436 server
= base
->server_head
;
2437 started_at
= base
->server_head
;
2443 struct nameserver
*next
= server
->next
;
2444 (void) event_del(&server
->event
);
2445 if (evtimer_initialized(&server
->timeout_event
))
2446 (void) evtimer_del(&server
->timeout_event
);
2447 if (server
->probe_request
) {
2448 evdns_cancel_request(server
->base
, server
->probe_request
);
2449 server
->probe_request
= NULL
;
2451 if (server
->socket
>= 0)
2452 evutil_closesocket(server
->socket
);
2454 if (next
== started_at
)
2458 base
->server_head
= NULL
;
2459 base
->global_good_nameservers
= 0;
2461 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
2462 struct request
*req
, *req_started_at
;
2463 req
= req_started_at
= base
->req_heads
[i
];
2465 struct request
*next
= req
->next
;
2466 req
->tx_count
= req
->reissue_count
= 0;
2468 /* ???? What to do about searches? */
2469 (void) evtimer_del(&req
->timeout_event
);
2471 req
->transmit_me
= 0;
2473 base
->global_requests_waiting
++;
2474 evdns_request_insert(req
, &base
->req_waiting_head
);
2475 /* We want to insert these suspended elements at the front of
2476 * the waiting queue, since they were pending before any of
2477 * the waiting entries were added. This is a circular list,
2478 * so we can just shift the start back by one.*/
2479 base
->req_waiting_head
= base
->req_waiting_head
->prev
;
2481 if (next
== req_started_at
)
2485 base
->req_heads
[i
] = NULL
;
2488 base
->global_requests_inflight
= 0;
2495 evdns_clear_nameservers_and_suspend(void)
2497 return evdns_base_clear_nameservers_and_suspend(current_base
);
2501 /* exported function */
2503 evdns_base_resume(struct evdns_base
*base
)
2506 evdns_requests_pump_waiting_queue(base
);
2515 return evdns_base_resume(current_base
);
2519 _evdns_nameserver_add_impl(struct evdns_base
*base
, const struct sockaddr
*address
, int addrlen
) {
2520 /* first check to see if we already have this nameserver */
2522 const struct nameserver
*server
= base
->server_head
, *const started_at
= base
->server_head
;
2523 struct nameserver
*ns
;
2527 ASSERT_LOCKED(base
);
2530 if (!evutil_sockaddr_cmp((struct sockaddr
*)&server
->address
, address
, 1)) return 3;
2531 server
= server
->next
;
2532 } while (server
!= started_at
);
2534 if (addrlen
> (int)sizeof(ns
->address
)) {
2535 log(EVDNS_LOG_DEBUG
, "Addrlen %d too long.", (int)addrlen
);
2539 ns
= (struct nameserver
*) mm_malloc(sizeof(struct nameserver
));
2542 memset(ns
, 0, sizeof(struct nameserver
));
2545 evtimer_assign(&ns
->timeout_event
, ns
->base
->event_base
, nameserver_prod_callback
, ns
);
2547 ns
->socket
= socket(address
->sa_family
, SOCK_DGRAM
, 0);
2548 if (ns
->socket
< 0) { err
= 1; goto out1
; }
2549 evutil_make_socket_closeonexec(ns
->socket
);
2550 evutil_make_socket_nonblocking(ns
->socket
);
2552 if (base
->global_outgoing_addrlen
&&
2553 !evutil_sockaddr_is_loopback(address
)) {
2554 if (bind(ns
->socket
,
2555 (struct sockaddr
*)&base
->global_outgoing_address
,
2556 base
->global_outgoing_addrlen
) < 0) {
2557 log(EVDNS_LOG_WARN
,"Couldn't bind to outgoing address");
2563 memcpy(&ns
->address
, address
, addrlen
);
2564 ns
->addrlen
= addrlen
;
2566 event_assign(&ns
->event
, ns
->base
->event_base
, ns
->socket
, EV_READ
| EV_PERSIST
, nameserver_ready_callback
, ns
);
2567 if (event_add(&ns
->event
, NULL
) < 0) {
2572 log(EVDNS_LOG_DEBUG
, "Added nameserver %s as %p",
2573 evutil_format_sockaddr_port(address
, addrbuf
, sizeof(addrbuf
)), ns
);
2575 /* insert this nameserver into the list of them */
2576 if (!base
->server_head
) {
2577 ns
->next
= ns
->prev
= ns
;
2578 base
->server_head
= ns
;
2580 ns
->next
= base
->server_head
->next
;
2581 ns
->prev
= base
->server_head
;
2582 base
->server_head
->next
= ns
;
2583 ns
->next
->prev
= ns
;
2586 base
->global_good_nameservers
++;
2591 evutil_closesocket(ns
->socket
);
2593 event_debug_unassign(&ns
->event
);
2595 log(EVDNS_LOG_WARN
, "Unable to add nameserver %s: error %d",
2596 evutil_format_sockaddr_port(address
, addrbuf
, sizeof(addrbuf
)), err
);
2600 /* exported function */
2602 evdns_base_nameserver_add(struct evdns_base
*base
, unsigned long int address
)
2604 struct sockaddr_in sin
;
2606 memset(&sin
, 0, sizeof(sin
));
2607 sin
.sin_addr
.s_addr
= address
;
2608 sin
.sin_port
= htons(53);
2609 sin
.sin_family
= AF_INET
;
2611 res
= _evdns_nameserver_add_impl(base
, (struct sockaddr
*)&sin
, sizeof(sin
));
2617 evdns_nameserver_add(unsigned long int address
) {
2619 current_base
= evdns_base_new(NULL
, 0);
2620 return evdns_base_nameserver_add(current_base
, address
);
2624 sockaddr_setport(struct sockaddr
*sa
, ev_uint16_t port
)
2626 if (sa
->sa_family
== AF_INET
) {
2627 ((struct sockaddr_in
*)sa
)->sin_port
= htons(port
);
2628 } else if (sa
->sa_family
== AF_INET6
) {
2629 ((struct sockaddr_in6
*)sa
)->sin6_port
= htons(port
);
2634 sockaddr_getport(struct sockaddr
*sa
)
2636 if (sa
->sa_family
== AF_INET
) {
2637 return ntohs(((struct sockaddr_in
*)sa
)->sin_port
);
2638 } else if (sa
->sa_family
== AF_INET6
) {
2639 return ntohs(((struct sockaddr_in6
*)sa
)->sin6_port
);
2645 /* exported function */
2647 evdns_base_nameserver_ip_add(struct evdns_base
*base
, const char *ip_as_string
) {
2648 struct sockaddr_storage ss
;
2649 struct sockaddr
*sa
;
2650 int len
= sizeof(ss
);
2652 if (evutil_parse_sockaddr_port(ip_as_string
, (struct sockaddr
*)&ss
,
2654 log(EVDNS_LOG_WARN
, "Unable to parse nameserver address %s",
2658 sa
= (struct sockaddr
*) &ss
;
2659 if (sockaddr_getport(sa
) == 0)
2660 sockaddr_setport(sa
, 53);
2663 res
= _evdns_nameserver_add_impl(base
, sa
, len
);
2669 evdns_nameserver_ip_add(const char *ip_as_string
) {
2671 current_base
= evdns_base_new(NULL
, 0);
2672 return evdns_base_nameserver_ip_add(current_base
, ip_as_string
);
2676 evdns_base_nameserver_sockaddr_add(struct evdns_base
*base
,
2677 const struct sockaddr
*sa
, ev_socklen_t len
, unsigned flags
)
2680 EVUTIL_ASSERT(base
);
2682 res
= _evdns_nameserver_add_impl(base
, sa
, len
);
2687 /* remove from the queue */
2689 evdns_request_remove(struct request
*req
, struct request
**head
)
2691 ASSERT_LOCKED(req
->base
);
2692 ASSERT_VALID_REQUEST(req
);
2696 struct request
*ptr
;
2698 EVUTIL_ASSERT(*head
!= NULL
);
2707 } while (ptr
!= *head
);
2708 EVUTIL_ASSERT(found
);
2710 EVUTIL_ASSERT(req
->next
);
2714 if (req
->next
== req
) {
2715 /* only item in the list */
2718 req
->next
->prev
= req
->prev
;
2719 req
->prev
->next
= req
->next
;
2720 if (*head
== req
) *head
= req
->next
;
2722 req
->next
= req
->prev
= NULL
;
2725 /* insert into the tail of the queue */
2727 evdns_request_insert(struct request
*req
, struct request
**head
) {
2728 ASSERT_LOCKED(req
->base
);
2729 ASSERT_VALID_REQUEST(req
);
2732 req
->next
= req
->prev
= req
;
2736 req
->prev
= (*head
)->prev
;
2737 req
->prev
->next
= req
;
2739 (*head
)->prev
= req
;
2743 string_num_dots(const char *s
) {
2745 while ((s
= strchr(s
, '.'))) {
2752 static struct request
*
2753 request_new(struct evdns_base
*base
, struct evdns_request
*handle
, int type
,
2754 const char *name
, int flags
, evdns_callback_type callback
,
2757 const char issuing_now
=
2758 (base
->global_requests_inflight
< base
->global_max_requests_inflight
) ? 1 : 0;
2760 const size_t name_len
= strlen(name
);
2761 const size_t request_max_len
= evdns_request_len(name_len
);
2762 const u16 trans_id
= issuing_now
? transaction_id_pick(base
) : 0xffff;
2763 /* the request data is alloced in a single block with the header */
2764 struct request
*const req
=
2765 mm_malloc(sizeof(struct request
) + request_max_len
);
2770 ASSERT_LOCKED(base
);
2772 if (!req
) return NULL
;
2774 if (name_len
>= sizeof(namebuf
)) {
2779 memset(req
, 0, sizeof(struct request
));
2782 evtimer_assign(&req
->timeout_event
, req
->base
->event_base
, evdns_request_timeout_callback
, req
);
2784 if (base
->global_randomize_case
) {
2786 char randbits
[(sizeof(namebuf
)+7)/8];
2787 strlcpy(namebuf
, name
, sizeof(namebuf
));
2788 rand_bytes_function(randbits
, (name_len
+7)/8);
2789 for (i
= 0; i
< name_len
; ++i
) {
2790 if (EVUTIL_ISALPHA(namebuf
[i
])) {
2791 if ((randbits
[i
>> 3] & (1<<(i
& 7))))
2794 namebuf
[i
] &= ~0x20;
2800 /* request data lives just after the header */
2801 req
->request
= ((u8
*) req
) + sizeof(struct request
);
2802 /* denotes that the request data shouldn't be free()ed */
2803 req
->request_appended
= 1;
2804 rlen
= evdns_request_data_build(name
, name_len
, trans_id
,
2805 type
, CLASS_INET
, req
->request
, request_max_len
);
2809 req
->request_len
= rlen
;
2810 req
->trans_id
= trans_id
;
2812 req
->request_type
= type
;
2813 req
->user_pointer
= user_ptr
;
2814 req
->user_callback
= callback
;
2815 req
->ns
= issuing_now
? nameserver_pick(base
) : NULL
;
2816 req
->next
= req
->prev
= NULL
;
2817 req
->handle
= handle
;
2819 handle
->current_req
= req
;
2820 handle
->base
= base
;
2830 request_submit(struct request
*const req
) {
2831 struct evdns_base
*base
= req
->base
;
2832 ASSERT_LOCKED(base
);
2833 ASSERT_VALID_REQUEST(req
);
2835 /* if it has a nameserver assigned then this is going */
2836 /* straight into the inflight queue */
2837 evdns_request_insert(req
, &REQ_HEAD(base
, req
->trans_id
));
2838 base
->global_requests_inflight
++;
2839 evdns_request_transmit(req
);
2841 evdns_request_insert(req
, &base
->req_waiting_head
);
2842 base
->global_requests_waiting
++;
2846 /* exported function */
2848 evdns_cancel_request(struct evdns_base
*base
, struct evdns_request
*handle
)
2850 struct request
*req
;
2852 if (!handle
->current_req
)
2856 /* This redundancy is silly; can we fix it? (Not for 2.0) XXXX */
2857 base
= handle
->base
;
2859 base
= handle
->current_req
->base
;
2863 if (handle
->pending_cb
) {
2868 req
= handle
->current_req
;
2869 ASSERT_VALID_REQUEST(req
);
2871 reply_schedule_callback(req
, 0, DNS_ERR_CANCEL
, NULL
);
2873 /* remove from inflight queue */
2874 request_finished(req
, &REQ_HEAD(base
, req
->trans_id
), 1);
2876 /* remove from global_waiting head */
2877 request_finished(req
, &base
->req_waiting_head
, 1);
2882 /* exported function */
2883 struct evdns_request
*
2884 evdns_base_resolve_ipv4(struct evdns_base
*base
, const char *name
, int flags
,
2885 evdns_callback_type callback
, void *ptr
) {
2886 struct evdns_request
*handle
;
2887 struct request
*req
;
2888 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s", name
);
2889 handle
= mm_calloc(1, sizeof(*handle
));
2893 if (flags
& DNS_QUERY_NO_SEARCH
) {
2895 request_new(base
, handle
, TYPE_A
, name
, flags
,
2898 request_submit(req
);
2900 search_request_new(base
, handle
, TYPE_A
, name
, flags
,
2903 if (handle
->current_req
== NULL
) {
2911 int evdns_resolve_ipv4(const char *name
, int flags
,
2912 evdns_callback_type callback
, void *ptr
)
2914 return evdns_base_resolve_ipv4(current_base
, name
, flags
, callback
, ptr
)
2919 /* exported function */
2920 struct evdns_request
*
2921 evdns_base_resolve_ipv6(struct evdns_base
*base
,
2922 const char *name
, int flags
,
2923 evdns_callback_type callback
, void *ptr
)
2925 struct evdns_request
*handle
;
2926 struct request
*req
;
2927 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s", name
);
2928 handle
= mm_calloc(1, sizeof(*handle
));
2932 if (flags
& DNS_QUERY_NO_SEARCH
) {
2933 req
= request_new(base
, handle
, TYPE_AAAA
, name
, flags
,
2936 request_submit(req
);
2938 search_request_new(base
, handle
, TYPE_AAAA
, name
, flags
,
2941 if (handle
->current_req
== NULL
) {
2949 int evdns_resolve_ipv6(const char *name
, int flags
,
2950 evdns_callback_type callback
, void *ptr
) {
2951 return evdns_base_resolve_ipv6(current_base
, name
, flags
, callback
, ptr
)
2955 struct evdns_request
*
2956 evdns_base_resolve_reverse(struct evdns_base
*base
, const struct in_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2958 struct evdns_request
*handle
;
2959 struct request
*req
;
2962 a
= ntohl(in
->s_addr
);
2963 evutil_snprintf(buf
, sizeof(buf
), "%d.%d.%d.%d.in-addr.arpa",
2964 (int)(u8
)((a
)&0xff),
2965 (int)(u8
)((a
>>8 )&0xff),
2966 (int)(u8
)((a
>>16)&0xff),
2967 (int)(u8
)((a
>>24)&0xff));
2968 handle
= mm_calloc(1, sizeof(*handle
));
2971 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s (reverse)", buf
);
2973 req
= request_new(base
, handle
, TYPE_PTR
, buf
, flags
, callback
, ptr
);
2975 request_submit(req
);
2976 if (handle
->current_req
== NULL
) {
2984 int evdns_resolve_reverse(const struct in_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2985 return evdns_base_resolve_reverse(current_base
, in
, flags
, callback
, ptr
)
2989 struct evdns_request
*
2990 evdns_base_resolve_reverse_ipv6(struct evdns_base
*base
, const struct in6_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
2991 /* 32 nybbles, 32 periods, "ip6.arpa", NUL. */
2994 struct evdns_request
*handle
;
2995 struct request
*req
;
2999 for (i
=15; i
>= 0; --i
) {
3000 u8 byte
= in
->s6_addr
[i
];
3001 *cp
++ = "0123456789abcdef"[byte
& 0x0f];
3003 *cp
++ = "0123456789abcdef"[byte
>> 4];
3006 EVUTIL_ASSERT(cp
+ strlen("ip6.arpa") < buf
+sizeof(buf
));
3007 memcpy(cp
, "ip6.arpa", strlen("ip6.arpa")+1);
3008 handle
= mm_calloc(1, sizeof(*handle
));
3011 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s (reverse)", buf
);
3013 req
= request_new(base
, handle
, TYPE_PTR
, buf
, flags
, callback
, ptr
);
3015 request_submit(req
);
3016 if (handle
->current_req
== NULL
) {
3024 int evdns_resolve_reverse_ipv6(const struct in6_addr
*in
, int flags
, evdns_callback_type callback
, void *ptr
) {
3025 return evdns_base_resolve_reverse_ipv6(current_base
, in
, flags
, callback
, ptr
)
3029 /* exported function */
3030 struct evdns_request
*
3031 evdns_base_resolve_txt(struct evdns_base
*base
, const char *name
, int flags
,
3032 evdns_callback_type callback
, void *ptr
) {
3033 struct evdns_request
*handle
;
3034 struct request
*req
;
3035 log(EVDNS_LOG_DEBUG
, "Resolve requested for %s (txt)", name
);
3036 handle
= mm_calloc(1, sizeof(*handle
));
3040 if (flags
& DNS_QUERY_NO_SEARCH
) {
3042 request_new(base
, handle
, TYPE_TXT
, name
, flags
,
3045 request_submit(req
);
3047 search_request_new(base
, handle
, TYPE_TXT
, name
, flags
,
3050 if (handle
->current_req
== NULL
) {
3058 /* ================================================================= */
3059 /* Search support */
3061 /* the libc resolver has support for searching a number of domains */
3062 /* to find a name. If nothing else then it takes the single domain */
3063 /* from the gethostname() call. */
3065 /* It can also be configured via the domain and search options in a */
3068 /* The ndots option controls how many dots it takes for the resolver */
3069 /* to decide that a name is non-local and so try a raw lookup first. */
3071 struct search_domain
{
3073 struct search_domain
*next
;
3074 /* the text string is appended to this structure */
3077 struct search_state
{
3081 struct search_domain
*head
;
3085 search_state_decref(struct search_state
*const state
) {
3088 if (!state
->refcount
) {
3089 struct search_domain
*next
, *dom
;
3090 for (dom
= state
->head
; dom
; dom
= next
) {
3098 static struct search_state
*
3099 search_state_new(void) {
3100 struct search_state
*state
= (struct search_state
*) mm_malloc(sizeof(struct search_state
));
3101 if (!state
) return NULL
;
3102 memset(state
, 0, sizeof(struct search_state
));
3103 state
->refcount
= 1;
3110 search_postfix_clear(struct evdns_base
*base
) {
3111 search_state_decref(base
->global_search_state
);
3113 base
->global_search_state
= search_state_new();
3116 /* exported function */
3118 evdns_base_search_clear(struct evdns_base
*base
)
3121 search_postfix_clear(base
);
3126 evdns_search_clear(void) {
3127 evdns_base_search_clear(current_base
);
3131 search_postfix_add(struct evdns_base
*base
, const char *domain
) {
3133 struct search_domain
*sdomain
;
3134 while (domain
[0] == '.') domain
++;
3135 domain_len
= strlen(domain
);
3137 ASSERT_LOCKED(base
);
3138 if (!base
->global_search_state
) base
->global_search_state
= search_state_new();
3139 if (!base
->global_search_state
) return;
3140 base
->global_search_state
->num_domains
++;
3142 sdomain
= (struct search_domain
*) mm_malloc(sizeof(struct search_domain
) + domain_len
);
3143 if (!sdomain
) return;
3144 memcpy( ((u8
*) sdomain
) + sizeof(struct search_domain
), domain
, domain_len
);
3145 sdomain
->next
= base
->global_search_state
->head
;
3146 sdomain
->len
= (int) domain_len
;
3148 base
->global_search_state
->head
= sdomain
;
3151 /* reverse the order of members in the postfix list. This is needed because, */
3152 /* when parsing resolv.conf we push elements in the wrong order */
3154 search_reverse(struct evdns_base
*base
) {
3155 struct search_domain
*cur
, *prev
= NULL
, *next
;
3156 ASSERT_LOCKED(base
);
3157 cur
= base
->global_search_state
->head
;
3165 base
->global_search_state
->head
= prev
;
3168 /* exported function */
3170 evdns_base_search_add(struct evdns_base
*base
, const char *domain
) {
3172 search_postfix_add(base
, domain
);
3176 evdns_search_add(const char *domain
) {
3177 evdns_base_search_add(current_base
, domain
);
3180 /* exported function */
3182 evdns_base_search_ndots_set(struct evdns_base
*base
, const int ndots
) {
3184 if (!base
->global_search_state
) base
->global_search_state
= search_state_new();
3185 if (base
->global_search_state
)
3186 base
->global_search_state
->ndots
= ndots
;
3190 evdns_search_ndots_set(const int ndots
) {
3191 evdns_base_search_ndots_set(current_base
, ndots
);
3195 search_set_from_hostname(struct evdns_base
*base
) {
3196 char hostname
[HOST_NAME_MAX
+ 1], *domainname
;
3198 ASSERT_LOCKED(base
);
3199 search_postfix_clear(base
);
3200 if (gethostname(hostname
, sizeof(hostname
))) return;
3201 domainname
= strchr(hostname
, '.');
3202 if (!domainname
) return;
3203 search_postfix_add(base
, domainname
);
3206 /* warning: returns malloced string */
3208 search_make_new(const struct search_state
*const state
, int n
, const char *const base_name
) {
3209 const size_t base_len
= strlen(base_name
);
3210 const char need_to_append_dot
= base_name
[base_len
- 1] == '.' ? 0 : 1;
3211 struct search_domain
*dom
;
3213 for (dom
= state
->head
; dom
; dom
= dom
->next
) {
3215 /* this is the postfix we want */
3216 /* the actual postfix string is kept at the end of the structure */
3217 const u8
*const postfix
= ((u8
*) dom
) + sizeof(struct search_domain
);
3218 const int postfix_len
= dom
->len
;
3219 char *const newname
= (char *) mm_malloc(base_len
+ need_to_append_dot
+ postfix_len
+ 1);
3220 if (!newname
) return NULL
;
3221 memcpy(newname
, base_name
, base_len
);
3222 if (need_to_append_dot
) newname
[base_len
] = '.';
3223 memcpy(newname
+ base_len
+ need_to_append_dot
, postfix
, postfix_len
);
3224 newname
[base_len
+ need_to_append_dot
+ postfix_len
] = 0;
3229 /* we ran off the end of the list and still didn't find the requested string */
3231 return NULL
; /* unreachable; stops warnings in some compilers. */
3234 static struct request
*
3235 search_request_new(struct evdns_base
*base
, struct evdns_request
*handle
,
3236 int type
, const char *const name
, int flags
,
3237 evdns_callback_type user_callback
, void *user_arg
) {
3238 ASSERT_LOCKED(base
);
3239 EVUTIL_ASSERT(type
== TYPE_A
|| type
== TYPE_AAAA
);
3240 EVUTIL_ASSERT(handle
->current_req
== NULL
);
3241 if ( ((flags
& DNS_QUERY_NO_SEARCH
) == 0) &&
3242 base
->global_search_state
&&
3243 base
->global_search_state
->num_domains
) {
3244 /* we have some domains to search */
3245 struct request
*req
;
3246 if (string_num_dots(name
) >= base
->global_search_state
->ndots
) {
3247 req
= request_new(base
, handle
, type
, name
, flags
, user_callback
, user_arg
);
3248 if (!req
) return NULL
;
3249 handle
->search_index
= -1;
3251 char *const new_name
= search_make_new(base
->global_search_state
, 0, name
);
3252 if (!new_name
) return NULL
;
3253 req
= request_new(base
, handle
, type
, new_name
, flags
, user_callback
, user_arg
);
3255 if (!req
) return NULL
;
3256 handle
->search_index
= 0;
3258 EVUTIL_ASSERT(handle
->search_origname
== NULL
);
3259 handle
->search_origname
= mm_strdup(name
);
3260 if (handle
->search_origname
== NULL
) {
3261 /* XXX Should we dealloc req? If yes, how? */
3266 handle
->search_state
= base
->global_search_state
;
3267 handle
->search_flags
= flags
;
3268 base
->global_search_state
->refcount
++;
3269 request_submit(req
);
3272 struct request
*const req
= request_new(base
, handle
, type
, name
, flags
, user_callback
, user_arg
);
3273 if (!req
) return NULL
;
3274 request_submit(req
);
3279 /* this is called when a request has failed to find a name. We need to check */
3280 /* if it is part of a search and, if so, try the next name in the list */
3282 /* 0 another request has been submitted */
3283 /* 1 no more requests needed */
3285 search_try_next(struct evdns_request
*const handle
) {
3286 struct request
*req
= handle
->current_req
;
3287 struct evdns_base
*base
= req
->base
;
3288 struct request
*newreq
;
3289 ASSERT_LOCKED(base
);
3290 if (handle
->search_state
) {
3291 /* it is part of a search */
3293 handle
->search_index
++;
3294 if (handle
->search_index
>= handle
->search_state
->num_domains
) {
3295 /* no more postfixes to try, however we may need to try */
3296 /* this name without a postfix */
3297 if (string_num_dots(handle
->search_origname
) < handle
->search_state
->ndots
) {
3298 /* yep, we need to try it raw */
3299 newreq
= request_new(base
, NULL
, req
->request_type
, handle
->search_origname
, handle
->search_flags
, req
->user_callback
, req
->user_pointer
);
3300 log(EVDNS_LOG_DEBUG
, "Search: trying raw query %s", handle
->search_origname
);
3302 search_request_finished(handle
);
3309 new_name
= search_make_new(handle
->search_state
, handle
->search_index
, handle
->search_origname
);
3310 if (!new_name
) return 1;
3311 log(EVDNS_LOG_DEBUG
, "Search: now trying %s (%d)", new_name
, handle
->search_index
);
3312 newreq
= request_new(base
, NULL
, req
->request_type
, new_name
, handle
->search_flags
, req
->user_callback
, req
->user_pointer
);
3314 if (!newreq
) return 1;
3320 request_finished(req
, &REQ_HEAD(req
->base
, req
->trans_id
), 0);
3321 handle
->current_req
= newreq
;
3322 newreq
->handle
= handle
;
3323 request_submit(newreq
);
3328 search_request_finished(struct evdns_request
*const handle
) {
3329 ASSERT_LOCKED(handle
->current_req
->base
);
3330 if (handle
->search_state
) {
3331 search_state_decref(handle
->search_state
);
3332 handle
->search_state
= NULL
;
3334 if (handle
->search_origname
) {
3335 mm_free(handle
->search_origname
);
3336 handle
->search_origname
= NULL
;
3340 /* ================================================================= */
3341 /* Parsing resolv.conf files */
3344 evdns_resolv_set_defaults(struct evdns_base
*base
, int flags
) {
3345 /* if the file isn't found then we assume a local resolver */
3346 ASSERT_LOCKED(base
);
3347 if (flags
& DNS_OPTION_SEARCH
) search_set_from_hostname(base
);
3348 if (flags
& DNS_OPTION_NAMESERVERS
) evdns_base_nameserver_ip_add(base
,"127.0.0.1");
3351 #ifndef _EVENT_HAVE_STRTOK_R
3353 strtok_r(char *s
, const char *delim
, char **state
) {
3355 start
= cp
= s
? s
: *state
;
3358 while (*cp
&& !strchr(delim
, *cp
))
3373 /* helper version of atoi which returns -1 on error */
3375 strtoint(const char *const str
)
3378 const int r
= strtol(str
, &endptr
, 10);
3379 if (*endptr
) return -1;
3383 /* Parse a number of seconds into a timeval; return -1 on error. */
3385 strtotimeval(const char *const str
, struct timeval
*out
)
3389 d
= strtod(str
, &endptr
);
3390 if (*endptr
) return -1;
3391 if (d
< 0) return -1;
3392 out
->tv_sec
= (int) d
;
3393 out
->tv_usec
= (int) ((d
- (int) d
)*1000000);
3394 if (out
->tv_sec
== 0 && out
->tv_usec
< 1000) /* less than 1 msec */
3399 /* helper version of atoi that returns -1 on error and clips to bounds. */
3401 strtoint_clipped(const char *const str
, int min
, int max
)
3403 int r
= strtoint(str
);
3415 evdns_base_set_max_requests_inflight(struct evdns_base
*base
, int maxinflight
)
3417 int old_n_heads
= base
->n_req_heads
, n_heads
;
3418 struct request
**old_heads
= base
->req_heads
, **new_heads
, *req
;
3421 ASSERT_LOCKED(base
);
3422 if (maxinflight
< 1)
3424 n_heads
= (maxinflight
+4) / 5;
3425 EVUTIL_ASSERT(n_heads
> 0);
3426 new_heads
= mm_calloc(n_heads
, sizeof(struct request
*));
3430 for (i
= 0; i
< old_n_heads
; ++i
) {
3431 while (old_heads
[i
]) {
3433 evdns_request_remove(req
, &old_heads
[i
]);
3434 evdns_request_insert(req
, &new_heads
[req
->trans_id
% n_heads
]);
3439 base
->req_heads
= new_heads
;
3440 base
->n_req_heads
= n_heads
;
3441 base
->global_max_requests_inflight
= maxinflight
;
3445 /* exported function */
3447 evdns_base_set_option(struct evdns_base
*base
,
3448 const char *option
, const char *val
)
3452 res
= evdns_base_set_option_impl(base
, option
, val
, DNS_OPTIONS_ALL
);
3458 str_matches_option(const char *s1
, const char *optionname
)
3460 /* Option names are given as "option:" We accept either 'option' in
3461 * s1, or 'option:randomjunk'. The latter form is to implement the
3462 * resolv.conf parser. */
3463 size_t optlen
= strlen(optionname
);
3464 size_t slen
= strlen(s1
);
3465 if (slen
== optlen
|| slen
== optlen
- 1)
3466 return !strncmp(s1
, optionname
, slen
);
3467 else if (slen
> optlen
)
3468 return !strncmp(s1
, optionname
, optlen
);
3474 evdns_base_set_option_impl(struct evdns_base
*base
,
3475 const char *option
, const char *val
, int flags
)
3477 ASSERT_LOCKED(base
);
3478 if (str_matches_option(option
, "ndots:")) {
3479 const int ndots
= strtoint(val
);
3480 if (ndots
== -1) return -1;
3481 if (!(flags
& DNS_OPTION_SEARCH
)) return 0;
3482 log(EVDNS_LOG_DEBUG
, "Setting ndots to %d", ndots
);
3483 if (!base
->global_search_state
) base
->global_search_state
= search_state_new();
3484 if (!base
->global_search_state
) return -1;
3485 base
->global_search_state
->ndots
= ndots
;
3486 } else if (str_matches_option(option
, "timeout:")) {
3488 if (strtotimeval(val
, &tv
) == -1) return -1;
3489 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3490 log(EVDNS_LOG_DEBUG
, "Setting timeout to %s", val
);
3491 memcpy(&base
->global_timeout
, &tv
, sizeof(struct timeval
));
3492 } else if (str_matches_option(option
, "getaddrinfo-allow-skew:")) {
3494 if (strtotimeval(val
, &tv
) == -1) return -1;
3495 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3496 log(EVDNS_LOG_DEBUG
, "Setting getaddrinfo-allow-skew to %s",
3498 memcpy(&base
->global_getaddrinfo_allow_skew
, &tv
,
3499 sizeof(struct timeval
));
3500 } else if (str_matches_option(option
, "max-timeouts:")) {
3501 const int maxtimeout
= strtoint_clipped(val
, 1, 255);
3502 if (maxtimeout
== -1) return -1;
3503 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3504 log(EVDNS_LOG_DEBUG
, "Setting maximum allowed timeouts to %d",
3506 base
->global_max_nameserver_timeout
= maxtimeout
;
3507 } else if (str_matches_option(option
, "max-inflight:")) {
3508 const int maxinflight
= strtoint_clipped(val
, 1, 65000);
3509 if (maxinflight
== -1) return -1;
3510 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3511 log(EVDNS_LOG_DEBUG
, "Setting maximum inflight requests to %d",
3513 evdns_base_set_max_requests_inflight(base
, maxinflight
);
3514 } else if (str_matches_option(option
, "attempts:")) {
3515 int retries
= strtoint(val
);
3516 if (retries
== -1) return -1;
3517 if (retries
> 255) retries
= 255;
3518 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3519 log(EVDNS_LOG_DEBUG
, "Setting retries to %d", retries
);
3520 base
->global_max_retransmits
= retries
;
3521 } else if (str_matches_option(option
, "randomize-case:")) {
3522 int randcase
= strtoint(val
);
3523 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3524 base
->global_randomize_case
= randcase
;
3525 } else if (str_matches_option(option
, "bind-to:")) {
3526 /* XXX This only applies to successive nameservers, not
3527 * to already-configured ones. We might want to fix that. */
3528 int len
= sizeof(base
->global_outgoing_address
);
3529 if (!(flags
& DNS_OPTION_NAMESERVERS
)) return 0;
3530 if (evutil_parse_sockaddr_port(val
,
3531 (struct sockaddr
*)&base
->global_outgoing_address
, &len
))
3533 base
->global_outgoing_addrlen
= len
;
3534 } else if (str_matches_option(option
, "initial-probe-timeout:")) {
3536 if (strtotimeval(val
, &tv
) == -1) return -1;
3537 if (tv
.tv_sec
> 3600)
3539 if (!(flags
& DNS_OPTION_MISC
)) return 0;
3540 log(EVDNS_LOG_DEBUG
, "Setting initial probe timeout to %s",
3542 memcpy(&base
->global_nameserver_probe_initial_timeout
, &tv
,
3549 evdns_set_option(const char *option
, const char *val
, int flags
)
3552 current_base
= evdns_base_new(NULL
, 0);
3553 return evdns_base_set_option(current_base
, option
, val
);
3557 resolv_conf_parse_line(struct evdns_base
*base
, char *const start
, int flags
) {
3559 static const char *const delims
= " \t";
3560 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
3563 char *const first_token
= strtok_r(start
, delims
, &strtok_state
);
3564 ASSERT_LOCKED(base
);
3565 if (!first_token
) return;
3567 if (!strcmp(first_token
, "nameserver") && (flags
& DNS_OPTION_NAMESERVERS
)) {
3568 const char *const nameserver
= NEXT_TOKEN
;
3571 evdns_base_nameserver_ip_add(base
, nameserver
);
3572 } else if (!strcmp(first_token
, "domain") && (flags
& DNS_OPTION_SEARCH
)) {
3573 const char *const domain
= NEXT_TOKEN
;
3575 search_postfix_clear(base
);
3576 search_postfix_add(base
, domain
);
3578 } else if (!strcmp(first_token
, "search") && (flags
& DNS_OPTION_SEARCH
)) {
3580 search_postfix_clear(base
);
3582 while ((domain
= NEXT_TOKEN
)) {
3583 search_postfix_add(base
, domain
);
3585 search_reverse(base
);
3586 } else if (!strcmp(first_token
, "options")) {
3588 while ((option
= NEXT_TOKEN
)) {
3589 const char *val
= strchr(option
, ':');
3590 evdns_base_set_option_impl(base
, option
, val
? val
+1 : "", flags
);
3596 /* exported function */
3599 /* 1 failed to open file */
3600 /* 2 failed to stat file */
3601 /* 3 file too large */
3602 /* 4 out of memory */
3603 /* 5 short read from file */
3605 evdns_base_resolv_conf_parse(struct evdns_base
*base
, int flags
, const char *const filename
) {
3608 res
= evdns_base_resolv_conf_parse_impl(base
, flags
, filename
);
3614 evdns_get_default_hosts_filename(void)
3617 /* Windows is a little coy about where it puts its configuration
3618 * files. Sure, they're _usually_ in C:\windows\system32, but
3619 * there's no reason in principle they couldn't be in
3620 * W:\hoboken chicken emergency\
3622 char path
[MAX_PATH
+1];
3623 static const char hostfile
[] = "\\drivers\\etc\\hosts";
3627 if (! SHGetSpecialFolderPathA(NULL
, path
, CSIDL_SYSTEM
, 0))
3629 len_out
= strlen(path
)+strlen(hostfile
);
3630 path_out
= mm_malloc(len_out
+1);
3631 evutil_snprintf(path_out
, len_out
, "%s%s", path
, hostfile
);
3634 return mm_strdup("/etc/hosts");
3639 evdns_base_resolv_conf_parse_impl(struct evdns_base
*base
, int flags
, const char *const filename
) {
3645 log(EVDNS_LOG_DEBUG
, "Parsing resolv.conf file %s", filename
);
3647 if (flags
& DNS_OPTION_HOSTSFILE
) {
3648 char *fname
= evdns_get_default_hosts_filename();
3649 evdns_base_load_hosts(base
, fname
);
3654 if ((err
= evutil_read_file(filename
, &resolv
, &n
, 0)) < 0) {
3657 evdns_resolv_set_defaults(base
, flags
);
3666 char *const newline
= strchr(start
, '\n');
3668 resolv_conf_parse_line(base
, start
, flags
);
3672 resolv_conf_parse_line(base
, start
, flags
);
3673 start
= newline
+ 1;
3677 if (!base
->server_head
&& (flags
& DNS_OPTION_NAMESERVERS
)) {
3678 /* no nameservers were configured. */
3679 evdns_base_nameserver_ip_add(base
, "127.0.0.1");
3682 if (flags
& DNS_OPTION_SEARCH
&& (!base
->global_search_state
|| base
->global_search_state
->num_domains
== 0)) {
3683 search_set_from_hostname(base
);
3691 evdns_resolv_conf_parse(int flags
, const char *const filename
) {
3693 current_base
= evdns_base_new(NULL
, 0);
3694 return evdns_base_resolv_conf_parse(current_base
, flags
, filename
);
3699 /* Add multiple nameservers from a space-or-comma-separated list. */
3701 evdns_nameserver_ip_add_line(struct evdns_base
*base
, const char *ips
) {
3705 ASSERT_LOCKED(base
);
3707 while (isspace(*ips
) || *ips
== ',' || *ips
== '\t')
3710 while (isdigit(*ips
) || *ips
== '.' || *ips
== ':' ||
3711 *ips
=='[' || *ips
==']')
3713 buf
= mm_malloc(ips
-addr
+1);
3715 memcpy(buf
, addr
, ips
-addr
);
3716 buf
[ips
-addr
] = '\0';
3717 r
= evdns_base_nameserver_ip_add(base
, buf
);
3724 typedef DWORD(WINAPI
*GetNetworkParams_fn_t
)(FIXED_INFO
*, DWORD
*);
3726 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
3727 /* figure out what our nameservers are. */
3729 load_nameservers_with_getnetworkparams(struct evdns_base
*base
)
3731 /* Based on MSDN examples and inspection of c-ares code. */
3734 ULONG size
= sizeof(FIXED_INFO
);
3736 int status
= 0, r
, added_any
;
3738 GetNetworkParams_fn_t fn
;
3740 ASSERT_LOCKED(base
);
3741 if (!(handle
= evutil_load_windows_system_library(
3742 TEXT("iphlpapi.dll")))) {
3743 log(EVDNS_LOG_WARN
, "Could not open iphlpapi.dll");
3747 if (!(fn
= (GetNetworkParams_fn_t
) GetProcAddress(handle
, "GetNetworkParams"))) {
3748 log(EVDNS_LOG_WARN
, "Could not get address of function.");
3753 buf
= mm_malloc(size
);
3754 if (!buf
) { status
= 4; goto done
; }
3756 r
= fn(fixed
, &size
);
3757 if (r
!= ERROR_SUCCESS
&& r
!= ERROR_BUFFER_OVERFLOW
) {
3761 if (r
!= ERROR_SUCCESS
) {
3763 buf
= mm_malloc(size
);
3764 if (!buf
) { status
= 4; goto done
; }
3766 r
= fn(fixed
, &size
);
3767 if (r
!= ERROR_SUCCESS
) {
3768 log(EVDNS_LOG_DEBUG
, "fn() failed.");
3774 EVUTIL_ASSERT(fixed
);
3776 ns
= &(fixed
->DnsServerList
);
3778 r
= evdns_nameserver_ip_add_line(base
, ns
->IpAddress
.String
);
3780 log(EVDNS_LOG_DEBUG
,"Could not add nameserver %s to list,error: %d",
3781 (ns
->IpAddress
.String
),(int)GetLastError());
3785 log(EVDNS_LOG_DEBUG
,"Successfully added %s as nameserver",ns
->IpAddress
.String
);
3792 log(EVDNS_LOG_DEBUG
, "No nameservers added.");
3803 FreeLibrary(handle
);
3808 config_nameserver_from_reg_key(struct evdns_base
*base
, HKEY key
, const TCHAR
*subkey
)
3811 DWORD bufsz
= 0, type
= 0;
3814 ASSERT_LOCKED(base
);
3815 if (RegQueryValueEx(key
, subkey
, 0, &type
, NULL
, &bufsz
)
3818 if (!(buf
= mm_malloc(bufsz
)))
3821 if (RegQueryValueEx(key
, subkey
, 0, &type
, (LPBYTE
)buf
, &bufsz
)
3822 == ERROR_SUCCESS
&& bufsz
> 1) {
3823 status
= evdns_nameserver_ip_add_line(base
,buf
);
3830 #define SERVICES_KEY TEXT("System\\CurrentControlSet\\Services\\")
3831 #define WIN_NS_9X_KEY SERVICES_KEY TEXT("VxD\\MSTCP")
3832 #define WIN_NS_NT_KEY SERVICES_KEY TEXT("Tcpip\\Parameters")
3835 load_nameservers_from_registry(struct evdns_base
*base
)
3839 #define TRY(k, name) \
3840 if (!found && config_nameserver_from_reg_key(base,k,TEXT(name)) == 0) { \
3841 log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
3843 } else if (!found) { \
3844 log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
3848 ASSERT_LOCKED(base
);
3850 if (((int)GetVersion()) > 0) { /* NT */
3851 HKEY nt_key
= 0, interfaces_key
= 0;
3853 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, WIN_NS_NT_KEY
, 0,
3854 KEY_READ
, &nt_key
) != ERROR_SUCCESS
) {
3855 log(EVDNS_LOG_DEBUG
,"Couldn't open nt key, %d",(int)GetLastError());
3858 r
= RegOpenKeyEx(nt_key
, TEXT("Interfaces"), 0,
3859 KEY_QUERY_VALUE
|KEY_ENUMERATE_SUB_KEYS
,
3861 if (r
!= ERROR_SUCCESS
) {
3862 log(EVDNS_LOG_DEBUG
,"Couldn't open interfaces key, %d",(int)GetLastError());
3865 TRY(nt_key
, "NameServer");
3866 TRY(nt_key
, "DhcpNameServer");
3867 TRY(interfaces_key
, "NameServer");
3868 TRY(interfaces_key
, "DhcpNameServer");
3869 RegCloseKey(interfaces_key
);
3870 RegCloseKey(nt_key
);
3873 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, WIN_NS_9X_KEY
, 0,
3874 KEY_READ
, &win_key
) != ERROR_SUCCESS
) {
3875 log(EVDNS_LOG_DEBUG
, "Couldn't open registry key, %d", (int)GetLastError());
3878 TRY(win_key
, "NameServer");
3879 RegCloseKey(win_key
);
3883 log(EVDNS_LOG_WARN
,"Didn't find any nameservers.");
3886 return found
? 0 : -1;
3891 evdns_base_config_windows_nameservers(struct evdns_base
*base
)
3896 base
= current_base
;
3900 if (load_nameservers_with_getnetworkparams(base
) == 0) {
3904 r
= load_nameservers_from_registry(base
);
3906 fname
= evdns_get_default_hosts_filename();
3907 evdns_base_load_hosts(base
, fname
);
3916 evdns_config_windows_nameservers(void)
3918 if (!current_base
) {
3919 current_base
= evdns_base_new(NULL
, 1);
3920 return current_base
== NULL
? -1 : 0;
3922 return evdns_base_config_windows_nameservers(current_base
);
3928 evdns_base_new(struct event_base
*event_base
, int initialize_nameservers
)
3930 struct evdns_base
*base
;
3932 if (rand_init_function
!= NULL
&& rand_init_function() < 0) {
3933 log(EVDNS_LOG_WARN
, "Unable to seed random number generator; "
3938 /* Give the evutil library a hook into its evdns-enabled
3939 * functionality. We can't just call evdns_getaddrinfo directly or
3940 * else libevent-core will depend on libevent-extras. */
3941 evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo
);
3943 base
= mm_malloc(sizeof(struct evdns_base
));
3946 memset(base
, 0, sizeof(struct evdns_base
));
3947 base
->req_waiting_head
= NULL
;
3949 EVTHREAD_ALLOC_LOCK(base
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
3952 /* Set max requests inflight and allocate req_heads. */
3953 base
->req_heads
= NULL
;
3955 evdns_base_set_max_requests_inflight(base
, 64);
3957 base
->server_head
= NULL
;
3958 base
->event_base
= event_base
;
3959 base
->global_good_nameservers
= base
->global_requests_inflight
=
3960 base
->global_requests_waiting
= 0;
3962 base
->global_timeout
.tv_sec
= 5;
3963 base
->global_timeout
.tv_usec
= 0;
3964 base
->global_max_reissues
= 1;
3965 base
->global_max_retransmits
= 3;
3966 base
->global_max_nameserver_timeout
= 3;
3967 base
->global_search_state
= NULL
;
3968 base
->global_randomize_case
= 1;
3969 base
->global_getaddrinfo_allow_skew
.tv_sec
= 3;
3970 base
->global_getaddrinfo_allow_skew
.tv_usec
= 0;
3971 base
->global_nameserver_probe_initial_timeout
.tv_sec
= 10;
3972 base
->global_nameserver_probe_initial_timeout
.tv_usec
= 0;
3974 TAILQ_INIT(&base
->hostsdb
);
3976 if (initialize_nameservers
) {
3979 r
= evdns_base_config_windows_nameservers(base
);
3981 r
= evdns_base_resolv_conf_parse(base
, DNS_OPTIONS_ALL
, "/etc/resolv.conf");
3984 evdns_base_free_and_unlock(base
, 0);
3995 struct evdns_base
*base
= evdns_base_new(NULL
, 1);
3997 current_base
= base
;
4005 evdns_err_to_string(int err
)
4008 case DNS_ERR_NONE
: return "no error";
4009 case DNS_ERR_FORMAT
: return "misformatted query";
4010 case DNS_ERR_SERVERFAILED
: return "server failed";
4011 case DNS_ERR_NOTEXIST
: return "name does not exist";
4012 case DNS_ERR_NOTIMPL
: return "query not implemented";
4013 case DNS_ERR_REFUSED
: return "refused";
4015 case DNS_ERR_TRUNCATED
: return "reply truncated or ill-formed";
4016 case DNS_ERR_UNKNOWN
: return "unknown";
4017 case DNS_ERR_TIMEOUT
: return "request timed out";
4018 case DNS_ERR_SHUTDOWN
: return "dns subsystem shut down";
4019 case DNS_ERR_CANCEL
: return "dns request canceled";
4020 case DNS_ERR_NODATA
: return "no records in the reply";
4021 default: return "[Unknown error code]";
4026 evdns_nameserver_free(struct nameserver
*server
)
4028 if (server
->socket
>= 0)
4029 evutil_closesocket(server
->socket
);
4030 (void) event_del(&server
->event
);
4031 event_debug_unassign(&server
->event
);
4032 if (server
->state
== 0)
4033 (void) event_del(&server
->timeout_event
);
4034 if (server
->probe_request
) {
4035 evdns_cancel_request(server
->base
, server
->probe_request
);
4036 server
->probe_request
= NULL
;
4038 event_debug_unassign(&server
->timeout_event
);
4043 evdns_base_free_and_unlock(struct evdns_base
*base
, int fail_requests
)
4045 struct nameserver
*server
, *server_next
;
4046 struct search_domain
*dom
, *dom_next
;
4049 /* Requires that we hold the lock. */
4051 /* TODO(nickm) we might need to refcount here. */
4053 for (server
= base
->server_head
; server
; server
= server_next
) {
4054 server_next
= server
->next
;
4055 evdns_nameserver_free(server
);
4056 if (server_next
== base
->server_head
)
4059 base
->server_head
= NULL
;
4060 base
->global_good_nameservers
= 0;
4062 for (i
= 0; i
< base
->n_req_heads
; ++i
) {
4063 while (base
->req_heads
[i
]) {
4065 reply_schedule_callback(base
->req_heads
[i
], 0, DNS_ERR_SHUTDOWN
, NULL
);
4066 request_finished(base
->req_heads
[i
], &REQ_HEAD(base
, base
->req_heads
[i
]->trans_id
), 1);
4069 while (base
->req_waiting_head
) {
4071 reply_schedule_callback(base
->req_waiting_head
, 0, DNS_ERR_SHUTDOWN
, NULL
);
4072 request_finished(base
->req_waiting_head
, &base
->req_waiting_head
, 1);
4074 base
->global_requests_inflight
= base
->global_requests_waiting
= 0;
4077 if (base
->global_search_state
) {
4078 for (dom
= base
->global_search_state
->head
; dom
; dom
= dom_next
) {
4079 dom_next
= dom
->next
;
4082 mm_free(base
->global_search_state
);
4083 base
->global_search_state
= NULL
;
4087 struct hosts_entry
*victim
;
4088 while ((victim
= TAILQ_FIRST(&base
->hostsdb
))) {
4089 TAILQ_REMOVE(&base
->hostsdb
, victim
, next
);
4094 mm_free(base
->req_heads
);
4097 EVTHREAD_FREE_LOCK(base
->lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
4103 evdns_base_free(struct evdns_base
*base
, int fail_requests
)
4106 evdns_base_free_and_unlock(base
, fail_requests
);
4110 evdns_shutdown(int fail_requests
)
4113 struct evdns_base
*b
= current_base
;
4114 current_base
= NULL
;
4115 evdns_base_free(b
, fail_requests
);
4117 evdns_log_fn
= NULL
;
4121 evdns_base_parse_hosts_line(struct evdns_base
*base
, char *line
)
4124 static const char *const delims
= " \t";
4125 char *const addr
= strtok_r(line
, delims
, &strtok_state
);
4126 char *hostname
, *hash
;
4127 struct sockaddr_storage ss
;
4128 int socklen
= sizeof(ss
);
4129 ASSERT_LOCKED(base
);
4131 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
4133 if (!addr
|| *addr
== '#')
4136 memset(&ss
, 0, sizeof(ss
));
4137 if (evutil_parse_sockaddr_port(addr
, (struct sockaddr
*)&ss
, &socklen
)<0)
4139 if (socklen
> (int)sizeof(struct sockaddr_in6
))
4142 if (sockaddr_getport((struct sockaddr
*)&ss
))
4145 while ((hostname
= NEXT_TOKEN
)) {
4146 struct hosts_entry
*he
;
4148 if ((hash
= strchr(hostname
, '#'))) {
4149 if (hash
== hostname
)
4154 namelen
= strlen(hostname
);
4156 he
= mm_calloc(1, sizeof(struct hosts_entry
)+namelen
);
4159 EVUTIL_ASSERT(socklen
<= (int)sizeof(he
->addr
));
4160 memcpy(&he
->addr
, &ss
, socklen
);
4161 memcpy(he
->hostname
, hostname
, namelen
+1);
4162 he
->addrlen
= socklen
;
4164 TAILQ_INSERT_TAIL(&base
->hostsdb
, he
, next
);
4175 evdns_base_load_hosts_impl(struct evdns_base
*base
, const char *hosts_fname
)
4177 char *str
=NULL
, *cp
, *eol
;
4181 ASSERT_LOCKED(base
);
4183 if (hosts_fname
== NULL
||
4184 (err
= evutil_read_file(hosts_fname
, &str
, &len
, 0)) < 0) {
4186 strlcpy(tmp
, "127.0.0.1 localhost", sizeof(tmp
));
4187 evdns_base_parse_hosts_line(base
, tmp
);
4188 strlcpy(tmp
, "::1 localhost", sizeof(tmp
));
4189 evdns_base_parse_hosts_line(base
, tmp
);
4190 return err
? -1 : 0;
4193 /* This will break early if there is a NUL in the hosts file.
4194 * Probably not a problem.*/
4197 eol
= strchr(cp
, '\n');
4201 evdns_base_parse_hosts_line(base
, cp
);
4204 evdns_base_parse_hosts_line(base
, cp
);
4214 evdns_base_load_hosts(struct evdns_base
*base
, const char *hosts_fname
)
4218 base
= current_base
;
4220 res
= evdns_base_load_hosts_impl(base
, hosts_fname
);
4225 /* A single request for a getaddrinfo, either v4 or v6. */
4226 struct getaddrinfo_subrequest
{
4227 struct evdns_request
*r
;
4231 /* State data used to implement an in-progress getaddrinfo. */
4232 struct evdns_getaddrinfo_request
{
4233 struct evdns_base
*evdns_base
;
4234 /* Copy of the modified 'hints' data that we'll use to build
4236 struct evutil_addrinfo hints
;
4237 /* The callback to invoke when we're done */
4238 evdns_getaddrinfo_cb user_cb
;
4239 /* User-supplied data to give to the callback. */
4241 /* The port to use when building sockaddrs. */
4243 /* The sub_request for an A record (if any) */
4244 struct getaddrinfo_subrequest ipv4_request
;
4245 /* The sub_request for an AAAA record (if any) */
4246 struct getaddrinfo_subrequest ipv6_request
;
4248 /* The cname result that we were told (if any) */
4251 /* If we have one request answered and one request still inflight,
4252 * then this field holds the answer from the first request... */
4253 struct evutil_addrinfo
*pending_result
;
4254 /* And this event is a timeout that will tell us to cancel the second
4255 * request if it's taking a long time. */
4256 struct event timeout
;
4258 /* And this field holds the error code from the first request... */
4260 /* If this is set, the user canceled this request. */
4261 unsigned user_canceled
: 1;
4262 /* If this is set, the user can no longer cancel this request; we're
4263 * just waiting for the free. */
4264 unsigned request_done
: 1;
4267 /* Convert an evdns errors to the equivalent getaddrinfo error. */
4269 evdns_err_to_getaddrinfo_err(int e1
)
4271 /* XXX Do this better! */
4272 if (e1
== DNS_ERR_NONE
)
4274 else if (e1
== DNS_ERR_NOTEXIST
)
4275 return EVUTIL_EAI_NONAME
;
4277 return EVUTIL_EAI_FAIL
;
4280 /* Return the more informative of two getaddrinfo errors. */
4282 getaddrinfo_merge_err(int e1
, int e2
)
4284 /* XXXX be cleverer here. */
4292 free_getaddrinfo_request(struct evdns_getaddrinfo_request
*data
)
4294 /* DO NOT CALL this if either of the requests is pending. Only once
4295 * both callbacks have been invoked is it safe to free the request */
4296 if (data
->pending_result
)
4297 evutil_freeaddrinfo(data
->pending_result
);
4298 if (data
->cname_result
)
4299 mm_free(data
->cname_result
);
4300 event_del(&data
->timeout
);
4306 add_cname_to_reply(struct evdns_getaddrinfo_request
*data
,
4307 struct evutil_addrinfo
*ai
)
4309 if (data
->cname_result
&& ai
) {
4310 ai
->ai_canonname
= data
->cname_result
;
4311 data
->cname_result
= NULL
;
4315 /* Callback: invoked when one request in a mixed-format A/AAAA getaddrinfo
4316 * request has finished, but the other one took too long to answer. Pass
4317 * along the answer we got, and cancel the other request.
4320 evdns_getaddrinfo_timeout_cb(evutil_socket_t fd
, short what
, void *ptr
)
4322 int v4_timedout
= 0, v6_timedout
= 0;
4323 struct evdns_getaddrinfo_request
*data
= ptr
;
4325 /* Cancel any pending requests, and note which one */
4326 if (data
->ipv4_request
.r
) {
4327 /* XXXX This does nothing if the request's callback is already
4328 * running (pending_cb is set). */
4329 evdns_cancel_request(NULL
, data
->ipv4_request
.r
);
4331 EVDNS_LOCK(data
->evdns_base
);
4332 ++data
->evdns_base
->getaddrinfo_ipv4_timeouts
;
4333 EVDNS_UNLOCK(data
->evdns_base
);
4335 if (data
->ipv6_request
.r
) {
4336 /* XXXX This does nothing if the request's callback is already
4337 * running (pending_cb is set). */
4338 evdns_cancel_request(NULL
, data
->ipv6_request
.r
);
4340 EVDNS_LOCK(data
->evdns_base
);
4341 ++data
->evdns_base
->getaddrinfo_ipv6_timeouts
;
4342 EVDNS_UNLOCK(data
->evdns_base
);
4345 /* We only use this timeout callback when we have an answer for
4347 EVUTIL_ASSERT(!v4_timedout
|| !v6_timedout
);
4349 /* Report the outcome of the other request that didn't time out. */
4350 if (data
->pending_result
) {
4351 add_cname_to_reply(data
, data
->pending_result
);
4352 data
->user_cb(0, data
->pending_result
, data
->user_data
);
4353 data
->pending_result
= NULL
;
4355 int e
= data
->pending_error
;
4357 e
= EVUTIL_EAI_AGAIN
;
4358 data
->user_cb(e
, NULL
, data
->user_data
);
4361 data
->user_cb
= NULL
; /* prevent double-call if evdns callbacks are
4362 * in-progress. XXXX It would be better if this
4363 * weren't necessary. */
4365 if (!v4_timedout
&& !v6_timedout
) {
4366 /* should be impossible? XXXX */
4367 free_getaddrinfo_request(data
);
4372 evdns_getaddrinfo_set_timeout(struct evdns_base
*evdns_base
,
4373 struct evdns_getaddrinfo_request
*data
)
4375 return event_add(&data
->timeout
, &evdns_base
->global_getaddrinfo_allow_skew
);
4379 evdns_result_is_answer(int result
)
4381 return (result
!= DNS_ERR_NOTIMPL
&& result
!= DNS_ERR_REFUSED
&&
4382 result
!= DNS_ERR_SERVERFAILED
&& result
!= DNS_ERR_CANCEL
);
4386 evdns_getaddrinfo_gotresolve(int result
, char type
, int count
,
4387 int ttl
, void *addresses
, void *arg
)
4390 struct getaddrinfo_subrequest
*req
= arg
;
4391 struct getaddrinfo_subrequest
*other_req
;
4392 struct evdns_getaddrinfo_request
*data
;
4394 struct evutil_addrinfo
*res
;
4396 struct sockaddr_in sin
;
4397 struct sockaddr_in6 sin6
;
4398 struct sockaddr
*sa
;
4399 int socklen
, addrlen
;
4404 EVUTIL_ASSERT(req
->type
== DNS_IPv4_A
|| req
->type
== DNS_IPv6_AAAA
);
4405 if (req
->type
== DNS_IPv4_A
) {
4406 data
= EVUTIL_UPCAST(req
, struct evdns_getaddrinfo_request
, ipv4_request
);
4407 other_req
= &data
->ipv6_request
;
4409 data
= EVUTIL_UPCAST(req
, struct evdns_getaddrinfo_request
, ipv6_request
);
4410 other_req
= &data
->ipv4_request
;
4413 EVDNS_LOCK(data
->evdns_base
);
4414 if (evdns_result_is_answer(result
)) {
4415 if (req
->type
== DNS_IPv4_A
)
4416 ++data
->evdns_base
->getaddrinfo_ipv4_answered
;
4418 ++data
->evdns_base
->getaddrinfo_ipv6_answered
;
4420 user_canceled
= data
->user_canceled
;
4421 if (other_req
->r
== NULL
)
4422 data
->request_done
= 1;
4423 EVDNS_UNLOCK(data
->evdns_base
);
4427 if (result
== DNS_ERR_CANCEL
&& ! user_canceled
) {
4428 /* Internal cancel request from timeout or internal error.
4429 * we already answered the user. */
4430 if (other_req
->r
== NULL
)
4431 free_getaddrinfo_request(data
);
4435 if (data
->user_cb
== NULL
) {
4436 /* We already answered. XXXX This shouldn't be needed; see
4437 * comments in evdns_getaddrinfo_timeout_cb */
4438 free_getaddrinfo_request(data
);
4442 if (result
== DNS_ERR_NONE
) {
4444 err
= EVUTIL_EAI_NODATA
;
4448 err
= evdns_err_to_getaddrinfo_err(result
);
4452 /* Looks like we got an error. */
4454 /* The other request is still working; maybe it will
4456 /* XXXX handle failure from set_timeout */
4457 evdns_getaddrinfo_set_timeout(data
->evdns_base
, data
);
4458 data
->pending_error
= err
;
4462 if (user_canceled
) {
4463 data
->user_cb(EVUTIL_EAI_CANCEL
, NULL
, data
->user_data
);
4464 } else if (data
->pending_result
) {
4465 /* If we have an answer waiting, and we weren't
4466 * canceled, ignore this error. */
4467 add_cname_to_reply(data
, data
->pending_result
);
4468 data
->user_cb(0, data
->pending_result
, data
->user_data
);
4469 data
->pending_result
= NULL
;
4471 if (data
->pending_error
)
4472 err
= getaddrinfo_merge_err(err
,
4473 data
->pending_error
);
4474 data
->user_cb(err
, NULL
, data
->user_data
);
4476 free_getaddrinfo_request(data
);
4478 } else if (user_canceled
) {
4480 /* The other request is still working; let it hit this
4481 * callback with EVUTIL_EAI_CANCEL callback and report
4485 data
->user_cb(EVUTIL_EAI_CANCEL
, NULL
, data
->user_data
);
4486 free_getaddrinfo_request(data
);
4490 /* Looks like we got some answers. We should turn them into addrinfos
4491 * and then either queue those or return them all. */
4492 EVUTIL_ASSERT(type
== DNS_IPv4_A
|| type
== DNS_IPv6_AAAA
);
4494 if (type
== DNS_IPv4_A
) {
4495 memset(&sin
, 0, sizeof(sin
));
4496 sin
.sin_family
= AF_INET
;
4497 sin
.sin_port
= htons(data
->port
);
4499 sa
= (struct sockaddr
*)&sin
;
4500 socklen
= sizeof(sin
);
4502 addrp
= &sin
.sin_addr
.s_addr
;
4504 memset(&sin6
, 0, sizeof(sin6
));
4505 sin6
.sin6_family
= AF_INET6
;
4506 sin6
.sin6_port
= htons(data
->port
);
4508 sa
= (struct sockaddr
*)&sin6
;
4509 socklen
= sizeof(sin6
);
4511 addrp
= &sin6
.sin6_addr
.s6_addr
;
4515 for (i
=0; i
< count
; ++i
) {
4516 struct evutil_addrinfo
*ai
;
4517 memcpy(addrp
, ((char*)addresses
)+i
*addrlen
, addrlen
);
4518 ai
= evutil_new_addrinfo(sa
, socklen
, &data
->hints
);
4521 evdns_cancel_request(NULL
, other_req
->r
);
4523 data
->user_cb(EVUTIL_EAI_MEMORY
, NULL
, data
->user_data
);
4525 evutil_freeaddrinfo(res
);
4527 if (other_req
->r
== NULL
)
4528 free_getaddrinfo_request(data
);
4531 res
= evutil_addrinfo_append(res
, ai
);
4535 /* The other request is still in progress; wait for it */
4536 /* XXXX handle failure from set_timeout */
4537 evdns_getaddrinfo_set_timeout(data
->evdns_base
, data
);
4538 data
->pending_result
= res
;
4541 /* The other request is done or never started; append its
4542 * results (if any) and return them. */
4543 if (data
->pending_result
) {
4544 if (req
->type
== DNS_IPv4_A
)
4545 res
= evutil_addrinfo_append(res
,
4546 data
->pending_result
);
4548 res
= evutil_addrinfo_append(
4549 data
->pending_result
, res
);
4550 data
->pending_result
= NULL
;
4553 /* Call the user callback. */
4554 add_cname_to_reply(data
, res
);
4555 data
->user_cb(0, res
, data
->user_data
);
4558 free_getaddrinfo_request(data
);
4562 static struct hosts_entry
*
4563 find_hosts_entry(struct evdns_base
*base
, const char *hostname
,
4564 struct hosts_entry
*find_after
)
4566 struct hosts_entry
*e
;
4569 e
= TAILQ_NEXT(find_after
, next
);
4571 e
= TAILQ_FIRST(&base
->hostsdb
);
4573 for (; e
; e
= TAILQ_NEXT(e
, next
)) {
4574 if (!evutil_ascii_strcasecmp(e
->hostname
, hostname
))
4581 evdns_getaddrinfo_fromhosts(struct evdns_base
*base
,
4582 const char *nodename
, struct evutil_addrinfo
*hints
, ev_uint16_t port
,
4583 struct evutil_addrinfo
**res
)
4586 struct hosts_entry
*e
;
4587 struct evutil_addrinfo
*ai
=NULL
;
4588 int f
= hints
->ai_family
;
4591 for (e
= find_hosts_entry(base
, nodename
, NULL
); e
;
4592 e
= find_hosts_entry(base
, nodename
, e
)) {
4593 struct evutil_addrinfo
*ai_new
;
4595 if ((e
->addr
.sa
.sa_family
== AF_INET
&& f
== PF_INET6
) ||
4596 (e
->addr
.sa
.sa_family
== AF_INET6
&& f
== PF_INET
))
4598 ai_new
= evutil_new_addrinfo(&e
->addr
.sa
, e
->addrlen
, hints
);
4603 sockaddr_setport(ai_new
->ai_addr
, port
);
4604 ai
= evutil_addrinfo_append(ai
, ai_new
);
4609 /* Note that we return an empty answer if we found entries for
4610 * this hostname but none were of the right address type. */
4615 evutil_freeaddrinfo(ai
);
4620 struct evdns_getaddrinfo_request
*
4621 evdns_getaddrinfo(struct evdns_base
*dns_base
,
4622 const char *nodename
, const char *servname
,
4623 const struct evutil_addrinfo
*hints_in
,
4624 evdns_getaddrinfo_cb cb
, void *arg
)
4626 struct evdns_getaddrinfo_request
*data
;
4627 struct evutil_addrinfo hints
;
4628 struct evutil_addrinfo
*res
= NULL
;
4634 dns_base
= current_base
;
4637 "Call to getaddrinfo_async with no "
4638 "evdns_base configured.");
4639 cb(EVUTIL_EAI_FAIL
, NULL
, arg
); /* ??? better error? */
4644 /* If we _must_ answer this immediately, do so. */
4645 if ((hints_in
&& (hints_in
->ai_flags
& EVUTIL_AI_NUMERICHOST
))) {
4647 err
= evutil_getaddrinfo(nodename
, servname
, hints_in
, &res
);
4653 memcpy(&hints
, hints_in
, sizeof(hints
));
4655 memset(&hints
, 0, sizeof(hints
));
4656 hints
.ai_family
= PF_UNSPEC
;
4659 evutil_adjust_hints_for_addrconfig(&hints
);
4661 /* Now try to see if we _can_ answer immediately. */
4662 /* (It would be nice to do this by calling getaddrinfo directly, with
4663 * AI_NUMERICHOST, on plaforms that have it, but we can't: there isn't
4664 * a reliable way to distinguish the "that wasn't a numeric host!" case
4665 * from any other EAI_NONAME cases.) */
4666 err
= evutil_getaddrinfo_common(nodename
, servname
, &hints
, &res
, &port
);
4667 if (err
!= EVUTIL_EAI_NEED_RESOLVE
) {
4672 /* If there is an entry in the hosts file, we should give it now. */
4673 if (!evdns_getaddrinfo_fromhosts(dns_base
, nodename
, &hints
, port
, &res
)) {
4678 /* Okay, things are serious now. We're going to need to actually
4681 data
= mm_calloc(1,sizeof(struct evdns_getaddrinfo_request
));
4683 cb(EVUTIL_EAI_MEMORY
, NULL
, arg
);
4687 memcpy(&data
->hints
, &hints
, sizeof(data
->hints
));
4688 data
->port
= (ev_uint16_t
)port
;
4689 data
->ipv4_request
.type
= DNS_IPv4_A
;
4690 data
->ipv6_request
.type
= DNS_IPv6_AAAA
;
4692 data
->user_data
= arg
;
4693 data
->evdns_base
= dns_base
;
4695 want_cname
= (hints
.ai_flags
& EVUTIL_AI_CANONNAME
);
4697 /* If we are asked for a PF_UNSPEC address, we launch two requests in
4698 * parallel: one for an A address and one for an AAAA address. We
4699 * can't send just one request, since many servers only answer one
4700 * question per DNS request.
4702 * Once we have the answer to one request, we allow for a short
4703 * timeout before we report it, to see if the other one arrives. If
4704 * they both show up in time, then we report both the answers.
4706 * If too many addresses of one type time out or fail, we should stop
4707 * launching those requests. (XXX we don't do that yet.)
4710 if (hints
.ai_family
!= PF_INET6
) {
4711 log(EVDNS_LOG_DEBUG
, "Sending request for %s on ipv4 as %p",
4712 nodename
, &data
->ipv4_request
);
4714 data
->ipv4_request
.r
= evdns_base_resolve_ipv4(dns_base
,
4715 nodename
, 0, evdns_getaddrinfo_gotresolve
,
4716 &data
->ipv4_request
);
4718 data
->ipv4_request
.r
->current_req
->put_cname_in_ptr
=
4719 &data
->cname_result
;
4721 if (hints
.ai_family
!= PF_INET
) {
4722 log(EVDNS_LOG_DEBUG
, "Sending request for %s on ipv6 as %p",
4723 nodename
, &data
->ipv6_request
);
4725 data
->ipv6_request
.r
= evdns_base_resolve_ipv6(dns_base
,
4726 nodename
, 0, evdns_getaddrinfo_gotresolve
,
4727 &data
->ipv6_request
);
4729 data
->ipv6_request
.r
->current_req
->put_cname_in_ptr
=
4730 &data
->cname_result
;
4733 evtimer_assign(&data
->timeout
, dns_base
->event_base
,
4734 evdns_getaddrinfo_timeout_cb
, data
);
4736 if (data
->ipv4_request
.r
|| data
->ipv6_request
.r
) {
4740 cb(EVUTIL_EAI_FAIL
, NULL
, arg
);
4746 evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request
*data
)
4748 EVDNS_LOCK(data
->evdns_base
);
4749 if (data
->request_done
) {
4750 EVDNS_UNLOCK(data
->evdns_base
);
4753 event_del(&data
->timeout
);
4754 data
->user_canceled
= 1;
4755 if (data
->ipv4_request
.r
)
4756 evdns_cancel_request(data
->evdns_base
, data
->ipv4_request
.r
);
4757 if (data
->ipv6_request
.r
)
4758 evdns_cancel_request(data
->evdns_base
, data
->ipv6_request
.r
);
4759 EVDNS_UNLOCK(data
->evdns_base
);