Coverage Report

Created: 2026-07-16 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libevent/evdns.c
Line
Count
Source
1
/* Copyright 2006-2007 Niels Provos
2
 * Copyright 2007-2012 Nick Mathewson and Niels Provos
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 * 1. Redistributions of source code must retain the above copyright
8
 *    notice, this list of conditions and the following disclaimer.
9
 * 2. Redistributions in binary form must reproduce the above copyright
10
 *    notice, this list of conditions and the following disclaimer in the
11
 *    documentation and/or other materials provided with the distribution.
12
 * 3. The name of the author may not be used to endorse or promote products
13
 *    derived from this software without specific prior written permission.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
/* Based on software by Adam Langley. Adam's original message:
28
 *
29
 * Async DNS Library
30
 * Adam Langley <agl@imperialviolet.org>
31
 * Public Domain code
32
 *
33
 * This software is Public Domain. To view a copy of the public domain dedication,
34
 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
35
 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
36
 *
37
 * I ask and expect, but do not require, that all derivative works contain an
38
 * attribution similar to:
39
 *  Parts developed by Adam Langley <agl@imperialviolet.org>
40
 *
41
 * You may wish to replace the word "Parts" with something else depending on
42
 * the amount of original code.
43
 *
44
 * (Derivative works does not include programs which link against, run or include
45
 * the source verbatim in their source distributions)
46
 *
47
 * Version: 0.1b
48
 */
49
50
#include "event2/event-config.h"
51
#include "evconfig-private.h"
52
53
#include <sys/types.h>
54
55
#ifndef _FORTIFY_SOURCE
56
#define _FORTIFY_SOURCE 3
57
#endif
58
59
#include <string.h>
60
#include <fcntl.h>
61
#ifdef EVENT__HAVE_SYS_TIME_H
62
#include <sys/time.h>
63
#endif
64
#ifdef EVENT__HAVE_STDINT_H
65
#include <stdint.h>
66
#endif
67
#include <stdlib.h>
68
#include <string.h>
69
#include <errno.h>
70
#ifdef EVENT__HAVE_UNISTD_H
71
#include <unistd.h>
72
#endif
73
#include <limits.h>
74
#include <sys/stat.h>
75
#include <stdio.h>
76
#include <stdarg.h>
77
#include <sys/tree.h>
78
#ifdef _WIN32
79
#include <winsock2.h>
80
#include <winerror.h>
81
#include <ws2tcpip.h>
82
#ifndef _WIN32_IE
83
#define _WIN32_IE 0x400
84
#endif
85
#include <shlobj.h>
86
#define strcasecmp strcmpi
87
#endif
88
89
#include "event2/buffer.h"
90
#include "event2/bufferevent.h"
91
#include "event2/dns.h"
92
#include "event2/dns_struct.h"
93
#include "event2/dns_compat.h"
94
#include "event2/util.h"
95
#include "event2/event.h"
96
#include "event2/event_struct.h"
97
#include "event2/listener.h"
98
#include "event2/thread.h"
99
100
#include "defer-internal.h"
101
#include "log-internal.h"
102
#include "mm-internal.h"
103
#include "strlcpy-internal.h"
104
#include "ipv6-internal.h"
105
#include "util-internal.h"
106
#include "evthread-internal.h"
107
#include "evdns-internal.h"
108
#ifdef _WIN32
109
#include <ctype.h>
110
#include <winsock2.h>
111
#include <windows.h>
112
#include <iphlpapi.h>
113
#include <io.h>
114
#else
115
#include <sys/socket.h>
116
#include <netinet/in.h>
117
#include <arpa/inet.h>
118
#endif
119
120
#ifdef EVENT__HAVE_NETINET_IN6_H
121
#include <netinet/in6.h>
122
#endif
123
124
0
#define EVDNS_LOG_DEBUG EVENT_LOG_DEBUG
125
0
#define EVDNS_LOG_WARN EVENT_LOG_WARN
126
0
#define EVDNS_LOG_MSG EVENT_LOG_MSG
127
128
#ifndef EVDNS_NAME_MAX
129
#define EVDNS_NAME_MAX 255
130
#endif
131
132
#include <stdio.h>
133
134
#undef MIN
135
#undef MAX
136
0
#define MIN(a,b) ((a)<(b)?(a):(b))
137
0
#define MAX(a,b) ((a)>(b)?(a):(b))
138
139
#define ASSERT_VALID_REQUEST(req) \
140
0
  EVUTIL_ASSERT((req)->handle && (req)->handle->current_req == (req))
141
142
#define u64 ev_uint64_t
143
0
#define u32 ev_uint32_t
144
0
#define u16 ev_uint16_t
145
0
#define u8  ev_uint8_t
146
147
/* maximum number of addresses from a single packet */
148
/* that we bother recording */
149
#define MAX_V4_ADDRS 32
150
#define MAX_V6_ADDRS 32
151
152
/* Maximum allowable size of a DNS message over UDP without EDNS.*/
153
0
#define DNS_MAX_UDP_SIZE 512
154
/* Maximum allowable size of a DNS message over UDP with EDNS.*/
155
0
#define EDNS_MAX_UDP_SIZE 65535
156
157
#define EDNS_ENABLED(base) \
158
0
  (((base)->global_max_udp_size) > DNS_MAX_UDP_SIZE)
159
160
0
#define TYPE_A         EVDNS_TYPE_A
161
0
#define TYPE_CNAME     5
162
0
#define TYPE_PTR       EVDNS_TYPE_PTR
163
0
#define TYPE_SOA       EVDNS_TYPE_SOA
164
0
#define TYPE_AAAA      EVDNS_TYPE_AAAA
165
0
#define TYPE_OPT       41
166
167
0
#define CLASS_INET     EVDNS_CLASS_INET
168
169
/* Timeout in seconds for idle TCP connections that server keeps alive. */
170
0
#define SERVER_IDLE_CONN_TIMEOUT 10
171
/* Timeout in seconds for idle TCP connections that client keeps alive. */
172
0
#define CLIENT_IDLE_CONN_TIMEOUT 5
173
/* Default maximum number of simultaneous TCP client connections that DNS server can hold. */
174
0
#define MAX_CLIENT_CONNECTIONS 10
175
176
struct reply {
177
  unsigned int type;
178
  unsigned int have_answer : 1;
179
  u32 rr_count;
180
  union {
181
    u32 *a;
182
    struct in6_addr *aaaa;
183
    char *ptr_name;
184
    void *raw;
185
  } data;
186
  char *cname;
187
};
188
189
190
/* Persistent handle.  We keep this separate from 'struct request' since we
191
 * need some object to last for as long as an evdns_request is outstanding so
192
 * that it can be canceled, whereas a search request can lead to multiple
193
 * 'struct request' instances being created over its lifetime. */
194
struct evdns_request {
195
  struct request *current_req;
196
  struct evdns_base *base;
197
198
  int pending_cb; /* Waiting for its callback to be invoked; not
199
       * owned by event base any more. */
200
201
  /* data used when fulfilling the callback */
202
  struct event_callback deferred;
203
  evdns_callback_type user_callback;
204
  void *user_pointer;
205
  u8 request_type;
206
  u8 have_reply;
207
  u32 ttl;
208
  u32 err;
209
  struct reply reply;
210
211
  /* elements used by the searching code */
212
  int search_index;
213
  struct search_state *search_state;
214
  char *search_origname;  /* needs to be free()ed */
215
  int search_flags;
216
  u16 tcp_flags;
217
};
218
219
struct request {
220
  u8 *request;  /* the dns packet data */
221
  u16 request_size; /* size of memory block stored in request field */
222
  u8 request_type; /* TYPE_PTR or TYPE_A or TYPE_AAAA */
223
  unsigned int request_len;
224
  int reissue_count;
225
  int tx_count;  /* the number of times that this packet has been sent */
226
  struct nameserver *ns;  /* the server which we last sent it */
227
228
  /* these objects are kept in a circular list */
229
  /* XXX We could turn this into a CIRCLEQ. */
230
  struct request *next, *prev;
231
232
  struct event timeout_event;
233
234
  u16 trans_id;  /* the transaction id */
235
  unsigned request_appended :1; /* true if the request pointer is data which follows this struct */
236
  unsigned transmit_me :1;  /* needs to be transmitted */
237
  unsigned need_cname :1;   /* make a separate callback for CNAME */
238
239
  /* XXXX This is a horrible hack. */
240
  char **put_cname_in_ptr; /* store the cname here if we get one. */
241
242
  struct evdns_base *base;
243
244
  struct evdns_request *handle;
245
};
246
247
enum tcp_state {
248
  TS_DISCONNECTED,
249
  TS_CONNECTING,
250
  TS_CONNECTED
251
};
252
253
struct tcp_connection {
254
  struct bufferevent *bev;
255
  enum tcp_state state;
256
  u16 awaiting_packet_size;
257
};
258
259
struct evdns_server_port;
260
261
struct client_tcp_connection {
262
  LIST_ENTRY(client_tcp_connection) next;
263
  struct tcp_connection connection;
264
  struct evdns_server_port *port;
265
};
266
267
struct nameserver {
268
  evutil_socket_t socket;  /* a connected UDP socket */
269
  struct tcp_connection *connection; /* intended for TCP support */
270
  struct sockaddr_storage address;
271
  ev_socklen_t addrlen;
272
  int failed_times;  /* number of times which we have given this server a chance */
273
  int timedout;  /* number of times in a row a request has timed out */
274
  struct event event;
275
  /* these objects are kept in a circular list */
276
  struct nameserver *next, *prev;
277
  struct event timeout_event;  /* used to keep the timeout for */
278
             /* when we next probe this server. */
279
             /* Valid if state == 0 */
280
  /* Outstanding probe request for this nameserver, if any */
281
  struct evdns_request *probe_request;
282
  char state;  /* zero if we think that this server is down */
283
  char choked;  /* true if we have an EAGAIN from this server's socket */
284
  char write_waiting;  /* true if we are waiting for EV_WRITE events */
285
  struct evdns_base *base;
286
287
  /* Number of currently inflight requests: used
288
   * to track when we should add/del the event. */
289
  int requests_inflight;
290
};
291
292
293
/* Represents a local port where we're listening for DNS requests. */
294
struct evdns_server_port {
295
  evutil_socket_t socket; /* socket we use to read queries and write replies. */
296
  int refcnt; /* reference count. */
297
  char choked; /* Are we currently blocked from writing? */
298
  char closing; /* Are we trying to close this port, pending writes? */
299
  evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
300
  void *user_data; /* Opaque pointer passed to user_callback */
301
  struct event event; /* Read/write event */
302
  /* circular list of replies that we want to write. */
303
  struct server_request *pending_replies;
304
  struct event_base *event_base;
305
306
  /* Structures for tcp support */
307
  struct evconnlistener *listener;
308
  LIST_HEAD(client_list, client_tcp_connection) client_connections;
309
  unsigned client_connections_count;
310
  unsigned max_client_connections;
311
  struct timeval tcp_idle_timeout;
312
313
#ifndef EVENT__DISABLE_THREAD_SUPPORT
314
  void *lock;
315
#endif
316
};
317
318
/* Represents part of a reply being built.  (That is, a single RR.) */
319
struct server_reply_item {
320
  struct server_reply_item *next; /* next item in sequence. */
321
  char *name; /* name part of the RR */
322
  u16 type; /* The RR type */
323
  u16 class; /* The RR class (usually CLASS_INET) */
324
  u32 ttl; /* The RR TTL */
325
  char is_name; /* True iff data is a label */
326
  u16 datalen; /* Length of data; -1 if data is a label */
327
  void *data; /* The contents of the RR */
328
};
329
330
/* Represents a request that we've received as a DNS server, and holds */
331
/* the components of the reply as we're constructing it. */
332
struct server_request {
333
  /* Pointers to the next and previous entries on the list of replies */
334
  /* that we're waiting to write.  Only set if we have tried to respond */
335
  /* and gotten EAGAIN. */
336
  struct server_request *next_pending;
337
  struct server_request *prev_pending;
338
339
  u16 trans_id; /* Transaction id. */
340
  struct evdns_server_port *port; /* Which port received this request on? */
341
  struct client_tcp_connection *client; /* Equal to NULL in case of UDP connection. */
342
  struct sockaddr_storage addr; /* Where to send the response in case of UDP. Equal to NULL in case of TCP connection.*/
343
  ev_socklen_t addrlen; /* length of addr */
344
  u16 max_udp_reply_size; /* Maximum size of udp reply that client can handle. */
345
346
  int n_answer; /* how many answer RRs have been set? */
347
  int n_authority; /* how many authority RRs have been set? */
348
  int n_additional; /* how many additional RRs have been set? */
349
350
  struct server_reply_item *answer; /* linked list of answer RRs */
351
  struct server_reply_item *authority; /* linked list of authority RRs */
352
  struct server_reply_item *additional; /* linked list of additional RRs */
353
354
  /* Constructed response.  Only set once we're ready to send a reply. */
355
  /* Once this is set, the RR fields are cleared, and no more should be set. */
356
  char *response;
357
  size_t response_len;
358
359
  /* Caller-visible fields: flags, questions. */
360
  struct evdns_server_request base;
361
};
362
363
struct evdns_base {
364
  /* An array of n_req_heads circular lists for inflight requests.
365
   * Each inflight request req is in req_heads[req->trans_id % n_req_heads].
366
   */
367
  struct request **req_heads;
368
  /* A circular list of requests that we're waiting to send, but haven't
369
   * sent yet because there are too many requests inflight */
370
  struct request *req_waiting_head;
371
  /* A circular list of nameservers. */
372
  struct nameserver *server_head;
373
  int n_req_heads;
374
375
  struct event_base *event_base;
376
377
  /* The number of good nameservers that we have */
378
  int global_good_nameservers;
379
380
  /* inflight requests are contained in the req_head list */
381
  /* and are actually going out across the network */
382
  int global_requests_inflight;
383
  /* requests which aren't inflight are in the waiting list */
384
  /* and are counted here */
385
  int global_requests_waiting;
386
387
  int global_max_requests_inflight;
388
389
  struct timeval global_timeout;  /* 5 seconds by default */
390
  int global_max_reissues;  /* a reissue occurs when we get some errors from the server */
391
  int global_max_retransmits;  /* number of times we'll retransmit a request which timed out */
392
  /* number of timeouts in a row before we consider this server to be down */
393
  int global_max_nameserver_timeout;
394
  /* true iff we will use the 0x20 hack to prevent poisoning attacks. */
395
  int global_randomize_case;
396
  /* Maximum size of a UDP DNS packet. */
397
  u16 global_max_udp_size;
398
399
  /* The first time that a nameserver fails, how long do we wait before
400
   * probing to see if it has returned?  */
401
  struct timeval global_nameserver_probe_initial_timeout;
402
403
  /* Combination of DNS_QUERY_USEVC, DNS_QUERY_IGNTC flags
404
   * to control requests via TCP. */
405
  u16 global_tcp_flags;
406
  /* Idle timeout for outgoing TCP connections. */
407
  struct timeval global_tcp_idle_timeout;
408
409
  /** Port to bind to for outgoing DNS packets. */
410
  struct sockaddr_storage global_outgoing_address;
411
  /** ev_socklen_t for global_outgoing_address. 0 if it isn't set. */
412
  ev_socklen_t global_outgoing_addrlen;
413
414
  struct timeval global_getaddrinfo_allow_skew;
415
416
  int so_rcvbuf;
417
  int so_sndbuf;
418
419
  int getaddrinfo_ipv4_timeouts;
420
  int getaddrinfo_ipv6_timeouts;
421
  int getaddrinfo_ipv4_answered;
422
  int getaddrinfo_ipv6_answered;
423
424
  struct search_state *global_search_state;
425
426
  TAILQ_HEAD(hosts_list, hosts_entry) hostsdb;
427
428
  SPLAY_HEAD(evdns_tree, evdns_cache) cache_root;
429
430
#ifndef EVENT__DISABLE_THREAD_SUPPORT
431
  void *lock;
432
#endif
433
434
  int disable_when_inactive;
435
  int disable_cache;
436
437
  /* Maximum timeout between two probe packets
438
   * will change `global_nameserver_probe_initial_timeout`
439
   * when this value is smaller */
440
  int ns_max_probe_timeout;
441
  /* Backoff factor of probe timeout */
442
  int ns_timeout_backoff_factor;
443
};
444
445
struct hosts_entry {
446
  TAILQ_ENTRY(hosts_entry) next;
447
  union {
448
    struct sockaddr sa;
449
    struct sockaddr_in sin;
450
    struct sockaddr_in6 sin6;
451
  } addr;
452
  int addrlen;
453
  char hostname[1];
454
};
455
456
struct evdns_cache {
457
  SPLAY_ENTRY(evdns_cache) node;
458
  char *name;
459
  struct evutil_addrinfo *ai;
460
  struct event ev_timeout;
461
  struct evdns_base *base;
462
};
463
464
static struct evdns_base *current_base = NULL;
465
466
struct evdns_base *
467
evdns_get_global_base(void)
468
0
{
469
0
  return current_base;
470
0
}
471
472
/* Given a pointer to an evdns_server_request, get the corresponding */
473
/* server_request. */
474
#define TO_SERVER_REQUEST(base_ptr)         \
475
0
  ((struct server_request*)         \
476
0
    (((char*)(base_ptr) - evutil_offsetof(struct server_request, base))))
477
478
0
#define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads])
479
480
static struct nameserver *nameserver_pick(struct evdns_base *base);
481
static void evdns_request_insert(struct request *req, struct request **head);
482
static void evdns_request_remove(struct request *req, struct request **head);
483
static void nameserver_ready_callback(evutil_socket_t fd, short events, void *arg);
484
static int evdns_transmit(struct evdns_base *base);
485
static int evdns_request_transmit(struct request *req);
486
static void nameserver_send_probe(struct nameserver *const ns);
487
static void search_request_finished(struct evdns_request *const);
488
static int search_try_next(struct evdns_request *const req);
489
static struct request *search_request_new(struct evdns_base *base, struct evdns_request *handle, int type, const char *const name, int flags);
490
static void evdns_requests_pump_waiting_queue(struct evdns_base *base);
491
static u16 transaction_id_pick(struct evdns_base *base);
492
static struct request *request_new(struct evdns_base *base, struct evdns_request *handle, int type, const char *name, int flags);
493
static struct request *request_clone(struct evdns_base *base, struct request* current);
494
static void request_submit(struct request *const req);
495
496
static int server_request_free(struct server_request *req);
497
static void server_request_free_answers(struct server_request *req);
498
static void server_port_free(struct evdns_server_port *port);
499
static void server_port_ready_callback(evutil_socket_t fd, short events, void *arg);
500
static int evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename);
501
static int evdns_base_set_option_impl(struct evdns_base *base,
502
    const char *option, const char *val, int flags);
503
static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests);
504
static void evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg);
505
static int evdns_server_request_format_response(struct server_request *req, int err);
506
static void incoming_conn_cb(struct evconnlistener *listener, evutil_socket_t fd,
507
    struct sockaddr *address, int socklen, void *arg);
508
509
static int strtoint(const char *const str);
510
511
#ifdef EVENT__DISABLE_THREAD_SUPPORT
512
#define EVDNS_LOCK(base)  EVUTIL_NIL_CONDITION_(base)
513
#define EVDNS_UNLOCK(base) EVUTIL_NIL_STMT_
514
#define ASSERT_LOCKED(base) EVUTIL_NIL_STMT_
515
#else
516
#define EVDNS_LOCK(base)      \
517
0
  EVLOCK_LOCK((base)->lock, 0)
518
#define EVDNS_UNLOCK(base)      \
519
0
  EVLOCK_UNLOCK((base)->lock, 0)
520
#define ASSERT_LOCKED(base)     \
521
0
  EVLOCK_ASSERT_LOCKED((base)->lock)
522
#endif
523
524
static evdns_debug_log_fn_type evdns_log_fn = NULL;
525
526
void
527
evdns_set_log_fn(evdns_debug_log_fn_type fn)
528
0
{
529
0
  evdns_log_fn = fn;
530
0
}
531
532
#ifdef __GNUC__
533
#define EVDNS_LOG_CHECK  __attribute__ ((format(printf, 2, 3)))
534
#else
535
#define EVDNS_LOG_CHECK
536
#endif
537
538
static void evdns_log_(int severity, const char *fmt, ...) EVDNS_LOG_CHECK;
539
static void
540
evdns_log_(int severity, const char *fmt, ...)
541
0
{
542
0
  va_list args;
543
0
  va_start(args,fmt);
544
0
  if (evdns_log_fn) {
545
0
    char buf[512];
546
0
    int is_warn = (severity == EVDNS_LOG_WARN);
547
0
    evutil_vsnprintf(buf, sizeof(buf), fmt, args);
548
0
    evdns_log_fn(is_warn, buf);
549
0
  } else {
550
0
    event_logv_(severity, NULL, fmt, args);
551
0
  }
552
0
  va_end(args);
553
0
}
554
555
0
#define log evdns_log_
556
557
/* Initialize tcp_connection structure. */
558
static void
559
init_tcp_connection(struct tcp_connection *conn, struct bufferevent *bev)
560
0
{
561
0
  memset(conn, 0, sizeof(*conn));
562
0
  conn->state = TS_DISCONNECTED;
563
0
  conn->bev = bev;
564
0
  conn->awaiting_packet_size = 0;
565
0
}
566
567
/* Disconnect tcp connection. */
568
static void
569
evdns_tcp_disconnect(struct tcp_connection *conn)
570
0
{
571
0
  if (!conn)
572
0
    return;
573
0
  conn->state = TS_DISCONNECTED;
574
0
  conn->awaiting_packet_size = 0;
575
0
  if (conn->bev) {
576
0
    bufferevent_free(conn->bev);
577
0
    conn->bev = NULL;
578
0
  }
579
0
}
580
581
/* Add new tcp client to the list of TCP clients in the TCP DNS server. */
582
static struct client_tcp_connection*
583
evdns_add_tcp_client(struct evdns_server_port *port, struct bufferevent *bev)
584
0
{
585
0
  struct client_tcp_connection *client;
586
0
  EVUTIL_ASSERT(port && bev);
587
0
  if (port->max_client_connections == port->client_connections_count)
588
0
    goto error;
589
590
0
  client = mm_calloc(1, sizeof(*client));
591
0
  if (!client)
592
0
    goto error;
593
0
  init_tcp_connection(&client->connection, bev);
594
0
  client->port = port;
595
0
  LIST_INSERT_HEAD(&port->client_connections, client, next);
596
597
0
  ++port->client_connections_count;
598
  /* we need to hold evdns_server_port as long as one connection at least stays alive */
599
0
  ++port->refcnt;
600
0
  return client;
601
0
error:
602
0
  return NULL;
603
0
}
604
605
/* Remove tcp client and free all associated data from the TCP DNS server. */
606
static int
607
evdns_remove_tcp_client(struct evdns_server_port *port, struct client_tcp_connection *client)
608
0
{
609
0
  if (!port || !client)
610
0
    goto error;
611
612
0
  evdns_tcp_disconnect(&client->connection);
613
0
  LIST_REMOVE(client, next);
614
0
  mm_free(client);
615
0
  --port->client_connections_count;
616
0
  --port->refcnt;
617
0
  return 0;
618
0
error:
619
0
  return -1;
620
0
}
621
622
/* Remove all tcp clients and free all associated data from the TCP DNS server. */
623
static void
624
evdns_remove_all_tcp_clients(struct evdns_server_port *port)
625
0
{
626
0
  struct client_tcp_connection *client;
627
0
  while ((client = LIST_FIRST(&port->client_connections))) {
628
0
    evdns_remove_tcp_client(port, client);
629
0
  }
630
0
}
631
632
/* Create new tcp connection structure for DNS client. */
633
static struct tcp_connection *
634
new_tcp_connection(struct bufferevent *bev)
635
0
{
636
0
  struct tcp_connection *conn;
637
0
  if (!bev)
638
0
    return NULL;
639
640
0
  conn = mm_calloc(1, sizeof(*conn));
641
0
  if (!conn)
642
0
    return NULL;
643
0
  init_tcp_connection(conn, bev);
644
0
  return conn;
645
0
}
646
647
/* Disconnect and free all associated data for the tcp connection in DNS client. */
648
static void
649
disconnect_and_free_connection(struct tcp_connection *conn)
650
0
{
651
0
  if (!conn)
652
0
    return;
653
0
  evdns_tcp_disconnect(conn);
654
0
  mm_free(conn);
655
0
}
656
657
/* This walks the list of inflight requests to find the */
658
/* one with a matching transaction id. Returns NULL on */
659
/* failure */
660
static struct request *
661
0
request_find_from_trans_id(struct evdns_base *base, u16 trans_id) {
662
0
  struct request *req = REQ_HEAD(base, trans_id);
663
0
  struct request *const started_at = req;
664
665
0
  ASSERT_LOCKED(base);
666
667
0
  if (req) {
668
0
    do {
669
0
      if (req->trans_id == trans_id) return req;
670
0
      req = req->next;
671
0
    } while (req != started_at);
672
0
  }
673
674
0
  return NULL;
675
0
}
676
677
/* a libevent callback function which is called when a nameserver */
678
/* has gone down and we want to test if it has came back to life yet */
679
static void
680
0
nameserver_prod_callback(evutil_socket_t fd, short events, void *arg) {
681
0
  struct nameserver *const ns = (struct nameserver *) arg;
682
0
  (void)fd;
683
0
  (void)events;
684
685
0
  EVDNS_LOCK(ns->base);
686
0
  nameserver_send_probe(ns);
687
0
  EVDNS_UNLOCK(ns->base);
688
0
}
689
690
/* a libevent callback which is called when a nameserver probe (to see if */
691
/* it has come back to life) times out. We increment the count of failed_times */
692
/* and wait longer to send the next probe packet. */
693
static void
694
0
nameserver_probe_failed(struct nameserver *const ns) {
695
0
  struct timeval timeout;
696
0
  int i;
697
698
0
  ASSERT_LOCKED(ns->base);
699
0
  (void) evtimer_del(&ns->timeout_event);
700
0
  if (ns->state == 1) {
701
    /* This can happen if the nameserver acts in a way which makes us mark */
702
    /* it as bad and then starts sending good replies. */
703
0
    return;
704
0
  }
705
706
0
  memcpy(&timeout, &ns->base->global_nameserver_probe_initial_timeout,
707
0
      sizeof(struct timeval));
708
0
  for (i = ns->failed_times; i > 0 && timeout.tv_sec < ns->base->ns_max_probe_timeout; --i) {
709
0
    timeout.tv_sec *= ns->base->ns_timeout_backoff_factor;
710
0
    timeout.tv_usec *= ns->base->ns_timeout_backoff_factor;
711
0
    if (timeout.tv_usec > 1000000) {
712
0
      timeout.tv_sec += timeout.tv_usec / 1000000;
713
0
      timeout.tv_usec %= 1000000;
714
0
    }
715
0
  }
716
0
  if (timeout.tv_sec > ns->base->ns_max_probe_timeout) {
717
0
    timeout.tv_sec = ns->base->ns_max_probe_timeout;
718
0
    timeout.tv_usec = 0;
719
0
  }
720
721
0
  ns->failed_times++;
722
723
0
  if (evtimer_add(&ns->timeout_event, &timeout) < 0) {
724
0
    char addrbuf[128];
725
0
    log(EVDNS_LOG_WARN,
726
0
        "Error from libevent when adding timer event for %s",
727
0
        evutil_format_sockaddr_port_(
728
0
          (struct sockaddr *)&ns->address,
729
0
          addrbuf, sizeof(addrbuf)));
730
0
  }
731
0
}
732
733
static void
734
0
request_swap_ns(struct request *req, struct nameserver *ns) {
735
0
  if (ns && req->ns != ns) {
736
0
    EVUTIL_ASSERT(req->ns->requests_inflight > 0);
737
0
    req->ns->requests_inflight--;
738
0
    ns->requests_inflight++;
739
740
0
    req->ns = ns;
741
0
  }
742
0
}
743
744
/* called when a nameserver has been deemed to have failed. For example, too */
745
/* many packets have timed out etc */
746
static void
747
0
nameserver_failed(struct nameserver *const ns, const char *msg, int err) {
748
0
  struct request *req, *started_at;
749
0
  struct evdns_base *base = ns->base;
750
0
  int i;
751
0
  char addrbuf[128];
752
753
0
  ASSERT_LOCKED(base);
754
  /* if this nameserver has already been marked as failed */
755
  /* then don't do anything */
756
0
  if (!ns->state) return;
757
758
0
  log(EVDNS_LOG_MSG, "Nameserver %s has failed: %s",
759
0
      evutil_format_sockaddr_port_(
760
0
        (struct sockaddr *)&ns->address,
761
0
        addrbuf, sizeof(addrbuf)),
762
0
      msg);
763
764
0
  base->global_good_nameservers--;
765
0
  EVUTIL_ASSERT(base->global_good_nameservers >= 0);
766
0
  if (base->global_good_nameservers == 0) {
767
0
    log(EVDNS_LOG_MSG, "All nameservers have failed");
768
0
  }
769
770
0
  ns->state = 0;
771
0
  ns->failed_times = 1;
772
773
0
  if (ns->connection) {
774
0
    disconnect_and_free_connection(ns->connection);
775
0
    ns->connection = NULL;
776
0
  } else if (err == ENOTCONN) {
777
    /* XXX: If recvfrom results in ENOTCONN, the socket remains readable
778
     * which triggers another recvfrom. The observed behavior is 100% CPU use.
779
     * This occurs on iOS (kqueue) after the process has been backgrounded
780
     * for a long time (~300 seconds) and then resumed.
781
     * All sockets, TCP and UDP, seem to get ENOTCONN and must be closed.
782
     * https://github.com/libevent/libevent/issues/265 */
783
0
    const struct sockaddr *address = (const struct sockaddr *)&ns->address;
784
0
    evutil_closesocket(ns->socket);
785
0
    ns->socket = evutil_socket_(address->sa_family,
786
0
      SOCK_DGRAM | EVUTIL_SOCK_NONBLOCK | EVUTIL_SOCK_CLOEXEC, 0);
787
788
0
    if (base->global_outgoing_addrlen &&
789
0
      !evutil_sockaddr_is_loopback_(address)) {
790
0
      if (bind(ns->socket,
791
0
          (struct sockaddr *)&base->global_outgoing_address,
792
0
          base->global_outgoing_addrlen) < 0) {
793
0
        log(EVDNS_LOG_WARN, "Couldn't bind to outgoing address");
794
0
      }
795
0
    }
796
797
0
    event_del(&ns->event);
798
0
    event_assign(&ns->event, ns->base->event_base, ns->socket,
799
0
      EV_READ | (ns->write_waiting ? EV_WRITE : 0) | EV_PERSIST,
800
0
      nameserver_ready_callback, ns);
801
0
    if (!base->disable_when_inactive && event_add(&ns->event, NULL) < 0) {
802
0
      log(EVDNS_LOG_WARN, "Couldn't add %s event",
803
0
        ns->write_waiting ? "rw": "read");
804
0
    }
805
0
  }
806
0
  if (evtimer_add(&ns->timeout_event,
807
0
    &base->global_nameserver_probe_initial_timeout) < 0) {
808
0
    log(EVDNS_LOG_WARN,
809
0
        "Error from libevent when adding timer event for %s",
810
0
        evutil_format_sockaddr_port_(
811
0
          (struct sockaddr *)&ns->address,
812
0
          addrbuf, sizeof(addrbuf)));
813
    /* ???? Do more? */
814
0
  }
815
816
  /* walk the list of inflight requests to see if any can be reassigned to */
817
  /* a different server. Requests in the waiting queue don't have a */
818
  /* nameserver assigned yet */
819
820
  /* if we don't have *any* good nameservers then there's no point */
821
  /* trying to reassign requests to one */
822
0
  if (!base->global_good_nameservers) return;
823
824
0
  for (i = 0; i < base->n_req_heads; ++i) {
825
0
    req = started_at = base->req_heads[i];
826
0
    if (req) {
827
0
      do {
828
0
        if (req->tx_count == 0 && req->ns == ns) {
829
          /* still waiting to go out, can be moved */
830
          /* to another server */
831
0
          request_swap_ns(req, nameserver_pick(base));
832
0
        }
833
0
        req = req->next;
834
0
      } while (req != started_at);
835
0
    }
836
0
  }
837
0
}
838
839
static void
840
nameserver_up(struct nameserver *const ns)
841
0
{
842
0
  char addrbuf[128];
843
0
  ASSERT_LOCKED(ns->base);
844
0
  if (ns->state) return;
845
0
  log(EVDNS_LOG_MSG, "Nameserver %s is back up",
846
0
      evutil_format_sockaddr_port_(
847
0
        (struct sockaddr *)&ns->address,
848
0
        addrbuf, sizeof(addrbuf)));
849
0
  evtimer_del(&ns->timeout_event);
850
0
  if (ns->probe_request) {
851
0
    evdns_cancel_request(ns->base, ns->probe_request);
852
0
    ns->probe_request = NULL;
853
0
  }
854
0
  ns->state = 1;
855
0
  ns->failed_times = 0;
856
0
  ns->timedout = 0;
857
0
  ns->base->global_good_nameservers++;
858
0
}
859
860
static void
861
0
request_trans_id_set(struct request *const req, const u16 trans_id) {
862
0
  req->trans_id = trans_id;
863
0
  *((u16 *) req->request) = htons(trans_id);
864
0
}
865
866
/* Called to remove a request from a list and dealloc it. */
867
/* head is a pointer to the head of the list it should be */
868
/* removed from or NULL if the request isn't in a list. */
869
/* when free_handle is one, free the handle as well. */
870
static void
871
0
request_finished(struct request *const req, struct request **head, int free_handle) {
872
0
  struct evdns_base *base = req->base;
873
0
  int was_inflight = (head != &base->req_waiting_head);
874
0
  EVDNS_LOCK(base);
875
0
  ASSERT_VALID_REQUEST(req);
876
877
0
  if (head)
878
0
    evdns_request_remove(req, head);
879
880
0
  log(EVDNS_LOG_DEBUG, "Removing timeout for request %p", (void *)req);
881
0
  if (was_inflight) {
882
0
    evtimer_del(&req->timeout_event);
883
0
    base->global_requests_inflight--;
884
0
    req->ns->requests_inflight--;
885
0
  } else {
886
0
    base->global_requests_waiting--;
887
0
  }
888
  /* it was initialized during request_new / evtimer_assign */
889
0
  event_debug_unassign(&req->timeout_event);
890
891
0
  if (req->ns &&
892
0
      req->ns->requests_inflight == 0 &&
893
0
      req->base->disable_when_inactive) {
894
0
    event_del(&req->ns->event);
895
0
    evtimer_del(&req->ns->timeout_event);
896
0
  }
897
898
0
  if (!req->request_appended) {
899
    /* need to free the request data on it's own */
900
0
    mm_free(req->request);
901
0
  } else {
902
    /* the request data is appended onto the header */
903
    /* so everything gets free()ed when we: */
904
0
  }
905
906
0
  if (req->handle) {
907
0
    EVUTIL_ASSERT(req->handle->current_req == req);
908
909
0
    if (free_handle) {
910
0
      search_request_finished(req->handle);
911
0
      req->handle->current_req = NULL;
912
0
      if (! req->handle->pending_cb) {
913
        /* If we're planning to run the callback,
914
         * don't free the handle until later. */
915
0
        mm_free(req->handle);
916
0
      }
917
0
      req->handle = NULL; /* If we have a bug, let's crash
918
               * early */
919
0
    } else {
920
0
      req->handle->current_req = NULL;
921
0
    }
922
0
  }
923
924
0
  mm_free(req);
925
926
0
  evdns_requests_pump_waiting_queue(base);
927
0
  EVDNS_UNLOCK(base);
928
0
}
929
930
/* This is called when a server returns a funny error code. */
931
/* We try the request again with another server. */
932
/* */
933
/* return: */
934
/*   0 ok */
935
/*   1 failed/reissue is pointless */
936
static int
937
0
request_reissue(struct request *req) {
938
0
  const struct nameserver *const last_ns = req->ns;
939
0
  ASSERT_LOCKED(req->base);
940
0
  ASSERT_VALID_REQUEST(req);
941
  /* the last nameserver should have been marked as failing */
942
  /* by the caller of this function, therefore pick will try */
943
  /* not to return it */
944
0
  request_swap_ns(req, nameserver_pick(req->base));
945
0
  if (req->ns == last_ns) {
946
    /* ... but pick did return it */
947
    /* not a lot of point in trying again with the */
948
    /* same server */
949
0
    return 1;
950
0
  }
951
952
0
  req->reissue_count++;
953
0
  req->tx_count = 0;
954
0
  req->transmit_me = 1;
955
956
0
  return 0;
957
0
}
958
959
/* this function looks for space on the inflight queue and promotes */
960
/* requests from the waiting queue if it can. */
961
/* */
962
/* TODO: */
963
/* add return code, see at nameserver_pick() and other functions. */
964
static void
965
0
evdns_requests_pump_waiting_queue(struct evdns_base *base) {
966
0
  ASSERT_LOCKED(base);
967
0
  while (base->global_requests_inflight < base->global_max_requests_inflight &&
968
0
       base->global_requests_waiting) {
969
0
    struct request *req;
970
971
0
    EVUTIL_ASSERT(base->req_waiting_head);
972
0
    req = base->req_waiting_head;
973
974
0
    req->ns = nameserver_pick(base);
975
0
    if (!req->ns)
976
0
      return;
977
978
    /* move a request from the waiting queue to the inflight queue */
979
0
    req->ns->requests_inflight++;
980
981
0
    evdns_request_remove(req, &base->req_waiting_head);
982
983
0
    base->global_requests_waiting--;
984
0
    base->global_requests_inflight++;
985
986
0
    request_trans_id_set(req, transaction_id_pick(base));
987
988
0
    evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
989
0
    evdns_request_transmit(req);
990
0
    evdns_transmit(base);
991
0
  }
992
0
}
993
994
static void
995
reply_run_callback(struct event_callback *d, void *user_pointer)
996
0
{
997
0
  struct evdns_request *handle =
998
0
      EVUTIL_UPCAST(d, struct evdns_request, deferred);
999
1000
0
  switch (handle->request_type) {
1001
0
  case TYPE_A:
1002
0
    if (handle->have_reply) {
1003
0
      handle->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
1004
0
          handle->reply.rr_count, handle->ttl,
1005
0
          handle->reply.data.a,
1006
0
          user_pointer);
1007
0
      if (handle->reply.cname)
1008
0
        handle->user_callback(DNS_ERR_NONE, DNS_CNAME, 1,
1009
0
            handle->ttl, handle->reply.cname, user_pointer);
1010
0
    } else
1011
0
      handle->user_callback(handle->err, DNS_IPv4_A, 0, handle->ttl, NULL, user_pointer);
1012
0
    break;
1013
0
  case TYPE_PTR:
1014
0
    if (handle->have_reply) {
1015
0
      char *name = handle->reply.data.ptr_name;
1016
0
      handle->user_callback(DNS_ERR_NONE, DNS_PTR, 1, handle->ttl,
1017
0
          &name, user_pointer);
1018
0
    } else {
1019
0
      handle->user_callback(handle->err, DNS_PTR, 0, handle->ttl, NULL, user_pointer);
1020
0
    }
1021
0
    break;
1022
0
  case TYPE_AAAA:
1023
0
    if (handle->have_reply) {
1024
0
      handle->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
1025
0
          handle->reply.rr_count, handle->ttl,
1026
0
          handle->reply.data.aaaa,
1027
0
          user_pointer);
1028
0
      if (handle->reply.cname)
1029
0
        handle->user_callback(DNS_ERR_NONE, DNS_CNAME, 1,
1030
0
            handle->ttl, handle->reply.cname, user_pointer);
1031
0
    } else
