PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/bind9/lib/dns/byaddr.c

https://bitbucket.org/freebsd/freebsd-head/
C | 317 lines | 230 code | 53 blank | 34 comment | 42 complexity | b429fbe86ef78054405946d583cabf01 MD5 | raw file
  1/*
  2 * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
  3 * Copyright (C) 2000-2003  Internet Software Consortium.
  4 *
  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.
  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.41 2009/09/02 23:48:02 tbox Exp $ */
 19
 20/*! \file */
 21
 22#include <config.h>
 23
 24#include <isc/mem.h>
 25#include <isc/netaddr.h>
 26#include <isc/print.h>
 27#include <isc/string.h>		/* Required for HP/UX (and others?) */
 28#include <isc/task.h>
 29#include <isc/util.h>
 30
 31#include <dns/byaddr.h>
 32#include <dns/db.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>
 40#include <dns/view.h>
 41
 42/*
 43 * XXXRTH  We could use a static event...
 44 */
 45
 46static char hex_digits[] = {
 47	'0', '1', '2', '3', '4', '5', '6', '7',
 48	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
 49};
 50
 51isc_result_t
 52dns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
 53			 dns_name_t *name)
 54{
 55	/*
 56	 * We dropped bitstring labels, so all lookups will use nibbles.
 57	 */
 58	UNUSED(nibble);
 59
 60	return (dns_byaddr_createptrname2(address,
 61					  DNS_BYADDROPT_IPV6INT, name));
 62}
 63
 64isc_result_t
 65dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
 66			  dns_name_t *name)
 67{
 68	char textname[128];
 69	unsigned char *bytes;
 70	int i;
 71	char *cp;
 72	isc_buffer_t buffer;
 73	unsigned int len;
 74
 75	REQUIRE(address != NULL);
 76
 77	/*
 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.
 81	 */
 82
 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.",
 87			       (bytes[3] & 0xff),
 88			       (bytes[2] & 0xff),
 89			       (bytes[1] & 0xff),
 90			       (bytes[0] & 0xff));
 91	} else if (address->family == AF_INET6) {
 92		cp = textname;
 93		for (i = 15; i >= 0; i--) {
 94			*cp++ = hex_digits[bytes[i] & 0x0f];
 95			*cp++ = '.';
 96			*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
 97			*cp++ = '.';
 98		}
 99		if ((options & DNS_BYADDROPT_IPV6INT) != 0)
100			strcpy(cp, "ip6.int.");
101		else
102			strcpy(cp, "ip6.arpa.");
103	} else
104		return (ISC_R_NOTIMPLEMENTED);
105
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));
110}
111
112#ifdef BIND9
113struct dns_byaddr {
114	/* 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 */