Coverage Report

Created: 2026-12-01 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/dispatch.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
/*
17
 * FIXME: Might need dns_dispatch_shuttingdown()
18
 */
19
20
#include <inttypes.h>
21
#include <stdbool.h>
22
#include <stdlib.h>
23
#include <sys/types.h>
24
#include <unistd.h>
25
26
#include <isc/async.h>
27
#include <isc/hash.h>
28
#include <isc/hashmap.h>
29
#include <isc/log.h>
30
#include <isc/loop.h>
31
#include <isc/mem.h>
32
#include <isc/mutex.h>
33
#include <isc/net.h>
34
#include <isc/netmgr.h>
35
#include <isc/portset.h>
36
#include <isc/random.h>
37
#include <isc/stats.h>
38
#include <isc/string.h>
39
#include <isc/tid.h>
40
#include <isc/time.h>
41
#include <isc/tls.h>
42
#include <isc/urcu.h>
43
#include <isc/util.h>
44
45
#include <dns/acl.h>
46
#include <dns/dispatch.h>
47
#include <dns/message.h>
48
#include <dns/stats.h>
49
#include <dns/transport.h>
50
#include <dns/types.h>
51
52
/*
53
 * Maximum number of queries to pipeline on a single shared TCP dispatch.
54
 * Once reached, the dispatch is removed from the hash table so new queries
55
 * get a fresh connection.  Can be overridden via 'named -T tcppipelining=N'.
56
 */
57
size_t dns_dispatch_tcppipelining = 256;
58
59
typedef ISC_LIST(dns_dispentry_t) dns_displist_t;
60
61
struct dns_dispatchmgr {
62
  /* Unlocked. */
63
  unsigned int magic;
64
  isc_refcount_t references;
65
  isc_mem_t *mctx;
66
  dns_acl_t *blackhole;
67
  isc_stats_t *stats;
68
69
  unsigned int reuse_timeout;
70
71
  uint32_t nloops;
72
73
  struct cds_lfht **tcps;
74
75
  struct cds_lfht *qids;
76
77
  in_port_t *v4ports;    /*%< available ports for IPv4 */
78
  unsigned int nv4ports; /*%< # of available ports for IPv4 */
79
  in_port_t *v6ports;    /*%< available ports for IPv6 */
80
  unsigned int nv6ports; /*%< # of available ports for IPv6 */
81
};
82
83
typedef enum {
84
  DNS_DISPATCHSTATE_NONE = 0UL,
85
  DNS_DISPATCHSTATE_CONNECTING,
86
  DNS_DISPATCHSTATE_CONNECTED,
87
  DNS_DISPATCHSTATE_CANCELED,
88
} dns_dispatchstate_t;
89
90
struct dns_dispentry {
91
  unsigned int magic;
92
  isc_refcount_t references;
93
  isc_mem_t *mctx;
94
  dns_dispatch_t *disp;
95
  isc_loop_t *loop;
96
  isc_nmhandle_t *handle; /*%< netmgr handle for UDP connection */
97
  dns_dispatchstate_t state;
98
  dns_transport_t *transport;
99
  isc_tlsctx_cache_t *tlsctx_cache;
100
  unsigned int retries;
101
  unsigned int connect_timeout;
102
  unsigned int timeout;
103
  isc_time_t start;
104
  isc_sockaddr_t local;
105
  isc_sockaddr_t peer;
106
  in_port_t port;
107
  dns_messageid_t id;
108
  dispatch_cb_t connected;
109
  dispatch_cb_t sent;
110
  dispatch_cb_t response;
111
  void *arg;
112
  bool reading;
113
  isc_result_t result;
114
  ISC_LINK(dns_dispentry_t) alink;
115
  ISC_LINK(dns_dispentry_t) plink;
116
  ISC_LINK(dns_dispentry_t) rlink;
117
118
  struct cds_lfht_node ht_node;
119
  struct rcu_head rcu_head;
120
};
121
122
struct dns_dispatch {
123
  /* Unlocked. */
124
  unsigned int magic; /*%< magic */
125
  isc_tid_t tid;
126
  isc_socktype_t socktype;
127
  isc_refcount_t references;
128
  isc_mem_t *mctx;
129
  dns_dispatchmgr_t *mgr;     /*%< dispatch manager */
130
  isc_nmhandle_t *handle;     /*%< netmgr handle for TCP connection */
131
  isc_sockaddr_t local;     /*%< local address */
132
  isc_sockaddr_t peer;      /*%< peer address (TCP) */
133
  dns_transport_t *transport; /*%< TCP transport parameters */
134
135
  dns_dispatchopt_t options;
136
  dns_dispatchstate_t state;
137
  dns_dispatchtype_t disptype;
138
139
  dns_messageid_t nextid; /*%< next sequential QID for TCP */
140
141
  bool reading;
142
143
  dns_displist_t pending;
144
  dns_displist_t active;
145
146
  uint_fast32_t requests; /*%< how many requests we have */
147
148
  unsigned int timedout; /*%< how many timeouts/cancels we had, so we can
149
            ignore this many unexpected answers. */
150
151
  struct cds_lfht_node ht_node;
152
  struct rcu_head rcu_head;
153
};
154
155
0
#define RESPONSE_MAGIC    ISC_MAGIC('D', 'r', 's', 'p')
156
#define VALID_RESPONSE(e) ISC_MAGIC_VALID((e), RESPONSE_MAGIC)
157
158
0
#define DISPATCH_MAGIC    ISC_MAGIC('D', 'i', 's', 'p')
159
#define VALID_DISPATCH(e) ISC_MAGIC_VALID((e), DISPATCH_MAGIC)
160
161
0
#define DNS_DISPATCHMGR_MAGIC ISC_MAGIC('D', 'M', 'g', 'r')
162
#define VALID_DISPATCHMGR(e)  ISC_MAGIC_VALID((e), DNS_DISPATCHMGR_MAGIC)
163
164
#if DNS_DISPATCH_TRACE
165
#define dns_dispentry_ref(ptr) \
166
  dns_dispentry__ref(ptr, __func__, __FILE__, __LINE__)
167
#define dns_dispentry_unref(ptr) \
168
  dns_dispentry__unref(ptr, __func__, __FILE__, __LINE__)
169
#define dns_dispentry_attach(ptr, ptrp) \
170
  dns_dispentry__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
171
#define dns_dispentry_detach(ptrp) \
172
  dns_dispentry__detach(ptrp, __func__, __FILE__, __LINE__)
173
ISC_REFCOUNT_TRACE_DECL(dns_dispentry);
174
#else
175
ISC_REFCOUNT_DECL(dns_dispentry);
176
#endif
177
178
/*
179
 * The number of attempts to find unique <addr, port, query_id> combination
180
 */
181
0
#define QID_MAX_TRIES 64
182
183
/*
184
 * Initial and minimum QID table sizes.
185
 */
186
0
#define QIDS_INIT_SIZE (1 << 4) /* Must be power of 2 */
187
0
#define QIDS_MIN_SIZE  (1 << 4) /* Must be power of 2 */
188
189
/*
190
 * Statics.
191
 */
192
static void
193
dispatchmgr_destroy(dns_dispatchmgr_t *mgr);
194
195
static void
196
udp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
197
   void *arg);
198
static void
199
tcp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
200
   void *arg);
201
static void
202
dispentry_cancel(dns_dispentry_t *resp, isc_result_t result);
203
static isc_result_t
204
dispatch_createudp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr,
205
       isc_tid_t tid, dns_dispatch_t **dispp);
206
static void
207
udp_startrecv(isc_nmhandle_t *handle, dns_dispentry_t *resp);
208
static void
209
udp_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp);
210
static void
211
tcp_startrecv(dns_dispatch_t *disp, dns_dispentry_t *resp);
212
static void
213
tcp_dispatch_getnext(dns_dispatch_t *disp, dns_dispentry_t *resp,
214
         int64_t timeout);
215
static void
216
udp_dispatch_getnext(dns_dispentry_t *resp, int64_t timeout);
217
218
static const char *
219
0
socktype2str(dns_dispentry_t *resp) {
220
0
  dns_transport_type_t transport_type = DNS_TRANSPORT_UDP;
221
0
  dns_dispatch_t *disp = resp->disp;
222
223
0
  if (disp->socktype == isc_socktype_tcp) {
224
0
    if (resp->transport != NULL) {
225
0
      transport_type =
226
0
        dns_transport_get_type(resp->transport);
227
0
    } else {
228
0
      transport_type = DNS_TRANSPORT_TCP;
229
0
    }
230
0
  }
231
232
0
  switch (transport_type) {
233
0
  case DNS_TRANSPORT_UDP:
234
0
    return "UDP";
235
0
  case DNS_TRANSPORT_TCP:
236
0
    return "TCP";
237
0
  case DNS_TRANSPORT_TLS:
238
0
    return "TLS";
239
0
  case DNS_TRANSPORT_HTTP:
240
0
    return "HTTP";
241
0
  default:
242
0
    return "<unexpected>";
243
0
  }
244
0
}
245
246
static const char *
247
0
state2str(dns_dispatchstate_t state) {
248
0
  switch (state) {
249
0
  case DNS_DISPATCHSTATE_NONE:
250
0
    return "none";
251
0
  case DNS_DISPATCHSTATE_CONNECTING:
252
0
    return "connecting";
253
0
  case DNS_DISPATCHSTATE_CONNECTED:
254
0
    return "connected";
255
0
  case DNS_DISPATCHSTATE_CANCELED:
256
0
    return "canceled";
257
0
  default:
258
0
    return "<unexpected>";
259
0
  }
260
0
}
261
262
static void
263
mgr_log(dns_dispatchmgr_t *mgr, int level, const char *fmt, ...)
264
  ISC_FORMAT_PRINTF(3, 4);
265
266
static void
267
0
mgr_log(dns_dispatchmgr_t *mgr, int level, const char *fmt, ...) {
268
0
  char msgbuf[2048];
269
0
  va_list ap;
270
271
0
  if (!isc_log_wouldlog(level)) {
272
0
    return;
273
0
  }
274
275
0
  va_start(ap, fmt);
276
0
  vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
277
0
  va_end(ap);
278
279
0
  isc_log_write(DNS_LOGCATEGORY_DISPATCH, DNS_LOGMODULE_DISPATCH, level,
280
0
          "dispatchmgr %p: %s", mgr, msgbuf);
281
0
}
282
283
static void
284
0
inc_stats(dns_dispatchmgr_t *mgr, isc_statscounter_t counter) {
285
0
  if (mgr->stats != NULL) {
286
0
    isc_stats_increment(mgr->stats, counter);
287
0
  }
288
0
}
289
290
static void
291
0
dec_stats(dns_dispatchmgr_t *mgr, isc_statscounter_t counter) {
292
0
  if (mgr->stats != NULL) {
293
0
    isc_stats_decrement(mgr->stats, counter);
294
0
  }
295
0
}
296
297
static void
298
dispatch_log(dns_dispatch_t *disp, int level, const char *fmt, ...)
299
  ISC_FORMAT_PRINTF(3, 4);
300
301
static void
302
0
dispatch_log(dns_dispatch_t *disp, int level, const char *fmt, ...) {
303
0
  char msgbuf[2048];
304
0
  va_list ap;
305
0
  int r;
306
307
0
  if (!isc_log_wouldlog(level)) {
308
0
    return;
309
0
  }
310
311
0
  va_start(ap, fmt);
312
0
  r = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
313
0
  if (r < 0) {
314
0
    msgbuf[0] = '\0';
315
0
  } else if ((unsigned int)r >= sizeof(msgbuf)) {
316
    /* Truncated */
317
0
    msgbuf[sizeof(msgbuf) - 1] = '\0';
318
0
  }
319
0
  va_end(ap);
320
321
0
  isc_log_write(DNS_LOGCATEGORY_DISPATCH, DNS_LOGMODULE_DISPATCH, level,
322
0
          "dispatch %p: %s", disp, msgbuf);
323
0
}
324
325
static void
326
dispentry_log(dns_dispentry_t *resp, int level, const char *fmt, ...)
327
  ISC_FORMAT_PRINTF(3, 4);
328
329
static void
330
0
dispentry_log(dns_dispentry_t *resp, int level, const char *fmt, ...) {
331
0
  char msgbuf[2048];
332
0
  va_list ap;
333
0
  int r;
334
335
0
  if (!isc_log_wouldlog(level)) {
336
0
    return;
337
0
  }
338
339
0
  va_start(ap, fmt);
340
0
  r = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
341
0
  if (r < 0) {
342
0
    msgbuf[0] = '\0';
343
0
  } else if ((unsigned int)r >= sizeof(msgbuf)) {
344
    /* Truncated */
345
0
    msgbuf[sizeof(msgbuf) - 1] = '\0';
346
0
  }
347
0
  va_end(ap);
348
349
0
  dispatch_log(resp->disp, level, "%s response %p: %s",
350
0
         socktype2str(resp), resp, msgbuf);
351
0
}
352
353
/*%
354
 * Choose a random port number for a dispatch entry.
355
 */