1032
0
      handle->user_callback(handle->err, DNS_IPv6_AAAA, 0, handle->ttl, NULL, user_pointer);
1033
0
    break;
1034
0
  default:
1035
0
    EVUTIL_ASSERT(0);
1036
0
  }
1037
1038
0
  if (handle->reply.data.raw) {
1039
0
    mm_free(handle->reply.data.raw);
1040
0
  }
1041
1042
0
  if (handle->reply.cname) {
1043
0
    mm_free(handle->reply.cname);
1044
0
  }
1045
1046
0
  mm_free(handle);
1047
0
}
1048
1049
static void
1050
reply_schedule_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply)
1051
0
{
1052
0
  struct evdns_request* handle = req->handle;
1053
1054
0
  ASSERT_LOCKED(req->base);
1055
1056
0
  handle->request_type = req->request_type;
1057
0
  handle->ttl = ttl;
1058
0
  handle->err = err;
1059
0
  if (reply) {
1060
0
    handle->have_reply = 1;
1061
0
    memcpy(&handle->reply, reply, sizeof(struct reply));
1062
    /* We've taken ownership of the data. */
1063
0
    reply->data.raw = NULL;
1064
0
  }
1065
1066
0
  handle->pending_cb = 1;
1067
1068
0
  event_deferred_cb_init_(
1069
0
      &handle->deferred,
1070
0
      event_get_priority(&req->timeout_event),
1071
0
      reply_run_callback,
1072
0
      handle->user_pointer);
1073
0
  event_deferred_cb_schedule_(
1074
0
    req->base->event_base,
1075
0
    &handle->deferred);
1076
0
}
1077
1078
static int
1079
client_retransmit_through_tcp(struct evdns_request *handle)
1080
0
{
1081
0
  struct request *req = handle->current_req;
1082
0
  struct evdns_base *base = req->base;
1083
0
  struct request *newreq = request_clone(base, req);
1084
0
  ASSERT_LOCKED(base);
1085
0
  if (!newreq)
1086
0
    return 1;
1087
0
  request_finished(req, &REQ_HEAD(req->base, req->trans_id), 0);
1088
0
  handle->current_req = newreq;
1089
0
  newreq->handle = handle;
1090
0
  request_submit(newreq);
1091
0
  return 0;
1092
0
}
1093
1094
0
#define _QR_MASK    0x8000U
1095
0
#define _OP_MASK    0x7800U
1096
#define _AA_MASK    0x0400U
1097
0
#define _TC_MASK    0x0200U
1098
0
#define _RD_MASK    0x0100U
1099
#define _RA_MASK    0x0080U
1100
#define _Z_MASK     0x0040U
1101
#define _AD_MASK    0x0020U
1102
0
#define _CD_MASK    0x0010U
1103
0
#define _RCODE_MASK 0x000fU
1104
#define _Z_MASK_DEPRECATED 0x0070U
1105
1106
/* this processes a parsed reply packet */
1107
static void
1108
0
reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) {
1109
0
  int error;
1110
0
  char addrbuf[128];
1111
0
  int retransmit_via_tcp = 0;
1112
0
  static const int error_codes[] = {
1113
0
    DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST,
1114
0
    DNS_ERR_NOTIMPL, DNS_ERR_REFUSED
1115
0
  };
1116
1117
0
  ASSERT_LOCKED(req->base);
1118
0
  ASSERT_VALID_REQUEST(req);
1119
1120
0
  if (flags & (_RCODE_MASK | _TC_MASK) || !reply || !reply->have_answer) {
1121
    /* there was an error */
1122
0
    if (flags & _TC_MASK) {
1123
0
      error = DNS_ERR_TRUNCATED;
1124
0
      retransmit_via_tcp = (req->handle->tcp_flags & (DNS_QUERY_IGNTC | DNS_QUERY_USEVC)) == 0;
1125
0
    } else if (flags & _RCODE_MASK) {
1126
0
      u16 error_code = (flags & _RCODE_MASK) - 1;
1127
0
      if (error_code > 4) {
1128
0
        error = DNS_ERR_UNKNOWN;
1129
0
      } else {
1130
0
        error = error_codes[error_code];
1131
0
      }
1132
0
    } else if (reply && !reply->have_answer) {
1133
0
      error = DNS_ERR_NODATA;
1134
0
    } else {
1135
0
      error = DNS_ERR_UNKNOWN;
1136
0
    }
1137
1138
0
    switch (error) {
1139
0
    case DNS_ERR_NOTIMPL:
1140
0
    case DNS_ERR_REFUSED:
1141
      /* we regard these errors as marking a bad nameserver */
1142
0
      if (req->reissue_count < req->base->global_max_reissues) {
1143
0
        char msg[64];
1144
0
        evutil_snprintf(msg, sizeof(msg), "Bad response %d (%s)",
1145
0
           error, evdns_err_to_string(error));
1146
0
        nameserver_failed(req->ns, msg, 0);
1147
0
        if (!request_reissue(req)) return;
1148
0
      }
1149
0
      break;
1150
0
    case DNS_ERR_SERVERFAILED:
1151
      /* rcode 2 (servfailed) sometimes means "we
1152
       * are broken" and sometimes (with some binds)
1153
       * means "that request was very confusing."
1154
       * Treat this as a timeout, not a failure.
1155
       */
1156
0
      log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver"
1157
0
        "at %s; will allow the request to time out.",
1158
0
          evutil_format_sockaddr_port_(
1159
0
            (struct sockaddr *)&req->ns->address,
1160
0
            addrbuf, sizeof(addrbuf)));
1161
      /* Call the timeout function */
1162
0
      evdns_request_timeout_callback(0, 0, req);
1163
0
      return;
1164
0
    default:
1165
      /* we got a good reply from the nameserver: it is up. */
1166
0
      if (req->handle == req->ns->probe_request) {
1167
        /* Avoid double-free */
1168
0
        req->ns->probe_request = NULL;
1169
0
      }
1170
1171
0
      nameserver_up(req->ns);
1172
0
    }
1173
1174
0
    if (retransmit_via_tcp) {
1175
0
      log(EVDNS_LOG_DEBUG, "Received truncated reply(flags 0x%x, transac ID: %d). Retransmitting via TCP.",
1176
0
        req->handle->tcp_flags, req->trans_id);
1177
0
      req->handle->tcp_flags |= DNS_QUERY_USEVC;
1178
0
      client_retransmit_through_tcp(req->handle);
1179
0
      return;
1180
0
    }
1181
1182
0
    if (req->handle->search_state &&
1183
0
        req->request_type != TYPE_PTR) {
1184
      /* if we have a list of domains to search in,
1185
       * try the next one */
1186
0
      if (!search_try_next(req->handle)) {
1187
        /* a new request was issued so this
1188
         * request is finished and */
1189
        /* the user callback will be made when
1190
         * that request (or a */
1191
        /* child of it) finishes. */
1192
0
        return;
1193
0
      }
1194
0
    }
1195
1196
    /* all else failed. Pass the failure up */
1197
0
    reply_schedule_callback(req, ttl, error, NULL);
1198
0
    request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
1199
0
  } else {
1200
    /* all ok, tell the user */
1201
0
    reply_schedule_callback(req, ttl, 0, reply);
1202
0
    if (req->handle == req->ns->probe_request)
1203
0
      req->ns->probe_request = NULL; /* Avoid double-free */
1204
0
    nameserver_up(req->ns);
1205
0
    request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
1206
0
  }
1207
0
}
1208
1209
static int
1210
0
name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
1211
0
  int name_end = -1;
1212
0
  int j = *idx;
1213
0
  int ptr_count = 0;
1214
0
#define GET32(x) do { if (j + 4 > length) goto err; memcpy(&t32_, packet + j, 4); j += 4; x = ntohl(t32_); } while (0)
1215
0
#define GET16(x) do { if (j + 2 > length) goto err; memcpy(&t_, packet + j, 2); j += 2; x = ntohs(t_); } while (0)
1216
0
#define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
1217
1218
0
  char *cp = name_out;
1219
0
  const char *const end = name_out + name_out_len;
1220
1221
  /* Normally, names are a series of length prefixed strings terminated */
1222
  /* with a length of 0 (the lengths are u8's < 63). */
1223
  /* However, the length can start with a pair of 1 bits and that */
1224
  /* means that the next 14 bits are a pointer within the current */
1225
  /* packet. */
1226
1227
0
  for (;;) {
1228
0
    u8 label_len;
1229
0
    GET8(label_len);
1230
0
    if (!label_len) break;
1231
0
    if (label_len & 0xc0) {
1232
0
      u8 ptr_low;
1233
0
      GET8(ptr_low);
1234
0
      if (name_end < 0) name_end = j;
1235
0
      j = (((int)label_len & 0x3f) << 8) + ptr_low;
1236
      /* Make sure that the target offset is in-bounds. */
1237
0
      if (j < 0 || j >= length) return -1;
1238
      /* If we've jumped more times than there are characters in the
1239
       * message, we must have a loop. */
1240
0
      if (++ptr_count > length) return -1;
1241
0
      continue;
1242
0
    }
1243
0
    if (label_len > 63) return -1;
1244
0
    if (cp != name_out) {
1245
0
      if (cp + 1 >= end) return -1;
1246
0
      *cp++ = '.';
1247
0
    }
1248
0
    if (cp + label_len >= end) return -1;
1249
0
    if (j + label_len > length) return -1;
1250
0
    memcpy(cp, packet + j, label_len);
1251
0
    cp += label_len;
1252
0
    j += label_len;
1253
0
  }
1254
0
  if (cp >= end) return -1;
1255
0
  *cp = '\0';
1256
0
  if (name_end < 0)
1257
0
    *idx = j;
1258
0
  else
1259
0
    *idx = name_end;
1260
0
  return 0;
1261
0
 err:
1262
0
  return -1;
1263
0
}
1264
1265
/* parses a raw request from a nameserver */
1266
static int
1267
reply_parse(struct evdns_base *base, u8 *packet, int length)
1268
0
{
1269
0
  int j = 0, k = 0;  /* index into packet */
1270
0
  u16 t_;   /* used by the macros */
1271
0
  u32 t32_;  /* used by the macros */
1272
0
  char tmp_name[256], cmp_name[256]; /* used by the macros */
1273
0
  int name_matches = 0;
1274
1275
0
  u16 trans_id, questions, answers, authority, additional, datalength;
1276
0
  u16 flags = 0;
1277
0
  u32 ttl, ttl_r = 0xffffffff;
1278
0
  struct reply reply;
1279
0
  struct request *req = NULL;
1280
0
  unsigned int i, buf_size;
1281
1282
0
  memset(&reply, 0, sizeof(reply));
1283
1284
0
  ASSERT_LOCKED(base);
1285
1286
0
  GET16(trans_id);
1287
0
  GET16(flags);
1288
0
  GET16(questions);
1289
0
  GET16(answers);
1290
0
  GET16(authority);
1291
0
  GET16(additional);
1292
0
  (void) authority; /* suppress "unused variable" warnings. */
1293
0
  (void) additional; /* suppress "unused variable" warnings. */
1294
1295
0
  req = request_find_from_trans_id(base, trans_id);
1296
0
  if (!req) return -1;
1297
0
  EVUTIL_ASSERT(req->base == base);
1298
1299
  /* If it's not an answer, it doesn't correspond to any request. */
1300
0
  if (!(flags & _QR_MASK)) return -1;  /* must be an answer */
1301
0
  if ((flags & (_RCODE_MASK|_TC_MASK)) && (flags & (_RCODE_MASK|_TC_MASK)) != DNS_ERR_NOTEXIST) {
1302
    /* there was an error and it's not NXDOMAIN */
1303
0
    goto err;
1304
0
  }
1305
  /* if (!answers) return; */  /* must have an answer of some form */
1306
1307
  /* This macro skips a name in the DNS reply. */
1308
0
#define SKIP_NAME           \
1309
0
  do { tmp_name[0] = '\0';       \
1310
0
    if (name_parse(packet, length, &j, tmp_name,  \
1311
0
      sizeof(tmp_name))<0)     \
1312
0
      goto err;       \
1313
0
  } while (0)
1314
1315
0
  reply.type = req->request_type;
1316
1317
  /* skip over each question in the reply */
1318
0
  for (i = 0; i < questions; ++i) {
1319
    /* the question looks like
1320
     *   <label:name><u16:type><u16:class>
1321
     */
1322
0
    tmp_name[0] = '\0';
1323
0
    cmp_name[0] = '\0';
1324
0
    k = j;
1325
0
    if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name)) < 0)
1326
0
      goto err;
1327
0
    if (name_parse(req->request, req->request_len, &k,
1328
0
      cmp_name, sizeof(cmp_name))<0)
1329
0
      goto err;
1330
0
    if (!base->global_randomize_case) {
1331
0
      if (strcmp(tmp_name, cmp_name) == 0)
1332
0
        name_matches = 1;
1333
0
    } else {
1334
0
      if (evutil_ascii_strcasecmp(tmp_name, cmp_name) == 0)
1335
0
        name_matches = 1;
1336
0
    }
1337
1338
0
    j += 4;
1339
0
    if (j > length)
1340
0
      goto err;
1341
0
  }
1342
1343
0
  if (!name_matches)
1344
0
    goto err;
1345
1346
  /* We can allocate less for the reply data, but to do it we'll have
1347
   * to parse the response. To simplify things let's just allocate
1348
   * a little bit more to avoid complex evaluations.
1349
   */
1350
0
  buf_size = MAX(length - j, EVDNS_NAME_MAX);
1351
0
  reply.data.raw = mm_malloc(buf_size);
1352
1353
  /* now we have the answer section which looks like
1354
   * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
1355
   */
1356
1357
0
  for (i = 0; i < answers; ++i) {
1358
0
    u16 type, class;
1359
1360
0
    SKIP_NAME;
1361
0
    GET16(type);
1362
0
    GET16(class);
1363
0
    GET32(ttl);
1364
0
    GET16(datalength);
1365
1366
0
    if (type == TYPE_A && class == CLASS_INET) {
1367
0
      int addrcount;
1368
0
      if (req->request_type != TYPE_A) {
1369
0
        j += datalength; continue;
1370
0
      }
1371
0
      if ((datalength & 3) != 0) /* not an even number of As. */
1372
0
          goto err;
1373
0
      addrcount = datalength >> 2;
1374
1375
0
      ttl_r = MIN(ttl_r, ttl);
1376
      /* we only bother with the first four addresses. */
1377
0
      if (j + 4*addrcount > length) goto err;
1378
0
      memcpy(&reply.data.a[reply.rr_count],
1379
0
           packet + j, 4*addrcount);
1380
0
      j += 4*addrcount;
1381
0
      reply.rr_count += addrcount;
1382
0
      reply.have_answer = 1;
1383
0
    } else if (type == TYPE_PTR && class == CLASS_INET) {
1384
0
      if (req->request_type != TYPE_PTR) {
1385
0
        j += datalength; continue;
1386
0
      }
1387
0
      if (name_parse(packet, length, &j, reply.data.ptr_name,
1388
0
               buf_size)<0)
1389
0
        goto err;
1390
0
      ttl_r = MIN(ttl_r, ttl);
1391
0
      reply.have_answer = 1;
1392
0
      break;
1393
0
    } else if (type == TYPE_CNAME) {
1394
0
      char cname[EVDNS_NAME_MAX];
1395
0
      if (name_parse(packet, length, &j, cname,
1396
0
        sizeof(cname))<0)
1397
0
        goto err;
1398
0
      if (req->need_cname)
1399
0
        reply.cname = mm_strdup(cname);
1400
0
      if (req->put_cname_in_ptr && !*req->put_cname_in_ptr)
1401
0
        *req->put_cname_in_ptr = mm_strdup(cname);
1402
0
    } else if (type == TYPE_AAAA && class == CLASS_INET) {
1403
0
      int addrcount;
1404
0
      if (req->request_type != TYPE_AAAA) {
1405
0
        j += datalength; continue;
1406
0
      }
1407
0
      if ((datalength & 15) != 0) /* not an even number of AAAAs. */
1408
0
        goto err;
1409
0
      addrcount = datalength >> 4;  /* each address is 16 bytes long */
1410
0
      ttl_r = MIN(ttl_r, ttl);
1411
1412
      /* we only bother with the first four addresses. */
1413
0
      if (j + 16*addrcount > length) goto err;
1414
0
      memcpy(&reply.data.aaaa[reply.rr_count],
1415
0
           packet + j, 16*addrcount);
1416
0
      reply.rr_count += addrcount;
1417
0
      j += 16*addrcount;
1418
0
      reply.have_answer = 1;
1419
0
    } else {
1420
      /* skip over any other type of resource */
1421
0
      j += datalength;
1422
0
    }
1423
0
  }
1424
1425
0
  if (!reply.have_answer) {
1426
0
    for (i = 0; i < authority; ++i) {
1427
0
      u16 type, class;
1428
0
      SKIP_NAME;
1429
0
      GET16(type);
1430
0
      GET16(class);
1431
0
      GET32(ttl);
1432
0
      GET16(datalength);
1433
0
      if (type == TYPE_SOA && class == CLASS_INET) {
1434
0
        u32 serial, refresh, retry, expire, minimum;
1435
0
        SKIP_NAME;
1436
0
        SKIP_NAME;
1437
0
        GET32(serial);
1438
0
        GET32(refresh);
1439
0
        GET32(retry);
1440
0
        GET32(expire);
1441
0
        GET32(minimum);
1442
0
        (void)expire;
1443
0
        (void)retry;
1444
0
        (void)refresh;
1445
0
        (void)serial;
1446
0
        ttl_r = MIN(ttl_r, ttl);
1447
0
        ttl_r = MIN(ttl_r, minimum);
1448
0
      } else {
1449
        /* skip over any other type of resource */
1450
0
        j += datalength;
1451
0
      }
1452
0
    }
1453
0
  }
1454
1455
0
  if (ttl_r == 0xffffffff)
1456
0
    ttl_r = 0;
1457
1458
0
  reply_handle(req, flags, ttl_r, &reply);
1459
0
  if (reply.data.raw)
1460
0
    mm_free(reply.data.raw);
1461
0
  return 0;
1462
0
 err:
1463
0
  if (req)
1464
0
    reply_handle(req, flags, 0, NULL);
1465
0
  if (reply.data.raw)
1466
0
    mm_free(reply.data.raw);
1467
0
  return -1;
1468
0
}
1469
1470
/* Parse a raw request (packet,length) sent to a nameserver port (port) from */
1471
/* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
1472
/* callback. */
1473
static int
1474
request_parse(u8 *packet, int length, struct evdns_server_port *port,
1475
        struct sockaddr *addr, ev_socklen_t addrlen, struct client_tcp_connection *client)
