request.c 38.9 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2002  Internet Software Consortium.
4
 *
Mark Andrews's avatar
Mark Andrews committed
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * 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
 */

18
/* $Id: request.c,v 1.75 2006/01/04 04:14:39 marka 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 */
98 99
#define DNS_REQUEST_F_TIMEDOUT 0x0008	/*%< cancelled due to a timeout */
#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 124 125 126 127
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);
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);
128
static void req_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
129
static void do_cancel(isc_task_t *task, isc_event_t *event);
Mark Andrews's avatar
Mark Andrews committed
130 131 132 133 134 135

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

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

150
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create");
151

Mark Andrews's avatar
Mark Andrews committed
152
	REQUIRE(requestmgrp != NULL && *requestmgrp == NULL);
Mark Andrews's avatar
Mark Andrews committed
153 154
	REQUIRE(timermgr != NULL);
	REQUIRE(socketmgr != NULL);
155
	REQUIRE(taskmgr != NULL);
156
	REQUIRE(dispatchmgr != NULL);
Mark Andrews's avatar
Mark Andrews committed
157 158
	if (dispatchv4 != NULL) {
		socket = dns_dispatch_getsocket(dispatchv4);
Mark Andrews's avatar
Mark Andrews committed
159
		REQUIRE(isc_socket_gettype(socket) == isc_sockettype_udp);
Mark Andrews's avatar
Mark Andrews committed
160 161 162
	}
	if (dispatchv6 != NULL) {
		socket = dns_dispatch_getsocket(dispatchv6);
Mark Andrews's avatar
Mark Andrews committed
163
		REQUIRE(isc_socket_gettype(socket) == isc_sockettype_udp);
Mark Andrews's avatar
Mark Andrews committed
164 165 166 167 168 169
	}

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

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

205 206 207
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create: %p", requestmgr);

	*requestmgrp = requestmgr;
Mark Andrews's avatar
Mark Andrews committed
208 209 210 211 212 213 214
	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
215 216
	isc_task_t *clone;
	isc_event_t *event;
Mark Andrews's avatar
Mark Andrews committed
217

218
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_whenshutdown");
219

Brian Wellington's avatar
Brian Wellington committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	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
239 240 241 242 243 244 245
	}
	UNLOCK(&requestmgr->lock);
}

void
dns_requestmgr_shutdown(dns_requestmgr_t *requestmgr) {

Brian Wellington's avatar
Brian Wellington committed
246
	REQUIRE(VALID_REQUESTMGR(requestmgr));
Mark Andrews's avatar
Mark Andrews committed
247

248
	req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_shutdown: %p", requestmgr);
249

Mark Andrews's avatar
Mark Andrews committed
250
	LOCK(&requestmgr->lock);
Mark Andrews's avatar
Mark Andrews committed
251 252 253 254
	mgr_shutdown(requestmgr);
	UNLOCK(&requestmgr->lock);
}

255
static void
Mark Andrews's avatar
Mark Andrews committed
256 257
mgr_shutdown(dns_requestmgr_t *requestmgr) {
	dns_request_t *request;
258

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

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

	/*
	 * Locked by caller.
	 */

Brian Wellington's avatar
Brian Wellington committed
283 284
	REQUIRE(VALID_REQUESTMGR(source));
	REQUIRE(targetp != NULL && *targetp == NULL);
285 286 287 288 289

	REQUIRE(!source->exiting);

	source->iref++;
	*targetp = source;
290 291 292

	req_log(ISC_LOG_DEBUG(3), "requestmgr_attach: %p: eref %d iref %d",
		source, source->eref, source->iref);
293 294 295 296 297
}

static void
requestmgr_detach(dns_requestmgr_t **requestmgrp) {
	dns_requestmgr_t *requestmgr;
298
	isc_boolean_t need_destroy = ISC_FALSE;
299 300 301 302

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

304 305 306 307
	*requestmgrp = NULL;
	LOCK(&requestmgr->lock);
	INSIST(requestmgr->iref > 0);
	requestmgr->iref--;
308 309 310 311

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

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

	if (need_destroy)
		mgr_destroy(requestmgr);
Mark Andrews's avatar
Mark Andrews committed
322 323 324 325 326
}

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

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

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

	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
338 339 340 341 342 343 344 345
}

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
346
	requestmgr = *requestmgrp;
Mark Andrews's avatar
Mark Andrews committed
347 348 349
	REQUIRE(VALID_REQUESTMGR(requestmgr));

	LOCK(&requestmgr->lock);
350 351
	INSIST(requestmgr->eref > 0);
	requestmgr->eref--;
