BIND: update vendor tree to 9.5.2-P2
[dragonfly.git] / contrib / bind / lib / dns / byaddr.c
1 /*
2  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: byaddr.c,v 1.39 2007/06/19 23:47:16 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <isc/mem.h>
25 #include <isc/netaddr.h>
26 #include <isc/print.h>
27 #include <isc/string.h>         /* Required for HP/UX (and others?) */
28 #include <isc/task.h>
29 #include <isc/util.h>
30
31 #include <dns/byaddr.h>
32 #include <dns/db.h>
33 #include <dns/events.h>
34 #include <dns/lookup.h>
35 #include <dns/rdata.h>
36 #include <dns/rdataset.h>
37 #include <dns/rdatastruct.h>
38 #include <dns/resolver.h>
39 #include <dns/result.h>
40 #include <dns/view.h>
41
42 /*
43  * XXXRTH  We could use a static event...
44  */
45
46 struct dns_byaddr {
47         /* Unlocked. */
48         unsigned int            magic;
49         isc_mem_t *             mctx;
50         isc_mutex_t             lock;
51         dns_fixedname_t         name;
52         /* Locked by lock. */
53         unsigned int            options;
54         dns_lookup_t *          lookup;
55         isc_task_t *            task;
56         dns_byaddrevent_t *     event;
57         isc_boolean_t           canceled;
58 };
59
60 #define BYADDR_MAGIC                    ISC_MAGIC('B', 'y', 'A', 'd')
61 #define VALID_BYADDR(b)                 ISC_MAGIC_VALID(b, BYADDR_MAGIC)
62
63 #define MAX_RESTARTS 16
64
65 static char hex_digits[] = {
66         '0', '1', '2', '3', '4', '5', '6', '7',
67         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
68 };
69
70 isc_result_t
71 dns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
72                          dns_name_t *name)
73 {
74         /*
75          * We dropped bitstring labels, so all lookups will use nibbles.
76          */
77         UNUSED(nibble);
78
79         return (dns_byaddr_createptrname2(address,
80                                           DNS_BYADDROPT_IPV6INT, name));
81 }
82
83 isc_result_t
84 dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
85                           dns_name_t *name)
86 {
87         char textname[128];
88         unsigned char *bytes;
89         int i;
90         char *cp;
91         isc_buffer_t buffer;
92         unsigned int len;
93
94         REQUIRE(address != NULL);
95
96         /*
97          * We create the text representation and then convert to a
98          * dns_name_t.  This is not maximally efficient, but it keeps all
99          * of the knowledge of wire format in the dns_name_ routines.
100          */
101
102         bytes = (unsigned char *)(&address->type);
103         if (address->family == AF_INET) {
104                 (void)snprintf(textname, sizeof(textname),
105                                "%u.%u.%u.%u.in-addr.arpa.",
106                                (bytes[3] & 0xff),
107                                (bytes[2] & 0xff),
108                                (bytes[1] & 0xff),
109                                (bytes[0] & 0xff));
110         } else if (address->family == AF_INET6) {
111                 cp = textname;
112                 for (i = 15; i >= 0; i--) {
113                         *cp++ = hex_digits[bytes[i] & 0x0f];
114                         *cp++ = '.';
115                         *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
116                         *cp++ = '.';
117                 }
118                 if ((options & DNS_BYADDROPT_IPV6INT) != 0)
119                         strcpy(cp, "ip6.int.");
120                 else
121                         strcpy(cp, "ip6.arpa.");
122         } else
123                 return (ISC_R_NOTIMPLEMENTED);
124
125         len = (unsigned int)strlen(textname);
126         isc_buffer_init(&buffer, textname, len);
127         isc_buffer_add(&buffer, len);
128         return (dns_name_fromtext(name, &buffer, dns_rootname,
129                                   ISC_FALSE, NULL));
130 }
131
132 static inline isc_result_t
133 copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
134         isc_result_t result;
135         dns_name_t *name;
136         dns_rdata_t rdata = DNS_RDATA_INIT;
137
138         /*
139          * The caller must be holding the byaddr's lock.
140          */
141
142         result = dns_rdataset_first(rdataset);
143         while (result == ISC_R_SUCCESS) {
144                 dns_rdata_ptr_t ptr;
145                 dns_rdataset_current(rdataset, &rdata);
146                 result = dns_rdata_tostruct(&rdata, &ptr, NULL);
147                 if (result != ISC_R_SUCCESS)
148                         return (result);
149                 name = isc_mem_get(byaddr->mctx, sizeof(*name));
150                 if (name == NULL) {
151                         dns_rdata_freestruct(&ptr);
152                         return (ISC_R_NOMEMORY);
153                 }
154                 dns_name_init(name, NULL);
155                 result = dns_name_dup(&ptr.ptr, byaddr->mctx, name);
156                 dns_rdata_freestruct(&ptr);
157                 if (result != ISC_R_SUCCESS) {
158                         isc_mem_put(byaddr->mctx, name, sizeof(*name));
159                         return (ISC_R_NOMEMORY);
160                 }
161                 ISC_LIST_APPEND(byaddr->event->names, name, link);
162                 dns_rdata_reset(&rdata);
163                 result = dns_rdataset_next(rdataset);
164         }
165         if (result == ISC_R_NOMORE)
166                 result = ISC_R_SUCCESS;
167
168         return (result);
169 }
170
171 static void
172 lookup_done(isc_task_t *task, isc_event_t *event) {
173         dns_byaddr_t *byaddr = event->ev_arg;
174         dns_lookupevent_t *levent;
175         isc_result_t result;
176
177         REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
178         REQUIRE(VALID_BYADDR(byaddr));
179         REQUIRE(byaddr->task == task);
180
181         UNUSED(task);
182
183         levent = (dns_lookupevent_t *)event;
184
185         if (levent->result == ISC_R_SUCCESS) {
186                 result = copy_ptr_targets(byaddr, levent->rdataset);
187                 byaddr->event->result = result;
188         } else
189                 byaddr->event->result = levent->result;
190         isc_event_free(&event);
191         isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
192 }
193
194 static void
195 bevent_destroy(isc_event_t *event) {
196         dns_byaddrevent_t *bevent;
197         dns_name_t *name, *next_name;
198         isc_mem_t *mctx;
199
200         REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
201         mctx = event->ev_destroy_arg;
202         bevent = (dns_byaddrevent_t *)event;
203
204         for (name = ISC_LIST_HEAD(bevent->names);
205              name != NULL;
206              name = next_name) {
207                 next_name = ISC_LIST_NEXT(name, link);
208                 ISC_LIST_UNLINK(bevent->names, name, link);
209                 dns_name_free(name, mctx);
210                 isc_mem_put(mctx, name, sizeof(*name));
211         }
212         isc_mem_put(mctx, event, event->ev_size);
213 }
214
215 isc_result_t
216 dns_byaddr_create(isc_mem_t *mctx, isc_netaddr_t *address, dns_view_t *view,
217                   unsigned int options, isc_task_t *task,
218                   isc_taskaction_t action, void *arg, dns_byaddr_t **byaddrp)
219 {
220         isc_result_t result;
221         dns_byaddr_t *byaddr;
222         isc_event_t *ievent;
223
224         byaddr = isc_mem_get(mctx, sizeof(*byaddr));
225         if (byaddr == NULL)
226                 return (ISC_R_NOMEMORY);
227         byaddr->mctx = mctx;
228         byaddr->options = options;
229
230         byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
231         if (byaddr->event == NULL) {
232                 result = ISC_R_NOMEMORY;
233                 goto cleanup_byaddr;
234         }
235         ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
236                        DNS_EVENT_BYADDRDONE, action, arg, byaddr,
237                        bevent_destroy, mctx);
238         byaddr->event->result = ISC_R_FAILURE;
239         ISC_LIST_INIT(byaddr->event->names);
240
241         byaddr->task = NULL;
242         isc_task_attach(task, &byaddr->task);
243
244         result = isc_mutex_init(&byaddr->lock);
245         if (result != ISC_R_SUCCESS)
246                 goto cleanup_event;
247
248         dns_fixedname_init(&byaddr->name);
249
250         result = dns_byaddr_createptrname2(address, options,
251                                            dns_fixedname_name(&byaddr->name));
252         if (result != ISC_R_SUCCESS)
253                 goto cleanup_lock;
254
255         byaddr->lookup = NULL;
256         result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
257                                    dns_rdatatype_ptr, view, 0, task,
258                                    lookup_done, byaddr, &byaddr->lookup);
259         if (result != ISC_R_SUCCESS)
260                 goto cleanup_lock;
261
262         byaddr->canceled = ISC_FALSE;
263         byaddr->magic = BYADDR_MAGIC;
264
265         *byaddrp = byaddr;
266
267         return (ISC_R_SUCCESS);
268
269  cleanup_lock:
270         DESTROYLOCK(&byaddr->lock);
271
272  cleanup_event:
273         ievent = (isc_event_t *)byaddr->event;
274         isc_event_free(&ievent);
275         byaddr->event = NULL;
276
277         isc_task_detach(&byaddr->task);
278
279  cleanup_byaddr:
280         isc_mem_put(mctx, byaddr, sizeof(*byaddr));
281
282         return (result);
283 }
284
285 void
286 dns_byaddr_cancel(dns_byaddr_t *byaddr) {
287         REQUIRE(VALID_BYADDR(byaddr));
288
289         LOCK(&byaddr->lock);
290
291         if (!byaddr->canceled) {
292                 byaddr->canceled = ISC_TRUE;
293                 if (byaddr->lookup != NULL)
294                         dns_lookup_cancel(byaddr->lookup);
295         }
296
297         UNLOCK(&byaddr->lock);
298 }
299
300 void
301 dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
302         dns_byaddr_t *byaddr;
303
304         REQUIRE(byaddrp != NULL);
305         byaddr = *byaddrp;
306         REQUIRE(VALID_BYADDR(byaddr));
307         REQUIRE(byaddr->event == NULL);
308         REQUIRE(byaddr->task == NULL);
309         dns_lookup_destroy(&byaddr->lookup);
310
311         DESTROYLOCK(&byaddr->lock);
312         byaddr->magic = 0;
313         isc_mem_put(byaddr->mctx, byaddr, sizeof(*byaddr));
314
315         *byaddrp = NULL;
316 }