2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001, 2003 Internet Software Consortium.
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.
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.
18 /* $Id: byaddr.c,v 1.29.2.3 2004/03/09 06:10:59 marka Exp $ */
23 #include <isc/netaddr.h>
24 #include <isc/string.h> /* Required for HP/UX (and others?) */
28 #include <dns/byaddr.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>
40 * XXXRTH We could use a static event...
51 dns_lookup_t * lookup;
53 dns_byaddrevent_t * event;
54 isc_boolean_t canceled;
57 #define BYADDR_MAGIC ISC_MAGIC('B', 'y', 'A', 'd')
58 #define VALID_BYADDR(b) ISC_MAGIC_VALID(b, BYADDR_MAGIC)
60 #define MAX_RESTARTS 16
62 static char hex_digits[] = {
63 '0', '1', '2', '3', '4', '5', '6', '7',
64 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
68 dns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
71 unsigned int options = DNS_BYADDROPT_IPV6INT;
74 options |= DNS_BYADDROPT_IPV6NIBBLE;
76 return (dns_byaddr_createptrname2(address, options, name));
80 dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
90 REQUIRE(address != NULL);
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.
98 bytes = (unsigned char *)(&address->type);
99 if (address->family == AF_INET) {
100 (void)sprintf(textname, "%u.%u.%u.%u.in-addr.arpa.",
105 } else if (address->family == AF_INET6) {
106 if ((options & DNS_BYADDROPT_IPV6NIBBLE) != 0) {
108 for (i = 15; i >= 0; i--) {
109 *cp++ = hex_digits[bytes[i] & 0x0f];
111 *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
114 if ((options & DNS_BYADDROPT_IPV6INT) != 0)
115 strcpy(cp, "ip6.int.");
117 strcpy(cp, "ip6.arpa.");
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];
129 strcpy(cp, "].ip6.arpa.");
132 return (ISC_R_NOTIMPLEMENTED);
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,
141 static inline isc_result_t
142 copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
145 dns_rdata_t rdata = DNS_RDATA_INIT;
148 * The caller must be holding the byaddr's lock.
151 result = dns_rdataset_first(rdataset);
152 while (result == ISC_R_SUCCESS) {
154 dns_rdataset_current(rdataset, &rdata);
155 result = dns_rdata_tostruct(&rdata, &ptr, NULL);
156 if (result != ISC_R_SUCCESS)
158 name = isc_mem_get(byaddr->mctx, sizeof *name);
160 dns_rdata_freestruct(&ptr);
161 return (ISC_R_NOMEMORY);
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);
170 ISC_LIST_APPEND(byaddr->event->names, name, link);
171 dns_rdata_reset(&rdata);
172 result = dns_rdataset_next(rdataset);
174 if (result == ISC_R_NOMORE)
175 result = ISC_R_SUCCESS;
181 lookup_done(isc_task_t *task, isc_event_t *event) {
182 dns_byaddr_t *byaddr = event->ev_arg;
183 dns_lookupevent_t *levent;
186 REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
187 REQUIRE(VALID_BYADDR(byaddr));
188 REQUIRE(byaddr->task == task);
192 levent = (dns_lookupevent_t *)event;
194 if (levent->result == ISC_R_SUCCESS) {
195 result = copy_ptr_targets(byaddr, levent->rdataset);
196 byaddr->event->result = result;
198 byaddr->event->result = levent->result;
199 isc_event_free(&event);
200 isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
204 bevent_destroy(isc_event_t *event) {
205 dns_byaddrevent_t *bevent;
206 dns_name_t *name, *next_name;
209 REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
210 mctx = event->ev_destroy_arg;
211 bevent = (dns_byaddrevent_t *)event;
213 for (name = ISC_LIST_HEAD(bevent->names);
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);
221 isc_mem_put(mctx, event, event->ev_size);
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)
230 dns_byaddr_t *byaddr;
233 byaddr = isc_mem_get(mctx, sizeof *byaddr);
235 return (ISC_R_NOMEMORY);
237 byaddr->options = options;
239 byaddr->event = isc_mem_get(mctx, sizeof *byaddr->event);
240 if (byaddr->event == NULL) {
241 result = ISC_R_NOMEMORY;
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);
251 isc_task_attach(task, &byaddr->task);
253 result = isc_mutex_init(&byaddr->lock);
254 if (result != ISC_R_SUCCESS)
257 dns_fixedname_init(&byaddr->name);
259 result = dns_byaddr_createptrname2(address, byaddr->options,
260 dns_fixedname_name(&byaddr->name));
261 if (result != ISC_R_SUCCESS)
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)
271 byaddr->canceled = ISC_FALSE;
272 byaddr->magic = BYADDR_MAGIC;
276 return (ISC_R_SUCCESS);
279 DESTROYLOCK(&byaddr->lock);
282 ievent = (isc_event_t *)byaddr->event;
283 isc_event_free(&ievent);
284 byaddr->event = NULL;
286 isc_task_detach(&byaddr->task);
289 isc_mem_put(mctx, byaddr, sizeof *byaddr);
295 dns_byaddr_cancel(dns_byaddr_t *byaddr) {
296 REQUIRE(VALID_BYADDR(byaddr));
300 if (!byaddr->canceled) {
301 byaddr->canceled = ISC_TRUE;
302 if (byaddr->lookup != NULL)
303 dns_lookup_cancel(byaddr->lookup);
306 UNLOCK(&byaddr->lock);
310 dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
311 dns_byaddr_t *byaddr;
313 REQUIRE(byaddrp != NULL);
315 REQUIRE(VALID_BYADDR(byaddr));
316 REQUIRE(byaddr->event == NULL);
317 REQUIRE(byaddr->task == NULL);
318 dns_lookup_destroy(&byaddr->lookup);
320 DESTROYLOCK(&byaddr->lock);
322 isc_mem_put(byaddr->mctx, byaddr, sizeof *byaddr);