resolver.c 126 KB
Newer Older
Michael Graff's avatar
Michael Graff committed
1
/*
Brian Wellington's avatar
Brian Wellington committed
2
 * Copyright (C) 1999-2001  Internet Software Consortium.
3
 *
Michael Graff's avatar
Michael Graff committed
4
5
6
 * 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.
7
 *
8
9
10
11
12
13
14
15
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM 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.
Michael Graff's avatar
Michael Graff committed
16
 */
Bob Halley's avatar
Bob Halley committed
17

Brian Wellington's avatar
Brian Wellington committed
18
/* $Id: resolver.c,v 1.206 2001/02/28 21:19:53 bwelling Exp $ */
David Lawrence's avatar
David Lawrence committed
19

20
21
#include <config.h>

Bob Halley's avatar
Bob Halley committed
22
#include <isc/task.h>
23
#include <isc/timer.h>
Michael Graff's avatar
Michael Graff committed
24
#include <isc/util.h>
Bob Halley's avatar
Bob Halley committed
25

26
#include <dns/acl.h>
Bob Halley's avatar
Bob Halley committed
27
#include <dns/adb.h>
Bob Halley's avatar
Bob Halley committed
28
#include <dns/db.h>
29
#include <dns/dispatch.h>
Bob Halley's avatar
Bob Halley committed
30
#include <dns/events.h>
31
#include <dns/forward.h>
Bob Halley's avatar
Bob Halley committed
32
#include <dns/keytable.h>
33
#include <dns/log.h>
Bob Halley's avatar
Bob Halley committed
34
#include <dns/message.h>
35
#include <dns/ncache.h>
36
#include <dns/peer.h>
37
#include <dns/rdata.h>
Bob Halley's avatar
Bob Halley committed
38
#include <dns/rdatalist.h>
Bob Halley's avatar
Bob Halley committed
39
#include <dns/rdataset.h>
40
#include <dns/rdatastruct.h>
41
#include <dns/rdatatype.h>
42
43
#include <dns/resolver.h>
#include <dns/result.h>
44
#include <dns/tsig.h>
45
#include <dns/validator.h>
Bob Halley's avatar
Bob Halley committed
46

Bob Halley's avatar
Bob Halley committed
47
48
#define DNS_RESOLVER_TRACE
#ifdef DNS_RESOLVER_TRACE
49
50
51
#define RTRACE(m)	isc_log_write(dns_lctx, \
				      DNS_LOGCATEGORY_RESOLVER, \
				      DNS_LOGMODULE_RESOLVER, \
52
				      ISC_LOG_DEBUG(3), \
53
54
55
56
				      "res %p: %s", res, (m))
#define RRTRACE(r, m)	isc_log_write(dns_lctx, \
				      DNS_LOGCATEGORY_RESOLVER, \
				      DNS_LOGMODULE_RESOLVER, \
57
				      ISC_LOG_DEBUG(3), \
58
59
60
61
				      "res %p: %s", (r), (m))
#define FCTXTRACE(m)	isc_log_write(dns_lctx, \
				      DNS_LOGCATEGORY_RESOLVER, \
				      DNS_LOGMODULE_RESOLVER, \
62
				      ISC_LOG_DEBUG(3), \
63
				      "fctx %p: %s", fctx, (m))
64
65
66
67
68
69
#define FCTXTRACE2(m1, m2) \
			isc_log_write(dns_lctx, \
				      DNS_LOGCATEGORY_RESOLVER, \
				      DNS_LOGMODULE_RESOLVER, \
				      ISC_LOG_DEBUG(3), \
				      "fctx %p: %s %s", fctx, (m1), (m2))
70
71
72
#define FTRACE(m)	isc_log_write(dns_lctx, \
				      DNS_LOGCATEGORY_RESOLVER, \
				      DNS_LOGMODULE_RESOLVER, \
73
				      ISC_LOG_DEBUG(3), \
74
75
76
77
78
				      "fetch %p (fctx %p): %s", \
				      fetch, fetch->private, (m))
#define QTRACE(m)	isc_log_write(dns_lctx, \
				      DNS_LOGCATEGORY_RESOLVER, \
				      DNS_LOGMODULE_RESOLVER, \
79
				      ISC_LOG_DEBUG(3), \
80
81
				      "resquery %p (fctx %p): %s", \
				      query, query->fctx, (m))
Bob Halley's avatar
Bob Halley committed
82
83
84
85
86
#else
#define RTRACE(m)
#define RRTRACE(r, m)
#define FCTXTRACE(m)
#define FTRACE(m)
Bob Halley's avatar
Bob Halley committed
87
#define QTRACE(m)
Bob Halley's avatar
Bob Halley committed
88
89
#endif

Bob Halley's avatar
Bob Halley committed
90
91
92
/*
 * Maximum EDNS0 input packet size.
 */
Bob Halley's avatar
Bob Halley committed
93
#define SEND_BUFFER_SIZE		2048		/* XXXRTH  Constant. */
94

95
/*
96
 * This defines the maximum number of timeouts we will permit before we
97
98
 * disable EDNS0 on the query.
 */
99
#define MAX_EDNS0_TIMEOUTS	3
100

Bob Halley's avatar
Bob Halley committed
101
102
typedef struct fetchctx fetchctx_t;

Bob Halley's avatar
Bob Halley committed
103
typedef struct query {
Bob Halley's avatar
Bob Halley committed
104
	/* Locked by task event serialization. */
Bob Halley's avatar
Bob Halley committed
105
106
	unsigned int			magic;
	fetchctx_t *			fctx;
107
	isc_mem_t *			mctx;
108
	dns_dispatchmgr_t *		dispatchmgr;
Bob Halley's avatar
Bob Halley committed
109
	dns_dispatch_t *		dispatch;
Bob Halley's avatar
Bob Halley committed
110
	dns_adbaddrinfo_t *		addrinfo;
111
	isc_socket_t *			tcpsocket;
Bob Halley's avatar
Bob Halley committed
112
	isc_time_t			start;
Bob Halley's avatar
Bob Halley committed
113
	dns_messageid_t			id;
Bob Halley's avatar
Bob Halley committed
114
	dns_dispentry_t *		dispentry;
Bob Halley's avatar
Bob Halley committed
115
	ISC_LINK(struct query)		link;
Bob Halley's avatar
Bob Halley committed
116
	isc_buffer_t			buffer;
117
	isc_buffer_t			*tsig;
118
	dns_tsigkey_t			*tsigkey;
Bob Halley's avatar
Bob Halley committed
119
	unsigned int			options;
120
	unsigned int			attributes;
121
	unsigned int			sends;
Bob Halley's avatar
Bob Halley committed
122
	unsigned char			data[512];
Bob Halley's avatar
Bob Halley committed
123
124
} resquery_t;

