dns_ho.c 27.1 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
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
/*
 * Copyright (c) 1985, 1988, 1993
 *    The Regents of the University of California.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 * 	This product includes software developed by the University of
 * 	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
Mark Andrews's avatar
Mark Andrews committed
35
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
36
37
38
39
40
41
 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
 *
 * 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.
 *
Mark Andrews's avatar
Mark Andrews committed
42
43
44
45
46
47
48
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC 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.
Mark Andrews's avatar
Mark Andrews committed
49
50
51
52
53
54
 */

/* from gethostnamadr.c	8.1 (Berkeley) 6/4/93 */
/* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */

#if defined(LIBC_SCCS) && !defined(lint)
55
static const char rcsid[] = "$Id: dns_ho.c,v 1.21 2006/12/07 03:51:29 marka Exp $";
Mark Andrews's avatar
Mark Andrews committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#endif /* LIBC_SCCS and not lint */

/* Imports. */

#include "port_before.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>

#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
77
#include <syslog.h>
Mark Andrews's avatar
Mark Andrews committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <isc/memcluster.h>
#include <irs.h>

#include "port_after.h"

#include "irs_p.h"
#include "dns_p.h"

#ifdef SPRINTF_CHAR
# define SPRINTF(x) strlen(sprintf/**/x)
#else
# define SPRINTF(x) sprintf x
#endif

/* Definitions. */

#define	MAXALIASES	35
#define	MAXADDRS	35

98
#define MAXPACKET (65535)	/*%< Maximum TCP message size */
Mark Andrews's avatar
Mark Andrews committed
99
100
101
102
103
104
105
106
107
108
109
110
111
#define BOUNDS_CHECK(ptr, count) \
	if ((ptr) + (count) > eom) { \
		had_error++; \
		continue; \
	} else (void)0

typedef union {
	HEADER hdr;
	u_char buf[MAXPACKET];
} querybuf;

struct dns_res_target {
	struct dns_res_target *next;
112
113
114
115
116
117
	querybuf qbuf;		/*%< query buffer */
	u_char *answer;		/*%< buffer to put answer */
	int anslen;		/*%< size of answer buffer */
	int qclass, qtype;	/*%< class and type of query */
	int action;		/*%< condition whether query is really issued */
	char qname[MAXDNAME +1]; /*%< domain name */
Mark Andrews's avatar
Mark Andrews committed
118
#if 0
119
	int n;			/*%< result length */
Mark Andrews's avatar
Mark Andrews committed
120
121
122
123
124
125
126
127
128
129
#endif
};
enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE};
enum {RESQRY_SUCCESS, RESQRY_FAIL};

struct pvt {
	struct hostent	host;
	char *		h_addr_ptrs[MAXADDRS + 1];
	char *		host_aliases[MAXALIASES];
	char		hostbuf[8*1024];
130
	u_char		host_addr[16];	/*%< IPv4 or IPv6 */
Mark Andrews's avatar
Mark Andrews committed
131
132
133
134
135
136
137
138
139
140
141
142
	struct __res_state  *res;
	void		(*free_res)(void *);
};

typedef union {
	int32_t al;
	char ac;
} align;

static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
/* Note: the IPv6 loopback address is in the "tunnel" space */
143
static const u_char v6local[] = { 0,0, 0,1 }; /*%< last 4 bytes of IPv6 addr */
Mark Andrews's avatar
Mark Andrews committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* Forwards. */

static void		ho_close(struct irs_ho *this);
static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
				   int af);
static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
				  int len, int af);
static struct hostent *	ho_next(struct irs_ho *this);
static void		ho_rewind(struct irs_ho *this);
static void		ho_minimize(struct irs_ho *this);
static struct __res_state * ho_res_get(struct irs_ho *this);
static void		ho_res_set(struct irs_ho *this,
				   struct __res_state *res,
				   void (*free_res)(void *));
static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
				     const struct addrinfo *pai);

static void		map_v4v6_hostent(struct hostent *hp, char **bp,
163
					 char *ep);
Mark Andrews's avatar
Mark Andrews committed
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
static void		addrsort(res_state, char **, int);
static struct hostent *	gethostans(struct irs_ho *this,
				   const u_char *ansbuf, int anslen,
				   const char *qname, int qtype,
				   int af, int size,
				   struct addrinfo **ret_aip,
				   const struct addrinfo *pai);
static int add_hostent(struct pvt *pvt, char *bp, char **hap,
		       struct addrinfo *ai);
static int		init(struct irs_ho *this);

/* Exports. */

