]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/dns/client.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / dns / client.c
1 /*
2  * Copyright (C) 2009-2013  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: client.c,v 1.14 2011/03/12 04:59:47 tbox Exp $ */
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  */
73 struct 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  */
120 typedef 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  */
146 typedef 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  */
163 typedef 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  */
181 typedef 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  */
196 typedef 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  */
211 typedef 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
245 static isc_result_t request_soa(updatectx_t *uctx);
246 static void client_resfind(resctx_t *rctx, dns_fetchevent_t *event);
247 static isc_result_t send_update(updatectx_t *uctx);
248
249 static isc_result_t
250 getudpdispatch(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                isc_sockaddr_t *localaddr)
254 {
255         unsigned int attrs, attrmask;
256         dns_dispatch_t *disp;
257         unsigned buffersize, maxbuffers, maxrequests, buckets, increment;
258         isc_result_t result;
259         isc_sockaddr_t anyaddr;
260
261         attrs = 0;
262         attrs |= DNS_DISPATCHATTR_UDP;
263         switch (family) {
264         case AF_INET:
265                 attrs |= DNS_DISPATCHATTR_IPV4;
266                 break;
267         case AF_INET6:
268                 attrs |= DNS_DISPATCHATTR_IPV6;
269                 break;
270         default:
271                 INSIST(0);
272         }
273         attrmask = 0;
274         attrmask |= DNS_DISPATCHATTR_UDP;
275         attrmask |= DNS_DISPATCHATTR_TCP;
276         attrmask |= DNS_DISPATCHATTR_IPV4;
277         attrmask |= DNS_DISPATCHATTR_IPV6;
278
279         if (localaddr == NULL) {
280                 localaddr = &anyaddr;
281                 isc_sockaddr_anyofpf(localaddr, family);
282         }
283
284         buffersize = 4096;
285         maxbuffers = is_shared ? 1000 : 8;
286         maxrequests = 32768;
287         buckets = is_shared ? 16411 : 3;
288         increment = is_shared ? 16433 : 5;
289
290         disp = NULL;
291         result = dns_dispatch_getudp(dispatchmgr, socketmgr,
292                                      taskmgr, localaddr,
293                                      buffersize, maxbuffers, maxrequests,
294                                      buckets, increment,
295                                      attrs, attrmask, &disp);
296         if (result == ISC_R_SUCCESS)
297                 *dispp = disp;
298
299         return (result);
300 }
301
302 static isc_result_t
303 dns_client_createview(isc_mem_t *mctx, dns_rdataclass_t rdclass,
304                       unsigned int options, isc_taskmgr_t *taskmgr,
305                       unsigned int ntasks, isc_socketmgr_t *socketmgr,
306                       isc_timermgr_t *timermgr, dns_dispatchmgr_t *dispatchmgr,
307                       dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6,
308                       dns_view_t **viewp)
309 {
310         isc_result_t result;
311         dns_view_t *view = NULL;
312         const char *dbtype;
313
314         result = dns_view_create(mctx, rdclass, DNS_CLIENTVIEW_NAME, &view);
315         if (result != ISC_R_SUCCESS)
316                 return (result);
317
318         /* Initialize view security roots */
319         result = dns_view_initsecroots(view, mctx);
320         if (result != ISC_R_SUCCESS) {
321                 dns_view_detach(&view);
322                 return (result);
323         }
324
325         result = dns_view_createresolver(view, taskmgr, ntasks, 1, socketmgr,
326                                          timermgr, 0, dispatchmgr,
327                                          dispatchv4, dispatchv6);
328         if (result != ISC_R_SUCCESS) {
329                 dns_view_detach(&view);
330                 return (result);
331         }
332
333         /*
334          * Set cache DB.
335          * XXX: it may be better if specific DB implementations can be
336          * specified via some configuration knob.
337          */
338         if ((options & DNS_CLIENTCREATEOPT_USECACHE) != 0)
339                 dbtype = "rbt";
340         else
341                 dbtype = "ecdb";
342         result = dns_db_create(mctx, dbtype, dns_rootname, dns_dbtype_cache,
343                                rdclass, 0, NULL, &view->cachedb);
344         if (result != ISC_R_SUCCESS) {
345                 dns_view_detach(&view);
346                 return (result);
347         }
348
349         *viewp = view;
350         return (ISC_R_SUCCESS);
351 }
352
353 isc_result_t
354 dns_client_create(dns_client_t **clientp, unsigned int options) {
355         isc_result_t result;
356         isc_mem_t *mctx = NULL;
357         isc_appctx_t *actx = NULL;
358         isc_taskmgr_t *taskmgr = NULL;
359         isc_socketmgr_t *socketmgr = NULL;
360         isc_timermgr_t *timermgr = NULL;
361 #if 0
362         /* XXXMPA add debug logging support */
363         isc_log_t *lctx = NULL;
364         isc_logconfig_t *logconfig = NULL;
365         unsigned int logdebuglevel = 0;
366 #endif
367
368         result = isc_mem_create(0, 0, &mctx);
369         if (result != ISC_R_SUCCESS)
370                 return (result);
371         result = isc_appctx_create(mctx, &actx);
372         if (result != ISC_R_SUCCESS)
373                 goto cleanup;
374         result = isc_app_ctxstart(actx);
375         if (result != ISC_R_SUCCESS)
376                 goto cleanup;
377         result = isc_taskmgr_createinctx(mctx, actx, 1, 0, &taskmgr);
378         if (result != ISC_R_SUCCESS)
379                 goto cleanup;
380         result = isc_socketmgr_createinctx(mctx, actx, &socketmgr);
381         if (result != ISC_R_SUCCESS)
382                 goto cleanup;
383         result = isc_timermgr_createinctx(mctx, actx, &timermgr);
384         if (result != ISC_R_SUCCESS)
385                 goto cleanup;
386 #if 0
387         result = isc_log_create(mctx, &lctx, &logconfig);
388         if (result != ISC_R_SUCCESS)
389                 goto cleanup;
390         isc_log_setcontext(lctx);
391         dns_log_init(lctx);
392         dns_log_setcontext(lctx);
393         result = isc_log_usechannel(logconfig, "default_debug", NULL, NULL);
394         if (result != ISC_R_SUCCESS)
395                 goto cleanup;
396         isc_log_setdebuglevel(lctx, logdebuglevel);
397 #endif
398         result = dns_client_createx(mctx, actx, taskmgr, socketmgr, timermgr,
399                                     options, clientp);
400         if (result != ISC_R_SUCCESS)
401                 goto cleanup;
402
403         (*clientp)->attributes |= DNS_CLIENTATTR_OWNCTX;
404
405         /* client has its own reference to mctx, so we can detach it here */
406         isc_mem_detach(&mctx);
407
408         return (ISC_R_SUCCESS);
409
410  cleanup:
411         if (taskmgr != NULL)
412                 isc_taskmgr_destroy(&taskmgr);
413         if (timermgr != NULL)
414                 isc_timermgr_destroy(&timermgr);
415         if (socketmgr != NULL)
416                 isc_socketmgr_destroy(&socketmgr);
417         if (actx != NULL)
418                 isc_appctx_destroy(&actx);
419         isc_mem_detach(&mctx);
420
421         return (result);
422 }
423
424 isc_result_t
425 dns_client_createx(isc_mem_t *mctx, isc_appctx_t *actx, isc_taskmgr_t *taskmgr,
426                    isc_socketmgr_t *socketmgr, isc_timermgr_t *timermgr,
427                    unsigned int options, dns_client_t **clientp)
428 {
429         isc_result_t result;
430         result = dns_client_createx2(mctx, actx, taskmgr, socketmgr, timermgr,
431                                      options, clientp, NULL, NULL);
432         return (result);
433 }
434
435 isc_result_t
436 dns_client_createx2(isc_mem_t *mctx, isc_appctx_t *actx,
437                     isc_taskmgr_t *taskmgr, isc_socketmgr_t *socketmgr,
438                     isc_timermgr_t *timermgr, unsigned int options,
439                     dns_client_t **clientp, isc_sockaddr_t *localaddr4,
440                     isc_sockaddr_t *localaddr6)
441 {
442         dns_client_t *client;
443         isc_result_t result;
444         dns_dispatchmgr_t *dispatchmgr = NULL;
445         dns_dispatch_t *dispatchv4 = NULL;
446         dns_dispatch_t *dispatchv6 = NULL;
447         dns_view_t *view = NULL;
448
449         REQUIRE(mctx != NULL);
450         REQUIRE(taskmgr != NULL);
451         REQUIRE(timermgr != NULL);
452         REQUIRE(socketmgr != NULL);
453         REQUIRE(clientp != NULL && *clientp == NULL);
454
455         client = isc_mem_get(mctx, sizeof(*client));
456         if (client == NULL)
457                 return (ISC_R_NOMEMORY);
458
459         result = isc_mutex_init(&client->lock);
460         if (result != ISC_R_SUCCESS) {
461                 isc_mem_put(mctx, client, sizeof(*client));
462                 return (result);
463         }
464
465         client->actx = actx;
466         client->taskmgr = taskmgr;
467         client->socketmgr = socketmgr;
468         client->timermgr = timermgr;
469
470         client->task = NULL;
471         result = isc_task_create(client->taskmgr, 0, &client->task);
472         if (result != ISC_R_SUCCESS)
473                 goto cleanup;
474
475         result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
476         if (result != ISC_R_SUCCESS)
477                 goto cleanup;
478         client->dispatchmgr = dispatchmgr;
479
480         /*
481          * If only one address family is specified, use it.
482          * If neither family is specified, or if both are, use both.
483          */
484         client->dispatchv4 = NULL;
485         if (localaddr4 != NULL || localaddr6 == NULL) {
486                 result = getudpdispatch(AF_INET, dispatchmgr, socketmgr,
487                                         taskmgr, ISC_TRUE,
488                                         &dispatchv4, localaddr4);
489                 if (result == ISC_R_SUCCESS)
490                         client->dispatchv4 = dispatchv4;
491         }
492
493         client->dispatchv6 = NULL;
494         if (localaddr6 != NULL || localaddr4 == NULL) {
495                 result = getudpdispatch(AF_INET6, dispatchmgr, socketmgr,
496                                         taskmgr, ISC_TRUE,
497                                         &dispatchv6, localaddr6);
498                 if (result == ISC_R_SUCCESS)
499                         client->dispatchv6 = dispatchv6;
500         }
501
502         /* We need at least one of the dispatchers */
503         if (dispatchv4 == NULL && dispatchv6 == NULL) {
504                 INSIST(result != ISC_R_SUCCESS);
505                 goto cleanup;
506         }
507
508         /* Create the default view for class IN */
509         result = dns_client_createview(mctx, dns_rdataclass_in, options,
510                                        taskmgr, 31, socketmgr, timermgr,
511                                        dispatchmgr, dispatchv4, dispatchv6,
512                                        &view);
513         if (result != ISC_R_SUCCESS)
514                 goto cleanup;
515         ISC_LIST_INIT(client->viewlist);
516         ISC_LIST_APPEND(client->viewlist, view, link);
517
518         dns_view_freeze(view); /* too early? */
519
520         ISC_LIST_INIT(client->resctxs);
521         ISC_LIST_INIT(client->reqctxs);
522         ISC_LIST_INIT(client->updatectxs);
523
524         client->mctx = NULL;
525         isc_mem_attach(mctx, &client->mctx);
526
527         client->update_timeout = DEF_UPDATE_TIMEOUT;
528         client->update_udptimeout = DEF_UPDATE_UDPTIMEOUT;
529         client->update_udpretries = DEF_UPDATE_UDPRETRIES;
530         client->find_timeout = DEF_FIND_TIMEOUT;
531         client->find_udpretries = DEF_FIND_UDPRETRIES;
532         client->attributes = 0;
533
534         client->references = 1;
535         client->magic = DNS_CLIENT_MAGIC;
536
537         *clientp = client;
538
539         return (ISC_R_SUCCESS);
540
541  cleanup:
542         if (dispatchv4 != NULL)
543                 dns_dispatch_detach(&dispatchv4);
544         if (dispatchv6 != NULL)
545                 dns_dispatch_detach(&dispatchv6);
546         if (dispatchmgr != NULL)
547                 dns_dispatchmgr_destroy(&dispatchmgr);
548         if (client->task != NULL)
549                 isc_task_detach(&client->task);
550         isc_mem_put(mctx, client, sizeof(*client));
551
552         return (result);
553 }
554
555 static void
556 destroyclient(dns_client_t **clientp) {
557         dns_client_t *client = *clientp;
558         dns_view_t *view;
559
560         while ((view = ISC_LIST_HEAD(client->viewlist)) != NULL) {
561                 ISC_LIST_UNLINK(client->viewlist, view, link);
562                 dns_view_detach(&view);
563         }
564
565         if (client->dispatchv4 != NULL)
566                 dns_dispatch_detach(&client->dispatchv4);
567         if (client->dispatchv6 != NULL)
568                 dns_dispatch_detach(&client->dispatchv6);
569
570         dns_dispatchmgr_destroy(&client->dispatchmgr);
571
572         isc_task_detach(&client->task);
573
574         /*
575          * If the client has created its own running environments,
576          * destroy them.
577          */
578         if ((client->attributes & DNS_CLIENTATTR_OWNCTX) != 0) {
579                 isc_taskmgr_destroy(&client->taskmgr);
580                 isc_timermgr_destroy(&client->timermgr);
581                 isc_socketmgr_destroy(&client->socketmgr);
582
583                 isc_app_ctxfinish(client->actx);
584                 isc_appctx_destroy(&client->actx);
585         }
586
587         DESTROYLOCK(&client->lock);
588         client->magic = 0;
589
590         isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
591
592         *clientp = NULL;
593 }
594
595 void
596 dns_client_destroy(dns_client_t **clientp) {
597         dns_client_t *client;
598         isc_boolean_t destroyok = ISC_FALSE;
599
600         REQUIRE(clientp != NULL);
601         client = *clientp;
602         REQUIRE(DNS_CLIENT_VALID(client));
603
604         LOCK(&client->lock);
605         client->references--;
606         if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
607             ISC_LIST_EMPTY(client->reqctxs) &&
608             ISC_LIST_EMPTY(client->updatectxs)) {
609                 destroyok = ISC_TRUE;
610         }
611         UNLOCK(&client->lock);
612
613         if (destroyok)
614                 destroyclient(&client);
615
616         *clientp = NULL;
617 }
618
619 isc_result_t
620 dns_client_setservers(dns_client_t *client, dns_rdataclass_t rdclass,
621                       dns_name_t *namespace, isc_sockaddrlist_t *addrs)
622 {
623         isc_result_t result;
624         dns_view_t *view = NULL;
625
626         REQUIRE(DNS_CLIENT_VALID(client));
627         REQUIRE(addrs != NULL);
628
629         if (namespace == NULL)
630                 namespace = dns_rootname;
631
632         LOCK(&client->lock);
633         result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
634                                    rdclass, &view);
635         if (result != ISC_R_SUCCESS) {
636                 UNLOCK(&client->lock);
637                 return (result);
638         }
639         UNLOCK(&client->lock);
640
641         result = dns_fwdtable_add(view->fwdtable, namespace, addrs,
642                                   dns_fwdpolicy_only);
643
644         dns_view_detach(&view);
645
646         return (result);
647 }
648
649 isc_result_t
650 dns_client_clearservers(dns_client_t *client, dns_rdataclass_t rdclass,
651                         dns_name_t *namespace)
652 {
653         isc_result_t result;
654         dns_view_t *view = NULL;
655
656         REQUIRE(DNS_CLIENT_VALID(client));
657
658         if (namespace == NULL)
659                 namespace = dns_rootname;
660
661         LOCK(&client->lock);
662         result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
663                                    rdclass, &view);
664         if (result != ISC_R_SUCCESS) {
665                 UNLOCK(&client->lock);
666                 return (result);
667         }
668         UNLOCK(&client->lock);
669
670         result = dns_fwdtable_delete(view->fwdtable, namespace);
671
672         dns_view_detach(&view);
673
674         return (result);
675 }
676
677 static isc_result_t
678 getrdataset(isc_mem_t *mctx, dns_rdataset_t **rdatasetp) {
679         dns_rdataset_t *rdataset;
680
681         REQUIRE(mctx != NULL);
682         REQUIRE(rdatasetp != NULL && *rdatasetp == NULL);
683
684         rdataset = isc_mem_get(mctx, sizeof(*rdataset));
685         if (rdataset == NULL)
686                 return (ISC_R_NOMEMORY);
687
688         dns_rdataset_init(rdataset);
689
690         *rdatasetp = rdataset;
691
692         return (ISC_R_SUCCESS);
693 }
694
695 static void
696 putrdataset(isc_mem_t *mctx, dns_rdataset_t **rdatasetp) {
697         dns_rdataset_t *rdataset;
698
699         REQUIRE(rdatasetp != NULL);
700         rdataset = *rdatasetp;
701         REQUIRE(rdataset != NULL);
702
703         if (dns_rdataset_isassociated(rdataset))
704                 dns_rdataset_disassociate(rdataset);
705
706         isc_mem_put(mctx, rdataset, sizeof(*rdataset));
707
708         *rdatasetp = NULL;
709 }
710
711 static void
712 fetch_done(isc_task_t *task, isc_event_t *event) {
713         resctx_t *rctx = event->ev_arg;
714         dns_fetchevent_t *fevent;
715
716         REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
717         REQUIRE(RCTX_VALID(rctx));
718         REQUIRE(rctx->task == task);
719         fevent = (dns_fetchevent_t *)event;
720
721         client_resfind(rctx, fevent);
722 }
723
724 static inline isc_result_t
725 start_fetch(resctx_t *rctx) {
726         isc_result_t result;
727
728         /*
729          * The caller must be holding the rctx's lock.
730          */
731
732         REQUIRE(rctx->fetch == NULL);
733
734         result = dns_resolver_createfetch(rctx->view->resolver,
735                                           dns_fixedname_name(&rctx->name),
736                                           rctx->type,
737                                           NULL, NULL, NULL, 0,
738                                           rctx->task, fetch_done, rctx,
739                                           rctx->rdataset,
740                                           rctx->sigrdataset,
741                                           &rctx->fetch);
742
743         return (result);
744 }
745
746 static isc_result_t
747 view_find(resctx_t *rctx, dns_db_t **dbp, dns_dbnode_t **nodep,
748           dns_name_t *foundname)
749 {
750         isc_result_t result;
751         dns_name_t *name = dns_fixedname_name(&rctx->name);
752         dns_rdatatype_t type;
753
754         if (rctx->type == dns_rdatatype_rrsig)
755                 type = dns_rdatatype_any;
756         else
757                 type = rctx->type;
758
759         result = dns_view_find(rctx->view, name, type, 0, 0, ISC_FALSE,
760                                dbp, nodep, foundname, rctx->rdataset,
761                                rctx->sigrdataset);
762
763         return (result);
764 }
765
766 static void
767 client_resfind(resctx_t *rctx, dns_fetchevent_t *event) {
768         isc_mem_t *mctx;
769         isc_result_t tresult, result = ISC_R_SUCCESS;
770         isc_result_t vresult = ISC_R_SUCCESS;
771         isc_boolean_t want_restart;
772         isc_boolean_t send_event = ISC_FALSE;
773         dns_name_t *name, *prefix;
774         dns_fixedname_t foundname, fixed;
775         dns_rdataset_t *trdataset;
776         dns_rdata_t rdata = DNS_RDATA_INIT;
777         unsigned int nlabels;
778         int order;
779         dns_namereln_t namereln;
780         dns_rdata_cname_t cname;
781         dns_rdata_dname_t dname;
782
783         REQUIRE(RCTX_VALID(rctx));
784
785         LOCK(&rctx->lock);
786
787         mctx = rctx->view->mctx;
788
789         name = dns_fixedname_name(&rctx->name);
790
791         do {
792                 dns_name_t *fname = NULL;
793                 dns_name_t *ansname = NULL;
794                 dns_db_t *db = NULL;
795                 dns_dbnode_t *node = NULL;
796
797                 rctx->restarts++;
798                 want_restart = ISC_FALSE;
799
800                 if (event == NULL && !rctx->canceled) {
801                         dns_fixedname_init(&foundname);
802                         fname = dns_fixedname_name(&foundname);
803                         INSIST(!dns_rdataset_isassociated(rctx->rdataset));
804                         INSIST(rctx->sigrdataset == NULL ||
805                                !dns_rdataset_isassociated(rctx->sigrdataset));
806                         result = view_find(rctx, &db, &node, fname);
807                         if (result == ISC_R_NOTFOUND) {
808                                 /*
809                                  * We don't know anything about the name.
810                                  * Launch a fetch.
811                                  */
812                                 if (node != NULL) {
813                                         INSIST(db != NULL);
814                                         dns_db_detachnode(db, &node);
815                                 }
816                                 if (db != NULL)
817                                         dns_db_detach(&db);
818                                 result = start_fetch(rctx);
819                                 if (result != ISC_R_SUCCESS) {
820                                         putrdataset(mctx, &rctx->rdataset);
821                                         if (rctx->sigrdataset != NULL)
822                                                 putrdataset(mctx,
823                                                             &rctx->sigrdataset);
824                                         send_event = ISC_TRUE;
825                                 }
826                                 goto done;
827                         }
828                 } else {
829                         INSIST(event != NULL);
830                         INSIST(event->fetch == rctx->fetch);
831                         dns_resolver_destroyfetch(&rctx->fetch);
832                         db = event->db;
833                         node = event->node;
834                         result = event->result;
835                         vresult = event->vresult;
836                         fname = dns_fixedname_name(&event->foundname);
837                         INSIST(event->rdataset == rctx->rdataset);
838                         INSIST(event->sigrdataset == rctx->sigrdataset);
839                 }
840
841                 /*
842                  * If we've been canceled, forget about the result.
843                  */
844                 if (rctx->canceled)
845                         result = ISC_R_CANCELED;
846                 else {
847                         /*
848                          * Otherwise, get some resource for copying the
849                          * result.
850                          */
851                         ansname = isc_mem_get(mctx, sizeof(*ansname));
852                         if (ansname == NULL)
853                                 tresult = ISC_R_NOMEMORY;
854                         else {
855                                 dns_name_t *aname;
856
857                                 aname = dns_fixedname_name(&rctx->name);
858                                 dns_name_init(ansname, NULL);
859                                 tresult = dns_name_dup(aname, mctx, ansname);
860                                 if (tresult != ISC_R_SUCCESS)
861                                         isc_mem_put(mctx, ansname,
862                                                     sizeof(*ansname));
863                         }
864                         if (tresult != ISC_R_SUCCESS)
865                                 result = tresult;
866                 }
867
868                 switch (result) {
869                 case ISC_R_SUCCESS:
870                         send_event = ISC_TRUE;
871                         /*
872                          * This case is handled in the main line below.
873                          */
874                         break;
875                 case DNS_R_CNAME:
876                         /*
877                          * Add the CNAME to the answer list.
878                          */
879                         trdataset = rctx->rdataset;
880                         ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
881                         rctx->rdataset = NULL;
882                         if (rctx->sigrdataset != NULL) {
883                                 ISC_LIST_APPEND(ansname->list,
884                                                 rctx->sigrdataset, link);
885                                 rctx->sigrdataset = NULL;
886                         }
887                         ISC_LIST_APPEND(rctx->namelist, ansname, link);
888                         ansname = NULL;
889
890                         /*
891                          * Copy the CNAME's target into the lookup's
892                          * query name and start over.
893                          */
894                         tresult = dns_rdataset_first(trdataset);
895                         if (tresult != ISC_R_SUCCESS)
896                                 goto done;
897                         dns_rdataset_current(trdataset, &rdata);
898                         tresult = dns_rdata_tostruct(&rdata, &cname, NULL);
899                         dns_rdata_reset(&rdata);
900                         if (tresult != ISC_R_SUCCESS)
901                                 goto done;
902                         tresult = dns_name_copy(&cname.cname, name, NULL);
903                         dns_rdata_freestruct(&cname);
904                         if (tresult == ISC_R_SUCCESS)
905                                 want_restart = ISC_TRUE;
906                         else
907                                 result = tresult;
908                         goto done;
909                 case DNS_R_DNAME:
910                         /*
911                          * Add the DNAME to the answer list.
912                          */
913                         trdataset = rctx->rdataset;
914                         ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
915                         rctx->rdataset = NULL;
916                         if (rctx->sigrdataset != NULL) {
917                                 ISC_LIST_APPEND(ansname->list,
918                                                 rctx->sigrdataset, link);
919                                 rctx->sigrdataset = NULL;
920                         }
921                         ISC_LIST_APPEND(rctx->namelist, ansname, link);
922                         ansname = NULL;
923
924                         namereln = dns_name_fullcompare(name, fname, &order,
925                                                         &nlabels);
926                         INSIST(namereln == dns_namereln_subdomain);
927                         /*
928                          * Get the target name of the DNAME.
929                          */
930                         tresult = dns_rdataset_first(trdataset);
931                         if (tresult != ISC_R_SUCCESS) {
932                                 result = tresult;
933                                 goto done;
934                         }
935                         dns_rdataset_current(trdataset, &rdata);
936                         tresult = dns_rdata_tostruct(&rdata, &dname, NULL);
937                         dns_rdata_reset(&rdata);
938                         if (tresult != ISC_R_SUCCESS) {
939                                 result = tresult;
940                                 goto done;
941                         }
942                         /*
943                          * Construct the new query name and start over.
944                          */
945                         dns_fixedname_init(&fixed);
946                         prefix = dns_fixedname_name(&fixed);
947                         dns_name_split(name, nlabels, prefix, NULL);
948                         tresult = dns_name_concatenate(prefix, &dname.dname,
949                                                       name, NULL);
950                         dns_rdata_freestruct(&dname);
951                         if (tresult == ISC_R_SUCCESS)
952                                 want_restart = ISC_TRUE;
953                         else
954                                 result = tresult;
955                         goto done;
956                 case DNS_R_NCACHENXDOMAIN:
957                 case DNS_R_NCACHENXRRSET:
958                         ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
959                         ISC_LIST_APPEND(rctx->namelist, ansname, link);
960                         ansname = NULL;
961                         rctx->rdataset = NULL;
962                         /* What about sigrdataset? */
963                         if (rctx->sigrdataset != NULL)
964                                 putrdataset(mctx, &rctx->sigrdataset);
965                         send_event = ISC_TRUE;
966                         goto done;
967                 default:
968                         if (rctx->rdataset != NULL)
969                                 putrdataset(mctx, &rctx->rdataset);
970                         if (rctx->sigrdataset != NULL)
971                                 putrdataset(mctx, &rctx->sigrdataset);
972                         send_event = ISC_TRUE;
973                         goto done;
974                 }
975
976                 if (rctx->type == dns_rdatatype_any) {
977                         int n = 0;
978                         dns_rdatasetiter_t *rdsiter = NULL;
979
980                         tresult = dns_db_allrdatasets(db, node, NULL, 0,
981                                                       &rdsiter);
982                         if (tresult != ISC_R_SUCCESS) {
983                                 result = tresult;
984                                 goto done;
985                         }
986
987                         tresult = dns_rdatasetiter_first(rdsiter);
988                         while (tresult == ISC_R_SUCCESS) {
989                                 dns_rdatasetiter_current(rdsiter,
990                                                          rctx->rdataset);
991                                 if (rctx->rdataset->type != 0) {
992                                         ISC_LIST_APPEND(ansname->list,
993                                                         rctx->rdataset,
994                                                         link);
995                                         n++;
996                                         rctx->rdataset = NULL;
997                                 } else {
998                                         /*
999                                          * We're not interested in this
1000                                          * rdataset.
1001                                          */
1002                                         dns_rdataset_disassociate(
1003                                                 rctx->rdataset);
1004                                 }
1005                                 tresult = dns_rdatasetiter_next(rdsiter);
1006
1007                                 if (tresult == ISC_R_SUCCESS &&
1008                                     rctx->rdataset == NULL) {
1009                                         tresult = getrdataset(mctx,
1010                                                               &rctx->rdataset);
1011                                         if (tresult != ISC_R_SUCCESS) {
1012                                                 result = tresult;
1013                                                 POST(result);
1014                                                 break;
1015                                         }
1016                                 }
1017                         }
1018                         if (n == 0) {
1019                                 /*
1020                                  * We didn't match any rdatasets (which means
1021                                  * something went wrong in this
1022                                  * implementation).
1023                                  */
1024                                 result = DNS_R_SERVFAIL; /* better code? */
1025                                 POST(result);
1026                         } else {
1027                                 ISC_LIST_APPEND(rctx->namelist, ansname, link);
1028                                 ansname = NULL;
1029                         }
1030                         dns_rdatasetiter_destroy(&rdsiter);
1031                         if (tresult != ISC_R_NOMORE)
1032                                 result = DNS_R_SERVFAIL; /* ditto */
1033                         else
1034                                 result = ISC_R_SUCCESS;
1035                         goto done;
1036                 } else {
1037                         /*
1038                          * This is the "normal" case -- an ordinary question
1039                          * to which we've got the answer.
1040                          */
1041                         ISC_LIST_APPEND(ansname->list, rctx->rdataset, link);
1042                         rctx->rdataset = NULL;
1043                         if (rctx->sigrdataset != NULL) {
1044                                 ISC_LIST_APPEND(ansname->list,
1045                                                 rctx->sigrdataset, link);
1046                                 rctx->sigrdataset = NULL;
1047                         }
1048                         ISC_LIST_APPEND(rctx->namelist, ansname, link);
1049                         ansname = NULL;
1050                 }
1051
1052         done:
1053                 /*
1054                  * Free temporary resources
1055                  */
1056                 if (ansname != NULL) {
1057                         dns_rdataset_t *rdataset;
1058
1059                         while ((rdataset = ISC_LIST_HEAD(ansname->list))
1060                                != NULL) {
1061                                 ISC_LIST_UNLINK(ansname->list, rdataset, link);
1062                                 putrdataset(mctx, &rdataset);
1063                         }
1064                         dns_name_free(ansname, mctx);
1065                         isc_mem_put(mctx, ansname, sizeof(*ansname));
1066                 }
1067
1068                 if (node != NULL)
1069                         dns_db_detachnode(db, &node);
1070                 if (db != NULL)
1071                         dns_db_detach(&db);
1072                 if (event != NULL)
1073                         isc_event_free(ISC_EVENT_PTR(&event));
1074
1075                 /*
1076                  * Limit the number of restarts.
1077                  */
1078                 if (want_restart && rctx->restarts == MAX_RESTARTS) {
1079                         want_restart = ISC_FALSE;
1080                         result = ISC_R_QUOTA;
1081                         send_event = ISC_TRUE;
1082                 }
1083
1084                 /*
1085                  * Prepare further find with new resources
1086                  */
1087                 if (want_restart) {
1088                         INSIST(rctx->rdataset == NULL &&
1089                                rctx->sigrdataset == NULL);
1090
1091                         result = getrdataset(mctx, &rctx->rdataset);
1092                         if (result == ISC_R_SUCCESS && rctx->want_dnssec) {
1093                                 result = getrdataset(mctx, &rctx->sigrdataset);
1094                                 if (result != ISC_R_SUCCESS) {
1095                                         putrdataset(mctx, &rctx->rdataset);
1096                                 }
1097                         }
1098
1099                         if (result != ISC_R_SUCCESS) {
1100                                 want_restart = ISC_FALSE;
1101                                 send_event = ISC_TRUE;
1102                         }
1103                 }
1104         } while (want_restart);
1105
1106         if (send_event) {
1107                 isc_task_t *task;
1108
1109                 while ((name = ISC_LIST_HEAD(rctx->namelist)) != NULL) {
1110                         ISC_LIST_UNLINK(rctx->namelist, name, link);
1111                         ISC_LIST_APPEND(rctx->event->answerlist, name, link);
1112                 }
1113
1114                 rctx->event->result = result;
1115                 rctx->event->vresult = vresult;
1116                 task = rctx->event->ev_sender;
1117                 rctx->event->ev_sender = rctx;
1118                 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&rctx->event));
1119         }
1120
1121         UNLOCK(&rctx->lock);
1122 }
1123
1124
1125 static void
1126 suspend(isc_task_t *task, isc_event_t *event) {
1127         isc_appctx_t *actx = event->ev_arg;
1128
1129         UNUSED(task);
1130
1131         isc_app_ctxsuspend(actx);
1132         isc_event_free(&event);
1133 }
1134
1135 static void
1136 resolve_done(isc_task_t *task, isc_event_t *event) {
1137         resarg_t *resarg = event->ev_arg;
1138         dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
1139         dns_name_t *name;
1140         isc_result_t result;
1141
1142         UNUSED(task);
1143
1144         LOCK(&resarg->lock);
1145
1146         resarg->result = rev->result;
1147         resarg->vresult = rev->vresult;
1148         while ((name = ISC_LIST_HEAD(rev->answerlist)) != NULL) {
1149                 ISC_LIST_UNLINK(rev->answerlist, name, link);
1150                 ISC_LIST_APPEND(*resarg->namelist, name, link);
1151         }
1152
1153         dns_client_destroyrestrans(&resarg->trans);
1154         isc_event_free(&event);
1155
1156         if (!resarg->canceled) {
1157                 UNLOCK(&resarg->lock);
1158
1159                 /*
1160                  * We may or may not be running.  isc__appctx_onrun will
1161                  * fail if we are currently running otherwise we post a
1162                  * action to call isc_app_ctxsuspend when we do start
1163                  * running.
1164                  */
1165                 result = isc_app_ctxonrun(resarg->actx, resarg->client->mctx,
1166                                            task, suspend, resarg->actx);
1167                 if (result == ISC_R_ALREADYRUNNING)
1168                         isc_app_ctxsuspend(resarg->actx);
1169         } else {
1170                 /*
1171                  * We have already exited from the loop (due to some
1172                  * unexpected event).  Just clean the arg up.
1173                  */
1174                 UNLOCK(&resarg->lock);
1175                 DESTROYLOCK(&resarg->lock);
1176                 isc_mem_put(resarg->client->mctx, resarg, sizeof(*resarg));
1177         }
1178 }
1179
1180 isc_result_t
1181 dns_client_resolve(dns_client_t *client, dns_name_t *name,
1182                    dns_rdataclass_t rdclass, dns_rdatatype_t type,
1183                    unsigned int options, dns_namelist_t *namelist)
1184 {
1185         isc_result_t result;
1186         isc_appctx_t *actx;
1187         resarg_t *resarg;
1188
1189         REQUIRE(DNS_CLIENT_VALID(client));
1190         REQUIRE(namelist != NULL && ISC_LIST_EMPTY(*namelist));
1191
1192         if ((client->attributes & DNS_CLIENTATTR_OWNCTX) == 0 &&
1193             (options & DNS_CLIENTRESOPT_ALLOWRUN) == 0) {
1194                 /*
1195                  * If the client is run under application's control, we need
1196                  * to create a new running (sub)environment for this
1197                  * particular resolution.
1198                  */
1199                 return (ISC_R_NOTIMPLEMENTED); /* XXXTBD */
1200         } else
1201                 actx = client->actx;
1202
1203         resarg = isc_mem_get(client->mctx, sizeof(*resarg));
1204         if (resarg == NULL)
1205                 return (ISC_R_NOMEMORY);
1206
1207         result = isc_mutex_init(&resarg->lock);
1208         if (result != ISC_R_SUCCESS) {
1209                 isc_mem_put(client->mctx, resarg, sizeof(*resarg));
1210                 return (result);
1211         }
1212
1213         resarg->actx = actx;
1214         resarg->client = client;
1215         resarg->result = DNS_R_SERVFAIL;
1216         resarg->namelist = namelist;
1217         resarg->trans = NULL;
1218         resarg->canceled = ISC_FALSE;
1219         result = dns_client_startresolve(client, name, rdclass, type, options,
1220                                          client->task, resolve_done, resarg,
1221                                          &resarg->trans);
1222         if (result != ISC_R_SUCCESS) {
1223                 DESTROYLOCK(&resarg->lock);
1224                 isc_mem_put(client->mctx, resarg, sizeof(*resarg));
1225                 return (result);
1226         }
1227
1228         /*
1229          * Start internal event loop.  It blocks until the entire process
1230          * is completed.
1231          */
1232         result = isc_app_ctxrun(actx);
1233
1234         LOCK(&resarg->lock);
1235         if (result == ISC_R_SUCCESS || result == ISC_R_SUSPEND)
1236                 result = resarg->result;
1237         if (result != ISC_R_SUCCESS && resarg->vresult != ISC_R_SUCCESS) {
1238                 /*
1239                  * If this lookup failed due to some error in DNSSEC
1240                  * validation, return the validation error code.
1241                  * XXX: or should we pass the validation result separately?
1242                  */
1243                 result = resarg->vresult;
1244         }
1245         if (resarg->trans != NULL) {
1246                 /*
1247                  * Unusual termination (perhaps due to signal).  We need some
1248                  * tricky cleanup process.
1249                  */
1250                 resarg->canceled = ISC_TRUE;
1251                 dns_client_cancelresolve(resarg->trans);
1252
1253                 UNLOCK(&resarg->lock);
1254
1255                 /* resarg will be freed in the event handler. */
1256         } else {
1257                 UNLOCK(&resarg->lock);
1258
1259                 DESTROYLOCK(&resarg->lock);
1260                 isc_mem_put(client->mctx, resarg, sizeof(*resarg));
1261         }
1262
1263         return (result);
1264 }
1265
1266 isc_result_t
1267 dns_client_startresolve(dns_client_t *client, dns_name_t *name,
1268                         dns_rdataclass_t rdclass, dns_rdatatype_t type,
1269                         unsigned int options, isc_task_t *task,
1270                         isc_taskaction_t action, void *arg,
1271                         dns_clientrestrans_t **transp)
1272 {
1273         dns_view_t *view = NULL;
1274         dns_clientresevent_t *event = NULL;
1275         resctx_t *rctx = NULL;
1276         isc_task_t *clone = NULL;
1277         isc_mem_t *mctx;
1278         isc_result_t result;
1279         dns_rdataset_t *rdataset, *sigrdataset;
1280         isc_boolean_t want_dnssec;
1281
1282         REQUIRE(DNS_CLIENT_VALID(client));
1283         REQUIRE(transp != NULL && *transp == NULL);
1284
1285         LOCK(&client->lock);
1286         result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
1287                                    rdclass, &view);
1288         UNLOCK(&client->lock);
1289         if (result != ISC_R_SUCCESS)
1290                 return (result);
1291
1292         mctx = client->mctx;
1293         rdataset = NULL;
1294         sigrdataset = NULL;
1295         want_dnssec = ISC_TF((options & DNS_CLIENTRESOPT_NODNSSEC) == 0);
1296
1297         /*
1298          * Prepare some intermediate resources
1299          */
1300         clone = NULL;
1301         isc_task_attach(task, &clone);
1302         event = (dns_clientresevent_t *)
1303                 isc_event_allocate(mctx, clone, DNS_EVENT_CLIENTRESDONE,
1304                                    action, arg, sizeof(*event));
1305         if (event == NULL) {
1306                 result = ISC_R_NOMEMORY;
1307                 goto cleanup;
1308         }
1309         event->result = DNS_R_SERVFAIL;
1310         ISC_LIST_INIT(event->answerlist);
1311
1312         rctx = isc_mem_get(mctx, sizeof(*rctx));
1313         if (rctx == NULL)
1314                 result = ISC_R_NOMEMORY;
1315         else {
1316                 result = isc_mutex_init(&rctx->lock);
1317                 if (result != ISC_R_SUCCESS) {
1318                         isc_mem_put(mctx, rctx, sizeof(*rctx));
1319                         rctx = NULL;
1320                 }
1321         }
1322         if (result != ISC_R_SUCCESS)
1323                 goto cleanup;
1324
1325         result = getrdataset(mctx, &rdataset);
1326         if (result != ISC_R_SUCCESS)
1327                 goto cleanup;
1328         rctx->rdataset = rdataset;
1329
1330         if (want_dnssec) {
1331                 result = getrdataset(mctx, &sigrdataset);
1332                 if (result != ISC_R_SUCCESS)
1333                         goto cleanup;
1334         }
1335         rctx->sigrdataset = sigrdataset;
1336
1337         dns_fixedname_init(&rctx->name);
1338         result = dns_name_copy(name, dns_fixedname_name(&rctx->name), NULL);
1339         if (result != ISC_R_SUCCESS)
1340                 goto cleanup;
1341
1342         rctx->client = client;
1343         ISC_LINK_INIT(rctx, link);
1344         rctx->canceled = ISC_FALSE;
1345         rctx->task = client->task;
1346         rctx->type = type;
1347         rctx->view = view;
1348         rctx->restarts = 0;
1349         rctx->fetch = NULL;
1350         rctx->want_dnssec = want_dnssec;
1351         ISC_LIST_INIT(rctx->namelist);
1352         rctx->event = event;
1353
1354         rctx->magic = RCTX_MAGIC;
1355
1356         LOCK(&client->lock);
1357         ISC_LIST_APPEND(client->resctxs, rctx, link);
1358         UNLOCK(&client->lock);
1359
1360         *transp = (dns_clientrestrans_t *)rctx;
1361         client_resfind(rctx, NULL);
1362
1363         return (ISC_R_SUCCESS);
1364
1365  cleanup:
1366         if (rdataset != NULL)
1367                 putrdataset(client->mctx, &rdataset);
1368         if (sigrdataset != NULL)
1369                 putrdataset(client->mctx, &sigrdataset);
1370         if (rctx != NULL) {
1371                 DESTROYLOCK(&rctx->lock);
1372                 isc_mem_put(mctx, rctx, sizeof(*rctx));
1373         }
1374         if (event != NULL)
1375                 isc_event_free(ISC_EVENT_PTR(&event));
1376         isc_task_detach(&clone);
1377         dns_view_detach(&view);
1378
1379         return (result);
1380 }
1381
1382 void
1383 dns_client_cancelresolve(dns_clientrestrans_t *trans) {
1384         resctx_t *rctx;
1385
1386         REQUIRE(trans != NULL);
1387         rctx = (resctx_t *)trans;
1388         REQUIRE(RCTX_VALID(rctx));
1389
1390         LOCK(&rctx->lock);
1391
1392         if (!rctx->canceled) {
1393                 rctx->canceled = ISC_TRUE;
1394                 if (rctx->fetch != NULL)
1395                         dns_resolver_cancelfetch(rctx->fetch);
1396         }
1397
1398         UNLOCK(&rctx->lock);
1399 }
1400
1401 void
1402 dns_client_freeresanswer(dns_client_t *client, dns_namelist_t *namelist) {
1403         dns_name_t *name;
1404         dns_rdataset_t *rdataset;
1405
1406         REQUIRE(DNS_CLIENT_VALID(client));
1407         REQUIRE(namelist != NULL);
1408
1409         while ((name = ISC_LIST_HEAD(*namelist)) != NULL) {
1410                 ISC_LIST_UNLINK(*namelist, name, link);
1411                 while ((rdataset = ISC_LIST_HEAD(name->list)) != NULL) {
1412                         ISC_LIST_UNLINK(name->list, rdataset, link);
1413                         putrdataset(client->mctx, &rdataset);
1414                 }
1415                 dns_name_free(name, client->mctx);
1416                 isc_mem_put(client->mctx, name, sizeof(*name));
1417         }
1418 }
1419
1420 void
1421 dns_client_destroyrestrans(dns_clientrestrans_t **transp) {
1422         resctx_t *rctx;
1423         isc_mem_t *mctx;
1424         dns_client_t *client;
1425         isc_boolean_t need_destroyclient = ISC_FALSE;
1426
1427         REQUIRE(transp != NULL);
1428         rctx = (resctx_t *)*transp;
1429         REQUIRE(RCTX_VALID(rctx));
1430         REQUIRE(rctx->fetch == NULL);
1431         REQUIRE(rctx->event == NULL);
1432         client = rctx->client;
1433         REQUIRE(DNS_CLIENT_VALID(client));
1434
1435         mctx = client->mctx;
1436         dns_view_detach(&rctx->view);
1437
1438         LOCK(&client->lock);
1439
1440         INSIST(ISC_LINK_LINKED(rctx, link));
1441         ISC_LIST_UNLINK(client->resctxs, rctx, link);
1442
1443         if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
1444             ISC_LIST_EMPTY(client->reqctxs) &&
1445             ISC_LIST_EMPTY(client->updatectxs))
1446                 need_destroyclient = ISC_TRUE;
1447
1448         UNLOCK(&client->lock);
1449
1450         INSIST(ISC_LIST_EMPTY(rctx->namelist));
1451
1452         DESTROYLOCK(&rctx->lock);
1453         rctx->magic = 0;
1454
1455         isc_mem_put(mctx, rctx, sizeof(*rctx));
1456
1457         if (need_destroyclient)
1458                 destroyclient(&client);
1459
1460         *transp = NULL;
1461 }
1462
1463 isc_result_t
1464 dns_client_addtrustedkey(dns_client_t *client, dns_rdataclass_t rdclass,
1465                          dns_name_t *keyname, isc_buffer_t *keydatabuf)
1466 {
1467         isc_result_t result;
1468         dns_view_t *view = NULL;
1469         dst_key_t *dstkey = NULL;
1470         dns_keytable_t *secroots = NULL;
1471
1472         REQUIRE(DNS_CLIENT_VALID(client));
1473
1474         LOCK(&client->lock);
1475         result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
1476                                    rdclass, &view);
1477         UNLOCK(&client->lock);
1478         if (result != ISC_R_SUCCESS)
1479                 goto cleanup;
1480
1481         result = dns_view_getsecroots(view, &secroots);
1482         if (result != ISC_R_SUCCESS)
1483                 goto cleanup;
1484
1485         result = dst_key_fromdns(keyname, rdclass, keydatabuf, client->mctx,
1486                                  &dstkey);
1487         if (result != ISC_R_SUCCESS)
1488                 goto cleanup;
1489
1490         result = dns_keytable_add(secroots, ISC_FALSE, &dstkey);
1491
1492  cleanup:
1493         if (dstkey != NULL)
1494                 dst_key_free(&dstkey);
1495         if (view != NULL)
1496                 dns_view_detach(&view);
1497         if (secroots != NULL)
1498                 dns_keytable_detach(&secroots);
1499         return (result);
1500 }
1501
1502 /*%
1503  * Simple request routines
1504  */
1505 static void
1506 request_done(isc_task_t *task, isc_event_t *event) {
1507         dns_requestevent_t *reqev = NULL;
1508         dns_request_t *request;
1509         isc_result_t result, eresult;
1510         reqctx_t *ctx;
1511
1512         UNUSED(task);
1513
1514         REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
1515         reqev = (dns_requestevent_t *)event;
1516         request = reqev->request;
1517         result = eresult = reqev->result;
1518         ctx = reqev->ev_arg;
1519         REQUIRE(REQCTX_VALID(ctx));
1520
1521         isc_event_free(&event);
1522
1523         LOCK(&ctx->lock);
1524
1525         if (eresult == ISC_R_SUCCESS) {
1526                 result = dns_request_getresponse(request, ctx->event->rmessage,
1527                                                  ctx->parseoptions);
1528         }
1529
1530         if (ctx->tsigkey != NULL)
1531                 dns_tsigkey_detach(&ctx->tsigkey);
1532
1533         if (ctx->canceled)
1534                 ctx->event->result = ISC_R_CANCELED;
1535         else
1536                 ctx->event->result = result;
1537         task = ctx->event->ev_sender;
1538         ctx->event->ev_sender = ctx;
1539         isc_task_sendanddetach(&task, ISC_EVENT_PTR(&ctx->event));
1540
1541         UNLOCK(&ctx->lock);
1542 }
1543
1544 static void
1545 localrequest_done(isc_task_t *task, isc_event_t *event) {
1546         reqarg_t *reqarg = event->ev_arg;
1547         dns_clientreqevent_t *rev =(dns_clientreqevent_t *)event;
1548
1549         UNUSED(task);
1550
1551         REQUIRE(event->ev_type == DNS_EVENT_CLIENTREQDONE);
1552
1553         LOCK(&reqarg->lock);
1554
1555         reqarg->result = rev->result;
1556         dns_client_destroyreqtrans(&reqarg->trans);
1557         isc_event_free(&event);
1558
1559         if (!reqarg->canceled) {
1560                 UNLOCK(&reqarg->lock);
1561
1562                 /* Exit from the internal event loop */
1563                 isc_app_ctxsuspend(reqarg->actx);
1564         } else {
1565                 /*
1566                  * We have already exited from the loop (due to some
1567                  * unexpected event).  Just clean the arg up.
1568                  */
1569                 UNLOCK(&reqarg->lock);
1570                 DESTROYLOCK(&reqarg->lock);
1571                 isc_mem_put(reqarg->client->mctx, reqarg, sizeof(*reqarg));
1572         }
1573 }
1574
1575 isc_result_t
1576 dns_client_request(dns_client_t *client, dns_message_t *qmessage,
1577                    dns_message_t *rmessage, isc_sockaddr_t *server,
1578                    unsigned int options, unsigned int parseoptions,
1579                    dns_tsec_t *tsec, unsigned int timeout,
1580                    unsigned int udptimeout, unsigned int udpretries)
1581 {
1582         isc_appctx_t *actx;
1583         reqarg_t *reqarg;
1584         isc_result_t result;
1585
1586         REQUIRE(DNS_CLIENT_VALID(client));
1587         REQUIRE(qmessage != NULL);
1588         REQUIRE(rmessage != NULL);
1589
1590         if ((client->attributes & DNS_CLIENTATTR_OWNCTX) == 0 &&
1591             (options & DNS_CLIENTREQOPT_ALLOWRUN) == 0) {
1592                 /*
1593                  * If the client is run under application's control, we need
1594                  * to create a new running (sub)environment for this
1595                  * particular resolution.
1596                  */
1597                 return (ISC_R_NOTIMPLEMENTED); /* XXXTBD */
1598         } else
1599                 actx = client->actx;
1600
1601         reqarg = isc_mem_get(client->mctx, sizeof(*reqarg));
1602         if (reqarg == NULL)
1603                 return (ISC_R_NOMEMORY);
1604
1605         result = isc_mutex_init(&reqarg->lock);
1606         if (result != ISC_R_SUCCESS) {
1607                 isc_mem_put(client->mctx, reqarg, sizeof(*reqarg));
1608                 return (result);
1609         }
1610
1611         reqarg->actx = actx;
1612         reqarg->client = client;
1613         reqarg->trans = NULL;
1614         reqarg->canceled = ISC_FALSE;
1615
1616         result = dns_client_startrequest(client, qmessage, rmessage, server,
1617                                          options, parseoptions, tsec, timeout,
1618                                          udptimeout, udpretries,
1619                                          client->task, localrequest_done,
1620                                          reqarg, &reqarg->trans);
1621         if (result != ISC_R_SUCCESS) {
1622                 DESTROYLOCK(&reqarg->lock);
1623                 isc_mem_put(client->mctx, reqarg, sizeof(*reqarg));
1624                 return (result);
1625         }
1626
1627         /*
1628          * Start internal event loop.  It blocks until the entire process
1629          * is completed.
1630          */
1631         result = isc_app_ctxrun(actx);
1632
1633         LOCK(&reqarg->lock);
1634         if (result == ISC_R_SUCCESS || result == ISC_R_SUSPEND)
1635                 result = reqarg->result;
1636         if (reqarg->trans != NULL) {
1637                 /*
1638                  * Unusual termination (perhaps due to signal).  We need some
1639                  * tricky cleanup process.
1640                  */
1641                 reqarg->canceled = ISC_TRUE;
1642                 dns_client_cancelresolve(reqarg->trans);
1643
1644                 UNLOCK(&reqarg->lock);
1645
1646                 /* reqarg will be freed in the event handler. */
1647         } else {
1648                 UNLOCK(&reqarg->lock);
1649
1650                 DESTROYLOCK(&reqarg->lock);
1651                 isc_mem_put(client->mctx, reqarg, sizeof(*reqarg));
1652         }
1653
1654         return (result);
1655 }
1656
1657 isc_result_t
1658 dns_client_startrequest(dns_client_t *client, dns_message_t *qmessage,
1659                         dns_message_t *rmessage, isc_sockaddr_t *server,
1660                         unsigned int options, unsigned int parseoptions,
1661                         dns_tsec_t *tsec, unsigned int timeout,
1662                         unsigned int udptimeout, unsigned int udpretries,
1663                         isc_task_t *task, isc_taskaction_t action, void *arg,
1664                         dns_clientreqtrans_t **transp)
1665 {
1666         isc_result_t result;
1667         dns_view_t *view = NULL;
1668         isc_task_t *clone = NULL;
1669         dns_clientreqevent_t *event = NULL;
1670         reqctx_t *ctx = NULL;
1671         dns_tsectype_t tsectype = dns_tsectype_none;
1672
1673         UNUSED(options);
1674
1675         REQUIRE(DNS_CLIENT_VALID(client));
1676         REQUIRE(qmessage != NULL);
1677         REQUIRE(rmessage != NULL);
1678         REQUIRE(transp != NULL && *transp == NULL);
1679
1680         if (tsec != NULL) {
1681                 tsectype = dns_tsec_gettype(tsec);
1682                 if (tsectype != dns_tsectype_tsig)
1683                         return (ISC_R_NOTIMPLEMENTED); /* XXX */
1684         }
1685
1686         LOCK(&client->lock);
1687         result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
1688                                    qmessage->rdclass, &view);
1689         UNLOCK(&client->lock);
1690         if (result != ISC_R_SUCCESS)
1691                 return (result);
1692
1693         clone = NULL;
1694         isc_task_attach(task, &clone);
1695         event = (dns_clientreqevent_t *)
1696                 isc_event_allocate(client->mctx, clone,
1697                                    DNS_EVENT_CLIENTREQDONE,
1698                                    action, arg, sizeof(*event));
1699         if (event == NULL) {
1700                 result = ISC_R_NOMEMORY;
1701                 goto cleanup;
1702         }
1703
1704         ctx = isc_mem_get(client->mctx, sizeof(*ctx));
1705         if (ctx == NULL)
1706                 result = ISC_R_NOMEMORY;
1707         else {
1708                 result = isc_mutex_init(&ctx->lock);
1709                 if (result != ISC_R_SUCCESS) {
1710                         isc_mem_put(client->mctx, ctx, sizeof(*ctx));
1711                         ctx = NULL;
1712                 }
1713         }
1714         if (result != ISC_R_SUCCESS)
1715                 goto cleanup;
1716
1717         ctx->client = client;
1718         ISC_LINK_INIT(ctx, link);
1719         ctx->parseoptions = parseoptions;
1720         ctx->canceled = ISC_FALSE;
1721         ctx->event = event;
1722         ctx->event->rmessage = rmessage;
1723         ctx->tsigkey = NULL;
1724         if (tsec != NULL)
1725                 dns_tsec_getkey(tsec, &ctx->tsigkey);
1726
1727         ctx->magic = REQCTX_MAGIC;
1728
1729         LOCK(&client->lock);
1730         ISC_LIST_APPEND(client->reqctxs, ctx, link);
1731         UNLOCK(&client->lock);
1732
1733         ctx->request = NULL;
1734         result = dns_request_createvia3(view->requestmgr, qmessage, NULL,
1735                                         server, options, ctx->tsigkey,
1736                                         timeout, udptimeout, udpretries,
1737                                         client->task, request_done, ctx,
1738                                         &ctx->request);
1739         if (result == ISC_R_SUCCESS) {
1740                 dns_view_detach(&view);
1741                 *transp = (dns_clientreqtrans_t *)ctx;
1742                 return (ISC_R_SUCCESS);
1743         }
1744
1745  cleanup:
1746         if (ctx != NULL) {
1747                 LOCK(&client->lock);
1748                 ISC_LIST_UNLINK(client->reqctxs, ctx, link);
1749                 UNLOCK(&client->lock);
1750                 DESTROYLOCK(&ctx->lock);
1751                 isc_mem_put(client->mctx, ctx, sizeof(*ctx));
1752         }
1753         if (event != NULL)
1754                 isc_event_free(ISC_EVENT_PTR(&event));
1755         isc_task_detach(&clone);
1756         dns_view_detach(&view);
1757
1758         return (result);
1759 }
1760
1761 void
1762 dns_client_cancelrequest(dns_clientreqtrans_t *trans) {
1763         reqctx_t *ctx;
1764
1765         REQUIRE(trans != NULL);
1766         ctx = (reqctx_t *)trans;
1767         REQUIRE(REQCTX_VALID(ctx));
1768
1769         LOCK(&ctx->lock);
1770
1771         if (!ctx->canceled) {
1772                 ctx->canceled = ISC_TRUE;
1773                 if (ctx->request != NULL)
1774                         dns_request_cancel(ctx->request);
1775         }
1776
1777         UNLOCK(&ctx->lock);
1778 }
1779
1780 void
1781 dns_client_destroyreqtrans(dns_clientreqtrans_t **transp) {
1782         reqctx_t *ctx;
1783         isc_mem_t *mctx;
1784         dns_client_t *client;
1785         isc_boolean_t need_destroyclient = ISC_FALSE;
1786
1787         REQUIRE(transp != NULL);
1788         ctx = (reqctx_t *)*transp;
1789         REQUIRE(REQCTX_VALID(ctx));
1790         client = ctx->client;
1791         REQUIRE(DNS_CLIENT_VALID(client));
1792         REQUIRE(ctx->event == NULL);
1793         REQUIRE(ctx->request != NULL);
1794
1795         dns_request_destroy(&ctx->request);
1796         mctx = client->mctx;
1797
1798         LOCK(&client->lock);
1799
1800         INSIST(ISC_LINK_LINKED(ctx, link));
1801         ISC_LIST_UNLINK(client->reqctxs, ctx, link);
1802
1803         if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
1804             ISC_LIST_EMPTY(client->reqctxs) &&
1805             ISC_LIST_EMPTY(client->updatectxs)) {
1806                 need_destroyclient = ISC_TRUE;
1807         }
1808
1809         UNLOCK(&client->lock);
1810
1811         DESTROYLOCK(&ctx->lock);
1812         ctx->magic = 0;
1813
1814         isc_mem_put(mctx, ctx, sizeof(*ctx));
1815
1816         if (need_destroyclient)
1817                 destroyclient(&client);
1818
1819         *transp = NULL;
1820 }
1821
1822 /*%
1823  * Dynamic update routines
1824  */
1825 static isc_result_t
1826 rcode2result(dns_rcode_t rcode) {
1827         /* XXX: isn't there a similar function? */
1828         switch (rcode) {
1829         case dns_rcode_formerr:
1830                 return (DNS_R_FORMERR);
1831         case dns_rcode_servfail:
1832                 return (DNS_R_SERVFAIL);
1833         case dns_rcode_nxdomain:
1834                 return (DNS_R_NXDOMAIN);
1835         case dns_rcode_notimp:
1836                 return (DNS_R_NOTIMP);
1837         case dns_rcode_refused:
1838                 return (DNS_R_REFUSED);
1839         case dns_rcode_yxdomain:
1840                 return (DNS_R_YXDOMAIN);
1841         case dns_rcode_yxrrset:
1842                 return (DNS_R_YXRRSET);
1843         case dns_rcode_nxrrset:
1844                 return (DNS_R_NXRRSET);
1845         case dns_rcode_notauth:
1846                 return (DNS_R_NOTAUTH);
1847         case dns_rcode_notzone:
1848                 return (DNS_R_NOTZONE);
1849         case dns_rcode_badvers:
1850                 return (DNS_R_BADVERS);
1851         }
1852
1853         return (ISC_R_FAILURE);
1854 }
1855
1856 static void
1857 update_sendevent(updatectx_t *uctx, isc_result_t result) {
1858         isc_task_t *task;
1859
1860         dns_message_destroy(&uctx->updatemsg);
1861         if (uctx->tsigkey != NULL)
1862                 dns_tsigkey_detach(&uctx->tsigkey);
1863         if (uctx->sig0key != NULL)
1864                 dst_key_free(&uctx->sig0key);
1865
1866         if (uctx->canceled)
1867                 uctx->event->result = ISC_R_CANCELED;
1868         else
1869                 uctx->event->result = result;
1870         uctx->event->state = uctx->state;
1871         task = uctx->event->ev_sender;
1872         uctx->event->ev_sender = uctx;
1873         isc_task_sendanddetach(&task, ISC_EVENT_PTR(&uctx->event));
1874 }
1875
1876 static void
1877 update_done(isc_task_t *task, isc_event_t *event) {
1878         isc_result_t result;
1879         dns_requestevent_t *reqev = NULL;
1880         dns_request_t *request;
1881         dns_message_t *answer = NULL;
1882         updatectx_t *uctx = event->ev_arg;
1883         dns_client_t *client;
1884         unsigned int timeout;
1885
1886         UNUSED(task);
1887
1888         REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
1889         reqev = (dns_requestevent_t *)event;
1890         request = reqev->request;
1891         REQUIRE(UCTX_VALID(uctx));
1892         client = uctx->client;
1893         REQUIRE(DNS_CLIENT_VALID(client));
1894
1895         result = reqev->result;
1896         if (result != ISC_R_SUCCESS)
1897                 goto out;
1898
1899         result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE,
1900                                     &answer);
1901         if (result != ISC_R_SUCCESS)
1902                 goto out;
1903         uctx->state = dns_clientupdatestate_done;
1904         result = dns_request_getresponse(request, answer,
1905                                          DNS_MESSAGEPARSE_PRESERVEORDER);
1906         if (result == ISC_R_SUCCESS && answer->rcode != dns_rcode_noerror)
1907                 result = rcode2result(answer->rcode);
1908
1909  out:
1910         if (answer != NULL)
1911                 dns_message_destroy(&answer);
1912         isc_event_free(&event);
1913
1914         LOCK(&uctx->lock);
1915         uctx->currentserver = ISC_LIST_NEXT(uctx->currentserver, link);
1916         dns_request_destroy(&uctx->updatereq);
1917         if (result != ISC_R_SUCCESS && !uctx->canceled &&
1918             uctx->currentserver != NULL) {
1919                 dns_message_renderreset(uctx->updatemsg);
1920                 dns_message_settsigkey(uctx->updatemsg, NULL);
1921
1922                 timeout = client->update_timeout / uctx->nservers;
1923                 if (timeout < MIN_UPDATE_TIMEOUT)
1924                         timeout = MIN_UPDATE_TIMEOUT;
1925                 result = dns_request_createvia3(uctx->view->requestmgr,
1926                                                 uctx->updatemsg,
1927                                                 NULL,
1928                                                 uctx->currentserver, 0,
1929                                                 uctx->tsigkey,
1930                                                 timeout,
1931                                                 client->update_udptimeout,
1932                                                 client->update_udpretries,
1933                                                 client->task,
1934                                                 update_done, uctx,
1935                                                 &uctx->updatereq);
1936                 UNLOCK(&uctx->lock);
1937
1938                 if (result == ISC_R_SUCCESS) {
1939                         /* XXX: should we keep the 'done' state here? */
1940                         uctx->state = dns_clientupdatestate_sent;
1941                         return;
1942                 }
1943         } else
1944                 UNLOCK(&uctx->lock);
1945
1946         update_sendevent(uctx, result);
1947 }
1948
1949 static isc_result_t
1950 send_update(updatectx_t *uctx) {
1951         isc_result_t result;
1952         dns_name_t *name = NULL;
1953         dns_rdataset_t *rdataset = NULL;
1954         dns_client_t *client = uctx->client;
1955         unsigned int timeout;
1956
1957         REQUIRE(uctx->zonename != NULL && uctx->currentserver != NULL);
1958
1959         result = dns_message_gettempname(uctx->updatemsg, &name);
1960         if (result != ISC_R_SUCCESS)
1961                 return (result);
1962         dns_name_init(name, NULL);
1963         dns_name_clone(uctx->zonename, name);
1964         result = dns_message_gettemprdataset(uctx->updatemsg, &rdataset);
1965         if (result != ISC_R_SUCCESS) {
1966                 dns_message_puttempname(uctx->updatemsg, &name);
1967                 return (result);
1968         }
1969         dns_rdataset_makequestion(rdataset, uctx->rdclass, dns_rdatatype_soa);
1970         ISC_LIST_INIT(name->list);
1971         ISC_LIST_APPEND(name->list, rdataset, link);
1972         dns_message_addname(uctx->updatemsg, name, DNS_SECTION_ZONE);
1973         if (uctx->tsigkey == NULL && uctx->sig0key != NULL) {
1974                 result = dns_message_setsig0key(uctx->updatemsg,
1975                                                 uctx->sig0key);
1976                 if (result != ISC_R_SUCCESS)
1977                         return (result);
1978         }
1979         timeout = client->update_timeout / uctx->nservers;
1980         if (timeout < MIN_UPDATE_TIMEOUT)
1981                 timeout = MIN_UPDATE_TIMEOUT;
1982         result = dns_request_createvia3(uctx->view->requestmgr,
1983                                         uctx->updatemsg,
1984                                         NULL, uctx->currentserver, 0,
1985                                         uctx->tsigkey, timeout,
1986                                         client->update_udptimeout,
1987                                         client->update_udpretries,
1988                                         client->task, update_done, uctx,
1989                                         &uctx->updatereq);
1990         if (result == ISC_R_SUCCESS &&
1991             uctx->state == dns_clientupdatestate_prepare) {
1992                 uctx->state = dns_clientupdatestate_sent;
1993         }
1994
1995         return (result);
1996 }
1997
1998 static void
1999 resolveaddr_done(isc_task_t *task, isc_event_t *event) {
2000         isc_result_t result;
2001         int family;
2002         dns_rdatatype_t qtype;
2003         dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
2004         dns_name_t *name;
2005         dns_rdataset_t *rdataset;
2006         updatectx_t *uctx;
2007         isc_boolean_t completed = ISC_FALSE;
2008
2009         UNUSED(task);
2010
2011         REQUIRE(event->ev_arg != NULL);
2012         uctx = *(updatectx_t **)event->ev_arg;
2013         REQUIRE(UCTX_VALID(uctx));
2014
2015         if (event->ev_arg == &uctx->bp4) {
2016                 family = AF_INET;
2017                 qtype = dns_rdatatype_a;
2018                 LOCK(&uctx->lock);
2019                 dns_client_destroyrestrans(&uctx->restrans);
2020                 UNLOCK(&uctx->lock);
2021         } else {
2022                 INSIST(event->ev_arg == &uctx->bp6);
2023                 family = AF_INET6;
2024                 qtype = dns_rdatatype_aaaa;
2025                 LOCK(&uctx->lock);
2026                 dns_client_destroyrestrans(&uctx->restrans2);
2027                 UNLOCK(&uctx->lock);
2028         }
2029
2030         result = rev->result;
2031         if (result != ISC_R_SUCCESS)
2032                 goto done;
2033
2034         for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL;
2035              name = ISC_LIST_NEXT(name, link)) {
2036                 for (rdataset = ISC_LIST_HEAD(name->list);
2037                      rdataset != NULL;
2038                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
2039                         if (!dns_rdataset_isassociated(rdataset))
2040                                 continue;
2041                         if (rdataset->type != qtype)
2042                                 continue;
2043
2044                         for (result = dns_rdataset_first(rdataset);
2045                              result == ISC_R_SUCCESS;
2046                              result = dns_rdataset_next(rdataset)) {
2047                                 dns_rdata_t rdata;
2048                                 dns_rdata_in_a_t rdata_a;
2049                                 dns_rdata_in_aaaa_t rdata_aaaa;
2050                                 isc_sockaddr_t *sa;
2051
2052                                 sa = isc_mem_get(uctx->client->mctx,
2053                                                  sizeof(*sa));
2054                                 if (sa == NULL) {
2055                                         /*
2056                                          * If we fail to get a sockaddr,
2057                                          we simply move forward with the
2058                                          * addresses we've got so far.
2059                                          */
2060                                         goto done;
2061                                 }
2062
2063                                 dns_rdata_init(&rdata);
2064                                 switch (family) {
2065                                 case AF_INET:
2066                                         dns_rdataset_current(rdataset, &rdata);
2067                                         result = dns_rdata_tostruct(&rdata, &rdata_a,
2068                                                                     NULL);
2069                                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
2070                                         isc_sockaddr_fromin(sa,
2071                                                             &rdata_a.in_addr,
2072                                                             53);
2073                                         dns_rdata_freestruct(&rdata_a);
2074                                         break;
2075                                 case AF_INET6:
2076                                         dns_rdataset_current(rdataset, &rdata);
2077                                         result = dns_rdata_tostruct(&rdata, &rdata_aaaa,
2078                                                                     NULL);
2079                                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
2080                                         isc_sockaddr_fromin6(sa,
2081                                                              &rdata_aaaa.in6_addr,
2082                                                              53);
2083                                         dns_rdata_freestruct(&rdata_aaaa);
2084                                         break;
2085                                 }
2086
2087                                 ISC_LINK_INIT(sa, link);
2088                                 ISC_LIST_APPEND(uctx->servers, sa, link);
2089                                 uctx->nservers++;
2090                         }
2091                 }
2092         }
2093
2094  done:
2095         dns_client_freeresanswer(uctx->client, &rev->answerlist);
2096         isc_event_free(&event);
2097
2098         LOCK(&uctx->lock);
2099         if (uctx->restrans == NULL && uctx->restrans2 == NULL)
2100                 completed = ISC_TRUE;
2101         UNLOCK(&uctx->lock);
2102
2103         if (completed) {
2104                 INSIST(uctx->currentserver == NULL);
2105                 uctx->currentserver = ISC_LIST_HEAD(uctx->servers);
2106                 if (uctx->currentserver != NULL && !uctx->canceled)
2107                         send_update(uctx);
2108                 else {
2109                         if (result == ISC_R_SUCCESS)
2110                                 result = ISC_R_NOTFOUND;
2111                         update_sendevent(uctx, result);
2112                 }
2113         }
2114 }
2115
2116 static isc_result_t
2117 process_soa(updatectx_t *uctx, dns_rdataset_t *soaset, dns_name_t *soaname) {
2118         isc_result_t result;
2119         dns_rdata_t soarr = DNS_RDATA_INIT;
2120         dns_rdata_soa_t soa;
2121         dns_name_t primary;
2122
2123         result = dns_rdataset_first(soaset);
2124         if (result != ISC_R_SUCCESS)
2125                 return (result);
2126         dns_rdata_init(&soarr);
2127         dns_rdataset_current(soaset, &soarr);
2128         result = dns_rdata_tostruct(&soarr, &soa, NULL);
2129         if (result != ISC_R_SUCCESS)
2130                 return (result);
2131
2132         dns_name_init(&primary, NULL);
2133         dns_name_clone(&soa.origin, &primary);
2134
2135         if (uctx->zonename == NULL) {
2136                 uctx->zonename = dns_fixedname_name(&uctx->zonefname);
2137                 result = dns_name_copy(soaname, uctx->zonename, NULL);
2138                 if (result != ISC_R_SUCCESS)
2139                         goto out;
2140         }
2141
2142         if (uctx->currentserver != NULL)
2143                 result = send_update(uctx);
2144         else {
2145                 /*
2146                  * Get addresses of the primary server.  We don't use the ADB
2147                  * feature so that we could avoid caching data.
2148                  */
2149                 LOCK(&uctx->lock);
2150                 uctx->bp4 = uctx;
2151                 result = dns_client_startresolve(uctx->client, &primary,
2152                                                  uctx->rdclass,
2153                                                  dns_rdatatype_a,
2154                                                  0, uctx->client->task,
2155                                                  resolveaddr_done, &uctx->bp4,
2156                                                  &uctx->restrans);
2157                 if (result == ISC_R_SUCCESS) {
2158                         uctx->bp6 = uctx;
2159                         result = dns_client_startresolve(uctx->client,
2160                                                          &primary,
2161                                                          uctx->rdclass,
2162                                                          dns_rdatatype_aaaa,
2163                                                          0, uctx->client->task,
2164                                                          resolveaddr_done,
2165                                                          &uctx->bp6,
2166                                                          &uctx->restrans2);
2167                 }
2168                 UNLOCK(&uctx->lock);
2169         }
2170
2171  out:
2172         dns_rdata_freestruct(&soa);
2173
2174         return (result);
2175 }
2176
2177 static void
2178 receive_soa(isc_task_t *task, isc_event_t *event) {
2179         dns_requestevent_t *reqev = NULL;
2180         updatectx_t *uctx;
2181         dns_client_t *client;
2182         isc_result_t result, eresult;
2183         dns_request_t *request;
2184         dns_message_t *rcvmsg = NULL;
2185         dns_section_t section;
2186         dns_rdataset_t *soaset = NULL;
2187         int pass = 0;
2188         dns_name_t *name;
2189         dns_message_t *soaquery = NULL;
2190         isc_sockaddr_t *addr;
2191         isc_boolean_t seencname = ISC_FALSE;
2192         isc_boolean_t droplabel = ISC_FALSE;
2193         dns_name_t tname;
2194         unsigned int nlabels;
2195
2196         UNUSED(task);
2197
2198         REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
2199         reqev = (dns_requestevent_t *)event;
2200         request = reqev->request;
2201         result = eresult = reqev->result;
2202         POST(result);
2203         uctx = reqev->ev_arg;
2204         client = uctx->client;
2205         soaquery = uctx->soaquery;
2206         addr = uctx->currentserver;
2207         INSIST(addr != NULL);
2208
2209         isc_event_free(&event);
2210
2211         if (eresult != ISC_R_SUCCESS) {
2212                 result = eresult;
2213                 goto out;
2214         }
2215
2216         result = dns_message_create(uctx->client->mctx,
2217                                     DNS_MESSAGE_INTENTPARSE, &rcvmsg);
2218         if (result != ISC_R_SUCCESS)
2219                 goto out;
2220         result = dns_request_getresponse(request, rcvmsg,
2221                                          DNS_MESSAGEPARSE_PRESERVEORDER);
2222
2223         if (result == DNS_R_TSIGERRORSET) {
2224                 dns_request_t *newrequest = NULL;
2225
2226                 /* Retry SOA request without TSIG */
2227                 dns_message_destroy(&rcvmsg);
2228                 dns_message_renderreset(uctx->soaquery);
2229                 result = dns_request_createvia3(uctx->view->requestmgr,
2230                                                 uctx->soaquery, NULL, addr, 0,
2231                                                 NULL,
2232                                                 client->find_timeout * 20,
2233                                                 client->find_timeout, 3,
2234                                                 uctx->client->task,
2235                                                 receive_soa, uctx,
2236                                                 &newrequest);
2237                 if (result == ISC_R_SUCCESS) {
2238                         LOCK(&uctx->lock);
2239                         dns_request_destroy(&uctx->soareq);
2240                         uctx->soareq = newrequest;
2241                         UNLOCK(&uctx->lock);
2242
2243                         return;
2244                 }
2245                 goto out;
2246         }
2247
2248         section = DNS_SECTION_ANSWER;
2249         POST(section);
2250
2251         if (rcvmsg->rcode != dns_rcode_noerror &&
2252             rcvmsg->rcode != dns_rcode_nxdomain) {
2253                 result = rcode2result(rcvmsg->rcode);
2254                 goto out;
2255         }
2256
2257  lookforsoa:
2258         if (pass == 0)
2259                 section = DNS_SECTION_ANSWER;
2260         else if (pass == 1)
2261                 section = DNS_SECTION_AUTHORITY;
2262         else {
2263                 droplabel = ISC_TRUE;
2264                 goto out;
2265         }
2266
2267         result = dns_message_firstname(rcvmsg, section);
2268         if (result != ISC_R_SUCCESS) {
2269                 pass++;
2270                 goto lookforsoa;
2271         }
2272         while (result == ISC_R_SUCCESS) {
2273                 name = NULL;
2274                 dns_message_currentname(rcvmsg, section, &name);
2275                 soaset = NULL;
2276                 result = dns_message_findtype(name, dns_rdatatype_soa, 0,
2277                                               &soaset);
2278                 if (result == ISC_R_SUCCESS)
2279                         break;
2280                 if (section == DNS_SECTION_ANSWER) {
2281                         dns_rdataset_t *tset = NULL;
2282                         if (dns_message_findtype(name, dns_rdatatype_cname, 0,
2283                                                  &tset) == ISC_R_SUCCESS
2284                             ||
2285                             dns_message_findtype(name, dns_rdatatype_dname, 0,
2286                                                  &tset) == ISC_R_SUCCESS
2287                             )
2288                         {
2289                                 seencname = ISC_TRUE;
2290                                 break;
2291                         }
2292                 }
2293
2294                 result = dns_message_nextname(rcvmsg, section);
2295         }
2296
2297         if (soaset == NULL && !seencname) {
2298                 pass++;
2299                 goto lookforsoa;
2300         }
2301
2302         if (seencname) {
2303                 droplabel = ISC_TRUE;
2304                 goto out;
2305         }
2306
2307         result = process_soa(uctx, soaset, name);
2308
2309  out:
2310         if (droplabel) {
2311                 result = dns_message_firstname(soaquery, DNS_SECTION_QUESTION);
2312                 INSIST(result == ISC_R_SUCCESS);
2313                 name = NULL;
2314                 dns_message_currentname(soaquery, DNS_SECTION_QUESTION, &name);
2315                 nlabels = dns_name_countlabels(name);
2316                 if (nlabels == 1)
2317                         result = DNS_R_SERVFAIL; /* is there a better error? */
2318                 else {
2319                         dns_name_init(&tname, NULL);
2320                         dns_name_getlabelsequence(name, 1, nlabels - 1,
2321                                                   &tname);
2322                         dns_name_clone(&tname, name);
2323                         dns_request_destroy(&request);
2324                         LOCK(&uctx->lock);
2325                         uctx->soareq = NULL;
2326                         UNLOCK(&uctx->lock);
2327                         dns_message_renderreset(soaquery);
2328                         dns_message_settsigkey(soaquery, NULL);
2329                         result = dns_request_createvia3(uctx->view->requestmgr,
2330                                                         soaquery, NULL,
2331                                                         uctx->currentserver, 0,
2332                                                         uctx->tsigkey,
2333                                                         client->find_timeout *
2334                                                         20,
2335                                                         client->find_timeout,
2336                                                         3, client->task,
2337                                                         receive_soa, uctx,
2338                                                         &uctx->soareq);
2339                 }
2340         }
2341
2342         if (!droplabel || result != ISC_R_SUCCESS) {
2343                 dns_message_destroy(&uctx->soaquery);
2344                 LOCK(&uctx->lock);
2345                 dns_request_destroy(&uctx->soareq);
2346                 UNLOCK(&uctx->lock);
2347         }
2348
2349         if (rcvmsg != NULL)
2350                 dns_message_destroy(&rcvmsg);
2351
2352         if (result != ISC_R_SUCCESS)
2353                 update_sendevent(uctx, result);
2354 }
2355
2356 static isc_result_t
2357 request_soa(updatectx_t *uctx) {
2358         isc_result_t result;
2359         dns_message_t *soaquery = uctx->soaquery;
2360         dns_name_t *name = NULL;
2361         dns_rdataset_t *rdataset = NULL;
2362
2363         if (soaquery == NULL) {
2364                 result = dns_message_create(uctx->client->mctx,
2365                                             DNS_MESSAGE_INTENTRENDER,
2366                                             &soaquery);
2367                 if (result != ISC_R_SUCCESS)
2368                         return (result);
2369         }
2370         soaquery->flags |= DNS_MESSAGEFLAG_RD;
2371         result = dns_message_gettempname(soaquery, &name);
2372         if (result != ISC_R_SUCCESS)
2373                 goto fail;
2374         result = dns_message_gettemprdataset(soaquery, &rdataset);
2375         if (result != ISC_R_SUCCESS)
2376                 goto fail;
2377         dns_rdataset_makequestion(rdataset, uctx->rdclass, dns_rdatatype_soa);
2378         dns_name_clone(uctx->firstname, name);
2379         ISC_LIST_APPEND(name->list, rdataset, link);
2380         dns_message_addname(soaquery, name, DNS_SECTION_QUESTION);
2381         rdataset = NULL;
2382         name = NULL;
2383
2384         result = dns_request_createvia3(uctx->view->requestmgr,
2385                                         soaquery, NULL, uctx->currentserver, 0,
2386                                         uctx->tsigkey,
2387                                         uctx->client->find_timeout * 20,
2388                                         uctx->client->find_timeout, 3,
2389                                         uctx->client->task, receive_soa, uctx,
2390                                         &uctx->soareq);
2391         if (result == ISC_R_SUCCESS) {
2392                 uctx->soaquery = soaquery;
2393                 return (ISC_R_SUCCESS);
2394         }
2395
2396  fail:
2397         if (rdataset != NULL) {
2398                 ISC_LIST_UNLINK(name->list, rdataset, link); /* for safety */
2399                 dns_message_puttemprdataset(soaquery, &rdataset);
2400         }
2401         if (name != NULL)
2402                 dns_message_puttempname(soaquery, &name);
2403         dns_message_destroy(&soaquery);
2404
2405         return (result);
2406 }
2407
2408 static void
2409 resolvesoa_done(isc_task_t *task, isc_event_t *event) {
2410         dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
2411         updatectx_t *uctx;
2412         dns_name_t *name, tname;
2413         dns_rdataset_t *rdataset = NULL;
2414         isc_result_t result = rev->result;
2415         unsigned int nlabels;
2416
2417         UNUSED(task);
2418
2419         uctx = event->ev_arg;
2420         REQUIRE(UCTX_VALID(uctx));
2421
2422         LOCK(&uctx->lock);
2423         dns_client_destroyrestrans(&uctx->restrans);
2424         UNLOCK(&uctx->lock);
2425
2426         uctx = event->ev_arg;
2427         if (result != ISC_R_SUCCESS &&
2428             result != DNS_R_NCACHENXDOMAIN &&
2429             result != DNS_R_NCACHENXRRSET) {
2430                 /* XXX: what about DNSSEC failure? */
2431                 goto out;
2432         }
2433
2434         for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL;
2435              name = ISC_LIST_NEXT(name, link)) {
2436                 for (rdataset = ISC_LIST_HEAD(name->list);
2437                      rdataset != NULL;
2438                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
2439                         if (dns_rdataset_isassociated(rdataset) &&
2440                             rdataset->type == dns_rdatatype_soa)
2441                                 break;
2442                 }
2443         }
2444
2445         if (rdataset == NULL) {
2446                 /* Drop one label and retry resolution. */
2447                 nlabels = dns_name_countlabels(&uctx->soaqname);
2448                 if (nlabels == 1) {
2449                         result = DNS_R_SERVFAIL; /* is there a better error? */
2450                         goto out;
2451                 }
2452                 dns_name_init(&tname, NULL);
2453                 dns_name_getlabelsequence(&uctx->soaqname, 1, nlabels - 1,
2454                                           &tname);
2455                 dns_name_clone(&tname, &uctx->soaqname);
2456
2457                 result = dns_client_startresolve(uctx->client, &uctx->soaqname,
2458                                                  uctx->rdclass,
2459                                                  dns_rdatatype_soa, 0,
2460                                                  uctx->client->task,
2461                                                  resolvesoa_done, uctx,
2462                                                  &uctx->restrans);
2463         } else
2464                 result = process_soa(uctx, rdataset, &uctx->soaqname);
2465
2466  out:
2467         dns_client_freeresanswer(uctx->client, &rev->answerlist);
2468         isc_event_free(&event);
2469
2470         if (result != ISC_R_SUCCESS)
2471                 update_sendevent(uctx, result);
2472 }
2473
2474 static isc_result_t
2475 copy_name(isc_mem_t *mctx, dns_message_t *msg, dns_name_t *name,
2476           dns_name_t **newnamep)
2477 {
2478         isc_result_t result;
2479         dns_name_t *newname = NULL;
2480         isc_region_t r;
2481         isc_buffer_t *namebuf = NULL, *rdatabuf = NULL;
2482         dns_rdatalist_t *rdatalist;
2483         dns_rdataset_t *rdataset, *newrdataset;
2484         dns_rdata_t rdata = DNS_RDATA_INIT, *newrdata;
2485
2486         result = dns_message_gettempname(msg, &newname);
2487         if (result != ISC_R_SUCCESS)
2488                 return (result);
2489         result = isc_buffer_allocate(mctx, &namebuf, DNS_NAME_MAXWIRE);
2490         if (result != ISC_R_SUCCESS)
2491                 goto fail;
2492         dns_name_init(newname, NULL);
2493         dns_name_setbuffer(newname, namebuf);
2494         dns_message_takebuffer(msg, &namebuf);
2495         result = dns_name_copy(name, newname, NULL);
2496         if (result != ISC_R_SUCCESS)
2497                 goto fail;
2498
2499         for (rdataset = ISC_LIST_HEAD(name->list); rdataset != NULL;
2500              rdataset = ISC_LIST_NEXT(rdataset, link)) {
2501                 rdatalist = NULL;
2502                 result = dns_message_gettemprdatalist(msg, &rdatalist);
2503                 if (result != ISC_R_SUCCESS)
2504                         goto fail;
2505                 dns_rdatalist_init(rdatalist);
2506                 rdatalist->type = rdataset->type;
2507                 rdatalist->rdclass = rdataset->rdclass;
2508                 rdatalist->covers = rdataset->covers;
2509                 rdatalist->ttl = rdataset->ttl;
2510
2511                 result = dns_rdataset_first(rdataset);
2512                 while (result == ISC_R_SUCCESS) {
2513                         dns_rdata_reset(&rdata);
2514                         dns_rdataset_current(rdataset, &rdata);
2515
2516                         newrdata = NULL;
2517                         result = dns_message_gettemprdata(msg, &newrdata);
2518                         if (result != ISC_R_SUCCESS)
2519                                 goto fail;
2520                         dns_rdata_toregion(&rdata, &r);
2521                         rdatabuf = NULL;
2522                         result = isc_buffer_allocate(mctx, &rdatabuf,
2523                                                      r.length);
2524                         if (result != ISC_R_SUCCESS)
2525                                 goto fail;
2526                         isc_buffer_putmem(rdatabuf, r.base, r.length);
2527                         isc_buffer_usedregion(rdatabuf, &r);
2528                         dns_rdata_init(newrdata);
2529                         dns_rdata_fromregion(newrdata, rdata.rdclass,
2530                                              rdata.type, &r);
2531                         newrdata->flags = rdata.flags;
2532
2533                         ISC_LIST_APPEND(rdatalist->rdata, newrdata, link);
2534                         dns_message_takebuffer(msg, &rdatabuf);
2535
2536                         result = dns_rdataset_next(rdataset);
2537                 }
2538
2539                 newrdataset = NULL;
2540                 result = dns_message_gettemprdataset(msg, &newrdataset);
2541                 if (result != ISC_R_SUCCESS)
2542                         goto fail;
2543                 dns_rdataset_init(newrdataset);
2544                 dns_rdatalist_tordataset(rdatalist, newrdataset);
2545
2546                 ISC_LIST_APPEND(newname->list, newrdataset, link);
2547         }
2548
2549         *newnamep = newname;
2550
2551         return (ISC_R_SUCCESS);
2552
2553  fail:
2554         dns_message_puttempname(msg, &newname);
2555
2556         return (result);
2557
2558 }
2559
2560 static void
2561 internal_update_callback(isc_task_t *task, isc_event_t *event) {
2562         updatearg_t *uarg = event->ev_arg;
2563         dns_clientupdateevent_t *uev = (dns_clientupdateevent_t *)event;
2564
2565         UNUSED(task);
2566
2567         LOCK(&uarg->lock);
2568
2569         uarg->result = uev->result;
2570
2571         dns_client_destroyupdatetrans(&uarg->trans);
2572         isc_event_free(&event);
2573
2574         if (!uarg->canceled) {
2575                 UNLOCK(&uarg->lock);
2576
2577                 /* Exit from the internal event loop */
2578                 isc_app_ctxsuspend(uarg->actx);
2579         } else {
2580                 /*
2581                  * We have already exited from the loop (due to some
2582                  * unexpected event).  Just clean the arg up.
2583                  */
2584                 UNLOCK(&uarg->lock);
2585                 DESTROYLOCK(&uarg->lock);
2586                 isc_mem_put(uarg->client->mctx, uarg, sizeof(*uarg));
2587         }
2588 }
2589
2590 isc_result_t
2591 dns_client_update(dns_client_t *client, dns_rdataclass_t rdclass,
2592                   dns_name_t *zonename, dns_namelist_t *prerequisites,
2593                   dns_namelist_t *updates, isc_sockaddrlist_t *servers,
2594                   dns_tsec_t *tsec, unsigned int options)
2595 {
2596         isc_result_t result;
2597         isc_appctx_t *actx;
2598         updatearg_t *uarg;
2599
2600         REQUIRE(DNS_CLIENT_VALID(client));
2601
2602         if ((client->attributes & DNS_CLIENTATTR_OWNCTX) == 0 &&
2603             (options & DNS_CLIENTRESOPT_ALLOWRUN) == 0) {
2604                 /*
2605                  * If the client is run under application's control, we need
2606                  * to create a new running (sub)environment for this
2607                  * particular resolution.
2608                  */
2609                 return (ISC_R_NOTIMPLEMENTED); /* XXXTBD */
2610         } else
2611                 actx = client->actx;
2612
2613         uarg = isc_mem_get(client->mctx, sizeof(*uarg));
2614         if (uarg == NULL)
2615                 return (ISC_R_NOMEMORY);
2616
2617         result = isc_mutex_init(&uarg->lock);
2618         if (result != ISC_R_SUCCESS) {
2619                 isc_mem_put(client->mctx, uarg, sizeof(*uarg));
2620                 return (result);
2621         }
2622
2623         uarg->actx = actx;
2624         uarg->client = client;
2625         uarg->result = ISC_R_FAILURE;
2626         uarg->trans = NULL;
2627         uarg->canceled = ISC_FALSE;
2628
2629         result = dns_client_startupdate(client, rdclass, zonename,
2630                                         prerequisites, updates, servers,
2631                                         tsec, options, client->task,
2632                                         internal_update_callback, uarg,
2633                                         &uarg->trans);
2634         if (result != ISC_R_SUCCESS) {
2635                 DESTROYLOCK(&uarg->lock);
2636                 isc_mem_put(client->mctx, uarg, sizeof(*uarg));
2637                 return (result);
2638         }
2639
2640         /*
2641          * Start internal event loop.  It blocks until the entire process
2642          * is completed.
2643          */
2644         result = isc_app_ctxrun(actx);
2645
2646         LOCK(&uarg->lock);
2647         if (result == ISC_R_SUCCESS || result == ISC_R_SUSPEND)
2648                 result = uarg->result;
2649
2650         if (uarg->trans != NULL) {
2651                 /*
2652                  * Unusual termination (perhaps due to signal).  We need some
2653                  * tricky cleanup process.
2654                  */
2655                 uarg->canceled = ISC_TRUE;
2656                 dns_client_cancelupdate(uarg->trans);
2657
2658                 UNLOCK(&uarg->lock);
2659
2660                 /* uarg will be freed in the event handler. */
2661         } else {
2662                 UNLOCK(&uarg->lock);
2663
2664                 DESTROYLOCK(&uarg->lock);
2665                 isc_mem_put(client->mctx, uarg, sizeof(*uarg));
2666         }
2667
2668         return (result);
2669 }
2670
2671 isc_result_t
2672 dns_client_startupdate(dns_client_t *client, dns_rdataclass_t rdclass,
2673                        dns_name_t *zonename, dns_namelist_t *prerequisites,
2674                        dns_namelist_t *updates, isc_sockaddrlist_t *servers,
2675                        dns_tsec_t *tsec, unsigned int options,
2676                        isc_task_t *task, isc_taskaction_t action, void *arg,
2677                        dns_clientupdatetrans_t **transp)
2678 {
2679         dns_view_t *view = NULL;
2680         isc_result_t result;
2681         dns_name_t *name, *newname;
2682         updatectx_t *uctx;
2683         isc_task_t *clone = NULL;
2684         dns_section_t section = DNS_SECTION_UPDATE;
2685         isc_sockaddr_t *server, *sa = NULL;
2686         dns_tsectype_t tsectype = dns_tsectype_none;
2687
2688         UNUSED(options);
2689
2690         REQUIRE(DNS_CLIENT_VALID(client));
2691         REQUIRE(transp != NULL && *transp == NULL);
2692         REQUIRE(updates != NULL);
2693         REQUIRE(task != NULL);
2694
2695         if (tsec != NULL) {
2696                 tsectype = dns_tsec_gettype(tsec);
2697                 if (tsectype != dns_tsectype_tsig)
2698                         return (ISC_R_NOTIMPLEMENTED); /* XXX */
2699         }
2700
2701         LOCK(&client->lock);
2702         result = dns_viewlist_find(&client->viewlist, DNS_CLIENTVIEW_NAME,
2703                                    rdclass, &view);
2704         UNLOCK(&client->lock);
2705         if (result != ISC_R_SUCCESS)
2706                 return (result);
2707
2708         /* Create a context and prepare some resources */
2709         uctx = isc_mem_get(client->mctx, sizeof(*uctx));
2710         if (uctx == NULL) {
2711                 dns_view_detach(&view);
2712                 return (ISC_R_NOMEMORY);
2713         }
2714         result = isc_mutex_init(&uctx->lock);
2715         if (result != ISC_R_SUCCESS) {
2716                 dns_view_detach(&view);
2717                 isc_mem_put(client->mctx, uctx, sizeof(*uctx));
2718                 return (ISC_R_NOMEMORY);
2719         }
2720         clone = NULL;
2721         isc_task_attach(task, &clone);
2722         uctx->client = client;
2723         ISC_LINK_INIT(uctx, link);
2724         uctx->state = dns_clientupdatestate_prepare;
2725         uctx->view = view;
2726         uctx->rdclass = rdclass;
2727         uctx->canceled = ISC_FALSE;
2728         uctx->updatemsg = NULL;
2729         uctx->soaquery = NULL;
2730         uctx->updatereq = NULL;
2731         uctx->restrans = NULL;
2732         uctx->restrans2 = NULL;
2733         uctx->bp4 = NULL;
2734         uctx->bp6 = NULL;
2735         uctx->soareq = NULL;
2736         uctx->event = NULL;
2737         uctx->tsigkey = NULL;
2738         uctx->sig0key = NULL;
2739         uctx->zonename = NULL;
2740         dns_name_init(&uctx->soaqname, NULL);
2741         ISC_LIST_INIT(uctx->servers);
2742         uctx->nservers = 0;
2743         uctx->currentserver = NULL;
2744         dns_fixedname_init(&uctx->zonefname);
2745         if (tsec != NULL)
2746                 dns_tsec_getkey(tsec, &uctx->tsigkey);
2747         uctx->event = (dns_clientupdateevent_t *)
2748                 isc_event_allocate(client->mctx, clone, DNS_EVENT_UPDATEDONE,
2749                                    action, arg, sizeof(*uctx->event));
2750         if (uctx->event == NULL)
2751                 goto fail;
2752         if (zonename != NULL) {
2753                 uctx->zonename = dns_fixedname_name(&uctx->zonefname);
2754                 result = dns_name_copy(zonename, uctx->zonename, NULL);
2755         }
2756         if (servers != NULL) {
2757                 for (server = ISC_LIST_HEAD(*servers);
2758                      server != NULL;
2759                      server = ISC_LIST_NEXT(server, link)) {
2760                         sa = isc_mem_get(client->mctx, sizeof(*sa));
2761                         if (sa == NULL)
2762                                 goto fail;
2763                         sa->type = server->type;
2764                         sa->length = server->length;
2765                         ISC_LINK_INIT(sa, link);
2766                         ISC_LIST_APPEND(uctx->servers, sa, link);
2767                         if (uctx->currentserver == NULL)
2768                                 uctx->currentserver = sa;
2769                         uctx->nservers++;
2770                 }
2771         }
2772
2773         /* Make update message */
2774         result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTRENDER,
2775                                     &uctx->updatemsg);
2776         if (result != ISC_R_SUCCESS)
2777                 goto fail;
2778         uctx->updatemsg->opcode = dns_opcode_update;
2779
2780         if (prerequisites != NULL) {
2781                 for (name = ISC_LIST_HEAD(*prerequisites); name != NULL;
2782                      name = ISC_LIST_NEXT(name, link)) {
2783                         newname = NULL;
2784                         result = copy_name(client->mctx, uctx->updatemsg,
2785                                            name, &newname);
2786                         if (result != ISC_R_SUCCESS)
2787                                 goto fail;
2788                         dns_message_addname(uctx->updatemsg, newname,
2789                                             DNS_SECTION_PREREQUISITE);
2790                 }
2791         }
2792
2793         for (name = ISC_LIST_HEAD(*updates); name != NULL;
2794              name = ISC_LIST_NEXT(name, link)) {
2795                 newname = NULL;
2796                 result = copy_name(client->mctx, uctx->updatemsg, name,
2797                                    &newname);
2798                 if (result != ISC_R_SUCCESS)
2799                         goto fail;
2800                 dns_message_addname(uctx->updatemsg, newname,
2801                                     DNS_SECTION_UPDATE);
2802         }
2803
2804         uctx->firstname = NULL;
2805         result = dns_message_firstname(uctx->updatemsg, section);
2806         if (result == ISC_R_NOMORE) {
2807                 section = DNS_SECTION_PREREQUISITE;
2808                 result = dns_message_firstname(uctx->updatemsg, section);
2809         }
2810         if (result != ISC_R_SUCCESS)
2811                 goto fail;
2812         dns_message_currentname(uctx->updatemsg, section, &uctx->firstname);
2813
2814         uctx->magic = UCTX_MAGIC;
2815
2816         LOCK(&client->lock);
2817         ISC_LIST_APPEND(client->updatectxs, uctx, link);
2818         UNLOCK(&client->lock);
2819
2820         if (uctx->zonename != NULL && uctx->currentserver != NULL) {
2821                 result = send_update(uctx);
2822                 if (result != ISC_R_SUCCESS)
2823                         goto fail;
2824         } else if (uctx->currentserver != NULL) {
2825                 result = request_soa(uctx);
2826                 if (result != ISC_R_SUCCESS)
2827                         goto fail;
2828         } else {
2829                 dns_name_clone(uctx->firstname, &uctx->soaqname);
2830                 result = dns_client_startresolve(uctx->client, &uctx->soaqname,
2831                                                  uctx->rdclass,
2832                                                  dns_rdatatype_soa, 0,
2833                                                  client->task, resolvesoa_done,
2834                                                  uctx, &uctx->restrans);
2835                 if (result != ISC_R_SUCCESS)
2836                         goto fail;
2837         }
2838
2839         *transp = (dns_clientupdatetrans_t *)uctx;
2840
2841         return (ISC_R_SUCCESS);
2842
2843  fail:
2844         if (ISC_LINK_LINKED(uctx, link)) {
2845                 LOCK(&client->lock);
2846                 ISC_LIST_UNLINK(client->updatectxs, uctx, link);
2847                 UNLOCK(&client->lock);
2848         }
2849         if (uctx->updatemsg != NULL)
2850                 dns_message_destroy(&uctx->updatemsg);
2851         while ((sa = ISC_LIST_HEAD(uctx->servers)) != NULL) {
2852                 ISC_LIST_UNLINK(uctx->servers, sa, link);
2853                 isc_mem_put(client->mctx, sa, sizeof(*sa));
2854         }
2855         if (uctx->event != NULL)
2856                 isc_event_free(ISC_EVENT_PTR(&uctx->event));
2857         if (uctx->tsigkey != NULL)
2858                 dns_tsigkey_detach(&uctx->tsigkey);
2859         isc_task_detach(&clone);
2860         DESTROYLOCK(&uctx->lock);
2861         uctx->magic = 0;
2862         isc_mem_put(client->mctx, uctx, sizeof(*uctx));
2863         dns_view_detach(&view);
2864
2865         return (result);
2866 }
2867
2868 void
2869 dns_client_cancelupdate(dns_clientupdatetrans_t *trans) {
2870         updatectx_t *uctx;
2871
2872         REQUIRE(trans != NULL);
2873         uctx = (updatectx_t *)trans;
2874         REQUIRE(UCTX_VALID(uctx));
2875
2876         LOCK(&uctx->lock);
2877
2878         if (!uctx->canceled) {
2879                 uctx->canceled = ISC_TRUE;
2880                 if (uctx->updatereq != NULL)
2881                         dns_request_cancel(uctx->updatereq);
2882                 if (uctx->soareq != NULL)
2883                         dns_request_cancel(uctx->soareq);
2884                 if (uctx->restrans != NULL)
2885                         dns_client_cancelresolve(uctx->restrans);
2886                 if (uctx->restrans2 != NULL)
2887                         dns_client_cancelresolve(uctx->restrans2);
2888         }
2889
2890         UNLOCK(&uctx->lock);
2891 }
2892
2893 void
2894 dns_client_destroyupdatetrans(dns_clientupdatetrans_t **transp) {
2895         updatectx_t *uctx;
2896         isc_mem_t *mctx;
2897         dns_client_t *client;
2898         isc_boolean_t need_destroyclient = ISC_FALSE;
2899         isc_sockaddr_t *sa;
2900
2901         REQUIRE(transp != NULL);
2902         uctx = (updatectx_t *)*transp;
2903         REQUIRE(UCTX_VALID(uctx));
2904         client = uctx->client;
2905         REQUIRE(DNS_CLIENT_VALID(client));
2906         REQUIRE(uctx->updatereq == NULL && uctx->updatemsg == NULL &&
2907                 uctx->soareq == NULL && uctx->soaquery == NULL &&
2908                 uctx->event == NULL && uctx->tsigkey == NULL &&
2909                 uctx->sig0key == NULL);
2910
2911         mctx = client->mctx;
2912         dns_view_detach(&uctx->view);
2913         while ((sa = ISC_LIST_HEAD(uctx->servers)) != NULL) {
2914                 ISC_LIST_UNLINK(uctx->servers, sa, link);
2915                 isc_mem_put(mctx, sa, sizeof(*sa));
2916         }
2917
2918         LOCK(&client->lock);
2919
2920         INSIST(ISC_LINK_LINKED(uctx, link));
2921         ISC_LIST_UNLINK(client->updatectxs, uctx, link);
2922
2923         if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
2924             ISC_LIST_EMPTY(client->reqctxs) &&
2925             ISC_LIST_EMPTY(client->updatectxs))
2926                 need_destroyclient = ISC_TRUE;
2927
2928         UNLOCK(&client->lock);
2929
2930         DESTROYLOCK(&uctx->lock);
2931         uctx->magic = 0;
2932
2933         isc_mem_put(mctx, uctx, sizeof(*uctx));
2934
2935         if (need_destroyclient)
2936                 destroyclient(&client);
2937
2938         *transp = NULL;
2939 }
2940
2941 isc_mem_t *
2942 dns_client_mctx(dns_client_t *client) {
2943
2944         REQUIRE(DNS_CLIENT_VALID(client));
2945         return (client->mctx);
2946 }
2947
2948 typedef struct {
2949         isc_buffer_t    buffer;
2950         dns_rdataset_t  rdataset;
2951         dns_rdatalist_t rdatalist;
2952         dns_rdata_t     rdata;
2953         size_t          size;
2954         isc_mem_t *     mctx;
2955         unsigned char   data[FLEXIBLE_ARRAY_MEMBER];
2956 } dns_client_updaterec_t;
2957
2958 isc_result_t
2959 dns_client_updaterec(dns_client_updateop_t op, dns_name_t *owner,
2960                      dns_rdatatype_t type, dns_rdata_t *source,
2961                      dns_ttl_t ttl, dns_name_t *target,
2962                      dns_rdataset_t *rdataset, dns_rdatalist_t *rdatalist,
2963                      dns_rdata_t *rdata, isc_mem_t *mctx)
2964 {
2965         dns_client_updaterec_t *updaterec = NULL;
2966         size_t size = offsetof(dns_client_updaterec_t, data);
2967
2968         REQUIRE(op < updateop_max);
2969         REQUIRE(owner != NULL);
2970         REQUIRE((rdataset != NULL && rdatalist != NULL && rdata != NULL) ||
2971                 (rdataset == NULL && rdatalist == NULL && rdata == NULL &&
2972                  mctx != NULL));
2973         if (op == updateop_add)
2974                 REQUIRE(source != NULL);
2975         if (source != NULL) {
2976                 REQUIRE(source->type == type);
2977                 REQUIRE(op == updateop_add || op == updateop_delete ||
2978                         op == updateop_exist);
2979         }
2980
2981         size += owner->length;
2982         if (source != NULL)
2983                 size += source->length;
2984
2985         if (rdataset == NULL) {
2986                 updaterec = isc_mem_get(mctx, size);
2987                 if (updaterec == NULL)
2988                         return (ISC_R_NOMEMORY);
2989                 rdataset = &updaterec->rdataset;
2990                 rdatalist = &updaterec->rdatalist;
2991                 rdata = &updaterec->rdata;
2992                 dns_rdataset_init(rdataset);
2993                 dns_rdatalist_init(&updaterec->rdatalist);
2994                 dns_rdata_init(&updaterec->rdata);
2995                 isc_buffer_init(&updaterec->buffer, updaterec->data,
2996                                 size - offsetof(dns_client_updaterec_t, data));
2997                 dns_name_copy(owner, target, &updaterec->buffer);
2998                 if (source != NULL) {
2999                         isc_region_t r;
3000                         dns_rdata_clone(source, rdata);
3001                         dns_rdata_toregion(rdata, &r);
3002                         rdata->data = isc_buffer_used(&updaterec->buffer);
3003                         isc_buffer_copyregion(&updaterec->buffer, &r);
3004                 }
3005                 updaterec->mctx = NULL;
3006                 isc_mem_attach(mctx, &updaterec->mctx);
3007         } else if (source != NULL)
3008                 dns_rdata_clone(source, rdata);
3009
3010         switch (op) {
3011         case updateop_add:
3012                 break;
3013         case updateop_delete:
3014                 if (source != NULL) {
3015                         ttl = 0;
3016                         dns_rdata_makedelete(rdata);
3017                 } else
3018                         dns_rdata_deleterrset(rdata, type);
3019                 break;
3020         case updateop_notexist:
3021                 dns_rdata_notexist(rdata, type);
3022                 break;
3023         case updateop_exist:
3024                 if (source == NULL) {
3025                         ttl = 0;
3026                         dns_rdata_exists(rdata, type);
3027                 }
3028         case updateop_none:
3029                 break;
3030         default:
3031                 INSIST(0);
3032         }
3033
3034         rdatalist->type = rdata->type;
3035         rdatalist->rdclass = rdata->rdclass;
3036         if (source != NULL) {
3037                 rdatalist->covers = dns_rdata_covers(rdata);
3038                 rdatalist->ttl = ttl;
3039         }
3040         ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
3041         dns_rdatalist_tordataset(rdatalist, rdataset);
3042         ISC_LIST_APPEND(target->list, rdataset, link);
3043         if (updaterec != NULL) {
3044                 target->attributes |= DNS_NAMEATTR_HASUPDATEREC;
3045                 dns_name_setbuffer(target, &updaterec->buffer);
3046         }
3047         if (op == updateop_add || op == updateop_delete)
3048                 target->attributes |= DNS_NAMEATTR_UPDATE;
3049         else
3050                 target->attributes |= DNS_NAMEATTR_PREREQUISITE;
3051         return (ISC_R_SUCCESS);
3052 }
3053
3054 void
3055 dns_client_freeupdate(dns_name_t **namep) {
3056         dns_client_updaterec_t *updaterec;
3057         dns_rdatalist_t *rdatalist;
3058         dns_rdataset_t *rdataset;
3059         dns_rdata_t *rdata;
3060         dns_name_t *name;
3061
3062         REQUIRE(namep != NULL && *namep != NULL);
3063
3064         name = *namep;
3065         for (rdataset = ISC_LIST_HEAD(name->list);
3066              rdataset != NULL;
3067              rdataset = ISC_LIST_HEAD(name->list)) {
3068                 ISC_LIST_UNLINK(name->list, rdataset, link);
3069                 rdatalist = NULL;
3070                 dns_rdatalist_fromrdataset(rdataset, &rdatalist);
3071                 if (rdatalist == NULL) {
3072                         dns_rdataset_disassociate(rdataset);
3073                         continue;
3074                 }
3075                 for (rdata = ISC_LIST_HEAD(rdatalist->rdata);
3076                      rdata != NULL;
3077                      rdata = ISC_LIST_HEAD(rdatalist->rdata))
3078                         ISC_LIST_UNLINK(rdatalist->rdata, rdata, link);
3079                 dns_rdataset_disassociate(rdataset);
3080         }
3081
3082         if ((name->attributes & DNS_NAMEATTR_HASUPDATEREC) != 0) {
3083                 updaterec = (dns_client_updaterec_t *)name->buffer;
3084                 INSIST(updaterec != NULL);
3085                 isc_mem_putanddetach(&updaterec->mctx, updaterec,
3086                                      updaterec->size);
3087                 *namep = NULL;
3088         }
3089 }