request.c 40 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004-2012  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2002  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Mark Andrews's avatar
Mark Andrews committed
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Mark Andrews's avatar
Mark Andrews committed
16 17
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id$ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

Mark Andrews's avatar
Mark Andrews committed
22 23
#include <config.h>

24
#include <isc/magic.h>
25
#include <isc/mem.h>
Mark Andrews's avatar
Mark Andrews committed
26 27 28 29
#include <isc/task.h>
#include <isc/timer.h>
#include <isc/util.h>

30
#include <dns/acl.h>
31
#include <dns/compress.h>
Mark Andrews's avatar
Mark Andrews committed
32 33
#include <dns/dispatch.h>
#include <dns/events.h>
34
#include <dns/log.h>
Mark Andrews's avatar
Mark Andrews committed
35
#include <dns/message.h>
36 37
#include <dns/rdata.h>
#include <dns/rdatastruct.h>
Mark Andrews's avatar
Mark Andrews committed
38
#include <dns/request.h>
39
#include <dns/result.h>
40
#include <dns/tsig.h>
Mark Andrews's avatar
Mark Andrews committed
41

42 43
#define REQUESTMGR_MAGIC	ISC_MAGIC('R', 'q', 'u', 'M')
#define VALID_REQUESTMGR(mgr)	ISC_MAGIC_VALID(mgr, REQUESTMGR_MAGIC)
Mark Andrews's avatar
Mark Andrews committed
44

45 46
#define REQUEST_MAGIC		ISC_MAGIC('R', 'q', 'u', '!')
#define VALID_REQUEST(request)	ISC_MAGIC_VALID(request, REQUEST_MAGIC)
Mark Andrews's avatar
Mark Andrews committed
47

Mark Andrews's avatar
Mark Andrews committed
48 49
typedef ISC_LIST(dns_request_t) dns_requestlist_t;

Mark Andrews's avatar
Mark Andrews committed
50 51
#define DNS_REQUEST_NLOCKS 7

Mark Andrews's avatar
Mark Andrews committed
52
struct dns_requestmgr {
53
	unsigned int			magic;
54 55
	isc_mutex_t			lock;
	isc_mem_t		       *mctx;
Mark Andrews's avatar
Mark Andrews committed
56 57

	/* locked */
58 59 60 61
	isc_int32_t			eref;
	isc_int32_t			iref;
	isc_timermgr_t		       *timermgr;
	isc_socketmgr_t		       *socketmgr;
62
	isc_taskmgr_t		       *taskmgr;
63 64 65 66 67 68 69 70
	dns_dispatchmgr_t	       *dispatchmgr;
	dns_dispatch_t		       *dispatchv4;
	dns_dispatch_t		       *dispatchv6;
	isc_boolean_t			exiting;
	isc_eventlist_t			whenshutdown;
	unsigned int			hash;
	isc_mutex_t			locks[DNS_REQUEST_NLOCKS];
	dns_requestlist_t 		requests;
Mark Andrews's avatar
Mark Andrews committed
71 72 73
};

struct dns_request {
74
	unsigned int			magic;
75 76 77 78 79 80 81 82 83 84 85
	unsigned int			hash;
	isc_mem_t		       *mctx;
	isc_int32_t			flags;
	ISC_LINK(dns_request_t) 	link;
	isc_buffer_t		       *query;
	isc_buffer_t		       *answer;
	dns_requestevent_t	       *event;
	dns_dispatch_t		       *dispatch;
	dns_dispentry_t		       *dispentry;
	isc_timer_t		       *timer;
	dns_requestmgr_t	       *requestmgr;
86
	isc_buffer_t		       *tsig;
87
	dns_tsigkey_t		       *tsigkey;
88 89
	isc_event_t			ctlevent;
	isc_boolean_t			canceling; /* ctlevent outstanding */
90
	isc_sockaddr_t			destaddr;
Michael Graff's avatar
Michael Graff committed
91
	unsigned int			udpcount;
Mark Andrews's avatar
Mark Andrews committed
92
};
Mark Andrews's avatar
Mark Andrews committed
93

Mark Andrews's avatar
Mark Andrews committed
94
#define DNS_REQUEST_F_CONNECTING 0x0001
95
#define DNS_REQUEST_F_SENDING 0x0002
96
#define DNS_REQUEST_F_CANCELED 0x0004	/*%< ctlevent received, or otherwise
97
					   synchronously canceled */
Francis Dupont's avatar
Francis Dupont committed
98
#define DNS_REQUEST_F_TIMEDOUT 0x0008	/*%< canceled due to a timeout */
99
#define DNS_REQUEST_F_TCP 0x0010	/*%< This request used TCP */
Mark Andrews's avatar
Mark Andrews committed
100 101 102 103
#define DNS_REQUEST_CANCELED(r) \
	(((r)->flags & DNS_REQUEST_F_CANCELED) != 0)
#define DNS_REQUEST_CONNECTING(r) \
	(((r)->flags & DNS_REQUEST_F_CONNECTING) != 0)
104 105
#define DNS_REQUEST_SENDING(r) \
	(((r)->flags & DNS_REQUEST_F_SENDING) != 0)
106 107
#define DNS_REQUEST_TIMEDOUT(r) \
	(((r)->flags & DNS_REQUEST_F_TIMEDOUT) != 0)
108

Mark Andrews's avatar
Mark Andrews committed
109

Mark Andrews's avatar
Mark Andrews committed
110 111 112 113
/***
 *** Forward
 ***/