352 353 354 355

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

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

	if (need_destroy)
Mark Andrews's avatar
Mark Andrews committed
364
		mgr_destroy(requestmgr);
Mark Andrews's avatar
Mark Andrews committed
365 366 367 368 369 370 371 372 373

	*requestmgrp = NULL;
}

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

374 375
	req_log(ISC_LOG_DEBUG(3), "send_shutdown_events: %p", requestmgr);

Mark Andrews's avatar
Mark Andrews committed
376 377 378 379 380 381
	/*
	 * Caller must be holding the manager lock.
	 */
	for (event = ISC_LIST_HEAD(requestmgr->whenshutdown);
	     event != NULL;
	     event = next_event) {
382 383 384 385
		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
386 387 388 389 390
		isc_task_sendanddetach(&etask, &event);
	}
}

static void
Mark Andrews's avatar
Mark Andrews committed
391
mgr_destroy(dns_requestmgr_t *requestmgr) {
Mark Andrews's avatar
Mark Andrews committed
392
	int i;
393 394
	isc_mem_t *mctx;

395
	req_log(ISC_LOG_DEBUG(3), "mgr_destroy");
Mark Andrews's avatar
Mark Andrews committed
396

397 398
	REQUIRE(requestmgr->eref == 0);
	REQUIRE(requestmgr->iref == 0);
Mark Andrews's avatar
Mark Andrews committed
399

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

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

Mark Andrews's avatar
Mark Andrews committed
423 424 425 426
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;
427
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
428

429 430
	req_log(ISC_LOG_DEBUG(3), "req_send: request %p", request);

Mark Andrews's avatar
Mark Andrews committed
431
	REQUIRE(VALID_REQUEST(request));
Mark Andrews's avatar
Mark Andrews committed
432
	socket = dns_dispatch_getsocket(request->dispatch);
433
	isc_buffer_usedregion(request->query, &r);
434 435 436 437 438
	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
439 440
}

Brian Wellington's avatar
Brian Wellington committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
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;
465 466 467 468
	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
469
	request->udpcount = 0;
Brian Wellington's avatar
Brian Wellington committed
470 471 472

	isc_mem_attach(mctx, &request->mctx);

473
	request->magic = REQUEST_MAGIC;
Brian Wellington's avatar
Brian Wellington committed
474 475 476 477 478
	*requestp = request;
	return (ISC_R_SUCCESS);
}


Mark Andrews's avatar
Mark Andrews committed
479 480
static isc_boolean_t
isblackholed(dns_dispatchmgr_t *dispatchmgr, isc_sockaddr_t *destaddr) {
481
	dns_acl_t *blackhole;
Mark Andrews's avatar
Mark Andrews committed
482 483 484 485 486
	isc_netaddr_t netaddr;
	int match;
	isc_boolean_t drop = ISC_FALSE;
	char netaddrstr[ISC_NETADDR_FORMATSIZE];

487
	blackhole = dns_dispatchmgr_getblackhole(dispatchmgr);
Mark Andrews's avatar
Mark Andrews committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	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);
517
#ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
Mark Andrews's avatar
Mark Andrews committed
518 519 520 521 522 523 524 525 526 527 528
	if (srcaddr == NULL) {
		isc_sockaddr_anyofpf(&bind_any,
				     isc_sockaddr_pf(destaddr));
		result = isc_socket_bind(socket, &bind_any);
	} else {
		src = *srcaddr;
		isc_sockaddr_setport(&src, 0);
		result = isc_socket_bind(socket, &src);
	}
	if (result != ISC_R_SUCCESS)
		goto cleanup;
529
#endif
Mark Andrews's avatar
Mark Andrews committed
530 531 532 533 534 535 536 537 538 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
	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
616
set_timer(isc_timer_t *timer, unsigned int timeout, unsigned int udpresend) {
Mark Andrews's avatar
Mark Andrews committed
617 618 619
	isc_time_t expires;
	isc_interval_t interval;
	isc_result_t result;
620
	isc_timertype_t timertype;
Mark Andrews's avatar
Mark Andrews committed
621 622 623

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

626
	timertype = udpresend != 0 ? isc_timertype_limited : isc_timertype_once;
Mark Andrews's avatar
Mark Andrews committed
627
	if (result == ISC_R_SUCCESS)
628 629
		result = isc_timer_reset(timer, timertype, &expires,
					 &interval, ISC_FALSE);
Mark Andrews's avatar
Mark Andrews committed
630 631 632
	return (result);
}

633 634 635 636 637 638
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)
639
{
Michael Graff's avatar
Michael Graff committed
640 641 642
	return(dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
				      options, timeout, 0, 0, task, action,
				      arg, requestp));
