1/*
2 * Copyright (C) 2004, 2005, 2007, 2009, 2013  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 = NULL;
228	isc_mem_attach(mctx, &byaddr->mctx);
229	byaddr->options = options;
230
231	byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
232	if (byaddr->event == NULL) {
233		result = ISC_R_NOMEMORY;
234		goto cleanup_byaddr;
235	}
236	ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
237		       DNS_EVENT_BYADDRDONE, action, arg, byaddr,
238		       bevent_destroy, mctx);
239	byaddr->event->result = ISC_R_FAILURE;
240	ISC_LIST_INIT(byaddr->event->names);
241
242	byaddr->task = NULL;
243	isc_task_attach(task, &byaddr->task);
244
245	result = isc_mutex_init(&byaddr->lock);
246	if (result != ISC_R_SUCCESS)
247		goto cleanup_event;
248
249	dns_fixedname_init(&byaddr->name);
250
251	result = dns_byaddr_createptrname2(address, options,
252					   dns_fixedname_name(&byaddr->name));
253	if (result != ISC_R_SUCCESS)
254		goto cleanup_lock;
255
256	byaddr->lookup = NULL;
257	result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
258				   dns_rdatatype_ptr, view, 0, task,
259				   lookup_done, byaddr, &byaddr->lookup);
260	if (result != ISC_R_SUCCESS)
261		goto cleanup_lock;
262
263	byaddr->canceled = ISC_FALSE;
264	byaddr->magic = BYADDR_MAGIC;
265
266	*byaddrp = byaddr;
267
268	return (ISC_R_SUCCESS);
269
270 cleanup_lock:
271	DESTROYLOCK(&byaddr->lock);
272
273 cleanup_event:
274	ievent = (isc_event_t *)byaddr->event;
275	isc_event_free(&ievent);
276	byaddr->event = NULL;
277
278	isc_task_detach(&byaddr->task);
279
280 cleanup_byaddr:
281	isc_mem_putanddetach(&mctx, byaddr, sizeof(*byaddr));
282
283	return (result);
284}
285
286void
287dns_byaddr_cancel(dns_byaddr_t *byaddr) {
288	REQUIRE(VALID_BYADDR(byaddr));
289
290	LOCK(&byaddr->lock);
291
292	if (!byaddr->canceled) {
293		byaddr->canceled = ISC_TRUE;
294		if (byaddr->lookup != NULL)
295			dns_lookup_cancel(byaddr->lookup);
296	}
297
298	UNLOCK(&byaddr->lock);
299}
300
301void
302dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
303	dns_byaddr_t *byaddr;
304
305	REQUIRE(byaddrp != NULL);
306	byaddr = *byaddrp;
307	REQUIRE(VALID_BYADDR(byaddr));
308	REQUIRE(byaddr->event == NULL);
309	REQUIRE(byaddr->task == NULL);
310	dns_lookup_destroy(&byaddr->lookup);
311
312	DESTROYLOCK(&byaddr->lock);
313	byaddr->magic = 0;
314	isc_mem_putanddetach(&byaddr->mctx, byaddr, sizeof(*byaddr));
315
316	*byaddrp = NULL;
317}
318#endif /* BIND9 */
319