356
static isc_result_t
357
setup_socket(dns_dispatch_t *disp, dns_dispentry_t *resp,
358
0
       const isc_sockaddr_t *dest, in_port_t *portp) {
359
0
  dns_dispatchmgr_t *mgr = disp->mgr;
360
0
  unsigned int nports;
361
0
  in_port_t *ports = NULL;
362
0
  in_port_t port = *portp;
363
364
0
  if (resp->retries++ > 5) {
365
0
    return ISC_R_FAILURE;
366
0
  }
367
368
0
  if (isc_sockaddr_pf(&disp->local) == AF_INET) {
369
0
    nports = mgr->nv4ports;
370
0
    ports = mgr->v4ports;
371
0
  } else {
372
0
    nports = mgr->nv6ports;
373
0
    ports = mgr->v6ports;
374
0
  }
375
0
  if (nports == 0) {
376
0
    return ISC_R_ADDRNOTAVAIL;
377
0
  }
378
379
0
  resp->local = disp->local;
380
0
  resp->peer = *dest;
381
382
0
  if (port == 0) {
383
0
    port = ports[isc_random_uniform(nports)];
384
0
    isc_sockaddr_setport(&resp->local, port);
385
0
    *portp = port;
386
0
  }
387
0
  resp->port = port;
388
389
0
  return ISC_R_SUCCESS;
390
0
}
391
392
static uint32_t
393
0
qid_hash(const dns_dispentry_t *dispentry) {
394
0
  isc_hash32_t hash;
395
396
0
  isc_hash32_init(&hash);
397
398
0
  isc_sockaddr_hash_ex(&hash, &dispentry->peer, true);
399
0
  isc_hash32_hash(&hash, &dispentry->id, sizeof(dispentry->id), true);
400
0
  isc_hash32_hash(&hash, &dispentry->port, sizeof(dispentry->port), true);
401
402
0
  return isc_hash32_finalize(&hash);
403
0
}
404
405
static int
406
0
qid_match(struct cds_lfht_node *node, const void *key0) {
407
0
  const dns_dispentry_t *dispentry =
408
0
    caa_container_of(node, dns_dispentry_t, ht_node);
409
0
  const dns_dispentry_t *key = key0;
410
411
0
  return dispentry->id == key->id && dispentry->port == key->port &&
412
0
         isc_sockaddr_equal(&dispentry->peer, &key->peer);
413
0
}
414
415
static void
416
0
dispentry_destroy_rcu(struct rcu_head *rcu_head) {
417
0
  dns_dispentry_t *resp = caa_container_of(rcu_head, dns_dispentry_t,
418
0
             rcu_head);
419
0
  isc_mem_putanddetach(&resp->mctx, resp, sizeof(*resp));
420
0
}
421
422
static void
423
0
dispentry_destroy(dns_dispentry_t *resp) {
424
0
  dns_dispatch_t *disp = resp->disp;
425
426
  /*
427
   * We need to call this from here in case there's an external event that
428
   * shuts down our dispatch (like ISC_R_SHUTTINGDOWN).
429
   */
430
0
  dispentry_cancel(resp, ISC_R_CANCELED);
431
432
0
  INSIST(disp->requests > 0);
433
0
  disp->requests--;
434
435
0
  resp->magic = 0;
436
437
0
  INSIST(!ISC_LINK_LINKED(resp, plink));
438
0
  INSIST(!ISC_LINK_LINKED(resp, alink));
439
0
  INSIST(!ISC_LINK_LINKED(resp, rlink));
440
441
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "destroying");
442
443
0
  if (resp->handle != NULL) {
444
0
    dispentry_log(resp, ISC_LOG_DEBUG(90),
445
0
            "detaching handle %p from %p", resp->handle,
446
0
            &resp->handle);
447
0
    isc_nmhandle_detach(&resp->handle);
448
0
  }
449
450
0
  if (resp->tlsctx_cache != NULL) {
451
0
    isc_tlsctx_cache_detach(&resp->tlsctx_cache);
452
0
  }
453
454
0
  if (resp->transport != NULL) {
455
0
    dns_transport_detach(&resp->transport);
456
0
  }
457
458
0
  dns_dispatch_detach(&disp); /* DISPATCH001 */
459
460
0
  call_rcu(&resp->rcu_head, dispentry_destroy_rcu);
461
0
}
462
463
#if DNS_DISPATCH_TRACE
464
ISC_REFCOUNT_TRACE_IMPL(dns_dispentry, dispentry_destroy);
465
#else
466
0
ISC_REFCOUNT_IMPL(dns_dispentry, dispentry_destroy);
Unexecuted instantiation: dns_dispentry_ref
Unexecuted instantiation: dns_dispentry_unref
Unexecuted instantiation: dns_dispentry_detach
467
0
#endif
468
0
469
0
/*
470
0
 * How long in milliseconds has it been since this dispentry
471
0
 * started reading?
472
0
 */
473
0
static unsigned int
474
0
dispentry_runtime(dns_dispentry_t *resp, const isc_time_t *now) {
475
0
  if (isc_time_isepoch(&resp->start)) {
476
0
    return 0;
477
0
  }
478
479
0
  return isc_time_microdiff(now, &resp->start) / 1000;
480
0
}
481
482
/*
483
 * General flow:
484
 *
485
 * If I/O result == CANCELED or error, free the buffer.
486
 *
487
 * If query, free the buffer, restart.
488
 *
489
 * If response:
490
 *  Allocate event, fill in details.
491
 *    If cannot allocate, free buffer, restart.
492
 *  find target.  If not found, free buffer, restart.
493
 *  if event queue is not empty, queue.  else, send.
494
 *  restart.
495
 */
496
static void
497
udp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
498
0
   void *arg) {
499
0
  dns_dispentry_t *resp = (dns_dispentry_t *)arg;
500
0
  dns_dispatch_t *disp = NULL;
501
0
  dns_messageid_t id;
502
0
  isc_result_t dres;
503
0
  isc_buffer_t source;
504
0
  unsigned int flags;
505
0
  isc_sockaddr_t peer;
506
0
  isc_netaddr_t netaddr;
507
0
  int match;
508
0
  int64_t timeout = 0;
509
0
  bool respond = true;
510
0
  isc_time_t now;
511
512
0
  REQUIRE(VALID_RESPONSE(resp));
513
0
  REQUIRE(VALID_DISPATCH(resp->disp));
514
515
0
  disp = resp->disp;
516
517
0
  REQUIRE(disp->tid == isc_tid());
518
0
  INSIST(resp->reading);
519
0
  resp->reading = false;
520
521
0
  if (resp->state == DNS_DISPATCHSTATE_CANCELED) {
522
    /*
523
     * Nobody is interested in the callback if the response
524
     * has been canceled already.  Detach from the response
525
     * and the handle.
526
     */
527
0
    respond = false;
528
0
    eresult = ISC_R_CANCELED;
529
0
  }
530
531
0
  dispentry_log(resp, ISC_LOG_DEBUG(90),
532
0
          "read callback:%s, requests %" PRIuFAST32,
533
0
          isc_result_totext(eresult), disp->requests);
534
535
0
  if (eresult != ISC_R_SUCCESS) {
536
    /*
537
     * This is most likely a network error on a connected
538
     * socket, a timeout, or the query has been canceled.
539
     * It makes no sense to check the address or parse the
540
     * packet, but we can return the error to the caller.
541
     */
542
0
    goto done;
543
0
  }
544
545
0
  peer = isc_nmhandle_peeraddr(handle);
546
0
  isc_netaddr_fromsockaddr(&netaddr, &peer);
547
548
  /*
549
   * If this is from a blackholed address, drop it.
550
   */
551
0
  if (disp->mgr->blackhole != NULL &&
552
0
      dns_acl_match(&netaddr, NULL, disp->mgr->blackhole, NULL, &match,
553
0
        NULL) == ISC_R_SUCCESS &&
554
0
      match > 0)
555
0
  {
556
0
    if (isc_log_wouldlog(ISC_LOG_DEBUG(10))) {
557
0
      char netaddrstr[ISC_NETADDR_FORMATSIZE];
558
0
      isc_netaddr_format(&netaddr, netaddrstr,
559
0
             sizeof(netaddrstr));
560
0
      dispentry_log(resp, ISC_LOG_DEBUG(10),
561
0
              "blackholed packet from %s", netaddrstr);
562
0
    }
563
0
    goto next;
564
0
  }
565
566
  /*
567
   * Peek into the buffer to see what we can see.
568
   */
569
0
  id = resp->id;
570
0
  isc_buffer_init(&source, region->base, region->length);
571
0
  isc_buffer_add(&source, region->length);
572
0
  dres = dns_message_peekheader(&source, &id, &flags);
573
0
  if (dres != ISC_R_SUCCESS) {
574
0
    char netaddrstr[ISC_NETADDR_FORMATSIZE];
575
0
    isc_netaddr_format(&netaddr, netaddrstr, sizeof(netaddrstr));
576
0
    dispentry_log(resp, ISC_LOG_DEBUG(10),
577
0
            "got garbage packet from %s", netaddrstr);
578
0
    goto next;
579
0
  }
580
581
0
  dispentry_log(resp, ISC_LOG_DEBUG(92),
582
0
          "got valid DNS message header, /QR %c, id %u",
583
0
          ((flags & DNS_MESSAGEFLAG_QR) != 0) ? '1' : '0', id);
584
585
  /*
586
   * Look at the message flags.  If it's a query, ignore it.
587
   */
588
0
  if ((flags & DNS_MESSAGEFLAG_QR) == 0) {
589
0
    goto next;
590
0
  }
591
592
  /*
593
   * The QID and the address must match the expected ones.  A
594
   * mismatch can happen during normal operation only when a stale
595
   * response from a previous query arrives late, which is rare in
596
   * practice; treat any mismatch as a possible spoofing attempt and
597
   * let the caller retry over TCP to prevent off-path spoofing.
598
   */
599
0
  if (resp->id != id || !isc_sockaddr_equal(&peer, &resp->peer)) {
600
0
    dispentry_log(resp, ISC_LOG_DEBUG(90),
601
0
            "response doesn't match");
602
0
    inc_stats(disp->mgr, dns_resstatscounter_mismatch);
603
0
    eresult = DNS_R_MISMATCH;
604
0
    goto done;
605
0
  }
606
607
  /*
608
   * We have the right resp, so call the caller back.
609
   */
610
0
  goto done;
611
612
0
next:
613
  /*
614
   * This is the wrong response.  Check whether there is still enough
615
   * time to wait for the correct one to arrive before the timeout fires.
616
   */
617
0
  now = isc_loop_now(resp->loop);
618
0
  if (resp->timeout > 0) {
619
0
    timeout = resp->timeout - dispentry_runtime(resp, &now);
620
0
    if (timeout <= 0) {
621
      /*
622
       * The time window for receiving the correct response is
623
       * already closed, libuv has just not processed the
624
       * socket timer yet.  Invoke the read callback,
625
       * indicating a timeout.
626
       */
627
0
      eresult = ISC_R_TIMEDOUT;
628
0
      goto done;
629
0
    }
630
0
  }
631
632
  /*
633
   * Do not invoke the read callback just yet and instead wait for the
634
   * proper response to arrive until the original timeout fires.
635
   */
636
0
  respond = false;
637
0
  udp_dispatch_getnext(resp, timeout);
638
639
0
done:
640
0
  if (respond) {
641
0
    dispentry_log(resp, ISC_LOG_DEBUG(90),
642
0
            "UDP read callback on %p: %s", handle,
643
0
            isc_result_totext(eresult));
644
0
    resp->response(eresult, region, resp->arg);
645
0
  }
646
647
0
  dns_dispentry_detach(&resp); /* DISPENTRY003 */
648
0
}
649
650
static isc_result_t
651
0
tcp_recv_oldest(dns_dispatch_t *disp, dns_dispentry_t **respp) {
652
0
  dns_dispentry_t *resp = NULL;
653
0
  resp = ISC_LIST_HEAD(disp->active);
654
0
  if (resp != NULL) {
655
0
    disp->timedout++;
656
657
0
    *respp = resp;
658
0
    return ISC_R_TIMEDOUT;
659
0
  }
660
661
0
  return ISC_R_NOTFOUND;
662
0
}
663
664
static isc_result_t
665
tcp_recv_success(dns_dispatch_t *disp, isc_region_t *region,
666
0
     dns_dispentry_t **respp) {
667
0
  isc_buffer_t source;
668
0
  dns_messageid_t id;
669
0
  unsigned int flags;
670
0
  isc_result_t result = ISC_R_SUCCESS;
671
672
0
  dispatch_log(disp, ISC_LOG_DEBUG(90),
673
0
         "TCP read success, length == %d, addr = %p",
674
0
         region->length, region->base);
675
676
  /*
677
   * Peek into the buffer to see what we can see.
678
   */
679
0
  isc_buffer_init(&source, region->base, region->length);
680
0
  isc_buffer_add(&source, region->length);
681
0
  result = dns_message_peekheader(&source, &id, &flags);
682
0
  if (result != ISC_R_SUCCESS) {
683
0
    dispatch_log(disp, ISC_LOG_DEBUG(10), "got garbage packet");
684
0
    return ISC_R_UNEXPECTED;
685
0
  }
686
687
0
  dispatch_log(disp, ISC_LOG_DEBUG(92),
688
0
         "got valid DNS message header, /QR %c, id %u",
689
0
         ((flags & DNS_MESSAGEFLAG_QR) != 0) ? '1' : '0', id);
690
691
  /*
692
   * Look at the message flags.  If it's a query, ignore it and keep
693
   * reading.
694
   */
695
0
  if ((flags & DNS_MESSAGEFLAG_QR) == 0) {
696
0
    dispatch_log(disp, ISC_LOG_DEBUG(10),
697
0
           "got DNS query instead of answer");
698
0
    return ISC_R_UNEXPECTED;
699
0
  }
700
701
  /*
702
   * We have a valid response; find the associated dispentry by
703
   * scanning disp->active.  With sequential IDs and a bounded
704
   * pipelining limit this is a short linear scan.
705
   */
706
0
  dns_dispentry_t *resp = NULL;
707
0
  ISC_LIST_FOREACH(disp->active, r, alink) {
708
0
    if (r->id == id) {
709
0
      resp = r;
710
0
      break;
711
0
    }
712
0
  }
713
714
0
  if (resp != NULL) {
715
0
    *respp = resp;
716
0
  } else {
717
0
    result = ISC_R_NOTFOUND;
718
0
  }
719
0
  dispatch_log(disp, ISC_LOG_DEBUG(90), "search for response: %s",
720
0
         isc_result_totext(result));
721
722
0
  return result;
723
0
}
724
725
static void
726
tcp_recv_add(dns_displist_t *resps, dns_dispentry_t *resp,
727
0
       isc_result_t result) {
728
0
  dns_dispentry_ref(resp); /* DISPENTRY009 */
729
0
  ISC_LIST_UNLINK(resp->disp->active, resp, alink);
730
0
  ISC_LIST_APPEND(*resps, resp, rlink);
731
0
  INSIST(resp->reading);
732
0
  resp->reading = false;
733
0
  resp->result = result;
734
0
}
735
736
static void
737
tcp_recv_shutdown(dns_dispatch_t *disp, dns_displist_t *resps,
738
0
      isc_result_t result) {
739
  /*
740
   * If there are any active responses, shut them all down.
741
   */
742
0
  ISC_LIST_FOREACH(disp->active, resp, alink) {
743
0
    tcp_recv_add(resps, resp, result);
744
0
  }
745
0
  disp->state = DNS_DISPATCHSTATE_CANCELED;
746
747
  /* Close now so the socket doesn't linger in CLOSE-WAIT. */
748
0
  if (disp->handle != NULL) {
749
0
    isc_nmhandle_close(disp->handle);
750
0
  }
751
0
}
752
753
static void
754
0
tcp_recv_processall(dns_displist_t *resps, isc_region_t *region) {
755
0
  ISC_LIST_FOREACH(*resps, resp, rlink) {
756
0
    ISC_LIST_UNLINK(*resps, resp, rlink);
757
758
0
    dispentry_log(resp, ISC_LOG_DEBUG(90), "read callback: %s",
759
0
            isc_result_totext(resp->result));
760
0
    resp->response(resp->result, region, resp->arg);
761
0
    dns_dispentry_detach(&resp); /* DISPENTRY009 */
762
0
  }
763
0
}
764
765
static void
766
0
tcp_startrecv_idle(dns_dispatch_t *disp) {
767
0
  REQUIRE(disp->mgr->reuse_timeout > 0);
768
769
  /*
770
   * No outstanding responses, but the connection is still up and
771
   * reusable.  Keep a read pending (bounded by the idle timeout)
772
   * so that a peer-initiated close is detected promptly;
773
   * otherwise the socket would linger in CLOSE-WAIT and be handed
774
   * out again as a dead reused connection.
775
   */
776
0
  isc_nmhandle_cleartimeout(disp->handle);
777
0
  isc_nmhandle_settimeout(disp->handle, disp->mgr->reuse_timeout);
778
0
  if (!disp->reading) {
779
0
    dispatch_log(disp, ISC_LOG_DEBUG(90), "keeping idle read on %p",
780
0
           disp->handle);
781
0
    tcp_startrecv(disp, NULL);
782
0
  }
783
0
}
784
785
/*
786
 * General flow:
787
 *
788
 * If I/O result == CANCELED, EOF, or error, notify everyone as the
789
 * various queues drain.
790
 *
791
 * If response:
792
 *  Allocate event, fill in details.
793
 *    If cannot allocate, restart.
794
 *  find target.  If not found, restart.
795
 *  if event queue is not empty, queue.  else, send.
796
 *  restart.
797
 */