Bob Halley's avatar
Bob Halley committed
125
126
127
128
#define QUERY_MAGIC			0x51212121U	/* Q!!! */
#define VALID_QUERY(query)		((query) != NULL && \
					 (query)->magic == QUERY_MAGIC)

129
130
131
132
133
134
135
#define RESQUERY_ATTR_CONNECTING	0x01
#define RESQUERY_ATTR_CANCELED		0x02

#define RESQUERY_CONNECTING(q)		(((q)->attributes & \
					  RESQUERY_ATTR_CONNECTING) != 0)
#define RESQUERY_CANCELED(q)		(((q)->attributes & \
					  RESQUERY_ATTR_CANCELED) != 0)
136
#define RESQUERY_SENDING(q)		((q)->sends > 0)
137

Bob Halley's avatar
Bob Halley committed
138
typedef enum {
Bob Halley's avatar
Bob Halley committed
139
	fetchstate_init = 0,		/* Start event has not run yet. */
Bob Halley's avatar
Bob Halley committed
140
	fetchstate_active,
Bob Halley's avatar
Bob Halley committed
141
	fetchstate_done			/* FETCHDONE events posted. */
Bob Halley's avatar
Bob Halley committed
142
143
} fetchstate;

Bob Halley's avatar
Bob Halley committed
144
145
struct fetchctx {
	/* Not locked. */
Bob Halley's avatar
Bob Halley committed
146
147
148
	unsigned int			magic;
	dns_resolver_t *		res;
	dns_name_t			name;
Bob Halley's avatar
Bob Halley committed
149
	dns_rdatatype_t			type;
Bob Halley's avatar
Bob Halley committed
150
	unsigned int			options;
Bob Halley's avatar
Bob Halley committed
151
	unsigned int			bucketnum;
Bob Halley's avatar
Bob Halley committed
152
	/* Locked by appropriate bucket lock. */
Bob Halley's avatar
Bob Halley committed
153
	fetchstate			state;
Bob Halley's avatar
Bob Halley committed
154
	isc_boolean_t			want_shutdown;
155
	isc_boolean_t			cloned;
Bob Halley's avatar
Bob Halley committed
156
	unsigned int			references;
Bob Halley's avatar
lint    
Bob Halley committed
157
	isc_event_t			control_event;
Bob Halley's avatar
Bob Halley committed
158
159
	ISC_LINK(struct fetchctx)	link;
	ISC_LIST(dns_fetchevent_t)	events;
Bob Halley's avatar
Bob Halley committed
160
	/* Locked by task event serialization. */
Bob Halley's avatar
Bob Halley committed
161
162
	dns_name_t			domain;
	dns_rdataset_t			nameservers;
Bob Halley's avatar
Bob Halley committed
163
	unsigned int			attributes;
Bob Halley's avatar
Bob Halley committed
164
165
166
	isc_timer_t *			timer;
	isc_time_t			expires;
	isc_interval_t			interval;
Bob Halley's avatar
Bob Halley committed
167
168
	dns_message_t *			qmessage;
	dns_message_t *			rmessage;
Bob Halley's avatar
Bob Halley committed
169
	ISC_LIST(resquery_t)		queries;
Bob Halley's avatar
Bob Halley committed
170
	dns_adbfindlist_t		finds;
Bob Halley's avatar
Bob Halley committed
171
	dns_adbfind_t *			find;
172
173
	dns_adbaddrinfolist_t		forwaddrs;
	isc_sockaddrlist_t		forwarders;
174
	dns_fwdpolicy_t			fwdpolicy;
Bob Halley's avatar
Bob Halley committed
175
	isc_sockaddrlist_t		bad;
176
	ISC_LIST(dns_validator_t)	validators;
177

Bob Halley's avatar
Bob Halley committed
178
	/*
179
	 * The number of events we're waiting for.
Bob Halley's avatar
Bob Halley committed
180
	 */
Bob Halley's avatar
Bob Halley committed
181
	unsigned int			pending;
182

183
	/*
184
185
186
187
188
189
	 * The number of times we've "restarted" the current
	 * nameserver set.  This acts as a failsafe to prevent
	 * us from pounding constantly on a particular set of
	 * servers that, for whatever reason, are not giving
	 * us useful responses, but are responding in such a
	 * way that they are not marked "bad".
190
	 */
Bob Halley's avatar
Bob Halley committed
191
	unsigned int			restarts;
192

193
	/*
194
195
196
	 * The number of timeouts that have occurred since we 
	 * last successfully received a response packet.  This
	 * is used for EDNS0 black hole detection.
197
198
	 */
	unsigned int			timeouts;
Bob Halley's avatar
Bob Halley committed
199
};
Bob Halley's avatar
Bob Halley committed
200
201
202
203
204

#define FCTX_MAGIC			0x46212121U	/* F!!! */
#define VALID_FCTX(fctx)		((fctx) != NULL && \
					 (fctx)->magic == FCTX_MAGIC)

Bob Halley's avatar
Bob Halley committed
205
206
#define FCTX_ATTR_HAVEANSWER		0x01
#define FCTX_ATTR_GLUING		0x02
Bob Halley's avatar
Bob Halley committed
207
#define FCTX_ATTR_ADDRWAIT		0x04
Bob Halley's avatar
Bob Halley committed
208
#define FCTX_ATTR_SHUTTINGDOWN		0x08
Bob Halley's avatar
Bob Halley committed
209
210
#define FCTX_ATTR_WANTCACHE		0x10
#define FCTX_ATTR_WANTNCACHE		0x20
211
#define FCTX_ATTR_NEEDEDNS0		0x40
Bob Halley's avatar
Bob Halley committed
212
213
214
215
216

#define HAVE_ANSWER(f)		(((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
				 0)
#define GLUING(f)		(((f)->attributes & FCTX_ATTR_GLUING) != \
				 0)
Bob Halley's avatar
Bob Halley committed
217
218
#define ADDRWAIT(f)		(((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
				 0)
Bob Halley's avatar
Bob Halley committed
219
220
#define SHUTTINGDOWN(f)		(((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
 				 != 0)
Bob Halley's avatar
Bob Halley committed
221
222
#define WANTCACHE(f)		(((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
#define WANTNCACHE(f)		(((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
223
#define NEEDEDNS0(f)		(((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
Bob Halley's avatar
Bob Halley committed
224

Bob Halley's avatar
Bob Halley committed
225
226
struct dns_fetch {
	unsigned int			magic;
227
	fetchctx_t *			private;
Bob Halley's avatar
Bob Halley committed
228
229
230
231
232
233
234
235
236
237
};