Mark Andrews's avatar
Mark Andrews committed
114 115
static void mgr_destroy(dns_requestmgr_t *requestmgr);
static void mgr_shutdown(dns_requestmgr_t *requestmgr);
Mark Andrews's avatar
Mark Andrews committed
116
static unsigned int mgr_gethash(dns_requestmgr_t *requestmgr);
Mark Andrews's avatar
Mark Andrews committed
117
static void send_shutdown_events(dns_requestmgr_t *requestmgr);
Mark Andrews's avatar
Mark Andrews committed
118

119
static isc_result_t req_render(dns_message_t *message, isc_buffer_t **buffer,
120
			       unsigned int options, isc_mem_t *mctx);
Mark Andrews's avatar
Mark Andrews committed
121 122 123
static void req_senddone(isc_task_t *task, isc_event_t *event);
static void req_response(isc_task_t *task, isc_event_t *event);
static void req_timeout(isc_task_t *task, isc_event_t *event);
124
static isc_socket_t * req_getsocket(dns_request_t *request);
Mark Andrews's avatar
Mark Andrews committed
125 126 127 128
static void req_connected(isc_task_t *task, isc_event_t *event);
static void req_sendevent(dns_request_t *request, isc_result_t result);
static void req_cancel(dns_request_t *request);
static void req_destroy(dns_request_t *request);
129
static void req_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
130
static void do_cancel(isc_task_t *task, isc_event_t *event);
Mark Andrews's avatar
Mark Andrews committed
131 132 133 134 135 136

/***
 *** Public
 ***/

isc_result_t
Mark Andrews's avatar
Mark Andrews committed
137 138 139
dns_requestmgr_create(isc_mem_t *mctx,
		      isc_timermgr_t *timermgr,
		      isc_socketmgr_t *socketmgr,
140
		      isc_taskmgr_t *taskmgr,
141
		      dns_dispatchmgr_t *dispatchmgr,
Mark Andrews's avatar
Mark Andrews committed
142 143 144 145
		      dns_dispatch_t *dispatchv4,
		      dns_dispatch_t *dispatchv6,
		      dns_requestmgr_t **requestmgrp)
{
Mark Andrews's avatar
Mark Andrews committed
146
	dns_requestmgr_t *requestmgr;
Mark Andrews's avatar
Mark Andrews committed
147 148
	isc_socket_t *socket;
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
149
	int i;
150
	unsigned int dispattr;
Mark Andrews's avatar
Mark Andrews committed
151

152
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create");
153

Mark Andrews's avatar
Mark Andrews committed
154
	REQUIRE(requestmgrp != NULL && *requestmgrp == NULL);
Mark Andrews's avatar
Mark Andrews committed
155 156
	REQUIRE(timermgr != NULL);
	REQUIRE(socketmgr != NULL);
157
	REQUIRE(taskmgr != NULL);
158
	REQUIRE(dispatchmgr != NULL);
159
	UNUSED(socket);
Mark Andrews's avatar
Mark Andrews committed
160
	if (dispatchv4 != NULL) {
161 162
		dispattr = dns_dispatch_getattributes(dispatchv4);
		REQUIRE((dispattr & DNS_DISPATCHATTR_UDP) != 0);
Mark Andrews's avatar
Mark Andrews committed
163 164
	}
	if (dispatchv6 != NULL) {
165 166
		dispattr = dns_dispatch_getattributes(dispatchv6);
		REQUIRE((dispattr & DNS_DISPATCHATTR_UDP) != 0);
Mark Andrews's avatar
Mark Andrews committed
167 168 169 170 171 172
	}

	requestmgr = isc_mem_get(mctx, sizeof(*requestmgr));
	if (requestmgr == NULL)
		return (ISC_R_NOMEMORY);

Mark Andrews's avatar
Mark Andrews committed
173
	result = isc_mutex_init(&requestmgr->lock);
174
	if (result != ISC_R_SUCCESS) {
Mark Andrews's avatar
Mark Andrews committed
175 176 177
		isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
		return (result);
	}
Mark Andrews's avatar
Mark Andrews committed
178 179
	for (i = 0; i < DNS_REQUEST_NLOCKS; i++) {
		result = isc_mutex_init(&requestmgr->locks[i]);
180
		if (result != ISC_R_SUCCESS) {
Mark Andrews's avatar
Mark Andrews committed
181
			while (--i >= 0)
182 183
				DESTROYLOCK(&requestmgr->locks[i]);
			DESTROYLOCK(&requestmgr->lock);
184
			isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
Mark Andrews's avatar
Mark Andrews committed
185 186 187
			return (result);
		}
	}
Mark Andrews's avatar
Mark Andrews committed
188
	requestmgr->timermgr = timermgr;
Mark Andrews's avatar
Mark Andrews committed
189
	requestmgr->socketmgr = socketmgr;
190
	requestmgr->taskmgr = taskmgr;
191
	requestmgr->dispatchmgr = dispatchmgr;
Mark Andrews's avatar
Mark Andrews committed
192
	requestmgr->dispatchv4 = NULL;
Mark Andrews's avatar
Mark Andrews committed
193 194
	if (dispatchv4 != NULL)
		dns_dispatch_attach(dispatchv4, &requestmgr->dispatchv4);
Mark Andrews's avatar
Mark Andrews committed
195 196
	requestmgr->dispatchv6 = NULL;
	if (dispatchv6 != NULL)
Mark Andrews's avatar
Mark Andrews committed
197
		dns_dispatch_attach(dispatchv6, &requestmgr->dispatchv6);
198 199
	requestmgr->mctx = NULL;
	isc_mem_attach(mctx, &requestmgr->mctx);
Francis Dupont's avatar
Francis Dupont committed
200
	requestmgr->eref = 1;	/* implicit attach */
201
	requestmgr->iref = 0;
Mark Andrews's avatar
Mark Andrews committed
202
	ISC_LIST_INIT(requestmgr->whenshutdown);
Mark Andrews's avatar
Mark Andrews committed
203
	ISC_LIST_INIT(requestmgr->requests);
Mark Andrews's avatar
Mark Andrews committed
204
	requestmgr->exiting = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
205
	requestmgr->hash = 0;
Mark Andrews's avatar
Mark Andrews committed
206 207
	requestmgr->magic = REQUESTMGR_MAGIC;

208 209 210
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create: %p", requestmgr);

	*requestmgrp = requestmgr;
Mark Andrews's avatar
Mark Andrews committed
211 212 213 214 215 216 217
	return (ISC_R_SUCCESS);
}