798
static void
799
tcp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
800
0
   void *arg) {
801
0
  dns_dispatch_t *disp = (dns_dispatch_t *)arg;
802
0
  dns_dispentry_t *resp = NULL;
803
0
  char buf[ISC_SOCKADDR_FORMATSIZE];
804
0
  isc_sockaddr_t peer;
805
0
  dns_displist_t resps = ISC_LIST_INITIALIZER;
806
0
  isc_time_t now;
807
0
  int timeout = 0;
808
0
  isc_result_t result = eresult;
809
810
0
  REQUIRE(VALID_DISPATCH(disp));
811
812
0
  REQUIRE(disp->tid == isc_tid());
813
0
  INSIST(disp->reading);
814
0
  disp->reading = false;
815
816
0
  dispatch_log(disp, ISC_LOG_DEBUG(90),
817
0
         "TCP read:%s:requests %" PRIuFAST32,
818
0
         isc_result_totext(eresult), disp->requests);
819
820
0
  peer = isc_nmhandle_peeraddr(handle);
821
822
0
  rcu_read_lock();
823
  /*
824
   * Phase 1: Process timeout and success.
825
   */
826
0
  switch (eresult) {
827
0
  case ISC_R_TIMEDOUT:
828
    /*
829
     * Time out the oldest response in the active queue.
830
     */
831
0
    result = tcp_recv_oldest(disp, &resp);
832
0
    break;
833
0
  case ISC_R_SUCCESS:
834
    /* We got an answer */
835
0
    result = tcp_recv_success(disp, region, &resp);
836
0
    break;
837
838
0
  default:
839
0
    break;
840
0
  }
841
842
0
  if (resp != NULL) {
843
0
    tcp_recv_add(&resps, resp, result);
844
0
  }
845
846
  /*
847
   * Phase 2: Look if we timed out before.
848
   */
849
850
0
  if (result == ISC_R_NOTFOUND) {
851
0
    if (eresult == ISC_R_TIMEDOUT) {
852
      /*
853
       * An idle keep-alive read timed out with no outstanding
854
       * responses.
855
       */
856
0
      result = ISC_R_CANCELED;
857
0
    } else if (disp->timedout > 0) {
858
      /*
859
       * There was an active query that timed-out or canceled
860
       * before.
861
       */
862
0
      disp->timedout--;
863
0
    } else {
864
0
      result = ISC_R_UNEXPECTED;
865
0
    }
866
0
  }
867
868
  /*
869
   * Phase 3: Trigger timeouts.  It's possible that the responses would
870
   * have been timed out out already, but non-matching TCP reads have
871
   * prevented this.
872
   */
873
0
  resp = ISC_LIST_HEAD(disp->active);
874
0
  if (resp != NULL) {
875
0
    now = isc_loop_now(resp->loop);
876
0
  }
877
0
  while (resp != NULL) {
878
0
    dns_dispentry_t *next = ISC_LIST_NEXT(resp, alink);
879
880
0
    if (resp->timeout > 0) {
881
0
      timeout = resp->timeout - dispentry_runtime(resp, &now);
882
0
      if (timeout <= 0) {
883
        /*
884
         * Count this so we can later ignore the
885
         * outstanding answer (if it eventually
886
         * arrives).
887
         */
888
0
        disp->timedout++;
889
890
0
        tcp_recv_add(&resps, resp, ISC_R_TIMEDOUT);
891
0
      }
892
0
    }
893
894
0
    resp = next;
895
0
  }
896
897
  /*
898
   * Phase 4: log if we errored out.
899
   */
900
0
  switch (result) {
901
0
  case ISC_R_SUCCESS:
902
0
  case ISC_R_TIMEDOUT:
903
0
  case ISC_R_NOTFOUND:
904
0
    break;
905
0
  case ISC_R_SHUTTINGDOWN:
906
0
  case ISC_R_CANCELED:
907
0
  case ISC_R_EOF:
908
0
  case ISC_R_CONNECTIONRESET:
909
0
    if (isc_log_wouldlog(ISC_LOG_DEBUG(90))) {
910
0
      isc_sockaddr_format(&peer, buf, sizeof(buf));
911
0
      dispatch_log(disp, ISC_LOG_DEBUG(90),
912
0
             "shutting down TCP: %s: %s", buf,
913
0
             isc_result_totext(result));
914
0
    }
915
0
    tcp_recv_shutdown(disp, &resps, result);
916
0
    break;
917
0
  default:
918
0
    isc_sockaddr_format(&peer, buf, sizeof(buf));
919
0
    dispatch_log(disp, ISC_LOG_ERROR,
920
0
           "shutting down due to TCP "
921
0
           "receive error: %s: %s",
922
0
           buf, isc_result_totext(result));
923
0
    tcp_recv_shutdown(disp, &resps, result);
924
0
    break;
925
0
  }
926
927
  /*
928
   * Phase 5: Resume reading if there are still active responses
929
   */
930
0
  resp = ISC_LIST_HEAD(disp->active);
931
0
  if (resp != NULL) {
932
0
    if (resp->timeout > 0) {
933
0
      timeout = resp->timeout - dispentry_runtime(resp, &now);
934
0
      INSIST(timeout > 0);
935
0
    }
936
0
    tcp_startrecv(disp, resp);
937
0
    if (timeout > 0) {
938
0
      isc_nmhandle_settimeout(handle, timeout);
939
0
    }
940
0
  }
941
942
0
  rcu_read_unlock();
943
944
  /*
945
   * Phase 6: Process all scheduled callbacks.
946
   */
947
0
  tcp_recv_processall(&resps, region);
948
949
  /*
950
   * Phase 7: Restart reading on idle connections.
951
   */
952
0
  if (ISC_LIST_EMPTY(disp->active) &&
953
0
      disp->state == DNS_DISPATCHSTATE_CONNECTED &&
954
0
      disp->mgr->reuse_timeout > 0)
955
0
  {
956
0
    tcp_startrecv_idle(disp);
957
0
  }
958
959
0
  dns_dispatch_detach(&disp); /* DISPATCH002 */
960
0
}
961
962
/*%
963
 * Create a temporary port list to set the initial default set of dispatch
964
 * ephemeral ports.  This is almost meaningless as the application will
965
 * normally set the ports explicitly, but is provided to fill some minor corner
966
 * cases.
967
 */
