byaddr.c 7.58 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Bob Halley's avatar
Bob Halley committed
10
11
 */

12
13

/*! \file */
David Lawrence's avatar
David Lawrence committed
14

Bob Halley's avatar
Bob Halley committed
15
16
#include <config.h>

17
18
#include <stdbool.h>

Bob Halley's avatar
Bob Halley committed
19
#include <isc/mem.h>
20
#include <isc/netaddr.h>
21
#include <isc/print.h>
22
#include <isc/string.h>		/* Required for HP/UX (and others?) */
Bob Halley's avatar
Bob Halley committed
23
24
25
26
#include <isc/task.h>
#include <isc/util.h>

#include <dns/byaddr.h>
Bob Halley's avatar
Bob Halley committed
27
#include <dns/db.h>
Bob Halley's avatar
Bob Halley committed
28
#include <dns/events.h>
29
#include <dns/lookup.h>
Bob Halley's avatar
Bob Halley committed
30
#include <dns/rdata.h>
Bob Halley's avatar
Bob Halley committed
31
#include <dns/rdataset.h>
32
#include <dns/rdatastruct.h>
Bob Halley's avatar
Bob Halley committed
33
#include <dns/resolver.h>
34
#include <dns/result.h>
Bob Halley's avatar
Bob Halley committed
35
36
37
38
39
40
#include <dns/view.h>

/*
 * XXXRTH  We could use a static event...
 */

Bob Halley's avatar
Bob Halley committed
41
static char hex_digits[] = {
42
	'0', '1', '2', '3', '4', '5', '6', '7',
Bob Halley's avatar
Bob Halley committed
43
44
45
	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

46
isc_result_t
47
dns_byaddr_createptrname(isc_netaddr_t *address, bool nibble,
48
			 dns_name_t *name)
49
{
Mark Andrews's avatar
Mark Andrews committed
50
51
52
53
	/*
	 * We dropped bitstring labels, so all lookups will use nibbles.
	 */
	UNUSED(nibble);
54

Mark Andrews's avatar
Mark Andrews committed
55
56
	return (dns_byaddr_createptrname2(address,
					  DNS_BYADDROPT_IPV6INT, name));
57
58
59
60
61
}

isc_result_t
dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
			  dns_name_t *name)
62
{
Bob Halley's avatar
Bob Halley committed
63
64
65
66
67
68
	char textname[128];
	unsigned char *bytes;
	int i;
	char *cp;
	isc_buffer_t buffer;
	unsigned int len;
Bob Halley's avatar
Bob Halley committed
69

70
	REQUIRE(address != NULL);
Bob Halley's avatar
Bob Halley committed
71

Bob Halley's avatar
Bob Halley committed
72
73
74
75
76
	/*
	 * We create the text representation and then convert to a
	 * dns_name_t.  This is not maximally efficient, but it keeps all
	 * of the knowledge of wire format in the dns_name_ routines.
	 */
Bob Halley's avatar
Bob Halley committed
77

Bob Halley's avatar
Bob Halley committed
78
79
	bytes = (unsigned char *)(&address->type);
	if (address->family == AF_INET) {
80
81
		(void)snprintf(textname, sizeof(textname),
			       "%u.%u.%u.%u.in-addr.arpa.",
Mark Andrews's avatar
Mark Andrews committed
82
83
84
85
			       ((unsigned int)bytes[3] & 0xffU),
			       ((unsigned int)bytes[2] & 0xffU),
			       ((unsigned int)bytes[1] & 0xffU),
			       ((unsigned int)bytes[0] & 0xffU));
Bob Halley's avatar
Bob Halley committed
86
	} else if (address->family == AF_INET6) {
Evan Hunt's avatar
Evan Hunt committed
87
88
		size_t remaining;

Mark Andrews's avatar
Mark Andrews committed
89
90
91
92
93
94
		cp = textname;
		for (i = 15; i >= 0; i--) {
			*cp++ = hex_digits[bytes[i] & 0x0f];
			*cp++ = '.';
			*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
			*cp++ = '.';
Bob Halley's avatar
Bob Halley committed
95
		}
Evan Hunt's avatar
Evan Hunt committed
96
97
98
99
100
101
		remaining = sizeof(textname) - (cp - textname);
		if ((options & DNS_BYADDROPT_IPV6INT) != 0) {
			strlcpy(cp, "ip6.int.", remaining);
		} else {
			strlcpy(cp, "ip6.arpa.", remaining);
		}
Bob Halley's avatar
Bob Halley committed
102
	} else
103
		return (ISC_R_NOTIMPLEMENTED);
Bob Halley's avatar
Bob Halley committed
104
105

	len = (unsigned int)strlen(textname);
106
	isc_buffer_init(&buffer, textname, len);
Bob Halley's avatar
Bob Halley committed
107
	isc_buffer_add(&buffer, len);
108
	return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
109
110
}

111
112
113
114
115
116
117
118
119
120
121
struct dns_byaddr {
	/* Unlocked. */
	unsigned int		magic;
	isc_mem_t *		mctx;
	isc_mutex_t		lock;
	dns_fixedname_t		name;
	/* Locked by lock. */
	unsigned int		options;
	dns_lookup_t *		lookup;
	isc_task_t *		task;
	dns_byaddrevent_t *	event;
122
	bool		canceled;
123
124
125
126
127
128
129
};