void
dns_requestmgr_whenshutdown(dns_requestmgr_t *requestmgr, isc_task_t *task,
			    isc_event_t **eventp)
{
Brian Wellington's avatar
Brian Wellington committed
218 219
	isc_task_t *clone;
	isc_event_t *event;
Mark Andrews's avatar
Mark Andrews committed
220

221
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_whenshutdown");
222

Brian Wellington's avatar
Brian Wellington committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	REQUIRE(VALID_REQUESTMGR(requestmgr));
	REQUIRE(eventp != NULL);

	event = *eventp;
	*eventp = NULL;

	LOCK(&requestmgr->lock);

	if (requestmgr->exiting) {
		/*
		 * We're already shutdown.  Send the event.
		 */
		event->ev_sender = requestmgr;
		isc_task_send(task, &event);
	} else {
		clone = NULL;
		isc_task_attach(task, &clone);
		event->ev_sender = clone;
		ISC_LIST_APPEND(requestmgr->whenshutdown, event, ev_link);
Mark Andrews's avatar
Mark Andrews committed
242 243 244 245 246 247 248
	}
	UNLOCK(&requestmgr->lock);
}

void
dns_requestmgr_shutdown(dns_requestmgr_t *requestmgr) {

Brian Wellington's avatar
Brian Wellington committed
249
	REQUIRE(VALID_REQUESTMGR(requestmgr));
Mark Andrews's avatar
Mark Andrews committed
250

251
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_shutdown: %p", requestmgr);
252

Mark Andrews's avatar
Mark Andrews committed
253
	LOCK(&requestmgr->lock);
Mark Andrews's avatar
Mark Andrews committed
254 255 256 257
	mgr_shutdown(requestmgr);
	UNLOCK(&requestmgr->lock);
}

258
static void
Mark Andrews's avatar
Mark Andrews committed
259 260
mgr_shutdown(dns_requestmgr_t *requestmgr) {
	dns_request_t *request;
261

Mark Andrews's avatar
Mark Andrews committed
262 263 264
	/*
	 * Caller holds lock.
	 */
Mark Andrews's avatar
Mark Andrews committed
265 266
	if (!requestmgr->exiting) {
		requestmgr->exiting = ISC_TRUE;
Mark Andrews's avatar
Mark Andrews committed
267 268 269 270 271
		for (request = ISC_LIST_HEAD(requestmgr->requests);
		     request != NULL;
		     request = ISC_LIST_NEXT(request, link)) {
			dns_request_cancel(request);
		}
272 273
		if (requestmgr->iref == 0) {
			INSIST(ISC_LIST_EMPTY(requestmgr->requests));
274
			send_shutdown_events(requestmgr);
275
		}
276 277 278 279 280 281 282 283 284 285
	}
}

static void
requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp) {

	/*
	 * Locked by caller.
	 */

Brian Wellington's avatar
Brian Wellington committed
286 287
	REQUIRE(VALID_REQUESTMGR(source));
	REQUIRE(targetp != NULL && *targetp == NULL);
288 289 290 291 292

	REQUIRE(!source->exiting);

	source->iref++;
	*targetp = source;
293 294 295

	req_log(ISC_LOG_DEBUG(3), "requestmgr_attach: %p: eref %d iref %d",
		source, source->eref, source->iref);
296 297 298 299 300
}

static void
requestmgr_detach(dns_requestmgr_t **requestmgrp) {
	dns_requestmgr_t *requestmgr;
301
	isc_boolean_t need_destroy = ISC_FALSE;
302 303 304 305

	REQUIRE(requestmgrp != NULL);
	requestmgr = *requestmgrp;
	REQUIRE(VALID_REQUESTMGR(requestmgr));
306

307 308 309 310
	*requestmgrp = NULL;
	LOCK(&requestmgr->lock);
	INSIST(requestmgr->iref > 0);
	requestmgr->iref--;
311 312 313 314

	req_log(ISC_LOG_DEBUG(3), "requestmgr_detach: %p: eref %d iref %d",
		requestmgr, requestmgr->eref, requestmgr->iref);

315 316
	if (requestmgr->iref == 0 && requestmgr->exiting) {
		INSIST(ISC_LIST_HEAD(requestmgr->requests) == NULL);
Mark Andrews's avatar
Mark Andrews committed
317
		send_shutdown_events(requestmgr);
318 319
		if (requestmgr->eref == 0)
			need_destroy = ISC_TRUE;
Mark Andrews's avatar
Mark Andrews committed
320
	}
321 322 323 324
	UNLOCK(&requestmgr->lock);

	if (need_destroy)
		mgr_destroy(requestmgr);
Mark Andrews's avatar
Mark Andrews committed
325 326 327 328 329
}

void
dns_requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp) {

Brian Wellington's avatar
Brian Wellington committed
330 331
	REQUIRE(VALID_REQUESTMGR(source));
	REQUIRE(targetp != NULL && *targetp == NULL);
Mark Andrews's avatar
Mark Andrews committed
332
	REQUIRE(!source->exiting);
Mark Andrews's avatar
Mark Andrews committed
333

334 335
	LOCK(&source->lock);
	source->eref++;
Mark Andrews's avatar
Mark Andrews committed
336
	*targetp = source;
337
	UNLOCK(&source->lock);
338 339 340

	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_attach: %p: eref %d iref %d",
		source, source->eref, source->iref);
