2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
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.
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.41 2009/09/02 23:48:02 tbox Exp $ */
25 #include <isc/netaddr.h>
26 #include <isc/print.h>
27 #include <isc/string.h> /* Required for HP/UX (and others?) */
31 #include <dns/byaddr.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>
43 * XXXRTH We could use a static event...
46 static char hex_digits[] = {
47 '0', '1', '2', '3', '4', '5', '6', '7',
48 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
52 dns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
56 * We dropped bitstring labels, so all lookups will use nibbles.
60 return (dns_byaddr_createptrname2(address,
61 DNS_BYADDROPT_IPV6INT, name));
65 dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
75 REQUIRE(address != NULL);
78 * We create the text representation and then convert to a
79 * dns_name_t. This is not maximally efficient, but it keeps all
80 * of the knowledge of wire format in the dns_name_ routines.
83 bytes = (unsigned char *)(&address->type);
84 if (address->family == AF_INET) {
85 (void)snprintf(textname, sizeof(textname),
86 "%u.%u.%u.%u.in-addr.arpa.",
91 } else if (address->family == AF_INET6) {
93 for (i = 15; i >= 0; i--) {
94 *cp++ = hex_digits[bytes[i] & 0x0f];
96 *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
99 if ((options & DNS_BYADDROPT_IPV6INT) != 0)
100 strcpy(cp, "ip6.int.");
102 strcpy(cp, "ip6.arpa.");
104 return (ISC_R_NOTIMPLEMENTED);
106 len = (unsigned int)strlen(textname);
107 isc_buffer_init(&buffer, textname, len);
108 isc_buffer_add(&buffer, len);
109 return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
118 dns_fixedname_t name;
119 /* Locked by lock. */
120 unsigned int options;
121 dns_lookup_t * lookup;
123 dns_byaddrevent_t * event;
124 isc_boolean_t canceled;
127 #define BYADDR_MAGIC ISC_MAGIC('B', 'y', 'A', 'd')
128 #define VALID_BYADDR(b) ISC_MAGIC_VALID(b, BYADDR_MAGIC)
130 #define MAX_RESTARTS 16
132 static inline isc_result_t
133 copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
136 dns_rdata_t rdata = DNS_RDATA_INIT;
139 * The caller must be holding the byaddr's lock.
142 result = dns_rdataset_first(rdataset);
143 while (result == ISC_R_SUCCESS) {
145 dns_rdataset_current(rdataset, &rdata);
146 result = dns_rdata_tostruct(&rdata, &ptr, NULL);
147 if (result != ISC_R_SUCCESS)
149 name = isc_mem_get(byaddr->mctx, sizeof(*name));
151 dns_rdata_freestruct(&ptr);
152 return (ISC_R_NOMEMORY);
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);
161 ISC_LIST_APPEND(byaddr->event->names, name, link);
162 dns_rdata_reset(&rdata);
163 result = dns_rdataset_next(rdataset);
165 if (result == ISC_R_NOMORE)
166 result = ISC_R_SUCCESS;
172 lookup_done(isc_task_t *task, isc_event_t *event) {
173 dns_byaddr_t *byaddr = event->ev_arg;
174 dns_lookupevent_t *levent;
177 REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
178 REQUIRE(VALID_BYADDR(byaddr));
179 REQUIRE(byaddr->task == task);
183 levent = (dns_lookupevent_t *)event;
185 if (levent->result == ISC_R_SUCCESS) {
186 result = copy_ptr_targets(byaddr, levent->rdataset);
187 byaddr->event->result = result;
189 byaddr->event->result = levent->result;
190 isc_event_free(&event);
191 isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
195 bevent_destroy(isc_event_t *event) {
196 dns_byaddrevent_t *bevent;
197 dns_name_t *name, *next_name;
200 REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
201 mctx = event->ev_destroy_arg;
202 bevent = (dns_byaddrevent_t *)event;
204 for (name = ISC_LIST_HEAD(bevent->names);
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));
212 isc_mem_put(mctx, event, event->ev_size);
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)
221 dns_byaddr_t *byaddr;
224 byaddr = isc_mem_get(mctx, sizeof(*byaddr));
226 return (ISC_R_NOMEMORY);
228 byaddr->options = options;
230 byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
231 if (byaddr->event == NULL) {
232 result = ISC_R_NOMEMORY;
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);
242 isc_task_attach(task, &byaddr->task);
244 result = isc_mutex_init(&byaddr->lock);
245 if (result != ISC_R_SUCCESS)
248 dns_fixedname_init(&byaddr->name);
250 result = dns_byaddr_createptrname2(address, options,
251 dns_fixedname_name(&byaddr->name));
252 if (result != ISC_R_SUCCESS)
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)
262 byaddr->canceled = ISC_FALSE;
263 byaddr->magic = BYADDR_MAGIC;
267 return (ISC_R_SUCCESS);
270 DESTROYLOCK(&byaddr->lock);
273 ievent = (isc_event_t *)byaddr->event;
274 isc_event_free(&ievent);
275 byaddr->event = NULL;
277 isc_task_detach(&byaddr->task);
280 isc_mem_put(mctx, byaddr, sizeof(*byaddr));
286 dns_byaddr_cancel(dns_byaddr_t *byaddr) {
287 REQUIRE(VALID_BYADDR(byaddr));
291 if (!byaddr->canceled) {
292 byaddr->canceled = ISC_TRUE;
293 if (byaddr->lookup != NULL)
294 dns_lookup_cancel(byaddr->lookup);
297 UNLOCK(&byaddr->lock);
301 dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
302 dns_byaddr_t *byaddr;
304 REQUIRE(byaddrp != NULL);
306 REQUIRE(VALID_BYADDR(byaddr));
307 REQUIRE(byaddr->event == NULL);
308 REQUIRE(byaddr->task == NULL);
309 dns_lookup_destroy(&byaddr->lookup);
311 DESTROYLOCK(&byaddr->lock);
313 isc_mem_put(byaddr->mctx, byaddr, sizeof(*byaddr));