#define DNS_FETCH_MAGIC			0x46746368U	/* Ftch */
#define DNS_FETCH_VALID(fetch)		((fetch) != NULL && \
					 (fetch)->magic == DNS_FETCH_MAGIC)

typedef struct fctxbucket {
	isc_task_t *			task;
	isc_mutex_t			lock;
	ISC_LIST(fetchctx_t)		fctxs;
Bob Halley's avatar
Bob Halley committed
238
	isc_boolean_t			exiting;
Bob Halley's avatar
Bob Halley committed
239
240
} fctxbucket_t;

Bob Halley's avatar
Bob Halley committed
241
struct dns_resolver {
Bob Halley's avatar
Bob Halley committed
242
	/* Unlocked. */
Bob Halley's avatar
Bob Halley committed
243
244
245
246
	unsigned int			magic;
	isc_mem_t *			mctx;
	isc_mutex_t			lock;
	dns_rdataclass_t		rdclass;
Bob Halley's avatar
Bob Halley committed
247
	isc_socketmgr_t *		socketmgr;
Bob Halley's avatar
Bob Halley committed
248
	isc_timermgr_t *		timermgr;
249
	isc_taskmgr_t *			taskmgr;
250
	dns_view_t *			view;
251
	isc_boolean_t			frozen;
252
	unsigned int			options;
253
	dns_dispatchmgr_t *		dispatchmgr;
254
255
	dns_dispatch_t *		dispatchv4;
	dns_dispatch_t *		dispatchv6;
Bob Halley's avatar
Bob Halley committed
256
257
	unsigned int			nbuckets;
	fctxbucket_t *			buckets;
Mark Andrews's avatar
Mark Andrews committed
258
	isc_uint32_t			lame_ttl;
259
260
261
262
263
	/* Locked by lock. */
	unsigned int			references;
	isc_boolean_t			exiting;
	isc_eventlist_t			whenshutdown;
	unsigned int			activebuckets;
Bob Halley's avatar
Bob Halley committed
264
265
	isc_boolean_t			priming;
	dns_fetch_t *			primefetch;
Bob Halley's avatar
Bob Halley committed
266
267
268
269
270
271
};

#define RES_MAGIC			0x52657321U	/* Res! */
#define VALID_RESOLVER(res)		((res) != NULL && \
					 (res)->magic == RES_MAGIC)

272
273
274
275
276
277
278
279
280
281
/*
 * Private addrinfo flags.  These must not conflict with DNS_FETCHOPT_NOEDNS0,
 * which we also use as an addrinfo flag.
 */
#define FCTX_ADDRINFO_MARK		0x0001
#define FCTX_ADDRINFO_FORWARDER		0x1000
#define UNMARKED(a)			(((a)->flags & FCTX_ADDRINFO_MARK) \
					 == 0)
#define ISFORWARDER(a)			(((a)->flags & \
					 FCTX_ADDRINFO_FORWARDER) != 0)
Bob Halley's avatar
Bob Halley committed
282
283

static void destroy(dns_resolver_t *res);
Bob Halley's avatar
Bob Halley committed
284
static void empty_bucket(dns_resolver_t *res);
Bob Halley's avatar
Bob Halley committed
285
static isc_result_t resquery_send(resquery_t *query);
Bob Halley's avatar
Bob Halley committed
286
static void resquery_response(isc_task_t *task, isc_event_t *event);
Bob Halley's avatar
Bob Halley committed
287
static void resquery_connected(isc_task_t *task, isc_event_t *event);
Bob Halley's avatar
Bob Halley committed
288
static void fctx_try(fetchctx_t *fctx);
Bob Halley's avatar
Bob Halley committed
289
static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
290
291
292
static isc_result_t ncache_adderesult(dns_message_t *message,
				      dns_db_t *cache, dns_dbnode_t *node,
				      dns_rdatatype_t covers,
293
				      isc_stdtime_t now, dns_ttl_t maxttl,
294
295
				      dns_rdataset_t *ardataset,
				      isc_result_t *eresultp);
Bob Halley's avatar
Bob Halley committed
296

Bob Halley's avatar
Bob Halley committed
297
static inline isc_result_t
Bob Halley's avatar
Bob Halley committed
298
fctx_starttimer(fetchctx_t *fctx) {
299
300
	/*
	 * Start the lifetime timer for fctx.
301
302
303
304
	 *
	 * This is also used for stopping the idle timer; in that
	 * case we must purge events already posted to ensure that
	 * no further idle events are delivered.
305
	 */
Bob Halley's avatar
Bob Halley committed
306
307
	return (isc_timer_reset(fctx->timer, isc_timertype_once,
				&fctx->expires, NULL,
308
				ISC_TRUE));
Bob Halley's avatar
Bob Halley committed
309
310
}

Bob Halley's avatar
Bob Halley committed
311
312
static inline void
fctx_stoptimer(fetchctx_t *fctx) {
Bob Halley's avatar
Bob Halley committed
313
	isc_result_t result;
Bob Halley's avatar
Bob Halley committed
314
315
316
317
318
319
320

	/*
	 * We don't return a result if resetting the timer to inactive fails
	 * since there's nothing to be done about it.  Resetting to inactive
	 * should never fail anyway, since the code as currently written
	 * cannot fail in that case.
	 */
Bob Halley's avatar
Bob Halley committed
321
	result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
Bob Halley's avatar
Bob Halley committed
322
				  NULL, NULL, ISC_TRUE);
Bob Halley's avatar
Bob Halley committed
323
	if (result != ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
324
325
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_timer_reset(): %s",
Bob Halley's avatar
Bob Halley committed
326
				 isc_result_totext(result));
Bob Halley's avatar
Bob Halley committed
327
328
329
	}
}

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

static inline isc_result_t
fctx_startidletimer(fetchctx_t *fctx) {
	/*
	 * Start the idle timer for fctx.  The lifetime timer continues
	 * to be in effect.
	 */
	return (isc_timer_reset(fctx->timer, isc_timertype_once,
				&fctx->expires, &fctx->interval,
				ISC_FALSE));
}

/*
 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
 * we use fctx_stopidletimer for readability in the code below.
 */
#define fctx_stopidletimer	fctx_starttimer


349
350
351
static inline void
resquery_destroy(resquery_t **queryp) {
	resquery_t *query;
352

353
354
355
356
	REQUIRE(queryp != NULL);
	query = *queryp;
	REQUIRE(!ISC_LINK_LINKED(query, link));

357
	INSIST(query->tcpsocket == NULL);
358

359
	query->magic = 0;
360
	isc_mem_put(query->mctx, query, sizeof(*query));
361
362
363
	*queryp = NULL;
}