1476
0
{
1477
0
  int j = 0;  /* index into packet */
1478
0
  u16 t_;   /* used by the macros */
1479
0
  u32 t32_;  /* used by the macros */
1480
0
  char tmp_name[256]; /* used by the macros */
1481
1482
0
  int i;
1483
0
  u16 trans_id, flags, questions, answers, authority, additional;
1484
0
  struct server_request *server_req = NULL;
1485
0
  u32 ttl;
1486
0
  u16 type, class, rdlen;
1487
1488
0
  ASSERT_LOCKED(port);
1489
1490
  /* Get the header fields */
1491
0
  GET16(trans_id);
1492
0
  GET16(flags);
1493
0
  GET16(questions);
1494
0
  GET16(answers);
1495
0
  GET16(authority);
1496
0
  GET16(additional);
1497
1498
0
  if (flags & _QR_MASK) return -1; /* Must not be an answer. */
1499
0
  flags &= (_RD_MASK|_CD_MASK); /* Only RD and CD get preserved. */
1500
1501
0
  server_req = mm_malloc(sizeof(struct server_request));
1502
0
  if (server_req == NULL) return -1;
1503
0
  memset(server_req, 0, sizeof(struct server_request));
1504
1505
0
  server_req->trans_id = trans_id;
1506
0
  if (addr) {
1507
0
    memcpy(&server_req->addr, addr, addrlen);
1508
0
    server_req->addrlen = addrlen;
1509
0
  }
1510
1511
0
  server_req->port = port;
1512
0
  server_req->client = client;
1513
0
  server_req->base.flags = flags;
1514
0
  server_req->base.nquestions = 0;
1515
0
  server_req->base.questions = mm_calloc(sizeof(struct evdns_server_question *), questions);
1516
0
  if (server_req->base.questions == NULL)
1517
0
    goto err;
1518
1519
0
  for (i = 0; i < questions; ++i) {
1520
0
    u16 type, class;
1521
0
    struct evdns_server_question *q;
1522
0
    int namelen;
1523
0
    if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
1524
0
      goto err;
1525
0
    GET16(type);
1526
0
    GET16(class);
1527
0
    namelen = (int)strlen(tmp_name);
1528
0
    q = mm_malloc(sizeof(struct evdns_server_question) + namelen);
1529
0
    if (!q)
1530
0
      goto err;
1531
0
    q->type = type;
1532
0
    q->dns_question_class = class;
1533
0
    memcpy(q->name, tmp_name, namelen+1);
1534
0
    server_req->base.questions[server_req->base.nquestions++] = q;
1535
0
  }
1536
1537
0
#define SKIP_RR \
1538
0
  do { \
1539
0
    SKIP_NAME; \
1540
0
    j += 2 /* type */ + 2 /* class */ + 4 /* ttl */; \
1541
0
    GET16(rdlen); \
1542
0
    j += rdlen; \
1543
0
  } while (0)
1544
1545
0
  for (i = 0; i < answers; ++i) {
1546
0
    SKIP_RR;
1547
0
  }
1548
1549
0
  for (i = 0; i < authority; ++i) {
1550
0
    SKIP_RR;
1551
0
  }
1552
1553
0
  server_req->max_udp_reply_size = DNS_MAX_UDP_SIZE;
1554
0
  for (i = 0; i < additional; ++i) {
1555
0
    SKIP_NAME;
1556
0
    GET16(type);
1557
0
    GET16(class);
1558
0
    GET32(ttl);
1559
0
    GET16(rdlen);
1560
0
    (void)ttl;
1561
0
    j += rdlen;
1562
0
    if (type == TYPE_OPT) {
1563
      /* In case of OPT pseudo-RR `class` field is treated
1564
       * as a requestor's UDP payload size. */
1565
0
      server_req->max_udp_reply_size = MAX(class, DNS_MAX_UDP_SIZE);
1566
0
      evdns_server_request_add_reply(&(server_req->base),
1567
0
        EVDNS_ADDITIONAL_SECTION,
1568
0
        "", /* name */
1569
0
        TYPE_OPT, /* type */
1570
0
        DNS_MAX_UDP_SIZE, /* class */
1571
0
        0, /* ttl */
1572
0
        0, /* datalen */
1573
0
        0, /* is_name */
1574
        NULL /* data */
1575
0
      );
1576
0
      break;
1577
0
    }
1578
0
  }
1579
1580
0
  port->refcnt++;
1581
1582
  /* Only standard queries are supported. */
1583
0
  if (flags & _OP_MASK) {
1584
0
    evdns_server_request_respond(&(server_req->base), DNS_ERR_NOTIMPL);
1585
0
    return -1;
1586
0
  }
1587
1588
0
  port->user_callback(&(server_req->base), port->user_data);
1589
1590
0
  return 0;
1591
0
err:
1592
0
  if (server_req) {
1593
0
    if (server_req->base.questions) {
1594
0
      for (i = 0; i < server_req->base.nquestions; ++i)
1595
0
        mm_free(server_req->base.questions[i]);
1596
0
      mm_free(server_req->base.questions);
1597
0
    }
1598
0
    mm_free(server_req);
1599
0
  }
1600
0
  return -1;
1601
1602
0
#undef SKIP_RR
1603
0
#undef SKIP_NAME
1604
0
#undef GET32
1605
0
#undef GET16
1606
0
#undef GET8
1607
0
}
1608
1609
/* Try to choose a strong transaction id which isn't already in flight */
1610
static u16
1611
0
transaction_id_pick(struct evdns_base *base) {
1612
0
  ASSERT_LOCKED(base);
1613
0
  for (;;) {
1614
0
    u16 trans_id;
1615
0
    evutil_secure_rng_get_bytes(&trans_id, sizeof(trans_id));
1616
1617
0
    if (trans_id == 0xffff) continue;
1618
    /* now check to see if that id is already inflight */
1619
0
    if (request_find_from_trans_id(base, trans_id) == NULL)
1620
0
      return trans_id;
1621
0
  }
1622
0
}
1623
1624
/* choose a namesever to use. This function will try to ignore */
1625
/* nameservers which we think are down and load balance across the rest */
1626
/* by updating the server_head global each time. */
1627
static struct nameserver *
1628
0
nameserver_pick(struct evdns_base *base) {
1629
0
  struct nameserver *started_at = base->server_head, *picked;
1630
0
  ASSERT_LOCKED(base);
1631
0
  if (!base->server_head) return NULL;
1632
1633
  /* if we don't have any good nameservers then there's no */
1634
  /* point in trying to find one. */
1635
0
  if (!base->global_good_nameservers) {
1636
0
    base->server_head = base->server_head->next;
1637
0
    return base->server_head;
1638
0
  }
1639
1640
  /* remember that nameservers are in a circular list */
1641
0
  for (;;) {
1642
0
    if (base->server_head->state) {
1643
      /* we think this server is currently good */
1644
0
      picked = base->server_head;
1645
0
      base->server_head = base->server_head->next;
1646
0
      return picked;
1647
0
    }
1648
1649
0
    base->server_head = base->server_head->next;
1650
0
    if (base->server_head == started_at) {
1651
      /* all the nameservers seem to be down */
1652
      /* so we just return this one and hope for the */
1653
      /* best */
1654
0
      EVUTIL_ASSERT(base->global_good_nameservers == 0);
1655
0
      picked = base->server_head;
1656
0
      base->server_head = base->server_head->next;
1657
0
      return picked;
1658
0
    }
1659
0
  }
1660
0
}
1661
1662
/* this is called when a namesever socket is ready for reading */
1663
static void
1664
0
nameserver_read(struct nameserver *ns) {
1665
0
  struct sockaddr_storage ss;
1666
0
  ev_socklen_t addrlen = sizeof(ss);
1667
0
  char addrbuf[128];
1668
0
  const size_t max_packet_size = ns->base->global_max_udp_size;
1669
0
  u8 *packet = mm_malloc(max_packet_size);
1670
0
  ASSERT_LOCKED(ns->base);
1671
1672
0
  if (!packet) {
1673
0
    nameserver_failed(ns, "not enough memory", 0);
1674
0
    return;
1675
0
  }
1676
1677
0
  for (;;) {
1678
0
    const int r = recvfrom(ns->socket, (void*)packet,
1679
0
        max_packet_size, 0,
1680
0
        (struct sockaddr*)&ss, &addrlen);
1681
0
    if (r < 0) {
1682
0
      int err = evutil_socket_geterror(ns->socket);
1683
0
      if (EVUTIL_ERR_RW_RETRIABLE(err))
1684
0
        goto done;
1685
0
      nameserver_failed(ns,
1686
0
          evutil_socket_error_to_string(err), err);
1687
0
      goto done;
1688
0
    }
1689
0
    if (evutil_sockaddr_cmp((struct sockaddr*)&ss,
1690
0
      (struct sockaddr*)&ns->address, 0)) {
1691
0
      log(EVDNS_LOG_WARN, "Address mismatch on received "
1692
0
          "DNS packet.  Apparent source was %s",
1693
0
          evutil_format_sockaddr_port_(
1694
0
            (struct sockaddr *)&ss,
1695
0
            addrbuf, sizeof(addrbuf)));
1696
0
      goto done;
1697
0
    }
1698
1699
0
    ns->timedout = 0;
1700
0
    reply_parse(ns->base, packet, r);
1701
0
  }
1702
0
done:
1703
0
  mm_free(packet);
1704
0
}
1705
1706
/* Read a packet from a DNS client on a server port s, parse it, and */
1707
/* act accordingly. */
1708
static void
1709
0
server_udp_port_read(struct evdns_server_port *s) {
1710
0
  u8 packet[1500];
1711
0
  struct sockaddr_storage addr;
1712
0
  ev_socklen_t addrlen;
1713
0
  int r;
1714
0
  ASSERT_LOCKED(s);
1715
1716
0
  for (;;) {
1717
0
    addrlen = sizeof(struct sockaddr_storage);
1718
0
    r = recvfrom(s->socket, (void*)packet, sizeof(packet), 0,
1719
0
           (struct sockaddr*) &addr, &addrlen);
1720
0
    if (r < 0) {
1721
0
      int err = evutil_socket_geterror(s->socket);
1722
0
      if (EVUTIL_ERR_RW_RETRIABLE(err))
1723
0
        return;
1724
0
      log(EVDNS_LOG_WARN,
1725
0
          "Error %s (%d) while reading request.",
1726
0
          evutil_socket_error_to_string(err), err);
1727
0
      return;
1728
0
    }
1729
0
    request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen, NULL);
1730
0
  }
1731
0
}
1732
1733
static int
1734
server_send_response(struct evdns_server_port *port, struct server_request *req)
1735
0
{
1736
0
  u16 packet_size = 0;
1737
0
  struct bufferevent *bev = NULL;
1738
0
  if (req->client) {
1739
0
    bev = req->client->connection.bev;
1740
0
    EVUTIL_ASSERT(bev);
1741
0
    EVUTIL_ASSERT(req->response_len <= 65535);
1742
0
    packet_size = htons((u16)req->response_len);
1743
0
    if (bufferevent_write(bev, &packet_size, sizeof(packet_size)))
1744
0
      goto beferevent_error;
1745
0
    if (bufferevent_write(bev, (void*)req->response, req->response_len))
1746
0
      goto beferevent_error;
1747
0
    return (int)req->response_len;
1748
0
  } else {
1749
0
    int r = sendto(port->socket, req->response, (int)req->response_len, 0,
1750
0
          (struct sockaddr*) &req->addr, (ev_socklen_t)req->addrlen);
1751
0
    return r;
1752
0
  }
1753
1754
0
beferevent_error:
1755
0
  log(EVDNS_LOG_WARN, "Failed to send reply to request %p for client %p", (void *)req, (void *)req->client);
1756
  /* disconnect if we got bufferevent error */
1757
0
  evdns_remove_tcp_client(port, req->client);
1758
0
  return -1;
1759
0
}
1760
1761
/* Try to write all pending replies on a given DNS server port. */
1762
static void
1763
server_port_flush(struct evdns_server_port *port)
1764
0
{
1765
0
  struct server_request *req = port->pending_replies;
1766
0
  ASSERT_LOCKED(port);
1767
0
  while (req) {
1768
0
    int r = server_send_response(port, req);
1769
0
    if (r < 0) {
1770
0
      int err = evutil_socket_geterror(port->socket);
1771
0
      if (EVUTIL_ERR_RW_RETRIABLE(err))
1772
0
        return;
1773
0
      log(EVDNS_LOG_WARN, "Error %s (%d) while writing response to port; dropping", evutil_socket_error_to_string(err), err);
1774
0
    }
1775
0
    if (server_request_free(req)) {
1776
      /* we released the last reference to req->port. */
1777
0
      return;
1778
0
    } else {
1779
0
      EVUTIL_ASSERT(req != port->pending_replies);
1780
0
      req = port->pending_replies;
1781
0
    }
1782
0
  }
1783
1784
  /* We have no more pending requests; stop listening for 'writeable' events. */
1785
0
  (void) event_del(&port->event);
1786
0
  event_assign(&port->event, port->event_base,
1787
0
         port->socket, EV_READ | EV_PERSIST,
1788
0
         server_port_ready_callback, port);
1789
1790
0
  if (event_add(&port->event, NULL) < 0) {
1791
0
    log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server.");
1792
    /* ???? Do more? */
1793
0
  }
1794
0
}
1795
1796
/* set if we are waiting for the ability to write to this server. */
1797
/* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1798
/* we stop these events. */
1799
static void
1800
0
nameserver_write_waiting(struct nameserver *ns, char waiting) {
1801
0
  ASSERT_LOCKED(ns->base);
1802
0
  if (ns->write_waiting == waiting) return;
1803
1804
0
  ns->write_waiting = waiting;
1805
0
  (void) event_del(&ns->event);
1806
0
  event_assign(&ns->event, ns->base->event_base,
1807
0
      ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
1808
0
      nameserver_ready_callback, ns);
1809
0
  if (event_add(&ns->event, NULL) < 0) {
1810
0
    char addrbuf[128];
1811
0
    log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
1812
0
        evutil_format_sockaddr_port_(
1813
0
          (struct sockaddr *)&ns->address,
1814
0
          addrbuf, sizeof(addrbuf)));
1815
    /* ???? Do more? */
1816
0
  }
1817
0
}
1818
1819
/* a callback function. Called by libevent when the kernel says that */
1820
/* a nameserver socket is ready for writing or reading */
1821
static void
1822
0
nameserver_ready_callback(evutil_socket_t fd, short events, void *arg) {
1823
0
  struct nameserver *ns = (struct nameserver *) arg;
1824
0
  (void)fd;
1825
1826
0
  EVDNS_LOCK(ns->base);
1827
0
  if (events & EV_WRITE) {
1828
0
    ns->choked = 0;
1829
0
    if (!evdns_transmit(ns->base)) {
1830
0
      nameserver_write_waiting(ns, 0);
1831
0
    }
1832
0
  }
1833
0
  if (events & EV_READ) {
1834
0
    nameserver_read(ns);
1835
0
  }
1836
0
  EVDNS_UNLOCK(ns->base);
1837
0
}
1838
1839
/* a callback function. Called by libevent when the kernel says that */
1840
/* a server socket is ready for writing or reading. */
1841
static void
1842
0
server_port_ready_callback(evutil_socket_t fd, short events, void *arg) {
1843
0
  struct evdns_server_port *port = (struct evdns_server_port *) arg;
1844
0
  (void) fd;
1845
1846
0
  EVDNS_LOCK(port);
1847
0
  if (events & EV_WRITE) {
1848
0
    port->choked = 0;
1849
0
    server_port_flush(port);
1850
0
  }
1851
0
  if (events & EV_READ) {
1852
0
    server_udp_port_read(port);
1853
0
  }
1854
0
  EVDNS_UNLOCK(port);
1855
0
}
1856
1857
/* This is an inefficient representation; only use it via the dnslabel_table_*
1858
 * functions, so that is can be safely replaced with something smarter later. */
1859
0
#define MAX_LABELS 128
1860
/* Structures used to implement name compression */
1861
struct dnslabel_entry { char *v; off_t pos; };
1862
struct dnslabel_table {
1863
  int n_labels; /* number of current entries */
1864
  /* map from name to position in message */
1865
  struct dnslabel_entry labels[MAX_LABELS];
1866
};
1867
1868
/* Initialize dnslabel_table. */
1869
static void
1870
dnslabel_table_init(struct dnslabel_table *table)
1871
0
{
1872
0
  table->n_labels = 0;
1873
0
}
1874
1875
/* Free all storage held by table, but not the table itself. */
1876
static void
1877
dnslabel_clear(struct dnslabel_table *table)
1878
0
{
1879
0
  int i;
1880
0
  for (i = 0; i < table->n_labels; ++i)
1881
0
    mm_free(table->labels[i].v);
1882
0
  table->n_labels = 0;
1883
0
}
1884
1885
/* return the position of the label in the current message, or -1 if the label */
1886
/* hasn't been used yet. */
1887
static int
1888
dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
1889
0
{
1890
0
  int i;
1891
0
  for (i = 0; i < table->n_labels; ++i) {
1892
0
    if (!strcmp(label, table->labels[i].v))
1893
0
      return table->labels[i].pos;
1894
0
  }
1895
0
  return -1;
1896
0
}
1897
1898
/* remember that we've used the label at position pos */
1899
static int
1900
dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
1901
0
{
1902
0
  char *v;
1903
0
  int p;
1904
0
  if (table->n_labels == MAX_LABELS)
1905
0
    return (-1);
1906
0
  v = mm_strdup(label);
1907
0
  if (v == NULL)
1908
0
    return (-1);
1909
0
  p = table->n_labels++;
1910
0
  table->labels[p].v = v;
1911
0
  table->labels[p].pos = pos;
1912
1913
0
  return (0);
1914
0
}
1915
1916
/* Converts a string to a length-prefixed set of DNS labels, starting */
1917
/* at buf[j]. name and buf must not overlap. name_len should be the length */
1918
/* of name.  table is optional, and is used for compression. */
1919
/* */
1920
/* Input: abc.def */
1921
/* Output: <3>abc<3>def<0> */
1922
/* */
1923
/* Returns the first index after the encoded name, or negative on error. */
1924
/*   -1  label was > 63 bytes */
1925
/*   -2  name too long to fit in buffer. */
1926
/* */
1927
static off_t
1928
dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
1929
          const char *name, const size_t name_len,
1930
0
          struct dnslabel_table *table) {
1931
0
  const char *end = name + name_len;
1932
0
  int ref = 0;
1933
0
  u16 t_;
1934
1935
0
#define APPEND16(x) do {           \
1936
0
    if (j + 2 > (off_t)buf_len)       \
1937
0
      goto overflow;         \
1938
0
    t_ = htons(x);            \
1939
0
    memcpy(buf + j, &t_, 2);        \
1940
0
    j += 2;             \
1941
0
  } while (0)
1942
0
#define APPEND32(x) do {           \
1943
0
    if (j + 4 > (off_t)buf_len)       \
1944
0
      goto overflow;         \
1945
0
    t32_ = htonl(x);          \
1946
0
    memcpy(buf + j, &t32_, 4);        \
1947
0
    j += 4;             \
1948
0
  } while (0)
1949
1950
0
  if (name_len > 255) return -2;
1951
1952
0
  for (;;) {
1953
0
    const char *const start = name;
1954
0
    if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
1955
0
      APPEND16(ref | 0xc000);
1956
0
      return j;
1957
0
    }
1958
0
    name = strchr(name, '.');
1959
0
    if (!name) {
1960
0
      const size_t label_len = end - start;
1961
0
      if (label_len > 63) return -1;
1962
0
      if ((size_t)(j+label_len+1) > buf_len) return -2;
1963
0
      if (table) dnslabel_table_add(table, start, j);
1964
0
      buf[j++] = (ev_uint8_t)label_len;
1965
1966
0
      memcpy(buf + j, start, label_len);
1967
0
      j += (int) label_len;
1968
0
      break;
1969
0
    } else {
1970
      /* append length of the label. */
1971
0
      const size_t label_len = name - start;
1972
0
      if (label_len > 63) return -1;
1973
0
      if ((size_t)(j+label_len+1) > buf_len) return -2;
1974
0
      if (table) dnslabel_table_add(table, start, j);
1975
0
      buf[j++] = (ev_uint8_t)label_len;
1976
1977
0
      memcpy(buf + j, start, label_len);
1978
0
      j += (int) label_len;
1979
      /* hop over the '.' */
1980
0
      name++;
1981
0
    }
1982
0
  }
1983
1984
  /* the labels must be terminated by a 0. */
1985
  /* It's possible that the name ended in a . */
1986
  /* in which case the zero is already there */
1987
0
  if ((size_t)j >= buf_len)
1988
0
    return -2;
1989
0
  if (!j || buf[j-1])
1990
0
    buf[j++] = 0;
1991
0
  return j;
1992
0
 overflow:
1993
0
  return (-2);
1994
0
}
1995
1996
/* Finds the length of a dns request for a DNS name of the given */
1997
/* length. The actual request may be smaller than the value returned */
1998
/* here */
1999
static size_t
2000
evdns_request_len(const struct evdns_base *base, const size_t name_len)
2001
0
{
2002
0
  int additional_section_len = 0;
2003
0
  if (EDNS_ENABLED(base)) {
2004
0
    additional_section_len = 1 + /* length of domain name string, always 0 */
2005
0
      2 + /* space for resource type */
2006
0
      2 + /* space for UDP payload size */
2007
0
      4 + /* space for extended RCODE flags */
2008
0
      2;  /* space for length of RDATA, always 0 */
2009
0
  }
2010
0
  return 96 + /* length of the DNS standard header */
2011
0
    name_len + 2 +
2012
0
    4 /* space for the resource type */ +
2013
0
    additional_section_len;
2014
0
}
2015
2016
/* build a dns request packet into buf. buf should be at least as long */
2017
/* as evdns_request_len told you it should be. */
2018
/* */
2019
/* Returns the amount of space used. Negative on error. */
2020
static int
2021
evdns_request_data_build(const struct evdns_base *base,
2022
  const char *const name, const size_t name_len,
2023
  const u16 trans_id, const u16 type, const u16 class, u8 *const buf,
2024
  size_t buf_len)
2025
0
{
2026
0
  off_t j = 0;  /* current offset into buf */
2027
0
  u16 t_;   /* used by the macros */
2028
0
  u32 t32_;  /* used by the macros */
2029
2030
0
  APPEND16(trans_id);
2031
0
  APPEND16(0x0100);  /* standard query, recursion needed */
2032
0
  APPEND16(1);  /* one question */
2033
0
  APPEND16(0);  /* no answers */
2034
0
  APPEND16(0);  /* no authority */
2035
0
  APPEND16(EDNS_ENABLED(base) ? 1 : 0); /* additional */
2036
2037
0
  j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
2038
0
  if (j < 0) {
2039
0
    return (int)j;
2040
0
  }
2041
2042
0
  APPEND16(type);
2043
0
  APPEND16(class);
2044
2045
0
  if (EDNS_ENABLED(base)) {
2046
    /* The OPT pseudo-RR format
2047
     * (https://tools.ietf.org/html/rfc6891#section-6.1.2)
2048
     * +------------+--------------+------------------------------+
2049
     * | Field Name | Field Type   | Description                  |
2050
     * +------------+--------------+------------------------------+
2051
     * | NAME       | domain name  | MUST be 0 (root domain)      |
2052
     * | TYPE       | u_int16_t    | OPT (41)                     |
2053
     * | CLASS      | u_int16_t    | requestor's UDP payload size |
2054
     * | TTL        | u_int32_t    | extended RCODE and flags     |
2055
     * | RDLEN      | u_int16_t    | length of all RDATA          |
2056
     * | RDATA      | octet stream | {attribute,value} pairs      |
2057
     * +------------+--------------+------------------------------+ */
2058
0
    buf[j++] = 0;  /* NAME, always 0 */
2059
0
    APPEND16(TYPE_OPT);  /* OPT type */
2060
0
    APPEND16(base->global_max_udp_size);  /* max UDP payload size */
2061
0
    APPEND32(0);  /* No extended RCODE flags set */
2062
0
    APPEND16(0);  /* length of RDATA is 0 */
2063
0
  }
2064
2065
0
  return (int)j;
2066
0
 overflow:
2067
0
  return (-1);
2068
0
}
2069
2070
/* exported function */
2071
struct evdns_server_port *
2072
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)
2073
0
{
2074
0
  struct evdns_server_port *port;
2075
0
  if (flags)
2076
0
    return NULL; /* flags not yet implemented */
2077
0
  if (!(port = mm_malloc(sizeof(struct evdns_server_port))))
2078
0
    return NULL;
2079
0
  memset(port, 0, sizeof(struct evdns_server_port));
2080
2081
2082
0
  port->socket = socket;
2083
0
  port->refcnt = 1;
2084
0
  port->choked = 0;
2085
0
  port->closing = 0;
2086
0
  port->user_callback = cb;
2087
0
  port->user_data = user_data;
2088
0
  port->pending_replies = NULL;
2089
0
  port->event_base = base;
2090
0
  port->max_client_connections = MAX_CLIENT_CONNECTIONS;
2091
0
  port->tcp_idle_timeout.tv_sec = SERVER_IDLE_CONN_TIMEOUT;
2092
0
  port->tcp_idle_timeout.tv_usec = 0;
2093
0
  port->client_connections_count = 0;
2094
0
  LIST_INIT(&port->client_connections);
2095
0
  event_assign(&port->event, port->event_base,
2096
0
         port->socket, EV_READ | EV_PERSIST,
2097
0
         server_port_ready_callback, port);
2098
0
  if (event_add(&port->event, NULL) < 0) {
2099
0
    mm_free(port);
2100
0
    return NULL;
2101
0
  }
2102
0
  EVTHREAD_ALLOC_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
2103
0
  return port;
2104
0
}
2105
2106
/* exported function */
2107
struct evdns_server_port *
2108
evdns_add_server_port_with_listener(struct event_base *base, struct evconnlistener *listener, int flags, evdns_request_callback_fn_type cb, void *user_data)
2109
0
{
2110
0
  struct evdns_server_port *port;
2111
0
  if (!listener)
2112
0
    return NULL;
2113
0
  if (flags)
2114
0
    return NULL; /* flags not yet implemented */
2115
2116
0
  if (!(port = mm_calloc(1, sizeof(struct evdns_server_port))))
2117
0
    return NULL;
2118
0
  port->socket = -1;
2119
0
  port->refcnt = 1;
2120
0
  port->choked = 0;
2121
0
  port->closing = 0;
2122
0
  port->user_callback = cb;
2123
0
  port->user_data = user_data;
2124
0
  port->pending_replies = NULL;
2125
0
  port->event_base = base;
2126
0
  port->max_client_connections = MAX_CLIENT_CONNECTIONS;
2127
0
  port->client_connections_count = 0;
2128
0
  LIST_INIT(&port->client_connections);
2129
0
  port->listener = listener;
2130
0
  evconnlistener_set_cb(port->listener, incoming_conn_cb, port);
2131
2132
0
  EVTHREAD_ALLOC_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
2133
0
  return port;
2134
0
}
2135
2136
static void
2137
server_tcp_event_cb(struct bufferevent *bev, short events, void *ctx);
2138
2139
static int
2140
tcp_read_message(struct tcp_connection *conn, u8 **msg, int *msg_len)
2141
0
{
2142
0
  struct bufferevent *bev = conn->bev;
2143
0
  struct evbuffer *input = bufferevent_get_input(bev);
2144
0
  u8 *packet = NULL;
2145
0
  int r = 0;
2146
2147
0
  EVUTIL_ASSERT(conn);
2148
0
  EVUTIL_ASSERT(conn->state == TS_CONNECTED);
2149
2150
  /* reading new packet size */
2151
0
  if (!conn->awaiting_packet_size) {
2152
0
    if (evbuffer_get_length(input) < sizeof(ev_uint16_t))
2153
0
      goto awaiting_next;
2154
2155
0
    bufferevent_read(bev, (void*)&conn->awaiting_packet_size,
2156
0
      sizeof(conn->awaiting_packet_size));
2157
0
    conn->awaiting_packet_size = ntohs(conn->awaiting_packet_size);
2158
0
    if (conn->awaiting_packet_size <= 0)
2159
0
      goto fail;
2160
0
  }
2161
2162
  /* reading new packet content */
2163
0
  if (evbuffer_get_length(input) < conn->awaiting_packet_size)
2164
0
    goto awaiting_next;
2165
2166
0
  packet = mm_malloc(conn->awaiting_packet_size);
2167
0
  if (!packet)
2168
0
    goto fail;
2169
2170
0
  r = (int)bufferevent_read(bev, (void*)packet, conn->awaiting_packet_size);
2171
0
  if (r != conn->awaiting_packet_size) {
2172
0
    mm_free(packet);
2173
0
    packet = NULL;
2174
0
    goto fail;
2175
0
  }
2176
2177
0
  *msg = packet;
2178
0
  *msg_len = r;
2179
0
awaiting_next:
2180
0
  return 0;
2181
0
fail:
2182
0
  return 1;
2183
0
}
2184
2185
static void
2186
server_tcp_read_packet_cb(struct bufferevent *bev, void *ctx)
2187
0
{
2188
0
  u8 *msg = NULL;
2189
0
  int msg_len = 0;
2190
0
  int rc;
2191
0
  struct client_tcp_connection *client = (struct client_tcp_connection *)ctx;
2192
0
  struct evdns_server_port *port = client->port;
2193
0
  struct tcp_connection *conn = &client->connection;
2194
0
  EVUTIL_ASSERT(port && bev);
2195
0
  EVDNS_LOCK(port);
2196
2197
0
  while (1) {
2198
0
    if (tcp_read_message(conn, &msg, &msg_len)) {
2199
0
      log(EVDNS_LOG_MSG, "Closing client connection %p due to error", (void *)bev);
2200
0
      evdns_remove_tcp_client(port, client);
2201
0
      rc = port->refcnt;
2202
0
      EVDNS_UNLOCK(port);
2203
0
      if (!rc)
2204
0
        server_port_free(port);
2205
0
      return;
2206
0
    }
2207
2208
    /* Only part of the message was received. */
2209
0
    if (!msg)
2210
0
      break;
2211
2212
0
    request_parse(msg, msg_len, port, NULL, 0, client);
2213
0
    mm_free(msg);
2214
0
    msg = NULL;
2215
0
    conn->awaiting_packet_size = 0;
2216
0
  }
2217
2218
0
  bufferevent_setwatermark(bev, EV_READ,
2219
0
      conn->awaiting_packet_size ? conn->awaiting_packet_size : sizeof(ev_uint16_t), 0);
2220
0
  bufferevent_setcb(bev, server_tcp_read_packet_cb, NULL, server_tcp_event_cb, ctx);
2221
0
  EVDNS_UNLOCK(port);
2222
0
}
2223
2224
static void
2225
server_tcp_event_cb(struct bufferevent *bev, short events, void *ctx)
2226
0
{
2227
0
  struct client_tcp_connection *client = (struct client_tcp_connection *)ctx;
2228
0
  struct evdns_server_port *port = client->port;
2229
0
  int rc;
2230
0
  EVUTIL_ASSERT(port && bev);
2231
0
  EVDNS_LOCK(port);
2232
0
  if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
2233
0
    log(EVDNS_LOG_DEBUG, "Closing connection %p", (void *)bev);
2234
0
    evdns_remove_tcp_client(port, client);
2235
0
  }
2236
0
  rc = port->refcnt;
2237
0
  EVDNS_UNLOCK(port);
2238
0
  if (!rc)
2239
0
    server_port_free(port);
2240
0
}
2241
2242
static void
2243
incoming_conn_cb(struct evconnlistener *listener, evutil_socket_t fd,
2244
          struct sockaddr *address, int socklen, void *arg)
