summaryrefslogtreecommitdiffstats
path: root/bin/named/lwdgnba.c
blob: 9e600f507f158a2e71716087746b58be2c46f721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * 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/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/* $Id: lwdgnba.c,v 1.22 2008/01/14 23:46:56 tbox Exp $ */

/*! \file */

#include <config.h>

#include <isc/socket.h>
#include <isc/string.h>		/* Required for HP/UX (and others?) */
#include <isc/util.h>

#include <dns/adb.h>
#include <dns/byaddr.h>
#include <dns/result.h>

#include <named/types.h>
#include <named/lwdclient.h>

static void start_byaddr(ns_lwdclient_t *);

static void
byaddr_done(isc_task_t *task, isc_event_t *event) {
	ns_lwdclient_t *client;
	ns_lwdclientmgr_t *cm;
	dns_byaddrevent_t *bevent;
	int lwres;
	lwres_buffer_t lwb;
	dns_name_t *name;
	isc_result_t result;
	lwres_result_t lwresult;
	isc_region_t r;
	isc_buffer_t b;
	lwres_gnbaresponse_t *gnba;
	uint16_t naliases;

	UNUSED(task);

	lwb.base = NULL;
	client = event->ev_arg;
	cm = client->clientmgr;
	INSIST(client->byaddr == (dns_byaddr_t *)event->ev_sender);

	bevent = (dns_byaddrevent_t *)event;
	gnba = &client->gnba;

	ns_lwdclient_log(50, "byaddr event result = %s",
			 isc_result_totext(bevent->result));

	result = bevent->result;
	if (result != ISC_R_SUCCESS) {
		dns_byaddr_destroy(&client->byaddr);
		isc_event_free(&event);
		bevent = NULL;

		if (client->na.family != AF_INET6 ||
		    (client->options & DNS_BYADDROPT_IPV6INT) != 0) {
			if (result == DNS_R_NCACHENXDOMAIN ||
			    result == DNS_R_NCACHENXRRSET ||
			    result == DNS_R_NXDOMAIN ||
			    result == DNS_R_NXRRSET)
				lwresult = LWRES_R_NOTFOUND;
			else
				lwresult = LWRES_R_FAILURE;
			ns_lwdclient_errorpktsend(client, lwresult);
			return;
		}

		/*
		 * Fall back to ip6.int reverse if the default ip6.arpa
		 * fails.
		 */
		client->options |= DNS_BYADDROPT_IPV6INT;

		start_byaddr(client);
		return;
	}

	for (name = ISC_LIST_HEAD(bevent->names);
	     name != NULL;
	     name = ISC_LIST_NEXT(name, link))
	{
		b = client->recv_buffer;

		result = dns_name_totext(name, true, &client->recv_buffer);
		if (result != ISC_R_SUCCESS)
			goto out;
		ns_lwdclient_log(50, "found name '%.*s'",
				 (int)(client->recv_buffer.used - b.used),
				 (char *)(b.base) + b.used);
		if (gnba->realname == NULL) {
			gnba->realname = (char *)(b.base) + b.used;
			gnba->realnamelen = client->recv_buffer.used - b.used;
		} else {
			naliases = gnba->naliases;
			if (naliases >= LWRES_MAX_ALIASES)
				break;
			gnba->aliases[naliases] = (char *)(b.base) + b.used;
			gnba->aliaslen[naliases] =
				client->recv_buffer.used - b.used;
			gnba->naliases++;
		}
	}

	dns_byaddr_destroy(&client->byaddr);
	isc_event_free(&event);

	/*
	 * Render the packet.
	 */
	client->pkt.recvlength = LWRES_RECVLENGTH;
	client->pkt.authtype = 0; /* XXXMLG */
	client->pkt.authlength = 0;
	client->pkt.result = LWRES_R_SUCCESS;

	lwres = lwres_gnbaresponse_render(cm->lwctx,
					  gnba, &client->pkt, &lwb);
	if (lwres != LWRES_R_SUCCESS)
		goto out;

	r.base = lwb.base;
	r.length = lwb.used;
	client->sendbuf = r.base;
	client->sendlength = r.length;
	result = ns_lwdclient_sendreply(client, &r);
	if (result != ISC_R_SUCCESS)
		goto out;

	NS_LWDCLIENT_SETSEND(client);

	return;

 out:
	if (client->byaddr != NULL)
		dns_byaddr_destroy(&client->byaddr);
	if (lwb.base != NULL)
		lwres_context_freemem(cm->lwctx,
				      lwb.base, lwb.length);

	if (event != NULL)
		isc_event_free(&event);
}