Mark Andrews's avatar
Mark Andrews committed
341 342 343 344 345 346 347 348
}

void
dns_requestmgr_detach(dns_requestmgr_t **requestmgrp) {
	dns_requestmgr_t *requestmgr;
	isc_boolean_t need_destroy = ISC_FALSE;

	REQUIRE(requestmgrp != NULL);
Mark Andrews's avatar
Mark Andrews committed
349
	requestmgr = *requestmgrp;
Mark Andrews's avatar
Mark Andrews committed
350 351 352
	REQUIRE(VALID_REQUESTMGR(requestmgr));

	LOCK(&requestmgr->lock);
353 354
	INSIST(requestmgr->eref > 0);
	requestmgr->eref--;
355 356 357 358

	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_detach: %p: eref %d iref %d",
		requestmgr, requestmgr->eref, requestmgr->iref);

359
	if (requestmgr->eref == 0 && requestmgr->iref == 0) {
Mark Andrews's avatar
Mark Andrews committed
360 361
		INSIST(requestmgr->exiting &&
		       ISC_LIST_HEAD(requestmgr->requests) == NULL);
362
		need_destroy = ISC_TRUE;
Mark Andrews's avatar
Mark Andrews committed
363
	}
Mark Andrews's avatar
Mark Andrews committed
364
	UNLOCK(&requestmgr->lock);
Mark Andrews's avatar
Mark Andrews committed
365 366

	if (need_destroy)
Mark Andrews's avatar
Mark Andrews committed
367
		mgr_destroy(requestmgr);
Mark Andrews's avatar
Mark Andrews committed
368 369 370 371 372 373 374 375 376

	*requestmgrp = NULL;
}

static void
send_shutdown_events(dns_requestmgr_t *requestmgr) {
	isc_event_t *event, *next_event;
	isc_task_t *etask;

377 378
	req_log(ISC_LOG_DEBUG(3), "send_shutdown_events: %p", requestmgr);

Mark Andrews's avatar
Mark Andrews committed
379 380 381 382 383 384
	/*
	 * Caller must be holding the manager lock.
	 */
	for (event = ISC_LIST_HEAD(requestmgr->whenshutdown);
	     event != NULL;
	     event = next_event) {
385 386 387 388
		next_event = ISC_LIST_NEXT(event, ev_link);
		ISC_LIST_UNLINK(requestmgr->whenshutdown, event, ev_link);
		etask = event->ev_sender;
		event->ev_sender = requestmgr;
Mark Andrews's avatar
Mark Andrews committed
389 390 391 392 393
		isc_task_sendanddetach(&etask, &event);
	}
}

static void
Mark Andrews's avatar
Mark Andrews committed
394
mgr_destroy(dns_requestmgr_t *requestmgr) {
Mark Andrews's avatar
Mark Andrews committed
395
	int i;
396 397
	isc_mem_t *mctx;

398
	req_log(ISC_LOG_DEBUG(3), "mgr_destroy");
Mark Andrews's avatar
Mark Andrews committed
399

400 401
	REQUIRE(requestmgr->eref == 0);
	REQUIRE(requestmgr->iref == 0);
Mark Andrews's avatar
Mark Andrews committed
402

403
	DESTROYLOCK(&requestmgr->lock);
Mark Andrews's avatar
Mark Andrews committed
404
	for (i = 0; i < DNS_REQUEST_NLOCKS; i++)
405
		DESTROYLOCK(&requestmgr->locks[i]);
Mark Andrews's avatar
Mark Andrews committed
406 407
	if (requestmgr->dispatchv4 != NULL)
		dns_dispatch_detach(&requestmgr->dispatchv4);
408 409
	if (requestmgr->dispatchv6 != NULL)
		dns_dispatch_detach(&requestmgr->dispatchv6);
Mark Andrews's avatar
Mark Andrews committed
410
	requestmgr->magic = 0;
411
	mctx = requestmgr->mctx;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
412
	isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
413
	isc_mem_detach(&mctx);
Mark Andrews's avatar
Mark Andrews committed
414 415
}

Mark Andrews's avatar
Mark Andrews committed
416 417
static unsigned int
mgr_gethash(dns_requestmgr_t *requestmgr) {
418
	req_log(ISC_LOG_DEBUG(3), "mgr_gethash");
Mark Andrews's avatar
Mark Andrews committed
419 420 421 422
	/*
	 * Locked by caller.
	 */
	requestmgr->hash++;
Brian Wellington's avatar
spacing  
Brian Wellington committed
423
	return (requestmgr->hash % DNS_REQUEST_NLOCKS);
Mark Andrews's avatar
Mark Andrews committed
424 425
}

Mark Andrews's avatar
Mark Andrews committed
426 427 428 429
static inline isc_result_t
req_send(dns_request_t *request, isc_task_t *task, isc_sockaddr_t *address) {
	isc_region_t r;
	isc_socket_t *socket;
430
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
431

432 433
	req_log(ISC_LOG_DEBUG(3), "req_send: request %p", request);

Mark Andrews's avatar
Mark Andrews committed
434
	REQUIRE(VALID_REQUEST(request));
435
	socket = req_getsocket(request);
436
	isc_buffer_usedregion(request->query, &r);
437 438 439 440 441
	/*
	 * We could connect the socket when we are using an exclusive dispatch
	 * as we do in resolver.c, but we prefer implementation simplicity
	 * at this moment.
	 */
442 443 444 445 446
	result = isc_socket_sendto(socket, &r, task, req_senddone,
				  request, address, NULL);
	if (result == ISC_R_SUCCESS)
		request->flags |= DNS_REQUEST_F_SENDING;
	return (result);
Mark Andrews's avatar
Mark Andrews committed
447 448
}