Bob Halley's avatar
Bob Halley committed
364
static void
Bob Halley's avatar
Bob Halley committed
365
fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
366
		 isc_time_t *finish, isc_boolean_t no_response)
Bob Halley's avatar
Bob Halley committed
367
{
Bob Halley's avatar
Bob Halley committed
368
369
	fetchctx_t *fctx;
	resquery_t *query;
Bob Halley's avatar
Bob Halley committed
370
371
	unsigned int rtt;
	unsigned int factor;
Bob Halley's avatar
Bob Halley committed
372

Bob Halley's avatar
Bob Halley committed
373
374
375
	query = *queryp;
	fctx = query->fctx;

Bob Halley's avatar
Bob Halley committed
376
377
	FCTXTRACE("cancelquery");

378
379
380
381
	REQUIRE(!RESQUERY_CANCELED(query));

	query->attributes |= RESQUERY_ATTR_CANCELED;

Bob Halley's avatar
Bob Halley committed
382
	/*
383
	 * Should we update the RTT?
Bob Halley's avatar
Bob Halley committed
384
	 */
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	if (finish != NULL || no_response) {
		if (finish != NULL) {
			/*
			 * We have both the start and finish times for this
			 * packet, so we can compute a real RTT.
			 */
			rtt = (unsigned int)isc_time_microdiff(finish,
							       &query->start);
			factor = DNS_ADB_RTTADJDEFAULT;
		} else {
			/*
			 * We don't have an RTT for this query.  Maybe the
			 * packet was lost, or maybe this server is very
			 * slow.  We don't know.  Increase the RTT.
			 */
			INSIST(no_response);
			rtt = query->addrinfo->srtt +
				(100000 * fctx->restarts);
			if (rtt > 10000000)
				rtt = 10000000;
			/*
			 * Replace the current RTT with our value.
			 */
			factor = DNS_ADB_RTTADJREPLACE;
		}
		dns_adb_adjustsrtt(fctx->res->view->adb, query->addrinfo, rtt,
				   factor);
Bob Halley's avatar
Bob Halley committed
412
413
	}

Bob Halley's avatar
Bob Halley committed
414
	if (query->dispentry != NULL)
415
		dns_dispatch_removeresponse(&query->dispentry, deventp);
416

Bob Halley's avatar
Bob Halley committed
417
	ISC_LIST_UNLINK(fctx->queries, query, link);
418

419
420
	if (query->tsig != NULL)
		isc_buffer_free(&query->tsig);
421

422
423
424
	if (query->tsigkey != NULL)
		dns_tsigkey_detach(&query->tsigkey);

425
426
427
428
429
430
431
	/*
	 * Check for any outstanding socket events.  If they exist, cancel
	 * them and let the event handlers finish the cleanup.  The resolver
	 * only needs to worry about managing the connect and send events;
	 * the dispatcher manages the recv events.
	 */
	if (RESQUERY_CONNECTING(query))
432
433
434
		/*
		 * Cancel the connect.
		 */
435
436
		isc_socket_cancel(query->tcpsocket, NULL,
				  ISC_SOCKCANCEL_CONNECT);
437
438
439
440
441
442
	else if (RESQUERY_SENDING(query))
		/*
		 * Cancel the pending send.
		 */
		isc_socket_cancel(dns_dispatch_getsocket(query->dispatch),
				  NULL, ISC_SOCKCANCEL_SEND);
443
444
445

	if (query->dispatch != NULL)
		dns_dispatch_detach(&query->dispatch);
446

447
	if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
448
449
450
451
		/*
		 * It's safe to destroy the query now.
		 */
		resquery_destroy(&query);
Bob Halley's avatar
Bob Halley committed
452
453
454
}

static void
455
fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
Bob Halley's avatar
Bob Halley committed
456
457
458
459
460
461
462
463
	resquery_t *query, *next_query;

	FCTXTRACE("cancelqueries");

	for (query = ISC_LIST_HEAD(fctx->queries);
	     query != NULL;
	     query = next_query) {
		next_query = ISC_LIST_NEXT(query, link);
464
		fctx_cancelquery(&query, NULL, NULL, no_response);
Bob Halley's avatar
Bob Halley committed
465
466
467
	}
}

Bob Halley's avatar
Bob Halley committed
468
static void
Bob Halley's avatar
Bob Halley committed
469
470
471
fctx_cleanupfinds(fetchctx_t *fctx) {
	dns_adbfind_t *find, *next_find;

Bob Halley's avatar
Bob Halley committed
472
473
	REQUIRE(ISC_LIST_EMPTY(fctx->queries));

Bob Halley's avatar
Bob Halley committed
474
475
476
477
478
479
	for (find = ISC_LIST_HEAD(fctx->finds);
	     find != NULL;
	     find = next_find) {
		next_find = ISC_LIST_NEXT(find, publink);
		ISC_LIST_UNLINK(fctx->finds, find, publink);
		dns_adb_destroyfind(&find);
Bob Halley's avatar
Bob Halley committed
480
	}
Bob Halley's avatar
Bob Halley committed
481
	fctx->find = NULL;
Bob Halley's avatar
Bob Halley committed
482
483
}

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
static void
fctx_cleanupforwaddrs(fetchctx_t *fctx) {
	dns_adbaddrinfo_t *addr, *next_addr;

	REQUIRE(ISC_LIST_EMPTY(fctx->queries));

	for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
	     addr != NULL;
	     addr = next_addr) {
		next_addr = ISC_LIST_NEXT(addr, publink);
		ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
		dns_adb_freeaddrinfo(fctx->res->view->adb, &addr);
	}
}

Bob Halley's avatar
Bob Halley committed
499
static inline void
500
fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
Bob Halley's avatar
Bob Halley committed
501
	FCTXTRACE("stopeverything");
502
	fctx_cancelqueries(fctx, no_response);
Bob Halley's avatar
Bob Halley committed
503
	fctx_cleanupfinds(fctx);
504
	fctx_cleanupforwaddrs(fctx);
Bob Halley's avatar
Bob Halley committed
505
	fctx_stoptimer(fctx);
Bob Halley's avatar
Bob Halley committed
506
}
Bob Halley's avatar
Bob Halley committed
507