static void
start_byaddr(ns_lwdclient_t *client) {
	isc_result_t result;
	ns_lwdclientmgr_t *cm;

	cm = client->clientmgr;

	INSIST(client->byaddr == NULL);

	result = dns_byaddr_create(cm->mctx, &client->na, cm->view,
				   client->options, cm->task, byaddr_done,
				   client, &client->byaddr);
	if (result != ISC_R_SUCCESS) {
		ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
		return;
	}
}

static void
init_gnba(ns_lwdclient_t *client) {
	int i;

	/*
	 * Initialize the real name and alias arrays in the reply we're
	 * going to build up.
	 */
	for (i = 0; i < LWRES_MAX_ALIASES; i++) {
		client->aliases[i] = NULL;
		client->aliaslen[i] = 0;
	}
	for (i = 0; i < LWRES_MAX_ADDRS; i++) {
		client->addrs[i].family = 0;
		client->addrs[i].length = 0;
		memset(client->addrs[i].address, 0, LWRES_ADDR_MAXLEN);
		LWRES_LINK_INIT(&client->addrs[i], link);
	}

	client->gnba.naliases = 0;
	client->gnba.realname = NULL;
	client->gnba.aliases = client->aliases;
	client->gnba.realnamelen = 0;
	client->gnba.aliaslen = client->aliaslen;
	client->gnba.base = NULL;
	client->gnba.baselen = 0;
	isc_buffer_init(&client->recv_buffer, client->buffer, LWRES_RECVLENGTH);
}

void
ns_lwdclient_processgnba(ns_lwdclient_t *client, lwres_buffer_t *b) {
	lwres_gnbarequest_t *req;
	isc_result_t result;
	isc_sockaddr_t sa;
	ns_lwdclientmgr_t *cm;

	REQUIRE(NS_LWDCLIENT_ISRECVDONE(client));
	INSIST(client->byaddr == NULL);

	cm = client->clientmgr;
	req = NULL;

	result = lwres_gnbarequest_parse(cm->lwctx,
					 b, &client->pkt, &req);
	if (result != LWRES_R_SUCCESS)
		goto out;

	client->options = 0;
	if (req->addr.family == LWRES_ADDRTYPE_V4) {
		client->na.family = AF_INET;
		if (req->addr.length != 4)
			goto out;
		memmove(&client->na.type.in, req->addr.address, 4);
	} else if (req->addr.family == LWRES_ADDRTYPE_V6) {
		client->na.family = AF_INET6;
		if (req->addr.length != 16)
			goto out;
		memmove(&client->na.type.in6, req->addr.address, 16);
	} else {
		goto out;
	}
	isc_sockaddr_fromnetaddr(&sa, &client->na, 53);

	ns_lwdclient_log(50, "client %p looking for addrtype %08x",
			 client, req->addr.family);

	/*
	 * We no longer need to keep this around.
	 */
	lwres_gnbarequest_free(cm->lwctx, &req);

	/*
	 * Initialize the real name and alias arrays in the reply we're
	 * going to build up.
	 */
	init_gnba(client);
	client->options = 0;

	/*
	 * Start the find.
	 */
	start_byaddr(client);

	return;

	/*
	 * We're screwed.  Return an error packet to our caller.
	 */
 out:
	if (req != NULL)
		lwres_gnbarequest_free(cm->lwctx, &req);

	ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
}