summaryrefslogtreecommitdiff
path: root/lib/libc/asr/hostaddr_async.c
blob: ecea827186a3245e994c628cc517104afdedc009 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*	$OpenBSD: hostaddr_async.c,v 1.1 2012/04/14 09:24:18 eric Exp $	*/
/*
 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/uio.h>

#include <arpa/nameser.h>
        
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "asr.h"
#include "asr_private.h"

static int hostaddr_async_run(struct async *, struct async_res *);
static int sockaddr_from_rr(struct sockaddr *, struct rr *);

/*
 * This API function allows to iterate over host addresses, for the given
 * family which, must be AF_INET, AF_INET6, or AF_UNSPEC (in which case
 * the family lookup list from the resolver is used). The strategy is to
 * return all addresses found for the first DB that returned at least one
 * address.
 *
 * Flags can be 0, or one of AI_CANONNAME or AI_FQDN. If set, the
 * canonical name will be returned along with the address.
 */
struct async *
hostaddr_async_ctx(const char *name, int family, int flags, struct asr_ctx *ac)
{
	struct async	*as;
	char		 buf[MAXDNAME];

#ifdef DEBUG
	asr_printf("asr: hostaddr_async_ctx(\"%s\", %i)\n", name, family);
#endif
	if (asr_domcat(name, NULL, buf, sizeof buf) == 0) {
		errno = EINVAL;
		return (NULL);
	}

	if ((as = async_new(ac, ASR_HOSTADDR)) == NULL)
		goto err; /* errno set */
	as->as_run = hostaddr_async_run;

	as->as.host.aiflags = flags;
	as->as.host.family = family;
	as->as.host.name = strdup(buf);
	if (as->as.host.name == NULL)
		goto err; /* errno set */

	return (as);
    err:
	if (as)
		async_free(as);
	return (NULL);
}