struct irs_ho *
irs_dns_ho(struct irs_acc *this) {
	struct irs_ho *ho;
	struct pvt *pvt;

	UNUSED(this);

	if (!(pvt = memget(sizeof *pvt))) {
		errno = ENOMEM;
		return (NULL);
	}
	memset(pvt, 0, sizeof *pvt);

	if (!(ho = memget(sizeof *ho))) {
		memput(pvt, sizeof *pvt);
		errno = ENOMEM;
		return (NULL);
	}
	memset(ho, 0x5e, sizeof *ho);
	ho->private = pvt;
	ho->close = ho_close;
	ho->byname = ho_byname;
	ho->byname2 = ho_byname2;
	ho->byaddr = ho_byaddr;
	ho->next = ho_next;
	ho->rewind = ho_rewind;
	ho->minimize = ho_minimize;
	ho->res_get = ho_res_get;
	ho->res_set = ho_res_set;
	ho->addrinfo = ho_addrinfo;
	return (ho);
}

/* Methods. */

static void
ho_close(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	ho_minimize(this);
	if (pvt->res && pvt->free_res)
		(*pvt->free_res)(pvt->res);
219
	memput(pvt, sizeof *pvt);
Mark Andrews's avatar
Mark Andrews committed
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
	memput(this, sizeof *this);
}

static struct hostent *
ho_byname(struct irs_ho *this, const char *name) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct hostent *hp;

	if (init(this) == -1)
		return (NULL);

	if (pvt->res->options & RES_USE_INET6) {
		hp = ho_byname2(this, name, AF_INET6);
		if (hp)
			return (hp);
	}
	return (ho_byname2(this, name, AF_INET));
}

static struct hostent *
ho_byname2(struct irs_ho *this, const char *name, int af)
{
	struct pvt *pvt = (struct pvt *)this->private;
	struct hostent *hp = NULL;
	int n, size;
	char tmp[NS_MAXDNAME];
	const char *cp;
	struct addrinfo ai;
248
	struct dns_res_target *q, *p;
Mark Andrews's avatar
Mark Andrews committed
249
250
251
252
253
	int querystate = RESQRY_FAIL;

	if (init(this) == -1)
		return (NULL);

Mark Andrews's avatar
Mark Andrews committed
254
	q = memget(sizeof(*q));
255
	if (q == NULL) {
Mark Andrews's avatar
Mark Andrews committed
256
257
258
259
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = ENOMEM;
		goto cleanup;
	}
260
	memset(q, 0, sizeof(*q));
Mark Andrews's avatar
Mark Andrews committed
261
262
263
264

	switch (af) {
	case AF_INET:
		size = INADDRSZ;
Mark Andrews's avatar
Mark Andrews committed
265
266
267
268
269
		q->qclass = C_IN;
		q->qtype = T_A;
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
270
271
272
		break;
	case AF_INET6:
		size = IN6ADDRSZ;
Mark Andrews's avatar
Mark Andrews committed
273
		q->qclass = C_IN;
274
		q->qtype = T_AAAA;
Mark Andrews's avatar
Mark Andrews committed
275
276
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
277
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
278
279
280
281
		break;
	default:
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = EAFNOSUPPORT;
Mark Andrews's avatar
Mark Andrews committed
282
283
		hp = NULL;
		goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
284
285
286
287
288
289
290
291
292
293
294
	}

	/*
	 * if there aren't any dots, it could be a user-level alias.
	 * this is also done in res_nquery() since we are not the only
	 * function that looks up host names.
	 */
	if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
						      tmp, sizeof tmp)))
		name = cp;

Mark Andrews's avatar
Mark Andrews committed
295
	for (p = q; p; p = p->next) {
Mark Andrews's avatar
Mark Andrews committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
		switch(p->action) {
		case RESTGT_DOALWAYS:
			break;
		case RESTGT_AFTERFAILURE:
			if (querystate == RESQRY_SUCCESS)
				continue;
			break;
		case RESTGT_IGNORE:
			continue;
		}

		if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
				     p->answer, p->anslen)) < 0) {
			querystate = RESQRY_FAIL;
			continue;
		}

		memset(&ai, 0, sizeof(ai));
		ai.ai_family = af;
		if ((hp = gethostans(this, p->answer, n, name, p->qtype,
				     af, size, NULL,
				     (const struct addrinfo *)&ai)) != NULL)
318
			goto cleanup;	/*%< no more loop is necessary */
Mark Andrews's avatar
Mark Andrews committed
319
320
321
322
		querystate = RESQRY_FAIL;
		continue;
	}

Mark Andrews's avatar
Mark Andrews committed
323
324
325
326
 cleanup:
	if (q != NULL)
		memput(q, sizeof(*q));
	return(hp);
Mark Andrews's avatar
Mark Andrews committed
327
328
329
330
331
332
333
334
}