Bob Halley's avatar
Bob Halley committed
508
509
510
511
static inline void
fctx_sendevents(fetchctx_t *fctx, isc_result_t result) {
	dns_fetchevent_t *event, *next_event;
	isc_task_t *task;
Bob Halley's avatar
Bob Halley committed
512

Bob Halley's avatar
Bob Halley committed
513
514
515
516
517
518
	/*
	 * Caller must be holding the appropriate bucket lock.
	 */
	REQUIRE(fctx->state == fetchstate_done);

	FCTXTRACE("sendevents");
Bob Halley's avatar
Bob Halley committed
519
520
521
522

	for (event = ISC_LIST_HEAD(fctx->events);
	     event != NULL;
	     event = next_event) {
523
		next_event = ISC_LIST_NEXT(event, ev_link);
524
		ISC_LIST_UNLINK(fctx->events, event, ev_link);
525
526
		task = event->ev_sender;
		event->ev_sender = fctx;
Bob Halley's avatar
Bob Halley committed
527
		if (!HAVE_ANSWER(fctx))
528
			event->result = result;
529
530
531
532
533
534

		INSIST(result != ISC_R_SUCCESS ||
		       dns_rdataset_isassociated(event->rdataset) ||
		       fctx->type == dns_rdatatype_any ||
		       fctx->type == dns_rdatatype_sig);

535
		isc_task_sendanddetach(&task, (isc_event_t **)&event);
Bob Halley's avatar
Bob Halley committed
536
	}
Bob Halley's avatar
Bob Halley committed
537
}
Bob Halley's avatar
Bob Halley committed
538

Bob Halley's avatar
Bob Halley committed
539
540
541
static void
fctx_done(fetchctx_t *fctx, isc_result_t result) {
	dns_resolver_t *res;
542
	isc_boolean_t no_response;
Bob Halley's avatar
Bob Halley committed
543
544
545
546
547

	FCTXTRACE("done");

	res = fctx->res;

548
549
550
551
552
	if (result == ISC_R_SUCCESS)
		no_response = ISC_TRUE;
	else
		no_response = ISC_FALSE;
	fctx_stopeverything(fctx, no_response);
Bob Halley's avatar
Bob Halley committed
553
554
555
556
557

	LOCK(&res->buckets[fctx->bucketnum].lock);

	fctx->state = fetchstate_done;
	fctx_sendevents(fctx, result);
Bob Halley's avatar
Bob Halley committed
558
559

	UNLOCK(&res->buckets[fctx->bucketnum].lock);
Bob Halley's avatar
Bob Halley committed
560
561
}

Bob Halley's avatar
Bob Halley committed
562
static void
Bob Halley's avatar
Bob Halley committed
563
resquery_senddone(isc_task_t *task, isc_event_t *event) {
Bob Halley's avatar
Bob Halley committed
564
	isc_socketevent_t *sevent = (isc_socketevent_t *)event;
565
	resquery_t *query = event->ev_arg;
Bob Halley's avatar
Bob Halley committed
566

567
	REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
Bob Halley's avatar
Bob Halley committed
568

Bob Halley's avatar
Bob Halley committed
569
570
	QTRACE("senddone");

Bob Halley's avatar
Bob Halley committed
571
572
573
574
575
576
577
578
	/*
	 * XXXRTH
	 *
	 * Currently we don't wait for the senddone event before retrying
	 * a query.  This means that if we get really behind, we may end
	 * up doing extra work!
	 */

Andreas Gustafsson's avatar
Andreas Gustafsson committed
579
	UNUSED(task);
Bob Halley's avatar
Bob Halley committed
580

581
582
583
584
	INSIST(RESQUERY_SENDING(query));

	query->sends--;

585
586
587
588
589
590
591
592
593
594
595
596
	if (RESQUERY_CANCELED(query)) {
		if (query->sends == 0) {
			/*
			 * This query was canceled while the
			 * isc_socket_sendto() was in progress.
			 */
			if (query->tcpsocket != NULL)
				isc_socket_detach(&query->tcpsocket);
			resquery_destroy(&query);
		}
	} else if (sevent->result != ISC_R_SUCCESS)
		fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
597

Bob Halley's avatar
Bob Halley committed
598
599
600
	isc_event_free(&event);
}

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
static void
resquery_aborted(isc_task_t *task, isc_event_t *event) {
	resquery_t *query = event->ev_arg;

	REQUIRE(event->ev_type == DNS_EVENT_QUERYABORTED);

	QTRACE("blackholed");

	UNUSED(task);

	/*
	 * Treat this as a "no response" to cause the RTT estimate to go up.
	 */
	fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);

	isc_event_free(&event);
}

Bob Halley's avatar
Bob Halley committed
619
620
621
622
623
static inline isc_result_t
fctx_addopt(dns_message_t *message) {
	dns_rdataset_t *rdataset;
	dns_rdatalist_t *rdatalist;
	dns_rdata_t *rdata;
Bob Halley's avatar
Bob Halley committed
624
	isc_result_t result;
Bob Halley's avatar
Bob Halley committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

	rdatalist = NULL;
	result = dns_message_gettemprdatalist(message, &rdatalist);
	if (result != ISC_R_SUCCESS)
		return (result);
	rdata = NULL;
	result = dns_message_gettemprdata(message, &rdata);
	if (result != ISC_R_SUCCESS)
		return (result);
	rdataset = NULL;
	result = dns_message_gettemprdataset(message, &rdataset);
	if (result != ISC_R_SUCCESS)
		return (result);
	dns_rdataset_init(rdataset);

	rdatalist->type = dns_rdatatype_opt;
	rdatalist->covers = 0;

	/*
	 * Set Maximum UDP buffer size.
	 */
	rdatalist->rdclass = SEND_BUFFER_SIZE;

	/*
649
	 * Set EXTENDED-RCODE, VERSION, and Z to 0, and the DO bit to 1.
Bob Halley's avatar
Bob Halley committed
650
	 */
651
	rdatalist->ttl = DNS_MESSAGEEXTFLAG_DO;
Bob Halley's avatar
Bob Halley committed
652
653

	/*
654
	 * No EDNS options.
Bob Halley's avatar
Bob Halley committed
655
656
657
	 */
	rdata->data = NULL;
	rdata->length = 0;
658
659
	rdata->rdclass = rdatalist->rdclass;
	rdata->type = rdatalist->type;
660
	rdata->flags = 0;
Bob Halley's avatar
Bob Halley committed
661
662
663
664
665
666
667
668

	ISC_LIST_INIT(rdatalist->rdata);
	ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
	dns_rdatalist_tordataset(rdatalist, rdataset);

	return (dns_message_setopt(message, rdataset));
}

669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
static inline void
fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
	unsigned int seconds;

	/*
	 * We retry every 2 seconds the first two times through the address
	 * list, and then we do exponential back-off.
	 */
	if (fctx->restarts < 3)
		seconds = 2;
	else
		seconds = (2 << (fctx->restarts - 1));

	/*
	 * Double the round-trip time and convert to seconds.
	 */
	rtt /= 500000;
686

687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
	/*
	 * Always wait for at least the doubled round-trip time.
	 */
	if (seconds < rtt)
		seconds = rtt;

	/*
	 * But don't ever wait for more than 30 seconds.
	 */
	if (seconds > 30)
		seconds = 30;

	isc_interval_set(&fctx->interval, seconds, 0);
}