968
static void
969
0
create_default_portset(isc_mem_t *mctx, int family, isc_portset_t **portsetp) {
970
0
  in_port_t low, high;
971
972
0
  isc_net_getportrange(family, &low, &high);
973
974
0
  isc_portset_create(mctx, portsetp);
975
0
  isc_portset_addrange(*portsetp, low, high);
976
0
}
977
978
static isc_result_t
979
setavailports(dns_dispatchmgr_t *mgr, isc_portset_t *v4portset,
980
0
        isc_portset_t *v6portset) {
981
0
  in_port_t *v4ports, *v6ports, p = 0;
982
0
  unsigned int nv4ports, nv6ports, i4 = 0, i6 = 0;
983
984
0
  nv4ports = isc_portset_nports(v4portset);
985
0
  nv6ports = isc_portset_nports(v6portset);
986
987
0
  v4ports = NULL;
988
0
  if (nv4ports != 0) {
989
0
    v4ports = isc_mem_cget(mgr->mctx, nv4ports, sizeof(in_port_t));
990
0
  }
991
0
  v6ports = NULL;
992
0
  if (nv6ports != 0) {
993
0
    v6ports = isc_mem_cget(mgr->mctx, nv6ports, sizeof(in_port_t));
994
0
  }
995
996
0
  do {
997
0
    if (isc_portset_isset(v4portset, p)) {
998
0
      INSIST(i4 < nv4ports);
999
0
      v4ports[i4++] = p;
1000
0
    }
1001
0
    if (isc_portset_isset(v6portset, p)) {
1002
0
      INSIST(i6 < nv6ports);
1003
0
      v6ports[i6++] = p;
1004
0
    }
1005
0
  } while (p++ < 65535);
1006
0
  INSIST(i4 == nv4ports && i6 == nv6ports);
1007
1008
0
  if (mgr->v4ports != NULL) {
1009
0
    isc_mem_cput(mgr->mctx, mgr->v4ports, mgr->nv4ports,
1010
0
           sizeof(in_port_t));
1011
0
  }
1012
0
  mgr->v4ports = v4ports;
1013
0
  mgr->nv4ports = nv4ports;
1014
1015
0
  if (mgr->v6ports != NULL) {
1016
0
    isc_mem_cput(mgr->mctx, mgr->v6ports, mgr->nv6ports,
1017
0
           sizeof(in_port_t));
1018
0
  }
1019
0
  mgr->v6ports = v6ports;
1020
0
  mgr->nv6ports = nv6ports;
1021
1022
0
  return ISC_R_SUCCESS;
1023
0
}
1024
1025
/*
1026
 * Publics.
1027
 */
1028
1029
isc_result_t
1030
0
dns_dispatchmgr_create(isc_mem_t *mctx, dns_dispatchmgr_t **mgrp) {
1031
0
  dns_dispatchmgr_t *mgr = NULL;
1032
0
  isc_portset_t *v4portset = NULL;
1033
0
  isc_portset_t *v6portset = NULL;
1034
1035
0
  REQUIRE(mctx != NULL);
1036
0
  REQUIRE(mgrp != NULL && *mgrp == NULL);
1037
1038
0
  mgr = isc_mem_get(mctx, sizeof(dns_dispatchmgr_t));
1039
0
  *mgr = (dns_dispatchmgr_t){
1040
0
    .magic = 0,
1041
0
    .nloops = isc_loopmgr_nloops(),
1042
0
  };
1043
1044
#if DNS_DISPATCH_TRACE
1045
  fprintf(stderr, "dns_dispatchmgr__init:%s:%s:%d:%p->references = 1\n",
1046
    __func__, __FILE__, __LINE__, mgr);
1047
#endif
1048
0
  isc_refcount_init(&mgr->references, 1);
1049
1050
0
  isc_mem_attach(mctx, &mgr->mctx);
1051
1052
0
  mgr->tcps = isc_mem_cget(mgr->mctx, mgr->nloops, sizeof(mgr->tcps[0]));
1053
0
  for (size_t i = 0; i < mgr->nloops; i++) {
1054
0
    mgr->tcps[i] = cds_lfht_new(
1055
0
      2, 2, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
1056
0
      NULL);
1057
0
  }
1058
1059
0
  create_default_portset(mgr->mctx, AF_INET, &v4portset);
1060
0
  create_default_portset(mgr->mctx, AF_INET6, &v6portset);
1061
1062
0
  setavailports(mgr, v4portset, v6portset);
1063
1064
0
  isc_portset_destroy(mgr->mctx, &v4portset);
1065
0
  isc_portset_destroy(mgr->mctx, &v6portset);
1066
1067
0
  mgr->qids = cds_lfht_new(QIDS_INIT_SIZE, QIDS_MIN_SIZE, 0,
1068
0
         CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
1069
0
         NULL);
1070
1071
0
  mgr->magic = DNS_DISPATCHMGR_MAGIC;
1072
1073
0
  *mgrp = mgr;
1074
0
  return ISC_R_SUCCESS;
1075
0
}
1076
1077
#if DNS_DISPATCH_TRACE
1078
ISC_REFCOUNT_TRACE_IMPL(dns_dispatchmgr, dispatchmgr_destroy);
1079
#else
1080
0
ISC_REFCOUNT_IMPL(dns_dispatchmgr, dispatchmgr_destroy);
Unexecuted instantiation: dns_dispatchmgr_ref
Unexecuted instantiation: dns_dispatchmgr_unref
Unexecuted instantiation: dns_dispatchmgr_detach
1081
0
#endif
1082
0
1083
0
void
1084
0
dns_dispatchmgr_setblackhole(dns_dispatchmgr_t *mgr, dns_acl_t *blackhole) {
1085
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1086
0
  if (mgr->blackhole != NULL) {
1087
0
    dns_acl_detach(&mgr->blackhole);
1088
0
  }
1089
0
  dns_acl_attach(blackhole, &mgr->blackhole);
1090
0
}
1091
1092
dns_acl_t *
1093
0
dns_dispatchmgr_getblackhole(dns_dispatchmgr_t *mgr) {
1094
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1095
0
  return mgr->blackhole;
1096
0
}
1097
1098
isc_result_t
1099
dns_dispatchmgr_setavailports(dns_dispatchmgr_t *mgr, isc_portset_t *v4portset,
1100
0
            isc_portset_t *v6portset) {
1101
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1102
0
  return setavailports(mgr, v4portset, v6portset);
1103
0
}
1104
1105
void
1106
dns_dispatchmgr_setreusetimeout(dns_dispatchmgr_t *mgr,
1107
0
        unsigned int reuse_timeout) {
1108
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1109
0
  mgr->reuse_timeout = reuse_timeout;
1110
0
}
1111
1112
static void
1113
0
dispatchmgr_destroy(dns_dispatchmgr_t *mgr) {
1114
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1115
1116
0
  isc_refcount_destroy(&mgr->references);
1117
1118
0
  mgr->magic = 0;
1119
1120
0
  RUNTIME_CHECK(!cds_lfht_destroy(mgr->qids, NULL));
1121
1122
0
  for (size_t i = 0; i < mgr->nloops; i++) {
1123
0
    RUNTIME_CHECK(!cds_lfht_destroy(mgr->tcps[i], NULL));
1124
0
  }
1125
0
  isc_mem_cput(mgr->mctx, mgr->tcps, mgr->nloops, sizeof(mgr->tcps[0]));
1126
1127
0
  if (mgr->blackhole != NULL) {
1128
0
    dns_acl_detach(&mgr->blackhole);
1129
0
  }
1130
1131
0
  if (mgr->stats != NULL) {
1132
0
    isc_stats_detach(&mgr->stats);
1133
0
  }
1134
1135
0
  if (mgr->v4ports != NULL) {
1136
0
    isc_mem_cput(mgr->mctx, mgr->v4ports, mgr->nv4ports,
1137
0
           sizeof(in_port_t));
1138
0
  }
1139
0
  if (mgr->v6ports != NULL) {
1140
0
    isc_mem_cput(mgr->mctx, mgr->v6ports, mgr->nv6ports,
1141
0
           sizeof(in_port_t));
1142
0
  }
1143
1144
0
  isc_mem_putanddetach(&mgr->mctx, mgr, sizeof(dns_dispatchmgr_t));
1145
0
}
1146
1147
void
1148
0
dns_dispatchmgr_setstats(dns_dispatchmgr_t *mgr, isc_stats_t *stats) {
1149
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1150
0
  REQUIRE(mgr->stats == NULL);
1151
1152
0
  isc_stats_attach(stats, &mgr->stats);
1153
0
}
1154
1155
/*
1156
 * Allocate and set important limits.
1157
 */
1158
static void
1159
dispatch_allocate(dns_dispatchmgr_t *mgr, isc_socktype_t type, isc_tid_t tid,
1160
0
      dns_dispatch_t **dispp) {
1161
0
  dns_dispatch_t *disp = NULL;
1162
1163
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1164
0
  REQUIRE(dispp != NULL && *dispp == NULL);
1165
1166
  /*
1167
   * Set up the dispatcher, mostly.  Don't bother setting some of
1168
   * the options that are controlled by tcp vs. udp, etc.
1169
   */
1170
1171
0
  disp = isc_mem_get(mgr->mctx, sizeof(*disp));
1172
0
  *disp = (dns_dispatch_t){
1173
0
    .socktype = type,
1174
0
    .active = ISC_LIST_INITIALIZER,
1175
0
    .pending = ISC_LIST_INITIALIZER,
1176
0
    .tid = tid,
1177
0
    .magic = DISPATCH_MAGIC,
1178
0
  };
1179
1180
0
  isc_mem_attach(mgr->mctx, &disp->mctx);
1181
1182
0
  dns_dispatchmgr_attach(mgr, &disp->mgr);
1183
#if DNS_DISPATCH_TRACE
1184
  fprintf(stderr, "dns_dispatch__init:%s:%s:%d:%p->references = 1\n",
1185
    __func__, __FILE__, __LINE__, disp);
1186
#endif
1187
0
  isc_refcount_init(&disp->references, 1); /* DISPATCH000 */
1188
1189
0
  *dispp = disp;
1190
0
}
1191
1192
struct dispatch_key {
1193
  const isc_sockaddr_t *local;
1194
  const isc_sockaddr_t *peer;
1195
  const dns_transport_t *transport;
1196
  const dns_dispatchtype_t disptype;
1197
};
1198
1199
static uint32_t
1200
0
dispatch_hash(struct dispatch_key *key) {
1201
0
  isc_hash32_t hash;
1202
1203
0
  isc_hash32_init(&hash);
1204
1205
0
  isc_sockaddr_hash_ex(&hash, key->peer, false);
1206
0
  if (key->local != NULL) {
1207
0
    isc_sockaddr_hash_ex(&hash, key->local, true);
1208
0
  }
1209
0
  if (key->transport != NULL) {
1210
0
    uintptr_t transport = (uintptr_t)key->transport;
1211
0
    isc_hash32_hash(&hash, &transport, sizeof(transport), true);
1212
0
  }
1213
0
  isc_hash32_hash(&hash, &key->disptype, sizeof(key->disptype), true);
1214
1215
0
  return isc_hash32_finalize(&hash);
1216
0
}
1217
1218
static int
1219
0
dispatch_match(struct cds_lfht_node *node, const void *key0) {
1220
0
  dns_dispatch_t *disp = caa_container_of(node, dns_dispatch_t, ht_node);
1221
0
  const struct dispatch_key *key = key0;
1222
1223
0
  return disp->disptype == key->disptype &&
1224
0
         disp->transport == key->transport &&
1225
0
         isc_sockaddr_equal(&disp->peer, key->peer) &&
1226
0
         (key->local == NULL ||
1227
0
    isc_sockaddr_equal(&disp->local, key->local));
1228
0
}
1229
1230
static isc_result_t
1231
dispatch_gettcp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr,
1232
    const isc_sockaddr_t *destaddr, dns_transport_t *transport,
1233
0
    dns_dispatchtype_t disptype, dns_dispatch_t **dispp) {
1234
0
  dns_dispatch_t *disp_connected = NULL;
1235
0
  dns_dispatch_t *disp_fallback = NULL;
1236
0
  isc_result_t result = ISC_R_NOTFOUND;
1237
0
  isc_tid_t tid = isc_tid();
1238
1239
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1240
0
  REQUIRE(destaddr != NULL);
1241
0
  REQUIRE(dispp != NULL && *dispp == NULL);
1242
1243
0
  struct dispatch_key key = {
1244
0
    .local = localaddr,
1245
0
    .peer = destaddr,
1246
0
    .transport = transport,
1247
0
    .disptype = disptype,
1248
0
  };
1249
1250
0
  rcu_read_lock();
1251
0
  struct cds_lfht_iter iter;
1252
0
  dns_dispatch_t *disp = NULL;
1253
0
  cds_lfht_for_each_entry_duplicate(mgr->tcps[tid], dispatch_hash(&key),
1254
0
            dispatch_match, &key, &iter, disp,
1255
0
            ht_node) {
1256
0
    INSIST(disp->tid == isc_tid());
1257
0
    INSIST(disp->socktype == isc_socktype_tcp);
1258
1259
0
    switch (disp->state) {
1260
0
    case DNS_DISPATCHSTATE_NONE:
1261
      /* A dispatch in indeterminate state, skip it */
1262
0
      break;
1263
0
    case DNS_DISPATCHSTATE_CONNECTED:
1264
      /* We found a connected dispatch */
1265
0
      dns_dispatch_attach(disp, &disp_connected);
1266
0
      break;
1267
0
    case DNS_DISPATCHSTATE_CONNECTING:
1268
      /* We found "a" dispatch, store it for later */
1269
0
      if (disp_fallback == NULL) {
1270
0
        dns_dispatch_attach(disp, &disp_fallback);
1271
0
      }
1272
0
      break;
1273
0
    case DNS_DISPATCHSTATE_CANCELED:
1274
      /* A canceled dispatch, skip it. */
1275
0
      break;
1276
0
    default:
1277
0
      UNREACHABLE();
1278
0
    }
1279
1280
0
    if (disp_connected != NULL) {
1281
0
      break;
1282
0
    }
1283
0
  }
1284
0
  rcu_read_unlock();
1285
1286
0
  if (disp_connected != NULL) {
1287
    /* We found connected dispatch */
1288
0
    INSIST(disp_connected->handle != NULL);
1289
1290
0
    *dispp = disp_connected;
1291
0
    disp_connected = NULL;
1292
1293
0
    result = ISC_R_SUCCESS;
1294
1295
0
    if (disp_fallback != NULL) {
1296
0
      dns_dispatch_detach(&disp_fallback);
1297
0
    }
1298
0
  } else if (disp_fallback != NULL) {
1299
0
    *dispp = disp_fallback;
1300
1301
0
    result = ISC_R_SUCCESS;
1302
0
  }
1303
1304
0
  return result;
1305
0
}
1306
1307
static void
1308
dispatch_createtcp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr,
1309
       const isc_sockaddr_t *destaddr, dns_transport_t *transport,