Brian Wellington's avatar
Brian Wellington committed
449
static isc_result_t
450 451
new_request(isc_mem_t *mctx, dns_request_t **requestp)
{
Brian Wellington's avatar
Brian Wellington committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	dns_request_t *request;

	request = isc_mem_get(mctx, sizeof(*request));
	if (request == NULL)
		return (ISC_R_NOMEMORY);

	/*
	 * Zero structure.
	 */
	request->magic = 0;
	request->mctx = NULL;
	request->flags = 0;
	ISC_LINK_INIT(request, link);
	request->query = NULL;
	request->answer = NULL;
	request->event = NULL;
	request->dispatch = NULL;
	request->dispentry = NULL;
	request->timer = NULL;
	request->requestmgr = NULL;
	request->tsig = NULL;
	request->tsigkey = NULL;
474 475 476 477
	ISC_EVENT_INIT(&request->ctlevent, sizeof(request->ctlevent), 0, NULL,
		       DNS_EVENT_REQUESTCONTROL, do_cancel, request, NULL,
		       NULL, NULL);
	request->canceling = ISC_FALSE;
Michael Graff's avatar
Michael Graff committed
478
	request->udpcount = 0;
Brian Wellington's avatar
Brian Wellington committed
479 480 481

	isc_mem_attach(mctx, &request->mctx);

482
	request->magic = REQUEST_MAGIC;
Brian Wellington's avatar
Brian Wellington committed
483 484 485 486 487
	*requestp = request;
	return (ISC_R_SUCCESS);
}


Mark Andrews's avatar
Mark Andrews committed
488 489
static isc_boolean_t
isblackholed(dns_dispatchmgr_t *dispatchmgr, isc_sockaddr_t *destaddr) {
490
	dns_acl_t *blackhole;
Mark Andrews's avatar
Mark Andrews committed
491 492 493 494 495
	isc_netaddr_t netaddr;
	int match;
	isc_boolean_t drop = ISC_FALSE;
	char netaddrstr[ISC_NETADDR_FORMATSIZE];

496
	blackhole = dns_dispatchmgr_getblackhole(dispatchmgr);
Mark Andrews's avatar
Mark Andrews committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	if (blackhole != NULL) {
		isc_netaddr_fromsockaddr(&netaddr, destaddr);
		if (dns_acl_match(&netaddr, NULL, blackhole,
				  NULL, &match, NULL) == ISC_R_SUCCESS &&
		    match > 0)
			drop = ISC_TRUE;
	}
	if (drop) {
		isc_netaddr_format(&netaddr, netaddrstr, sizeof(netaddrstr));
		req_log(ISC_LOG_DEBUG(10), "blackholed address %s", netaddrstr);
	}
	return (drop);
}

static isc_result_t
create_tcp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
		    isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
{
	isc_result_t result;
	isc_socket_t *socket = NULL;
	isc_sockaddr_t src;
	unsigned int attrs;
	isc_sockaddr_t bind_any;

	result = isc_socket_create(requestmgr->socketmgr,
				   isc_sockaddr_pf(destaddr),
				   isc_sockettype_tcp, &socket);
	if (result != ISC_R_SUCCESS)
		return (result);
526
#ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
Mark Andrews's avatar
Mark Andrews committed
527 528 529
	if (srcaddr == NULL) {
		isc_sockaddr_anyofpf(&bind_any,
				     isc_sockaddr_pf(destaddr));
530
		result = isc_socket_bind(socket, &bind_any, 0);
Mark Andrews's avatar
Mark Andrews committed
531 532 533
	} else {
		src = *srcaddr;
		isc_sockaddr_setport(&src, 0);
534
		result = isc_socket_bind(socket, &src, 0);
Mark Andrews's avatar
Mark Andrews committed
535 536 537
	}
	if (result != ISC_R_SUCCESS)
		goto cleanup;
538
#endif
Mark Andrews's avatar
Mark Andrews committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	attrs = 0;
	attrs |= DNS_DISPATCHATTR_TCP;
	attrs |= DNS_DISPATCHATTR_PRIVATE;
	if (isc_sockaddr_pf(destaddr) == AF_INET)
		attrs |= DNS_DISPATCHATTR_IPV4;
	else
		attrs |= DNS_DISPATCHATTR_IPV6;
	attrs |= DNS_DISPATCHATTR_MAKEQUERY;
	result = dns_dispatch_createtcp(requestmgr->dispatchmgr,
					socket, requestmgr->taskmgr,
					4096, 2, 1, 1, 3, attrs,
					dispatchp);
cleanup:
	isc_socket_detach(&socket);
	return (result);
}

static isc_result_t
find_udp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
		  isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
{
	dns_dispatch_t *disp = NULL;
	unsigned int attrs, attrmask;

	if (srcaddr == NULL) {
		switch (isc_sockaddr_pf(destaddr)) {
		case PF_INET:
			disp = requestmgr->dispatchv4;
			break;

		case PF_INET6:
			disp = requestmgr->dispatchv6;
			break;

		default:
			return (ISC_R_NOTIMPLEMENTED);
		}
		if (disp == NULL)
			return (ISC_R_FAMILYNOSUPPORT);
		dns_dispatch_attach(disp, dispatchp);
		return (ISC_R_SUCCESS);
	}
	attrs = 0;
	attrs |= DNS_DISPATCHATTR_UDP;
	switch (isc_sockaddr_pf(srcaddr)) {
	case PF_INET:
		attrs |= DNS_DISPATCHATTR_IPV4;
		break;

	case PF_INET6:
		attrs |= DNS_DISPATCHATTR_IPV6;
		break;

	default:
		return (ISC_R_NOTIMPLEMENTED);
	}
	attrmask = 0;
	attrmask |= DNS_DISPATCHATTR_UDP;
	attrmask |= DNS_DISPATCHATTR_TCP;
	attrmask |= DNS_DISPATCHATTR_IPV4;
	attrmask |= DNS_DISPATCHATTR_IPV6;
	return (dns_dispatch_getudp(requestmgr->dispatchmgr,
				    requestmgr->socketmgr,
				    requestmgr->taskmgr,
				    srcaddr, 4096,
				    1000, 32768, 16411, 16433,
				    attrs, attrmask,
				    dispatchp));
}