Bob Halley's avatar
Bob Halley committed
702
703
704
705
static isc_result_t
fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
	   unsigned int options)
{
Bob Halley's avatar
Bob Halley committed
706
707
	dns_resolver_t *res;
	isc_task_t *task;
Bob Halley's avatar
Bob Halley committed
708
709
	isc_result_t result;
	resquery_t *query;
Bob Halley's avatar
Bob Halley committed
710

Bob Halley's avatar
Bob Halley committed
711
	FCTXTRACE("query");
Bob Halley's avatar
Bob Halley committed
712

Bob Halley's avatar
Bob Halley committed
713
714
	res = fctx->res;
	task = res->buckets[fctx->bucketnum].task;
Bob Halley's avatar
Bob Halley committed
715

716
	fctx_setretryinterval(fctx, addrinfo->srtt);
717
	result = fctx_startidletimer(fctx);
Bob Halley's avatar
Bob Halley committed
718
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
719
720
		return (result);

Bob Halley's avatar
Bob Halley committed
721
	dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
Bob Halley's avatar
Bob Halley committed
722

Bob Halley's avatar
Bob Halley committed
723
	query = isc_mem_get(res->mctx, sizeof *query);
Bob Halley's avatar
Bob Halley committed
724
	if (query == NULL) {
725
		result = ISC_R_NOMEMORY;
726
		goto stop_idle_timer;
Bob Halley's avatar
Bob Halley committed
727
	}
728
	query->mctx = res->mctx;
Bob Halley's avatar
Bob Halley committed
729
	query->options = options;
730
	query->attributes = 0;
731
	query->sends = 0;
Bob Halley's avatar
Bob Halley committed
732
733
734
735
736
737
738
739
	/*
	 * Note that the caller MUST guarantee that 'addrinfo' will remain
	 * valid until this query is canceled.
	 */
	query->addrinfo = addrinfo;
	result = isc_time_now(&query->start);
	if (result != ISC_R_SUCCESS)
		goto cleanup_query;
740

Bob Halley's avatar
Bob Halley committed
741
742
743
	/*
	 * If this is a TCP query, then we need to make a socket and
	 * a dispatch for it here.  Otherwise we use the resolver's
Bob Halley's avatar
Bob Halley committed
744
745
	 * shared dispatch.
	 */
746
	query->dispatchmgr = res->dispatchmgr;
Bob Halley's avatar
Bob Halley committed
747
	query->dispatch = NULL;
748
	query->tcpsocket = NULL;
Bob Halley's avatar
Bob Halley committed
749
	if ((query->options & DNS_FETCHOPT_TCP) != 0) {
750
		isc_sockaddr_t any;
751
		int pf;
752

753
754
		pf = isc_sockaddr_pf(&addrinfo->sockaddr);

755
		result = isc_socket_create(res->socketmgr, pf,
Bob Halley's avatar
Bob Halley committed
756
					   isc_sockettype_tcp,
757
					   &query->tcpsocket);
Bob Halley's avatar
Bob Halley committed
758
759
		if (result != ISC_R_SUCCESS)
			goto cleanup_query;
760

761
		isc_sockaddr_anyofpf(&any, pf);
762
		result = isc_socket_bind(query->tcpsocket, &any);
763
764
		if (result != ISC_R_SUCCESS)
			goto cleanup_socket;
765

Bob Halley's avatar
Bob Halley committed
766
		/*
767
		 * A dispatch will be created once the connect succeeds.
Bob Halley's avatar
Bob Halley committed
768
		 */
Bob Halley's avatar
Bob Halley committed
769
	} else {
770
		switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
Bob Halley's avatar
Bob Halley committed
771
		case PF_INET:
772
			dns_dispatch_attach(res->dispatchv4, &query->dispatch);
Bob Halley's avatar
Bob Halley committed
773
			break;
Bob Halley's avatar
Bob Halley committed
774
		case PF_INET6:
775
			dns_dispatch_attach(res->dispatchv6, &query->dispatch);
Bob Halley's avatar
Bob Halley committed
776
777
			break;
		default:
778
			result = ISC_R_NOTIMPLEMENTED;
779
			goto cleanup_query;
Bob Halley's avatar
Bob Halley committed
780
781
782
783
784
785
786
787
788
789
		}
		/*
		 * We should always have a valid dispatcher here.  If we
		 * don't support a protocol family, then its dispatcher
		 * will be NULL, but we shouldn't be finding addresses for
		 * protocol types we don't support, so the dispatcher
		 * we found should never be NULL.
		 */
		INSIST(query->dispatch != NULL);
	}
Bob Halley's avatar
Bob Halley committed
790
791
792
793
794

	query->dispentry = NULL;
	query->fctx = fctx;
	query->tsig = NULL;
	query->tsigkey = NULL;
795
	ISC_LINK_INIT(query, link);
Bob Halley's avatar
Bob Halley committed
796
797
798
799
800
801
802
803
	query->magic = QUERY_MAGIC;

	if ((query->options & DNS_FETCHOPT_TCP) != 0) {
		/*
		 * Connect to the remote server.
		 *
		 * XXXRTH  Should we attach to the socket?
		 */
David Lawrence's avatar
David Lawrence committed
804
		result = isc_socket_connect(query->tcpsocket,
805
					    &addrinfo->sockaddr, task,
David Lawrence's avatar
David Lawrence committed
806
					    resquery_connected, query);
Bob Halley's avatar
Bob Halley committed
807
		if (result != ISC_R_SUCCESS)
808
			goto cleanup_socket;
809
		query->attributes |= RESQUERY_ATTR_CONNECTING;
Bob Halley's avatar
Bob Halley committed
810
811
812
813
814
815
816
		QTRACE("connecting via TCP");
	} else {
		result = resquery_send(query);
		if (result != ISC_R_SUCCESS)
			goto cleanup_dispatch;
	}

817
818
	ISC_LIST_APPEND(fctx->queries, query, link);

Bob Halley's avatar
Bob Halley committed
819
820
	return (ISC_R_SUCCESS);

821
822
823
 cleanup_socket:
	isc_socket_detach(&query->tcpsocket);

Bob Halley's avatar
Bob Halley committed
824
 cleanup_dispatch:
825
826
	if (query->dispatch != NULL)
		dns_dispatch_detach(&query->dispatch);
Bob Halley's avatar
Bob Halley committed
827
828
829
830
831

 cleanup_query:
	query->magic = 0;
	isc_mem_put(res->mctx, query, sizeof *query);

832
833
 stop_idle_timer:
	fctx_stopidletimer(fctx);