1310
       dns_dispatchtype_t disptype, dns_dispatchopt_t options,
1311
0
       dns_dispatch_t **dispp) {
1312
0
  dns_dispatch_t *disp = NULL;
1313
0
  isc_tid_t tid = isc_tid();
1314
1315
0
  dispatch_allocate(mgr, isc_socktype_tcp, tid, &disp);
1316
1317
0
  disp->disptype = disptype;
1318
0
  disp->nextid = isc_random16();
1319
0
  disp->options = options;
1320
0
  disp->peer = *destaddr;
1321
0
  if (transport != NULL) {
1322
0
    dns_transport_attach(transport, &disp->transport);
1323
0
  }
1324
1325
0
  if (localaddr != NULL) {
1326
0
    disp->local = *localaddr;
1327
0
  } else {
1328
0
    int pf;
1329
0
    pf = isc_sockaddr_pf(destaddr);
1330
0
    isc_sockaddr_anyofpf(&disp->local, pf);
1331
0
    isc_sockaddr_setport(&disp->local, 0);
1332
0
  }
1333
1334
  /*
1335
   * Append it to the dispatcher list.
1336
   */
1337
0
  if ((options & DNS_DISPATCHOPT_FIXEDID) == 0) {
1338
0
    struct dispatch_key key = {
1339
0
      .local = &disp->local,
1340
0
      .peer = &disp->peer,
1341
0
      .transport = transport,
1342
0
      .disptype = disptype,
1343
0
    };
1344
0
    rcu_read_lock();
1345
0
    cds_lfht_add(mgr->tcps[tid], dispatch_hash(&key),
1346
0
           &disp->ht_node);
1347
0
    rcu_read_unlock();
1348
0
  }
1349
1350
0
  *dispp = disp;
1351
0
}
1352
1353
isc_result_t
1354
dns_dispatch_createtcp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr,
1355
           const isc_sockaddr_t *destaddr,
1356
           dns_transport_t *transport, dns_dispatchtype_t disptype,
1357
0
           dns_dispatchopt_t options, dns_dispatch_t **dispp) {
1358
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1359
0
  REQUIRE(destaddr != NULL);
1360
1361
0
  isc_result_t result;
1362
1363
0
  if ((options & DNS_DISPATCHOPT_FIXEDID) == 0 &&
1364
0
      disptype != DNS_DISPATCHTYPE_XFRIN)
1365
0
  {
1366
0
    result = dispatch_gettcp(mgr, localaddr, destaddr, transport,
1367
0
           disptype, dispp);
1368
0
    if (result == ISC_R_SUCCESS) {
1369
0
      if (isc_log_wouldlog(90)) {
1370
0
        char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1371
1372
0
        isc_sockaddr_format(&(*dispp)->local, addrbuf,
1373
0
                ISC_SOCKADDR_FORMATSIZE);
1374
1375
0
        mgr_log(mgr, ISC_LOG_DEBUG(90),
1376
0
          "dns_dispatch_createtcp: reused TCP "
1377
0
          "dispatch %p for "
1378
0
          "%s",
1379
0
          *dispp, addrbuf);
1380
0
      }
1381
0
      return result;
1382
0
    }
1383
0
  }
1384
1385
  /*
1386
   * Otherwise allocate new TCP dispatch.
1387
   */
1388
1389
0
  dispatch_createtcp(mgr, localaddr, destaddr, transport, disptype,
1390
0
         options, dispp);
1391
1392
0
  if (isc_log_wouldlog(90)) {
1393
0
    char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1394
1395
0
    isc_sockaddr_format(&(*dispp)->local, addrbuf,
1396
0
            ISC_SOCKADDR_FORMATSIZE);
1397
1398
0
    mgr_log(mgr, ISC_LOG_DEBUG(90),
1399
0
      "dns_dispatch_createtcp: created TCP dispatch %p for "
1400
0
      "%s",
1401
0
      *dispp, addrbuf);
1402
0
  }
1403
1404
0
  return ISC_R_SUCCESS;
1405
0
}
1406
1407
isc_result_t
1408
dns_dispatch_createudp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr,
1409
0
           dns_dispatch_t **dispp) {
1410
0
  isc_result_t result;
1411
0
  dns_dispatch_t *disp = NULL;
1412
1413
0
  REQUIRE(VALID_DISPATCHMGR(mgr));
1414
0
  REQUIRE(localaddr != NULL);
1415
0
  REQUIRE(dispp != NULL && *dispp == NULL);
1416
1417
0
  result = dispatch_createudp(mgr, localaddr, isc_tid(), &disp);
1418
0
  if (result == ISC_R_SUCCESS) {
1419
0
    *dispp = disp;
1420
0
  }
1421
1422
0
  return result;
1423
0
}
1424
1425
static isc_result_t
1426
dispatch_createudp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr,
1427
0
       isc_tid_t tid, dns_dispatch_t **dispp) {
1428
0
  isc_result_t result = ISC_R_SUCCESS;
1429
0
  dns_dispatch_t *disp = NULL;
1430
0
  isc_sockaddr_t sa_any;
1431
1432
  /*
1433
   * Check whether this address/port is available locally.
1434
   */
1435
0
  isc_sockaddr_anyofpf(&sa_any, isc_sockaddr_pf(localaddr));
1436
0
  if (!isc_sockaddr_eqaddr(&sa_any, localaddr)) {
1437
0
    RETERR(isc_nm_checkaddr(localaddr, isc_socktype_udp));
1438
0
  }
1439
1440
0
  dispatch_allocate(mgr, isc_socktype_udp, tid, &disp);
1441
1442
0
  if (isc_log_wouldlog(90)) {
1443
0
    char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1444
1445
0
    isc_sockaddr_format(localaddr, addrbuf,
1446
0
            ISC_SOCKADDR_FORMATSIZE);
1447
0
    mgr_log(mgr, ISC_LOG_DEBUG(90),
1448
0
      "dispatch_createudp: created UDP dispatch %p for %s",
1449
0
      disp, addrbuf);
1450
0
  }
1451
1452
0
  disp->local = *localaddr;
1453
1454
  /*
1455
   * Don't append it to the dispatcher list, we don't care about UDP, only
1456
   * TCP should be searched
1457
   *
1458
   * ISC_LIST_APPEND(mgr->list, disp, link);
1459
   */
1460
1461
0
  *dispp = disp;
1462
1463
0
  return result;
1464
0
}
1465
1466
static void
1467
0
dispatch_destroy_rcu(struct rcu_head *rcu_head) {
1468
0
  dns_dispatch_t *disp = caa_container_of(rcu_head, dns_dispatch_t,
1469
0
            rcu_head);
1470
1471
0
  isc_mem_putanddetach(&disp->mctx, disp, sizeof(*disp));
1472
0
}
1473
1474
static void
1475
0
dispatch_destroy(dns_dispatch_t *disp) {
1476
0
  dns_dispatchmgr_t *mgr = disp->mgr;
1477
0
  isc_tid_t tid = isc_tid();
1478
1479
0
  disp->magic = 0;
1480
1481
0
  if ((disp->options & DNS_DISPATCHOPT_FIXEDID) == 0 &&
1482
0
      disp->socktype == isc_socktype_tcp)
1483
0
  {
1484
0
    (void)cds_lfht_del(mgr->tcps[tid], &disp->ht_node);
1485
0
  }
1486
1487
0
  INSIST(disp->requests == 0);
1488
0
  INSIST(ISC_LIST_EMPTY(disp->pending));
1489
0
  INSIST(ISC_LIST_EMPTY(disp->active));
1490
1491
0
  dispatch_log(disp, ISC_LOG_DEBUG(90), "destroying dispatch %p", disp);
1492
1493
0
  if (disp->handle) {
1494
0
    dispatch_log(disp, ISC_LOG_DEBUG(90),
1495
0
           "detaching TCP handle %p from %p", disp->handle,
1496
0
           &disp->handle);
1497
0
    isc_nmhandle_detach(&disp->handle);
1498
0
  }
1499
0
  if (disp->transport != NULL) {
1500
0
    dns_transport_detach(&disp->transport);
1501
0
  }
1502
0
  dns_dispatchmgr_detach(&disp->mgr);
1503
1504
0
  call_rcu(&disp->rcu_head, dispatch_destroy_rcu);
1505
0
}
1506
1507
#if DNS_DISPATCH_TRACE
1508
ISC_REFCOUNT_TRACE_IMPL(dns_dispatch, dispatch_destroy);
1509
#else
1510
0
ISC_REFCOUNT_IMPL(dns_dispatch, dispatch_destroy);
Unexecuted instantiation: dns_dispatch_ref
Unexecuted instantiation: dns_dispatch_unref
Unexecuted instantiation: dns_dispatch_detach
1511
0
#endif
1512
0
1513
0
isc_result_t
1514
0
dns_dispatch_add(dns_dispatch_t *disp, isc_loop_t *loop,
1515
0
     dns_dispatchopt_t options, unsigned int connect_timeout,
1516
0
     unsigned int timeout, const isc_sockaddr_t *dest,
1517
0
     dns_transport_t *transport, isc_tlsctx_cache_t *tlsctx_cache,
1518
0
     dispatch_cb_t connected, dispatch_cb_t sent,
1519
0
     dispatch_cb_t response, void *arg, dns_messageid_t *idp,
1520
0
     dns_dispentry_t **respp) {
1521
0
  REQUIRE(VALID_DISPATCH(disp));
1522
0
  REQUIRE(dest != NULL);
1523
0
  REQUIRE(respp != NULL && *respp == NULL);
1524
0
  REQUIRE(idp != NULL);
1525
0
  REQUIRE(disp->socktype == isc_socktype_tcp ||
1526
0
    disp->socktype == isc_socktype_udp);
1527
0
  REQUIRE(connected != NULL);
1528
0
  REQUIRE(response != NULL);
1529
0
  REQUIRE(sent != NULL);
1530
0
  REQUIRE(loop != NULL);
1531
0
  REQUIRE(disp->tid == isc_tid());
1532
0
  REQUIRE(disp->transport == transport);
1533
1534
0
  if (disp->state == DNS_DISPATCHSTATE_CANCELED) {
1535
0
    return ISC_R_CANCELED;
1536
0
  }
1537
1538
0
  in_port_t localport = isc_sockaddr_getport(&disp->local);
1539
0
  dns_dispentry_t *resp = isc_mem_get(disp->mctx, sizeof(*resp));
1540
0
  *resp = (dns_dispentry_t){
1541
0
    .connect_timeout = connect_timeout,
1542
0
    .timeout = timeout,
1543
0
    .port = localport,
1544
0
    .peer = *dest,
1545
0
    .loop = loop,
1546
0
    .connected = connected,
1547
0
    .sent = sent,
1548
0
    .response = response,
1549
0
    .arg = arg,
1550
0
    .alink = ISC_LINK_INITIALIZER,
1551
0
    .plink = ISC_LINK_INITIALIZER,
1552
0
    .rlink = ISC_LINK_INITIALIZER,
1553
0
    .magic = RESPONSE_MAGIC,
1554
0
  };
1555
1556
#if DNS_DISPATCH_TRACE
1557
  fprintf(stderr, "dns_dispentry__init:%s:%s:%d:%p->references = 1\n",
1558
    __func__, __FILE__, __LINE__, resp);
1559
#endif
1560
0
  isc_refcount_init(&resp->references, 1); /* DISPENTRY000 */
1561
1562
0
  if (disp->socktype == isc_socktype_udp) {
1563
0
    isc_result_t result = setup_socket(disp, resp, dest,
1564
0
               &localport);
1565
0
    if (result != ISC_R_SUCCESS) {
1566
0
      isc_mem_put(disp->mctx, resp, sizeof(*resp));
1567
0
      inc_stats(disp->mgr, dns_resstatscounter_dispsockfail);
1568
0
      return result;
1569
0
    }
1570
0
  }
1571
1572
0
  isc_result_t result = ISC_R_NOMORE;
1573
0
  rcu_read_lock();
1574
1575
0
  if (disp->socktype == isc_socktype_tcp) {
1576
    /*
1577
     * TCP dispentries don't use the global QID hash table.
1578
     * Responses are matched by scanning disp->active, and
1579
     * sequential per-dispatch IDs (bounded by the pipelining
1580
     * limit) are guaranteed to be unique within the dispatch.
1581
     * FIXEDID TCP dispatches are always fresh and isolated
1582
     * (see dns_dispatch_createtcp), so the caller-supplied ID
1583
     * can't collide either.
1584
     */
1585
0
    resp->id = ((options & DNS_DISPATCHOPT_FIXEDID) != 0)
1586
0
           ? *idp
1587
0
           : disp->nextid++;
1588
0
    result = ISC_R_SUCCESS;
1589
0
  } else {
1590
0
    size_t i = 0;
1591
0
    do {
1592
      /*
1593
       * Try somewhat hard to find a unique random ID
1594
       * (or use the fixed ID if DNS_DISPATCHOPT_FIXEDID
1595
       * is set).
1596
       */
1597
0
      resp->id = ((options & DNS_DISPATCHOPT_FIXEDID) != 0)
1598
0
             ? *idp
1599
0
             : (dns_messageid_t)isc_random16();
1600
1601
0
      struct cds_lfht_node *node = cds_lfht_add_unique(
1602
0
        disp->mgr->qids, qid_hash(resp), qid_match,
1603
0
        resp, &resp->ht_node);
1604
1605
0
      if (node != &resp->ht_node) {
1606
0
        if ((options & DNS_DISPATCHOPT_FIXEDID) != 0) {
1607
          /*
1608
           * When using fixed ID, we either
1609
           * must use it or fail.
1610
           */
1611
0
          goto fail;
1612
0
        }
1613
0
      } else {
1614
0
        result = ISC_R_SUCCESS;
1615
0
        break;
1616
0
      }
1617
0
    } while (i++ < QID_MAX_TRIES);
1618
0
  }
1619
0
fail:
1620
0
  if (result != ISC_R_SUCCESS) {
1621
0
    isc_mem_put(disp->mctx, resp, sizeof(*resp));
1622
0
    rcu_read_unlock();
1623
0
    return result;
1624
0
  }
1625
1626
0
  isc_mem_attach(disp->mctx, &resp->mctx);
1627
1628
0
  if (transport != NULL) {
1629
0
    dns_transport_attach(transport, &resp->transport);
1630
0
  }
1631
1632
0
  if (tlsctx_cache != NULL) {
1633
0
    isc_tlsctx_cache_attach(tlsctx_cache, &resp->tlsctx_cache);
1634
0
  }
1635
1636
0
  dns_dispatch_attach(disp, &resp->disp); /* DISPATCH001 */
1637
1638
0
  disp->requests++;
1639
1640
  /*
1641
   * If this shared TCP dispatch has reached the pipelining limit,
1642
   * remove it from the hash table so new queries get a fresh
1643
   * connection.  The dispatch continues to serve its existing
1644
   * queries until they complete.
1645
   */
1646
0
  if (disp->socktype == isc_socktype_tcp &&
1647
0
      (disp->options & DNS_DISPATCHOPT_FIXEDID) == 0 &&
1648
0
      disp->requests >= dns_dispatch_tcppipelining)
1649
0
  {
1650
0
    (void)cds_lfht_del(disp->mgr->tcps[isc_tid()], &disp->ht_node);
1651
0
  }
1652
1653
0
  inc_stats(disp->mgr, (disp->socktype == isc_socktype_udp)
1654
0
             ? dns_resstatscounter_disprequdp
1655
0
             : dns_resstatscounter_dispreqtcp);
1656
1657
0
  rcu_read_unlock();
1658
1659
0
  *idp = resp->id;
1660
0
  *respp = resp;
1661
1662
0
  return ISC_R_SUCCESS;
1663
0
}
1664
1665
isc_result_t
1666
0
dns_dispatch_getnext(dns_dispentry_t *resp) {
1667
0
  REQUIRE(VALID_RESPONSE(resp));
1668
0
  REQUIRE(VALID_DISPATCH(resp->disp));
1669
1670
0
  dns_dispatch_t *disp = resp->disp;
1671
0
  isc_result_t result = ISC_R_SUCCESS;
1672
0
  int64_t timeout = 0;
1673
1674
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "getnext for QID %d", resp->id);
1675
1676
0
  if (resp->timeout > 0) {
1677
0
    isc_time_t now = isc_loop_now(resp->loop);
1678
0
    timeout = resp->timeout - dispentry_runtime(resp, &now);
1679
0
    if (timeout <= 0) {
1680
0
      return ISC_R_TIMEDOUT;
1681
0
    }
1682
0
  }