2245
0
{
2246
0
  struct evdns_server_port *port = (struct evdns_server_port*)arg;
2247
0
  struct bufferevent *bev = bufferevent_socket_new(port->event_base, fd, BEV_OPT_CLOSE_ON_FREE);
2248
0
  struct client_tcp_connection *client = NULL;
2249
0
  struct tcp_connection *cd = NULL;
2250
2251
0
  if (!bev)
2252
0
    goto error;
2253
0
  log(EVDNS_LOG_DEBUG, "New incoming client connection %p", (void *)bev);
2254
2255
0
  bufferevent_set_timeouts(bev, &port->tcp_idle_timeout, &port->tcp_idle_timeout);
2256
2257
0
  client = evdns_add_tcp_client(port, bev);
2258
0
  if (!client)
2259
0
    goto error;
2260
0
  cd = &client->connection;
2261
2262
0
  cd->state = TS_CONNECTED;
2263
0
  bufferevent_setwatermark(bev, EV_READ, sizeof(ev_uint16_t), 0);
2264
0
  bufferevent_setcb(bev, server_tcp_read_packet_cb, NULL, server_tcp_event_cb, (void *)client);
2265
0
  bufferevent_enable(bev, EV_READ);
2266
2267
0
  return;
2268
0
error:
2269
0
  if (bev)
2270
0
    bufferevent_free(bev);
2271
0
  return;
2272
0
}
2273
2274
struct evdns_server_port *
2275
evdns_add_server_port(evutil_socket_t socket, int flags, evdns_request_callback_fn_type cb, void *user_data)
2276
0
{
2277
0
  return evdns_add_server_port_with_base(NULL, socket, flags, cb, user_data);
2278
0
}
2279
2280
/* exported function */
2281
void
2282
evdns_close_server_port(struct evdns_server_port *port)
2283
0
{
2284
0
  EVDNS_LOCK(port);
2285
0
  evdns_remove_all_tcp_clients(port);
2286
0
  if (--port->refcnt == 0) {
2287
0
    EVDNS_UNLOCK(port);
2288
0
    server_port_free(port);
2289
0
  } else {
2290
0
    port->closing = 1;
2291
0
    EVDNS_UNLOCK(port);
2292
0
  }
2293
0
}
2294
2295
/* exported function */
2296
int
2297
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)
2298
0
{
2299
0
  struct server_request *req = TO_SERVER_REQUEST(req_);
2300
0
  struct server_reply_item **itemp, *item;
2301
0
  int *countp;
2302
0
  int result = -1;
2303
2304
0
  EVDNS_LOCK(req->port);
2305
0
  if (req->response) /* have we already answered? */
2306
0
    goto done;
2307
2308
0
  switch (section) {
2309
0
  case EVDNS_ANSWER_SECTION:
2310
0
    itemp = &req->answer;
2311
0
    countp = &req->n_answer;
2312
0
    break;
2313
0
  case EVDNS_AUTHORITY_SECTION:
2314
0
    itemp = &req->authority;
2315
0
    countp = &req->n_authority;
2316
0
    break;
2317
0
  case EVDNS_ADDITIONAL_SECTION:
2318
0
    itemp = &req->additional;
2319
0
    countp = &req->n_additional;
2320
0
    break;
2321
0
  default:
2322
0
    goto done;
2323
0
  }
2324
0
  while (*itemp) {
2325
0
    itemp = &((*itemp)->next);
2326
0
  }
2327
0
  item = mm_malloc(sizeof(struct server_reply_item));
2328
0
  if (!item)
2329
0
    goto done;
2330
0
  item->next = NULL;
2331
0
  if (!(item->name = mm_strdup(name))) {
2332
0
    mm_free(item);
2333
0
    goto done;
2334
0
  }
2335
0
  item->type = type;
2336
0
  item->dns_question_class = class;
2337
0
  item->ttl = ttl;
2338
0
  item->is_name = is_name != 0;
2339
0
  item->datalen = 0;
2340
0
  item->data = NULL;
2341
0
  if (data) {
2342
0
    if (item->is_name) {
2343
0
      if (!(item->data = mm_strdup(data))) {
2344
0
        mm_free(item->name);
2345
0
        mm_free(item);
2346
0
        goto done;
2347
0
      }
2348
0
      item->datalen = (u16)-1;
2349
0
    } else {
2350
0
      if (!(item->data = mm_malloc(datalen))) {
2351
0
        mm_free(item->name);
2352
0
        mm_free(item);
2353
0
        goto done;
2354
0
      }
2355
0
      item->datalen = datalen;
2356
0
      memcpy(item->data, data, datalen);
2357
0
    }
2358
0
  }
2359
2360
0
  *itemp = item;
2361
0
  ++(*countp);
2362
0
  result = 0;
2363
0
done:
2364
0
  EVDNS_UNLOCK(req->port);
2365
0
  return result;
2366
0
}
2367
2368
/* exported function */
2369
int
2370
evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl)
2371
0
{
2372
0
  return evdns_server_request_add_reply(
2373
0
      req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
2374
0
      ttl, n*4, 0, addrs);
2375
0
}
2376
2377
/* exported function */
2378
int
2379
evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl)
2380
0
{
2381
0
  return evdns_server_request_add_reply(
2382
0
      req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
2383
0
      ttl, n*16, 0, addrs);
2384
0
}
2385
2386
/* exported function */
2387
int
2388
evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
2389
0
{
2390
0
  u32 a;
2391
0
  char buf[32];
2392
0
  if (in && inaddr_name)
2393
0
    return -1;
2394
0
  else if (!in && !inaddr_name)
2395
0
    return -1;
2396
0
  if (in) {
2397
0
    a = ntohl(in->s_addr);
2398
0
    evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
2399
0
        (int)(u8)((a  )&0xff),
2400
0
        (int)(u8)((a>>8 )&0xff),
2401
0
        (int)(u8)((a>>16)&0xff),
2402
0
        (int)(u8)((a>>24)&0xff));
2403
0
    inaddr_name = buf;
2404
0
  }
2405
0
  return evdns_server_request_add_reply(
2406
0
      req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
2407
0
      ttl, -1, 1, hostname);
2408
0
}
2409
2410
/* exported function */
2411
int
2412
evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
2413
0
{
2414
0
  return evdns_server_request_add_reply(
2415
0
      req, EVDNS_ANSWER_SECTION, name, TYPE_CNAME, CLASS_INET,
2416
0
      ttl, -1, 1, cname);
2417
0
}
2418
2419
/* exported function */
2420
void
2421
evdns_server_request_set_flags(struct evdns_server_request *exreq, int flags)
2422
0
{
2423
0
  struct server_request *req = TO_SERVER_REQUEST(exreq);
2424
0
  req->base.flags &= ~(EVDNS_FLAGS_AA|EVDNS_FLAGS_RD);
2425
0
  req->base.flags |= flags;
2426
0
}
2427
2428
static int
2429
evdns_server_request_format_response(struct server_request *req, int err)
2430
0
{
2431
0
  unsigned char buf[1024 * 64];
2432
0
  size_t buf_len = sizeof(buf);
2433
0
  off_t j = 0, r;
2434
0
  u16 t_;
2435
0
  u32 t32_;
2436
0
  int i;
2437
0
  u16 flags;
2438
0
  struct dnslabel_table table;
2439
2440
0
  if (err < 0 || err > 15) return -1;
2441
2442
  /* Set response bit and error code; copy OPCODE and RD fields from
2443
   * question; copy RA and AA if set by caller. */
2444
0
  flags = req->base.flags;
2445
0
  flags |= (_QR_MASK | err);
2446
2447
0
  dnslabel_table_init(&table);
2448
0
  APPEND16(req->trans_id);
2449
0
  APPEND16(flags);
2450
0
  APPEND16(req->base.nquestions);
2451
0
  APPEND16(req->n_answer);
2452
0
  APPEND16(req->n_authority);
2453
0
  APPEND16(req->n_additional);
2454
2455
  /* Add questions. */
2456
0
  for (i=0; i < req->base.nquestions; ++i) {
2457
0
    const char *s = req->base.questions[i]->name;
2458
0
    j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
2459
0
    if (j < 0) {
2460
0
      dnslabel_clear(&table);
2461
0
      return (int) j;
2462
0
    }
2463
0
    APPEND16(req->base.questions[i]->type);
2464
0
    APPEND16(req->base.questions[i]->dns_question_class);
2465
0
  }
2466
2467
  /* Add answer, authority, and additional sections. */
2468
0
  for (i=0; i<3; ++i) {
2469
0
    struct server_reply_item *item;
2470
0
    if (i==0)
2471
0
      item = req->answer;
2472
0
    else if (i==1)
2473
0
      item = req->authority;
2474
0
    else
2475
0
      item = req->additional;
2476
0
    while (item) {
2477
0
      r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
2478
0
      if (r < 0)
2479
0
        goto overflow;
2480
0
      j = r;
2481
2482
0
      APPEND16(item->type);
2483
0
      APPEND16(item->dns_question_class);
2484
0
      APPEND32(item->ttl);
2485
0
      if (item->is_name) {
2486
0
        off_t len_idx = j, name_start;
2487
0
        j += 2;
2488
0
        name_start = j;
2489
0
        r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
2490
0
        if (r < 0)
2491
0
          goto overflow;
2492
0
        j = r;
2493
0
        t_ = htons( (short) (j-name_start) );
2494
0
        memcpy(buf+len_idx, &t_, 2);
2495
0
      } else {
2496
0
        APPEND16(item->datalen);
2497
0
        if (j+item->datalen > (off_t)buf_len)
2498
0
          goto overflow;
2499
0
        if (item->data) {
2500
0
          memcpy(buf+j, item->data, item->datalen);
2501
0
          j += item->datalen;
2502
0
        } else {
2503
0
          EVUTIL_ASSERT(item->datalen == 0);
2504
0
        }
2505
0
      }
2506
0
      item = item->next;
2507
0
    }
2508
0
  }
2509
2510
0
  if (j > req->max_udp_reply_size && !req->client) {
2511
0
overflow:
2512
0
    j = req->max_udp_reply_size;
2513
0
    buf[2] |= 0x02; /* set the truncated bit. */
2514
0
  }
2515
2516
0
  req->response_len = j;
2517
2518
0
  if (!(req->response = mm_malloc(req->response_len))) {
2519
0
    server_request_free_answers(req);
2520
0
    dnslabel_clear(&table);
2521
0
    return (-1);
2522
0
  }
2523
0
  memcpy(req->response, buf, req->response_len);
2524
0
  server_request_free_answers(req);
2525
0
  dnslabel_clear(&table);
2526
0
  return (0);
2527
0
}
2528
2529
/* exported function */
2530
int
2531
evdns_server_request_respond(struct evdns_server_request *req_, int err)
2532
0
{
2533
0
  struct server_request *req = TO_SERVER_REQUEST(req_);
2534
0
  struct evdns_server_port *port = req->port;
2535
0
  int r = -1;
2536
2537
0
  EVDNS_LOCK(port);
2538
0
  if (!req->response) {
2539
0
    if ((r = evdns_server_request_format_response(req, err))<0)
2540
0
      goto done;
2541
0
  }
2542
2543
0
  r = server_send_response(port, req);
2544
0
  if (r < 0 && req->client) {
2545
0
    int sock_err = evutil_socket_geterror(port->socket);
2546
0
    if (EVUTIL_ERR_RW_RETRIABLE(sock_err))
2547
0
      goto done;
2548
2549
0
    if (port->pending_replies) {
2550
0
      req->prev_pending = port->pending_replies->prev_pending;
2551
0
      req->next_pending = port->pending_replies;
2552
0
      req->prev_pending->next_pending =
2553
0
        req->next_pending->prev_pending = req;
2554
0
    } else {
2555
0
      req->prev_pending = req->next_pending = req;
2556
0
      port->pending_replies = req;
2557
0
      port->choked = 1;
2558
2559
0
      (void) event_del(&port->event);
2560
0
      event_assign(&port->event, port->event_base, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
2561
2562
0
      if (event_add(&port->event, NULL) < 0) {
2563
0
        log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server");
2564
0
      }
2565
2566
0
    }
2567
2568
0
    r = 1;
2569
0
    goto done;
2570
0
  }
2571
0
  if (server_request_free(req)) {
2572
0
    r = 0;
2573
0
    goto done;
2574
0
  }
2575
2576
0
  if (port->pending_replies)
2577
0
    server_port_flush(port);
2578
2579
0
  r = 0;
2580
0
done:
2581
0
  EVDNS_UNLOCK(port);
2582
0
  return r;
2583
0
}
2584
2585
/* Free all storage held by RRs in req. */
2586
static void
2587
server_request_free_answers(struct server_request *req)
2588
0
{
2589
0
  struct server_reply_item *victim, *next, **list;
2590
0
  int i;
2591
0
  for (i = 0; i < 3; ++i) {
2592
0
    if (i==0)
2593
0
      list = &req->answer;
2594
0
    else if (i==1)
2595
0
      list = &req->authority;
2596
0
    else
2597
0
      list = &req->additional;
2598
2599
0
    victim = *list;
2600
0
    while (victim) {
2601
0
      next = victim->next;
2602
0
      mm_free(victim->name);
2603
0
      victim->name = NULL;
2604
0
      if (victim->data) {
2605
0
        mm_free(victim->data);
2606
0
        victim->data = NULL;
2607
0
      }
2608
0
      mm_free(victim);
2609
0
      victim = next;
2610
0
    }
2611
0
    *list = NULL;
2612
0
  }
2613
0
}
2614
2615
/* Free all storage held by req, and remove links to it. */
2616
/* return true iff we just wound up freeing the server_port. */
2617
static int
2618
server_request_free(struct server_request *req)
2619
0
{
2620
0
  int i, rc=1, lock=0;
2621
0
  if (req->base.questions) {
2622
0
    for (i = 0; i < req->base.nquestions; ++i) {
2623
0
      mm_free(req->base.questions[i]);
2624
0
      req->base.questions[i] = NULL;
2625
0
    }
2626
0
    mm_free(req->base.questions);
2627
0
    req->base.questions = NULL;
2628
0
  }
2629
2630
0
  if (req->port) {
2631
0
    EVDNS_LOCK(req->port);
2632
0
    lock=1;
2633
0
    if (req->port->pending_replies == req) {
2634
0
      if (req->next_pending && req->next_pending != req)
2635
0
        req->port->pending_replies = req->next_pending;
2636
0
      else
2637
0
        req->port->pending_replies = NULL;
2638
0
    }
2639
0
    rc = --req->port->refcnt;
2640
0
  }
2641
2642
0
  if (req->response) {
2643
0
    mm_free(req->response);
2644
0
    req->response = NULL;
2645
0
  }
2646
2647
0
  server_request_free_answers(req);
2648
2649
0
  if (req->next_pending && req->next_pending != req) {
2650
0
    req->next_pending->prev_pending = req->prev_pending;
2651
0
    req->prev_pending->next_pending = req->next_pending;
2652
0
  }
2653
2654
0
  if (rc == 0) {
2655
0
    EVDNS_UNLOCK(req->port); /* ????? nickm */
2656
0
    server_port_free(req->port);
2657
0
    mm_free(req);
2658
0
    return (1);
2659
0
  }
2660
0
  if (lock)
2661
0
    EVDNS_UNLOCK(req->port);
2662
0
  mm_free(req);
2663
0
  return (0);
2664
0
}
2665
2666
/* Free all storage held by an evdns_server_port.  Only called when  */
2667
static void
2668
server_port_free(struct evdns_server_port *port)
2669
0
{
2670
0
  EVUTIL_ASSERT(port);
2671
0
  EVUTIL_ASSERT(!port->refcnt);
2672
0
  EVUTIL_ASSERT(!port->pending_replies);
2673
0
  if (port->socket > 0) {
2674
0
    evutil_closesocket(port->socket);
2675
0
    port->socket = -1;
2676
0
  }
2677
2678
  /* if tcp server */
2679
0
  if (port->listener) {
2680
0
    evconnlistener_free(port->listener);
2681
0
  } else {
2682
0
    (void) event_del(&port->event);
2683
0
    event_debug_unassign(&port->event);
2684
0
  }
2685
2686
0
  EVTHREAD_FREE_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
2687
0
  mm_free(port);
2688
0
}
2689
2690
/* exported function */
2691
int
2692
evdns_server_request_drop(struct evdns_server_request *req_)
2693
0
{
2694
0
  struct server_request *req = TO_SERVER_REQUEST(req_);
2695
0
  server_request_free(req);
2696
0
  return 0;
2697
0
}
2698
2699
/* exported function */
2700
int
2701
evdns_server_request_get_requesting_addr(struct evdns_server_request *req_, struct sockaddr *sa, int addr_len)
2702
0
{
2703
0
  struct server_request *req = TO_SERVER_REQUEST(req_);
2704
0
  if (addr_len < (int)req->addrlen)
2705
0
    return -1;
2706
0
  memcpy(sa, &(req->addr), req->addrlen);
2707
0
  return req->addrlen;
2708
0
}
2709
2710
static void
2711
retransmit_all_tcp_requests_for(struct nameserver *server)
2712
0
{
2713
0
  int i = 0;
2714
0
  for (i = 0; i < server->base->n_req_heads; ++i) {
2715
0
    struct request *started_at = server->base->req_heads[i];
2716
0
    struct request *req = started_at;
2717
0
    if (!req)
2718
0
      continue;
2719
2720
0
    do {
2721
0
      if (req->ns == server && (req->handle->tcp_flags & DNS_QUERY_USEVC)) {
2722
0
        if (req->tx_count >= req->base->global_max_retransmits) {
2723
0
          log(EVDNS_LOG_DEBUG, "Giving up on request %p; tx_count==%d",
2724
0
            (void *)req, req->tx_count);
2725
0
          reply_schedule_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
2726
0
          request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
2727
0
        } else {
2728
0
          (void) evtimer_del(&req->timeout_event);
2729
0
          evdns_request_transmit(req);
2730
0
        }
2731
0
      }
2732
0
      req = req->next;
2733
0
    } while (req != started_at);
2734
0
  }
2735
0
}
2736
2737
/* this is a libevent callback function which is called when a request */
2738
/* has timed out. */
2739
static void
2740
0
evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg) {
2741
0
  struct request *const req = (struct request *) arg;
2742
0
  struct evdns_base *base = req->base;
2743
2744
0
  (void) fd;
2745
0
  (void) events;
2746
2747
0
  log(EVDNS_LOG_DEBUG, "Request %p timed out", arg);
2748
0
  EVDNS_LOCK(base);
2749
2750
0
  if (req->tx_count >= req->base->global_max_retransmits) {
2751
0
    struct nameserver *ns = req->ns;
2752
    /* this request has failed */
2753
0
    log(EVDNS_LOG_DEBUG, "Giving up on request %p; tx_count==%d",
2754
0
        arg, req->tx_count);
2755
0
    reply_schedule_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
2756
2757
0
    request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
2758
0
    nameserver_failed(ns, "request timed out.", 0);
2759
0
  } else {
2760
    /* if request is using tcp connection, so tear connection */
2761
0
    if (req->handle->tcp_flags & DNS_QUERY_USEVC) {
2762
0
      disconnect_and_free_connection(req->ns->connection);
2763
0
      req->ns->connection = NULL;
2764
2765
      /* client can have the only connection to DNS server */
2766
0
      retransmit_all_tcp_requests_for(req->ns);
2767
0
    } else {
2768
      /* retransmit it */
2769
0
      log(EVDNS_LOG_DEBUG, "Retransmitting request %p; tx_count==%d by udp", arg, req->tx_count);
2770
0
      (void) evtimer_del(&req->timeout_event);
2771
0
      request_swap_ns(req, nameserver_pick(base));
2772
0
      evdns_request_transmit(req);
2773
2774
0
      req->ns->timedout++;
2775
0
      if (req->ns->timedout > req->base->global_max_nameserver_timeout) {
2776
0
        req->ns->timedout = 0;
2777
0
        nameserver_failed(req->ns, "request timed out.", 0);
2778
0
      }
2779
0
    }
2780
0
  }
2781
2782
0
  EVDNS_UNLOCK(base);
2783
0
}
2784
2785
/* try to send a request to a given server. */
2786
/* */
2787
/* return: */
2788
/*   0 ok */
2789
/*   1 temporary failure */
2790
/*   2 other failure */
2791
static int
2792
0
evdns_request_transmit_to(struct request *req, struct nameserver *server) {
2793
0
  int r;
2794
0
  ASSERT_LOCKED(req->base);
2795
0
  ASSERT_VALID_REQUEST(req);
2796
2797
0
  if (server->requests_inflight == 1 &&
2798
0
    req->base->disable_when_inactive &&
2799
0
    event_add(&server->event, NULL) < 0) {
2800
0
    return 1;
2801
0
  }
2802
2803
0
  r = sendto(server->socket, (void*)req->request, req->request_len, 0,
2804
0
      (struct sockaddr *)&server->address, server->addrlen);
2805
0
  if (r < 0) {
2806
0
    int err = evutil_socket_geterror(server->socket);
2807
0
    if (EVUTIL_ERR_RW_RETRIABLE(err))
2808
0
      return 1;
2809
0
    nameserver_failed(req->ns, evutil_socket_error_to_string(err), err);
2810
0
    return 2;
2811
0
  } else if (r != (int)req->request_len) {
2812
0
    return 1;  /* short write */
2813
0
  } else {
2814
0
    return 0;
2815
0
  }
2816
0
}
2817
2818
/* try to connect to a given server. */
2819
/* */
2820
/* return: */
2821
/*   0 ok */
2822
/*   1 temporary failure */
2823
/*   2 other failure */
2824
static int
2825
evdns_tcp_connect_if_disconnected(struct nameserver *server)
2826
0
{
2827
0
  struct tcp_connection *conn = server->connection;
2828
0
  struct timeval *timeout = &server->base->global_tcp_idle_timeout;
2829
0
  if (conn && conn->state != TS_DISCONNECTED && conn->bev != NULL)
2830
0
    return 0;
2831
2832
0
  disconnect_and_free_connection(conn);
2833
0
  conn = new_tcp_connection(bufferevent_socket_new(server->base->event_base, -1, BEV_OPT_CLOSE_ON_FREE));
2834
0
  if (!conn)
2835
0
    return 2;
2836
0
  server->connection = conn;
2837
2838
0
  if (bufferevent_set_timeouts(conn->bev, timeout, timeout))
2839
0
    return 1;
2840
2841
0
  EVUTIL_ASSERT(conn->state == TS_DISCONNECTED);
2842
0
  if (bufferevent_socket_connect(conn->bev, (struct sockaddr *)&server->address, server->addrlen))
2843
0
    return 1;
2844
2845
0
  conn->state = TS_CONNECTING;
2846
0
  log(EVDNS_LOG_DEBUG, "New tcp connection %p created", (void *)conn);
2847
0
  return 0;
2848
0
}
2849
2850
static void
2851
client_tcp_event_cb(struct bufferevent *bev, short events, void *ctx);
2852
2853
2854
static void
2855
client_tcp_read_packet_cb(struct bufferevent *bev, void *ctx)
2856
0
{
2857
0
  u8 *msg = NULL;
2858
0
  int msg_len = 0;
2859
0
  struct nameserver *server = (struct nameserver*)ctx;
2860
0
  struct tcp_connection *conn = server->connection;
2861
0
  EVUTIL_ASSERT(server && bev);
2862
0
  EVDNS_LOCK(server->base);
2863
2864
0
  while (1) {
2865
0
    if (tcp_read_message(conn, &msg, &msg_len)) {
2866
0
      disconnect_and_free_connection(server->connection);
2867
0
      server->connection = NULL;
2868
0
      EVDNS_UNLOCK(server->base);
2869
0
      return;
2870
0
    }
2871
2872
    /* Only part of the message was received. */
2873
0
    if (!msg)
2874
0
      break;
2875
2876
0
    reply_parse(server->base, msg, msg_len);
2877
0
    mm_free(msg);
2878
0
    msg = NULL;
2879
0
    if (server->connection == NULL) {
2880
      /* Some errors occurred in reply_parse, and TCP connection has been
2881
       * close. Stop reading from it. */
2882
0
      EVDNS_UNLOCK(server->base);
2883
0
      return;
2884
0
    }
2885
0
    conn->awaiting_packet_size = 0;
2886
0
  }
2887
2888
0
  bufferevent_setwatermark(bev, EV_READ,
2889
0
    conn->awaiting_packet_size ? conn->awaiting_packet_size : sizeof(ev_uint16_t), 0);
2890
0
  bufferevent_setcb(bev, client_tcp_read_packet_cb, NULL, client_tcp_event_cb, ctx);
2891
0
  EVDNS_UNLOCK(server->base);
2892
0
}
2893
2894
static void
2895
0
client_tcp_event_cb(struct bufferevent *bev, short events, void *ctx) {
2896
0
  struct nameserver *server = (struct nameserver*)ctx;
2897
0
  struct tcp_connection *conn = server->connection;
2898
0
  EVUTIL_ASSERT(server);
2899
0
  EVDNS_LOCK(server->base);
2900
0
  EVUTIL_ASSERT(conn && conn->bev == bev && bev);
2901
2902
0
  log(EVDNS_LOG_DEBUG, "Event %d on connection %p", events, (void *)conn);
2903
2904
0
  if (events & (BEV_EVENT_TIMEOUT)) {
2905
0
    disconnect_and_free_connection(server->connection);
2906
0
    server->connection = NULL;
2907
0
  } else if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
2908
0
    disconnect_and_free_connection(server->connection);
2909
0
    server->connection = NULL;
2910
0
  } else if (events & BEV_EVENT_CONNECTED) {
2911
0
    EVUTIL_ASSERT (conn->state == TS_CONNECTING);
2912
0
    conn->state = TS_CONNECTED;
2913
0
    evutil_make_socket_nonblocking(bufferevent_getfd(bev));
2914
0
    bufferevent_setcb(bev, client_tcp_read_packet_cb, NULL, client_tcp_event_cb, server);
2915
0
    bufferevent_setwatermark(bev, EV_READ, sizeof(ev_uint16_t), 0);
2916
0
  }
2917
0
  EVDNS_UNLOCK(server->base);
2918
0
}
2919
2920
/* try to send a request to a given server. */
2921
/* */
2922
/* return: */
2923
/*   0 ok */
2924
/*   1 temporary failure */
2925
/*   2 other failure */
2926
static int
2927
0
evdns_request_transmit_through_tcp(struct request *req, struct nameserver *server) {
2928
0
  uint16_t packet_size;
2929
0
  struct tcp_connection *conn = NULL;
2930
0
  int r;
2931
0
  ASSERT_LOCKED(req->base);
2932
0
  ASSERT_VALID_REQUEST(req);
2933
2934
0
  if ((r = evdns_tcp_connect_if_disconnected(server)))
2935
0
    return r;
2936
2937
0
  conn = server->connection;
2938
0
  bufferevent_setcb(conn->bev, client_tcp_read_packet_cb, NULL, client_tcp_event_cb, server);
2939
2940
0
  log(EVDNS_LOG_DEBUG, "Sending request %p via tcp connection %p", (void *)req, (void *)conn);
2941
0
  packet_size = htons(req->request_len);
2942
0
  if (bufferevent_write(conn->bev, &packet_size, sizeof(packet_size)) )
2943
0
    goto fail;
2944
0
  if (bufferevent_write(conn->bev, (void*)req->request, req->request_len) )
2945
0
    goto fail;
2946
0
  if (bufferevent_enable(conn->bev, EV_READ))
2947
0
    goto fail;
2948
0
  if (evtimer_add(&req->timeout_event, &req->base->global_timeout) < 0)
2949
0
    goto fail;
2950
2951
0
  return 0;
2952
0
fail:
2953
0
  log(EVDNS_LOG_WARN, "Failed to send request %p via tcp connection %p", (void *)req, (void *)conn);
2954
0
  disconnect_and_free_connection(server->connection);
2955
0
  server->connection = NULL;
2956
0
  return 2;
2957
0
}
2958
2959
/* try to send a request, updating the fields of the request */
2960
/* as needed */
2961
/* */
2962
/* return: */
2963
/*   0 ok */
2964
/*   1 failed */
2965
static int
2966
0
evdns_request_transmit(struct request *req) {
2967
0
  int retcode = 0, r;
2968
2969
0
  ASSERT_LOCKED(req->base);
2970
0
  ASSERT_VALID_REQUEST(req);
2971
  /* if we fail to send this packet then this flag marks it */
2972
  /* for evdns_transmit */
2973
0
  req->transmit_me = 1;
2974
0
  EVUTIL_ASSERT(req->trans_id != 0xffff);
2975
2976
0
  if (!req->ns)
2977
0
  {
2978
    /* unable to transmit request if no nameservers */
2979
0
    return 1;
2980
0
  }
2981
2982
0
  if (req->ns->choked) {
2983
    /* don't bother trying to write to a socket */
2984
    /* which we have had EAGAIN from */
2985
0
    return 1;
2986
0
  }
2987
2988
0
  if (req->handle->tcp_flags & DNS_QUERY_USEVC) {
2989
0
    r = evdns_request_transmit_through_tcp(req, req->ns);
2990
    /*
2991
    If connection didn't initiated now, so report about temporary problems.
2992
    We don't mark name server as chocked so udp packets possibly have no
2993
    problems during transmit. Simply we will retry attempt later */
2994
0
    if (r == 1) {
2995
0
      return r;
2996
0
    }
2997
0
  } else {
2998
0
    r = evdns_request_transmit_to(req, req->ns);
2999
0
  }
3000
0
  switch (r) {
3001
0
  case 1:
3002
    /* temp failure */
3003
0
    req->ns->choked = 1;
3004
0
    nameserver_write_waiting(req->ns, 1);
3005
0
    return 1;
3006
0
  case 2:
3007
    /* failed to transmit the request entirely. we can fallthrough since
3008
     * we'll set a timeout, which will time out, and make us retransmit the
3009
     * request anyway. */
3010
0
    retcode = 1;
3011
0
    EVUTIL_FALLTHROUGH;
3012
0
  default:
3013
    /* all ok */
3014
0
    log(EVDNS_LOG_DEBUG,
3015
0
        "Setting timeout for request %p, sent to nameserver %p", (void *)req, (void *)req->ns);
3016
0
    if (evtimer_add(&req->timeout_event, &req->base->global_timeout) < 0) {
3017
0
      log(EVDNS_LOG_WARN,
3018
0
          "Error from libevent when adding timer for request %p",
3019
0
          (void *)req);
3020
      /* ???? Do more? */
3021
0
    }
3022
0
    req->tx_count++;
3023
0
    req->transmit_me = 0;
3024
0
    return retcode;
3025
0
  }
3026
0
}
3027
3028
static void
3029
0
nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
3030
0
  struct nameserver *const ns = (struct nameserver *) arg;
