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