static isc_result_t
get_dispatch(isc_boolean_t tcp, dns_requestmgr_t *requestmgr,
	     isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
	     dns_dispatch_t **dispatchp)
{
	isc_result_t result;
	if (tcp)
		result = create_tcp_dispatch(requestmgr, srcaddr,
					     destaddr, dispatchp);
	else
		result = find_udp_dispatch(requestmgr, srcaddr,
					   destaddr, dispatchp);
	return (result);
}

static isc_result_t
625
set_timer(isc_timer_t *timer, unsigned int timeout, unsigned int udpresend) {
Mark Andrews's avatar
Mark Andrews committed
626 627 628
	isc_time_t expires;
	isc_interval_t interval;
	isc_result_t result;
629
	isc_timertype_t timertype;
Mark Andrews's avatar
Mark Andrews committed
630 631 632

	isc_interval_set(&interval, timeout, 0);
	result = isc_time_nowplusinterval(&expires, &interval);
633
	isc_interval_set(&interval, udpresend, 0);
Mark Andrews's avatar
Mark Andrews committed
634

635
	timertype = udpresend != 0 ? isc_timertype_limited : isc_timertype_once;
Mark Andrews's avatar
Mark Andrews committed
636
	if (result == ISC_R_SUCCESS)
637 638
		result = isc_timer_reset(timer, timertype, &expires,
					 &interval, ISC_FALSE);
Mark Andrews's avatar
Mark Andrews committed
639 640 641
	return (result);
}

642 643 644 645 646 647
isc_result_t
dns_request_createraw(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
		      isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
		      unsigned int options, unsigned int timeout,
		      isc_task_t *task, isc_taskaction_t action, void *arg,
		      dns_request_t **requestp)
648
{
Michael Graff's avatar
Michael Graff committed
649 650 651
	return(dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
				      options, timeout, 0, 0, task, action,
				      arg, requestp));
652 653 654 655 656
}

isc_result_t
dns_request_createraw2(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
		       isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
Michael Graff's avatar
Michael Graff committed
657 658
		       unsigned int options, unsigned int timeout,
		       unsigned int udptimeout, isc_task_t *task,
659 660
		       isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Michael Graff's avatar
Michael Graff committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
{
	unsigned int udpretries = 0;

	if (udptimeout != 0)
		udpretries = timeout / udptimeout;

	return (dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
				       options, timeout, udptimeout,
				       udpretries, task, action, arg,
				       requestp));
}

isc_result_t
dns_request_createraw3(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
		       isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
		       unsigned int options, unsigned int timeout,
		       unsigned int udptimeout, unsigned int udpretries,
		       isc_task_t *task, isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
{
	dns_request_t *request = NULL;
	isc_task_t *tclone = NULL;
	isc_socket_t *socket = NULL;
	isc_result_t result;
	isc_mem_t *mctx;
	dns_messageid_t	id;
	isc_boolean_t tcp = ISC_FALSE;
	isc_region_t r;

	REQUIRE(VALID_REQUESTMGR(requestmgr));
	REQUIRE(msgbuf != NULL);
	REQUIRE(destaddr != NULL);
	REQUIRE(task != NULL);
	REQUIRE(action != NULL);
	REQUIRE(requestp != NULL && *requestp == NULL);
	REQUIRE(timeout > 0);
Automatic Updater's avatar
Automatic Updater committed
697
	if (srcaddr != NULL)
698
		REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
699 700 701 702 703

	mctx = requestmgr->mctx;

	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw");

Mark Andrews's avatar
Mark Andrews committed
704 705
	if (isblackholed(requestmgr->dispatchmgr, destaddr))
		return (DNS_R_BLACKHOLED);
706

Brian Wellington's avatar
Brian Wellington committed
707 708 709 710
	request = NULL;
	result = new_request(mctx, &request);
	if (result != ISC_R_SUCCESS)
		return (result);
711

Michael Graff's avatar
Michael Graff committed
712 713 714 715 716
	if (udptimeout == 0 && udpretries != 0) {
		udptimeout = timeout / (udpretries + 1);
		if (udptimeout == 0)
			udptimeout = 1;
	}
717
	request->udpcount = udpretries;
Michael Graff's avatar
Michael Graff committed
718

719 720 721 722 723 724 725 726 727 728 729
	/*
	 * Create timer now.  We will set it below once.
	 */
	result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
				  NULL, NULL, task, req_timeout, request,
				  &request->timer);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	request->event = (dns_requestevent_t *)
		isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
730
				   action, arg, sizeof(dns_requestevent_t));
731 732 733 734 735 736 737 738 739 740 741 742 743 744
	if (request->event == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}
	isc_task_attach(task, &tclone);
	request->event->ev_sender = task;
	request->event->request = request;
	request->event->result = ISC_R_FAILURE;

	isc_buffer_usedregion(msgbuf, &r);
	if (r.length < DNS_MESSAGE_HEADERLEN || r.length > 65535) {
		result = DNS_R_FORMERR;
		goto cleanup;
	}
Automatic Updater's avatar
Automatic Updater committed
745