643 644 645 646 647
}

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
648 649
		       unsigned int options, unsigned int timeout,
		       unsigned int udptimeout, isc_task_t *task,
650 651
		       isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Michael Graff's avatar
Michael Graff committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
{
	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)
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
{
	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);
688 689
	if (srcaddr != NULL) 
		REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
690 691 692 693 694

	mctx = requestmgr->mctx;

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

Mark Andrews's avatar
Mark Andrews committed
695 696
	if (isblackholed(requestmgr->dispatchmgr, destaddr))
		return (DNS_R_BLACKHOLED);
697

Brian Wellington's avatar
Brian Wellington committed
698 699 700 701
	request = NULL;
	result = new_request(mctx, &request);
	if (result != ISC_R_SUCCESS)
		return (result);
702

Michael Graff's avatar
Michael Graff committed
703 704 705 706 707 708
	if (udptimeout == 0 && udpretries != 0) {
		udptimeout = timeout / (udpretries + 1);
		if (udptimeout == 0)
			udptimeout = 1;
	}

709 710 711 712 713 714 715 716 717 718 719
	/*
	 * 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
720
				   action, arg, sizeof(dns_requestevent_t));
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	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;
	}
		
	if ((options & DNS_REQUESTOPT_TCP) != 0 || r.length > 512)
		tcp = ISC_TRUE;

Mark Andrews's avatar
Mark Andrews committed
739 740 741 742 743
	result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
			      &request->dispatch);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
	socket = dns_dispatch_getsocket(request->dispatch);
	INSIST(socket != NULL);
	result = dns_dispatch_addresponse(request->dispatch, destaddr, task,
					  req_response, request, &id,
					  &request->dispentry);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	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
780
	result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
781 782 783
	if (result != ISC_R_SUCCESS)
		goto unlink;

784 785
	request->destaddr = *destaddr;
	if (tcp) {
786 787 788 789 790 791 792 793 794 795 796
		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
797 798
	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: request %p",
		request);
799 800 801 802 803 804 805 806 807 808 809
	*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
810 811
	req_destroy(request);
	req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: failed %s",
812 813 814 815
		dns_result_totext(result));
	return (result);
}

Mark Andrews's avatar
Mark Andrews committed
816 817 818
isc_result_t
dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
		   isc_sockaddr_t *address, unsigned int options,
819
		   dns_tsigkey_t *key,
Mark Andrews's avatar
Mark Andrews committed
820 821 822
		   unsigned int timeout, isc_task_t *task,
		   isc_taskaction_t action, void *arg,
		   dns_request_t **requestp)
823
{
Michael Graff's avatar
Michael Graff committed
824 825 826
	return (dns_request_createvia3(requestmgr, message, NULL, address,
				       options, key, timeout, 0, 0, task,
				       action, arg, requestp));
827 828 829 830 831 832 833 834 835
}

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)
836
{
Michael Graff's avatar
Michael Graff committed
837 838 839
	return(dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
				      options, key, timeout, 0, 0, task,
				      action, arg, requestp));
840 841 842 843 844 845
}

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
846
		       unsigned int timeout, unsigned int udptimeout,
847 848
		       isc_task_t *task, isc_taskaction_t action, void *arg,
		       dns_request_t **requestp)
Michael Graff's avatar
Michael Graff committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
{
	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));
}
					
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
868
{
Mark Andrews's avatar
Mark Andrews committed
869 870 871 872 873
	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
874
	dns_messageid_t	id;
Mark Andrews's avatar
Mark Andrews committed
875
	isc_boolean_t tcp;
876
	isc_boolean_t setkey = ISC_TRUE;
Mark Andrews's avatar
Mark Andrews committed
877 878 879

	REQUIRE(VALID_REQUESTMGR(requestmgr));
	REQUIRE(message != NULL);
880
	REQUIRE(destaddr != NULL);
Mark Andrews's avatar
Mark Andrews committed
881 882 883 884
	REQUIRE(task != NULL);
	REQUIRE(action != NULL);
	REQUIRE(requestp != NULL && *requestp == NULL);
	REQUIRE(timeout > 0);
885 886
	if (srcaddr != NULL) 
		REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
Mark Andrews's avatar
Mark Andrews committed
887

Mark Andrews's avatar
Mark Andrews committed
888 889
	mctx = requestmgr->mctx;

Brian Wellington's avatar
Brian Wellington committed
890
	req_log(ISC_LOG_DEBUG(3), "dns_request_createvia");
891

Mark Andrews's avatar
Mark Andrews committed
892 893
	if (isblackholed(requestmgr->dispatchmgr, destaddr))
		return (DNS_R_BLACKHOLED);
894

Brian Wellington's avatar
Brian Wellington committed
895 896 897 898
	request = NULL;
	result = new_request(mctx, &request);
	if (result != ISC_R_SUCCESS)
		return (result);
Mark Andrews's avatar
Mark Andrews committed
899

Michael Graff's avatar
Michael Graff committed
900 901 902 903 904 905
	if (udptimeout == 0 && udpretries != 0) {
		udptimeout = timeout / (udpretries + 1);
		if (udptimeout == 0)
			udptimeout = 1;
	}

Mark Andrews's avatar
Mark Andrews committed
906 907 908 909 910 911 912 913 914 915
	/*
	 * 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
916
		isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
917
				   action, arg, sizeof(dns_requestevent_t));
Mark Andrews's avatar
Mark Andrews committed
918
	if (request->event == NULL) {
Mark Andrews's avatar
Mark Andrews committed
919 920 921 922
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}
	isc_task_attach(task, &tclone);
923
	request->event->ev_sender = task;
Mark Andrews's avatar
Mark Andrews committed
924 925
	request->event->request = request;
	request->event->result = ISC_R_FAILURE;
926 927
	if (key != NULL)
		dns_tsigkey_attach(key, &request->tsigkey);
928

Mark Andrews's avatar
Mark Andrews committed
929
 use_tcp:
Andreas Gustafsson's avatar
spacing  
Andreas Gustafsson committed
930
	tcp = ISC_TF((options & DNS_REQUESTOPT_TCP) != 0);
Mark Andrews's avatar
Mark Andrews committed
931 932 933 934 935
	result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
			      &request->dispatch);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

Mark Andrews's avatar
Mark Andrews committed
936 937
	socket = dns_dispatch_getsocket(request->dispatch);
	INSIST(socket != NULL);
938
	result = dns_dispatch_addresponse(request->dispatch, destaddr, task,
Mark Andrews's avatar
Mark Andrews committed
939
					  req_response, request, &id,
Mark Andrews's avatar
Mark Andrews committed
940 941 942 943
					  &request->dispentry);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

Mark Andrews's avatar
Mark Andrews committed
944
	message->id = id;
945 946 947 948 949
	if (setkey) {
		result = dns_message_settsigkey(message, request->tsigkey);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	}
950
	result = req_render(message, &request->query, options, mctx);
Mark Andrews's avatar
Mark Andrews committed
951 952 953 954 955 956
	if (result == DNS_R_USETCP &&
	    (options & DNS_REQUESTOPT_TCP) == 0) {
		/*
		 * Try again using TCP.
		 */
		dns_message_renderreset(message);