static int
hostaddr_async_run(struct async *as, struct async_res *ar)
{
	struct packed	 p;
	struct header	 h;
	struct query	 q;
	struct rr	 rr;
	char		 buf[MAXDNAME], *c, *name;
	int		 i, n, family, type, r;

    next:
	switch(as->as_state) {

	case ASR_STATE_INIT:

		if (as->as.host.family != AF_INET &&
		    as->as.host.family != AF_INET6 &&
		    as->as.host.family != AF_UNSPEC) {
			ar->ar_gai_errno = EAI_FAMILY;
			async_set_state(as, ASR_STATE_HALT);
			break;
		}

		as->as_count = 0;
		as->as_db_idx = 0;
		async_set_state(as, ASR_STATE_NEXT_DB);
		break;

	case ASR_STATE_NEXT_FAMILY:

		as->as_family_idx += 1;
		if (as->as.host.family != AF_UNSPEC || AS_FAMILY(as) == -1) {
			/* The family was specified, or we have tried all
			 * families with this DB.
			 */
			if (as->as_count) {
				ar->ar_errno = 0;
				ar->ar_gai_errno = 0;
				async_set_state(as, ASR_STATE_HALT);
			} else
				async_set_state(as, ASR_STATE_NEXT_DB);
			break;
		}

		async_set_state(as, ASR_STATE_LOOKUP_FAMILY);
		break;

	case ASR_STATE_LOOKUP_FAMILY:

		async_set_state(as, ASR_STATE_SAME_DB);
		break;

	case ASR_STATE_NEXT_DB:

		if (asr_iter_db(as) == -1) {
			async_set_state(as, ASR_STATE_NOT_FOUND);
			break;
		}
		as->as_family_idx = 0;
		/* FALLTHROUGH */

	case ASR_STATE_SAME_DB:

		/* Query the current DB again. */

		switch(AS_DB(as)) {
		case ASR_DB_DNS:

			family = (as->as.host.family == AF_UNSPEC) ?
			    AS_FAMILY(as) : as->as.host.family;
			type = (family == AF_INET6) ? T_AAAA : T_A;
			name = as->as.host.name;
			as->as.host.subq = res_query_async_ctx(name, C_IN,
			    type, NULL, 0, as->as_ctx);
			if (as->as.host.subq == NULL) {
				ar->ar_errno = errno;
				ar->ar_h_errno = NETDB_INTERNAL;
				if (errno == ENOMEM)
					ar->ar_gai_errno = EAI_MEMORY;
				else
					ar->ar_gai_errno = EAI_FAIL;
				async_set_state(as, ASR_STATE_HALT);
				break;
			}
			async_set_state(as, ASR_STATE_SUBQUERY);
			break;

		case ASR_DB_FILE:

			as->as.host.file = fopen(as->as_ctx->ac_hostfile, "r");
			if (as->as.host.file == NULL)
				async_set_state(as, ASR_STATE_NEXT_DB);
			else
				async_set_state(as, ASR_STATE_READ_FILE);
			break;

		default:
			async_set_state(as, ASR_STATE_NEXT_DB);
		}
		break;

	case ASR_STATE_SUBQUERY:
		if ((r = async_run(as->as.host.subq, ar)) == ASYNC_COND)
			return (ASYNC_COND);
		as->as.host.subq = NULL;

		if (ar->ar_datalen == -1) {
			async_set_state(as, ASR_STATE_NEXT_DB);
			break;
		}

		as->as.host.pkt = ar->ar_data;
		as->as.host.pktlen = ar->ar_datalen;
		packed_init(&p, as->as.host.pkt, as->as.host.pktlen);
		unpack_header(&p, &h);
		for(; h.qdcount; h.qdcount--)
			unpack_query(&p, &q);
		as->as.host.pktpos = p.offset;
		as->as.host.ancount = h.ancount;
		as->as.host.class = q.q_class;
		as->as.host.type = q.q_type;
		async_set_state(as, ASR_STATE_READ_RR);
		break;

	case ASR_STATE_READ_RR:

		/* When done with this NS, try with next family */
		if (as->as.host.ancount == 0) {
			free(as->as.host.pkt);
			as->as.host.pkt = NULL;
			async_set_state(as, ASR_STATE_NEXT_FAMILY);
			break;
		}

		/* Continue reading the packet where we left it. */
		packed_init(&p, as->as.host.pkt, as->as.host.pktlen);
		p.offset = as->as.host.pktpos;
		unpack_rr(&p, &rr);
		as->as.host.pktpos = p.offset;
		as->as.host.ancount -= 1;
		if (rr.rr_type == as->as.host.type &&
		    rr.rr_class == as->as.host.class) {
			as->as_count += 1;
			ar->ar_count = as->as_count;
			sockaddr_from_rr(&ar->ar_sa.sa, &rr);
			if (as->as.host.aiflags & AI_CANONNAME)
				c = asr_strdname(rr.rr_dname, buf,
				    sizeof buf);
			else if (as->as.host.aiflags & AI_FQDN) {
				strlcpy(buf, as->as.host.name, sizeof buf);
				c = buf;
			} else
				c = NULL;
			if (c) {
				if (c[strlen(c) - 1] == '.')
					c[strlen(c) - 1] = '\0';
				ar->ar_cname = strdup(c);
			} else
				ar->ar_cname = NULL;
			return (ASYNC_YIELD);
		}
		break;

	case ASR_STATE_READ_FILE:

		/* When done with the file, try next family. */
		n = asr_parse_namedb_line(as->as.host.file, as->as.host.tokens,
		    MAXTOKEN);
		if (n == -1) {
			fclose(as->as.host.file);
			as->as.host.file = NULL;
			async_set_state(as, ASR_STATE_NEXT_FAMILY);
			break;
		}

		for (i = 1; i < n; i++) {
			if (strcasecmp(as->as.host.name,
			    as->as.host.tokens[i]))
				continue;

			family = as->as.host.family;
			if (family == AF_UNSPEC)
				family = AS_FAMILY(as);

			if (sockaddr_from_str(&ar->ar_sa.sa, family,
			    as->as.host.tokens[0]) == -1)
				continue;

			if (as->as.host.aiflags & AI_CANONNAME) {
				strlcpy(buf, as->as.host.tokens[1],
				    sizeof buf);
				c = buf;
			} else if (as->as.host.aiflags & AI_FQDN) {
				strlcpy(buf, as->as.host.name, sizeof buf);
				c = buf;
			} else
				c = NULL;
			if (c) {
				if (c[strlen(c) - 1] == '.')
					c[strlen(c) - 1] = '\0';
				ar->ar_cname = strdup(c);
			} else
				ar->ar_cname = NULL;
			as->as_count += 1;
			ar->ar_count = as->as_count;
			return (ASYNC_YIELD);
		}
		break;

	case ASR_STATE_NOT_FOUND:
		/* XXX the exact error depends on what query/send returned */
		ar->ar_errno = 0;
		ar->ar_gai_errno = EAI_NODATA;
		async_set_state(as, ASR_STATE_HALT);
		break;
	
	case ASR_STATE_HALT:

		ar->ar_count = as->as_count;
		if (ar->ar_count) {
			ar->ar_errno = 0;
			ar->ar_gai_errno = 0;
		}
		return (ASYNC_DONE);

	default:
		ar->ar_errno = EOPNOTSUPP;
		ar->ar_gai_errno = EAI_SYSTEM;
		async_set_state(as, ASR_STATE_HALT);
		break;
	}
	goto next;
}

static int
sockaddr_from_rr(struct sockaddr *sa, struct rr *rr)
{
	struct sockaddr_in	*sin;
	struct sockaddr_in6	*sin6;

	if (rr->rr_class != C_IN)
		return (-1);

	switch (rr->rr_type) {
	case T_A:
		sin = (struct sockaddr_in*)sa;
		memset(sin, 0, sizeof *sin);
		sin->sin_len = sizeof *sin;
		sin->sin_family = PF_INET;
		sin->sin_addr = rr->rr.in_a.addr;
		sin->sin_port = 0;
		return (0);
	case T_AAAA:
		sin6 = (struct sockaddr_in6*)sa;
		memset(sin6, 0, sizeof *sin6);
		sin6->sin6_len = sizeof *sin6;
		sin6->sin6_family = PF_INET6;
		sin6->sin6_addr = rr->rr.in_aaaa.addr6;
		sin6->sin6_port = 0;
		return (0);

	default:
		break;
	}

	return (-1);
}