static struct hostent *
ho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
{
	struct pvt *pvt = (struct pvt *)this->private;
	const u_char *uaddr = addr;
	char *qp;
Mark Andrews's avatar
Mark Andrews committed
335
	struct hostent *hp = NULL;
Mark Andrews's avatar
Mark Andrews committed
336
	struct addrinfo ai;
Mark Andrews's avatar
Mark Andrews committed
337
	struct dns_res_target *q, *q2, *p;
338
	int n, size, i;
Mark Andrews's avatar
Mark Andrews committed
339
340
341
342
343
	int querystate = RESQRY_FAIL;
	
	if (init(this) == -1)
		return (NULL);

Mark Andrews's avatar
Mark Andrews committed
344
345
346
347
348
349
350
	q = memget(sizeof(*q));
	q2 = memget(sizeof(*q2));
	if (q == NULL || q2 == NULL) {
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = ENOMEM;
		goto cleanup;
	}
351
352
	memset(q, 0, sizeof(*q));
	memset(q2, 0, sizeof(*q2));
Mark Andrews's avatar
Mark Andrews committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366

	if (af == AF_INET6 && len == IN6ADDRSZ &&
	    (!memcmp(uaddr, mapped, sizeof mapped) ||
           (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
            memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
		/* Unmap. */
		addr = (const char *)addr + sizeof mapped;
		uaddr += sizeof mapped;
		af = AF_INET;
		len = INADDRSZ;
	}
	switch (af) {
	case AF_INET:
		size = INADDRSZ;
Mark Andrews's avatar
Mark Andrews committed
367
368
369
370
371
		q->qclass = C_IN;
		q->qtype = T_PTR;
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
372
373
374
		break;
	case AF_INET6:
		size = IN6ADDRSZ;
Mark Andrews's avatar
Mark Andrews committed
375
376
377
378
379
		q->qclass = C_IN;
		q->qtype = T_PTR;
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
		q->next = q2;
Mark Andrews's avatar
pullup:    
Mark Andrews committed
380
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
381
382
383
384
		q2->qclass = C_IN;
		q2->qtype = T_PTR;
		q2->answer = q2->qbuf.buf;
		q2->anslen = sizeof(q2->qbuf);
Mark Andrews's avatar
Mark Andrews committed
385
		if ((pvt->res->options & RES_NO_NIBBLE2) != 0U)
Mark Andrews's avatar
Mark Andrews committed
386
			q2->action = RESTGT_IGNORE;
387
		else
Mark Andrews's avatar
Mark Andrews committed
388
			q2->action = RESTGT_AFTERFAILURE;
Mark Andrews's avatar
Mark Andrews committed
389
390
391
392
		break;
	default:
		errno = EAFNOSUPPORT;
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
Mark Andrews's avatar
Mark Andrews committed
393
394
		hp = NULL;
		goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
395
396
397
398
	}
	if (size > len) {
		errno = EINVAL;
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
Mark Andrews's avatar
Mark Andrews committed
399
400
		hp = NULL;
		goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
401
402
403
	}
	switch (af) {
	case AF_INET:
Mark Andrews's avatar
Mark Andrews committed
404
		qp = q->qname;
Mark Andrews's avatar
Mark Andrews committed
405
406
407
408
409
410
411
		(void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
			       (uaddr[3] & 0xff),
			       (uaddr[2] & 0xff),
			       (uaddr[1] & 0xff),
			       (uaddr[0] & 0xff));
		break;
	case AF_INET6:
Mark Andrews's avatar
Mark Andrews committed
412
		if (q->action != RESTGT_IGNORE) {
Mark Andrews's avatar
pullup:    
Mark Andrews committed
413
			const char *nibsuff = res_get_nibblesuffix(pvt->res);
Mark Andrews's avatar
Mark Andrews committed
414
			qp = q->qname;
Mark Andrews's avatar
pullup:    
Mark Andrews committed
415
			for (n = IN6ADDRSZ - 1; n >= 0; n--) {
416
				i = SPRINTF((qp, "%x.%x.",
Mark Andrews's avatar
pullup:    
Mark Andrews committed
417
418
					       uaddr[n] & 0xf,
					       (uaddr[n] >> 4) & 0xf));
Mark Andrews's avatar
pullup:    
Mark Andrews committed
419
				if (i != 4)
420
421
					abort();
				qp += i;
Mark Andrews's avatar
pullup:    
Mark Andrews committed
422
			}
Mark Andrews's avatar
pullup:    
Mark Andrews committed
423
424
425
426
427
428
429
430
			if (strlen(q->qname) + strlen(nibsuff) + 1 >
			    sizeof q->qname) {
				errno = ENAMETOOLONG;
				RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
				hp = NULL;
				goto cleanup;
			}
			strcpy(qp, nibsuff);	/* (checked) */
Mark Andrews's avatar
Mark Andrews committed
431
		}
Mark Andrews's avatar
Mark Andrews committed
432
		if (q2->action != RESTGT_IGNORE) {
Mark Andrews's avatar
pullup:    
Mark Andrews committed
433
			const char *nibsuff2 = res_get_nibblesuffix2(pvt->res);
Mark Andrews's avatar
Mark Andrews committed
434
			qp = q2->qname;
435
			for (n = IN6ADDRSZ - 1; n >= 0; n--) {
436
				i = SPRINTF((qp, "%x.%x.",
437
438
					       uaddr[n] & 0xf,
					       (uaddr[n] >> 4) & 0xf));
Mark Andrews's avatar
pullup:    
Mark Andrews committed
439
				if (i != 4)
440
441
					abort();
				qp += i;
442
			}
Mark Andrews's avatar
pullup:    
Mark Andrews committed
443
444
			if (strlen(q2->qname) + strlen(nibsuff2) + 1 >
			    sizeof q2->qname) {
Mark Andrews's avatar
pullup:    
Mark Andrews committed
445
446
447
448
449
450
				errno = ENAMETOOLONG;
				RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
				hp = NULL;
				goto cleanup;
			}
			strcpy(qp, nibsuff2);	/* (checked) */
Mark Andrews's avatar
Mark Andrews committed
451
452
453
454
455
456
		}
		break;
	default:
		abort();
	}

Mark Andrews's avatar
Mark Andrews committed
457
	for (p = q; p; p = p->next) {
Mark Andrews's avatar
Mark Andrews committed
458
459
460
461
462
463
464
465
466
467
468
		switch(p->action) {
		case RESTGT_DOALWAYS:
			break;
		case RESTGT_AFTERFAILURE:
			if (querystate == RESQRY_SUCCESS)
				continue;
			break;
		case RESTGT_IGNORE:
			continue;
		}

469
470
		if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
				    p->answer, p->anslen)) < 0) {
Mark Andrews's avatar
Mark Andrews committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
			querystate = RESQRY_FAIL;
			continue;
		}

		memset(&ai, 0, sizeof(ai));
		ai.ai_family = af;
		hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
				NULL, (const struct addrinfo *)&ai);
		if (!hp) {
			querystate = RESQRY_FAIL;
			continue;
		}
			
		memcpy(pvt->host_addr, addr, len);
		pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
		pvt->h_addr_ptrs[1] = NULL;
		if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
			map_v4v6_address((char*)pvt->host_addr,
					 (char*)pvt->host_addr);
			pvt->host.h_addrtype = AF_INET6;
			pvt->host.h_length = IN6ADDRSZ;
		}

		RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