957
		dns_dispatch_removeresponse(&request->dispentry, NULL);
Mark Andrews's avatar
Mark Andrews committed
958 959 960
		dns_dispatch_detach(&request->dispatch);
		socket = NULL;
		options |= DNS_REQUESTOPT_TCP;
961
		setkey = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
962 963
		goto use_tcp;
	}
964
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
965
		goto cleanup;
966 967 968 969 970

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

Mark Andrews's avatar
Mark Andrews committed
971
	LOCK(&requestmgr->lock);
972 973 974 975 976 977
	if (requestmgr->exiting) {
		UNLOCK(&requestmgr->lock);
		result = ISC_R_SHUTTINGDOWN;
		goto cleanup;
	}
	requestmgr_attach(requestmgr, &request->requestmgr);
Mark Andrews's avatar
Mark Andrews committed
978
	request->hash = mgr_gethash(requestmgr);
Mark Andrews's avatar
Mark Andrews committed
979 980
	ISC_LIST_APPEND(requestmgr->requests, request, link);
	UNLOCK(&requestmgr->lock);
Mark Andrews's avatar
Mark Andrews committed
981

Michael Graff's avatar
Michael Graff committed
982
	result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
Mark Andrews's avatar
Mark Andrews committed
983
	if (result != ISC_R_SUCCESS)
984
		goto unlink;
Mark Andrews's avatar
Mark Andrews committed
985

986 987
	request->destaddr = *destaddr;
	if (tcp) {
988
		result = isc_socket_connect(socket, destaddr, task,
Mark Andrews's avatar
Mark Andrews committed
989 990
					    req_connected, request);
		if (result != ISC_R_SUCCESS)
991
			goto unlink;
992
		request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
Mark Andrews's avatar
Mark Andrews committed
993
	} else {
994
		result = req_send(request, task, destaddr);
Mark Andrews's avatar
Mark Andrews committed
995
		if (result != ISC_R_SUCCESS)
996
			goto unlink;
Mark Andrews's avatar
Mark Andrews committed
997 998
	}