Bob Halley's avatar
Bob Halley committed
834
835
836
837
838
839
840
841

	return (result);
}

static isc_result_t
resquery_send(resquery_t *query) {
	fetchctx_t *fctx;
	isc_result_t result;
842
843
	dns_name_t *qname = NULL;
	dns_rdataset_t *qrdataset = NULL;
Bob Halley's avatar
Bob Halley committed
844
845
846
847
848
	isc_region_t r;
	dns_resolver_t *res;
	isc_task_t *task;
	isc_socket_t *socket;
	isc_buffer_t tcpbuffer;
849
	isc_sockaddr_t *address;
Bob Halley's avatar
Bob Halley committed
850
	isc_buffer_t *buffer;
851
	isc_netaddr_t ipaddr;
852
	dns_tsigkey_t *tsigkey = NULL;
853
	dns_acl_t *blackhole;
854
855
	dns_peer_t *peer = NULL;
	isc_boolean_t bogus;
856
	isc_boolean_t aborted = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
857
858
859
860
861
862
863
864
865
866
867
868

	fctx = query->fctx;
	QTRACE("send");

	res = fctx->res;
	task = res->buckets[fctx->bucketnum].task;
	address = NULL;

	if ((query->options & DNS_FETCHOPT_TCP) != 0) {
		/*
		 * Reserve space for the TCP message length.
		 */
869
		isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
Bob Halley's avatar
Bob Halley committed
870
		isc_buffer_init(&query->buffer, query->data + 2,
871
				sizeof(query->data) - 2);
Bob Halley's avatar
Bob Halley committed
872
873
874
		buffer = &tcpbuffer;
	} else {
		isc_buffer_init(&query->buffer, query->data,
875
				sizeof(query->data));
Bob Halley's avatar
Bob Halley committed
876
877
878
879
880
881
882
883
884
885
		buffer = &query->buffer;
	}

	result = dns_message_gettempname(fctx->qmessage, &qname);
	if (result != ISC_R_SUCCESS)
		goto cleanup_temps;
	result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
	if (result != ISC_R_SUCCESS)
		goto cleanup_temps;

Bob Halley's avatar
Bob Halley committed
886
	/*
Bob Halley's avatar
Bob Halley committed
887
	 * Get a query id from the dispatch.
Bob Halley's avatar
Bob Halley committed
888
	 */
Bob Halley's avatar
Bob Halley committed
889
	result = dns_dispatch_addresponse(query->dispatch,
890
					  &query->addrinfo->sockaddr,
Bob Halley's avatar
Bob Halley committed
891
					  task,
Bob Halley's avatar
Bob Halley committed
892
					  resquery_response,
Bob Halley's avatar
Bob Halley committed
893
894
895
					  query,
					  &query->id,
					  &query->dispentry);
Bob Halley's avatar
Bob Halley committed
896
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
897
		goto cleanup_temps;
Bob Halley's avatar
Bob Halley committed
898
899
900
901
902
903

	fctx->qmessage->opcode = dns_opcode_query;

	/*
	 * Set up question.
	 */
Bob Halley's avatar
Bob Halley committed
904
905
906
907
908
909
	dns_name_init(qname, NULL);
	dns_name_clone(&fctx->name, qname);
	dns_rdataset_init(qrdataset);
	dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
	ISC_LIST_APPEND(qname->list, qrdataset, link);
	dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
910
911
	qname = NULL;
	qrdataset = NULL;
912
913
914
915
916
917
918

	/*
	 * Set RD if the client has requested that we do a recursive query,
	 * or if we're sending to a forwarder.
	 */
	if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
	    ISFORWARDER(query->addrinfo))
Bob Halley's avatar
Bob Halley committed
919
		fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
920

Bob Halley's avatar
Bob Halley committed
921
922
923
	/*
	 * We don't have to set opcode because it defaults to query.
	 */
Bob Halley's avatar
Bob Halley committed
924
	fctx->qmessage->id = query->id;
Bob Halley's avatar
Bob Halley committed
925

Bob Halley's avatar
Bob Halley committed
926
927
928
	/*
	 * Convert the question to wire format.
	 */
Bob Halley's avatar
Bob Halley committed
929
	result = dns_message_renderbegin(fctx->qmessage, &query->buffer);
Bob Halley's avatar
Bob Halley committed
930
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
931
		goto cleanup_message;
Bob Halley's avatar
Bob Halley committed
932

Bob Halley's avatar
Bob Halley committed
933
	result = dns_message_rendersection(fctx->qmessage,
Michael Graff's avatar
Michael Graff committed
934
					   DNS_SECTION_QUESTION, 0);
Bob Halley's avatar
Bob Halley committed
935
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
936
		goto cleanup_message;
Bob Halley's avatar
Bob Halley committed
937
938
939
940
941

	/*
	 * Use EDNS0, unless the caller doesn't want it, or we know that
	 * the remote server doesn't like it.
	 */
942
943
	if (fctx->timeouts >= MAX_EDNS0_TIMEOUTS &&
	    (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
944
		query->options |= DNS_FETCHOPT_NOEDNS0;
945
		FCTXTRACE("too many timeouts, disabling EDNS0");
946
947
	}

Bob Halley's avatar
Bob Halley committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
	if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
		if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
			result = fctx_addopt(fctx->qmessage);
			if (result != ISC_R_SUCCESS) {
				/*
				 * We couldn't add the OPT, but we'll press on.
				 * We're not using EDNS0, so set the NOEDNS0
				 * bit.
				 */
				query->options |= DNS_FETCHOPT_NOEDNS0;
			}
		} else {
			/*
			 * We know this server doesn't like EDNS0, so we
			 * won't use it.  Set the NOEDNS0 bit since we're
			 * not using EDNS0.
			 */
			query->options |= DNS_FETCHOPT_NOEDNS0;
		}
	}

969
970
971
972
973
974
975
976
977
	/*
	 * If we need EDNS0 to do this query and aren't using it, we lose.
	 */
	if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
		result = DNS_R_SERVFAIL;
		goto cleanup_message;
	}

	/*
978
979
980
	 * If we're using EDNS, set CD.  CD and EDNS aren't really related,
	 * but if we send a non EDNS query, there's a chance the server
	 * won't understand CD either.
981
982
	 */
	if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
983
		fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
984

Bob Halley's avatar
Bob Halley committed
985
	/*
986
	 * Add TSIG record tailored to the current recipient.
Bob Halley's avatar
Bob Halley committed
987
	 */
988
	isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
989
990
991
992
993
994
995
	result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
		goto cleanup_message;

	if (tsigkey != NULL) {
		dns_message_settsigkey(fctx->qmessage, tsigkey);
		dns_tsigkey_detach(&tsigkey);
996
	}