495
		goto cleanup;	/*%< no more loop is necessary. */
Mark Andrews's avatar
Mark Andrews committed
496
	}
497
	hp = NULL; /*%< H_ERRNO was set by subroutines */
Mark Andrews's avatar
Mark Andrews committed
498
499
500
501
502
503
 cleanup:
	if (q != NULL)
		memput(q, sizeof(*q));
	if (q2 != NULL)
		memput(q2, sizeof(*q2));
	return(hp);
Mark Andrews's avatar
Mark Andrews committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
}

static struct hostent *
ho_next(struct irs_ho *this) {

	UNUSED(this);

	return (NULL);
}

static void
ho_rewind(struct irs_ho *this) {

	UNUSED(this);

	/* NOOP */
}

static void
ho_minimize(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	if (pvt->res)
		res_nclose(pvt->res);
}

static struct __res_state *
ho_res_get(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	if (!pvt->res) {
		struct __res_state *res;
		res = (struct __res_state *)malloc(sizeof *res);
		if (!res) {
			errno = ENOMEM;
			return (NULL);
		}
		memset(res, 0, sizeof *res);
		ho_res_set(this, res, free);
	}

	return (pvt->res);
}

/* XXX */
extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
					   const char *));

static struct addrinfo *
ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
{
	struct pvt *pvt = (struct pvt *)this->private;
	int n;
	char tmp[NS_MAXDNAME];
	const char *cp;
559
	struct dns_res_target *q, *q2, *p;
Mark Andrews's avatar
Mark Andrews committed
560
561
562
563
564
565
566
567
568
	struct addrinfo sentinel, *cur;
	int querystate = RESQRY_FAIL;

	if (init(this) == -1)
		return (NULL);

	memset(&sentinel, 0, sizeof(sentinel));
	cur = &sentinel;

Mark Andrews's avatar
Mark Andrews committed
569
570
	q = memget(sizeof(*q));
	q2 = memget(sizeof(*q2));
571
	if (q == NULL || q2 == NULL) {
Mark Andrews's avatar
Mark Andrews committed
572
573
574
575
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = ENOMEM;
		goto cleanup;
	}
576
577
	memset(q, 0, sizeof(*q2));
	memset(q2, 0, sizeof(*q2));
Mark Andrews's avatar
Mark Andrews committed
578

Mark Andrews's avatar
Mark Andrews committed
579
580
581
	switch (pai->ai_family) {
	case AF_UNSPEC:
		/* prefer IPv6 */
Mark Andrews's avatar
Mark Andrews committed
582
		q->qclass = C_IN;
583
		q->qtype = T_AAAA;
Mark Andrews's avatar
Mark Andrews committed
584
585
586
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
		q->next = q2;
587
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
588
		q2->qclass = C_IN;
589
		q2->qtype = T_A;
Mark Andrews's avatar
Mark Andrews committed
590
591
		q2->answer = q2->qbuf.buf;
		q2->anslen = sizeof(q2->qbuf);
592
		q2->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
593
594
		break;
	case AF_INET:
Mark Andrews's avatar
Mark Andrews committed
595
596
597
598
599
		q->qclass = C_IN;
		q->qtype = T_A;
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
600
601
		break;
	case AF_INET6:
Mark Andrews's avatar
Mark Andrews committed
602
		q->qclass = C_IN;
603
		q->qtype = T_AAAA;
Mark Andrews's avatar
Mark Andrews committed
604
605
		q->answer = q->qbuf.buf;
		q->anslen = sizeof(q->qbuf);
606
		q->action = RESTGT_DOALWAYS;
Mark Andrews's avatar
Mark Andrews committed
607
608
		break;
	default:
609
		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< better error? */
Mark Andrews's avatar
Mark Andrews committed
610
		goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
611
612
613
614
615
616
617
618
619
620
621
	}

	/*
	 * if there aren't any dots, it could be a user-level alias.
	 * this is also done in res_nquery() since we are not the only
	 * function that looks up host names.
	 */
	if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
						      tmp, sizeof tmp)))
		name = cp;