3031
0
  (void) type;
3032
0
  (void) count;
3033
0
  (void) ttl;
3034
0
  (void) addresses;
3035
3036
0
  if (result == DNS_ERR_CANCEL) {
3037
    /* We canceled this request because the nameserver came up
3038
     * for some other reason.  Do not change our opinion about
3039
     * the nameserver. */
3040
0
    return;
3041
0
  }
3042
3043
0
  EVDNS_LOCK(ns->base);
3044
0
  ns->probe_request = NULL;
3045
0
  if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
3046
    /* this is a good reply */
3047
0
    nameserver_up(ns);
3048
0
  } else {
3049
0
    nameserver_probe_failed(ns);
3050
0
  }
3051
0
  EVDNS_UNLOCK(ns->base);
3052
0
}
3053
3054
static void
3055
0
nameserver_send_probe(struct nameserver *const ns) {
3056
0
  struct evdns_request *handle;
3057
0
  struct request *req;
3058
0
  char addrbuf[128];
3059
  /* here we need to send a probe to a given nameserver */
3060
  /* in the hope that it is up now. */
3061
3062
0
  ASSERT_LOCKED(ns->base);
3063
0
  log(EVDNS_LOG_DEBUG, "Sending probe to %s",
3064
0
      evutil_format_sockaddr_port_(
3065
0
        (struct sockaddr *)&ns->address,
3066
0
        addrbuf, sizeof(addrbuf)));
3067
0
  handle = mm_calloc(1, sizeof(*handle));
3068
0
  if (!handle) return;
3069
0
  handle->user_callback = nameserver_probe_callback;
3070
0
  handle->user_pointer = ns;
3071
0
  req = request_new(ns->base, handle, TYPE_A, "google.com", DNS_QUERY_NO_SEARCH);
3072
0
  if (!req) {
3073
0
    mm_free(handle);
3074
0
    return;
3075
0
  }
3076
0
  ns->probe_request = handle;
3077
  /* we force this into the inflight queue no matter what */
3078
0
  request_trans_id_set(req, transaction_id_pick(ns->base));
3079
0
  req->ns = ns;
3080
0
  request_submit(req);
3081
0
}
3082
3083
/* returns: */
3084
/*   0 didn't try to transmit anything */
3085
/*   1 tried to transmit something */
3086
static int
3087
0
evdns_transmit(struct evdns_base *base) {
3088
0
  char did_try_to_transmit = 0;
3089
0
  int i;
3090
3091
0
  ASSERT_LOCKED(base);
3092
0
  for (i = 0; i < base->n_req_heads; ++i) {
3093
0
    if (base->req_heads[i]) {
3094
0
      struct request *const started_at = base->req_heads[i], *req = started_at;
3095
      /* first transmit all the requests which are currently waiting */
3096
0
      do {
3097
0
        if (req->transmit_me) {
3098
0
          did_try_to_transmit = 1;
3099
0
          evdns_request_transmit(req);
3100
0
        }
3101
3102
0
        req = req->next;
3103
0
      } while (req != started_at);
3104
0
    }
3105
0
  }
3106
3107
0
  return did_try_to_transmit;
3108
0
}
3109
3110
/* exported function */
3111
int
3112
evdns_base_count_nameservers(struct evdns_base *base)
3113
0
{
3114
0
  const struct nameserver *server;
3115
0
  int n = 0;
3116
3117
0
  EVDNS_LOCK(base);
3118
0
  server = base->server_head;
3119
0
  if (!server)
3120
0
    goto done;
3121
0
  do {
3122
0
    ++n;
3123
0
    server = server->next;
3124
0
  } while (server != base->server_head);
3125
0
done:
3126
0
  EVDNS_UNLOCK(base);
3127
0
  return n;
3128
0
}
3129
3130
int
3131
evdns_count_nameservers(void)
3132
0
{
3133
0
  return evdns_base_count_nameservers(current_base);
3134
0
}
3135
3136
/* exported function */
3137
int
3138
evdns_base_clear_nameservers_and_suspend(struct evdns_base *base)
3139
0
{
3140
0
  struct nameserver *server, *started_at;
3141
0
  int i;
3142
3143
0
  EVDNS_LOCK(base);
3144
0
  server = base->server_head;
3145
0
  started_at = base->server_head;
3146
0
  if (!server) {
3147
0
    EVDNS_UNLOCK(base);
3148
0
    return 0;
3149
0
  }
3150
0
  while (1) {
3151
0
    struct nameserver *next = server->next;
3152
0
    disconnect_and_free_connection(server->connection);
3153
0
    server->connection = NULL;
3154
0
    (void) event_del(&server->event);
3155
0
    if (evtimer_initialized(&server->timeout_event))
3156
0
      (void) evtimer_del(&server->timeout_event);
3157
0
    if (server->probe_request) {
3158
0
      evdns_cancel_request(server->base, server->probe_request);
3159
0
      server->probe_request = NULL;
3160
0
    }
3161
0
    if (server->socket >= 0)
3162
0
      evutil_closesocket(server->socket);
3163
0
    mm_free(server);
3164
0
    if (next == started_at)
3165
0
      break;
3166
0
    server = next;
3167
0
  }
3168
0
  base->server_head = NULL;
3169
0
  base->global_good_nameservers = 0;
3170
3171
0
  for (i = 0; i < base->n_req_heads; ++i) {
3172
0
    struct request *req, *req_started_at;
3173
0
    req = req_started_at = base->req_heads[i];
3174
0
    while (req) {
3175
0
      struct request *next = req->next;
3176
0
      req->tx_count = req->reissue_count = 0;
3177
0
      req->ns = NULL;
3178
      /* ???? What to do about searches? */
3179
0
      (void) evtimer_del(&req->timeout_event);
3180
0
      req->trans_id = 0;
3181
0
      req->transmit_me = 0;
3182
3183
0
      base->global_requests_waiting++;
3184
0
      evdns_request_insert(req, &base->req_waiting_head);
3185
      /* We want to insert these suspended elements at the front of
3186
       * the waiting queue, since they were pending before any of
3187
       * the waiting entries were added.  This is a circular list,
3188
       * so we can just shift the start back by one.*/
3189
0
      base->req_waiting_head = base->req_waiting_head->prev;
3190
3191
0
      if (next == req_started_at)
3192
0
        break;
3193
0
      req = next;
3194
0
    }
3195
0
    base->req_heads[i] = NULL;
3196
0
  }
3197
3198
0
  base->global_requests_inflight = 0;
3199
3200
0
  EVDNS_UNLOCK(base);
3201
0
  return 0;
3202
0
}
3203
3204
int
3205
evdns_clear_nameservers_and_suspend(void)
3206
0
{
3207
0
  return evdns_base_clear_nameservers_and_suspend(current_base);
3208
0
}
3209
3210
3211
/* exported function */
3212
int
3213
evdns_base_resume(struct evdns_base *base)
3214
0
{
3215
0
  EVDNS_LOCK(base);
3216
0
  evdns_requests_pump_waiting_queue(base);
3217
0
  EVDNS_UNLOCK(base);
3218
3219
0
  return 0;
3220
0
}
3221
3222
int
3223
evdns_resume(void)
3224
0
{
3225
0
  return evdns_base_resume(current_base);
3226
0
}
3227
3228
static int
3229
0
evdns_nameserver_add_impl_(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
3230
  /* first check to see if we already have this nameserver */
3231
3232
0
  const struct nameserver *server = base->server_head, *const started_at = base->server_head;
3233
0
  struct nameserver *ns;
3234
0
  int err = 0;
3235
0
  char addrbuf[128];
3236
3237
0
  ASSERT_LOCKED(base);
3238
0
  if (server) {
3239
0
    do {
3240
0
      if (!evutil_sockaddr_cmp((struct sockaddr*)&server->address, address, 1)) return 3;
3241
0
      server = server->next;
3242
0
    } while (server != started_at);
3243
0
  }
3244
0
  if (addrlen > (int)sizeof(ns->address)) {
3245
0
    log(EVDNS_LOG_DEBUG, "Addrlen %d too long.", (int)addrlen);
3246
0
    return 2;
3247
0
  }
3248
3249
0
  ns = (struct nameserver *) mm_malloc(sizeof(struct nameserver));
3250
0
  if (!ns) return -1;
3251
3252
0
  memset(ns, 0, sizeof(struct nameserver));
3253
0
  ns->base = base;
3254
3255
0
  evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns);
3256
3257
0
  ns->socket = evutil_socket_(address->sa_family,
3258
0
      SOCK_DGRAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
3259
0
  if (ns->socket < 0) { err = 1; goto out1; }
3260
3261
0
  if (base->global_outgoing_addrlen &&
3262
0
      !evutil_sockaddr_is_loopback_(address)) {
3263
0
    if (bind(ns->socket,
3264
0
      (struct sockaddr*)&base->global_outgoing_address,
3265
0
      base->global_outgoing_addrlen) < 0) {
3266
0
      log(EVDNS_LOG_WARN,"Couldn't bind to outgoing address");
3267
0
      err = 2;
3268
0
      goto out2;
3269
0
    }
3270
0
  }
3271
3272
0
  if (base->so_rcvbuf) {
3273
0
    if (setsockopt(ns->socket, SOL_SOCKET, SO_RCVBUF,
3274
0
        (void *)&base->so_rcvbuf, sizeof(base->so_rcvbuf))) {
3275
0
      log(EVDNS_LOG_WARN, "Couldn't set SO_RCVBUF to %i", base->so_rcvbuf);
3276
0
      err = -SO_RCVBUF;
3277
0
      goto out2;
3278
0
    }
3279
0
  }
3280
0
  if (base->so_sndbuf) {
3281
0
    if (setsockopt(ns->socket, SOL_SOCKET, SO_SNDBUF,
3282
0
        (void *)&base->so_sndbuf, sizeof(base->so_sndbuf))) {
3283
0
      log(EVDNS_LOG_WARN, "Couldn't set SO_SNDBUF to %i", base->so_sndbuf);
3284
0
      err = -SO_SNDBUF;
3285
0
      goto out2;
3286
0
    }
3287
0
  }
3288
3289
0
  memcpy(&ns->address, address, addrlen);
3290
0
  ns->addrlen = addrlen;
3291
0
  ns->state = 1;
3292
0
  ns->connection = NULL;
3293
0
  event_assign(&ns->event, ns->base->event_base, ns->socket,
3294
0
         EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
3295
0
  if (!base->disable_when_inactive && event_add(&ns->event, NULL) < 0) {
3296
0
    err = 2;
3297
0
    goto out2;
3298
0
  }
3299
3300
0
  log(EVDNS_LOG_DEBUG, "Added nameserver %s as %p",
3301
0
      evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), (void *)ns);
3302
3303
  /* insert this nameserver into the list of them */
3304
0
  if (!base->server_head) {
3305
0
    ns->next = ns->prev = ns;
3306
0
    base->server_head = ns;
3307
0
  } else {
3308
0
    ns->next = base->server_head->next;
3309
0
    ns->prev = base->server_head;
3310
0
    base->server_head->next = ns;
3311
0
    ns->next->prev = ns;
3312
0
  }
3313
3314
0
  base->global_good_nameservers++;
3315
3316
0
  return 0;
3317
3318
0
out2:
3319
0
  evutil_closesocket(ns->socket);
3320
0
out1:
3321
0
  event_debug_unassign(&ns->event);
3322
0
  mm_free(ns);
3323
0
  log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d",
3324
0
      evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), err);
3325
0
  return err;
3326
0
}
3327
3328
/* exported function */
3329
int
3330
evdns_base_nameserver_add(struct evdns_base *base, unsigned long int address)
3331
0
{
3332
0
  struct sockaddr_in sin;
3333
0
  int res;
3334
0
  memset(&sin, 0, sizeof(sin));
3335
0
  sin.sin_addr.s_addr = address;
3336
0
  sin.sin_port = htons(53);
3337
0
  sin.sin_family = AF_INET;
3338
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
3339
  sin.sin_len = sizeof(sin);
3340
#endif
3341
0
  EVDNS_LOCK(base);
3342
0
  res = evdns_nameserver_add_impl_(base, (struct sockaddr*)&sin, sizeof(sin));
3343
0
  EVDNS_UNLOCK(base);
3344
0
  return res;
3345
0
}
3346
3347
int
3348
0
evdns_nameserver_add(unsigned long int address) {
3349
0
  if (!current_base)
3350
0
    current_base = evdns_base_new(NULL, 0);
3351
0
  return evdns_base_nameserver_add(current_base, address);
3352
0
}
3353
3354
static void
3355
sockaddr_setport(struct sockaddr *sa, ev_uint16_t port)
3356
0
{
3357
0
  if (sa->sa_family == AF_INET) {
3358
0
    ((struct sockaddr_in *)sa)->sin_port = htons(port);
3359
0
  } else if (sa->sa_family == AF_INET6) {
3360
0
    ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
3361
0
  }
3362
0
}
3363
3364
static ev_uint16_t
3365
sockaddr_getport(struct sockaddr *sa)
3366
0
{
3367
0
  if (sa->sa_family == AF_INET) {
3368
0
    return ntohs(((struct sockaddr_in *)sa)->sin_port);
3369
0
  } else if (sa->sa_family == AF_INET6) {
3370
0
    return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
3371
0
  } else {
3372
0
    return 0;
3373
0
  }
3374
0
}
3375
3376
/* exported function */
3377
int
3378
0
evdns_base_nameserver_ip_add(struct evdns_base *base, const char *ip_as_string) {
3379
0
  struct sockaddr_storage ss;
3380
0
  struct sockaddr *sa;
3381
0
  int len = sizeof(ss);
3382
0
  int res;
3383
0
  if (evutil_parse_sockaddr_port(ip_as_string, (struct sockaddr *)&ss,
3384
0
    &len)) {
3385
0
    log(EVDNS_LOG_WARN, "Unable to parse nameserver address %s",
3386
0
      ip_as_string);
3387
0
    return 4;
3388
0
  }
3389
0
  sa = (struct sockaddr *) &ss;
3390
0
  if (sockaddr_getport(sa) == 0)
3391
0
    sockaddr_setport(sa, 53);
3392
3393
0
  EVDNS_LOCK(base);
3394
0
  res = evdns_nameserver_add_impl_(base, sa, len);
3395
0
  EVDNS_UNLOCK(base);
3396
0
  return res;
3397
0
}
3398
3399
int
3400
0
evdns_nameserver_ip_add(const char *ip_as_string) {
3401
0
  if (!current_base)
3402
0
    current_base = evdns_base_new(NULL, 0);
3403
0
  return evdns_base_nameserver_ip_add(current_base, ip_as_string);
3404
0
}
3405
3406
int
3407
evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
3408
    const struct sockaddr *sa, ev_socklen_t len, unsigned flags)
3409
0
{
3410
0
  int res;
3411
0
  EVUTIL_ASSERT(base);
3412
0
  EVDNS_LOCK(base);
3413
0
  res = evdns_nameserver_add_impl_(base, sa, len);
3414
0
  EVDNS_UNLOCK(base);
3415
0
  return res;
3416
0
}
3417
3418
int
3419
evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
3420
    struct sockaddr *sa, ev_socklen_t len)
3421
0
{
3422
0
  int result = -1;
3423
0
  int i;
3424
0
  struct nameserver *server;
3425
0
  EVDNS_LOCK(base);
3426
0
  server = base->server_head;
3427
0
  for (i = 0; i < idx && server; ++i, server = server->next) {
3428
0
    if (server->next == base->server_head)
3429
0
      goto done;
3430
0
  }
3431
0
  if (! server)
3432
0
    goto done;
3433
3434
0
  if (server->addrlen > len) {
3435
0
    result = (int) server->addrlen;
3436
0
    goto done;
3437
0
  }
3438
3439
0
  memcpy(sa, &server->address, server->addrlen);
3440
0
  result = (int) server->addrlen;
3441
0
done:
3442
0
  EVDNS_UNLOCK(base);
3443
0
  return result;
3444
0
}
3445
3446
int
3447
evdns_base_get_nameserver_fd(struct evdns_base *base, int idx)
3448
0
{
3449
0
  int result = -1;
3450
0
  int i;
3451
0
  struct nameserver *server;
3452
0
  EVDNS_LOCK(base);
3453
0
  server = base->server_head;
3454
0
  for (i = 0; i < idx && server; ++i, server = server->next) {
3455
0
    if (server->next == base->server_head)
3456
0
      goto done;
3457
0
  }
3458
0
  if (! server)
3459
0
    goto done;
3460
0
  result = server->socket;
3461
0
done:
3462
0
  EVDNS_UNLOCK(base);
3463
0
  return result;
3464
0
}
3465
3466
3467
/* remove from the queue */
3468
static void
3469
evdns_request_remove(struct request *req, struct request **head)
3470
0
{
3471
0
  ASSERT_LOCKED(req->base);
3472
0
  ASSERT_VALID_REQUEST(req);
3473
3474
#if 0
3475
  {
3476
    struct request *ptr;
3477
    int found = 0;
3478
    EVUTIL_ASSERT(*head != NULL);
3479
3480
    ptr = *head;
3481
    do {
3482
      if (ptr == req) {
3483
        found = 1;
3484
        break;
3485
      }
3486
      ptr = ptr->next;
3487
    } while (ptr != *head);
3488
    EVUTIL_ASSERT(found);
3489
3490
    EVUTIL_ASSERT(req->next);
3491
  }
3492
#endif
3493
3494
0
  if (req->next == req) {
3495
    /* only item in the list */
3496
0
    *head = NULL;
3497
0
  } else {
3498
0
    req->next->prev = req->prev;
3499
0
    req->prev->next = req->next;
3500
0
    if (*head == req) *head = req->next;
3501
0
  }
3502
0
  req->next = req->prev = NULL;
3503
0
}
3504
3505
/* insert into the tail of the queue */
3506
static void
3507
0
evdns_request_insert(struct request *req, struct request **head) {
3508
0
  ASSERT_LOCKED(req->base);
3509
0
  ASSERT_VALID_REQUEST(req);
3510
0
  if (!*head) {
3511
0
    *head = req;
3512
0
    req->next = req->prev = req;
3513
0
    return;
3514
0
  }
3515
3516
0
  req->prev = (*head)->prev;
3517
0
  req->prev->next = req;
3518
0
  req->next = *head;
3519
0
  (*head)->prev = req;
3520
0
}
3521
3522
static int
3523
0
string_num_dots(const char *s) {
3524
0
  int count = 0;
3525
0
  while ((s = strchr(s, '.'))) {
3526
0
    s++;
3527
0
    count++;
3528
0
  }
3529
0
  return count;
3530
0
}
3531
3532
static struct request *
3533
request_new(struct evdns_base *base, struct evdns_request *handle, int type,
3534
0
      const char *name, int flags) {
3535
3536
0
  const char issuing_now =
3537
0
      (base->global_requests_inflight < base->global_max_requests_inflight) ? 1 : 0;
3538
3539
0
  const size_t name_len = strlen(name);
3540
0
  const size_t request_max_len = evdns_request_len(base, name_len);
3541
0
  const u16 trans_id = issuing_now ? transaction_id_pick(base) : 0xffff;
3542
  /* the request data is alloced in a single block with the header */
3543
0
  struct request *const req =
3544
0
      mm_malloc(sizeof(struct request) + request_max_len);
3545
0
  int rlen;
3546
0
  char namebuf[256];
3547
0
  (void) flags;
3548
3549
0
  ASSERT_LOCKED(base);
3550
3551
0
  if (!req) return NULL;
3552
3553
0
  if (name_len >= sizeof(namebuf)) {
3554
0
    mm_free(req);
3555
0
    return NULL;
3556
0
  }
3557
3558
0
  memset(req, 0, sizeof(struct request));
3559
0
  req->request_size = (u16)(sizeof(struct request) + request_max_len);
3560
0
  req->base = base;
3561
3562
0
  evtimer_assign(&req->timeout_event, req->base->event_base, evdns_request_timeout_callback, req);
3563
3564
0
  if (base->global_randomize_case) {
3565
0
    unsigned i;
3566
0
    char randbits[(sizeof(namebuf)+7)/8];
3567
0
    strlcpy(namebuf, name, sizeof(namebuf));
3568
0
    evutil_secure_rng_get_bytes(randbits, (name_len+7)/8);
3569
0
    for (i = 0; i < name_len; ++i) {
3570
0
      if (EVUTIL_ISALPHA_(namebuf[i])) {
3571
0
        if ((randbits[i >> 3] & (1<<(i & 7))))
3572
0
          namebuf[i] |= 0x20;
3573
0
        else
3574
0
          namebuf[i] &= ~0x20;
3575
0
      }
3576
0
    }
3577
0
    name = namebuf;
3578
0
  }
3579
3580
  /* request data lives just after the header */
3581
0
  req->request = ((u8 *) req) + sizeof(struct request);
3582
  /* denotes that the request data shouldn't be free()ed */
3583
0
  req->request_appended = 1;
3584
0
  rlen = evdns_request_data_build(base, name, name_len, trans_id,
3585
0
      type, CLASS_INET, req->request, request_max_len);
3586
0
  if (rlen < 0)
3587
0
    goto err1;
3588
3589
0
  req->request_len = rlen;
3590
0
  req->trans_id = trans_id;
3591
0
  req->tx_count = 0;
3592
0
  req->request_type = type;
3593
0
  req->ns = issuing_now ? nameserver_pick(base) : NULL;
3594
0
  req->next = req->prev = NULL;
3595
0
  req->handle = handle;
3596
0
  if (handle) {
3597
0
    handle->current_req = req;
3598
0
    handle->base = base;
3599
0
  }
3600
3601
0
  if (flags & DNS_CNAME_CALLBACK)
3602
0
    req->need_cname = 1;
3603
3604
0
  return req;
3605
0
err1:
3606
0
  mm_free(req);
3607
0
  return NULL;
3608
0
}
3609
3610
static struct request *
3611
request_clone(struct evdns_base *base, struct request* current)
3612
0
{
3613
0
  const char issuing_now =
3614
0
      (base->global_requests_inflight < base->global_max_requests_inflight) ? 1 : 0;
3615
0
  const u16 trans_id = issuing_now ? transaction_id_pick(base) : 0xffff;
3616
  /* the request data is alloced in a single block with the header */
3617
0
  struct request *const req = mm_malloc(current->request_size);
3618
0
  EVUTIL_ASSERT(current && base);
3619
0
  ASSERT_LOCKED(base);
3620
3621
0
  if (!req)
3622
0
    return NULL;
3623
0
  memcpy(req, current, current->request_size);
3624
3625
0
  evtimer_assign(&req->timeout_event, req->base->event_base, evdns_request_timeout_callback, req);
3626
3627
  /* request data lives just after the header */
3628
0
  req->request = ((u8 *) req) + sizeof(struct request);
3629
  /* We need to replace transact id */
3630
0
  request_trans_id_set(req, trans_id);
3631
3632
0
  req->tx_count = 0;
3633
0
  req->ns = issuing_now ? nameserver_pick(base) : NULL;
3634
0
  req->next = req->prev = NULL;
3635
0
  req->handle = NULL;
3636
0
  log(EVDNS_LOG_DEBUG, "Clone new request TID %d from TID %d", req->trans_id, current->trans_id);
3637
3638
0
  return req;
3639
0
}
3640
3641
static void
3642
0
request_submit(struct request *const req) {
3643
0
  struct evdns_base *base = req->base;
3644
0
  ASSERT_LOCKED(base);
3645
0
  ASSERT_VALID_REQUEST(req);
3646
0
  if (req->ns) {
3647
    /* if it has a nameserver assigned then this is going */
3648
    /* straight into the inflight queue */
3649
0
    evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
3650
3651
0
    base->global_requests_inflight++;
3652
0
    req->ns->requests_inflight++;
3653
3654
0
    evdns_request_transmit(req);
3655
0
  } else {
3656
0
    evdns_request_insert(req, &base->req_waiting_head);
3657
0
    base->global_requests_waiting++;
3658
0
  }
3659
0
}
3660
3661
/* exported function */
3662
void
3663
evdns_cancel_request(struct evdns_base *base, struct evdns_request *handle)
3664
0
{
3665
0
  struct request *req;
3666
3667
0
  if (!handle->current_req)
3668
0
    return;
3669
3670
0
  if (!base) {
3671
    /* This redundancy is silly; can we fix it? (Not for 2.0) XXXX */
3672
0
    base = handle->base;
3673
0
    if (!base)
3674
0
      base = handle->current_req->base;
3675
0
  }
3676
3677
0
  EVDNS_LOCK(base);
3678
0
  if (handle->pending_cb) {
3679
0
    EVDNS_UNLOCK(base);
3680
0
    return;
3681
0
  }
3682
3683
0
  req = handle->current_req;
3684
0
  ASSERT_VALID_REQUEST(req);
3685
3686
0
  reply_schedule_callback(req, 0, DNS_ERR_CANCEL, NULL);
3687
0
  if (req->ns) {
3688
    /* remove from inflight queue */
3689
0
    request_finished(req, &REQ_HEAD(base, req->trans_id), 1);
3690
0
  } else {
3691
    /* remove from global_waiting head */
3692
0
    request_finished(req, &base->req_waiting_head, 1);
3693
0
  }
3694
0
  EVDNS_UNLOCK(base);
3695
0
}
3696
3697
/* exported function */
3698
struct evdns_request *
3699
evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags,
3700
0
    evdns_callback_type callback, void *ptr) {
3701
0
  struct evdns_request *handle;
3702
0
  struct request *req;
3703
0
  log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
3704
0
  handle = mm_calloc(1, sizeof(*handle));
3705
0
  if (handle == NULL)
3706
0
    return NULL;
3707
0
  handle->user_callback = callback;
3708
0
  handle->user_pointer = ptr;
3709
0
  EVDNS_LOCK(base);
3710
0
  handle->tcp_flags = base->global_tcp_flags;
3711
0
  handle->tcp_flags |= flags & (DNS_QUERY_USEVC | DNS_QUERY_IGNTC);
3712
0
  if (flags & DNS_QUERY_NO_SEARCH) {
3713
0
    req =
3714
0
      request_new(base, handle, TYPE_A, name, flags);
3715
0
    if (req)
3716
0
      request_submit(req);
3717
0
  } else {
3718
0
    search_request_new(base, handle, TYPE_A, name, flags);
3719
0
  }
3720
0
  if (handle->current_req == NULL) {
3721
0
    mm_free(handle);
3722
0
    handle = NULL;
3723
0
  }
3724
0
  EVDNS_UNLOCK(base);
3725
0
  return handle;
3726
0
}
3727
3728
int evdns_resolve_ipv4(const char *name, int flags,
3729
             evdns_callback_type callback, void *ptr)
3730
0
{
3731
0
  return evdns_base_resolve_ipv4(current_base, name, flags, callback, ptr)
3732
0
    ? 0 : -1;
3733
0
}
3734
3735
3736
/* exported function */
3737
struct evdns_request *
3738
evdns_base_resolve_ipv6(struct evdns_base *base,
3739
    const char *name, int flags,
3740
    evdns_callback_type callback, void *ptr)
3741
0
{
3742
0
  struct evdns_request *handle;
3743
0
  struct request *req;
3744
0
  log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
3745
0
  handle = mm_calloc(1, sizeof(*handle));
3746
0
  if (handle == NULL)
3747
0
    return NULL;
3748
0
  handle->user_callback = callback;
3749
0
  handle->user_pointer = ptr;
3750
0
  EVDNS_LOCK(base);
3751
0
  handle->tcp_flags = base->global_tcp_flags;
3752
0
  handle->tcp_flags |= flags & (DNS_QUERY_USEVC | DNS_QUERY_IGNTC);
3753
0
  if (flags & DNS_QUERY_NO_SEARCH) {
3754
0
    req = request_new(base, handle, TYPE_AAAA, name, flags);
3755
0
    if (req)
3756
0
      request_submit(req);
3757
0
  } else {
3758
0
    search_request_new(base, handle, TYPE_AAAA, name, flags);
3759
0
  }
3760
0
  if (handle->current_req == NULL) {
3761
0
    mm_free(handle);
3762
0
    handle = NULL;
3763
0
  }
3764
0
  EVDNS_UNLOCK(base);
3765
0
  return handle;
3766
0
}
3767
3768
int evdns_resolve_ipv6(const char *name, int flags,
3769
0
    evdns_callback_type callback, void *ptr) {
3770
0
  return evdns_base_resolve_ipv6(current_base, name, flags, callback, ptr)
3771
0
    ? 0 : -1;
3772
0
}
3773
3774
struct evdns_request *
3775
0
evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3776
0
  char buf[32];
3777
0
  struct evdns_request *handle;
3778
0
  struct request *req;
3779
0
  u32 a;
3780
0
  EVUTIL_ASSERT(in);
3781
0
  a = ntohl(in->s_addr);
3782
0
  evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
3783
0
      (int)(u8)((a  )&0xff),
3784
0
      (int)(u8)((a>>8 )&0xff),
3785
0
      (int)(u8)((a>>16)&0xff),
3786
0
      (int)(u8)((a>>24)&0xff));
3787
0
  handle = mm_calloc(1, sizeof(*handle));
3788
0
  if (handle == NULL)
3789
0
    return NULL;
3790
0
  handle->user_callback = callback;
3791
0
  handle->user_pointer = ptr;
3792
0
  log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
