request.c 40.1 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004-2008  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
 */

Francis Dupont's avatar
Francis Dupont committed
18
/* $Id: request.c,v 1.83 2009/01/17 14:56:25 fdupont Exp $ */
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;
431
	unsigned int dispattr;
Mark Andrews's avatar
Mark Andrews committed
432

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

Mark Andrews's avatar
Mark Andrews committed
435
	REQUIRE(VALID_REQUEST(request));
436 437
	dispattr = dns_dispatch_getattributes(request->dispatch);
	socket = req_getsocket(request);
438
	isc_buffer_usedregion(request->query, &r);
439 440 441 442 443
	/*
	 * 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.
	 */
444 445 446 447 448
	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
449 450
}

Brian Wellington's avatar
Brian Wellington committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
static isc_result_t
new_request(isc_mem_t *mctx, dns_request_t **requestp) {
	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;
475 476 477 478
	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
479
	request->udpcount = 0;
Brian Wellington's avatar
Brian Wellington committed
480 481 482

	isc_mem_attach(mctx, &request->mctx);

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


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

497
	blackhole = dns_dispatchmgr_getblackhole(dispatchmgr);
Mark Andrews's avatar
Mark Andrews committed
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 526
	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);
527
#ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
Mark Andrews's avatar
Mark Andrews committed
528 529 530
	if (srcaddr == NULL) {
		isc_sockaddr_anyofpf(&bind_any,
				     isc_sockaddr_pf(destaddr));
531
		result = isc_socket_bind(socket, &bind_any, 0);
Mark Andrews's avatar
Mark Andrews committed
532 533 534
	} else {
		src = *srcaddr;
		isc_sockaddr_setport(&src, 0);
535
		result = isc_socket_bind(socket, &src, 0);
Mark Andrews's avatar
Mark Andrews committed
536 537 538
	}
	if (result != ISC_R_SUCCESS)
		goto cleanup;
539
#endif
Mark Andrews's avatar
Mark Andrews committed
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 625
	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
626
set_timer(isc_timer_t *timer, unsigned int timeout, unsigned int udpresend) {
Mark Andrews's avatar
Mark Andrews committed
627 628 629
	isc_time_t expires;
	isc_interval_t interval;
	isc_result_t result;
630
	isc_timertype_t timertype;
Mark Andrews's avatar
Mark Andrews committed
631 632 633

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

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

643 644 645 646 647 648
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)
649
{
Michael Graff's avatar
Michael Graff committed
650 651 652
	return(dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
				      options, timeout, 0, 0, task, action,
				      arg, requestp));
653 654 655 656 657
}

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
658 659
		       unsigned int options, unsigned int timeout,
		       unsigned int udptimeout, isc_task_t *task,
660 661
		       isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Michael Graff's avatar