746 747 748
	if ((options & DNS_REQUESTOPT_TCP) != 0 || r.length > 512)
		tcp = ISC_TRUE;

Mark Andrews's avatar
Mark Andrews committed
749 750 751 752 753
	result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
			      &request->dispatch);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

754 755 756 757
	result = dns_dispatch_addresponse2(request->dispatch, destaddr, task,
					   req_response, request, &id,
					   &request->dispentry,
					   requestmgr->socketmgr);
758 759 760
	if (result != ISC_R_SUCCESS)
		goto cleanup;

761 762 763
	socket = req_getsocket(request);
	INSIST(socket != NULL);

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	result = isc_buffer_allocate(mctx, &request->query,
				     r.length + (tcp ? 2 : 0));
	if (result != ISC_R_SUCCESS)
		goto cleanup;
	if (tcp)
		isc_buffer_putuint16(request->query, (isc_uint16_t)r.length);
	result = isc_buffer_copyregion(request->query, &r);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	/* Add message ID. */
	isc_buffer_usedregion(request->query, &r);
	if (tcp)
		isc_region_consume(&r, 2);
	r.base[0] = (id>>8) & 0xff;
	r.base[1] = id & 0xff;

	LOCK(&requestmgr->lock);
	if (requestmgr->exiting) {
		UNLOCK(&requestmgr->lock);
		result = ISC_R_SHUTTINGDOWN;
		goto cleanup;
	}
	requestmgr_attach(requestmgr, &request->requestmgr);
	request->hash = mgr_gethash(requestmgr);
	ISC_LIST_APPEND(requestmgr->requests, request, link);
	UNLOCK(&requestmgr->lock);

Michael Graff's avatar
Michael Graff committed
792
	result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
793 794 795
	if (result != ISC_R_SUCCESS)
		goto unlink;

796 797
	request->destaddr = *destaddr;
	if (tcp) {
798 799 800 801 802 803 804 805 806 807 808
		result = isc_socket_connect(socket, destaddr, task,
					    req_connected, request);
		if (result != ISC_R_SUCCESS)
			goto unlink;
		request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
	} else {
		result = req_send(request, task, destaddr);
		if (result != ISC_R_SUCCESS)
			goto unlink;
	}

Brian Wellington's avatar
Brian Wellington committed
809 810
	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: request %p",
		request);
811 812 813 814 815 816 817 818 819 820 821
	*requestp = request;
	return (ISC_R_SUCCESS);

 unlink:
	LOCK(&requestmgr->lock);
	ISC_LIST_UNLINK(requestmgr->requests, request, link);
	UNLOCK(&requestmgr->lock);

 cleanup:
	if (tclone != NULL)
		isc_task_detach(&tclone);
Brian Wellington's avatar
Brian Wellington committed
822 823
	req_destroy(request);
	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: failed %s",
824 825 826 827
		dns_result_totext(result));
	return (result);
}

Mark Andrews's avatar
Mark Andrews committed
828 829 830
isc_result_t
dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
		   isc_sockaddr_t *address, unsigned int options,
831
		   dns_tsigkey_t *key,
Mark Andrews's avatar
Mark Andrews committed
832 833 834
		   unsigned int timeout, isc_task_t *task,
		   isc_taskaction_t action, void *arg,
		   dns_request_t **requestp)
835
{
Michael Graff's avatar
Michael Graff committed
836 837 838
	return (dns_request_createvia3(requestmgr, message, NULL, address,
				       options, key, timeout, 0, 0, task,
				       action, arg, requestp));
839 840 841 842 843 844 845 846 847
}

isc_result_t
dns_request_createvia(dns_requestmgr_t *requestmgr, dns_message_t *message,
		      isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
		      unsigned int options, dns_tsigkey_t *key,
		      unsigned int timeout, isc_task_t *task,
		      isc_taskaction_t action, void *arg,
		      dns_request_t **requestp)
848
{
Michael Graff's avatar
Michael Graff committed
849 850 851
	return(dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
				      options, key, timeout, 0, 0, task,
				      action, arg, requestp));
852 853 854 855 856 857
}

isc_result_t
dns_request_createvia2(dns_requestmgr_t *requestmgr, dns_message_t *message,
		       isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
		       unsigned int options, dns_tsigkey_t *key,
Michael Graff's avatar
Michael Graff committed
858
		       unsigned int timeout, unsigned int udptimeout,
859 860
		       isc_task_t *task, isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Michael Graff's avatar
Michael Graff committed
861 862 863 864 865 866 867 868 869 870
{
	unsigned int udpretries = 0;

	if (udptimeout != 0)
		udpretries = timeout / udptimeout;
	return (dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
				       options, key, timeout, udptimeout,
				       udpretries, task, action, arg,
				       requestp));
}
Automatic Updater's avatar
Automatic Updater committed
871