1683
1684
0
  INSIST(disp->tid == isc_tid());
1685
0
  switch (disp->socktype) {
1686
0
  case isc_socktype_udp:
1687
0
    udp_dispatch_getnext(resp, timeout);
1688
0
    break;
1689
0
  case isc_socktype_tcp:
1690
0
    tcp_dispatch_getnext(disp, resp, timeout);
1691
0
    break;
1692
0
  default:
1693
0
    UNREACHABLE();
1694
0
  }
1695
1696
0
  return result;
1697
0
}
1698
1699
/*
1700
 * NOTE: Must be RCU read locked!
1701
 */
1702
static void
1703
0
udp_dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) {
1704
0
  REQUIRE(VALID_RESPONSE(resp));
1705
0
  REQUIRE(VALID_DISPATCH(resp->disp));
1706
0
  REQUIRE(VALID_DISPATCHMGR(resp->disp->mgr));
1707
1708
0
  dns_dispatch_t *disp = resp->disp;
1709
0
  bool respond = false;
1710
1711
0
  REQUIRE(disp->tid == isc_tid());
1712
0
  dispentry_log(resp, ISC_LOG_DEBUG(90),
1713
0
          "canceling response: %s, %s/%s (%s/%s), "
1714
0
          "requests %" PRIuFAST32,
1715
0
          isc_result_totext(result), state2str(resp->state),
1716
0
          resp->reading ? "reading" : "not reading",
1717
0
          state2str(disp->state),
1718
0
          disp->reading ? "reading" : "not reading",
1719
0
          disp->requests);
1720
1721
0
  if (ISC_LINK_LINKED(resp, alink)) {
1722
0
    ISC_LIST_UNLINK(disp->active, resp, alink);
1723
0
  }
1724
1725
0
  switch (resp->state) {
1726
0
  case DNS_DISPATCHSTATE_NONE:
1727
0
    break;
1728
1729
0
  case DNS_DISPATCHSTATE_CONNECTING:
1730
0
    break;
1731
1732
0
  case DNS_DISPATCHSTATE_CONNECTED:
1733
0
    if (resp->reading) {
1734
0
      respond = true;
1735
0
      dispentry_log(resp, ISC_LOG_DEBUG(90),
1736
0
              "canceling read on %p", resp->handle);
1737
0
      isc_nm_cancelread(resp->handle);
1738
0
    }
1739
0
    break;
1740
1741
0
  case DNS_DISPATCHSTATE_CANCELED:
1742
0
    goto unlock;
1743
1744
0
  default:
1745
0
    UNREACHABLE();
1746
0
  }
1747
1748
0
  dec_stats(disp->mgr, dns_resstatscounter_disprequdp);
1749
1750
0
  (void)cds_lfht_del(disp->mgr->qids, &resp->ht_node);
1751
1752
0
  resp->state = DNS_DISPATCHSTATE_CANCELED;
1753
1754
0
unlock:
1755
0
  if (respond) {
1756
0
    dispentry_log(resp, ISC_LOG_DEBUG(90), "read callback: %s",
1757
0
            isc_result_totext(result));
1758
0
    resp->response(result, NULL, resp->arg);
1759
0
  }
1760
0
}
1761
1762
/*
1763
 * NOTE: Must be RCU read locked!
1764
 */
1765
static void
1766
0
tcp_dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) {
1767
0
  REQUIRE(VALID_RESPONSE(resp));
1768
0
  REQUIRE(VALID_DISPATCH(resp->disp));
1769
0
  REQUIRE(VALID_DISPATCHMGR(resp->disp->mgr));
1770
1771
0
  dns_dispatch_t *disp = resp->disp;
1772
0
  dns_displist_t resps = ISC_LIST_INITIALIZER;
1773
1774
0
  REQUIRE(disp->tid == isc_tid());
1775
0
  dispentry_log(resp, ISC_LOG_DEBUG(90),
1776
0
          "canceling response: %s, %s/%s (%s/%s), "
1777
0
          "requests %" PRIuFAST32,
1778
0
          isc_result_totext(result), state2str(resp->state),
1779
0
          resp->reading ? "reading" : "not reading",
1780
0
          state2str(disp->state),
1781
0
          disp->reading ? "reading" : "not reading",
1782
0
          disp->requests);
1783
1784
0
  switch (resp->state) {
1785
0
  case DNS_DISPATCHSTATE_NONE:
1786
0
    break;
1787
1788
0
  case DNS_DISPATCHSTATE_CONNECTING:
1789
0
    break;
1790
1791
0
  case DNS_DISPATCHSTATE_CONNECTED:
1792
0
    if (resp->reading) {
1793
      /*
1794
       * This is not a timeout, but we still count it as one
1795
       * so we can later ignore the outstanding answer (if it
1796
       * happens to eventually arrive).
1797
       */
1798
0
      disp->timedout++;
1799
1800
0
      tcp_recv_add(&resps, resp, ISC_R_CANCELED);
1801
0
    }
1802
1803
0
    INSIST(!ISC_LINK_LINKED(resp, alink));
1804
1805
0
    if (ISC_LIST_EMPTY(disp->active) &&
1806
0
        disp->state == DNS_DISPATCHSTATE_CONNECTED)
1807
0
    {
1808
0
      INSIST(disp->handle != NULL);
1809
1810
      /*
1811
       * This was the last outstanding response on a still
1812
       * connected dispatch.  Keep the connection open with a
1813
       * read pending (bounded by the idle timeout) so that it
1814
       * can be reused by a subsequent query and so that a
1815
       * peer-initiated close is noticed promptly instead of
1816
       * leaving the socket stuck in CLOSE-WAIT.  If the
1817
       * dispatch is already shutting down, leave it alone so
1818
       * it can be torn down.
1819
       */
1820
0
      if (disp->mgr->reuse_timeout > 0) {
1821
0
        tcp_startrecv_idle(disp);
1822
0
      } else if (disp->reading) {
1823
0
        dispentry_log(resp, ISC_LOG_DEBUG(90),
1824
0
                "canceling read on %p",
1825
0
                disp->handle);
1826
0
        isc_nm_cancelread(disp->handle);
1827
0
      }
1828
0
    }
1829
0
    break;
1830
1831
0
  case DNS_DISPATCHSTATE_CANCELED:
1832
0
    goto unlock;
1833
1834
0
  default:
1835
0
    UNREACHABLE();
1836
0
  }
1837
1838
0
  dec_stats(disp->mgr, dns_resstatscounter_dispreqtcp);
1839
1840
0
  resp->state = DNS_DISPATCHSTATE_CANCELED;
1841
1842
0
unlock:
1843
1844
  /*
1845
   * NOTE: Calling the response callback directly from here should be done
1846
   * asynchronously, as the dns_dispatch_done() is usually called directly
1847
   * from the response callback, so there's a slight chance that the call
1848
   * stack will get higher here, but it's mitigated by the ".reading"
1849
   * flag, so we don't ever go into a loop.
1850
   */
1851
1852
0
  tcp_recv_processall(&resps, NULL);
1853
0
}
1854
1855
static void
1856
0
dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) {
1857
0
  REQUIRE(VALID_RESPONSE(resp));
1858
0
  REQUIRE(VALID_DISPATCH(resp->disp));
1859
1860
0
  dns_dispatch_t *disp = resp->disp;
1861
1862
0
  rcu_read_lock();
1863
0
  switch (disp->socktype) {
1864
0
  case isc_socktype_udp:
1865
0
    udp_dispentry_cancel(resp, result);
1866
0
    break;
1867
0
  case isc_socktype_tcp:
1868
0
    tcp_dispentry_cancel(resp, result);
1869
0
    break;
1870
0
  default:
1871
0
    UNREACHABLE();
1872
0
  }
1873
0
  rcu_read_unlock();
1874
0
}
1875
1876
void
1877
0
dns_dispatch_done(dns_dispentry_t **respp) {
1878
0
  REQUIRE(VALID_RESPONSE(*respp));
1879
1880
0
  dns_dispentry_t *resp = *respp;
1881
0
  *respp = NULL;
1882
1883
0
  dispentry_cancel(resp, ISC_R_CANCELED);
1884
0
  dns_dispentry_detach(&resp); /* DISPENTRY000 */
1885
0
}
1886
1887
static void
1888
0
udp_startrecv(isc_nmhandle_t *handle, dns_dispentry_t *resp) {
1889
0
  REQUIRE(VALID_RESPONSE(resp));
1890
1891
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "attaching handle %p to %p",
1892
0
          handle, &resp->handle);