Mark Andrews's avatar
Mark Andrews committed
622
	for (p = q; p; p = p->next) {
Mark Andrews's avatar
Mark Andrews committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
		struct addrinfo *ai;

		switch(p->action) {
		case RESTGT_DOALWAYS:
			break;
		case RESTGT_AFTERFAILURE:
			if (querystate == RESQRY_SUCCESS)
				continue;
			break;
		case RESTGT_IGNORE:
			continue;
		}

		if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
				     p->answer, p->anslen)) < 0) {
			querystate = RESQRY_FAIL;
			continue;
		}
		(void)gethostans(this, p->answer, n, name, p->qtype,
642
				 pai->ai_family, /*%< XXX: meaningless */
Mark Andrews's avatar
Mark Andrews committed
643
644
645
646
				 0, &ai, pai);
		if (ai) {
			querystate = RESQRY_SUCCESS;
			cur->ai_next = ai;
647
			while (cur->ai_next)
Mark Andrews's avatar
Mark Andrews committed
648
				cur = cur->ai_next;
649
		} else
Mark Andrews's avatar
Mark Andrews committed
650
651
652
			querystate = RESQRY_FAIL;
	}

Mark Andrews's avatar
Mark Andrews committed
653
654
655
656
657
 cleanup:
	if (q != NULL)
		memput(q, sizeof(*q));
	if (q2 != NULL)
		memput(q2, sizeof(*q2));
Mark Andrews's avatar
Mark Andrews committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
	return(sentinel.ai_next);
}

static void
ho_res_set(struct irs_ho *this, struct __res_state *res,
		void (*free_res)(void *)) {
	struct pvt *pvt = (struct pvt *)this->private;

	if (pvt->res && pvt->free_res) {
		res_nclose(pvt->res);
		(*pvt->free_res)(pvt->res);
	}

	pvt->res = res;
	pvt->free_res = free_res;
}

/* Private. */

static struct hostent *
gethostans(struct irs_ho *this,
	   const u_char *ansbuf, int anslen, const char *qname, int qtype,
680
	   int af, int size,	/*!< meaningless for addrinfo cases  */
Mark Andrews's avatar
Mark Andrews committed
681
682
683
	   struct addrinfo **ret_aip, const struct addrinfo *pai)
{
	struct pvt *pvt = (struct pvt *)this->private;
684
	int type, class, ancount, qdcount, n, haveanswer, had_error;
685
	int error = NETDB_SUCCESS;
Mark Andrews's avatar
Mark Andrews committed
686
687
688
	int (*name_ok)(const char *);
	const HEADER *hp;
	const u_char *eom;
689
	const u_char *eor;
Mark Andrews's avatar
Mark Andrews committed
690
691
692
	const u_char *cp;
	const char *tname;
	const char *hname;
693
	char *bp, *ep, **ap, **hap;
Mark Andrews's avatar
Mark Andrews committed
694
695
696
697
698
699
700
701
702
703
704
705
706
707
	char tbuf[MAXDNAME+1];
	struct addrinfo sentinel, *cur, ai;

	if (pai == NULL) abort();
	if (ret_aip != NULL)
		*ret_aip = NULL;
	memset(&sentinel, 0, sizeof(sentinel));
	cur = &sentinel;

	tname = qname;
	eom = ansbuf + anslen;
	switch (qtype) {
	case T_A:
	case T_AAAA:
708
	case T_ANY:	/*%< use T_ANY only for T_A/T_AAAA lookup */
Mark Andrews's avatar
Mark Andrews committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
		name_ok = res_hnok;
		break;
	case T_PTR:
		name_ok = res_dnok;
		break;
	default:
		abort();
	}

	pvt->host.h_addrtype = af;
	pvt->host.h_length = size;
	hname = pvt->host.h_name = NULL;