Michael Graff's avatar
Michael Graff committed
872 873 874 875 876 877 878 879
isc_result_t
dns_request_createvia3(dns_requestmgr_t *requestmgr, dns_message_t *message,
		       isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
		       unsigned int options, dns_tsigkey_t *key,
		       unsigned int timeout, unsigned int udptimeout,
		       unsigned int udpretries, isc_task_t *task,
		       isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Mark Andrews's avatar
Mark Andrews committed
880
{
Mark Andrews's avatar
Mark Andrews committed
881 882 883 884 885
	dns_request_t *request = NULL;
	isc_task_t *tclone = NULL;
	isc_socket_t *socket = NULL;
	isc_result_t result;
	isc_mem_t *mctx;
Mark Andrews's avatar
Mark Andrews committed
886
	dns_messageid_t	id;
Mark Andrews's avatar
Mark Andrews committed
887
	isc_boolean_t tcp;
888
	isc_boolean_t setkey = ISC_TRUE;
Mark Andrews's avatar
Mark Andrews committed
889 890 891

	REQUIRE(VALID_REQUESTMGR(requestmgr));
	REQUIRE(message != NULL);
892
	REQUIRE(destaddr != NULL);
Mark Andrews's avatar
Mark Andrews committed
893 894 895 896
	REQUIRE(task != NULL);
	REQUIRE(action != NULL);
	REQUIRE(requestp != NULL && *requestp == NULL);
	REQUIRE(timeout > 0);
Automatic Updater's avatar
Automatic Updater committed
897
	if (srcaddr != NULL)
898
		REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
Mark Andrews's avatar
Mark Andrews committed
899

Mark Andrews's avatar
Mark Andrews committed
900 901
	mctx = requestmgr->mctx;

Brian Wellington's avatar
Brian Wellington committed
902
	req_log(ISC_LOG_DEBUG(3), "dns_request_createvia");
903

Mark Andrews's avatar
Mark Andrews committed
904 905
	if (isblackholed(requestmgr->dispatchmgr, destaddr))
		return (DNS_R_BLACKHOLED);
906

Brian Wellington's avatar
Brian Wellington committed
907 908 909 910
	request = NULL;
	result = new_request(mctx, &request);
	if (result != ISC_R_SUCCESS)
		return (result);
Mark Andrews's avatar
Mark Andrews committed
911

Michael Graff's avatar
Michael Graff committed
912 913 914 915 916
	if (udptimeout == 0 && udpretries != 0) {
		udptimeout = timeout / (udpretries + 1);
		if (udptimeout == 0)
			udptimeout = 1;
	}
917
	request->udpcount = udpretries;
Michael Graff's avatar
Michael Graff committed
918

Mark Andrews's avatar
Mark Andrews committed
919 920 921 922 923 924 925 926 927 928
	/*
	 * Create timer now.  We will set it below once.
	 */
	result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
				  NULL, NULL, task, req_timeout, request,
				  &request->timer);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	request->event = (dns_requestevent_t *)
Mark Andrews's avatar
Mark Andrews committed
929
		isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
930
				   action, arg, sizeof(dns_requestevent_t));
Mark Andrews's avatar
Mark Andrews committed
931
	if (request->event == NULL) {
Mark Andrews's avatar
Mark Andrews committed
932 933 934 935
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}
	isc_task_attach(task, &tclone);
936
	request->event->ev_sender = task;
Mark Andrews's avatar
Mark Andrews committed
937 938
	request->event->request = request;
	request->event->result = ISC_R_FAILURE;
939 940
	if (key != NULL)
		dns_tsigkey_attach(key, &request->tsigkey);
941

Mark Andrews's avatar
Mark Andrews committed
942
 use_tcp:
Andreas Gustafsson's avatar
spacing  
Andreas Gustafsson committed
943
	tcp = ISC_TF((options & DNS_REQUESTOPT_TCP) != 0);
Mark Andrews's avatar
Mark Andrews committed
944 945 946 947 948
	result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
			      &request->dispatch);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

949 950 951 952
	result = dns_dispatch_addresponse2(request->dispatch, destaddr, task,
					   req_response, request, &id,
					   &request->dispentry,
					   requestmgr->socketmgr);
Mark Andrews's avatar
Mark Andrews committed
953 954
	if (result != ISC_R_SUCCESS)
		goto cleanup;
955 956
	socket = req_getsocket(request);
	INSIST(socket != NULL);
Mark Andrews's avatar
Mark Andrews committed
957

Mark Andrews's avatar
Mark Andrews committed
958
	message->id = id;
959 960 961 962 963
	if (setkey) {
		result = dns_message_settsigkey(message, request->tsigkey);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	}
964
	result = req_render(message, &request->query, options, mctx);
Mark Andrews's avatar
Mark Andrews committed
965 966 967 968 969 970
	if (result == DNS_R_USETCP &&
	    (options & DNS_REQUESTOPT_TCP) == 0) {
		/*
		 * Try again using TCP.
		 */
		dns_message_renderreset(message);
971
		dns_dispatch_removeresponse(&request->dispentry, NULL);
Mark Andrews's avatar
Mark Andrews committed
972 973 974
		dns_dispatch_detach(&request->dispatch);
		socket = NULL;
		options |= DNS_REQUESTOPT_TCP;
975
		setkey = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
976 977
		goto use_tcp;
	}
978
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
979
		goto cleanup;
980 981 982 983 984

	result = dns_message_getquerytsig(message, mctx, &request->tsig);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

Mark Andrews's avatar
Mark Andrews committed
985
	LOCK(&requestmgr->lock);
986 987 988 989 990 991
	if (requestmgr->exiting) {
		UNLOCK(&requestmgr->lock);
		result = ISC_R_SHUTTINGDOWN;
		goto cleanup;
	}
	requestmgr_attach(requestmgr, &request->requestmgr);
Mark Andrews's avatar
Mark Andrews committed
992
	request->hash = mgr_gethash(requestmgr);
Mark Andrews's avatar
Mark Andrews committed
993 994
	ISC_LIST_APPEND(requestmgr->requests, request, link);
	UNLOCK(&requestmgr->lock);
Mark Andrews's avatar
Mark Andrews committed
995

Michael Graff's avatar
Michael Graff committed
996
	result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
Mark Andrews's avatar
Mark Andrews committed
997
	if (result != ISC_R_SUCCESS)
998
		goto unlink;
Mark Andrews's avatar
Mark Andrews committed
999

1000 1001
	request->destaddr = *destaddr;
	if (tcp) {
1002
		result = isc_socket_connect(socket, destaddr, task,
Mark Andrews's avatar
Mark Andrews committed