#define BYADDR_MAGIC			ISC_MAGIC('B', 'y', 'A', 'd')
#define VALID_BYADDR(b)			ISC_MAGIC_VALID(b, BYADDR_MAGIC)

#define MAX_RESTARTS 16

Bob Halley's avatar
Bob Halley committed
130
static inline isc_result_t
131
copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
Bob Halley's avatar
Bob Halley committed
132
133
	isc_result_t result;
	dns_name_t *name;
134
	dns_rdata_t rdata = DNS_RDATA_INIT;
Bob Halley's avatar
Bob Halley committed
135
136
137
138

	/*
	 * The caller must be holding the byaddr's lock.
	 */
139

140
	result = dns_rdataset_first(rdataset);
Bob Halley's avatar
Bob Halley committed
141
	while (result == ISC_R_SUCCESS) {
142
		dns_rdata_ptr_t ptr;
143
		dns_rdataset_current(rdataset, &rdata);
144
145
146
		result = dns_rdata_tostruct(&rdata, &ptr, NULL);
		if (result != ISC_R_SUCCESS)
			return (result);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
147
		name = isc_mem_get(byaddr->mctx, sizeof(*name));
148
149
		if (name == NULL) {
			dns_rdata_freestruct(&ptr);
Bob Halley's avatar
Bob Halley committed
150
			return (ISC_R_NOMEMORY);
151
		}
Bob Halley's avatar
Bob Halley committed
152
		dns_name_init(name, NULL);
153
154
		result = dns_name_dup(&ptr.ptr, byaddr->mctx, name);
		dns_rdata_freestruct(&ptr);
Bob Halley's avatar
Bob Halley committed
155
		if (result != ISC_R_SUCCESS) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
156
			isc_mem_put(byaddr->mctx, name, sizeof(*name));
Bob Halley's avatar
Bob Halley committed
157
158
159
			return (ISC_R_NOMEMORY);
		}
		ISC_LIST_APPEND(byaddr->event->names, name, link);
160
		dns_rdata_reset(&rdata);
161
		result = dns_rdataset_next(rdataset);
Bob Halley's avatar
Bob Halley committed
162
	}
163
	if (result == ISC_R_NOMORE)
Bob Halley's avatar
Bob Halley committed
164
		result = ISC_R_SUCCESS;
165

Bob Halley's avatar
Bob Halley committed
166
	return (result);
Bob Halley's avatar
Bob Halley committed
167
168
169
}

static void
170
lookup_done(isc_task_t *task, isc_event_t *event) {
171
	dns_byaddr_t *byaddr = event->ev_arg;
172
173
	dns_lookupevent_t *levent;
	isc_result_t result;
Bob Halley's avatar
Bob Halley committed
174

175
	REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
Bob Halley's avatar
Bob Halley committed
176
177
	REQUIRE(VALID_BYADDR(byaddr));
	REQUIRE(byaddr->task == task);
178
179
180

	UNUSED(task);

181
	levent = (dns_lookupevent_t *)event;
Bob Halley's avatar
Bob Halley committed
182

183
	if (levent->result == ISC_R_SUCCESS) {
184
		result = copy_ptr_targets(byaddr, levent->rdataset);
185
186
187
		byaddr->event->result = result;
	} else
		byaddr->event->result = levent->result;
188
189
	isc_event_free(&event);
	isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
Bob Halley's avatar
Bob Halley committed
190
}
Bob Halley's avatar
Bob Halley committed
191

Bob Halley's avatar
Bob Halley committed
192
193
194
195
196
197
static void
bevent_destroy(isc_event_t *event) {
	dns_byaddrevent_t *bevent;
	dns_name_t *name, *next_name;
	isc_mem_t *mctx;

198
199
	REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
	mctx = event->ev_destroy_arg;
Bob Halley's avatar
Bob Halley committed
200
201
202
203
204
205
	bevent = (dns_byaddrevent_t *)event;

	for (name = ISC_LIST_HEAD(bevent->names);
	     name != NULL;
	     name = next_name) {
		next_name = ISC_LIST_NEXT(name, link);
Mark Andrews's avatar
Mark Andrews committed
206
		ISC_LIST_UNLINK(bevent->names, name, link);
Bob Halley's avatar
Bob Halley committed
207
		dns_name_free(name, mctx);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
208
		isc_mem_put(mctx, name, sizeof(*name));
Bob Halley's avatar
Bob Halley committed
209
	}
210
	isc_mem_put(mctx, event, event->ev_size);
Bob Halley's avatar
Bob Halley committed
211
212
}