3793
0
  EVDNS_LOCK(base);
3794
0
  handle->tcp_flags = base->global_tcp_flags;
3795
0
  handle->tcp_flags |= flags & (DNS_QUERY_USEVC | DNS_QUERY_IGNTC);
3796
0
  req = request_new(base, handle, TYPE_PTR, buf, flags);
3797
0
  if (req)
3798
0
    request_submit(req);
3799
0
  if (handle->current_req == NULL) {
3800
0
    mm_free(handle);
3801
0
    handle = NULL;
3802
0
  }
3803
0
  EVDNS_UNLOCK(base);
3804
0
  return (handle);
3805
0
}
3806
3807
0
int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3808
0
  return evdns_base_resolve_reverse(current_base, in, flags, callback, ptr)
3809
0
    ? 0 : -1;
3810
0
}
3811
3812
struct evdns_request *
3813
0
evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3814
  /* 32 nybbles, 32 periods, "ip6.arpa", NUL. */
3815
0
  char buf[73];
3816
0
  char *cp;
3817
0
  struct evdns_request *handle;
3818
0
  struct request *req;
3819
0
  int i;
3820
0
  EVUTIL_ASSERT(in);
3821
0
  cp = buf;
3822
0
  for (i=15; i >= 0; --i) {
3823
0
    u8 byte = in->s6_addr[i];
3824
0
    *cp++ = "0123456789abcdef"[byte & 0x0f];
3825
0
    *cp++ = '.';
3826
0
    *cp++ = "0123456789abcdef"[byte >> 4];
3827
0
    *cp++ = '.';
3828
0
  }
3829
0
  EVUTIL_ASSERT(cp + strlen("ip6.arpa") < buf+sizeof(buf));
3830
0
  memcpy(cp, "ip6.arpa", strlen("ip6.arpa")+1);
3831
0
  handle = mm_calloc(1, sizeof(*handle));
3832
0
  if (handle == NULL)
3833
0
    return NULL;
3834
0
  handle->user_callback = callback;
3835
0
  handle->user_pointer = ptr;
3836
0
  log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
3837
0
  EVDNS_LOCK(base);
3838
0
  handle->tcp_flags = base->global_tcp_flags;
3839
0
  handle->tcp_flags |= flags & (DNS_QUERY_USEVC | DNS_QUERY_IGNTC);
3840
0
  req = request_new(base, handle, TYPE_PTR, buf, flags);
3841
0
  if (req)
3842
0
    request_submit(req);
3843
0
  if (handle->current_req == NULL) {
3844
0
    mm_free(handle);
3845
0
    handle = NULL;
3846
0
  }
3847
0
  EVDNS_UNLOCK(base);
3848
0
  return (handle);
3849
0
}
3850
3851
0
int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
3852
0
  return evdns_base_resolve_reverse_ipv6(current_base, in, flags, callback, ptr)
3853
0
    ? 0 : -1;
3854
0
}
3855
3856
/* ================================================================= */
3857
/* Search support */
3858
/* */
3859
/* the libc resolver has support for searching a number of domains */
3860
/* to find a name. If nothing else then it takes the single domain */
3861
/* from the gethostname() call. */
3862
/* */
3863
/* It can also be configured via the domain and search options in a */
3864
/* resolv.conf. */
3865
/* */
3866
/* The ndots option controls how many dots it takes for the resolver */
3867
/* to decide that a name is non-local and so try a raw lookup first. */
3868
3869
struct search_domain {
3870
  int len;
3871
  struct search_domain *next;
3872
  /* the text string is appended to this structure */
3873
};
3874
3875
struct search_state {
3876
  int refcount;
3877
  int ndots;
3878
  int num_domains;
3879
  struct search_domain *head;
3880
};
3881
3882
static void
3883
0
search_state_decref(struct search_state *const state) {
3884
0
  if (!state) return;
3885
0
  state->refcount--;
3886
0
  if (!state->refcount) {
3887
0
    struct search_domain *next, *dom;
3888
0
    for (dom = state->head; dom; dom = next) {
3889
0
      next = dom->next;
3890
0
      mm_free(dom);
3891
0
    }
3892
0
    mm_free(state);
3893
0
  }
3894
0
}
3895
3896
static struct search_state *
3897
0
search_state_new(void) {
3898
0
  struct search_state *state = (struct search_state *) mm_malloc(sizeof(struct search_state));
3899
0
  if (!state) return NULL;
3900
0
  memset(state, 0, sizeof(struct search_state));
3901
0
  state->refcount = 1;
3902
0
  state->ndots = 1;
3903
3904
0
  return state;
3905
0
}
3906
3907
static void
3908
0
search_postfix_clear(struct evdns_base *base) {
3909
0
  search_state_decref(base->global_search_state);
3910
3911
0
  base->global_search_state = search_state_new();
3912
0
}
3913
3914
/* exported function */
3915
void
3916
evdns_base_search_clear(struct evdns_base *base)
3917
0
{
3918
0
  EVDNS_LOCK(base);
3919
0
  search_postfix_clear(base);
3920
0
  EVDNS_UNLOCK(base);
3921
0
}
3922
3923
void
3924
0
evdns_search_clear(void) {
3925
0
  evdns_base_search_clear(current_base);
3926
0
}
3927
3928
static void
3929
0
search_postfix_add(struct evdns_base *base, const char *domain) {
3930
0
  size_t domain_len;
3931
0
  struct search_domain *sdomain;
3932
0
  while (domain[0] == '.') domain++;
3933
0
  domain_len = strlen(domain);
3934
3935
0
  ASSERT_LOCKED(base);
3936
0
  if (!base->global_search_state) base->global_search_state = search_state_new();
3937
0
  if (!base->global_search_state) return;
3938
0
  base->global_search_state->num_domains++;
3939
3940
0
  sdomain = (struct search_domain *) mm_malloc(sizeof(struct search_domain) + domain_len);
3941
0
  if (!sdomain) return;
3942
0
  memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
3943
0
  sdomain->next = base->global_search_state->head;
3944
0
  sdomain->len = (int) domain_len;
3945
3946
0
  base->global_search_state->head = sdomain;
3947
0
}
3948
3949
/* reverse the order of members in the postfix list. This is needed because, */
3950
/* when parsing resolv.conf we push elements in the wrong order */
3951
static void
3952
0
search_reverse(struct evdns_base *base) {
3953
0
  struct search_domain *cur, *prev = NULL, *next;
3954
0
  ASSERT_LOCKED(base);
3955
0
  cur = base->global_search_state->head;
3956
0
  while (cur) {
3957
0
    next = cur->next;
3958
0
    cur->next = prev;
3959
0
    prev = cur;
3960
0
    cur = next;
3961
0
  }
3962
3963
0
  base->global_search_state->head = prev;
3964
0
}
3965
3966
/* exported function */
3967
void
3968
0
evdns_base_search_add(struct evdns_base *base, const char *domain) {
3969
0
  EVDNS_LOCK(base);
3970
0
  search_postfix_add(base, domain);
3971
0
  EVDNS_UNLOCK(base);
3972
0
}
3973
void
3974
0
evdns_search_add(const char *domain) {
3975
0
  evdns_base_search_add(current_base, domain);
3976
0
}
3977
3978
/* exported function */
3979
void
3980
0
evdns_base_search_ndots_set(struct evdns_base *base, const int ndots) {
3981
0
  EVDNS_LOCK(base);
3982
0
  if (!base->global_search_state) base->global_search_state = search_state_new();
3983
0
  if (base->global_search_state)
3984
0
    base->global_search_state->ndots = ndots;
3985
0
  EVDNS_UNLOCK(base);
3986
0
}
3987
void
3988
0
evdns_search_ndots_set(const int ndots) {
3989
0
  evdns_base_search_ndots_set(current_base, ndots);
3990
0
}
3991
3992
static void
3993
0
search_set_from_hostname(struct evdns_base *base) {
3994
0
  char hostname[EVDNS_NAME_MAX + 1], *domainname;
3995
3996
0
  ASSERT_LOCKED(base);
3997
0
  search_postfix_clear(base);
3998
0
  if (gethostname(hostname, sizeof(hostname))) return;
3999
0
  domainname = strchr(hostname, '.');
4000
0
  if (!domainname) return;
4001
0
  search_postfix_add(base, domainname);
4002
0
}
4003
4004
/* warning: returns malloced string */
4005
static char *
4006
0
search_make_new(const struct search_state *const state, int n, const char *const base_name) {
4007
0
  const size_t base_len = strlen(base_name);
4008
0
  char need_to_append_dot;
4009
0
  struct search_domain *dom;
4010
4011
0
  if (!base_len) return NULL;
4012
0
  need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
4013
4014
0
  for (dom = state->head; dom; dom = dom->next) {
4015
0
    if (!n--) {
4016
      /* this is the postfix we want */
4017
      /* the actual postfix string is kept at the end of the structure */
4018
0
      const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
4019
0
      const int postfix_len = dom->len;
4020
0
      char *const newname = (char *) mm_malloc(base_len + need_to_append_dot + postfix_len + 1);
4021
0
      if (!newname) return NULL;
4022
0
      memcpy(newname, base_name, base_len);
4023
0
      if (need_to_append_dot) newname[base_len] = '.';
4024
0
      memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
4025
0
      newname[base_len + need_to_append_dot + postfix_len] = 0;
4026
0
      return newname;
4027
0
    }
4028
0
  }
4029
4030
  /* we ran off the end of the list and still didn't find the requested string */
4031
0
  EVUTIL_ASSERT(0);
4032
0
  return NULL; /* unreachable; stops warnings in some compilers. */
4033
0
}
4034
4035
static struct request *
4036
search_request_new(struct evdns_base *base, struct evdns_request *handle,
4037
0
       int type, const char *const name, int flags) {
4038
0
  ASSERT_LOCKED(base);
4039
0
  EVUTIL_ASSERT(type == TYPE_A || type == TYPE_AAAA);
4040
0
  EVUTIL_ASSERT(handle->current_req == NULL);
4041
0
  if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
4042
0
       base->global_search_state &&
4043
0
     base->global_search_state->num_domains) {
4044
    /* we have some domains to search */
4045
0
    struct request *req;
4046
0
    if (string_num_dots(name) >= base->global_search_state->ndots) {
4047
0
      req = request_new(base, handle, type, name, flags);
4048
0
      if (!req) return NULL;
4049
0
      handle->search_index = -1;
4050
0
    } else {
4051
0
      char *const new_name = search_make_new(base->global_search_state, 0, name);
4052
0
      if (!new_name) return NULL;
4053
0
      req = request_new(base, handle, type, new_name, flags);
4054
0
      mm_free(new_name);
4055
0
      if (!req) return NULL;
4056
0
      handle->search_index = 0;
4057
0
    }
4058
0
    EVUTIL_ASSERT(handle->search_origname == NULL);
4059
0
    handle->search_origname = mm_strdup(name);
4060
0
    if (handle->search_origname == NULL) {
4061
      /* XXX Should we dealloc req? If yes, how? */
4062
0
      if (req)
4063
0
        mm_free(req);
4064
0
      return NULL;
4065
0
    }
4066
0
    handle->search_state = base->global_search_state;
4067
0
    handle->search_flags = flags;
4068
0
    base->global_search_state->refcount++;
4069
0
    request_submit(req);
4070
0
    return req;
4071
0
  } else {
4072
0
    struct request *const req = request_new(base, handle, type, name, flags);
4073
0
    if (!req) return NULL;
4074
0
    request_submit(req);
4075
0
    return req;
4076
0
  }
4077
0
}
4078
4079
/* this is called when a request has failed to find a name. We need to check */
4080
/* if it is part of a search and, if so, try the next name in the list */
4081
/* returns: */
4082
/*   0 another request has been submitted */
4083
/*   1 no more requests needed */
4084
static int
4085
0
search_try_next(struct evdns_request *const handle) {
4086
0
  struct request *req = handle->current_req;
4087
0
  struct evdns_base *base = req->base;
4088
0
  struct request *newreq;
4089
0
  ASSERT_LOCKED(base);
4090
0
  if (handle->search_state) {
4091
    /* it is part of a search */
4092
0
    char *new_name;
4093
0
    handle->search_index++;
4094
0
    if (handle->search_index >= handle->search_state->num_domains) {
4095
      /* no more postfixes to try, however we may need to try */
4096
      /* this name without a postfix */
4097
0
      if (string_num_dots(handle->search_origname) < handle->search_state->ndots) {
4098
        /* yep, we need to try it raw */
4099
0
        newreq = request_new(base, NULL, req->request_type, handle->search_origname, handle->search_flags);
4100
0
        log(EVDNS_LOG_DEBUG, "Search: trying raw query %s", handle->search_origname);
4101
0
        if (newreq) {
4102
0
          search_request_finished(handle);
4103
0
          goto submit_next;
4104
0
        }
4105
0
      }
4106
0
      return 1;
4107
0
    }
4108
4109
0
    new_name = search_make_new(handle->search_state, handle->search_index, handle->search_origname);
4110
0
    if (!new_name) return 1;
4111
0
    log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, handle->search_index);
4112
0
    newreq = request_new(base, NULL, req->request_type, new_name, handle->search_flags);
4113
0
    mm_free(new_name);
4114
0
    if (!newreq) return 1;
4115
0
    goto submit_next;
4116
0
  }
4117
0
  return 1;
4118
4119
0
submit_next:
4120
0
  request_finished(req, &REQ_HEAD(req->base, req->trans_id), 0);
4121
0
  handle->current_req = newreq;
4122
0
  newreq->handle = handle;
4123
0
  request_submit(newreq);
4124
0
  return 0;
4125
0
}
4126
4127
static void
4128
0
search_request_finished(struct evdns_request *const handle) {
4129
0
  ASSERT_LOCKED(handle->current_req->base);
4130
0
  if (handle->search_state) {
4131
0
    search_state_decref(handle->search_state);
4132
0
    handle->search_state = NULL;
4133
0
  }
4134
0
  if (handle->search_origname) {
4135
0
    mm_free(handle->search_origname);
4136
0
    handle->search_origname = NULL;
4137
0
  }
4138
0
}
4139
4140
/* ================================================================= */
4141
/* Parsing resolv.conf files */
4142
4143
static void
4144
0
evdns_resolv_set_defaults(struct evdns_base *base, int flags) {
4145
0
  int add_default = flags & DNS_OPTION_NAMESERVERS;
4146
0
  if (flags & DNS_OPTION_NAMESERVERS_NO_DEFAULT)
4147
0
    add_default = 0;
4148
4149
  /* if the file isn't found then we assume a local resolver */
4150
0
  ASSERT_LOCKED(base);
4151
0
  if (flags & DNS_OPTION_SEARCH)
4152
0
    search_set_from_hostname(base);
4153
0
  if (add_default)
4154
0
    evdns_base_nameserver_ip_add(base, "127.0.0.1");
4155
0
}
4156
4157
#ifndef EVENT__HAVE_STRTOK_R
4158
static char *
4159
strtok_r(char *s, const char *delim, char **state) {
4160
  char *cp, *start;
4161
  start = cp = s ? s : *state;
4162
  if (!cp)
4163
    return NULL;
4164
  while (*cp && !strchr(delim, *cp))
4165
    ++cp;
4166
  if (!*cp) {
4167
    if (cp == start)
4168
      return NULL;
4169
    *state = NULL;
4170
    return start;
4171
  } else {
4172
    *cp++ = '\0';
4173
    *state = cp;
4174
    return start;
4175
  }
4176
}
4177
#endif
4178
4179
/* helper version of atoi which returns -1 on error */
4180
static int
4181
strtoint(const char *const str)
4182
0
{
4183
0
  char *endptr;
4184
0
  const int r = strtol(str, &endptr, 10);
4185
0
  if (*endptr) return -1;
4186
0
  return r;
4187
0
}
4188
4189
/* Parse a number of seconds into a timeval; return -1 on error. */
4190
static int
4191
evdns_strtotimeval(const char *const str, struct timeval *out)
4192
0
{
4193
0
  double d;
4194
0
  char *endptr;
4195
0
  d = strtod(str, &endptr);
4196
0
  if (*endptr) return -1;
4197
0
  if (d < 0) return -1;
4198
0
  out->tv_sec = (int) d;
4199
0
  out->tv_usec = (int) ((d - (int) d)*1000000);
4200
0
  if (out->tv_sec == 0 && out->tv_usec < 1000) /* less than 1 msec */
4201
0
    return -1;
4202
0
  return 0;
4203
0
}
4204
4205
/* helper version of atoi that returns -1 on error and clips to bounds. */
4206
static int
4207
strtoint_clipped(const char *const str, int min, int max)
4208
0
{
4209
0
  int r = strtoint(str);
4210
0
  if (r == -1)
4211
0
    return r;
4212
0
  else if (r<min)
4213
0
    return min;
4214
0
  else if (r>max)
4215
0
    return max;
4216
0
  else
4217
0
    return r;
4218
0
}
4219
4220
static int
4221
evdns_base_set_max_requests_inflight(struct evdns_base *base, int maxinflight)
4222
0
{
4223
0
  int old_n_heads = base->n_req_heads, n_heads;
4224
0
  struct request **old_heads = base->req_heads, **new_heads, *req;
4225
0
  int i;
4226
4227
0
  ASSERT_LOCKED(base);
4228
0
  if (maxinflight < 1)
4229
0
    maxinflight = 1;
4230
0
  n_heads = (maxinflight+4) / 5;
4231
0
  EVUTIL_ASSERT(n_heads > 0);
4232
0
  new_heads = mm_calloc(n_heads, sizeof(struct request*));
4233
0
  if (!new_heads)
4234
0
    return (-1);
4235
0
  if (old_heads) {
4236
0
    for (i = 0; i < old_n_heads; ++i) {
4237
0
      while (old_heads[i]) {
4238
0
        req = old_heads[i];
4239
0
        evdns_request_remove(req, &old_heads[i]);
4240
0
        evdns_request_insert(req, &new_heads[req->trans_id % n_heads]);
4241
0
      }
4242
0
    }
4243
0
    mm_free(old_heads);
4244
0
  }
4245
0
  base->req_heads = new_heads;
4246
0
  base->n_req_heads = n_heads;
4247
0
  base->global_max_requests_inflight = maxinflight;
4248
0
  return (0);
4249
0
}
4250
4251
/* exported function */
4252
int
4253
evdns_base_set_option(struct evdns_base *base,
4254
    const char *option, const char *val)
4255
0
{
4256
0
  int res;
4257
0
  EVDNS_LOCK(base);
4258
0
  res = evdns_base_set_option_impl(base, option, val, DNS_OPTIONS_ALL);
4259
0
  EVDNS_UNLOCK(base);
4260
0
  return res;
4261
0
}
4262
4263
static inline int
4264
str_matches_option(const char *s1, const char *optionname)
4265
0
{
4266
  /* Option names are given as "option:" We accept either 'option' in
4267
   * s1, or 'option:randomjunk'.  The latter form is to implement the
4268
   * resolv.conf parser. */
4269
0
  size_t optlen = strlen(optionname);
4270
0
  size_t slen = strlen(s1);
4271
0
  if (slen == optlen || slen == optlen - 1)
4272
0
    return !strncmp(s1, optionname, slen);
4273
0
  else if (slen > optlen)
4274
0
    return !strncmp(s1, optionname, optlen);
4275
0
  else
4276
0
    return 0;
4277
0
}
4278
4279
/* exported function */
4280
int
4281
evdns_server_port_set_option(struct evdns_server_port *port,
4282
  enum evdns_server_option option, size_t value)
4283
0
{
4284
0
  int res = 0;
4285
0
  EVDNS_LOCK(port);
4286
0
  switch (option) {
4287
0
  case EVDNS_SOPT_TCP_MAX_CLIENTS:
4288
0
    if (!port->listener) {
4289
0
      log(EVDNS_LOG_WARN, "EVDNS_SOPT_TCP_MAX_CLIENTS option can be set only on TCP server");
4290
0
      res = -1;
4291
0
      goto end;
4292
0
    }
4293
0
    port->max_client_connections = value;
4294
0
    log(EVDNS_LOG_DEBUG, "Setting EVDNS_SOPT_TCP_MAX_CLIENTS to %u", port->max_client_connections);
4295
0
    break;
4296
0
  case EVDNS_SOPT_TCP_IDLE_TIMEOUT:
4297
0
    if (!port->listener) {
4298
0
      log(EVDNS_LOG_WARN, "EVDNS_SOPT_TCP_IDLE_TIMEOUT option can be set only on TCP server");
4299
0
      res = -1;
4300
0
      goto end;
4301
0
    }
4302
0
    port->tcp_idle_timeout.tv_sec = value;
4303
0
    port->tcp_idle_timeout.tv_usec = 0;
4304
0
    log(EVDNS_LOG_DEBUG, "Setting EVDNS_SOPT_TCP_IDLE_TIMEOUT to %u seconds",
4305
0
      (unsigned)port->tcp_idle_timeout.tv_sec);
4306
0
    break;
4307
0
  default:
4308
0
    log(EVDNS_LOG_WARN, "Invalid DNS server option %d", (int)option);
4309
0
    res = -1;
4310
0
    break;
4311
0
  }
4312
0
end:
4313
0
  EVDNS_UNLOCK(port);
4314
0
  return res;
4315
0
}
4316
4317
static int
4318
evdns_base_set_option_impl(struct evdns_base *base,
4319
    const char *option, const char *val, int flags)
4320
0
{
4321
0
  ASSERT_LOCKED(base);
4322
0
  if (str_matches_option(option, "ndots:")) {
4323
0
    const int ndots = strtoint(val);
4324
0
    if (ndots == -1) return -1;
4325
0
    if (!(flags & DNS_OPTION_SEARCH)) return 0;
4326
0
    log(EVDNS_LOG_DEBUG, "Setting ndots to %d", ndots);
4327
0
    if (!base->global_search_state) base->global_search_state = search_state_new();
4328
0
    if (!base->global_search_state) return -1;
4329
0
    base->global_search_state->ndots = ndots;
4330
0
  } else if (str_matches_option(option, "timeout:")) {
4331
0
    struct timeval tv;
4332
0
    if (evdns_strtotimeval(val, &tv) == -1) return -1;
4333
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4334
0
    log(EVDNS_LOG_DEBUG, "Setting timeout to %s", val);
4335
0
    memcpy(&base->global_timeout, &tv, sizeof(struct timeval));
4336
0
  } else if (str_matches_option(option, "getaddrinfo-allow-skew:")) {
4337
0
    struct timeval tv;
4338
0
    if (evdns_strtotimeval(val, &tv) == -1) return -1;
4339
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4340
0
    log(EVDNS_LOG_DEBUG, "Setting getaddrinfo-allow-skew to %s",
4341
0
        val);
4342
0
    memcpy(&base->global_getaddrinfo_allow_skew, &tv,
4343
0
        sizeof(struct timeval));
4344
0
  } else if (str_matches_option(option, "max-timeouts:")) {
4345
0
    const int maxtimeout = strtoint_clipped(val, 1, 255);
4346
0
    if (maxtimeout == -1) return -1;
4347
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4348
0
    log(EVDNS_LOG_DEBUG, "Setting maximum allowed timeouts to %d",
4349
0
      maxtimeout);
4350
0
    base->global_max_nameserver_timeout = maxtimeout;
4351
0
  } else if (str_matches_option(option, "max-inflight:")) {
4352
0
    const int maxinflight = strtoint_clipped(val, 1, 65000);
4353
0
    if (maxinflight == -1) return -1;
4354
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4355
0
    log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d",
4356
0
      maxinflight);
4357
0
    evdns_base_set_max_requests_inflight(base, maxinflight);
4358
0
  } else if (str_matches_option(option, "attempts:")) {
4359
0
    int retries = strtoint(val);
4360
0
    if (retries == -1) return -1;
4361
0
    if (retries > 255) retries = 255;
4362
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4363
0
    log(EVDNS_LOG_DEBUG, "Setting retries to %d", retries);
4364
0
    base->global_max_retransmits = retries;
4365
0
  } else if (str_matches_option(option, "randomize-case:")) {
4366
0
    int randcase = strtoint(val);
4367
0
    if (randcase == -1) return -1;
4368
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4369
0
    base->global_randomize_case = randcase;
4370
0
  } else if (str_matches_option(option, "bind-to:")) {
4371
    /* XXX This only applies to successive nameservers, not
4372
     * to already-configured ones.  We might want to fix that. */
4373
0
    int len = sizeof(base->global_outgoing_address);
4374
0
    if (!(flags & DNS_OPTION_NAMESERVERS)) return 0;
4375
0
    if (evutil_parse_sockaddr_port(val,
4376
0
      (struct sockaddr*)&base->global_outgoing_address, &len))
4377
0
      return -1;
4378
0
    base->global_outgoing_addrlen = len;
4379
0
  } else if (str_matches_option(option, "initial-probe-timeout:")) {
4380
0
    struct timeval tv;
4381
0
    if (evdns_strtotimeval(val, &tv) == -1) return -1;
4382
0
    if (tv.tv_sec > 3600)
4383
0
      tv.tv_sec = 3600;
4384
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4385
0
    log(EVDNS_LOG_DEBUG, "Setting initial probe timeout to %s",
4386
0
        val);
4387
0
    memcpy(&base->global_nameserver_probe_initial_timeout, &tv,
4388
0
        sizeof(tv));
4389
0
  } else if (str_matches_option(option, "max-probe-timeout:")) {
4390
0
    const int max_probe_timeout = strtoint_clipped(val, 1, 3600);
4391
0
    if (max_probe_timeout == -1) return -1;
4392
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4393
0
    log(EVDNS_LOG_DEBUG, "Setting maximum probe timeout to %d",
4394
0
      max_probe_timeout);
4395
0
    base->ns_max_probe_timeout = max_probe_timeout;
4396
0
    if (base->global_nameserver_probe_initial_timeout.tv_sec > max_probe_timeout) {
4397
0
      base->global_nameserver_probe_initial_timeout.tv_sec = max_probe_timeout;
4398
0
      base->global_nameserver_probe_initial_timeout.tv_usec = 0;
4399
0
      log(EVDNS_LOG_DEBUG, "Setting initial probe timeout to %s",
4400
0
        val);
4401
0
    }
4402
0
  } else if (str_matches_option(option, "probe-backoff-factor:")) {
4403
0
    const int backoff_backtor = strtoint_clipped(val, 1, 10);
4404
0
    if (backoff_backtor == -1) return -1;
4405
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4406
0
    log(EVDNS_LOG_DEBUG, "Setting probe timeout backoff factor to %d",
4407
0
      backoff_backtor);
4408
0
    base->ns_timeout_backoff_factor = backoff_backtor;
4409
0
  } else if (str_matches_option(option, "so-rcvbuf:")) {
4410
0
    int buf = strtoint(val);
4411
0
    if (buf == -1) return -1;
4412
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4413
0
    log(EVDNS_LOG_DEBUG, "Setting SO_RCVBUF to %s", val);
4414
0
    base->so_rcvbuf = buf;
4415
0
  } else if (str_matches_option(option, "so-sndbuf:")) {
4416
0
    int buf = strtoint(val);
4417
0
    if (buf == -1) return -1;
4418
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4419
0
    log(EVDNS_LOG_DEBUG, "Setting SO_SNDBUF to %s", val);
4420
0
    base->so_sndbuf = buf;
4421
0
  } else if (str_matches_option(option, "tcp-idle-timeout:")) {
4422
0
    struct timeval tv;
4423
0
    if (evdns_strtotimeval(val, &tv) == -1) return -1;
4424
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4425
0
    log(EVDNS_LOG_DEBUG, "Setting tcp idle timeout to %s", val);
4426
0
    memcpy(&base->global_tcp_idle_timeout, &tv, sizeof(tv));
4427
0
  } else if (str_matches_option(option, "use-vc:")) {
4428
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4429
0
    if (val && strlen(val)) return -1;
4430
0
    log(EVDNS_LOG_DEBUG, "Setting use-vc option");
4431
0
    base->global_tcp_flags |= DNS_QUERY_USEVC;
4432
0
  } else if (str_matches_option(option, "ignore-tc:")) {
4433
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4434
0
    if (val && strlen(val)) return -1;
4435
0
    log(EVDNS_LOG_DEBUG, "Setting ignore-tc option");
4436
0
    base->global_tcp_flags |= DNS_QUERY_IGNTC;
4437
0
  } else if (str_matches_option(option, "edns-udp-size:")) {
4438
0
    const int sz = strtoint_clipped(val, DNS_MAX_UDP_SIZE, EDNS_MAX_UDP_SIZE);
4439
0
    if (sz == -1) return -1;
4440
0
    if (!(flags & DNS_OPTION_MISC)) return 0;
4441
0
    log(EVDNS_LOG_DEBUG, "Setting edns-udp-size to %d", sz);
4442
0
    base->global_max_udp_size = sz;
4443
0
  }
4444
0
  return 0;
4445
0
}
4446
4447
int
4448
evdns_set_option(const char *option, const char *val, int flags)
4449
0
{
4450
0
  if (!current_base)
4451
0
    current_base = evdns_base_new(NULL, 0);
4452
0
  return evdns_base_set_option(current_base, option, val);
4453
0
}
4454
4455
static void
4456
0
resolv_conf_parse_line(struct evdns_base *base, char *const start, int flags) {
4457
0
  char *strtok_state;
4458
0
  static const char *const delims = " \t";
4459
0
#define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
4460
4461
4462
0
  char *const first_token = strtok_r(start, delims, &strtok_state);
4463
0
  ASSERT_LOCKED(base);
4464
0
  if (!first_token) return;
4465
4466
0
  if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
4467
0
    const char *const nameserver = NEXT_TOKEN;
4468
4469
0
    if (nameserver)
4470
0
      evdns_base_nameserver_ip_add(base, nameserver);
4471
0
  } else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
4472
0
    const char *const domain = NEXT_TOKEN;
4473
0
    if (domain) {
4474
0
      search_postfix_clear(base);
4475
0
      search_postfix_add(base, domain);
4476
0
    }
4477
0
  } else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
4478
0
    const char *domain;
4479
0
    search_postfix_clear(base);