Michael Graff committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
{
	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)
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
{
	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
698
	if (srcaddr != NULL)
699
		REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
700 701 702 703 704

	mctx = requestmgr->mctx;

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

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

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

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

720 721 722 723 724 725 726 727 728 729 730
	/*
	 * 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
731
				   action, arg, sizeof(dns_requestevent_t));
732 733 734 735 736 737 738 739 740 741 742 743 744 745
	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
746

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

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

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

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

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 792
	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
793
	result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
794 795 796
	if (result != ISC_R_SUCCESS)
		goto unlink;

797 798
	request->destaddr = *destaddr;
	if (tcp) {
799 800 801 802 803 804 805 806 807 808 809
		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
810 811
	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: request %p",
		request);
812 813 814 815 816 817 818 819 820 821 822
	*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
823 824
	req_destroy(request);
	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: failed %s",
825 826 827 828
		dns_result_totext(result));
	return (result);
}

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

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)
849
{
Michael Graff's avatar
Michael Graff committed
850 851 852
	return(dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
				      options, key, timeout, 0, 0, task,
				      action, arg, requestp));
853 854 855 856 857 858
}

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
859
		       unsigned int timeout, unsigned int udptimeout,
860 861
		       isc_task_t *task, isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Michael Graff's avatar
Michael Graff committed
862 863 864 865 866 867 868 869 870 871
{
	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
872

Michael Graff's avatar
Michael Graff committed
873 874 875 876 877 878 879 880
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
881
{
Mark Andrews's avatar
Mark Andrews committed
882 883 884 885 886
	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
887
	dns_messageid_t	id;
Mark Andrews's avatar
Mark Andrews committed
888
	isc_boolean_t tcp;
889
	isc_boolean_t setkey = ISC_TRUE;
Mark Andrews's avatar
Mark Andrews committed
890 891 892

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

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

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

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

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

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

Mark Andrews's avatar
Mark Andrews committed
920 921 922 923 924 925 926 927 928 929
	/*
	 * 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
930
		isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
931
				   action, arg, sizeof(dns_requestevent_t));
Mark Andrews's avatar
Mark Andrews committed
932
	if (request->event == NULL) {
Mark Andrews's avatar
Mark Andrews committed
933 934 935 936
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}
	isc_task_attach(task, &tclone);
937
	request->event->ev_sender = task;
Mark Andrews's avatar
Mark Andrews committed
938 939
	request->event->request = request;
	request->event->result = ISC_R_FAILURE;
940 941
	if (key != NULL)
		dns_tsigkey_attach(key, &request->tsigkey);
942

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

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

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

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

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

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

1001 1002
	request->destaddr = *destaddr;
	if (tcp) {
1003
		result = isc_socket_connect(socket, destaddr, task,
Mark Andrews's avatar
Mark Andrews committed
1004 1005
					    req_connected, request);
		if (result != ISC_R_SUCCESS)
1006
			goto unlink;
1007
		request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
Mark Andrews's avatar
Mark Andrews committed
1008
	} else {
1009
		result = req_send(request, task, destaddr);
Mark Andrews's avatar
Mark Andrews committed
1010
		if (result != ISC_R_SUCCESS)
1011
			goto unlink;
Mark Andrews's avatar
Mark Andrews committed
1012 1013
	}

Brian Wellington's avatar
Brian Wellington committed
1014 1015
	req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: request %p",
		request);
Mark Andrews's avatar
Mark Andrews committed
1016
	*requestp = request;
Mark Andrews's avatar
Mark Andrews committed
1017
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
1018

1019 1020 1021 1022 1023
 unlink:
	LOCK(&requestmgr->lock);
	ISC_LIST_UNLINK(requestmgr->requests, request, link);
	UNLOCK(&requestmgr->lock);

Mark Andrews's avatar
Mark Andrews committed
1024 1025 1026
 cleanup:
	if (tclone != NULL)
		isc_task_detach(&tclone);
Brian Wellington's avatar
Brian Wellington committed
1027 1028
	req_destroy(request);
	req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: failed %s",
1029
		dns_result_totext(result));
Mark Andrews's avatar
Mark Andrews committed
1030 1031 1032 1033
	return (result);
}

static isc_result_t
1034 1035 1036
req_render(dns_message_t *message, isc_buffer_t **bufferp,
	   unsigned int options, isc_mem_t *mctx)
{
Mark Andrews's avatar
Mark Andrews committed
1037 1038 1039 1040
	isc_buffer_t *buf1 = NULL;
	isc_buffer_t *buf2 = NULL;
	isc_result_t result;
	isc_region_t r;
1041
	isc_boolean_t tcp = ISC_FALSE;
1042 1043
	dns_compress_t cctx;
	isc_boolean_t cleanup_cctx = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
1044 1045 1046

	REQUIRE(bufferp != NULL && *bufferp == NULL);

1047
	req_log(ISC_LOG_DEBUG(3), "request_render");
Mark Andrews's avatar
Mark Andrews committed
1048

Mark Andrews's avatar
Mark Andrews committed
1049 1050 1051
	/*
	 * Create buffer able to hold largest possible message.
	 */
1052
	result = isc_buffer_allocate(mctx, &buf1, 65535);
Mark Andrews's avatar
Mark Andrews committed
1053 1054 1055
	if (result != ISC_R_SUCCESS)
		return (result);

1056 1057 1058 1059 1060
	result = dns_compress_init(&cctx, -1, mctx);
	if (result != ISC_R_SUCCESS)
		return (result);
	cleanup_cctx = ISC_TRUE;

Mark Andrews's avatar
Mark Andrews committed
1061 1062 1063
	/*
	 * Render message.
	 */
1064
	result = dns_message_renderbegin(message, &cctx, buf1);
1065
	if (re