1893
0
  isc_nmhandle_attach(handle, &resp->handle);
1894
0
  dns_dispentry_ref(resp); /* DISPENTRY003 */
1895
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "reading");
1896
0
  isc_nm_read(resp->handle, udp_recv, resp);
1897
0
  resp->reading = true;
1898
0
}
1899
1900
static void
1901
0
tcp_startrecv(dns_dispatch_t *disp, dns_dispentry_t *resp) {
1902
0
  REQUIRE(VALID_DISPATCH(disp));
1903
0
  REQUIRE(disp->socktype == isc_socktype_tcp);
1904
1905
0
  if (disp->reading) {
1906
0
    return;
1907
0
  }
1908
1909
0
  dns_dispatch_ref(disp); /* DISPATCH002 */
1910
0
  if (resp != NULL) {
1911
0
    dispentry_log(resp, ISC_LOG_DEBUG(90), "reading from %p",
1912
0
            disp->handle);
1913
0
    INSIST(!isc_time_isepoch(&resp->start));
1914
0
  } else {
1915
0
    dispatch_log(disp, ISC_LOG_DEBUG(90),
1916
0
           "TCP reading without response from %p",
1917
0
           disp->handle);
1918
0
  }
1919
0
  isc_nm_read(disp->handle, tcp_recv, disp);
1920
0
  disp->reading = true;
1921
0
}
1922
1923
static void
1924
0
resp_connected(void *arg) {
1925
0
  dns_dispentry_t *resp = arg;
1926
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "connect callback: %s",
1927
0
          isc_result_totext(resp->result));
1928
1929
0
  resp->connected(resp->result, NULL, resp->arg);
1930
0
  dns_dispentry_detach(&resp); /* DISPENTRY005 */
1931
0
}
1932
1933
static void
1934
0
tcp_connected(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
1935
0
  dns_dispatch_t *disp = (dns_dispatch_t *)arg;
1936
0
  dns_displist_t resps = ISC_LIST_INITIALIZER;
1937
1938
0
  if (isc_log_wouldlog(90)) {
1939
0
    char localbuf[ISC_SOCKADDR_FORMATSIZE];
1940
0
    char peerbuf[ISC_SOCKADDR_FORMATSIZE];
1941
0
    if (handle != NULL) {
1942
0
      isc_sockaddr_t local = isc_nmhandle_localaddr(handle);
1943
0
      isc_sockaddr_t peer = isc_nmhandle_peeraddr(handle);
1944
1945
0
      isc_sockaddr_format(&local, localbuf,
1946
0
              ISC_SOCKADDR_FORMATSIZE);
1947
0
      isc_sockaddr_format(&peer, peerbuf,
1948
0
              ISC_SOCKADDR_FORMATSIZE);
1949
0
    } else {
1950
0
      isc_sockaddr_format(&disp->local, localbuf,
1951
0
              ISC_SOCKADDR_FORMATSIZE);
1952
0
      isc_sockaddr_format(&disp->peer, peerbuf,
1953
0
              ISC_SOCKADDR_FORMATSIZE);
1954
0
    }
1955
1956
0
    dispatch_log(disp, ISC_LOG_DEBUG(90),
1957
0
           "connected from %s to %s: %s", localbuf, peerbuf,
1958
0
           isc_result_totext(eresult));
1959
0
  }
1960
1961
0
  REQUIRE(disp->tid == isc_tid());
1962
0
  INSIST(disp->state == DNS_DISPATCHSTATE_CONNECTING);
1963
1964
  /*
1965
   * If there are pending responses, call the connect
1966
   * callbacks for all of them.
1967
   */
1968
0
  ISC_LIST_FOREACH(disp->pending, resp, plink) {
1969
0
    ISC_LIST_UNLINK(disp->pending, resp, plink);
1970
0
    ISC_LIST_APPEND(resps, resp, rlink);
1971
0
    resp->result = eresult;
1972
1973
0
    if (resp->state == DNS_DISPATCHSTATE_CANCELED) {
1974
0
      resp->result = ISC_R_CANCELED;
1975
0
    } else if (eresult == ISC_R_SUCCESS) {
1976
0
      resp->state = DNS_DISPATCHSTATE_CONNECTED;
1977
0
      ISC_LIST_APPEND(disp->active, resp, alink);
1978
0
      resp->reading = true;
1979
0
      dispentry_log(resp, ISC_LOG_DEBUG(90), "start reading");
1980
0
    } else {
1981
0
      resp->state = DNS_DISPATCHSTATE_NONE;
1982
0
    }
1983
0
  }
1984
1985
  /* Take the oldest active response. */
1986
0
  dns_dispentry_t *oldest = ISC_LIST_HEAD(disp->active);
1987
0
  if (oldest == NULL) {
1988
    /* All responses have been canceled */
1989
0
    disp->state = DNS_DISPATCHSTATE_CANCELED;
1990
0
  } else if (eresult == ISC_R_SUCCESS) {
1991
0
    disp->state = DNS_DISPATCHSTATE_CONNECTED;
1992
0
    isc_nmhandle_attach(handle, &disp->handle);
1993
0
    isc_nmhandle_cleartimeout(disp->handle);
1994
0
    if (oldest->timeout != 0) {
1995
0
      isc_nmhandle_settimeout(disp->handle, oldest->timeout);
1996
0
    }
1997
0
    tcp_startrecv(disp, oldest);
1998
0
  } else {
1999
0
    disp->state = DNS_DISPATCHSTATE_NONE;
2000
0
  }
2001
2002
0
  ISC_LIST_FOREACH(resps, resp, rlink) {
2003
0
    ISC_LIST_UNLINK(resps, resp, rlink);
2004
2005
0
    resp_connected(resp);
2006
0
  }
2007
2008
0
  dns_dispatch_detach(&disp); /* DISPATCH003 */
2009
0
}
2010
2011
static void
2012
0
udp_connected(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
2013
0
  dns_dispentry_t *resp = (dns_dispentry_t *)arg;
2014
0
  dns_dispatch_t *disp = resp->disp;
2015
2016
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "connected: %s",
2017
0
          isc_result_totext(eresult));
2018
2019
0
  REQUIRE(disp->tid == isc_tid());
2020
0
  switch (resp->state) {
2021
0
  case DNS_DISPATCHSTATE_CANCELED:
2022
0
    eresult = ISC_R_CANCELED;
2023
0
    ISC_LIST_UNLINK(disp->pending, resp, plink);
2024
0
    goto unlock;
2025
0
  case DNS_DISPATCHSTATE_CONNECTING:
2026
0
    ISC_LIST_UNLINK(disp->pending, resp, plink);
2027
0
    break;
2028
0
  default:
2029
0
    UNREACHABLE();
2030
0
  }
2031
2032
0
  switch (eresult) {
2033
0
  case ISC_R_CANCELED:
2034
0
    break;
2035
0
  case ISC_R_SUCCESS:
2036
0
    resp->state = DNS_DISPATCHSTATE_CONNECTED;
2037
0
    udp_startrecv(handle, resp);
2038
0
    break;
2039
0
  case ISC_R_NOPERM:
2040
0
  case ISC_R_ADDRINUSE: {
2041
0
    in_port_t localport = isc_sockaddr_getport(&disp->local);
2042
0
    isc_result_t result;
2043
2044
    /* probably a port collision; try a different one */
2045
0
    result = setup_socket(disp, resp, &resp->peer, &localport);
2046
0
    if (result == ISC_R_SUCCESS) {
2047
0
      udp_dispatch_connect(disp, resp);
2048
0
      goto detach;
2049
0
    }
2050
0
    resp->state = DNS_DISPATCHSTATE_NONE;
2051
0
    break;
2052
0
  }
2053
0
  default:
2054
0
    resp->state = DNS_DISPATCHSTATE_NONE;
2055
0
    break;
2056
0
  }
2057
0
unlock:
2058
2059
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "connect callback: %s",
2060
0
          isc_result_totext(eresult));
2061
0
  resp->connected(eresult, NULL, resp->arg);
2062
2063
0
detach:
2064
0
  dns_dispentry_detach(&resp); /* DISPENTRY004 */
2065
0
}
2066
2067
static void
2068
0
udp_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp) {
2069
0
  REQUIRE(disp->tid == isc_tid());
2070
0
  resp->state = DNS_DISPATCHSTATE_CONNECTING;
2071
0
  resp->start = isc_loop_now(resp->loop);
2072
0
  dns_dispentry_ref(resp); /* DISPENTRY004 */
2073
0
  ISC_LIST_APPEND(disp->pending, resp, plink);
2074
2075
0
  isc_nm_udpconnect(&resp->local, &resp->peer, udp_connected, resp,
2076
0
        resp->timeout);
2077
0
}
2078
2079
static inline const char *
2080
0
get_tls_sni_hostname(dns_dispentry_t *resp) {
2081
0
  char *hostname = NULL;
2082
2083
0
  if (resp->transport != NULL) {
2084
0
    hostname = dns_transport_get_remote_hostname(resp->transport);
2085
0
  }
2086
2087
0
  if (hostname == NULL) {
2088
0
    return NULL;
2089
0
  }
2090
2091
0
  if (isc_tls_valid_sni_hostname(hostname)) {
2092
0
    return hostname;
2093
0
  }
2094
2095
0
  return NULL;
2096
0
}
2097
2098
static isc_result_t
2099
0
tcp_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp) {
2100
0
  dns_transport_type_t transport_type = DNS_TRANSPORT_TCP;
2101
0
  isc_tlsctx_t *tlsctx = NULL;
2102
0
  isc_tlsctx_client_session_cache_t *sess_cache = NULL;
2103
2104
0
  if (resp->transport != NULL) {
2105
0
    transport_type = dns_transport_get_type(resp->transport);
2106
0
  }
2107
2108
0
  if (transport_type == DNS_TRANSPORT_TLS) {
2109
0
    RETERR(dns_transport_get_tlsctx(resp->transport, &resp->peer,
2110
0
            resp->tlsctx_cache, resp->mctx,
2111
0
            &tlsctx, &sess_cache));
2112
0
    INSIST(tlsctx != NULL);
2113
0
  }
2114
2115
  /* Check whether the dispatch is already connecting or connected. */
2116
0
  REQUIRE(disp->tid == isc_tid());
2117
0
  switch (disp->state) {
2118
0
  case DNS_DISPATCHSTATE_NONE:
2119
    /* First connection, continue with connecting */
2120
0
    disp->state = DNS_DISPATCHSTATE_CONNECTING;
2121
0
    resp->state = DNS_DISPATCHSTATE_CONNECTING;
2122
0
    resp->start = isc_loop_now(resp->loop);
2123
0
    dns_dispentry_ref(resp); /* DISPENTRY005 */
2124
0
    ISC_LIST_APPEND(disp->pending, resp, plink);
2125
2126
0
    char localbuf[ISC_SOCKADDR_FORMATSIZE];
2127
0
    char peerbuf[ISC_SOCKADDR_FORMATSIZE];
2128
2129
0
    isc_sockaddr_format(&disp->local, localbuf,
2130
0
            ISC_SOCKADDR_FORMATSIZE);
2131
0
    isc_sockaddr_format(&disp->peer, peerbuf,
2132
0
            ISC_SOCKADDR_FORMATSIZE);
2133
2134
0
    dns_dispatch_ref(disp); /* DISPATCH003 */
2135
0
    dispentry_log(resp, ISC_LOG_DEBUG(90),
2136
0
            "connecting from %s to %s, timeout %u", localbuf,
2137
0
            peerbuf, resp->connect_timeout);
2138
2139
0
    const char *hostname = get_tls_sni_hostname(resp);
2140
2141
0
    isc_nm_streamdnsconnect(&disp->local, &disp->peer,
2142
0
          tcp_connected, disp,
2143
0
          resp->connect_timeout, tlsctx, hostname,
2144
0
          sess_cache, ISC_NM_PROXY_NONE, NULL);
2145
0
    break;
2146
2147
0
  case DNS_DISPATCHSTATE_CONNECTING:
2148
    /* Connection pending; add resp to the list */
2149
0
    resp->state = DNS_DISPATCHSTATE_CONNECTING;
2150
0
    resp->start = isc_loop_now(resp->loop);
2151
0
    dns_dispentry_ref(resp); /* DISPENTRY005 */
2152
0
    ISC_LIST_APPEND(disp->pending, resp, plink);
2153
0
    break;
2154
2155
0
  case DNS_DISPATCHSTATE_CONNECTED:
2156
0
    resp->state = DNS_DISPATCHSTATE_CONNECTED;
2157
0
    resp->start = isc_loop_now(resp->loop);
2158
2159
    /* Add the resp to the reading list */
2160
0
    ISC_LIST_APPEND(disp->active, resp, alink);
2161
0
    dispentry_log(resp, ISC_LOG_DEBUG(90),
2162
0
            "already connected; attaching");
2163
0
    resp->reading = true;
2164
2165
    /*
2166
     * Replace any idle timeout with this query's timeout.  The
2167
     * connection may have been kept reading while idle (with the
2168
     * idle timeout applied), so update the handle timeout even when
2169
     * a read is already pending.
2170
     */
2171
0
    isc_nmhandle_cleartimeout(disp->handle);
2172
0
    if (resp->timeout != 0) {
2173
0
      isc_nmhandle_settimeout(disp->handle, resp->timeout);
2174
0
    }
2175
2176
0
    if (!disp->reading) {
2177
      /* Restart the reading */
2178
0
      tcp_startrecv(disp, resp);
2179
0
    }
2180
2181
    /* Already connected; call the connected cb asynchronously */
2182
0
    dns_dispentry_ref(resp); /* DISPENTRY005 */
2183
0
    isc_async_run(resp->loop, resp_connected, resp);
2184
0
    break;
2185
2186
0
  default:
2187
0
    UNREACHABLE();
2188
0
  }
2189
2190
0
  return ISC_R_SUCCESS;
2191
0
}
2192
2193
isc_result_t
2194
0
dns_dispatch_connect(dns_dispentry_t *resp) {
2195
0
  REQUIRE(VALID_RESPONSE(resp));
2196
0
  REQUIRE(VALID_DISPATCH(resp->disp));
2197
2198
0
  dns_dispatch_t *disp = resp->disp;
2199
2200
0
  switch (disp->socktype) {
2201
0
  case isc_socktype_tcp:
2202
0
    return tcp_dispatch_connect(disp, resp);
2203
2204
0
  case isc_socktype_udp:
2205
0
    udp_dispatch_connect(disp, resp);
2206
0
    return ISC_R_SUCCESS;
2207
2208
0
  default:
2209
0
    UNREACHABLE();
2210
0
  }
2211
0
}
2212
2213
static void
2214
0
send_done(isc_nmhandle_t *handle, isc_result_t result, void *cbarg) {
2215
0
  dns_dispentry_t *resp = (dns_dispentry_t *)cbarg;
2216
2217
0
  REQUIRE(VALID_RESPONSE(resp));
2218
2219
0
  dns_dispatch_t *disp = resp->disp;
2220
2221
0
  REQUIRE(VALID_DISPATCH(disp));
2222
2223
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "sent: %s",
2224
0
          isc_result_totext(result));
