PageRenderTime 251ms CodeModel.GetById 145ms app.highlight 91ms RepoModel.GetById 1ms app.codeStats 1ms

/contrib/bind9/lib/dns/client.c

https://bitbucket.org/freebsd/freebsd-head/
C | 3023 lines | 2383 code | 428 blank | 212 comment | 707 complexity | dad12cffac10c77aabbcef2dc0bad2bb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Copyright (C) 2009-2012  Internet Systems Consortium, Inc. ("ISC")
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
   9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14 * PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17/* $Id$ */
  18
  19#include <config.h>
  20
  21#include <stddef.h>
  22
  23#include <isc/app.h>
  24#include <isc/mem.h>
  25#include <isc/mutex.h>
  26#include <isc/sockaddr.h>
  27#include <isc/socket.h>
  28#include <isc/task.h>
  29#include <isc/timer.h>
  30#include <isc/util.h>
  31
  32#include <dns/adb.h>
  33#include <dns/client.h>
  34#include <dns/db.h>
  35#include <dns/dispatch.h>
  36#include <dns/events.h>
  37#include <dns/forward.h>
  38#include <dns/keytable.h>
  39#include <dns/message.h>
  40#include <dns/name.h>
  41#include <dns/rdata.h>
  42#include <dns/rdatalist.h>
  43#include <dns/rdataset.h>
  44#include <dns/rdatatype.h>
  45#include <dns/rdatasetiter.h>
  46#include <dns/rdatastruct.h>
  47#include <dns/request.h>
  48#include <dns/resolver.h>
  49#include <dns/result.h>
  50#include <dns/tsec.h>
  51#include <dns/tsig.h>
  52#include <dns/view.h>
  53
  54#include <dst/dst.h>
  55
  56#define DNS_CLIENT_MAGIC		ISC_MAGIC('D', 'N', 'S', 'c')
  57#define DNS_CLIENT_VALID(c)		ISC_MAGIC_VALID(c, DNS_CLIENT_MAGIC)
  58
  59#define RCTX_MAGIC			ISC_MAGIC('R', 'c', 't', 'x')
  60#define RCTX_VALID(c)			ISC_MAGIC_VALID(c, RCTX_MAGIC)
  61
  62#define REQCTX_MAGIC			ISC_MAGIC('R', 'q', 'c', 'x')
  63#define REQCTX_VALID(c)			ISC_MAGIC_VALID(c, REQCTX_MAGIC)
  64
  65#define UCTX_MAGIC			ISC_MAGIC('U', 'c', 't', 'x')
  66#define UCTX_VALID(c)			ISC_MAGIC_VALID(c, UCTX_MAGIC)
  67
  68#define MAX_RESTARTS 16
  69
  70/*%
  71 * DNS client object
  72 */
  73struct dns_client {
  74	/* Unlocked */
  75	unsigned int			magic;
  76	unsigned int			attributes;
  77	isc_mutex_t			lock;
  78	isc_mem_t			*mctx;
  79	isc_appctx_t			*actx;
  80	isc_taskmgr_t			*taskmgr;
  81	isc_task_t			*task;
  82	isc_socketmgr_t			*socketmgr;
  83	isc_timermgr_t			*timermgr;
  84	dns_dispatchmgr_t		*dispatchmgr;
  85	dns_dispatch_t			*dispatchv4;
  86	dns_dispatch_t			*dispatchv6;
  87
  88	unsigned int			update_timeout;
  89	unsigned int			update_udptimeout;
  90	unsigned int			update_udpretries;
  91	unsigned int			find_timeout;
  92	unsigned int			find_udpretries;
  93
  94	/* Locked */
  95	unsigned int			references;
  96	dns_viewlist_t			viewlist;
  97	ISC_LIST(struct resctx)		resctxs;
  98	ISC_LIST(struct reqctx)		reqctxs;
  99	ISC_LIST(struct updatectx)	updatectxs;
 100};
 101
 102/*%
 103 * Timeout/retry constants for dynamic update borrowed from nsupdate
 104 */
 105#define DEF_UPDATE_TIMEOUT	300
 106#define MIN_UPDATE_TIMEOUT	30
 107#define DEF_UPDATE_UDPTIMEOUT	3
 108#define DEF_UPDATE_UDPRETRIES	3
 109
 110#define DEF_FIND_TIMEOUT	5
 111#define DEF_FIND_UDPRETRIES	3
 112
 113#define DNS_CLIENTATTR_OWNCTX			0x01
 114
 115#define DNS_CLIENTVIEW_NAME			"dnsclient"
 116
 117/*%
 118 * Internal state for a single name resolution procedure
 119 */
 120typedef struct resctx {
 121	/* Unlocked */
 122	unsigned int		magic;
 123	isc_mutex_t		lock;
 124	dns_client_t		*client;
 125	isc_boolean_t		want_dnssec;
 126
 127	/* Locked */
 128	ISC_LINK(struct resctx)	link;
 129	isc_task_t		*task;
 130	dns_view_t		*view;
 131	unsigned int		restarts;
 132	dns_fixedname_t		name;
 133	dns_rdatatype_t		type;
 134	dns_fetch_t		*fetch;
 135	dns_namelist_t		namelist;
 136	isc_result_t		result;
 137	dns_clientresevent_t	*event;
 138	isc_boolean_t		canceled;
 139	dns_rdataset_t		*rdataset;
 140	dns_rdataset_t		*sigrdataset;
 141} resctx_t;
 142
 143/*%
 144 * Argument of an internal event for synchronous name resolution.
 145 */
 146typedef struct resarg {
 147	/* Unlocked */
 148	isc_appctx_t		*actx;
 149	dns_client_t		*client;
 150	isc_mutex_t		lock;
 151
 152	/* Locked */
 153	isc_result_t		result;
 154	isc_result_t		vresult;
 155	dns_namelist_t		*namelist;
 156	dns_clientrestrans_t	*trans;
 157	isc_boolean_t		canceled;
 158} resarg_t;
 159
 160/*%
 161 * Internal state for a single DNS request
 162 */
 163typedef struct reqctx {
 164	/* Unlocked */
 165	unsigned int		magic;
 166	isc_mutex_t		lock;
 167	dns_client_t		*client;
 168	unsigned int		parseoptions;
 169
 170	/* Locked */
 171	ISC_LINK(struct reqctx)	link;
 172	isc_boolean_t		canceled;
 173	dns_tsigkey_t		*tsigkey;
 174	dns_request_t		*request;
 175	dns_clientreqevent_t	*event;
 176} reqctx_t;
 177
 178/*%
 179 * Argument of an internal event for synchronous DNS request.
 180 */
 181typedef struct reqarg {
 182	/* Unlocked */
 183	isc_appctx_t		*actx;
 184	dns_client_t		*client;
 185	isc_mutex_t		lock;
 186
 187	/* Locked */
 188	isc_result_t		result;
 189	dns_clientreqtrans_t	*trans;
 190	isc_boolean_t		canceled;
 191} reqarg_t;
 192
 193/*%
 194 * Argument of an internal event for synchronous name resolution.
 195 */
 196typedef struct updatearg {
 197	/* Unlocked */
 198	isc_appctx_t		*actx;
 199	dns_client_t		*client;
 200	isc_mutex_t		lock;
 201
 202	/* Locked */
 203	isc_result_t		result;
 204	dns_clientupdatetrans_t	*trans;
 205	isc_boolean_t		canceled;
 206} updatearg_t;
 207
 208/*%
 209 * Internal state for a single dynamic update procedure
 210 */
 211typedef struct updatectx {
 212	/* Unlocked */
 213	unsigned int			magic;
 214	isc_mutex_t			lock;
 215	dns_client_t			*client;
 216
 217	/* Locked */
 218	dns_request_t			*updatereq;
 219	dns_request_t			*soareq;
 220	dns_clientrestrans_t		*restrans;
 221	dns_clientrestrans_t		*restrans2;
 222	isc_boolean_t			canceled;
 223
 224	/* Task Locked */
 225	ISC_LINK(struct updatectx) 	link;
 226	dns_clientupdatestate_t		state;
 227	dns_rdataclass_t		rdclass;
 228	dns_view_t			*view;
 229	dns_message_t			*updatemsg;
 230	dns_message_t			*soaquery;
 231	dns_clientupdateevent_t		*event;
 232	dns_tsigkey_t			*tsigkey;
 233	dst_key_t			*sig0key;
 234	dns_name_t			*firstname;
 235	dns_name_t			soaqname;
 236	dns_fixedname_t			zonefname;
 237	dns_name_t			*zonename;
 238	isc_sockaddrlist_t		servers;
 239	unsigned int			nservers;
 240	isc_sockaddr_t			*currentserver;
 241	struct updatectx		*bp4;
 242	struct updatectx		*bp6;
 243} updatectx_t;
 244
 245static isc_result_t request_soa(updatectx_t *uctx);
 246static void client_resfind(resctx_t *rctx, dns_fetchevent_t *event);
 247static isc_result_t send_update(updatectx_t *uctx);
 248
 249static isc_result_t
 250getudpdispatch(int family, dns_dispatchmgr_t *dispatchmgr,
 251	       isc_socketmgr_t *socketmgr, isc_taskmgr_t *taskmgr,
 252	       isc_boolean_t is_shared, dns_dispatch_t **dispp)
 253{
 254	unsigned int attrs, attrmask;
 255	isc_sockaddr_t sa;
 256	dns_dispatch_t *disp;
 257	unsigned buffersize, maxbuffers, maxrequests, buckets, increment;
 258	isc_result_t result;
 259
 260	attrs = 0;
 261	attrs |= DNS_DISPATCHATTR_UDP;
 262	switch (family) {
 263	case AF_INET:
 264		attrs |= DNS_DISPATCHATTR_IPV4;
 265		break;
 266	case AF_INET6:
 267		attrs |= DNS_DISPATCHATTR_IPV6;
 268		break;
 269	default:
 270		INSIST(0);
 271	}
 272	attrmask = 0;
 273	attrmask |= DNS_DISPATCHATTR_UDP;
 274	attrmask |= DNS_DISPATCHATTR_TCP;
 275	attrmask |= DNS_DISPATCHATTR_IPV4;
 276	attrmask |= DNS_DISPATCHATTR_IPV6;
 277
 278	isc_sockaddr_anyofpf(&sa, family);
 279
 280	buffersize = 4096;
 281	maxbuffers = is_shared ? 1000 : 8;
 282	maxrequests = 32768;
 283	buckets = is_shared ? 16411 : 3;
 284	increment = is_shared ? 16433 : 5;
 285
 286	disp = NULL;
 287	result = dns_dispatch_getudp(dispatchmgr, socketmgr,
 288				     taskmgr, &sa,
 289				     buffersize, maxbuffers, maxrequests,
 290				     buckets, increment,
 291				     attrs, attrmask, &disp);
 292	if (result == ISC_R_SUCCESS)
 293		*dispp = disp;
 294
 295	return (result);
 296}
 297
 298static isc_result_t
 299dns_client_createview(isc_mem_t *mctx, dns_rdataclass_t rdclass,
 300		      unsigned int options, isc_taskmgr_t *taskmgr,
 301		      unsigned int ntasks, isc_socketmgr_t *socketmgr,
 302		      isc_timermgr_t *timermgr, dns_dispatchmgr_t *dispatchmgr,
 303		      dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6,
 304		      dns_view_t **viewp)
 305{
 306	isc_result_t result;
 307	dns_view_t *view = NULL;
 308	const char *dbtype;
 309
 310	result = dns_view_create(mctx, rdclass, DNS_CLIENTVIEW_NAME, &view);
 311	if (result != ISC_R_SUCCESS)
 312		return (result);
 313
 314	/* Initialize view security roots */
 315	result = dns_view_initsecroots(view, mctx);
 316	if (result != ISC_R_SUCCESS) {
 317		dns_view_detach(&view);
 318		return (result);
 319	}
 320
 321	result = dns_view_createresolver(view, taskmgr, ntasks, socketmgr,
 322					 timermgr, 0, dispatchmgr,
 323					 dispatchv4, dispatchv6);
 324	if (result != ISC_R_SUCCESS) {
 325		dns_view_detach(&view);
 326		return (result);
 327	}
 328
 329	/*
 330	 * Set cache DB.
 331	 * XXX: it may be better if specific DB implementations can be
 332	 * specified via some configuration knob.
 333	 */
 334	if ((options & DNS_CLIENTCREATEOPT_USECACHE) != 0)
 335		dbtype = "rbt";
 336	else
 337		dbtype = "ecdb";
 338	result = dns_db_create(mctx, dbtype, dns_rootname, dns_dbtype_cache,
 339			       rdclass, 0, NULL, &view->cachedb);
 340	if (result != ISC_R_SUCCESS) {
 341		dns_view_detach(&view);
 342		return (result);
 343	}
 344
 345	*viewp = view;
 346	return (ISC_R_SUCCESS);
 347}
 348
 349isc_result_t
 350dns_client_create(dns_client_t **clientp, unsigned int options) {
 351	isc_result_t result;
 352	isc_mem_t *mctx = NULL;
 353	isc_appctx_t *actx = NULL;
 354	isc_taskmgr_t *taskmgr = NULL;
 355	isc_socketmgr_t *socketmgr = NULL;
 356	isc_timermgr_t *timermgr = NULL;
 357
 358	result = isc_mem_create(0, 0, &mctx);
 359	if (result != ISC_R_SUCCESS)
 360		return (result);
 361	result = isc_appctx_create(mctx, &actx);
 362	if (result != ISC_R_SUCCESS)
 363		goto cleanup;
 364	result = isc_app_ctxstart(actx);
 365	if (result != ISC_R_SUCCESS)
 366		goto cleanup;
 367	result = isc_taskmgr_createinctx(mctx, actx, 1, 0, &taskmgr);
 368	if (result != ISC_R_SUCCESS)
 369		goto cleanup;
 370	result = isc_socketmgr_createinctx(mctx, actx, &socketmgr);
 371	if (result != ISC_R_SUCCESS)
 372		goto cleanup;
 373	result = isc_timermgr_createinctx(mctx, actx, &timermgr);
 374	if (result != ISC_R_SUCCESS)
 375		goto cleanup;
 376
 377	result = dns_client_createx(mctx, actx, taskmgr, socketmgr, timermgr,
 378				    options, clientp);
 379	if (result != ISC_R_SUCCESS)
 380		goto cleanup;
 381
 382	(*clientp)->attributes |= DNS_CLIENTATTR_OWNCTX;
 383
 384	/* client has its own reference to mctx, so we can detach it here */
 385	isc_mem_detach(&mctx);
 386
 387	return (ISC_R_SUCCESS);
 388
 389 cleanup:
 390	if (taskmgr != NULL)
 391		isc_taskmgr_destroy(&taskmgr);
 392	if (timermgr != NULL)
 393		isc_timermgr_destroy(&timermgr);
 394	if (socketmgr != NULL)
 395		isc_socketmgr_destroy(&socketmgr);
 396	if (actx != NULL)
 397		isc_appctx_destroy(&actx);
 398	isc_mem_detach(&mctx);
 399
 400	return (result);
 401}
 402
 403isc_result_t
 404dns_client_createx(isc_mem_t *mctx, isc_appctx_t *actx, isc_taskmgr_t *taskmgr,
 405		   isc_socketmgr_t *socketmgr, isc_timermgr_t *timermgr,
 406		   unsigned int options, dns_client_t **clientp)
 407{
 408	dns_client_t *client;
 409	isc_result_t result;
 410	dns_dispatchmgr_t *dispatchmgr = NULL;
 411	dns_dispatch_t *dispatchv4 = NULL;
 412	dns_dispatch_t *dispatchv6 = NULL;
 413	dns_view_t *view = NULL;
 414
 415	REQUIRE(mctx != NULL);
 416	REQUIRE(taskmgr != NULL);
 417	REQUIRE(timermgr != NULL);
 418	REQUIRE(socketmgr != NULL);
 419	REQUIRE(clientp != NULL && *clientp == NULL);
 420
 421	client = isc_mem_get(mctx, sizeof(*client));
 422	if (client == NULL)
 423		return (ISC_R_NOMEMORY);
 424
 425	result = isc_mutex_init(&client->lock);
 426	if (result != ISC_R_SUCCESS) {
 427		isc_mem_put(mctx, client, sizeof(*client));
 428		return (result);
 429	}
 430
 431	client->actx = actx;
 432	client->taskmgr = taskmgr;
 433	client->socketmgr = socketmgr;
 434	client->timermgr = timermgr;
 435
 436	client->task = NULL;
 437	result = isc_task_create(client->taskmgr, 0, &client->task);
 438	if (result != ISC_R_SUCCESS)
 439		goto cleanup;
 440
 441	result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
 442	if (result != ISC_R_SUCCESS)
 443		goto cleanup;
 444	client->dispatchmgr = dispatchmgr;
 445
 446	/* TODO: whether to use dispatch v4 or v6 should be configurable */
 447	client->dispatchv4 = NULL;
 448	client->dispatchv6 = NULL;
 449	result = getudpdispatch(AF_INET, dispatchmgr, socketmgr,
 450				taskmgr, ISC_TRUE, &dispatchv4);
 451	if (result == ISC_R_SUCCESS)
 452		client->dispatchv4 = dispatchv4;
 453	result = getudpdispatch(AF_INET6, dispatchmgr, socketmgr,
 454				taskmgr, ISC_TRUE, &dispatchv6);
 455	if (result == ISC_R_SUCCESS)
 456		client->dispatchv6 = dispatchv6;
 457
 458	/* We need at least one of the dispatchers */
 459	if (dispatchv4 == NULL && dispatchv6 == NULL) {
 460		INSIST(result != ISC_R_SUCCESS);
 461		goto cleanup;
 462	}
 463
 464	/* Create the default view for class IN */
 465	result = dns_client_createview(mctx, dns_rdataclass_in, options,
 466				       taskmgr, 31, socketmgr, timermgr,
 467				       dispatchmgr, dispatchv4, dispatchv6,
 468				       &view);
 469	if (result != ISC_R_SUCCESS)
 470		goto cleanup;
 471	ISC_LIST_INIT(client->viewlist);
 472	ISC_LIST_APPEND(client->viewlist, view, link);
 473
 474	dns_view_freeze(view); /* too early? */
 475
 476	ISC_LIST_INIT(client->resctxs);
 477	ISC_LIST_INIT(client->reqctxs);
 478	ISC_LIST_INIT(client->updatectxs);
 479
 480	client->mctx = NULL;
 481	isc_mem_attach(mctx, &client->mctx);
 482
 483	client->update_timeout = DEF_UPDATE_TIMEOUT;
 484	client->update_udptimeout = DEF_UPDATE_UDPTIMEOUT;
 485	client->update_udpretries = DEF_UPDATE_UDPRETRIES;
 486	client->find_timeout = DEF_FIND_TIMEOUT;
 487	client->find_udpretries = DEF_FIND_UDPRETRIES;
 488
 489	client->references = 1;
 490	client->magic = DNS_CLIENT_MAGIC;
 491
 492	*clientp = client;
 493
 494	return (ISC_R_SUCCESS);
 495
 496 cleanup:
 497	if (dispatchv4 != NULL)
 498		dns_dispatch_detach(&dispatchv4);
 499	if (dispatchv6 != NULL)
 500		dns_dispatch_detach(&dispatchv6);
 501	if (dispatchmgr != NULL)
 502		dns_dispatchmgr_destroy(&dispatchmgr);
 503	if (client->task != NULL)
 504		isc_task_detach(&client->task);
 505	isc_mem_put(mctx, client, sizeof(*client));
 506
 507	return (result);
 508}
 509
 510static void
 511destroyclient(dns_client_t **clientp) {
 512	dns_client_t *client = *clientp;
 513	dns_view_t *view;
 514
 515	while ((view = ISC_LIST_HEAD(client->viewlist)) != NULL) {
 516		ISC_LIST_UNLINK(client->viewlist, view, link);
 517		dns_view_detach(&view);
 518	}
 519
 520	if (client->dispatchv4 != NULL)
 521		dns_dispatch_detach(&client->dispatchv4);
 522	if (client->dispatchv6 != NULL)
 523		dns_dispatch_detach(&client->dispatchv6);
 524
 525	dns_dispatchmgr_destroy(&client->dispatchmgr);
 526
 527	isc_task_detach(&client->task);
 528
 529	/*
 530	 * If the client has created its own running environments,
 531	 * destroy them.
 532	 */
 533	if ((client->attributes & DNS_CLIENTATTR_OWNCTX) != 0) {
 534		isc_taskmgr_destroy(&client->taskmgr);
 535		isc_timermgr_destroy(&client->timermgr);
 536		isc_socketmgr_destroy(&client->socketmgr);
 537
 538		isc_app_ctxfinish(client->actx);
 539		isc_appctx_destroy(&client->actx);
 540	}
 541
 542	DESTROYLOCK(&client->lock);
 543	client->magic = 0;
 544
 545	isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
 546
 547	*clientp = NULL;
 548}
 549
 550void
 551dns_client_destroy(dns_client_t **clientp) {
 552	dns_client_t *client;
 553	isc_boolean_t destroyok = ISC_FALSE;
 554
 555	REQUIRE(clientp != NULL);
 556	client = *clientp;
 557	REQUIRE(DNS_CLIENT_VALID(client));
 558
 559	LOCK(&client->lock);
 560	client->references--;
 561	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
 562	    ISC_LIST_EMPTY(client->reqctxs) &&
 563	    ISC_LIST_EMPTY(client->updatectxs)) {
 564		destroyok = ISC_TRUE;
 565	}
 566	UNLOCK(&client->lock);
 567
 568	if (destroyok)
 569		destroyclient(&client);
 570
 571	*clientp = NULL;
 572}
 573
 574isc_result_t
 575dns_client_setservers(dns_client_t *client, dns_rdataclass_t rdclass,
 576		      dns_name_t *namespace, isc_sockaddrlist_t *addrs)
 577{
 578	isc_result_t result;
 579	dns_view_t *view = NULL;
 580
 581	REQUIRE(DNS_CLIENT_VALID(client));
 582	REQUIRE(addrs != NULL);
 583
 584	if (namespace == NULL)
 585		namespace = dns_rootname;
 586
 587	LOCK(&client->lock);
 588	result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
 589				   rdclass, &view);
 590	if (result != ISC_R_SUCCESS) {
 591		UNLOCK(&client->lock);
 592		return (result);
 593	}
 594	UNLOCK(&client->lock);
 595
 596	result = dns_fwdtable_add(view->fwdtable, namespace, addrs,
 597				  dns_fwdpolicy_only);
 598
 599	dns_view_detach(&view);
 600
 601	return (result);
 602}
 603
 604isc_result_t
 605dns_client_clearservers(dns_client_t *client, dns_rdataclass_t rdclass,
 606			dns_name_t *namespace)
 607{
 608	isc_result_t result;
 609	dns_view_t *view = NULL;
 610
 611	REQUIRE(DNS_CLIENT_VALID(client));
 612
 613	if (namespace == NULL)
 614		namespace = dns_rootname;
 615
 616	LOCK(&client->lock);
 617	result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
 618				   rdclass, &view);
 619	if (result != ISC_R_SUCCESS) {
 620		UNLOCK(&client->lock);
 621		return (result);
 622	}
 623	UNLOCK(&client->lock);
 624
 625	result = dns_fwdtable_delete(view->fwdtable, namespace);
 626
 627	dns_view_detach(&view);
 628
 629	return (result);
 630}
 631
 632static isc_result_t
 633getrdataset(isc_mem_t *mctx, dns_rdataset_t **rdatasetp) {
 634	dns_rdataset_t *rdataset;
 635
 636	REQUIRE(mctx != NULL);
 637	REQUIRE(rdatasetp != NULL && *rdatasetp == NULL);
 638
 639	rdataset = isc_mem_get(mctx, sizeof(*rdataset));
 640	if (rdataset == NULL)
 641		return (ISC_R_NOMEMORY);
 642
 643	dns_rdataset_init(rdataset);
 644
 645	*rdatasetp = rdataset;
 646
 647	return (ISC_R_SUCCESS);
 648}
 649
 650static void
 651putrdataset(isc_mem_t *mctx, dns_rdataset_t **rdatasetp) {
 652	dns_rdataset_t *rdataset;
 653
 654	REQUIRE(rdatasetp != NULL);
 655	rdataset = *rdatasetp;
 656	REQUIRE(rdataset != NULL);
 657
 658	if (dns_rdataset_isassociated(rdataset))
 659		dns_rdataset_disassociate(rdataset);
 660
 661	isc_mem_put(mctx, rdataset, sizeof(*rdataset));
 662
 663	*rdatasetp = NULL;
 664}
 665
 666static void
 667fetch_done(isc_task_t *task, isc_event_t *event) {
 668	resctx_t *rctx = event->ev_arg;
 669	dns_fetchevent_t *fevent;
 670
 671	REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
 672	REQUIRE(RCTX_VALID(rctx));
 673	REQUIRE(rctx->task == task);
 674	fevent = (dns_fetchevent_t *)event;
 675
 676	client_resfind(rctx, fevent);
 677}
 678
 679static inline isc_result_t
 680start_fetch(resctx_t *rctx) {
 681	isc_result_t result;
 682
 683	/*
 684	 * The caller must be holding the rctx's lock.
 685	 */
 686
 687	REQUIRE(rctx->fetch == NULL);
 688
 689	result = dns_resolver_createfetch(rctx->view->resolver,
 690					  dns_fixedname_name(&rctx->name),
 691					  rctx->type,
 692					  NULL, NULL, NULL, 0,
 693					  rctx->task, fetch_done, rctx,
 694					  rctx->rdataset,
 695					  rctx->sigrdataset,
 696					  &rctx->fetch);
 697
 698	return (result);
 699}
 700
 701static isc_result_t
 702view_find(resctx_t *rctx, dns_db_t **dbp, dns_dbnode_t **nodep,
 703	  dns_name_t *foundname)
 704{
 705	isc_result_t result;
 706	dns_name_t *name = dns_fixedname_name(&rctx->name);
 707	dns_rdatatype_t type;
 708
 709	if (rctx->type == dns_rdatatype_rrsig)
 710		type = dns_rdatatype_any;
 711	else
 712		type = rctx->type;
 713
 714	result = dns_view_find(rctx->view, name, type, 0, 0, ISC_FALSE,
 715			       dbp, nodep, foundname, rctx->rdataset,
 716			       rctx->sigrdataset);
 717
 718	return (result);
 719}
 720
 721static void
 722client_resfind(resctx_t *rctx, dns_fetchevent_t *event) {
 723	isc_mem_t *mctx;
 724	isc_result_t tresult, result = ISC_R_SUCCESS;
 725	isc_result_t vresult = ISC_R_SUCCESS;
 726	isc_boolean_t want_restart;
 727	isc_boolean_t send_event = ISC_FALSE;
 728	dns_name_t *name, *prefix;
 729	dns_fixedname_t foundname, fixed;
 730	dns_rdataset_t *trdataset;
 731	dns_rdata_t rdata = DNS_RDATA_INIT;
 732	unsigned int nlabels;
 733	int order;
 734	dns_namereln_t namereln;
 735	dns_rdata_cname_t cname;
 736	dns_rdata_dname_t dname;
 737
 738	REQUIRE(RCTX_VALID(rctx));
 739
 740	LOCK(&rctx->lock);
 741
 742	mctx = rctx->view->mctx;
 743
 744	name = dns_fixedname_name(&rctx->name);
 745
 746	do {
 747		dns_name_t *fname = NULL;
 748		dns_name_t *ansname = NULL;
 749		dns_db_t *db = NULL;
 750		dns_dbnode_t *node = NULL;
 751
 752		rctx->restarts++;
 753		want_restart = ISC_FALSE;
 754
 755		if (event == NULL && !rctx->canceled) {
 756			dns_fixedname_init(&foundname);
 757			fname = dns_fixedname_name(&foundname);
 758			INSIST(!dns_rdataset_isassociated(rctx->rdataset));
 759			INSIST(rctx->sigrdataset == NULL ||
 760			       !dns_rdataset_isassociated(rctx->sigrdataset));
 761			result = view_find(rctx, &db, &node, fname);
 762			if (result == ISC_R_NOTFOUND) {
 763				/*
 764				 * We don't know anything about the name.
 765				 * Launch a fetch.
 766				 */
 767				if (node != NULL) {
 768					INSIST(db != NULL);
 769					dns_db_detachnode(db, &node);
 770				}
 771				if (db != NULL)
 772					dns_db_detach(&db);
 773				result = start_fetch(rctx);
 774				if (result != ISC_R_SUCCESS) {
 775					putrdataset(mctx, &rctx->rdataset);
 776					if (rctx->sigrdataset != NULL)
 777						putrdataset(mctx,
 778							    &rctx->sigrdataset);
 779					send_event = ISC_TRUE;
 780				}
 781				goto done;
 782			}
 783		} else {
 784			INSIST(event != NULL);
 785			INSIST(event->fetch == rctx->fetch);
 786			dns_resolver_destroyfetch(&rctx->fetch);
 787			db = event->db;
 788			node = event->node;
 789			result = event->result;
 790			vresult = event->vresult;
 791			fname = dns_fixedname_name(&event->foundname);
 792			INSIST(event->rdataset == rctx->rdataset);
 793			INSIST(event->sigrdataset == rctx->sigrdataset);
 794		}
 795
 796		/*
 797		 * If we've been canceled, forget about the result.
 798		 */
 799		if (rctx->canceled)
 800			result = ISC_R_CANCELED;
 801		else {
 802			/*
 803			 * Otherwise, get some resource for copying the
 804			 * result.
 805			 */
 806			ansname = isc_mem_get(mctx, sizeof(*ansname));
 807			if (ansname == NULL)
 808				tresult = ISC_R_NOMEMORY;
 809			else {
 810				dns_name_t *aname;
 811
 812				aname = dns_fixedname_name(&rctx->name);
 813				dns_name_init(ansname, NULL);
 814				tresult = dns_name_dup(aname, mctx, ansname);
 815				if (tresult != ISC_R_SUCCESS)
 816					isc_mem_put(mctx, ansname,
 817						    sizeof(*ansname));
 818			}
 819			if (tresult != ISC_R_SUCCESS)
 820				result = tresult;
 821		}
 822
 823		switch (result) {
 824		case ISC_R_SUCCESS:
 825			send_event = ISC_TRUE;
 826			/*
 827			 * This case is handled in the main line below.
 828			 */
 829			break;
 830		case DNS_R_CNAME:
 831			/*
 832			 * Add the CNAME to the answer list.
 833			 */
 834			trdataset = rctx->rdataset;
 835			ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
 836			rctx->rdataset = NULL;
 837			if (rctx->sigrdataset != NULL) {
 838				ISC_LIST_APPEND(ansname->list,
 839						rctx->sigrdataset, link);
 840				rctx->sigrdataset = NULL;
 841			}
 842			ISC_LIST_APPEND(rctx->namelist, ansname, link);
 843			ansname = NULL;
 844
 845			/*
 846			 * Copy the CNAME's target into the lookup's
 847			 * query name and start over.
 848			 */
 849			tresult = dns_rdataset_first(trdataset);
 850			if (tresult != ISC_R_SUCCESS)
 851				goto done;
 852			dns_rdataset_current(trdataset, &rdata);
 853			tresult = dns_rdata_tostruct(&rdata, &cname, NULL);
 854			dns_rdata_reset(&rdata);
 855			if (tresult != ISC_R_SUCCESS)
 856				goto done;
 857			tresult = dns_name_copy(&cname.cname, name, NULL);
 858			dns_rdata_freestruct(&cname);
 859			if (tresult == ISC_R_SUCCESS)
 860				want_restart = ISC_TRUE;
 861			else
 862				result = tresult;
 863			goto done;
 864		case DNS_R_DNAME:
 865			/*
 866			 * Add the DNAME to the answer list.
 867			 */
 868			trdataset = rctx->rdataset;
 869			ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
 870			rctx->rdataset = NULL;
 871			if (rctx->sigrdataset != NULL) {
 872				ISC_LIST_APPEND(ansname->list,
 873						rctx->sigrdataset, link);
 874				rctx->sigrdataset = NULL;
 875			}
 876			ISC_LIST_APPEND(rctx->namelist, ansname, link);
 877			ansname = NULL;
 878
 879			namereln = dns_name_fullcompare(name, fname, &order,
 880							&nlabels);
 881			INSIST(namereln == dns_namereln_subdomain);
 882			/*
 883			 * Get the target name of the DNAME.
 884			 */
 885			tresult = dns_rdataset_first(trdataset);
 886			if (tresult != ISC_R_SUCCESS) {
 887				result = tresult;
 888				goto done;
 889			}
 890			dns_rdataset_current(trdataset, &rdata);
 891			tresult = dns_rdata_tostruct(&rdata, &dname, NULL);
 892			dns_rdata_reset(&rdata);
 893			if (tresult != ISC_R_SUCCESS) {
 894				result = tresult;
 895				goto done;
 896			}
 897			/*
 898			 * Construct the new query name and start over.
 899			 */
 900			dns_fixedname_init(&fixed);
 901			prefix = dns_fixedname_name(&fixed);
 902			dns_name_split(name, nlabels, prefix, NULL);
 903			tresult = dns_name_concatenate(prefix, &dname.dname,
 904						      name, NULL);
 905			dns_rdata_freestruct(&dname);
 906			if (tresult == ISC_R_SUCCESS)
 907				want_restart = ISC_TRUE;
 908			else
 909				result = tresult;
 910			goto done;
 911		case DNS_R_NCACHENXDOMAIN:
 912		case DNS_R_NCACHENXRRSET:
 913			ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
 914			ISC_LIST_APPEND(rctx->namelist, ansname, link);
 915			ansname = NULL;
 916			rctx->rdataset = NULL;
 917			/* What about sigrdataset? */
 918			if (rctx->sigrdataset != NULL)
 919				putrdataset(mctx, &rctx->sigrdataset);
 920			send_event = ISC_TRUE;
 921			goto done;
 922		default:
 923			if (rctx->rdataset != NULL)
 924				putrdataset(mctx, &rctx->rdataset);
 925			if (rctx->sigrdataset != NULL)
 926				putrdataset(mctx, &rctx->sigrdataset);
 927			send_event = ISC_TRUE;
 928			goto done;
 929		}
 930
 931		if (rctx->type == dns_rdatatype_any) {
 932			int n = 0;
 933			dns_rdatasetiter_t *rdsiter = NULL;
 934
 935			tresult = dns_db_allrdatasets(db, node, NULL, 0,
 936						      &rdsiter);
 937			if (tresult != ISC_R_SUCCESS) {
 938				result = tresult;
 939				goto done;
 940			}
 941
 942			tresult = dns_rdatasetiter_first(rdsiter);
 943			while (tresult == ISC_R_SUCCESS) {
 944				dns_rdatasetiter_current(rdsiter,
 945							 rctx->rdataset);
 946				if (rctx->rdataset->type != 0) {
 947					ISC_LIST_APPEND(ansname->list,
 948							rctx->rdataset,
 949							link);
 950					n++;
 951					rctx->rdataset = NULL;
 952				} else {
 953					/*
 954					 * We're not interested in this
 955					 * rdataset.
 956					 */
 957					dns_rdataset_disassociate(
 958						rctx->rdataset);
 959				}
 960				tresult = dns_rdatasetiter_next(rdsiter);
 961
 962				if (tresult == ISC_R_SUCCESS &&
 963				    rctx->rdataset == NULL) {
 964					tresult = getrdataset(mctx,
 965							      &rctx->rdataset);
 966					if (tresult != ISC_R_SUCCESS) {
 967						result = tresult;
 968						POST(result);
 969						break;
 970					}
 971				}
 972			}
 973			if (n == 0) {
 974				/*
 975				 * We didn't match any rdatasets (which means
 976				 * something went wrong in this
 977				 * implementation).
 978				 */
 979				result = DNS_R_SERVFAIL; /* better code? */
 980				POST(result);
 981			} else {
 982				ISC_LIST_APPEND(rctx->namelist, ansname, link);
 983				ansname = NULL;
 984			}
 985			dns_rdatasetiter_destroy(&rdsiter);
 986			if (tresult != ISC_R_NOMORE)
 987				result = DNS_R_SERVFAIL; /* ditto */
 988			else
 989				result = ISC_R_SUCCESS;
 990			goto done;
 991		} else {
 992			/*
 993			 * This is the "normal" case -- an ordinary question
 994			 * to which we've got the answer.
 995			 */
 996			ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
 997			rctx->rdataset = NULL;
 998			if (rctx->sigrdataset != NULL) {
 999				ISC_LIST_APPEND(ansname->list,
1000						rctx->sigrdataset, link);
1001				rctx->sigrdataset = NULL;
1002			}
1003			ISC_LIST_APPEND(rctx->namelist, ansname, link);
1004			ansname = NULL;
1005		}
1006
1007	done:
1008		/*
1009		 * Free temporary resources
1010		 */
1011		if (ansname != NULL) {
1012			dns_rdataset_t *rdataset;
1013
1014			while ((rdataset = ISC_LIST_HEAD(ansname->list))
1015			       != NULL) {
1016				ISC_LIST_UNLINK(ansname->list, rdataset, link);
1017				putrdataset(mctx, &rdataset);
1018			}
1019			dns_name_free(ansname, mctx);
1020			isc_mem_put(mctx, ansname, sizeof(*ansname));
1021		}
1022
1023		if (node != NULL)
1024			dns_db_detachnode(db, &node);
1025		if (db != NULL)
1026			dns_db_detach(&db);
1027		if (event != NULL)
1028			isc_event_free(ISC_EVENT_PTR(&event));
1029
1030		/*
1031		 * Limit the number of restarts.
1032		 */
1033		if (want_restart && rctx->restarts == MAX_RESTARTS) {
1034			want_restart = ISC_FALSE;
1035			result = ISC_R_QUOTA;
1036			send_event = ISC_TRUE;
1037		}
1038
1039		/*
1040		 * Prepare further find with new resources
1041		 */
1042		if (want_restart) {
1043			INSIST(rctx->rdataset == NULL &&
1044			       rctx->sigrdataset == NULL);
1045
1046			result = getrdataset(mctx, &rctx->rdataset);
1047			if (result == ISC_R_SUCCESS && rctx->want_dnssec) {
1048				result = getrdataset(mctx, &rctx->sigrdataset);
1049				if (result != ISC_R_SUCCESS) {
1050					putrdataset(mctx, &rctx->rdataset);
1051				}
1052			}
1053
1054			if (result != ISC_R_SUCCESS) {
1055				want_restart = ISC_FALSE;
1056				send_event = ISC_TRUE;
1057			}
1058		}
1059	} while (want_restart);
1060
1061	if (send_event) {
1062		isc_task_t *task;
1063
1064		while ((name = ISC_LIST_HEAD(rctx->namelist)) != NULL) {
1065			ISC_LIST_UNLINK(rctx->namelist, name, link);
1066			ISC_LIST_APPEND(rctx->event->answerlist, name, link);
1067		}
1068
1069		rctx->event->result = result;
1070		rctx->event->vresult = vresult;
1071		task = rctx->event->ev_sender;
1072		rctx->event->ev_sender = rctx;
1073		isc_task_sendanddetach(&task, ISC_EVENT_PTR(&rctx->event));
1074	}
1075
1076	UNLOCK(&rctx->lock);
1077}
1078
1079static void
1080resolve_done(isc_task_t *task, isc_event_t *event) {
1081	resarg_t *resarg = event->ev_arg;
1082	dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
1083	dns_name_t *name;
1084
1085	UNUSED(task);
1086
1087	LOCK(&resarg->lock);
1088
1089	resarg->result = rev->result;
1090	resarg->vresult = rev->vresult;
1091	while ((name = ISC_LIST_HEAD(rev->answerlist)) != NULL) {
1092		ISC_LIST_UNLINK(rev->answerlist, name, link);
1093		ISC_LIST_APPEND(*resarg->namelist, name, link);
1094	}
1095
1096	dns_client_destroyrestrans(&resarg->trans);
1097	isc_event_free(&event);
1098
1099	if (!resarg->canceled) {
1100		UNLOCK(&resarg->lock);
1101
1102		/* Exit from the internal event loop */
1103		isc_app_ctxsuspend(resarg->actx);
1104	} else {
1105		/*
1106		 * We have already exited from the loop (due to some
1107		 * unexpected event).  Just clean the arg up.
1108		 */
1109		UNLOCK(&resarg->lock);
1110		DESTROYLOCK(&resarg->lock);
1111		isc_mem_put(resarg->client->mctx, resarg, sizeof(*resarg));
1112	}
1113}
1114
1115isc_result_t
1116dns_client_resolve(dns_client_t *client, dns_name_t *name,
1117		   dns_rdataclass_t rdclass, dns_rdatatype_t type,
1118		   unsigned int options, dns_namelist_t *namelist)
1119{
1120	isc_result_t result;
1121	isc_appctx_t *actx;
1122	resarg_t *resarg;
1123
1124	REQUIRE(DNS_CLIENT_VALID(client));
1125	REQUIRE(namelist != NULL && ISC_LIST_EMPTY(*namelist));
1126
1127	if ((client->attributes & DNS_CLIENTATTR_OWNCTX) == 0 &&
1128	    (options & DNS_CLIENTRESOPT_ALLOWRUN) == 0) {
1129		/*
1130		 * If the client is run under application's control, we need
1131		 * to create a new running (sub)environment for this
1132		 * particular resolution.
1133		 */
1134		return (ISC_R_NOTIMPLEMENTED); /* XXXTBD */
1135	} else
1136		actx = client->actx;
1137
1138	resarg = isc_mem_get(client->mctx, sizeof(*resarg));
1139	if (resarg == NULL)
1140		return (ISC_R_NOMEMORY);
1141
1142	result = isc_mutex_init(&resarg->lock);
1143	if (result != ISC_R_SUCCESS) {
1144		isc_mem_put(client->mctx, resarg, sizeof(*resarg));
1145		return (result);
1146	}
1147
1148	resarg->actx = actx;
1149	resarg->client = client;
1150	resarg->result = DNS_R_SERVFAIL;
1151	resarg->namelist = namelist;
1152	resarg->trans = NULL;
1153	resarg->canceled = ISC_FALSE;
1154	result = dns_client_startresolve(client, name, rdclass, type, options,
1155					 client->task, resolve_done, resarg,
1156					 &resarg->trans);
1157	if (result != ISC_R_SUCCESS) {
1158		DESTROYLOCK(&resarg->lock);
1159		isc_mem_put(client->mctx, resarg, sizeof(*resarg));
1160		return (result);
1161	}
1162
1163	/*
1164	 * Start internal event loop.  It blocks until the entire process
1165	 * is completed.
1166	 */
1167	result = isc_app_ctxrun(actx);
1168
1169	LOCK(&resarg->lock);
1170	if (result == ISC_R_SUCCESS || result == ISC_R_SUSPEND)
1171		result = resarg->result;
1172	if (result != ISC_R_SUCCESS && resarg->vresult != ISC_R_SUCCESS) {
1173		/*
1174		 * If this lookup failed due to some error in DNSSEC
1175		 * validation, return the validation error code.
1176		 * XXX: or should we pass the validation result separately?
1177		 */
1178		result = resarg->vresult;
1179	}
1180	if (resarg->trans != NULL) {
1181		/*
1182		 * Unusual termination (perhaps due to signal).  We need some
1183		 * tricky cleanup process.
1184		 */
1185		resarg->canceled = ISC_TRUE;
1186		dns_client_cancelresolve(resarg->trans);
1187
1188		UNLOCK(&resarg->lock);
1189
1190		/* resarg will be freed in the event handler. */
1191	} else {
1192		UNLOCK(&resarg->lock);
1193
1194		DESTROYLOCK(&resarg->lock);
1195		isc_mem_put(client->mctx, resarg, sizeof(*resarg));
1196	}
1197
1198	return (result);
1199}
1200
1201isc_result_t
1202dns_client_startresolve(dns_client_t *client, dns_name_t *name,
1203			dns_rdataclass_t rdclass, dns_rdatatype_t type,
1204			unsigned int options, isc_task_t *task,
1205			isc_taskaction_t action, void *arg,
1206			dns_clientrestrans_t **transp)
1207{
1208	dns_view_t *view = NULL;
1209	dns_clientresevent_t *event = NULL;
1210	resctx_t *rctx = NULL;
1211	isc_task_t *clone = NULL;
1212	isc_mem_t *mctx;
1213	isc_result_t result;
1214	dns_rdataset_t *rdataset, *sigrdataset;
1215	isc_boolean_t want_dnssec;
1216
1217	REQUIRE(DNS_CLIENT_VALID(client));
1218	REQUIRE(transp != NULL && *transp == NULL);
1219
1220	LOCK(&client->lock);
1221	result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
1222				   rdclass, &view);
1223	UNLOCK(&client->lock);
1224	if (result != ISC_R_SUCCESS)
1225		return (result);
1226
1227	mctx = client->mctx;
1228	rdataset = NULL;
1229	sigrdataset = NULL;
1230	want_dnssec = ISC_TF((options & DNS_CLIENTRESOPT_NODNSSEC) == 0);
1231
1232	/*
1233	 * Prepare some intermediate resources
1234	 */
1235	clone = NULL;
1236	isc_task_attach(task, &clone);
1237	event = (dns_clientresevent_t *)
1238		isc_event_allocate(mctx, clone, DNS_EVENT_CLIENTRESDONE,
1239				   action, arg, sizeof(*event));
1240	if (event == NULL) {
1241		result = ISC_R_NOMEMORY;
1242		goto cleanup;
1243	}
1244	event->result = DNS_R_SERVFAIL;
1245	ISC_LIST_INIT(event->answerlist);
1246
1247	rctx = isc_mem_get(mctx, sizeof(*rctx));
1248	if (rctx == NULL)
1249		result = ISC_R_NOMEMORY;
1250	else {
1251		result = isc_mutex_init(&rctx->lock);
1252		if (result != ISC_R_SUCCESS) {
1253			isc_mem_put(mctx, rctx, sizeof(*rctx));
1254			rctx = NULL;
1255		}
1256	}
1257	if (result != ISC_R_SUCCESS)
1258		goto cleanup;
1259
1260	result = getrdataset(mctx, &rdataset);
1261	if (result != ISC_R_SUCCESS)
1262		goto cleanup;
1263	rctx->rdataset = rdataset;
1264
1265	if (want_dnssec) {
1266		result = getrdataset(mctx, &sigrdataset);
1267		if (result != ISC_R_SUCCESS)
1268			goto cleanup;
1269	}
1270	rctx->sigrdataset = sigrdataset;
1271
1272	dns_fixedname_init(&rctx->name);
1273	result = dns_name_copy(name, dns_fixedname_name(&rctx->name), NULL);
1274	if (result != ISC_R_SUCCESS)
1275		goto cleanup;
1276
1277	rctx->client = client;
1278	ISC_LINK_INIT(rctx, link);
1279	rctx->canceled = ISC_FALSE;
1280	rctx->task = client->task;
1281	rctx->type = type;
1282	rctx->view = view;
1283	rctx->restarts = 0;
1284	rctx->fetch = NULL;
1285	rctx->want_dnssec = want_dnssec;
1286	ISC_LIST_INIT(rctx->namelist);
1287	rctx->event = event;
1288
1289	rctx->magic = RCTX_MAGIC;
1290
1291	LOCK(&client->lock);
1292	ISC_LIST_APPEND(client->resctxs, rctx, link);
1293	UNLOCK(&client->lock);
1294
1295	client_resfind(rctx, NULL);
1296
1297	*transp = (dns_clientrestrans_t *)rctx;
1298
1299	return (ISC_R_SUCCESS);
1300
1301 cleanup:
1302	if (rdataset != NULL)
1303		putrdataset(client->mctx, &rdataset);
1304	if (sigrdataset != NULL)
1305		putrdataset(client->mctx, &sigrdataset);
1306	if (rctx != NULL) {
1307		DESTROYLOCK(&rctx->lock);
1308		isc_mem_put(mctx, rctx, sizeof(*rctx));
1309	}
1310	if (event != NULL)
1311		isc_event_free(ISC_EVENT_PTR(&event));
1312	isc_task_detach(&clone);
1313	dns_view_detach(&view);
1314
1315	return (result);
1316}
1317
1318void
1319dns_client_cancelresolve(dns_clientrestrans_t *trans) {
1320	resctx_t *rctx;
1321
1322	REQUIRE(trans != NULL);
1323	rctx = (resctx_t *)trans;
1324	REQUIRE(RCTX_VALID(rctx));
1325
1326	LOCK(&rctx->lock);
1327
1328	if (!rctx->canceled) {
1329		rctx->canceled = ISC_TRUE;
1330		if (rctx->fetch != NULL)
1331			dns_resolver_cancelfetch(rctx->fetch);
1332	}
1333
1334	UNLOCK(&rctx->lock);
1335}
1336
1337void
1338dns_client_freeresanswer(dns_client_t *client, dns_namelist_t *namelist) {
1339	dns_name_t *name;
1340	dns_rdataset_t *rdataset;
1341
1342	REQUIRE(DNS_CLIENT_VALID(client));
1343	REQUIRE(namelist != NULL);
1344
1345	while ((name = ISC_LIST_HEAD(*namelist)) != NULL) {
1346		ISC_LIST_UNLINK(*namelist, name, link);
1347		while ((rdataset = ISC_LIST_HEAD(name->list)) != NULL) {
1348			ISC_LIST_UNLINK(name->list, rdataset, link);
1349			putrdataset(client->mctx, &rdataset);
1350		}
1351		dns_name_free(name, client->mctx);
1352		isc_mem_put(client->mctx, name, sizeof(*name));
1353	}
1354}
1355
1356void
1357dns_client_destroyrestrans(dns_clientrestrans_t **transp) {
1358	resctx_t *rctx;
1359	isc_mem_t *mctx;
1360	dns_client_t *client;
1361	isc_boolean_t need_destroyclient = ISC_FALSE;
1362
1363	REQUIRE(transp != NULL);
1364	rctx = (resctx_t *)*transp;
1365	REQUIRE(RCTX_VALID(rctx));
1366	REQUIRE(rctx->fetch == NULL);
1367	REQUIRE(rctx->event == NULL);
1368	client = rctx->client;
1369	REQUIRE(DNS_CLIENT_VALID(client));
1370
1371	mctx = client->mctx;
1372	dns_view_detach(&rctx->view);
1373
1374	LOCK(&client->lock);
1375
1376	INSIST(ISC_LINK_LINKED(rctx, link));
1377	ISC_LIST_UNLINK(client->resctxs, rctx, link);
1378
1379	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
1380	    ISC_LIST_EMPTY(client->reqctxs) &&
1381	    ISC_LIST_EMPTY(client->updatectxs))
1382		need_destroyclient = ISC_TRUE;
1383
1384	UNLOCK(&client->lock);
1385
1386	INSIST(ISC_LIST_EMPTY(rctx->namelist));
1387
1388	DESTROYLOCK(&rctx->lock);
1389	rctx->magic = 0;
1390
1391	isc_mem_put(mctx, rctx, sizeof(*rctx));
1392
1393	if (need_destroyclient)
1394		destroyclient(&client);
1395
1396	*transp = NULL;
1397}
1398
1399isc_result_t
1400dns_client_addtrustedkey(dns_client_t *client, dns_rdataclass_t rdclass,
1401			 dns_name_t *keyname, isc_buffer_t *keydatabuf)
1402{
1403	isc_result_t result;
1404	dns_view_t *view = NULL;
1405	dst_key_t *dstkey = NULL;
1406	dns_keytable_t *secroots = NULL;
1407
1408	REQUIRE(DNS_CLIENT_VALID(client));
1409
1410	LOCK(&client->lock);
1411	result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
1412				   rdclass, &view);
1413	UNLOCK(&client->lock);
1414	if (result != ISC_R_SUCCESS)
1415		goto cleanup;
1416
1417	result = dns_view_getsecroots(view, &secroots);
1418	if (result != ISC_R_SUCCESS)
1419		goto cleanup;
1420
1421	result = dst_key_fromdns(keyname, rdclass, keydatabuf, client->mctx,
1422				 &dstkey);
1423	if (result != ISC_R_SUCCESS)
1424		goto cleanup;
1425
1426	result = dns_keytable_add(secroots, ISC_FALSE, &dstkey);
1427
1428 cleanup:
1429	if (dstkey != NULL)
1430		dst_key_free(&dstkey);
1431	if (view != NULL)
1432		dns_view_detach(&view);
1433	if (secroots != NULL)
1434		dns_keytable_detach(&secroots);
1435	return (result);
1436}
1437
1438/*%
1439 * Simple request routines
1440 */
1441static void
1442request_done(isc_task_t *task, isc_event_t *event) {
1443	dns_requestevent_t *reqev = NULL;
1444	dns_request_t *request;
1445	isc_result_t result, eresult;
1446	reqctx_t *ctx;
1447
1448	UNUSED(task);
1449
1450	REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
1451	reqev = (dns_requestevent_t *)event;
1452	request = reqev->request;
1453	result = eresult = reqev->result;
1454	ctx = reqev->ev_arg;
1455	REQUIRE(REQCTX_VALID(ctx));
1456
1457	isc_event_free(&event);
1458
1459	LOCK(&ctx->lock);
1460
1461	if (eresult == ISC_R_SUCCESS) {
1462		result = dns_request_getresponse(request, ctx->event->rmessage,
1463						 ctx->parseoptions);
1464	}
1465
1466	if (ctx->tsigkey != NULL)
1467		dns_tsigkey_detach(&ctx->tsigkey);
1468
1469	if (ctx->canceled)
1470		ctx->event->result = ISC_R_CANCELED;
1471	else
1472		ctx->event->result = result;
1473	task = ctx->event->ev_sender;
1474	ctx->event->ev_sender = ctx;
1475	isc_task_sendanddetach(&task, ISC_EVENT_PTR(&ctx->event));
1476
1477	UNLOCK(&ctx->lock);
1478}
1479
1480static void
1481localrequest_done(isc_task_t *task, isc_event_t *event) {
1482	reqarg_t *reqarg = event->ev_arg;
1483	dns_clientreqevent_t *rev =(dns_clientreqevent_t *)event;
1484
1485	UNUSED(task);
1486
1487	REQUIRE(event->ev_type == DNS_EVENT_CLIENTREQDONE);
1488
1489	LOCK(&reqarg->lock);
1490
1491	reqarg->result = rev->result;
1492	dns_client_destroyreqtrans(&reqarg->trans);
1493	isc_event_free(&event);
1494
1495	if (!reqarg->canceled) {
1496		UNLOCK(&reqarg->lock);
1497
1498		/* Exit from the internal event loop */
1499		isc_app_ctxsuspend(reqarg->actx);
1500	} else {
1501		/*
1502		 * We have already exited from the loop (due to some
1503		 * unexpected event).  Just clean the arg up.
1504		 */
1505		UNLOCK(&reqarg->lock);
1506		DESTROYLOCK(&reqarg->lock);
1507		isc_mem_put(reqarg->client->mctx, reqarg, sizeof(*reqarg));
1508	}
1509}
1510
1511isc_result_t
1512dns_client_request(dns_client_t *client, dns_message_t *qmessage,
1513		   dns_message_t *rmessage, isc_sockaddr_t *server,
1514		   unsigned int options, unsigned int parseoptions,
1515		   dns_tsec_t *tsec, unsigned int timeout,
1516		   unsigned int udptimeout, unsigned int udpretries)
1517{
1518	isc_appctx_t *actx;
1519	reqarg_t *reqarg;
1520	isc_result_t result;
1521
1522	REQUIRE(DNS_CLIENT_VALID(client));
1523	REQUIRE(qmessage != NULL);
1524	REQUIRE(rmessage != NULL);
1525
1526	if ((client->attributes & DNS_CLIENTATTR_OWNCTX) == 0 &&
1527	    (options & DNS_CLIENTREQOPT_ALLOWRUN) == 0) {
1528		/*
1529		 * If the client is run under application's control, we need
1530		 * to create a new running (sub)environment for this
1531		 * particular resolution.
1532		 */
1533		return (ISC_R_NOTIMPLEMENTED); /* XXXTBD */
1534	} else
1535		actx = client->actx;
1536
1537	reqarg = isc_mem_get(client->mctx, sizeof(*reqarg));
1538	if (reqarg == NULL)
1539		return (ISC_R_NOMEMORY);
1540
1541	result = isc_mutex_init(&reqarg->lock);
1542	if (result != ISC_R_SUCCESS) {
1543		isc_mem_put(client->mctx, reqarg, sizeof(*reqarg));
1544		return (result);
1545	}
1546
1547	reqarg->actx = actx;
1548	reqarg->client = client;
1549	reqarg->trans = NULL;
1550	reqarg->canceled = ISC_FALSE;
1551
1552	result = dns_client_startrequest(client, qmessage, rmessage, server,
1553					 options, parseoptions, tsec, timeout,
1554					 udptimeout, udpretries,
1555					 client->task, localrequest_done,
1556					 reqarg, &reqarg->trans);
1557	if (result != ISC_R_SUCCESS) {
1558		DESTROYLOCK(&reqarg->lock);
1559		isc_mem_put(client->mctx, reqarg, sizeof(*reqarg));
1560		return (result);
1561	}
1562
1563	/*
1564	 * Start internal event loop.  It blocks until the entire process
1565	 * is completed.
1566	 */
1567	result = isc_app_ctxrun(actx);
1568
1569	LOCK(&reqarg->lock);
1570	if (result == ISC_R_SUCCESS || result == ISC_R_SUSPEND)
1571		result = reqarg->result;
1572	if (reqarg->trans != NULL) {
1573		/*
1574		 * Unusual termination (perhaps due to signal).  We need some
1575		 * tricky cleanup process.
1576		 */
1577		reqarg->canceled = ISC_TRUE;
1578		dns_client_cancelresolve(reqarg->trans);
1579
1580		UNLOCK(&reqarg->lock);
1581
1582		/* reqarg will be freed in the event handler. */
1583	} else {
1584		UNLOCK(&reqarg->lock);
1585
1586		DESTROYLOCK(&reqarg->lock);
1587		isc_mem_put(client->mctx, reqarg, sizeof(*reqarg));
1588	}
1589
1590	return (result);
1591}
1592
1593isc_result_t
1594dns_client_startrequest(dns_client_t *client, dns_message_t *qmessage,
1595			dns_message_t *rmessage, isc_sockaddr_t *server,
1596			unsigned int options, unsigned int parseoptions,
1597			dns_tsec_t *tsec, unsigned int timeout,
1598			unsigned int udptimeout, unsigned int udpretries,
1599			isc_task_t *task, isc_taskaction_t action, void *arg,
1600			dns_clientreqtrans_t **transp)
1601{
1602	isc_result_t result;
1603	dns_view_t *view = NULL;
1604	isc_task_t *clone = NULL;
1605	dns_clientreqevent_t *event = NULL;
1606	reqctx_t *ctx = NULL;
1607	dns_tsectype_t tsectype = dns_tsectype_none;
1608
1609	UNUSED(options);
1610
1611	REQUIRE(DNS_CLIENT_VALID(client));
1612	REQUIRE(qmessage != NULL);
1613	REQUIRE(rmessage != NULL);
1614	REQUIRE(transp != NULL && *transp == NULL);
1615
1616	if (tsec != NULL) {
1617		tsectype = dns_tsec_gettype(tsec);
1618		if (tsectype != dns_tsectype_tsig)
1619			return (ISC_R_NOTIMPLEMENTED); /* XXX */
1620	}
1621
1622	LOCK(&client->lock);
1623	result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
1624				   qmessage->rdclass, &view);
1625	UNLOCK(&client->lock);
1626	if (result != ISC_R_SUCCESS)
1627		return (result);
1628
1629	clone = NULL;
1630	isc_task_attach(task, &clone);
1631	event = (dns_clientreqevent_t *)
1632		isc_event_allocate(client->mctx, clone,
1633				   DNS_EVENT_CLIENTREQDONE,
1634				   action, arg, sizeof(*event));
1635	if (event == NULL) {
1636		result = ISC_R_NOMEMORY;
1637		goto cleanup;
1638	}
1639
1640	ctx = isc_mem_get(client->mctx, sizeof(*ctx));
1641	if (ctx == NULL)
1642		result = ISC_R_NOMEMORY;
1643	else {
1644		result = isc_mutex_init(&ctx->lock);
1645		if (result != ISC_R_SUCCESS) {
1646			isc_mem_put(client->mctx, ctx, sizeof(*ctx));
1647			ctx = NULL;
1648		}
1649	}
1650	if (result != ISC_R_SUCCESS)
1651		goto cleanup;
1652
1653	ctx->client = client;
1654	ISC_LINK_INIT(ctx, link);
1655	ctx->parseoptions = parseoptions;
1656	ctx->canceled = ISC_FALSE;
1657	ctx->event = event;
1658	ctx->event->rmessage = rmessage;
1659	ctx->tsigkey = NULL;
1660	if (tsec != NULL)
1661		dns_tsec_getkey(tsec, &ctx->tsigkey);
1662
1663	ctx->magic = REQCTX_MAGIC;
1664
1665	LOCK(&client->lock);
1666	ISC_LIST_APPEND(client->reqctxs, ctx, link);
1667	UNLOCK(&client->lock);
1668
1669	ctx->request = NULL;
1670	result = dns_request_createvia3(view->requestmgr, qmessage, NULL,
1671					server, options, ctx->tsigkey,
1672					timeout, udptimeout, udpretries,
1673					client->task, request_done, ctx,
1674					&ctx->request);
1675	if (result == ISC_R_SUCCESS) {
1676		dns_view_detach(&view);
1677		*transp = (dns_clientreqtrans_t *)ctx;
1678		return (ISC_R_SUCCESS);
1679	}
1680
1681 cleanup:
1682	if (ctx != NULL) {
1683		LOCK(&client->lock);
1684		ISC_LIST_UNLINK(client->reqctxs, ctx, link);
1685		UNLOCK(&client->lock);
1686		DESTROYLOCK(&ctx->lock);
1687		isc_mem_put(client->mctx, ctx, sizeof(*ctx));
1688	}
1689	if (event != NULL)
1690		isc_event_free(ISC_EVENT_PTR(&event));
1691	isc_task_detach(&clone);
1692	dns_view_detach(&view);
1693
1694	return (result);
1695}
1696
1697void
1698dns_client_cancelrequest(dns_clientreqtrans_t *trans) {
1699	reqctx_t *ctx;
1700
1701	REQUIRE(trans != NULL);
1702	ctx = (reqctx_t *)trans;
1703	REQUIRE(REQCTX_VALID(ctx));
1704
1705	LOCK(&ctx->lock);
1706
1707	if (!ctx->canceled) {
1708		ctx->canceled = ISC_TRUE;
1709		if (ctx->request != NULL)
1710			dns_request_cancel(ctx->request);
1711	}
1712
1713	UNLOCK(&ctx->lock);
1714}
1715
1716void
1717dns_client_destroyreqtrans(dns_clientreqtrans_t **transp) {
1718	reqctx_t *ctx;
1719	isc_mem_t *mctx;
1720	dns_client_t *client;
1721	isc_boolean_t need_destroyclient = ISC_FALSE;
1722
1723	REQUIRE(transp != NULL);
1724	ctx = (reqctx_t *)*transp;
1725	REQUIRE(REQCTX_VALID(ctx));
1726	client = ctx->client;
1727	REQUIRE(DNS_CLIENT_VALID(client));
1728	REQUIRE(ctx->event == NULL);
1729	REQUIRE(ctx->request != NULL);
1730
1731	dns_request_destroy(&ctx->request);
1732	mctx = client->mctx;
1733
1734	LOCK(&client->lock);
1735
1736	INSIST(ISC_LINK_LINKED(ctx, link));
1737	ISC_LIST_UNLINK(client->reqctxs, ctx, link);
1738
1739	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
1740	    ISC_LIST_EMPTY(client->reqctxs) &&
1741	    ISC_LIST_EMPTY(client->updatectxs)) {
1742		need_destroyclient = ISC_TRUE;
1743	}
1744
1745	UNLOCK(&client->lock);
1746
1747	DESTROYLOCK(&ctx->lock);
1748	ctx->magic = 0;
1749
1750	isc_mem_put(mctx, ctx, sizeof(*ctx));
1751
1752	if (need_destroyclient)
1753		destroyclient(&client);
1754
1755	*transp = NULL;
1756}
1757
1758/*%
1759 * Dynamic update routines
1760 */
1761static isc_result_t
1762rcode2result(dns_rcode_t rcode) {
1763	/* XXX: isn't there a similar function? */
1764	switch (rcode) {
1765	case dns_rcode_formerr:
1766		return (DNS_R_FORMERR);
1767	case dns_rcode_servfail:
1768		return (DNS_R_SERVFAIL);
1769	case dns_rcode_nxdomain:
1770		return (DNS_R_NXDOMAIN);
1771	case dns_rcode_notimp:
1772		return (DNS_R_NOTIMP);
1773	case dns_rcode_refused:
1774		return (DNS_R_REFUSED);
1775	case dns_rcode_yxdomain:
1776		return (DNS_R_YXDOMAIN);
1777	case dns_rcode_yxrrset:
1778		return (DNS_R_YXRRSET);
1779	case dns_rcode_nxrrset:
1780		return (DNS_R_NXRRSET);
1781	case dns_rcode_notauth:
1782		return (DNS_R_NOTAUTH);
1783	case dns_rcode_notzone:
1784		return (DNS_R_NOTZONE);
1785	case dns_rcode_badvers:
1786		return (DNS_R_BADVERS);
1787	}
1788
1789	return (ISC_R_FAILURE);
1790}
1791
1792static void
1793update_sendevent(updatectx_t *uctx, isc_result_t result) {
1794	isc_task_t *task;
1795
1796	dns_message_destroy(&uctx->updatemsg);
1797	if (uctx->tsigkey != NULL)
1798		dns_tsigkey_detach(&uctx->tsigkey);
1799	if (uctx->sig0key != NULL)
1800		dst_key_free(&uctx->sig0key);
1801
1802	if (uctx->canceled)
1803		uctx->event->result = ISC_R_CANCELED;
1804	else
1805		uctx->event->result = result;
1806	uctx->event->state = uctx->state;
1807	task = uctx->event->ev_sender;
1808	uctx->event->ev_sender = uctx;
1809	isc_task_sendanddetach(&task, ISC_EVENT_PTR(&uctx->event));
1810}
1811
1812static void
1813update_done(isc_task_t *task, isc_event_t *event) {
1814	isc_result_t result;
1815	dns_requestevent_t *reqev = NULL;
1816	dns_request_t *request;
1817	dns_message_t *answer = NULL;
1818	updatectx_t *uctx = event->ev_arg;
1819	dns_client_t *client;
1820	unsigned int timeout;
1821
1822	UNUSED(task);
1823
1824	REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
1825	reqev = (dns_requestevent_t *)event;
1826	request = reqev->request;
1827	REQUIRE(UCTX_VALID(uctx));
1828	client = uctx->client;
1829	REQUIRE(DNS_CLIENT_VALID(client));
1830
1831	result = reqev->result;
1832	if (result != ISC_R_SUCCESS)
1833		goto out;
1834
1835	result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE,
1836				    &answer);
1837	if (result != ISC_R_SUCCESS)
1838		goto out;
1839	uctx->state = dns_clientupdatestate_done;
1840	result = dns_request_getresponse(request, answer,
1841					 DNS_MESSAGEPARSE_PRESERVEORDER);
1842	if (result == ISC_R_SUCCESS && answer->rcode != dns_rcode_noerror)
1843		result = rcode2result(answer->rcode);
1844
1845 out:
1846	if (answer != NULL)
1847		dns_message_destroy(&answer);
1848	isc_event_free(&event);
1849
1850	LOCK(&uctx->lock);
1851	uctx->currentserver = ISC_LIST_NEXT(uctx->currentserver, link);
1852	dns_request_destroy(&uctx->updatereq);
1853	if (result != ISC_R_SUCCESS && !uctx->canceled &&
1854	    uctx->currentserver != NULL) {
1855		dns_message_renderreset(uctx->updatemsg);
1856		dns_message_settsigkey(uctx->updatemsg, NULL);
1857
1858		timeout = client->update_timeout / uctx->nservers;
1859		if (timeout < MIN_UPDATE_TIMEOUT)
1860			timeout = MIN_UPDATE_TIMEOUT;
1861		result = dns_request_createvia3(uctx->view->requestmgr,
1862						uctx->updatemsg,
1863						NULL,
1864						uctx->currentserver, 0,
1865						uctx->tsigkey,
1866						timeout,
1867						client->update_udptimeout,
1868						client->update_udpretries,
1869						client->task,
1870						update_done, uctx,
1871						&uctx->updatereq);
1872		UNLOCK(&uctx->lock);
1873
1874		if (result == ISC_R_SUCCESS) {
1875			/* XXX: should we keep the 'done' state here? */
1876			uctx->state = dns_clientupdatestate_sent;
1877			return;
1878		}
1879	} else
1880		UNLOCK(&uctx->lock);
1881
1882	update_sendevent(uctx, result);
1883}
1884
1885static isc_result_t
1886send_update(updatectx_t *uctx) {
1887	isc_result_t result;
1888	dns_name_t *name = NULL;
1889	dns_rdataset_t *rdataset = NULL;
1890	dns_client_t *client = uctx->client;
1891	unsigned int timeout;
1892
1893	REQUIRE(uctx->zonename != NULL && uctx->currentserver != NULL);
1894
1895	result = dns_message_gettempname(uctx->updatemsg, &name);
1896	if (result != ISC_R_SUCCESS)
1897		return (result);
1898	dns_name_init(name, NULL);
1899	dns_name_clone(uctx->zonename, name);
1900	result = dns_message_gettemprdataset(uctx->updatemsg, &rdataset);
1901	if (result != ISC_R_SUCCESS) {
1902		dns_message_puttempname(uctx->updatemsg, &name);
1903		return (result);
1904	}
1905	dns_rdataset_makequestion(rdataset, uctx->rdclass, dns_rdatatype_soa);
1906	ISC_LIST_INIT(name->list);
1907	ISC_LIST_APPEND(name->list, rdataset, link);
1908	dns_message_addname(uctx->updatemsg, name, DNS_SECTION_ZONE);
1909	if (uctx->tsigkey == NULL && uctx->sig0key != NULL) {
1910		result = dns_message_setsig0key(uctx->updatemsg,
1911						uctx->sig0key);
1912		if (result != ISC_R_SUCCESS)
1913			return (result);
1914	}
1915	timeout = client->update_timeout / uctx->nservers;
1916	if (timeout < MIN_UPDATE_TIMEOUT)
1917		timeout = MIN_UPDATE_TIMEOUT;
1918	result = dns_request_createvia3(uctx->view->requestmgr,
1919					uctx->updatemsg,
1920					NULL, uctx->currentserver, 0,
1921					uctx->tsigkey, timeout,
1922					client->update_udptimeout,
1923					client->update_udpretries,
1924					client->task, update_done, uctx,
1925					&uctx->updatereq);
1926	if (result == ISC_R_SUCCESS &&
1927	    uctx->state == dns_clientupdatestate_prepare) {
1928		uctx->state = dns_clientupdatestate_sent;
1929	}
1930
1931	return (result);
1932}
1933
1934static void
1935resolveaddr_done(isc_task_t *task, isc_event_t *event) {
1936	isc_result_t result;
1937	int family;
1938	dns_rdatatype_t qtype;
1939	dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
1940	dns_name_t *name;
1941	dns_rdataset_t *rdataset;
1942	updatectx_t *uctx;
1943	isc_boolean_t completed = ISC_FALSE;
1944
1945	UNUSED(task);
1946
1947	REQUIRE(event->ev_arg != NULL);
1948	uctx = *(updatectx_t **)event->ev_arg;
1949	REQUIRE(UCTX_VALID(uctx));
1950
1951	if (event->ev_arg == &uctx->bp4) {
1952		family = AF_INET;
1953		qtype = dns_rdatatype_a;
1954		LOCK(&uctx->lock);
1955		dns_client_destroyrestrans(&uctx->restrans);
1956		UNLOCK(&uctx->lock);
1957	} else {
1958		INSIST(event->ev_arg == &uctx->bp6);
1959		family = AF_INET6;
1960		qtype = dns_rdatatype_aaaa;
1961		LOCK(&uctx->lock);
1962		dns_client_destroyrestrans(&uctx->restrans2);
1963		UNLOCK(&uctx->lock);
1964	}
1965
1966	result = rev->result;
1967	if (result != ISC_R_SUCCESS)
1968		goto done;
1969
1970	for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL;
1971	     name = ISC_LIST_NEXT(name, link)) {
1972		for (rdataset = ISC_LIST_HEAD(name->list);
1973		     rdataset != NULL;
1974		     rdataset = ISC_LIST_NEXT(rdataset, link)) {
1975			if (!dns_rdataset_isassociated(rdataset))
1976				continue;
1977			if (rdataset->type != qtype)
1978				continue;
1979
1980			for (result = dns_rdataset_first(rdataset);
1981			     result == ISC_R_SUCCESS;
1982			     result = dns_rdataset_next(rdataset)) {
1983				dns_rdata_t rdata;
1984				dns_rdata_in_a_t rdata_a;
1985				dns_rdata_in_aaaa_t rdata_aaaa;
1986				isc_sockaddr_t *sa;
1987
1988				sa = isc_mem_get(uctx->client->mctx,
1989						 sizeof(*sa));
1990				if (sa == NULL) {
1991					/*
1992					 * If we fail to get a sockaddr,
1993					 we simply move forward with the
1994					 * addresses we've got so far.
1995					 */
1996					goto done;
1997				}
1998
1999				dns_rdata_init(&rdata);
2000				switch (family) {
2001				case AF_INET:
2002					dns_rdataset_current(rdataset, &rdata);
2003					dns_rdata_tostruct(&rdata, &rdata_a,
2004							   NULL);
2005					isc_sockaddr_fromin(sa,
2006							    &rdata_a.in_addr,
2007							    53);
2008					dns_rdata_freestruct(&rdata_a);
2009					break;
2010				case AF_INET6:
2011					dns_rdataset_current(rdataset, &rdata);
2012					dns_rdata_tostruct(&rdata, &

Large files files are truncated, but you can click here to view the full file