Brian Wellington's avatar
Brian Wellington committed
999 1000
	req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: request %p",
		request);
Mark Andrews's avatar
Mark Andrews committed
1001
	*requestp = request;
Mark Andrews's avatar
Mark Andrews committed
1002
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
1003

1004 1005 1006 1007 1008
 unlink:
	LOCK(&requestmgr->lock);
	ISC_LIST_UNLINK(requestmgr->requests, request, link);
	UNLOCK(&requestmgr->lock);

Mark Andrews's avatar
Mark Andrews committed
1009 1010 1011
 cleanup:
	if (tclone != NULL)
		isc_task_detach(&tclone);
Brian Wellington's avatar
Brian Wellington committed
1012 1013
	req_destroy(request);
	req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: failed %s",
1014
		dns_result_totext(result));
Mark Andrews's avatar
Mark Andrews committed
1015 1016 1017 1018
	return (result);
}

static isc_result_t
1019 1020 1021
req_render(dns_message_t *message, isc_buffer_t **bufferp,
	   unsigned int options, isc_mem_t *mctx)
{
Mark Andrews's avatar
Mark Andrews committed
1022 1023 1024 1025
	isc_buffer_t *buf1 = NULL;
	isc_buffer_t *buf2 = NULL;
	isc_result_t result;
	isc_region_t r;
1026
	isc_boolean_t tcp = ISC_FALSE;
1027 1028
	dns_compress_t cctx;
	isc_boolean_t cleanup_cctx = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
1029 1030 1031

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

1032
	req_log(ISC_LOG_DEBUG(3), "request_render");
Mark Andrews's avatar
Mark Andrews committed
1033

Mark Andrews's avatar
Mark Andrews committed
1034 1035 1036
	/*
	 * Create buffer able to hold largest possible message.
	 */
1037
	result = isc_buffer_allocate(mctx, &buf1, 65535);
Mark Andrews's avatar
Mark Andrews committed
1038 1039 1040
	if (result != ISC_R_SUCCESS)
		return (result);

1041 1042 1043 1044 1045
	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
1046 1047 1048
	/*
	 * Render message.
	 */
1049
	result = dns_message_renderbegin(message, &cctx, buf1);
1050
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
1051 1052
		goto cleanup;
	result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
1053
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
1054 1055
		goto cleanup;
	result = dns_message_rendersection(message, DNS_SECTION_ANSWER, 0);
1056
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
1057 1058
		goto cleanup;
	result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY, 0);
1059
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
1060 1061
		goto cleanup;
	result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL, 0);
1062
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
1063 1064
		goto cleanup;
	result = dns_message_renderend(message);
1065
	if (result != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
1066 1067
		goto cleanup;

1068 1069 1070
	dns_compress_invalidate(&cctx);
	cleanup_cctx = ISC_FALSE;

Mark Andrews's avatar
Mark Andrews committed
1071 1072 1073
	/*
	 * Copy rendered message to exact sized buffer.
	 */
1074
	isc_buffer_usedregion(buf1, &r);
1075 1076 1077 1078 1079 1080 1081
	if ((options & DNS_REQUESTOPT_TCP) != 0) {
		tcp = ISC_TRUE;
	} else if (r.length > 512) {
		result = DNS_R_USETCP;
		goto cleanup;
	}
	result = isc_buffer_allocate(mctx, &buf2, r.length + (tcp ? 2 : 0));
Mark Andrews's avatar
Mark Andrews committed
1082 1083
	if (result != ISC_R_SUCCESS)
		goto cleanup;
1084
	if (tcp)
Mark Andrews's avatar
Mark Andrews committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
		isc_buffer_putuint16(buf2, (isc_uint16_t)r.length);
	result = isc_buffer_copyregion(buf2, &r);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	/*
	 * Cleanup and return.
	 */
	isc_buffer_free(&buf1);
	*bufferp = buf2;
	return (ISC_R_SUCCESS);

 cleanup:
Mark Andrews's avatar
Mark Andrews committed
1098
	dns_message_renderreset(message);
Mark Andrews's avatar
Mark Andrews committed
1099 1100 1101 1102
	if (buf1 != NULL)
		isc_buffer_free(&buf1);
	if (buf2 != NULL)
		isc_buffer_free(&buf2);
1103 1104
	if (cleanup_cctx)
		dns_compress_invalidate(&cctx);
Mark Andrews's avatar
Mark Andrews committed
1105
	return (