	/*
	 * Find first satisfactory answer.
	 */
	if (ansbuf + HFIXEDSZ > eom) {
		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
		return (NULL);
	}
	hp = (const HEADER *)ansbuf;
	ancount = ntohs(hp->ancount);
	qdcount = ntohs(hp->qdcount);
	bp = pvt->hostbuf;
733
	ep = pvt->hostbuf + sizeof(pvt->hostbuf);
Mark Andrews's avatar
Mark Andrews committed
734
735
736
737
738
	cp = ansbuf + HFIXEDSZ;
	if (qdcount != 1) {
		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
		return (NULL);
	}
739
	n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
Mark Andrews's avatar
Mark Andrews committed
740
741
742
743
744
745
746
747
748
	if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
		return (NULL);
	}
	cp += n + QFIXEDSZ;
	if (cp > eom) {
		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
		return (NULL);
	}
749
	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
Mark Andrews's avatar
Mark Andrews committed
750
751
752
753
		/* res_nsend() has already verified that the query name is the
		 * same as the one we sent; this just gets the expanded name
		 * (i.e., with the succeeding search-domain tacked on).
		 */
754
		n = strlen(bp) + 1;		/*%< for the \\0 */
Mark Andrews's avatar
Mark Andrews committed
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
		if (n > MAXHOSTNAMELEN) {
			RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
			return (NULL);
		}
		pvt->host.h_name = bp;
		hname = bp;
		bp += n;
		/* The qname can be abbreviated, but hname is now absolute. */
		qname = pvt->host.h_name;
	}
	ap = pvt->host_aliases;
	*ap = NULL;
	pvt->host.h_aliases = pvt->host_aliases;
	hap = pvt->h_addr_ptrs;
	*hap = NULL;
	pvt->host.h_addr_list = pvt->h_addr_ptrs;
	haveanswer = 0;
	had_error = 0;
	while (ancount-- > 0 && cp < eom && !had_error) {
774
		n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
Mark Andrews's avatar
Mark Andrews committed
775
776
777
778
		if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
			had_error++;
			continue;
		}
779
		cp += n;			/*%< name */
Mark Andrews's avatar
Mark Andrews committed
780
781
		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
		type = ns_get16(cp);
782
 		cp += INT16SZ;			/*%< type */
Mark Andrews's avatar
Mark Andrews committed
783
		class = ns_get16(cp);
784
 		cp += INT16SZ + INT32SZ;	/*%< class, TTL */
Mark Andrews's avatar
Mark Andrews committed
785
		n = ns_get16(cp);
786
		cp += INT16SZ;			/*%< len */
Mark Andrews's avatar
Mark Andrews committed
787
788
789
790
791
		BOUNDS_CHECK(cp, n);
		if (class != C_IN) {
			cp += n;
			continue;
		}
792
		eor = cp + n;
793
794
		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
		    type == T_CNAME) {
795
796
797
798
799
800
801
802
803
			if (haveanswer) {
				int level = LOG_CRIT;
#ifdef LOG_SECURITY
				level |= LOG_SECURITY;
#endif
				syslog(level,
 "gethostans: possible attempt to exploit buffer overflow while looking up %s",
					*qname ? qname : ".");
			}
804
			n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
Mark Andrews's avatar
Mark Andrews committed
805
806
807
808
809
810
			if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
				had_error++;
				continue;
			}
			cp += n;
			/* Store alias. */
Mark Andrews's avatar
Mark Andrews committed
811
812
			if (ap >= &pvt->host_aliases[MAXALIASES-1])
				continue;
Mark Andrews's avatar
Mark Andrews committed
813
			*ap++ = bp;
814
			n = strlen(bp) + 1;	/*%< for the \\0 */
Mark Andrews's avatar
Mark Andrews committed
815
816
			bp += n;
			/* Get canonical name. */
817
			n = strlen(tbuf) + 1;	/*%< for the \\0 */
818
			if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
Mark Andrews's avatar
Mark Andrews committed
819
820
821
				had_error++;
				continue;
			}
Mark Andrews's avatar
pullup:    
Mark Andrews committed
822
			strcpy(bp, tbuf);	/* (checked) */
Mark Andrews's avatar
Mark Andrews committed
823
824
825
826
827
828
			pvt->host.h_name = bp;
			hname = bp;
			bp += n;
			continue;
		}
		if (qtype == T_PTR && type == T_CNAME) {
829
			n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
Mark Andrews's avatar
Mark Andrews committed
830
831
832
833
834
835
			if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
				had_error++;
				continue;
			}
			cp += n;
#ifdef RES_USE_DNAME
Mark Andrews's avatar
Mark Andrews committed
836
			if ((pvt->res->options & RES_USE_DNAME) != 0U)
Mark Andrews's avatar
Mark Andrews committed
837
838
839
840
841
842
843
844
845
846
847
848
#endif
			{
				/*
				 * We may be able to check this regardless
				 * of the USE_DNAME bit, but we add the check
				 * for now since the DNAME support is
				 * experimental.
				 */
				if (ns_samename(tname, bp) != 1)
					continue;
			}
			/* Get canonical name. */
849
			n = strlen(tbuf) + 1;	/*%< for the \\0 */
