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