4480
4481
0
    while ((domain = NEXT_TOKEN)) {
4482
0
      search_postfix_add(base, domain);
4483
0
    }
4484
0
    search_reverse(base);
4485
0
  } else if (!strcmp(first_token, "options")) {
4486
0
    const char *option;
4487
0
    while ((option = NEXT_TOKEN)) {
4488
0
      const char *val = strchr(option, ':');
4489
0
      evdns_base_set_option_impl(base, option, val ? val+1 : "", flags);
4490
0
    }
4491
0
  }
4492
0
#undef NEXT_TOKEN
4493
0
}
4494
4495
/* exported function */
4496
/* returns: */
4497
/*   EVDNS_ERROR_NONE (0) no errors */
4498
/*   EVDNS_ERROR_FAILED_TO_OPEN_FILE (1) failed to open file */
4499
/*   EVDNS_ERROR_FAILED_TO_STAT_FILE (2) failed to stat file */
4500
/*   EVDNS_ERROR_FILE_TOO_LARGE (3) file too large */
4501
/*   EVDNS_ERROR_OUT_OF_MEMORY (4) out of memory */
4502
/*   EVDNS_ERROR_SHORT_READ_FROM_FILE (5) short read from file */
4503
/*   EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED (6) no nameservers configured */
4504
int
4505
0
evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename) {
4506
0
  int res;
4507
0
  EVDNS_LOCK(base);
4508
0
  res = evdns_base_resolv_conf_parse_impl(base, flags, filename);
4509
0
  EVDNS_UNLOCK(base);
4510
0
  return res;
4511
0
}
4512
4513
static char *
4514
evdns_get_default_hosts_filename(void)
4515
0
{
4516
#ifdef _WIN32
4517
  /* Windows is a little coy about where it puts its configuration
4518
   * files.  Sure, they're _usually_ in C:\windows\system32, but
4519
   * there's no reason in principle they couldn't be in
4520
   * W:\hoboken chicken emergency\
4521
   */
4522
  char path[MAX_PATH+1];
4523
  static const char hostfile[] = "\\drivers\\etc\\hosts";
4524
  char *path_out;
4525
  size_t len_out;
4526
4527
  if (! SHGetSpecialFolderPathA(NULL, path, CSIDL_SYSTEM, 0))
4528
    return NULL;
4529
  len_out = strlen(path)+strlen(hostfile)+1;
4530
  path_out = mm_malloc(len_out);
4531
  evutil_snprintf(path_out, len_out, "%s%s", path, hostfile);
4532
  return path_out;
4533
#else
4534
0
  return mm_strdup("/etc/hosts");
4535
0
#endif
4536
0
}
4537
4538
static int
4539
0
evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename) {
4540
0
  size_t n;
4541
0
  char *resolv;
4542
0
  char *start;
4543
0
  int err = EVDNS_ERROR_NONE;
4544
0
  int add_default;
4545
4546
0
  log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
4547
4548
0
  add_default = flags & DNS_OPTION_NAMESERVERS;
4549
0
  if (flags & DNS_OPTION_NAMESERVERS_NO_DEFAULT)
4550
0
    add_default = 0;
4551
4552
0
  if (flags & DNS_OPTION_HOSTSFILE) {
4553
0
    char *fname = evdns_get_default_hosts_filename();
4554
0
    evdns_base_load_hosts(base, fname);
4555
0
    if (fname)
4556
0
      mm_free(fname);
4557
0
  }
4558
4559
0
  if (!filename) {
4560
0
    evdns_resolv_set_defaults(base, flags);
4561
0
    return EVDNS_ERROR_FAILED_TO_OPEN_FILE;
4562
0
  }
4563
4564
0
  if ((err = evutil_read_file_(filename, &resolv, &n, 0)) < 0) {
4565
0
    if (err == -1) {
4566
      /* No file. */
4567
0
      evdns_resolv_set_defaults(base, flags);
4568
0
      return EVDNS_ERROR_FAILED_TO_OPEN_FILE;
4569
0
    } else {
4570
0
      return EVDNS_ERROR_FAILED_TO_STAT_FILE;
4571
0
    }
4572
0
  }
4573
4574
0
  start = resolv;
4575
0
  for (;;) {
4576
0
    char *const newline = strchr(start, '\n');
4577
0
    if (!newline) {
4578
0
      resolv_conf_parse_line(base, start, flags);
4579
0
      break;
4580
0
    } else {
4581
0
      *newline = 0;
4582
0
      resolv_conf_parse_line(base, start, flags);
4583
0
      start = newline + 1;
4584
0
    }
4585
0
  }
4586
4587
0
  if (!base->server_head && add_default) {
4588
    /* no nameservers were configured. */
4589
0
    evdns_base_nameserver_ip_add(base, "127.0.0.1");
4590
0
    err = EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED;
4591
0
  }
4592
0
  if (flags & DNS_OPTION_SEARCH && (!base->global_search_state || base->global_search_state->num_domains == 0)) {
4593
0
    search_set_from_hostname(base);
4594
0
  }
4595
4596
0
  mm_free(resolv);
4597
0
  return err;
4598
0
}
4599
4600
int
4601
0
evdns_resolv_conf_parse(int flags, const char *const filename) {
4602
0
  if (!current_base)
4603
0
    current_base = evdns_base_new(NULL, 0);
4604
0
  return evdns_base_resolv_conf_parse(current_base, flags, filename);
4605
0
}
4606
4607
4608
#ifdef _WIN32
4609
/* Add multiple nameservers from a space-or-comma-separated list. */
4610
static int
4611
evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) {
4612
  const char *addr;
4613
  char *buf;
4614
  int r;
4615
  ASSERT_LOCKED(base);
4616
  while (*ips) {
4617
    while (isspace(*ips) || *ips == ',' || *ips == '\t')
4618
      ++ips;
4619
    addr = ips;
4620
    while (isdigit(*ips) || *ips == '.' || *ips == ':' ||
4621
        *ips=='[' || *ips==']')
4622
      ++ips;
4623
    buf = mm_malloc(ips-addr+1);
4624
    if (!buf) return 4;
4625
    memcpy(buf, addr, ips-addr);
4626
    buf[ips-addr] = '\0';
4627
    r = evdns_base_nameserver_ip_add(base, buf);
4628
    mm_free(buf);
4629
    if (r) return r;
4630
  }
4631
  return 0;
4632
}
4633
4634
typedef DWORD(WINAPI *GetAdaptersAddresses_fn_t)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
4635
4636
/* Use the windows GetAdaptersAddresses interface in iphlpapi.dll to */
4637
/* figure out what our nameservers are. */
4638
static int
4639
load_nameservers_with_getadaptersaddresses_unlocked(struct evdns_base *base)
4640
{
4641
  PIP_ADAPTER_ADDRESSES addresses = NULL;
4642
  HMODULE handle = 0;
4643
  ULONG size = sizeof(IP_ADAPTER_ADDRESSES);
4644
  void *buf = NULL;
4645
  int status = 0, r, added_any = 0;
4646
  GetAdaptersAddresses_fn_t fn;
4647
  IP_ADAPTER_DNS_SERVER_ADDRESS *dnsserver = NULL;
4648
4649
  ASSERT_LOCKED(base);
4650
  if (!(handle = evutil_load_windows_system_library_(
4651
      TEXT("iphlpapi.dll")))) {
4652
    log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
4653
    status = -1;
4654
    goto done;
4655
  }
4656
  if (!(fn = (GetAdaptersAddresses_fn_t) GetProcAddress(handle, "GetAdaptersAddresses"))) {
4657
    log(EVDNS_LOG_WARN, "Could not get address of function.");
4658
    status = -1;
4659
    goto done;
4660
  }
4661
4662
  buf = mm_malloc(size);
4663
  if (!buf) { status = 4; goto done; }
4664
  addresses = buf;
4665
  r = fn(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, addresses, &size);
4666
  if (r != NO_ERROR && r != ERROR_BUFFER_OVERFLOW) {
4667
    status = -1;
4668
    goto done;
4669
  }
4670
  if (r != NO_ERROR) {
4671
    mm_free(buf);
4672
    buf = mm_malloc(size);
4673
    if (!buf) { status = 4; goto done; }
4674
    addresses = buf;
4675
    r = fn(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, addresses, &size);
4676
    if (r != NO_ERROR) {
4677
      log(EVDNS_LOG_DEBUG, "fn() failed.");
4678
      status = -1;
4679
      goto done;
4680
    }
4681
  }
4682
4683
  while (addresses) {
4684
    dnsserver = addresses->FirstDnsServerAddress;
4685
    while (dnsserver && (addresses->OperStatus == IfOperStatusUp)) {
4686
      char ip[INET6_ADDRSTRLEN] = {0};
4687
      if (AF_INET == dnsserver->Address.lpSockaddr->sa_family) {
4688
        inet_ntop(AF_INET, &((SOCKADDR_IN *)dnsserver->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
4689
      } else if (AF_INET6 == dnsserver->Address.lpSockaddr->sa_family) {
4690
        inet_ntop(AF_INET6, &((SOCKADDR_IN6 *)dnsserver->Address.lpSockaddr)->sin6_addr, ip, sizeof(ip));
4691
      }
4692
4693
      dnsserver = dnsserver->Next;
4694
      if (strncmp(ip, "fec0:", 5) == 0) { /* remove ipv6 reserved address */
4695
        continue;
4696
      }
4697
      
4698
      r = evdns_base_nameserver_ip_add(base, ip);
4699
      if (r) {
4700
        log(EVDNS_LOG_DEBUG, "Could not add nameserver %s to list, error: %d", ip, r);
4701
        status = r;
4702
      } else {
4703
        ++added_any;
4704
        log(EVDNS_LOG_DEBUG, "Successfully added %s as nameserver", ip);
4705
      }
4706
    }
4707
    
4708
    addresses = addresses->Next;
4709
  }
4710
4711
  if (!added_any) {
4712
    log(EVDNS_LOG_DEBUG, "No nameservers added.");
4713
    if (status == 0)
4714
      status = -1;
4715
  } else {
4716
    status = 0;
4717
  }
4718
4719
 done:
4720
  if (buf)
4721
    mm_free(buf);
4722
  if (handle)
4723
    FreeLibrary(handle);
4724
  return status;
4725
}
4726
4727
int
4728
load_nameservers_with_getadaptersaddresses(struct evdns_base *base)
4729
{
4730
  int r;
4731
  EVDNS_LOCK(base);
4732
  r = load_nameservers_with_getadaptersaddresses_unlocked(base);
4733
  EVDNS_UNLOCK(base);
4734
  return r;
4735
}
4736
4737
static int
4738
config_nameserver_from_reg_key(struct evdns_base *base, HKEY key, const TCHAR *subkey)
4739
{
4740
  char *buf;
4741
  DWORD bufsz = 0, type = 0;
4742
  int status = 0;
4743
4744
  ASSERT_LOCKED(base);
4745
  if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
4746
      != ERROR_MORE_DATA)
4747
    return -1;
4748
  if (!(buf = mm_malloc(bufsz)))
4749
    return -1;
4750
4751
  if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
4752
      == ERROR_SUCCESS && bufsz > 1) {
4753
    status = evdns_nameserver_ip_add_line(base,buf);
4754
  }
4755
4756
  mm_free(buf);
4757
  return status;
4758
}
4759
4760
#define SERVICES_KEY TEXT("System\\CurrentControlSet\\Services\\")
4761
#define WIN_NS_9X_KEY  SERVICES_KEY TEXT("VxD\\MSTCP")
4762
#define WIN_NS_NT_KEY  SERVICES_KEY TEXT("Tcpip\\Parameters")
4763
4764
static int
4765
load_nameservers_from_registry(struct evdns_base *base)
4766
{
4767
  int found = 0;
4768
  int r;
4769
#define TRY(k, name) \
4770
  if (!found && config_nameserver_from_reg_key(base,k,TEXT(name)) == 0) { \
4771
    log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
4772
    found = 1;            \
4773
  } else if (!found) {            \
4774
    log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
4775
        #k,#name);            \
4776
  }
4777
4778
  ASSERT_LOCKED(base);
4779
4780
  if (((int)GetVersion()) > 0) { /* NT */
4781
    HKEY nt_key = 0, interfaces_key = 0;
4782
4783
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
4784
         KEY_READ, &nt_key) != ERROR_SUCCESS) {
4785
      log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
4786
      return -1;
4787
    }
4788
    r = RegOpenKeyEx(nt_key, TEXT("Interfaces"), 0,
4789
           KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
4790
           &interfaces_key);
4791
    if (r != ERROR_SUCCESS) {
4792
      log(EVDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
4793
      return -1;
4794
    }
4795
    TRY(nt_key, "NameServer");
4796
    TRY(nt_key, "DhcpNameServer");
4797
    TRY(interfaces_key, "NameServer");
4798
    TRY(interfaces_key, "DhcpNameServer");
4799
    RegCloseKey(interfaces_key);
4800
    RegCloseKey(nt_key);
4801
  } else {
4802
    HKEY win_key = 0;
4803
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
4804
         KEY_READ, &win_key) != ERROR_SUCCESS) {
4805
      log(EVDNS_LOG_DEBUG, "Couldn't open registry key, %d", (int)GetLastError());
4806
      return -1;
4807
    }
4808
    TRY(win_key, "NameServer");
4809
    RegCloseKey(win_key);
4810
  }
4811
4812
  if (found == 0) {
4813
    log(EVDNS_LOG_WARN,"Didn't find any nameservers.");
4814
  }
4815
4816
  return found ? 0 : -1;
4817
#undef TRY
4818
}
4819
4820
int
4821
evdns_base_config_windows_nameservers(struct evdns_base *base)
4822
{
4823
  int r;
4824
  char *fname;
4825
  if (base == NULL)
4826
    base = current_base;
4827
  if (base == NULL)
4828
    return -1;
4829
  EVDNS_LOCK(base);
4830
  fname = evdns_get_default_hosts_filename();
4831
  log(EVDNS_LOG_DEBUG, "Loading hosts entries from %s", fname);
4832
  evdns_base_load_hosts(base, fname);
4833
  if (fname)
4834
    mm_free(fname);
4835
4836
  if (load_nameservers_with_getadaptersaddresses_unlocked(base) == 0) {
4837
    EVDNS_UNLOCK(base);
4838
    return 0;
4839
  }
4840
  r = load_nameservers_from_registry(base);
4841
4842
  EVDNS_UNLOCK(base);
4843
  return r;
4844
}
4845
4846
int
4847
evdns_config_windows_nameservers(void)
4848
{
4849
  if (!current_base) {
4850
    current_base = evdns_base_new(NULL, 1);
4851
    return current_base == NULL ? -1 : 0;
4852
  } else {
4853
    return evdns_base_config_windows_nameservers(current_base);
4854
  }
4855
}
4856
#endif
4857
4858
struct evdns_base *
4859
evdns_base_new(struct event_base *event_base, int flags)
4860
0
{
4861
0
  struct evdns_base *base;
4862
4863
0
  if (evutil_secure_rng_init() < 0) {
4864
0
    log(EVDNS_LOG_WARN, "Unable to seed random number generator; "
4865
0
        "DNS can't run.");
4866
0
    return NULL;
4867
0
  }
4868
4869
  /* Give the evutil library a hook into its evdns-enabled
4870
   * functionality.  We can't just call evdns_getaddrinfo directly or
4871
   * else libevent-core will depend on libevent-extras. */
4872
0
  evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo);
4873
0
  evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel);
4874
4875
0
  base = mm_malloc(sizeof(struct evdns_base));
4876
0
  if (base == NULL)
4877
0
    return (NULL);
4878
0
  memset(base, 0, sizeof(struct evdns_base));
4879
0
  base->req_waiting_head = NULL;
4880
4881
0
  EVTHREAD_ALLOC_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
4882
0
  EVDNS_LOCK(base);
4883
4884
  /* Set max requests inflight and allocate req_heads. */
4885
0
  base->req_heads = NULL;
4886
4887
0
  evdns_base_set_max_requests_inflight(base, 64);
4888
4889
0
  base->server_head = NULL;
4890
0
  base->event_base = event_base;
4891
0
  base->global_good_nameservers = base->global_requests_inflight =
4892
0
    base->global_requests_waiting = 0;
4893
4894
0
  base->global_timeout.tv_sec = 5;
4895
0
  base->global_timeout.tv_usec = 0;
4896
0
  base->global_max_reissues = 1;
4897
0
  base->global_max_retransmits = 3;
4898
0
  base->global_max_nameserver_timeout = 3;
4899
0
  base->global_search_state = NULL;
4900
0
  base->global_randomize_case = 1;
4901
0
  base->global_max_udp_size = DNS_MAX_UDP_SIZE;
4902
0
  base->global_getaddrinfo_allow_skew.tv_sec = 3;
4903
0
  base->global_getaddrinfo_allow_skew.tv_usec = 0;
4904
0
  base->global_nameserver_probe_initial_timeout.tv_sec = 10;
4905
0
  base->global_nameserver_probe_initial_timeout.tv_usec = 0;
4906
0
  base->ns_max_probe_timeout = 3600;
4907
0
  base->ns_timeout_backoff_factor = 3;
4908
0
  base->global_tcp_idle_timeout.tv_sec = CLIENT_IDLE_CONN_TIMEOUT;
4909
4910
0
  TAILQ_INIT(&base->hostsdb);
4911
0
  SPLAY_INIT(&base->cache_root);
4912
4913
0
#define EVDNS_BASE_ALL_FLAGS ( \
4914
0
  EVDNS_BASE_INITIALIZE_NAMESERVERS | \
4915
0
  EVDNS_BASE_DISABLE_WHEN_INACTIVE  | \
4916
0
  EVDNS_BASE_NAMESERVERS_NO_DEFAULT | \
4917
0
  EVDNS_BASE_NO_CACHE               | \
4918
0
  0)
4919
4920
0
  if (flags & ~EVDNS_BASE_ALL_FLAGS) {
4921
0
    flags = EVDNS_BASE_INITIALIZE_NAMESERVERS;
4922
0
    log(EVDNS_LOG_WARN,
4923
0
        "Unrecognized flag passed to evdns_base_new(). Assuming "
4924
0
        "you meant EVDNS_BASE_INITIALIZE_NAMESERVERS.");
4925
0
  }
4926
0
#undef EVDNS_BASE_ALL_FLAGS
4927
4928
0
  if (flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE) {
4929
0
    base->disable_when_inactive = 1;
4930
0
  }
4931
4932
0
  if (flags & EVDNS_BASE_INITIALIZE_NAMESERVERS) {
4933
0
    int r;
4934
0
    int opts = DNS_OPTIONS_ALL;
4935
0
    if (flags & EVDNS_BASE_NAMESERVERS_NO_DEFAULT) {
4936
0
      opts |= DNS_OPTION_NAMESERVERS_NO_DEFAULT;
4937
0
    }
4938
4939
#ifdef _WIN32
4940
    r = evdns_base_config_windows_nameservers(base);
4941
#else
4942
0
    r = evdns_base_resolv_conf_parse(base, opts, evutil_resolvconf_filename_());
4943
0
#endif
4944
0
    if (r && (EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED != r)) {
4945
0
      evdns_base_free_and_unlock(base, 0);
4946
0
      return NULL;
4947
0
    }
4948
0
  }
4949
4950
0
  base->disable_cache = flags & EVDNS_BASE_NO_CACHE;
4951
4952
0
  EVDNS_UNLOCK(base);
4953
0
  return base;
4954
0
}
4955
4956
int
4957
evdns_init(void)
4958
0
{
4959
0
  struct evdns_base *base = evdns_base_new(NULL, 1);
4960
0
  if (base) {
4961
0
    current_base = base;
4962
0
    return 0;
4963
0
  } else {
4964
0
    return -1;
4965
0
  }
4966
0
}
4967
4968
const char *
4969
evdns_err_to_string(int err)
4970
0
{
4971
0
    switch (err) {
4972
0
  case DNS_ERR_NONE: return "no error";
4973
0
  case DNS_ERR_FORMAT: return "misformatted query";
4974
0
  case DNS_ERR_SERVERFAILED: return "server failed";
4975
0
  case DNS_ERR_NOTEXIST: return "name does not exist";
4976
0
  case DNS_ERR_NOTIMPL: return "query not implemented";
4977
0
  case DNS_ERR_REFUSED: return "refused";
4978
4979
0
  case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
4980
0
  case DNS_ERR_UNKNOWN: return "unknown";
4981
0
  case DNS_ERR_TIMEOUT: return "request timed out";
4982
0
  case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
4983
0
  case DNS_ERR_CANCEL: return "dns request canceled";
4984
0
  case DNS_ERR_NODATA: return "no records in the reply";
4985
0
  default: return "[Unknown error code]";
4986
0
    }
4987
0
}
4988
4989
static void
4990
evdns_nameserver_free(struct nameserver *server)
4991
0
{
4992
0
  if (server->socket >= 0)
4993
0
    evutil_closesocket(server->socket);
4994
0
  (void) event_del(&server->event);
4995
0
  event_debug_unassign(&server->event);
4996
0
  if (server->state == 0)
4997
0
    (void) event_del(&server->timeout_event);
4998
0
  if (server->probe_request) {
4999
0
    evdns_cancel_request(server->base, server->probe_request);
5000
0
    server->probe_request = NULL;
5001
0
  }
5002
0
  event_debug_unassign(&server->timeout_event);
5003
0
  disconnect_and_free_connection(server->connection);
5004
0
  mm_free(server);
5005
0
}
5006
5007
static int
5008
evdns_cache_compare(struct evdns_cache *a, struct evdns_cache *b)
5009
0
{
5010
0
  return strcasecmp(a->name, b->name);
5011
0
}
5012
5013
0
SPLAY_PROTOTYPE(evdns_tree, evdns_cache, node, evdns_cache_compare);
5014
0
SPLAY_GENERATE(evdns_tree, evdns_cache, node, evdns_cache_compare);
Unexecuted instantiation: evdns_tree_SPLAY_INSERT
Unexecuted instantiation: evdns_tree_SPLAY_REMOVE
Unexecuted instantiation: evdns_tree_SPLAY
Unexecuted instantiation: evdns_tree_SPLAY_MINMAX
5015
5016
static void
5017
evdns_cache_free(struct evdns_cache *cache)
5018
0
{
5019
0
  SPLAY_REMOVE(evdns_tree, &cache->base->cache_root, cache);
5020
0
  mm_free(cache->name);
5021
0
  evtimer_del(&cache->ev_timeout);
5022
0
  evutil_freeaddrinfo(cache->ai);
5023
0
  mm_free(cache);
5024
0
}
5025
5026
static void
5027
evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests)
5028
0
{
5029
0
  struct nameserver *server, *server_next;
5030
0
  struct search_domain *dom, *dom_next;
5031
0
  int i;
5032
5033
  /* Requires that we hold the lock. */
5034
5035
  /* TODO(nickm) we might need to refcount here. */
5036
5037
0
  while (base->req_waiting_head) {
5038
0
    if (fail_requests)
5039
0
      reply_schedule_callback(base->req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
5040
0
    request_finished(base->req_waiting_head, &base->req_waiting_head, 1);
5041
0
  }
5042
0
  for (i = 0; i < base->n_req_heads; ++i) {
5043
0
    while (base->req_heads[i]) {
5044
0
      if (fail_requests)
5045
0
        reply_schedule_callback(base->req_heads[i], 0, DNS_ERR_SHUTDOWN, NULL);
5046
0
      request_finished(base->req_heads[i], &REQ_HEAD(base, base->req_heads[i]->trans_id), 1);
5047
0
    }
5048
0
  }
5049
0
  base->global_requests_inflight = base->global_requests_waiting = 0;
5050
5051
0
  for (server = base->server_head; server; server = server_next) {
5052
0
    server_next = server->next;
5053
    /** already done something before */
5054
0
    server->probe_request = NULL;
5055
0
    evdns_nameserver_free(server);
5056
0
    if (server_next == base->server_head)
5057
0
      break;
5058
0
  }
5059
0
  base->server_head = NULL;
5060
0
  base->global_good_nameservers = 0;
5061
5062
0
  if (base->global_search_state) {
5063
0
    for (dom = base->global_search_state->head; dom; dom = dom_next) {
5064
0
      dom_next = dom->next;
5065
0
      mm_free(dom);
5066
0
    }
5067
0
    mm_free(base->global_search_state);
5068
0
    base->global_search_state = NULL;
5069
0
  }
5070
5071
0
  {
5072
0
    struct hosts_entry *victim;
5073
0
    while ((victim = TAILQ_FIRST(&base->hostsdb))) {
5074
0
      TAILQ_REMOVE(&base->hostsdb, victim, next);
5075
0
      mm_free(victim);
5076
0
    }
5077
0
  }
5078
5079
0
  mm_free(base->req_heads);
5080
5081
0
  while (!SPLAY_EMPTY(&base->cache_root)) {
5082
0
    evdns_cache_free(SPLAY_ROOT(&base->cache_root));
5083
0
  }
5084
5085
0
  EVDNS_UNLOCK(base);
5086
0
  EVTHREAD_FREE_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
5087
5088
0
  mm_free(base);
5089
0
}
5090
5091
void
5092
evdns_base_free(struct evdns_base *base, int fail_requests)
5093
0
{
5094
0
  EVDNS_LOCK(base);
5095
0
  evdns_base_free_and_unlock(base, fail_requests);
5096
0
}
5097
5098
void
5099
evdns_base_clear_host_addresses(struct evdns_base *base)
5100
0
{
5101
0
  struct hosts_entry *victim;
5102
0
  EVDNS_LOCK(base);
5103
0
  while ((victim = TAILQ_FIRST(&base->hostsdb))) {
5104
0
    TAILQ_REMOVE(&base->hostsdb, victim, next);
5105
0
    mm_free(victim);
5106
0
  }
5107
0
  EVDNS_UNLOCK(base);
5108
0
}
5109
5110
void
5111
evdns_shutdown(int fail_requests)
5112
0
{
5113
0
  if (current_base) {
5114
0
    struct evdns_base *b = current_base;
5115
0
    current_base = NULL;
5116
0
    evdns_base_free(b, fail_requests);
5117
0
  }
5118
0
  evdns_log_fn = NULL;
5119
0
}
5120
5121
static int
5122
evdns_base_parse_hosts_line(struct evdns_base *base, char *line)
5123
0
{
5124
0
  char *strtok_state;
5125
0
  static const char *const delims = " \t";
5126
0
  char *const addr = strtok_r(line, delims, &strtok_state);
5127
0
  char *hostname, *hash;
5128
0
  struct sockaddr_storage ss;
5129
0
  int socklen = sizeof(ss);
5130
0
  ASSERT_LOCKED(base);
5131
5132
0
#define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
5133
5134
0
  if (!addr || *addr == '#')
5135
0
    return 0;
5136
5137
0
  memset(&ss, 0, sizeof(ss));
5138
0
  if (evutil_parse_sockaddr_port(addr, (struct sockaddr*)&ss, &socklen)<0)
5139
0
    return -1;
5140
0
  if (socklen > (int)sizeof(struct sockaddr_in6))
5141
0
    return -1;
5142
5143
0
  if (sockaddr_getport((struct sockaddr*)&ss))
5144
0
    return -1;
5145
5146
0
  while ((hostname = NEXT_TOKEN)) {
5147
0
    struct hosts_entry *he;
5148
0
    size_t namelen;
5149
0
    if ((hash = strchr(hostname, '#'))) {
5150
0
      if (hash == hostname)
5151
0
        return 0;
5152
0
      *hash = '\0';
5153
0
    }
5154
5155
0
    namelen = strlen(hostname);
5156
5157
0
    he = mm_calloc(1, sizeof(struct hosts_entry)+namelen);
5158
0
    if (!he)
5159
0
      return -1;
5160
0
    EVUTIL_ASSERT(socklen <= (int)sizeof(he->addr));
5161
0
    memcpy(&he->addr, &ss, socklen);
5162
0
    memcpy(he->hostname, hostname, namelen+1);
5163
0
    he->addrlen = socklen;
5164
5165
0
    TAILQ_INSERT_TAIL(&base->hostsdb, he, next);
5166
5167
0
    if (hash)
5168
0
      return 0;
5169
0
  }
5170
5171
0
  return 0;
5172
0
#undef NEXT_TOKEN
5173
0
}
5174
5175
static int
5176
evdns_base_load_hosts_impl(struct evdns_base *base, const char *hosts_fname)
5177
0
{
5178
0
  char *str=NULL, *cp, *eol;
5179
0
  size_t len;
5180
0
  int err=0;
5181
5182
0
  ASSERT_LOCKED(base);
5183
5184
0
  if (hosts_fname == NULL ||
5185
0
      (err = evutil_read_file_(hosts_fname, &str, &len, 0)) < 0) {
5186
0
    char tmp[64];
5187
0
    strlcpy(tmp, "127.0.0.1   localhost", sizeof(tmp));
5188
0
    evdns_base_parse_hosts_line(base, tmp);
5189
0
    strlcpy(tmp, "::1   localhost", sizeof(tmp));
5190
0
    evdns_base_parse_hosts_line(base, tmp);
5191
0
    return err ? -1 : 0;
5192
0
  }
5193
5194
  /* This will break early if there is a NUL in the hosts file.
5195
   * Probably not a problem.*/
5196
0
  cp = str;
5197
0
  for (;;) {
5198
0
    eol = strchr(cp, '\n');
5199
5200
0
    if (eol) {
5201
0
      *eol = '\0';
5202
0
      evdns_base_parse_hosts_line(base, cp);
5203
0
      cp = eol+1;
5204
0
    } else {
5205
0
      evdns_base_parse_hosts_line(base, cp);
5206
0
      break;
5207
0
    }
5208
0
  }
5209
5210
0
  mm_free(str);
5211
0
  return 0;
5212
0
}
5213
5214
int
5215
evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname)
5216
0
{
5217
0
  int res;
5218
0
  if (!base)
5219
0
    base = current_base;
5220
0
  EVDNS_LOCK(base);
5221
0
  res = evdns_base_load_hosts_impl(base, hosts_fname);
5222
0
  EVDNS_UNLOCK(base);
5223
0
  return res;
5224
0
}
5225
5226
/* A single request for a getaddrinfo, either v4 or v6. */
5227
struct getaddrinfo_subrequest {
5228
  struct evdns_request *r;
5229
  ev_uint32_t type;
5230
};
5231
5232
/* State data used to implement an in-progress getaddrinfo. */
5233
struct evdns_getaddrinfo_request {
5234
  struct evdns_base *evdns_base;
5235
  /* Copy of the modified 'hints' data that we'll use to build
5236
   * answers. */
5237
  struct evutil_addrinfo hints;
5238
  /* The original requested nodename */
5239
  char *nodename;
5240
  /* The callback to invoke when we're done */
5241
  evdns_getaddrinfo_cb user_cb;
5242
  /* User-supplied data to give to the callback. */
5243
  void *user_data;
5244
  /* The port to use when building sockaddrs. */
5245
  ev_uint16_t port;
5246
  /* The sub_request for an A record (if any) */
5247
  struct getaddrinfo_subrequest ipv4_request;
5248
  /* The sub_request for an AAAA record (if any) */
5249
  struct getaddrinfo_subrequest ipv6_request;
5250
5251
  /* The cname result that we were told (if any) */
5252
  char *cname_result;
5253
5254
  /* If we have one request answered and one request still inflight,
5255
   * then this field holds the answer from the first request... */
5256
  struct evutil_addrinfo *pending_result;
5257
  int pending_result_ttl;
5258
  /* And this event is a timeout that will tell us to cancel the second
5259
   * request if it's taking a long time. */
5260
  struct event timeout;
5261
5262
  /* And this field holds the error code from the first request... */
5263
  int pending_error;
5264
  /* If this is set, the user canceled this request. */
5265
  unsigned user_canceled : 1;
5266
  /* If this is set, the user can no longer cancel this request; we're
5267
   * just waiting for the free. */
5268
  unsigned request_done : 1;
5269
};
5270
5271
/* Convert an evdns errors to the equivalent getaddrinfo error. */
5272
static int
5273
evdns_err_to_getaddrinfo_err(int e1)
5274
0
{
5275
  /* XXX Do this better! */
5276
0
  if (e1 == DNS_ERR_NONE)
5277
0
    return 0;
5278
0
  else if (e1 == DNS_ERR_NOTEXIST)
5279
0
    return EVUTIL_EAI_NONAME;
5280
0
  else
5281
0
    return EVUTIL_EAI_FAIL;
5282
0
}
5283
5284
/* Return the more informative of two getaddrinfo errors. */
5285
static int
5286
getaddrinfo_merge_err(int e1, int e2)
5287
0
{
5288
  /* XXXX be cleverer here. */
5289
0
  if (e1 == 0)
5290
0
    return e2;
5291
0
  else
5292
0
    return e1;
5293
0
}
5294
5295
static void
5296
free_getaddrinfo_request(struct evdns_getaddrinfo_request *data)
5297
0
{
5298
  /* DO NOT CALL this if either of the requests is pending.  Only once
5299
   * both callbacks have been invoked is it safe to free the request */
5300
0
  if (data->pending_result)
5301
0
    evutil_freeaddrinfo(data->pending_result);
5302
0
  if (data->cname_result)
5303
0
    mm_free(data->cname_result);
5304
0
  event_del(&data->timeout);
5305
0
  mm_free(data->nodename);
5306
0
  mm_free(data);
5307
0
  return;
5308
0
}
5309
5310
static void
5311
add_cname_to_reply(struct evdns_getaddrinfo_request *data,
5312
    struct evutil_addrinfo *ai)