Bob Halley's avatar
Bob Halley committed
997

Bob Halley's avatar
Bob Halley committed
998
	result = dns_message_rendersection(fctx->qmessage,
Michael Graff's avatar
Michael Graff committed
999
					   DNS_SECTION_ADDITIONAL, 0);
Bob Halley's avatar
Bob Halley committed
1000
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
1001
		goto cleanup_message;
Bob Halley's avatar
Bob Halley committed
1002

Bob Halley's avatar
Bob Halley committed
1003
	result = dns_message_renderend(fctx->qmessage);
Bob Halley's avatar
Bob Halley committed
1004
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
1005
1006
		goto cleanup_message;

1007
1008
1009
	if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
		dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
				   &query->tsigkey);
1010
1011
1012
1013
1014
		result = dns_message_getquerytsig(fctx->qmessage,
						  fctx->res->mctx,
						  &query->tsig);
		if (result != ISC_R_SUCCESS)
			goto cleanup_message;
1015
1016
	}

Bob Halley's avatar
Bob Halley committed
1017
1018
1019
1020
1021
	/*
	 * If using TCP, write the length of the message at the beginning
	 * of the buffer.
	 */
	if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1022
		isc_buffer_usedregion(&query->buffer, &r);
Bob Halley's avatar
Bob Halley committed
1023
1024
1025
1026
		isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
		isc_buffer_add(&tcpbuffer, r.length);
	}

Bob Halley's avatar
Bob Halley committed
1027
1028
1029
1030
	/*
	 * We're now done with the query message.
	 */
	dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
Bob Halley's avatar
Bob Halley committed
1031

Bob Halley's avatar
Bob Halley committed
1032
	socket = dns_dispatch_getsocket(query->dispatch);
Bob Halley's avatar
Bob Halley committed
1033
1034
1035
	/*
	 * Send the query!
	 */
1036
	if ((query->options & DNS_FETCHOPT_TCP) == 0)
1037
		address = &query->addrinfo->sockaddr;
1038
	isc_buffer_usedregion(buffer, &r);
1039

1040
1041

	blackhole = dns_dispatchmgr_getblackhole(query->dispatchmgr);
1042
1043
1044
	if (blackhole != NULL) {
		int match;

1045
		if (dns_acl_match(&ipaddr, NULL, blackhole,
1046
1047
				  NULL, &match, NULL) == ISC_R_SUCCESS &&
		    match > 0)
1048
			aborted = ISC_TRUE;
1049
1050
	}

1051
1052
1053
1054
1055
1056
	peer = NULL;
	result = dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr,
					 &peer);
	if (result == ISC_R_SUCCESS &&
	    dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
	    bogus)
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
		aborted = ISC_TRUE;

	if (aborted) {
		isc_event_t *event;
		event = isc_event_allocate(fctx->res->mctx, NULL,
					   DNS_EVENT_QUERYABORTED,
					   resquery_aborted, query,
					   sizeof(isc_event_t));
		if (event == NULL)
			return (ISC_R_NOMEMORY);
		isc_task_send(task, &event);
		result = ISC_R_SUCCESS;
1069
1070
1071
		goto cleanup_message;
	}

1072
1073
1074
1075
	/*
	 * XXXRTH  Make sure we don't send to ourselves!  We should probably
	 *         prune out these addresses when we get them from the ADB.
	 */
Bob Halley's avatar
Bob Halley committed
1076
	result = isc_socket_sendto(socket, &r, task, resquery_senddone,
Michael Graff's avatar
Michael Graff committed
1077
				   query, address, NULL);
Bob Halley's avatar
Bob Halley committed
1078
1079
	if (result != ISC_R_SUCCESS)
		goto cleanup_message;
1080
	query->sends++;
Bob Halley's avatar
Bob Halley committed
1081
	QTRACE("sent");
Bob Halley's avatar
Bob Halley committed
1082

Bob Halley's avatar
Bob Halley committed
1083
	return (ISC_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
1084
1085
1086

 cleanup_message:
	dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
Bob Halley's avatar
Bob Halley committed
1087

Bob Halley's avatar
Bob Halley committed
1088
	/*
Bob Halley's avatar
Bob Halley committed
1089
	 * Stop the dispatcher from listening.
Bob Halley's avatar
Bob Halley committed
1090
	 */
1091
	dns_dispatch_removeresponse(&query->dispentry, NULL);
Bob Halley's avatar
Bob Halley committed
1092

Bob Halley's avatar
Bob Halley committed
1093
1094
1095
1096
1097
 cleanup_temps:
	if (qname != NULL)
		dns_message_puttempname(fctx->qmessage, &qname);
	if (qrdataset != NULL)
		dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
Bob Halley's avatar
Bob Halley committed
1098

Bob Halley's avatar
Bob Halley committed
1099
	return (result);
Bob Halley's avatar
Bob Halley committed
1100
1101
}

Bob Halley's avatar
Bob Halley committed
1102
1103
1104
static void
resquery_connected(isc_task_t *task, isc_event_t *event) {
	isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1105
	resquery_t *query = event->ev_arg;
Bob Halley's avatar
Bob Halley committed
1106
1107
	isc_result_t result;

1108
	REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1109
	REQUIRE(VALID_QUERY(query));
Bob Halley's avatar
Bob Halley committed
1110
1111
1112

	QTRACE("connected");

Andreas Gustafsson's avatar
Andreas Gustafsson committed
1113
	UNUSED(task);
Bob Halley's avatar
Bob Halley committed
1114
1115
1116
1117
1118
1119
1120
1121
1122

	/*
	 * XXXRTH
	 *
	 * Currently we don't wait for the connect event before retrying
	 * a query.  This means that if we get really behind, we may end
	 * up doing extra work!
	 */

1123
1124
1125
1126
1127
1128
1129
	query->attributes &= ~RESQUERY_ATTR_CONNECTING;

	if (RESQUERY_CANCELED(query)) {
		/*
		 * This query was canceled while the connect() was in
		 * progress.
		 */
1130
		isc_socket_detach(&query->tcpsocket);
1131
1132
1133
		resquery_destroy(&query);
	} else {
		if (sevent->result == ISC_R_SUCCESS) {
1134
			unsigned int attrs;
1135

1136
1137
1138
1139
1140
1141
1142
			/*
			 * We are connected.  Create a dispatcher and
			 * send the query.
			 */
			attrs = 0;
			attrs |= DNS_DISPATCHATTR_TCP;
			attrs |= DNS_DISPATCHATTR_PRIVATE;
1143
			attrs |= DNS_DISPATCHATTR_CONNECTED;
1144
			if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
1145
1146
1147
1148