850
			if (n > (ep - bp)) {
Mark Andrews's avatar
Mark Andrews committed
851
852
853
				had_error++;
				continue;
			}
Mark Andrews's avatar
pullup:    
Mark Andrews committed
854
			strcpy(bp, tbuf);	/* (checked) */
Mark Andrews's avatar
Mark Andrews committed
855
856
857
858
859
			tname = bp;
			bp += n;
			continue;
		}
		if (qtype == T_ANY) {
860
			if (!(type == T_A || type == T_AAAA)) {
Mark Andrews's avatar
Mark Andrews committed
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
				cp += n;
				continue;
			}
		} else if (type != qtype) {
			cp += n;
			continue;
		}
		switch (type) {
		case T_PTR:
			if (ret_aip != NULL) {
				/* addrinfo never needs T_PTR */
				cp += n;
				continue;
			}
			if (ns_samename(tname, bp) != 1) {
				cp += n;
				continue;
			}
879
			n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
Mark Andrews's avatar
Mark Andrews committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
			if (n < 0 || !maybe_hnok(pvt->res, bp) ||
			    n >= MAXHOSTNAMELEN) {
				had_error++;
				break;
			}
			cp += n;
			if (!haveanswer) {
				pvt->host.h_name = bp;
				hname = bp;
			}
			else if (ap < &pvt->host_aliases[MAXALIASES-1])
				*ap++ = bp;
			else
				n = -1;
			if (n != -1) {
895
				n = strlen(bp) + 1;	/*%< for the \\0 */
Mark Andrews's avatar
Mark Andrews committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
				bp += n;
			}
			break;
		case T_A:
		case T_AAAA:
			if (ns_samename(hname, bp) != 1) {
				cp += n;
				continue;
			}
			if (type == T_A && n != INADDRSZ) {
				cp += n;
				continue;
			}
			if (type == T_AAAA && n != IN6ADDRSZ) {
				cp += n;
				continue;
			}

			/* make addrinfo. don't overwrite constant PAI */
			ai = *pai;
			ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
Mark Andrews's avatar
Mark Andrews committed
917
918
919
			cur->ai_next = addr2addrinfo(
					(const struct addrinfo *)&ai,
					(const char *)cp);
Mark Andrews's avatar
Mark Andrews committed
920
921
922
923
924
925
			if (cur->ai_next == NULL)
				had_error++;

			if (!haveanswer) {
				int nn;

926
				nn = strlen(bp) + 1;	/*%< for the \\0 */
Mark Andrews's avatar
Mark Andrews committed
927
928
929
930
931
932
933
934
935
936
				if (nn >= MAXHOSTNAMELEN) {
					cp += n;
					had_error++;
					continue;
				}
				pvt->host.h_name = bp;
				hname = bp;
				bp += nn;
			}
			/* Ensure alignment. */
937
938
			bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
				      ~(sizeof(align) - 1));
Mark Andrews's avatar
Mark Andrews committed
939
			/* Avoid overflows. */
940
			if (bp + n > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1]) {
Mark Andrews's avatar
Mark Andrews committed
941
942
943
				had_error++;
				continue;
			}
944
			if (ret_aip) { /*%< need addrinfo. keep it. */
945
				while (cur->ai_next)
Mark Andrews's avatar
Mark Andrews committed
946
					cur = cur->ai_next;
947
			} else if (cur->ai_next) { /*%< need hostent */
Mark Andrews's avatar
Mark Andrews committed
948
949
950
951
952
953
954
955
956
957
958
				struct addrinfo *aip = cur->ai_next;

				for (aip = cur->ai_next; aip;
				     aip = aip->ai_next) {
					int m;

					m = add_hostent(pvt, bp, hap, aip);
					if (m < 0) {
						had_error++;
						break;
					}
959
960
					if (m == 0)
						continue;
Mark Andrews's avatar
Mark Andrews committed
961
962
					if (hap < &pvt->h_addr_ptrs[MAXADDRS-1])
						hap++;
963
					*hap = NULL;
Mark Andrews's avatar
Mark Andrews committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
					bp += m;
				}

				freeaddrinfo(cur->ai_next);
				cur->ai_next = NULL;
			}
			cp += n;
			break;
		default:
			abort();
		}
		if (!had_error)
			haveanswer++;
	}
	if (haveanswer) {
		if (ret_aip == NULL) {
			*ap = NULL;
			*hap = NULL;

			if (pvt->res->nsort && haveanswer > 1 && qtype == T_A)
				addrsort(pvt->res, pvt->h_addr_ptrs,
					 haveanswer);
			if (pvt->host.h_name == NULL) {
987
				n = strlen(qname) + 1;	/*%< for the \\0 */
988
				if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
Mark Andrews's avatar
Mark Andrews committed
989
					goto no_recovery;
Mark Andrews's avatar
pullup:    
Mark Andrews committed
990
				strcpy(bp, qname);	/* (checked) */
Mark Andrews's avatar
Mark Andrews committed
991
992
993
994
				pvt->host.h_name = bp;
				bp += n;
			}
			if (pvt->res->options & RES_USE_INET6)
995
				map_v4v6_hostent(&pvt->host, &bp, ep);
Mark Andrews's avatar
Mark Andrews committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
			RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
			return (&pvt->host);
		} else {
			if ((pai->ai_flags & AI_CANONNAME) != 0) {
				if (pvt->host.h_name == NULL) {
					sentinel.ai_next->ai_canonname =
						strdup(qname);
				}
				else {
					sentinel.ai_next->ai_canonname =
						strdup(pvt->host.h_name);
				}
			}
			*ret_aip = sentinel.ai_next;
			return(NULL);
		}
	}
 no_recovery:
	if (sentinel.ai_next) {
		/* this should be impossible, but check it for safety */
		freeaddrinfo(sentinel.ai_next);
	}
	if (error == NETDB_SUCCESS)
		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
	else
		RES_SET_H_ERRNO(pvt->res, error);
	return(NULL);
}