5313
0
{
5314
0
  if (data->cname_result && ai) {
5315
0
    ai->ai_canonname = data->cname_result;
5316
0
    data->cname_result = NULL;
5317
0
  }
5318
0
}
5319
5320
/* Callback: invoked when one request in a mixed-format A/AAAA getaddrinfo
5321
 * request has finished, but the other one took too long to answer. Pass
5322
 * along the answer we got, and cancel the other request.
5323
 */
5324
static void
5325
evdns_getaddrinfo_timeout_cb(evutil_socket_t fd, short what, void *ptr)
5326
0
{
5327
0
  int v4_timedout = 0, v6_timedout = 0;
5328
0
  struct evdns_getaddrinfo_request *data = ptr;
5329
5330
  /* Cancel any pending requests, and note which one */
5331
0
  if (data->ipv4_request.r) {
5332
    /* XXXX This does nothing if the request's callback is already
5333
     * running (pending_cb is set). */
5334
0
    evdns_cancel_request(NULL, data->ipv4_request.r);
5335
0
    v4_timedout = 1;
5336
0
    EVDNS_LOCK(data->evdns_base);
5337
0
    ++data->evdns_base->getaddrinfo_ipv4_timeouts;
5338
0
    EVDNS_UNLOCK(data->evdns_base);
5339
0
  }
5340
0
  if (data->ipv6_request.r) {
5341
    /* XXXX This does nothing if the request's callback is already
5342
     * running (pending_cb is set). */
5343
0
    evdns_cancel_request(NULL, data->ipv6_request.r);
5344
0
    v6_timedout = 1;
5345
0
    EVDNS_LOCK(data->evdns_base);
5346
0
    ++data->evdns_base->getaddrinfo_ipv6_timeouts;
5347
0
    EVDNS_UNLOCK(data->evdns_base);
5348
0
  }
5349
5350
  /* We only use this timeout callback when we have an answer for
5351
   * one address. */
5352
0
  EVUTIL_ASSERT(!v4_timedout || !v6_timedout);
5353
5354
  /* Report the outcome of the other request that didn't time out. */
5355
0
  if (data->pending_result) {
5356
0
    add_cname_to_reply(data, data->pending_result);
5357
0
    data->user_cb(0, data->pending_result, data->user_data);
5358
0
    data->pending_result = NULL;
5359
0
  } else {
5360
0
    int e = data->pending_error;
5361
0
    if (!e)
5362
0
      e = EVUTIL_EAI_AGAIN;
5363
0
    data->user_cb(e, NULL, data->user_data);
5364
0
  }
5365
5366
0
  data->user_cb = NULL; /* prevent double-call if evdns callbacks are
5367
             * in-progress. XXXX It would be better if this
5368
             * weren't necessary. */
5369
5370
0
  if (!v4_timedout && !v6_timedout) {
5371
    /* should be impossible? XXXX */
5372
0
    free_getaddrinfo_request(data);
5373
0
  }
5374
0
}
5375
5376
static int
5377
evdns_getaddrinfo_set_timeout(struct evdns_base *evdns_base,
5378
    struct evdns_getaddrinfo_request *data)
5379
0
{
5380
0
  return event_add(&data->timeout, &evdns_base->global_getaddrinfo_allow_skew);
5381
0
}
5382
5383
static inline int
5384
evdns_result_is_answer(int result)
5385
0
{
5386
0
  return (result != DNS_ERR_NOTIMPL && result != DNS_ERR_REFUSED &&
5387
0
      result != DNS_ERR_SERVERFAILED && result != DNS_ERR_CANCEL);
5388
0
}
5389
5390
static void
5391
evdns_ttl_expired(evutil_socket_t fd, short what, void *arg)
5392
0
{
5393
0
  struct evdns_cache *cache = arg;
5394
0
  struct evdns_base *base = cache->base;
5395
0
  log(EVDNS_LOG_DEBUG, "Expiring cache for %s", cache->name);
5396
0
  EVDNS_LOCK(base);
5397
0
  evdns_cache_free(cache);
5398
0
  EVDNS_UNLOCK(base);
5399
0
}
5400
5401
void
5402
evdns_cache_write(struct evdns_base *dns_base, char *nodename, struct evutil_addrinfo *res, int ttl)
5403
0
{
5404
0
  struct timeval tv;
5405
0
  struct evdns_cache *cache;
5406
0
  struct evdns_cache find;
5407
5408
0
  log(EVDNS_LOG_DEBUG, "Writing cache for %s", nodename);
5409
0
  EVDNS_LOCK(dns_base);
5410
0
  find.name = (char *)nodename;
5411
0
  cache = SPLAY_FIND(evdns_tree, &dns_base->cache_root, &find);
5412
0
  if (cache) {
5413
0
    log(EVDNS_LOG_DEBUG, "Ejecting old cache for %s", nodename);
5414
0
    evdns_cache_free(cache);
5415
0
  }
5416
0
  if (res) {
5417
0
    cache = mm_calloc(1, sizeof(struct evdns_cache));
5418
0
    cache->base = dns_base;
5419
0
    cache->name = mm_strdup(nodename);
5420
0
    cache->ai = evutil_dup_addrinfo_(res);
5421
0
    SPLAY_INSERT(evdns_tree, &cache->base->cache_root, cache);
5422
0
    evtimer_assign(&cache->ev_timeout, dns_base->event_base, evdns_ttl_expired, cache);
5423
0
    timerclear(&tv);
5424
0
    tv.tv_sec = ttl;
5425
0
    evtimer_add(&cache->ev_timeout, &tv);
5426
0
  }
5427
0
  EVDNS_UNLOCK(dns_base);
5428
0
}
5429
5430
int
5431
evdns_cache_lookup(struct evdns_base *base,
5432
    const char *nodename, struct evutil_addrinfo *hints, ev_uint16_t port,
5433
    struct evutil_addrinfo **res)
5434
0
{
5435
0
  int n_found = 0;
5436
0
  struct evdns_cache *cache;
5437
0
  struct evdns_cache find;
5438
0
  struct evutil_addrinfo *ai = NULL;
5439
0
  int want_cname = hints->ai_flags & EVUTIL_AI_CANONNAME;
5440
0
  int f = hints->ai_family;
5441
5442
0
  log(EVDNS_LOG_DEBUG, "Looking in cache for %s", nodename);
5443
0
  EVDNS_LOCK(base);
5444
0
  find.name = (char *)nodename;
5445
0
  cache = SPLAY_FIND(evdns_tree, &base->cache_root, &find);
5446
0
  if (cache) {
5447
0
    struct evutil_addrinfo *e = cache->ai;
5448
0
    log(EVDNS_LOG_DEBUG, "Found cache for %s", cache->name);
5449
0
    for (; e; e = e->ai_next) {
5450
0
      struct evutil_addrinfo *ai_new;
5451
      // an existing record might not have the canonname
5452
0
      if (want_cname && e->ai_canonname == NULL)
5453
0
        continue;
5454
0
      ++n_found;
5455
0
      if ((e->ai_addr->sa_family == AF_INET && f == PF_INET6) ||
5456
0
        (e->ai_addr->sa_family == AF_INET6 && f == PF_INET))
5457
0
        continue;
5458
0
      ai_new = evutil_new_addrinfo_(e->ai_addr, e->ai_addrlen, hints);
5459
0
      if (!ai_new) {
5460
0
        n_found = 0;
5461
0
        goto out;
5462
0
      }
5463
0
      if (want_cname) {
5464
0
        ai_new->ai_canonname = mm_strdup(e->ai_canonname);
5465
0
      }
5466
0
      sockaddr_setport(ai_new->ai_addr, port);
5467
0
      ai = evutil_addrinfo_append_(ai, ai_new);
5468
0
    }
5469
0
  }
5470
0
  EVDNS_UNLOCK(base);
5471
0
out:
5472
0
  if (n_found) {
5473
0
    if (!ai) {
5474
0
      return EVUTIL_EAI_ADDRFAMILY;
5475
0
    }
5476
0
    *res = ai;
5477
0
    return 0;
5478
0
  } else {
5479
0
    if (ai)
5480
0
      evutil_freeaddrinfo(ai);
5481
0
    return -1;
5482
0
  }
5483
0
}
5484
5485
static void
5486
evdns_getaddrinfo_gotresolve(int result, char type, int count,
5487
    int ttl, void *addresses, void *arg)
5488
0
{
5489
0
  int i;
5490
0
  struct getaddrinfo_subrequest *req = arg;
5491
0
  struct getaddrinfo_subrequest *other_req;
5492
0
  struct evdns_getaddrinfo_request *data;
5493
5494
0
  struct evutil_addrinfo *res;
5495
5496
0
  struct sockaddr_in sin;
5497
0
  struct sockaddr_in6 sin6;
5498
0
  struct sockaddr *sa;
5499
0
  int socklen, addrlen;
5500
0
  void *addrp;
5501
0
  int err;
5502
0
  int user_canceled;
5503
5504
0
  EVUTIL_ASSERT(req->type == DNS_IPv4_A || req->type == DNS_IPv6_AAAA);
5505
0
  if (req->type == DNS_IPv4_A) {
5506
0
    data = EVUTIL_UPCAST(req, struct evdns_getaddrinfo_request, ipv4_request);
5507
0
    other_req = &data->ipv6_request;
5508
0
  } else {
5509
0
    data = EVUTIL_UPCAST(req, struct evdns_getaddrinfo_request, ipv6_request);
5510
0
    other_req = &data->ipv4_request;
5511
0
  }
5512
5513
  /** Called from evdns_base_free() with @fail_requests == 1 */
5514
0
  if (result != DNS_ERR_SHUTDOWN) {
5515
0
    EVDNS_LOCK(data->evdns_base);
5516
0
    if (evdns_result_is_answer(result)) {
5517
0
      if (req->type == DNS_IPv4_A)
5518
0
        ++data->evdns_base->getaddrinfo_ipv4_answered;
5519
0
      else
5520
0
        ++data->evdns_base->getaddrinfo_ipv6_answered;
5521
0
    }
5522
0
    user_canceled = data->user_canceled;
5523
0
    if (other_req->r == NULL)
5524
0
      data->request_done = 1;
5525
0
    EVDNS_UNLOCK(data->evdns_base);
5526
0
  } else {
5527
0
    data->evdns_base = NULL;
5528
0
    user_canceled = data->user_canceled;
5529
0
  }
5530
5531
0
  req->r = NULL;
5532
5533
0
  if (result == DNS_ERR_CANCEL && ! user_canceled) {
5534
    /* Internal cancel request from timeout or internal error.
5535
     * we already answered the user. */
5536
0
    if (other_req->r == NULL)
5537
0
      free_getaddrinfo_request(data);
5538
0
    return;
5539
0
  }
5540
5541
0
  if (data->user_cb == NULL) {
5542
    /* We already answered.  XXXX This shouldn't be needed; see
5543
     * comments in evdns_getaddrinfo_timeout_cb */
5544
0
    free_getaddrinfo_request(data);
5545
0
    return;
5546
0
  }
5547
5548
0
  if (result == DNS_ERR_NONE) {
5549
0
    if (count == 0)
5550
0
      err = EVUTIL_EAI_NODATA;
5551
0
    else
5552
0
      err = 0;
5553
0
  } else {
5554
0
    err = evdns_err_to_getaddrinfo_err(result);
5555
0
  }
5556
5557
0
  if (err) {
5558
    /* Looks like we got an error. */
5559
0
    if (other_req->r) {
5560
      /* The other request is still working; maybe it will
5561
       * succeed. */
5562
      /* XXXX handle failure from set_timeout */
5563
0
      if (result != DNS_ERR_SHUTDOWN) {
5564
0
        evdns_getaddrinfo_set_timeout(data->evdns_base, data);
5565
0
      }
5566
0
      data->pending_error = err;
5567
0
      return;
5568
0
    }
5569
5570
0
    if (user_canceled) {
5571
0
      data->user_cb(EVUTIL_EAI_CANCEL, NULL, data->user_data);
5572
0
    } else if (data->pending_result) {
5573
      /* If we have an answer waiting, and we weren't
5574
       * canceled, ignore this error. */
5575
0
      add_cname_to_reply(data, data->pending_result);
5576
0
      if (data->evdns_base && !data->evdns_base->disable_cache) {
5577
0
        evdns_cache_write(data->evdns_base, data->nodename, data->pending_result, data->pending_result_ttl);
5578
0
      }
5579
0
      data->user_cb(0, data->pending_result, data->user_data);
5580
0
      data->pending_result = NULL;
5581
0
    } else {
5582
0
      if (data->pending_error)
5583
0
        err = getaddrinfo_merge_err(err,
5584
0
            data->pending_error);
5585
0
      data->user_cb(err, NULL, data->user_data);
5586
0
    }
5587
0
    free_getaddrinfo_request(data);
5588
0
    return;
5589
0
  } else if (user_canceled) {
5590
0
    if (other_req->r) {
5591
      /* The other request is still working; let it hit this
5592
       * callback with EVUTIL_EAI_CANCEL callback and report
5593
       * the failure. */
5594
0
      return;
5595
0
    }
5596
0
    data->user_cb(EVUTIL_EAI_CANCEL, NULL, data->user_data);
5597
0
    free_getaddrinfo_request(data);
5598
0
    return;
5599
0
  }
5600
5601
  /* Looks like we got some answers. We should turn them into addrinfos
5602
   * and then either queue those or return them all. */
5603
0
  EVUTIL_ASSERT(type == DNS_IPv4_A || type == DNS_IPv6_AAAA);
5604
5605
0
  if (type == DNS_IPv4_A) {
5606
0
    memset(&sin, 0, sizeof(sin));
5607
0
    sin.sin_family = AF_INET;
5608
0
    sin.sin_port = htons(data->port);
5609
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
5610
    sin.sin_len = sizeof(sin);
5611
#endif
5612
5613
0
    sa = (struct sockaddr *)&sin;
5614
0
    socklen = sizeof(sin);
5615
0
    addrlen = 4;
5616
0
    addrp = &sin.sin_addr.s_addr;
5617
0
  } else {
5618
0
    memset(&sin6, 0, sizeof(sin6));
5619
0
    sin6.sin6_family = AF_INET6;
5620
0
    sin6.sin6_port = htons(data->port);
5621
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
5622
    sin6.sin6_len = sizeof(sin6);
5623
#endif
5624
5625
0
    sa = (struct sockaddr *)&sin6;
5626
0
    socklen = sizeof(sin6);
5627
0
    addrlen = 16;
5628
0
    addrp = &sin6.sin6_addr.s6_addr;
5629
0
  }
5630
5631
0
  res = NULL;
5632
0
  for (i=0; i < count; ++i) {
5633
0
    struct evutil_addrinfo *ai;
5634
0
    memcpy(addrp, ((char*)addresses)+i*addrlen, addrlen);
5635
0
    ai = evutil_new_addrinfo_(sa, socklen, &data->hints);
5636
0
    if (!ai) {
5637
0
      if (other_req->r) {
5638
0
        evdns_cancel_request(NULL, other_req->r);
5639
0
      }
5640
0
      data->user_cb(EVUTIL_EAI_MEMORY, NULL, data->user_data);
5641
0
      if (res)
5642
0
        evutil_freeaddrinfo(res);
5643
5644
0
      if (other_req->r == NULL)
5645
0
        free_getaddrinfo_request(data);
5646
0
      return;
5647
0
    }
5648
0
    res = evutil_addrinfo_append_(res, ai);
5649
0
  }
5650
5651
0
  if (other_req->r) {
5652
    /* The other request is still in progress; wait for it */
5653
    /* XXXX handle failure from set_timeout */
5654
0
    evdns_getaddrinfo_set_timeout(data->evdns_base, data);
5655
0
    data->pending_result = res;
5656
0
    data->pending_result_ttl = ttl;
5657
0
    return;
5658
0
  } else {
5659
    /* The other request is done or never started; append its
5660
     * results (if any) and return them. */
5661
0
    int res_ttl = ttl;
5662
0
    if (data->pending_result) {
5663
0
      if (req->type == DNS_IPv4_A)
5664
0
        res = evutil_addrinfo_append_(res,
5665
0
            data->pending_result);
5666
0
      else
5667
0
        res = evutil_addrinfo_append_(
5668
0
            data->pending_result, res);
5669
0
      res_ttl = data->pending_result_ttl;
5670
0
      data->pending_result = NULL;
5671
0
    }
5672
5673
    /* Call the user callback. */
5674
0
    add_cname_to_reply(data, res);
5675
0
    if (data->evdns_base && !data->evdns_base->disable_cache) {
5676
0
      evdns_cache_write(data->evdns_base, data->nodename, res, res_ttl);
5677
0
    }
5678
0
    data->user_cb(0, res, data->user_data);
5679
5680
    /* Free data. */
5681
0
    free_getaddrinfo_request(data);
5682
0
  }
5683
0
}
5684
5685
static struct hosts_entry *
5686
find_hosts_entry(struct evdns_base *base, const char *hostname,
5687
    struct hosts_entry *find_after)
5688
0
{
5689
0
  struct hosts_entry *e;
5690
5691
0
  if (find_after)
5692
0
    e = TAILQ_NEXT(find_after, next);
5693
0
  else
5694
0
    e = TAILQ_FIRST(&base->hostsdb);
5695
5696
0
  for (; e; e = TAILQ_NEXT(e, next)) {
5697
0
    if (!evutil_ascii_strcasecmp(e->hostname, hostname))
5698
0
      return e;
5699
0
  }
5700
0
  return NULL;
5701
0
}
5702
5703
static int
5704
evdns_getaddrinfo_fromhosts(struct evdns_base *base,
5705
    const char *nodename, struct evutil_addrinfo *hints, ev_uint16_t port,
5706
    struct evutil_addrinfo **res)
5707
0
{
5708
0
  int n_found = 0;
5709
0
  struct hosts_entry *e;
5710
0
  struct evutil_addrinfo *ai = NULL;
5711
0
  int f = hints->ai_family;
5712
5713
0
  EVDNS_LOCK(base);
5714
0
  for (e = find_hosts_entry(base, nodename, NULL); e;
5715
0
      e = find_hosts_entry(base, nodename, e)) {
5716
0
    struct evutil_addrinfo *ai_new;
5717
0
    ++n_found;
5718
0
    if ((e->addr.sa.sa_family == AF_INET && f == PF_INET6) ||
5719
0
        (e->addr.sa.sa_family == AF_INET6 && f == PF_INET))
5720
0
      continue;
5721
0
    ai_new = evutil_new_addrinfo_(&e->addr.sa, e->addrlen, hints);
5722
0
    if (!ai_new) {
5723
0
      n_found = 0;
5724
0
      goto out;
5725
0
    }
5726
0
    sockaddr_setport(ai_new->ai_addr, port);
5727
0
    ai = evutil_addrinfo_append_(ai, ai_new);
5728
0
  }
5729
0
  EVDNS_UNLOCK(base);
5730
0
out:
5731
0
  if (n_found) {
5732
0
    if (!ai) {
5733
0
      return EVUTIL_EAI_ADDRFAMILY;
5734
0
    }
5735
0
    *res = ai;
5736
0
    return 0;
5737
0
  } else {
5738
0
    if (ai)
5739
0
      evutil_freeaddrinfo(ai);
5740
0
    return -1;
5741
0
  }
5742
0
}
5743
5744
struct evdns_getaddrinfo_request *
5745
evdns_getaddrinfo(struct evdns_base *dns_base,
5746
    const char *nodename, const char *servname,
5747
    const struct evutil_addrinfo *hints_in,
5748
    evdns_getaddrinfo_cb cb, void *arg)
5749
0
{
5750
0
  struct evdns_getaddrinfo_request *data;
5751
0
  struct evutil_addrinfo hints;
5752
0
  struct evutil_addrinfo *res = NULL;
5753
0
  int err;
5754
0
  int port = 0;
5755
0
  int want_cname = 0;
5756
0
  int started = 0;
5757
5758
0
  if (!dns_base) {
5759
0
    dns_base = current_base;
5760
0
    if (!dns_base) {
5761
0
      log(EVDNS_LOG_WARN,
5762
0
          "Call to getaddrinfo_async with no "
5763
0
          "evdns_base configured.");
5764
0
      cb(EVUTIL_EAI_FAIL, NULL, arg); /* ??? better error? */
5765
0
      return NULL;
5766
0
    }
5767
0
  }
5768
5769
  /* If we _must_ answer this immediately, do so. */
5770
0
  if ((hints_in && (hints_in->ai_flags & EVUTIL_AI_NUMERICHOST))) {
5771
0
    res = NULL;
5772
0
    err = evutil_getaddrinfo(nodename, servname, hints_in, &res);
5773
0
    cb(err, res, arg);
5774
0
    return NULL;
5775
0
  }
5776
5777
0
  if (hints_in) {
5778
0
    memcpy(&hints, hints_in, sizeof(hints));
5779
0
  } else {
5780
0
    memset(&hints, 0, sizeof(hints));
5781
0
    hints.ai_family = PF_UNSPEC;
5782
0
  }
5783
5784
0
  evutil_adjust_hints_for_addrconfig_(&hints);
5785
5786
  /* Now try to see if we _can_ answer immediately. */
5787
  /* (It would be nice to do this by calling getaddrinfo directly, with
5788
   * AI_NUMERICHOST, on platforms that have it, but we can't: there isn't
5789
   * a reliable way to distinguish the "that wasn't a numeric host!" case
5790
   * from any other EAI_NONAME cases.) */
5791
0
  err = evutil_getaddrinfo_common_(nodename, servname, &hints, &res, &port);
5792
0
  if (err != EVUTIL_EAI_NEED_RESOLVE) {
5793
0
    cb(err, res, arg);
5794
0
    return NULL;
5795
0
  }
5796
5797
  /* If there is an entry in the hosts file, we should give it now. */
5798
0
  err = evdns_getaddrinfo_fromhosts(dns_base, nodename, &hints, port, &res);
5799
0
  if (!err || err == EVUTIL_EAI_ADDRFAMILY) {
5800
0
    cb(err, res, arg);
5801
0
    return NULL;
5802
0
  }
5803
5804
  /* See if we have it in the cache */
5805
0
  if (!dns_base->disable_cache) {
5806
0
    err = evdns_cache_lookup(dns_base, nodename, &hints, port, &res);
5807
0
    if (!err || err == EVUTIL_EAI_ADDRFAMILY) {
5808
0
      cb(err, res, arg);
5809
0
      return NULL;
5810
0
    }
5811
0
  }
5812
5813
  /* Okay, things are serious now. We're going to need to actually
5814
   * launch a request.
5815
   */
5816
0
  data = mm_calloc(1,sizeof(struct evdns_getaddrinfo_request));
5817
0
  if (!data) {
5818
0
    cb(EVUTIL_EAI_MEMORY, NULL, arg);
5819
0
    return NULL;
5820
0
  }
5821
5822
0
  memcpy(&data->hints, &hints, sizeof(data->hints));
5823
0
  data->port = (ev_uint16_t)port;
5824
0
  data->ipv4_request.type = DNS_IPv4_A;
5825
0
  data->ipv6_request.type = DNS_IPv6_AAAA;
5826
0
  data->user_cb = cb;
5827
0
  data->user_data = arg;
5828
0
  data->evdns_base = dns_base;
5829
0
  data->nodename = mm_strdup(nodename);
5830
5831
0
  want_cname = (hints.ai_flags & EVUTIL_AI_CANONNAME);
5832
5833
  /* If we are asked for a PF_UNSPEC address, we launch two requests in
5834
   * parallel: one for an A address and one for an AAAA address.  We
5835
   * can't send just one request, since many servers only answer one
5836
   * question per DNS request.
5837
   *
5838
   * Once we have the answer to one request, we allow for a short
5839
   * timeout before we report it, to see if the other one arrives.  If
5840
   * they both show up in time, then we report both the answers.
5841
   *
5842
   * If too many addresses of one type time out or fail, we should stop
5843
   * launching those requests. (XXX we don't do that yet.)
5844
   */
5845
5846
0
  EVDNS_LOCK(dns_base);
5847
5848
0
  if (hints.ai_family != PF_INET6) {
5849
0
    log(EVDNS_LOG_DEBUG, "Sending request for %s on ipv4 as %p",
5850
0
        nodename, (void *)&data->ipv4_request);
5851
5852
0
    data->ipv4_request.r = evdns_base_resolve_ipv4(dns_base,
5853
0
        nodename, 0, evdns_getaddrinfo_gotresolve,
5854
0
        &data->ipv4_request);
5855
0
    if (want_cname && data->ipv4_request.r)
5856
0
      data->ipv4_request.r->current_req->put_cname_in_ptr =
5857
0
          &data->cname_result;
5858
0
  }
5859
0
  if (hints.ai_family != PF_INET) {
5860
0
    log(EVDNS_LOG_DEBUG, "Sending request for %s on ipv6 as %p",
5861
0
        nodename, (void *)&data->ipv6_request);
5862
5863
0
    data->ipv6_request.r = evdns_base_resolve_ipv6(dns_base,
5864
0
        nodename, 0, evdns_getaddrinfo_gotresolve,
5865
0
        &data->ipv6_request);
5866
0
    if (want_cname && data->ipv6_request.r)
5867
0
      data->ipv6_request.r->current_req->put_cname_in_ptr =
5868
0
          &data->cname_result;
5869
0
  }
5870
5871
0
  evtimer_assign(&data->timeout, dns_base->event_base,
5872
0
      evdns_getaddrinfo_timeout_cb, data);
5873
5874
0
  started = (data->ipv4_request.r || data->ipv6_request.r);
5875
5876
0
  EVDNS_UNLOCK(dns_base);
5877
5878
0
  if (started) {
5879
0
    return data;
5880
0
  } else {
5881
0
    free_getaddrinfo_request(data);
5882
0
    cb(EVUTIL_EAI_FAIL, NULL, arg);
5883
0
    return NULL;
5884
0
  }
5885
0
}
5886
5887
void
5888
evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *data)
5889
0
{
5890
0
  EVDNS_LOCK(data->evdns_base);
5891
0
  if (data->request_done) {
5892
0
    EVDNS_UNLOCK(data->evdns_base);
5893
0
    return;
5894
0
  }
5895
0
  event_del(&data->timeout);
5896
0
  data->user_canceled = 1;
5897
0
  if (data->ipv4_request.r)
5898
0
    evdns_cancel_request(data->evdns_base, data->ipv4_request.r);
5899
0
  if (data->ipv6_request.r)
5900
0
    evdns_cancel_request(data->evdns_base, data->ipv6_request.r);
5901
0
  EVDNS_UNLOCK(data->evdns_base);
5902
0
}