2225
2226
0
  resp->sent(result, NULL, resp->arg);
2227
2228
0
  if (result != ISC_R_SUCCESS) {
2229
0
    dispentry_cancel(resp, result);
2230
0
  }
2231
2232
0
  dns_dispentry_detach(&resp); /* DISPENTRY007 */
2233
0
  isc_nmhandle_detach(&handle);
2234
0
}
2235
2236
static void
2237
tcp_dispatch_getnext(dns_dispatch_t *disp, dns_dispentry_t *resp,
2238
0
         int64_t timeout) {
2239
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "continue reading");
2240
2241
0
  if (!resp->reading) {
2242
0
    ISC_LIST_APPEND(disp->active, resp, alink);
2243
0
    resp->reading = true;
2244
0
  }
2245
2246
0
  if (disp->reading) {
2247
0
    return;
2248
0
  }
2249
2250
0
  if (timeout != 0) {
2251
0
    INSIST(timeout > 0 && timeout <= UINT32_MAX);
2252
0
    isc_nmhandle_settimeout(disp->handle, timeout);
2253
0
  }
2254
2255
0
  dns_dispatch_ref(disp); /* DISPATCH002 */
2256
0
  isc_nm_read(disp->handle, tcp_recv, disp);
2257
0
  disp->reading = true;
2258
0
}
2259
2260
static void
2261
0
udp_dispatch_getnext(dns_dispentry_t *resp, int64_t timeout) {
2262
0
  if (resp->reading) {
2263
0
    return;
2264
0
  }
2265
2266
0
  if (timeout != 0) {
2267
0
    INSIST(timeout > 0 && timeout <= UINT32_MAX);
2268
0
    isc_nmhandle_settimeout(resp->handle, timeout);
2269
0
  }
2270
2271
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "continue reading");
2272
2273
0
  dns_dispentry_ref(resp); /* DISPENTRY003 */
2274
0
  isc_nm_read(resp->handle, udp_recv, resp);
2275
0
  resp->reading = true;
2276
0
}
2277
2278
void
2279
0
dns_dispatch_resume(dns_dispentry_t *resp, unsigned int timeout) {
2280
0
  REQUIRE(VALID_RESPONSE(resp));
2281
0
  REQUIRE(VALID_DISPATCH(resp->disp));
2282
2283
0
  dns_dispatch_t *disp = resp->disp;
2284
2285
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "resume");
2286
2287
0
  REQUIRE(disp->tid == isc_tid());
2288
0
  switch (disp->socktype) {
2289
0
  case isc_socktype_udp: {
2290
0
    udp_dispatch_getnext(resp, timeout);
2291
0
    break;
2292
0
  }
2293
0
  case isc_socktype_tcp:
2294
0
    INSIST(disp->timedout > 0);
2295
0
    disp->timedout--;
2296
0
    tcp_dispatch_getnext(disp, resp, timeout);
2297
0
    break;
2298
0
  default:
2299
0
    UNREACHABLE();
2300
0
  }
2301
0
}
2302
2303
void
2304
0
dns_dispatch_send(dns_dispentry_t *resp, isc_region_t *r) {
2305
0
  REQUIRE(VALID_RESPONSE(resp));
2306
0
  REQUIRE(VALID_DISPATCH(resp->disp));
2307
2308
0
  dns_dispatch_t *disp = resp->disp;
2309
0
  isc_nmhandle_t *sendhandle = NULL;
2310
2311
0
  dispentry_log(resp, ISC_LOG_DEBUG(90), "sending");
2312
0
  switch (disp->socktype) {
2313
0
  case isc_socktype_udp:
2314
0
    isc_nmhandle_attach(resp->handle, &sendhandle);
2315
0
    break;
2316
0
  case isc_socktype_tcp:
2317
0
    isc_nmhandle_attach(disp->handle, &sendhandle);
2318
0
    break;
2319
0
  default:
2320
0
    UNREACHABLE();
2321
0
  }
2322
0
  dns_dispentry_ref(resp); /* DISPENTRY007 */
2323
0
  isc_nm_send(sendhandle, r, send_done, resp);
2324
0
}
2325
2326
isc_result_t
2327
0
dns_dispatch_getlocaladdress(dns_dispatch_t *disp, isc_sockaddr_t *addrp) {
2328
0
  REQUIRE(VALID_DISPATCH(disp));
2329
0
  REQUIRE(addrp != NULL);
2330
2331
0
  if (disp->socktype == isc_socktype_udp) {
2332
0
    *addrp = disp->local;
2333
0
    return ISC_R_SUCCESS;
2334
0
  }
2335
0
  return ISC_R_NOTIMPLEMENTED;
2336
0
}
2337
2338
isc_result_t
2339
0
dns_dispentry_getlocaladdress(dns_dispentry_t *resp, isc_sockaddr_t *addrp) {
2340
0
  REQUIRE(VALID_RESPONSE(resp));
2341
0
  REQUIRE(VALID_DISPATCH(resp->disp));
2342
0
  REQUIRE(addrp != NULL);
2343
2344
0
  dns_dispatch_t *disp = resp->disp;
2345
2346
0
  switch (disp->socktype) {
2347
0
  case isc_socktype_tcp:
2348
0
    *addrp = isc_nmhandle_localaddr(disp->handle);
2349
0
    return ISC_R_SUCCESS;
2350
0
  case isc_socktype_udp:
2351
0
    *addrp = isc_nmhandle_localaddr(resp->handle);
2352
0
    return ISC_R_SUCCESS;
2353
0
  default:
2354
0
    UNREACHABLE();
2355
0
  }
2356
0
}
2357
2358
dns_dispatch_t *
2359
0
dns_dispatchset_get(dns_dispatchset_t *dset) {
2360
0
  isc_tid_t tid = isc_tid();
2361
2362
  /* check that dispatch set is configured */
2363
0
  if (dset == NULL || dset->ndisp == 0) {
2364
0
    return NULL;
2365
0
  }
2366
2367
0
  INSIST((uint32_t)tid < dset->ndisp);
2368
2369
0
  return dset->dispatches[tid];
2370
0
}
2371
2372
isc_result_t
2373
dns_dispatchset_create(isc_mem_t *mctx, dns_dispatch_t *source,
2374
0
           dns_dispatchset_t **dsetp, uint32_t ndisp) {
2375
0
  isc_result_t result;
2376
0
  dns_dispatchset_t *dset = NULL;
2377
0
  dns_dispatchmgr_t *mgr = NULL;
2378
0
  size_t i;
2379
2380
0
  REQUIRE(VALID_DISPATCH(source));
2381
0
  REQUIRE(source->socktype == isc_socktype_udp);
2382
0
  REQUIRE(dsetp != NULL && *dsetp == NULL);
2383
2384
0
  mgr = source->mgr;
2385
2386
0
  dset = isc_mem_get(mctx, sizeof(dns_dispatchset_t));
2387
0
  *dset = (dns_dispatchset_t){ .ndisp = ndisp };
2388
2389
0
  isc_mem_attach(mctx, &dset->mctx);
2390
2391
0
  dset->dispatches = isc_mem_cget(dset->mctx, ndisp,
2392
0
          sizeof(dns_dispatch_t *));
2393
2394
0
  dset->dispatches[0] = NULL;
2395
0
  dns_dispatch_attach(source, &dset->dispatches[0]); /* DISPATCH004 */
2396
2397
0
  for (i = 1; i < dset->ndisp; i++) {
2398
0
    result = dispatch_createudp(mgr, &source->local, i,
2399
0
              &dset->dispatches[i]);
2400
0
    if (result != ISC_R_SUCCESS) {
2401
0
      goto fail;
2402
0
    }
2403
0
  }
2404
2405
0
  *dsetp = dset;
2406
2407
0
  return ISC_R_SUCCESS;
2408
2409
0
fail:
2410
0
  for (size_t j = 0; j < i; j++) {
2411
0
    dns_dispatch_detach(&(dset->dispatches[j])); /* DISPATCH004 */
2412
0
  }
2413
0
  isc_mem_cput(dset->mctx, dset->dispatches, ndisp,
2414
0
         sizeof(dns_dispatch_t *));
2415
2416
0
  isc_mem_putanddetach(&dset->mctx, dset, sizeof(dns_dispatchset_t));
2417
0
  return result;
2418
0
}
2419
2420
void
2421
0
dns_dispatchset_destroy(dns_dispatchset_t **dsetp) {
2422
0
  REQUIRE(dsetp != NULL && *dsetp != NULL);
2423
2424
0
  dns_dispatchset_t *dset = *dsetp;
2425
0
  *dsetp = NULL;
2426
2427
0
  for (size_t i = 0; i < dset->ndisp; i++) {
2428
0
    dns_dispatch_detach(&(dset->dispatches[i])); /* DISPATCH004 */
2429
0
  }
2430
0
  isc_mem_cput(dset->mctx, dset->dispatches, dset->ndisp,
2431
0
         sizeof(dns_dispatch_t *));
2432
0
  isc_mem_putanddetach(&dset->mctx, dset, sizeof(dns_dispatchset_t));
2433
0
}
2434
2435
isc_result_t
2436
0
dns_dispatch_checkperm(dns_dispatch_t *disp) {
2437
0
  REQUIRE(VALID_DISPATCH(disp));
2438
2439
0
  if (disp->handle == NULL || disp->socktype == isc_socktype_udp) {
2440
0
    return ISC_R_NOPERM;
2441
0
  }
2442
2443
0
  return isc_nm_xfr_checkperm(disp->handle);
2444
0
}