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