byaddr.c revision 224092
12088Ssos/*
22088Ssos * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
32088Ssos * Copyright (C) 2000-2003  Internet Software Consortium.
42088Ssos *
52088Ssos * Permission to use, copy, modify, and/or distribute this software for any
62088Ssos * purpose with or without fee is hereby granted, provided that the above
72088Ssos * copyright notice and this permission notice appear in all copies.
82088Ssos *
92088Ssos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
102088Ssos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
112088Ssos * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
122088Ssos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
132088Ssos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
142088Ssos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
152088Ssos * PERFORMANCE OF THIS SOFTWARE.
162088Ssos */
172088Ssos
182088Ssos/* $Id: byaddr.c,v 1.41 2009-09-02 23:48:02 tbox Exp $ */
192088Ssos
202088Ssos/*! \file */
212088Ssos
222088Ssos#include <config.h>
232088Ssos
242088Ssos#include <isc/mem.h>
252088Ssos#include <isc/netaddr.h>
262088Ssos#include <isc/print.h>
272088Ssos#include <isc/string.h>		/* Required for HP/UX (and others?) */
282088Ssos#include <isc/task.h>
292088Ssos#include <isc/util.h>
302088Ssos
312088Ssos#include <dns/byaddr.h>
322088Ssos#include <dns/db.h>
332088Ssos#include <dns/events.h>
342088Ssos#include <dns/lookup.h>
352088Ssos#include <dns/rdata.h>
362088Ssos#include <dns/rdataset.h>
372088Ssos#include <dns/rdatastruct.h>
382088Ssos#include <dns/resolver.h>
392088Ssos#include <dns/result.h>
402088Ssos#include <dns/view.h>
412088Ssos
422088Ssos/*
432088Ssos * XXXRTH  We could use a static event...
442088Ssos */
452088Ssos
462088Ssosstatic char hex_digits[] = {
472088Ssos	'0', '1', '2', '3', '4', '5', '6', '7',
482088Ssos	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
492088Ssos};
502088Ssos
512088Ssosisc_result_t
522088Ssosdns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
532088Ssos			 dns_name_t *name)
542088Ssos{
552088Ssos	/*
562088Ssos	 * We dropped bitstring labels, so all lookups will use nibbles.
572088Ssos	 */
582088Ssos	UNUSED(nibble);
592088Ssos
602088Ssos	return (dns_byaddr_createptrname2(address,
612088Ssos					  DNS_BYADDROPT_IPV6INT, name));
622088Ssos}
632088Ssos
642088Ssosisc_result_t
652088Ssosdns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
662088Ssos			  dns_name_t *name)
672088Ssos{
682088Ssos	char textname[128];
692088Ssos	unsigned char *bytes;
702088Ssos	int i;
712088Ssos	char *cp;
722088Ssos	isc_buffer_t buffer;
732088Ssos	unsigned int len;
742088Ssos
752088Ssos	REQUIRE(address != NULL);
762088Ssos
772088Ssos	/*
782088Ssos	 * We create the text representation and then convert to a
792088Ssos	 * dns_name_t.  This is not maximally efficient, but it keeps all
802088Ssos	 * of the knowledge of wire format in the dns_name_ routines.
812088Ssos	 */
822088Ssos
832088Ssos	bytes = (unsigned char *)(&address->type);
842088Ssos	if (address->family == AF_INET) {
852088Ssos		(void)snprintf(textname, sizeof(textname),
862088Ssos			       "%u.%u.%u.%u.in-addr.arpa.",
872088Ssos			       (bytes[3] & 0xff),
882088Ssos			       (bytes[2] & 0xff),
892088Ssos			       (bytes[1] & 0xff),
902088Ssos			       (bytes[0] & 0xff));
912088Ssos	} else if (address->family == AF_INET6) {
922088Ssos		cp = textname;
932088Ssos		for (i = 15; i >= 0; i--) {
942088Ssos			*cp++ = hex_digits[bytes[i] & 0x0f];
952088Ssos			*cp++ = '.';
962088Ssos			*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
972088Ssos			*cp++ = '.';
982088Ssos		}
992088Ssos		if ((options & DNS_BYADDROPT_IPV6INT) != 0)
1002088Ssos			strcpy(cp, "ip6.int.");
1012088Ssos		else
1022088Ssos			strcpy(cp, "ip6.arpa.");
1032088Ssos	} else
1042088Ssos		return (ISC_R_NOTIMPLEMENTED);
1052088Ssos
1062088Ssos	len = (unsigned int)strlen(textname);
1072088Ssos	isc_buffer_init(&buffer, textname, len);
1082088Ssos	isc_buffer_add(&buffer, len);
1092088Ssos	return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
1102088Ssos}
1112088Ssos
1122088Ssos#ifdef BIND9
1132088Ssosstruct dns_byaddr {
1142088Ssos	/* Unlocked. */
115	unsigned int		magic;
116	isc_mem_t *		mctx;
117	isc_mutex_t		lock;
118	dns_fixedname_t		name;
119	/* Locked by lock. */
120	unsigned int		options;
121	dns_lookup_t *		lookup;
122	isc_task_t *		task;
123	dns_byaddrevent_t *	event;
124	isc_boolean_t		canceled;
125};
126
127#define BYADDR_MAGIC			ISC_MAGIC('B', 'y', 'A', 'd')
128#define VALID_BYADDR(b)			ISC_MAGIC_VALID(b, BYADDR_MAGIC)
129
130#define MAX_RESTARTS 16
131
132static inline isc_result_t
133copy_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
171static void
172lookup_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
194static void
195bevent_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
215isc_result_t
216dns_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
285void
286dns_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
300void
301dns_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}
317#endif /* BIND9 */
318