Bob Halley's avatar
Bob Halley committed
213
isc_result_t
Bob Halley's avatar
Bob Halley committed
214
dns_byaddr_create(isc_mem_t *mctx, isc_netaddr_t *address, dns_view_t *view,
Bob Halley's avatar
Bob Halley committed
215
216
217
218
219
		  unsigned int options, isc_task_t *task,
		  isc_taskaction_t action, void *arg, dns_byaddr_t **byaddrp)
{
	isc_result_t result;
	dns_byaddr_t *byaddr;
Bob Halley's avatar
Bob Halley committed
220
	isc_event_t *ievent;
Bob Halley's avatar
Bob Halley committed
221

Andreas Gustafsson's avatar
Andreas Gustafsson committed
222
	byaddr = isc_mem_get(mctx, sizeof(*byaddr));
Bob Halley's avatar
Bob Halley committed
223
224
	if (byaddr == NULL)
		return (ISC_R_NOMEMORY);
225
226
	byaddr->mctx = NULL;
	isc_mem_attach(mctx, &byaddr->mctx);
Bob Halley's avatar
Bob Halley committed
227
228
	byaddr->options = options;

Andreas Gustafsson's avatar
Andreas Gustafsson committed
229
	byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
Bob Halley's avatar
Bob Halley committed
230
231
232
233
	if (byaddr->event == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup_byaddr;
	}
Andreas Gustafsson's avatar
Andreas Gustafsson committed
234
	ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
Bob Halley's avatar
Bob Halley committed
235
236
		       DNS_EVENT_BYADDRDONE, action, arg, byaddr,
		       bevent_destroy, mctx);
Bob Halley's avatar
Bob Halley committed
237
238
	byaddr->event->result = ISC_R_FAILURE;
	ISC_LIST_INIT(byaddr->event->names);
Bob Halley's avatar
Bob Halley committed
239

Bob Halley's avatar
Bob Halley committed
240
241
242
	byaddr->task = NULL;
	isc_task_attach(task, &byaddr->task);

Bob Halley's avatar
Bob Halley committed
243
244
	result = isc_mutex_init(&byaddr->lock);
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
245
246
		goto cleanup_event;

247
248
	dns_fixedname_init(&byaddr->name);

249
250
	result = dns_byaddr_createptrname2(address, options,
					   dns_fixedname_name(&byaddr->name));
Bob Halley's avatar
Bob Halley committed
251
252
253
	if (result != ISC_R_SUCCESS)
		goto cleanup_lock;

254
255
256
257
258
259
260
	byaddr->lookup = NULL;
	result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
				   dns_rdatatype_ptr, view, 0, task,
				   lookup_done, byaddr, &byaddr->lookup);
	if (result != ISC_R_SUCCESS)
		goto cleanup_lock;

261
	byaddr->canceled = false;
Bob Halley's avatar
Bob Halley committed
262
	byaddr->magic = BYADDR_MAGIC;
263

Bob Halley's avatar
Bob Halley committed
264
265
	*byaddrp = byaddr;

Bob Halley's avatar
Bob Halley committed
266
267
268
	return (ISC_R_SUCCESS);

 cleanup_lock:
269
	DESTROYLOCK(&byaddr->lock);
Bob Halley's avatar
Bob Halley committed
270

Bob Halley's avatar
Bob Halley committed
271
272
273
274
275
 cleanup_event:
	ievent = (isc_event_t *)byaddr->event;
	isc_event_free(&ievent);
	byaddr->event = NULL;

Bob Halley's avatar
Bob Halley committed
276
277
	isc_task_detach(&byaddr->task);

Bob Halley's avatar
Bob Halley committed
278
 cleanup_byaddr:
279
	isc_mem_putanddetach(&mctx, byaddr, sizeof(*byaddr));
Bob Halley's avatar
Bob Halley committed
280
281
282
283
284
285

	return (result);
}

void
dns_byaddr_cancel(dns_byaddr_t *byaddr) {
Bob Halley's avatar
Bob Halley committed
286
287
288
289
290
	REQUIRE(VALID_BYADDR(byaddr));

	LOCK(&byaddr->lock);

	if (!byaddr->canceled) {
291
		byaddr->canceled = true;
292
293
		if (byaddr->lookup != NULL)
			dns_lookup_cancel(byaddr->lookup);
Bob Halley's avatar
Bob Halley committed
294
295
296
	}

	UNLOCK(&byaddr->lock);
Bob Halley's avatar
Bob Halley committed
297
298
299
300
}

void
dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
Bob Halley's avatar
Bob Halley committed
301
	dns_byaddr_t *byaddr;
Bob Halley's avatar
Bob Halley committed
302

Bob Halley's avatar
Bob Halley committed
303
304
305
306
	REQUIRE(byaddrp != NULL);
	byaddr = *byaddrp;
	REQUIRE(VALID_BYADDR(byaddr));
	REQUIRE(byaddr->event == NULL);
Bob Halley's avatar
Bob Halley committed
307
	REQUIRE(byaddr->task == NULL);
308
	dns_lookup_destroy(&byaddr->lookup);
Bob Halley's avatar
Bob Halley committed
309

310
	DESTROYLOCK(&byaddr->lock);
Bob Halley's avatar
Bob Halley committed
311
	byaddr->magic = 0;
312
	isc_mem_putanddetach(&byaddr->mctx, byaddr, sizeof(*byaddr));
Bob Halley's avatar
Bob Halley committed
313
314
315

	*byaddrp = NULL;
}