static int
add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
{
	int addrlen;
	char *addrp;
1030
1031
	const char **tap;
	char *obp = bp;
Mark Andrews's avatar
Mark Andrews committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042

	switch(ai->ai_addr->sa_family) {
	case AF_INET6:
		addrlen = IN6ADDRSZ;
		addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
		break;
	case AF_INET:
		addrlen = INADDRSZ;
		addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
		break;
	default:
1043
		return(-1);	/*%< abort? */
Mark Andrews's avatar
Mark Andrews committed
1044
1045
	}

1046
1047
1048
1049
	/* Ensure alignment. */
	bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
		      ~(sizeof(align) - 1));
	/* Avoid overflows. */
1050
	if (bp + addrlen > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1])
Mark Andrews's avatar
Mark Andrews committed
1051
1052
		return(-1);
	if (hap >= &pvt->h_addr_ptrs[MAXADDRS-1])
1053
		return(0); /*%< fail, but not treat it as an error. */
Mark Andrews's avatar
Mark Andrews committed
1054
1055
1056
1057
	/* Suppress duplicates. */
	for (tap = (const char **)pvt->h_addr_ptrs;
	     *tap != NULL;
	     tap++)
1058
		if (memcmp(*tap, addrp, addrlen) == 0)
Mark Andrews's avatar
Mark Andrews committed
1059
			break;
1060
1061
	if (*tap != NULL)
		return (0);
Mark Andrews's avatar
Mark Andrews committed
1062
1063

	memcpy(*hap = bp, addrp, addrlen);
1064
	return((bp + addrlen) - obp);
Mark Andrews's avatar
Mark Andrews committed
1065
1066
1067
}

static void
1068
map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
Mark Andrews's avatar
Mark Andrews committed
1069
1070
1071
1072
1073
1074
1075
	char **ap;

	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
		return;
	hp->h_addrtype = AF_INET6;
	hp->h_length = IN6ADDRSZ;
	for (ap = hp->h_addr_list; *ap; ap++) {
1076
1077
1078
1079
		int i = (u_long)*bpp % sizeof(align);

		if (i != 0)
			i = sizeof(align) - i;
Mark Andrews's avatar
Mark Andrews committed
1080

1081
		if ((ep - *bpp) < (i + IN6ADDRSZ)) {
Mark Andrews's avatar
Mark Andrews committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
			/* Out of memory.  Truncate address list here. */
			*ap = NULL;
			return;
		}
		*bpp += i;
		map_v4v6_address(*ap, *bpp);
		*ap = *bpp;
		*bpp += IN6ADDRSZ;
	}
}

static void
addrsort(res_state statp, char **ap, int num) {
	int i, j, needsort = 0, aval[MAXADDRS];
	char **p;

	p = ap;
	for (i = 0; i < num; i++, p++) {
		for (j = 0 ; (unsigned)j < statp->nsort; j++)
			if (statp->sort_list[j].addr.s_addr == 
			    (((struct in_addr *)(*p))->s_addr &
			     statp->sort_list[j].mask))
				break;
		aval[i] = j;
		if (needsort == 0 && i > 0 && j < aval[i-1])
			needsort = i;
	}
	if (!needsort)
		return;

	while (needsort < num) {
		for (j = needsort - 1; j >= 0; j--) {
			if (aval[j] > aval[j+1]) {
				char *hp;

				i = aval[j];
				aval[j] = aval[j+1];
				aval[j+1] = i;

				hp = ap[j];
				ap[j] = ap[j+1];
				ap[j+1] = hp;

			} else
				break;
		}
		needsort++;
	}
}

static int
init(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	
	if (!pvt->res && !ho_res_get(this))
		return (-1);
Mark Andrews's avatar
Mark Andrews committed
1138
	if (((pvt->res->options & RES_INIT) == 0U) &&
Mark Andrews's avatar
Mark Andrews committed
1139
1140
1141
1142
	    res_ninit(pvt->res) == -1)
		return (-1);
	return (0);
}