summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/gethostnamadr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/gethostnamadr.c')
-rw-r--r--src/lib/libc/net/gethostnamadr.c977
1 files changed, 727 insertions, 250 deletions
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c
index ec3f14a900..f4e655eeaf 100644
--- a/src/lib/libc/net/gethostnamadr.c
+++ b/src/lib/libc/net/gethostnamadr.c
@@ -1,5 +1,4 @@
1/* $NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $ */ 1/* $OpenBSD: gethostnamadr.c,v 1.73 2009/11/18 07:43:22 guenther Exp $ */
2
3/*- 2/*-
4 * Copyright (c) 1985, 1988, 1993 3 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
@@ -12,11 +11,7 @@
12 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 14 * 3. Neither the name of the University nor the names of its contributors
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 15 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 16 * without specific prior written permission.
22 * 17 *
@@ -53,15 +48,6 @@
53 * --Copyright-- 48 * --Copyright--
54 */ 49 */
55 50
56#if defined(LIBC_SCCS) && !defined(lint)
57#if 0
58static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel ";
60#else
61static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $";
62#endif
63#endif /* LIBC_SCCS and not lint */
64
65#include <sys/param.h> 51#include <sys/param.h>
66#include <sys/socket.h> 52#include <sys/socket.h>
67#include <netinet/in.h> 53#include <netinet/in.h>
@@ -73,11 +59,17 @@ static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycro
73#include <ctype.h> 59#include <ctype.h>
74#include <errno.h> 60#include <errno.h>
75#include <string.h> 61#include <string.h>
62#include <syslog.h>
63#include <stdlib.h>
76#ifdef YP 64#ifdef YP
77#include <rpc/rpc.h> 65#include <rpc/rpc.h>
78#include <rpcsvc/yp_prot.h> 66#include <rpcsvc/yp.h>
79#include <rpcsvc/ypclnt.h> 67#include <rpcsvc/ypclnt.h>
68#include "ypinternal.h"
80#endif 69#endif
70#include "thread_private.h"
71
72#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
81 73
82#define MAXALIASES 35 74#define MAXALIASES 35
83#define MAXADDRS 35 75#define MAXADDRS 35
@@ -91,47 +83,101 @@ static char *__ypdomain;
91static struct hostent host; 83static struct hostent host;
92static char *host_aliases[MAXALIASES]; 84static char *host_aliases[MAXALIASES];
93static char hostbuf[BUFSIZ+1]; 85static char hostbuf[BUFSIZ+1];
94static struct in_addr host_addr; 86static union {
87 struct in_addr _host_in_addr;
88 u_char _host_addr[16]; /* IPv4 or IPv6 */
89} _host_addr_u;
90#define host_addr _host_addr_u._host_addr
95static FILE *hostf = NULL; 91static FILE *hostf = NULL;
96static int stayopen = 0; 92static int stayopen = 0;
97 93
98#if PACKETSZ > 1024 94static void map_v4v6_address(const char *src, char *dst);
99#define MAXPACKET PACKETSZ 95static void map_v4v6_hostent(struct hostent *hp, char **bp, char *);
100#else 96
101#define MAXPACKET 1024 97#ifdef RESOLVSORT
98static void addrsort(char **, int);
102#endif 99#endif
103 100
101int _hokchar(const char *);
102
103static const char AskedForGot[] =
104 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
105
106#define MAXPACKET (64*1024)
107
104typedef union { 108typedef union {
105 HEADER hdr; 109 HEADER hdr;
106 u_char buf[MAXPACKET]; 110 u_char buf[MAXPACKET];
107} querybuf; 111} querybuf;
108 112
109typedef union { 113typedef union {
110 int32_t al; 114 int32_t al;
111 char ac; 115 char ac;
112} align; 116} align;
113 117
114static int qcomp __P((struct in_addr **, struct in_addr **)); 118static struct hostent *getanswer(const querybuf *, int, const char *, int);
115static struct hostent *getanswer __P((querybuf *, int, int));
116 119
117extern int h_errno; 120extern int h_errno;
118 121
122int
123_hokchar(const char *p)
124{
125 char c;
126
127 /*
128 * Many people do not obey RFC 822 and 1035. The valid
129 * characters are a-z, A-Z, 0-9, '-' and . But the others
130 * tested for below can happen, and we must be more permissive
131 * than the resolver until those idiots clean up their act.
132 * We let '/' through, but not '..'
133 */
134 while ((c = *p++)) {
135 if (('a' <= c && c <= 'z') ||
136 ('A' <= c && c <= 'Z') ||
137 ('0' <= c && c <= '9'))
138 continue;
139 if (strchr("-_/", c))
140 continue;
141 if (c == '.' && *p != '.')
142 continue;
143 return 0;
144 }
145 return 1;
146}
147
119static struct hostent * 148static struct hostent *
120getanswer(answer, anslen, iquery) 149getanswer(const querybuf *answer, int anslen, const char *qname, int qtype)
121 querybuf *answer;
122 int anslen;
123 int iquery;
124{ 150{
125 register HEADER *hp; 151 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
126 register u_char *cp; 152 const HEADER *hp;
127 register int n; 153 const u_char *cp, *eom;
128 u_char *eom; 154 char tbuf[MAXDNAME];
129 char *bp, **ap; 155 char *bp, **ap, **hap, *ep;
130 int type, class, buflen, ancount, qdcount; 156 int type, class, ancount, qdcount, n;
131 int haveanswer, getclass = C_ANY; 157 int haveanswer, had_error, toobig = 0;
132 char **hap; 158 const char *tname;
159 int (*name_ok)(const char *);
133 160
161 tname = qname;
162 host.h_name = NULL;
134 eom = answer->buf + anslen; 163 eom = answer->buf + anslen;
164 switch (qtype) {
165 case T_A:
166 case T_AAAA:
167#ifdef USE_RESOLV_NAME_OK
168 name_ok = res_hnok;
169 break;
170#endif
171 case T_PTR:
172#ifdef USE_RESOLV_NAME_OK
173 name_ok = res_dnok;
174#else
175 name_ok = _hokchar;
176#endif
177 break;
178 default:
179 return (NULL);
180 }
135 /* 181 /*
136 * find first satisfactory answer 182 * find first satisfactory answer
137 */ 183 */
@@ -139,31 +185,27 @@ getanswer(answer, anslen, iquery)
139 ancount = ntohs(hp->ancount); 185 ancount = ntohs(hp->ancount);
140 qdcount = ntohs(hp->qdcount); 186 qdcount = ntohs(hp->qdcount);
141 bp = hostbuf; 187 bp = hostbuf;
142 buflen = sizeof(hostbuf); 188 ep = hostbuf + sizeof hostbuf;
143 cp = answer->buf + sizeof(HEADER); 189 cp = answer->buf + HFIXEDSZ;
144 if (qdcount) { 190 if (qdcount != 1) {
145 if (iquery) { 191 h_errno = NO_RECOVERY;
146 if ((n = dn_expand((u_char *)answer->buf, 192 return (NULL);
147 (u_char *)eom, (u_char *)cp, (u_char *)bp, 193 }
148 buflen)) < 0) { 194 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
149 h_errno = NO_RECOVERY; 195 if ((n < 0) || !(*name_ok)(bp)) {
150 return ((struct hostent *) NULL); 196 h_errno = NO_RECOVERY;
151 } 197 return (NULL);
152 cp += n + QFIXEDSZ; 198 }
153 host.h_name = bp; 199 cp += n + QFIXEDSZ;
154 n = strlen(bp) + 1; 200 if (qtype == T_A || qtype == T_AAAA) {
155 bp += n; 201 /* res_send() has already verified that the query name is the
156 buflen -= n; 202 * same as the one we sent; this just gets the expanded name
157 } else 203 * (i.e., with the succeeding search-domain tacked on).
158 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 204 */
159 while (--qdcount > 0) 205 host.h_name = bp;
160 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 206 bp += strlen(bp) + 1; /* for the \0 */
161 } else if (iquery) { 207 /* The qname can be abbreviated, but h_name is now absolute. */
162 if (hp->aa) 208 qname = host.h_name;
163 h_errno = HOST_NOT_FOUND;
164 else
165 h_errno = TRY_AGAIN;
166 return ((struct hostent *) NULL);
167 } 209 }
168 ap = host_aliases; 210 ap = host_aliases;
169 *ap = NULL; 211 *ap = NULL;
@@ -172,103 +214,325 @@ getanswer(answer, anslen, iquery)
172 *hap = NULL; 214 *hap = NULL;
173 host.h_addr_list = h_addr_ptrs; 215 host.h_addr_list = h_addr_ptrs;
174 haveanswer = 0; 216 haveanswer = 0;
175 while (--ancount >= 0 && cp < eom) { 217 had_error = 0;
176 if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, 218 while (ancount-- > 0 && cp < eom && !had_error) {
177 (u_char *)cp, (u_char *)bp, buflen)) < 0) 219 size_t len;
220
221 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
222 if ((n < 0) || !(*name_ok)(bp)) {
223 had_error++;
224 continue;
225 }
226 cp += n; /* name */
227 if (cp >= eom)
178 break; 228 break;
179 cp += n;
180 type = _getshort(cp); 229 type = _getshort(cp);
181 cp += sizeof(u_int16_t); 230 cp += INT16SZ; /* type */
231 if (cp >= eom)
232 break;
182 class = _getshort(cp); 233 class = _getshort(cp);
183 cp += sizeof(u_int16_t) + sizeof(u_int32_t); 234 cp += INT16SZ + INT32SZ; /* class, TTL */
235 if (cp >= eom)
236 break;
184 n = _getshort(cp); 237 n = _getshort(cp);
185 cp += sizeof(u_int16_t); 238 cp += INT16SZ; /* len */
186 if (type == T_CNAME) { 239 if (cp >= eom)
240 break;
241 if (type == T_SIG || type == T_RRSIG) {
242 /* XXX - ignore signatures as we don't use them yet */
187 cp += n; 243 cp += n;
188 if (ap >= &host_aliases[MAXALIASES-1])
189 continue;
190 *ap++ = bp;
191 n = strlen(bp) + 1;
192 bp += n;
193 buflen -= n;
194 continue; 244 continue;
195 } 245 }
196 if (iquery && type == T_PTR) { 246 if (class != C_IN) {
197 if ((n = dn_expand((u_char *)answer->buf, 247 /* XXX - debug? syslog? */
198 (u_char *)eom, (u_char *)cp, (u_char *)bp,
199 buflen)) < 0)
200 break;
201 cp += n; 248 cp += n;
249 continue; /* XXX - had_error++ ? */
250 }
251 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
252 if (ap >= &host_aliases[MAXALIASES-1])
253 continue;
254 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
255 if ((n < 0) || !(*name_ok)(tbuf)) {
256 had_error++;
257 continue;
258 }
259 cp += n;
260 /* Store alias. */
261 *ap++ = bp;
262 bp += strlen(bp) + 1; /* for the \0 */
263 /* Get canonical name. */
264 len = strlen(tbuf) + 1; /* for the \0 */
265 if (len > ep - bp) {
266 had_error++;
267 continue;
268 }
269 strlcpy(bp, tbuf, ep - bp);
202 host.h_name = bp; 270 host.h_name = bp;
203 return(&host); 271 bp += len;
272 continue;
204 } 273 }
205 if (iquery || type != T_A) { 274 if (qtype == T_PTR && type == T_CNAME) {
206#ifdef DEBUG 275 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
207 if (_res.options & RES_DEBUG) 276#ifdef USE_RESOLV_NAME_OK
208 printf("unexpected answer type %d, size %d\n", 277 if ((n < 0) || !res_hnok(tbuf)) {
209 type, n); 278#else
279 if ((n < 0) || !_hokchar(tbuf)) {
210#endif 280#endif
281 had_error++;
282 continue;
283 }
211 cp += n; 284 cp += n;
285 /* Get canonical name. */
286 len = strlen(tbuf) + 1; /* for the \0 */
287 if (len > ep - bp) {
288 had_error++;
289 continue;
290 }
291 strlcpy(bp, tbuf, ep - bp);
292 tname = bp;
293 bp += len;
212 continue; 294 continue;
213 } 295 }
214 if (haveanswer) { 296 if (type != qtype) {
215 if (n != host.h_length) { 297#ifndef NO_LOG_BAD_DNS_RESPONSES
298 syslog(LOG_NOTICE|LOG_AUTH,
299 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
300 qname, p_class(C_IN), p_type(qtype),
301 p_type(type));
302#endif /* NO_LOG_BAD_DNS_RESPONSES */
303 cp += n;
304 continue; /* XXX - had_error++ ? */
305 }
306 switch (type) {
307 case T_PTR:
308 if (strcasecmp(tname, bp) != 0) {
309 syslog(LOG_NOTICE|LOG_AUTH,
310 AskedForGot, qname, bp);
216 cp += n; 311 cp += n;
217 continue; 312 continue; /* XXX - had_error++ ? */
313 }
314 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
315#ifdef USE_RESOLV_NAME_OK
316 if ((n < 0) || !res_hnok(bp)) {
317#else
318 if ((n < 0) || !_hokchar(bp)) {
319#endif
320 had_error++;
321 break;
322 }
323#if MULTI_PTRS_ARE_ALIASES
324 cp += n;
325 if (!haveanswer)
326 host.h_name = bp;
327 else if (ap < &host_aliases[MAXALIASES-1])
328 *ap++ = bp;
329 else
330 n = -1;
331 if (n != -1) {
332 n = strlen(bp) + 1; /* for the \0 */
333 bp += n;
334 }
335 break;
336#else
337 host.h_name = bp;
338 if (_resp->options & RES_USE_INET6) {
339 n = strlen(bp) + 1; /* for the \0 */
340 bp += n;
341 map_v4v6_hostent(&host, &bp, ep);
342 }
343 h_errno = NETDB_SUCCESS;
344 return (&host);
345#endif
346 case T_A:
347 case T_AAAA:
348 if (strcasecmp(host.h_name, bp) != 0) {
349 syslog(LOG_NOTICE|LOG_AUTH,
350 AskedForGot, host.h_name, bp);
351 cp += n;
352 continue; /* XXX - had_error++ ? */
218 } 353 }
219 if (class != getclass) { 354 if (n != host.h_length) {
220 cp += n; 355 cp += n;
221 continue; 356 continue;
222 } 357 }
223 } else { 358 if (type == T_AAAA) {
224 host.h_length = n; 359 struct in6_addr in6;
225 getclass = class; 360 memcpy(&in6, cp, IN6ADDRSZ);
226 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 361 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
227 if (!iquery) { 362 cp += n;
363 continue;
364 }
365 }
366 if (!haveanswer) {
228 host.h_name = bp; 367 host.h_name = bp;
229 bp += strlen(bp) + 1; 368 bp += strlen(bp) + 1; /* for the \0 */
230 } 369 }
231 }
232 370
233 bp += sizeof(align) - ((u_long)bp % sizeof(align)); 371 bp += sizeof(align) - ((u_long)bp % sizeof(align));
234 372
235 if (bp + n >= &hostbuf[sizeof(hostbuf)]) { 373 if (bp + n >= &hostbuf[sizeof hostbuf]) {
236#ifdef DEBUG 374#ifdef DEBUG
237 if (_res.options & RES_DEBUG) 375 if (_resp->options & RES_DEBUG)
238 printf("size (%d) too big\n", n); 376 printf("size (%d) too big\n", n);
239#endif 377#endif
378 had_error++;
379 continue;
380 }
381 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
382 if (!toobig++)
383#ifdef DEBUG
384 if (_resp->options & RES_DEBUG)
385 printf("Too many addresses (%d)\n", MAXADDRS);
386#endif
387 cp += n;
388 continue;
389 }
390 bcopy(cp, *hap++ = bp, n);
391 bp += n;
392 cp += n;
240 break; 393 break;
241 } 394 }
242 bcopy(cp, *hap++ = bp, n); 395 if (!had_error)
243 bp +=n; 396 haveanswer++;
244 cp += n;
245 haveanswer++;
246 } 397 }
247 if (haveanswer) { 398 if (haveanswer) {
248 *ap = NULL; 399 *ap = NULL;
249 *hap = NULL; 400 *hap = NULL;
250 if (_res.nsort) { 401# if defined(RESOLVSORT)
251 qsort(host.h_addr_list, haveanswer, 402 /*
252 sizeof(struct in_addr), 403 * Note: we sort even if host can take only one address
253 (int (*)__P((const void *, const void *)))qcomp); 404 * in its return structures - should give it the "best"
405 * address in that case, not some random one
406 */
407 if (_resp->nsort && haveanswer > 1 && qtype == T_A)
408 addrsort(h_addr_ptrs, haveanswer);
409# endif /*RESOLVSORT*/
410 if (!host.h_name) {
411 size_t len;
412
413 len = strlen(qname) + 1;
414 if (len > ep - bp) /* for the \0 */
415 goto try_again;
416 strlcpy(bp, qname, ep - bp);
417 host.h_name = bp;
418 bp += len;
254 } 419 }
420 if (_resp->options & RES_USE_INET6)
421 map_v4v6_hostent(&host, &bp, ep);
422 h_errno = NETDB_SUCCESS;
255 return (&host); 423 return (&host);
256 } else {
257 h_errno = TRY_AGAIN;
258 return ((struct hostent *) NULL);
259 } 424 }
425 try_again:
426 h_errno = TRY_AGAIN;
427 return (NULL);
260} 428}
261 429
430#ifdef notyet
431/*
432 * XXX This is an extremely bogus implementation.
433 *
434 * FreeBSD has this interface:
435 * int gethostbyaddr_r(const char *addr, int len, int type,
436 * struct hostent *result, struct hostent_data *buffer)
437 */
438
439struct hostent *
440gethostbyname_r(const char *name, struct hostent *hp, char *buf, int buflen,
441 int *errorp)
442{
443 struct hostent *res;
444
445 res = gethostbyname(name);
446 *errorp = h_errno;
447 if (res == NULL)
448 return NULL;
449 memcpy(hp, res, sizeof *hp); /* XXX not sufficient */
450 return hp;
451}
452
453/*
454 * XXX This is an extremely bogus implementation.
455 */
262struct hostent * 456struct hostent *
263gethostbyname(name) 457gethostbyaddr_r(const char *addr, int len, int af, struct hostent *he,
264 const char *name; 458 char *buf, int buflen, int *errorp)
265{ 459{
266 querybuf buf; 460 struct hostent * res;
267 register const char *cp; 461
268 int n, i; 462 res = gethostbyaddr(addr, len, af);
269 extern struct hostent *_gethtbyname(), *_yp_gethtbyname(); 463 *errorp = h_errno;
270 register struct hostent *hp; 464 if (res == NULL)
465 return NULL;
466 memcpy(he, res, sizeof *he); /* XXX not sufficient */
467 return he;
468}
469
470/* XXX RFC2133 expects a gethostbyname2_r() -- unimplemented */
471#endif
472
473struct hostent *
474gethostbyname(const char *name)
475{
476 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
477 struct hostent *hp;
478 extern struct hostent *_gethtbyname2(const char *, int);
479
480 if (_res_init(0) == -1)
481 hp = _gethtbyname2(name, AF_INET);
482
483 else if (_resp->options & RES_USE_INET6) {
484 hp = gethostbyname2(name, AF_INET6);
485 if (hp == NULL)
486 hp = gethostbyname2(name, AF_INET);
487 }
488 else
489 hp = gethostbyname2(name, AF_INET);
490 return hp;
491}
492
493struct hostent *
494gethostbyname2(const char *name, int af)
495{
496 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
497 querybuf *buf;
498 const char *cp;
499 char *bp, *ep;
500 int n, size, type, i;
501 struct hostent *hp;
271 char lookups[MAXDNSLUS]; 502 char lookups[MAXDNSLUS];
503 extern struct hostent *_gethtbyname2(const char *, int);
504#ifdef YP
505 extern struct hostent *_yp_gethtbyname(const char *);
506#endif
507
508 if (_res_init(0) == -1)
509 return (_gethtbyname2(name, af));
510
511 switch (af) {
512 case AF_INET:
513 size = INADDRSZ;
514 type = T_A;
515 break;
516 case AF_INET6:
517 size = IN6ADDRSZ;
518 type = T_AAAA;
519 break;
520 default:
521 h_errno = NETDB_INTERNAL;
522 errno = EAFNOSUPPORT;
523 return (NULL);
524 }
525
526 host.h_addrtype = af;
527 host.h_length = size;
528
529 /*
530 * if there aren't any dots, it could be a user-level alias.
531 * this is also done in res_query() since we are not the only
532 * function that looks up host names.
533 */
534 if (!strchr(name, '.') && (cp = __hostalias(name)))
535 name = cp;
272 536
273 /* 537 /*
274 * disallow names consisting only of digits/dots, unless 538 * disallow names consisting only of digits/dots, unless
@@ -284,122 +548,229 @@ gethostbyname(name)
284 * Fake up a hostent as if we'd actually 548 * Fake up a hostent as if we'd actually
285 * done a lookup. 549 * done a lookup.
286 */ 550 */
287 if (!inet_aton(name, &host_addr)) { 551 if (inet_pton(af, name, host_addr) <= 0) {
288 h_errno = HOST_NOT_FOUND; 552 h_errno = HOST_NOT_FOUND;
289 return((struct hostent *) NULL); 553 return (NULL);
290 } 554 }
291 host.h_name = (char *)name; 555 strlcpy(hostbuf, name, MAXHOSTNAMELEN);
556 bp = hostbuf + MAXHOSTNAMELEN;
557 ep = hostbuf + sizeof(hostbuf);
558 host.h_name = hostbuf;
292 host.h_aliases = host_aliases; 559 host.h_aliases = host_aliases;
293 host_aliases[0] = NULL; 560 host_aliases[0] = NULL;
294 host.h_addrtype = AF_INET; 561 h_addr_ptrs[0] = (char *)host_addr;
295 host.h_length = sizeof(u_int32_t);
296 h_addr_ptrs[0] = (char *)&host_addr;
297 h_addr_ptrs[1] = NULL; 562 h_addr_ptrs[1] = NULL;
298 host.h_addr_list = h_addr_ptrs; 563 host.h_addr_list = h_addr_ptrs;
564 if (_resp->options & RES_USE_INET6)
565 map_v4v6_hostent(&host, &bp, ep);
566 h_errno = NETDB_SUCCESS;
299 return (&host); 567 return (&host);
300 } 568 }
301 if (!isdigit(*cp) && *cp != '.') 569 if (!isdigit(*cp) && *cp != '.')
302 break; 570 break;
303 } 571 }
572 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
573 name[0] == ':')
574 for (cp = name;; ++cp) {
575 if (!*cp) {
576 if (*--cp == '.')
577 break;
578 /*
579 * All-IPv6-legal, no dot at the end.
580 * Fake up a hostent as if we'd actually
581 * done a lookup.
582 */
583 if (inet_pton(af, name, host_addr) <= 0) {
584 h_errno = HOST_NOT_FOUND;
585 return (NULL);
586 }
587 strlcpy(hostbuf, name, MAXHOSTNAMELEN);
588 bp = hostbuf + MAXHOSTNAMELEN;
589 ep = hostbuf + sizeof(hostbuf);
590 host.h_name = hostbuf;
591 host.h_aliases = host_aliases;
592 host_aliases[0] = NULL;
593 h_addr_ptrs[0] = (char *)host_addr;
594 h_addr_ptrs[1] = NULL;
595 host.h_addr_list = h_addr_ptrs;
596 h_errno = NETDB_SUCCESS;
597 return (&host);
598 }
599 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
600 break;
601 }
304 602
305 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 603 bcopy(_resp->lookups, lookups, sizeof lookups);
306 return (_gethtbyname(name));
307
308 bcopy(_res.lookups, lookups, sizeof lookups);
309 if (lookups[0] == '\0') 604 if (lookups[0] == '\0')
310 strncpy(lookups, "bf", sizeof lookups); 605 strlcpy(lookups, "bf", sizeof lookups);
311 606
312 hp = (struct hostent *)NULL; 607 hp = (struct hostent *)NULL;
313 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { 608 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
314 switch (lookups[i]) { 609 switch (lookups[i]) {
315#ifdef YP 610#ifdef YP
316 case 'y': 611 case 'y':
317 hp = _yp_gethtbyname(name); 612 /* YP only supports AF_INET. */
613 if (af == AF_INET)
614 hp = _yp_gethtbyname(name);
318 break; 615 break;
319#endif 616#endif
320 case 'b': 617 case 'b':
321 if ((n = res_search(name, C_IN, T_A, buf.buf, 618 buf = malloc(sizeof(*buf));
322 sizeof(buf))) < 0) { 619 if (buf == NULL)
620 break;
621 if ((n = res_search(name, C_IN, type, buf->buf,
622 sizeof(buf->buf))) < 0) {
623 free(buf);
323#ifdef DEBUG 624#ifdef DEBUG
324 if (_res.options & RES_DEBUG) 625 if (_resp->options & RES_DEBUG)
325 printf("res_search failed\n"); 626 printf("res_search failed\n");
326#endif 627#endif
327 break; 628 break;
328 } 629 }
329 hp = getanswer(&buf, n, 0); 630 hp = getanswer(buf, n, name, type);
631 free(buf);
330 break; 632 break;
331 case 'f': 633 case 'f':
332 hp = _gethtbyname(name); 634 hp = _gethtbyname2(name, af);
333 break; 635 break;
334 } 636 }
335 } 637 }
638 /* XXX h_errno not correct in all cases... */
336 return (hp); 639 return (hp);
337} 640}
338 641
339struct hostent * 642struct hostent *
340gethostbyaddr(addr, len, type) 643gethostbyaddr(const void *addr, socklen_t len, int af)
341 const char *addr;
342 int len, type;
343{ 644{
344 int n, i; 645 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
345 querybuf buf; 646 const u_char *uaddr = (const u_char *)addr;
346 register struct hostent *hp; 647 int n, size, i;
347 char qbuf[MAXDNAME]; 648 querybuf *buf;
348 extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); 649 struct hostent *hp;
650 char qbuf[MAXDNAME+1], *qp, *ep;
349 char lookups[MAXDNSLUS]; 651 char lookups[MAXDNSLUS];
652 struct hostent *res;
653 extern struct hostent *_gethtbyaddr(const void *, socklen_t, int);
654#ifdef YP
655 extern struct hostent *_yp_gethtbyaddr(const void *);
656#endif
350 657
351 if (type != AF_INET) 658 if (_res_init(0) == -1) {
352 return ((struct hostent *) NULL); 659 res = _gethtbyaddr(addr, len, af);
353 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", 660 return (res);
354 ((unsigned)addr[3] & 0xff), 661 }
355 ((unsigned)addr[2] & 0xff), 662
356 ((unsigned)addr[1] & 0xff), 663 if (af == AF_INET6 && len == IN6ADDRSZ &&
357 ((unsigned)addr[0] & 0xff)); 664 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)uaddr) ||
358 665 IN6_IS_ADDR_SITELOCAL((struct in6_addr *)uaddr))) {
359 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 666 h_errno = HOST_NOT_FOUND;
360 return (_gethtbyaddr(addr, len, type)); 667 return (NULL);
361 668 }
362 bcopy(_res.lookups, lookups, sizeof lookups); 669 if (af == AF_INET6 && len == IN6ADDRSZ &&
670 (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)uaddr) ||
671 IN6_IS_ADDR_V4COMPAT((struct in6_addr *)uaddr))) {
672 /* Unmap. */
673 uaddr += IN6ADDRSZ - INADDRSZ;
674 af = AF_INET;
675 len = INADDRSZ;
676 }
677 switch (af) {
678 case AF_INET:
679 size = INADDRSZ;
680 break;
681 case AF_INET6:
682 size = IN6ADDRSZ;
683 break;
684 default:
685 errno = EAFNOSUPPORT;
686 h_errno = NETDB_INTERNAL;
687 return (NULL);
688 }
689 if (size != len) {
690 errno = EINVAL;
691 h_errno = NETDB_INTERNAL;
692 return (NULL);
693 }
694 ep = qbuf + sizeof(qbuf);
695 switch (af) {
696 case AF_INET:
697 (void) snprintf(qbuf, sizeof qbuf, "%u.%u.%u.%u.in-addr.arpa",
698 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
699 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
700 break;
701 case AF_INET6:
702 qp = qbuf;
703 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
704 i = snprintf(qp, ep - qp, "%x.%x.",
705 uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
706 if (i <= 0 || i >= ep - qp) {
707 errno = EINVAL;
708 h_errno = NETDB_INTERNAL;
709 return (NULL);
710 }
711 qp += i;
712 }
713 strlcpy(qp, "ip6.arpa", ep - qp);
714 break;
715 }
716
717 bcopy(_resp->lookups, lookups, sizeof lookups);
363 if (lookups[0] == '\0') 718 if (lookups[0] == '\0')
364 strncpy(lookups, "bf", sizeof lookups); 719 strlcpy(lookups, "bf", sizeof lookups);
365 720
366 hp = (struct hostent *)NULL; 721 hp = (struct hostent *)NULL;
367 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { 722 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
368 switch (lookups[i]) { 723 switch (lookups[i]) {
369#ifdef YP 724#ifdef YP
370 case 'y': 725 case 'y':
371 hp = _yp_gethtbyaddr(addr, len, type); 726 /* YP only supports AF_INET. */
727 if (af == AF_INET)
728 hp = _yp_gethtbyaddr(uaddr);
372 break; 729 break;
373#endif 730#endif
374 case 'b': 731 case 'b':
375 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); 732 buf = malloc(sizeof(*buf));
733 if (!buf)
734 break;
735 n = res_query(qbuf, C_IN, T_PTR, buf->buf,
736 sizeof(buf->buf));
376 if (n < 0) { 737 if (n < 0) {
738 free(buf);
377#ifdef DEBUG 739#ifdef DEBUG
378 if (_res.options & RES_DEBUG) 740 if (_resp->options & RES_DEBUG)
379 printf("res_query failed\n"); 741 printf("res_query failed\n");
380#endif 742#endif
381 break; 743 break;
382 } 744 }
383 hp = getanswer(&buf, n, 1); 745 if (!(hp = getanswer(buf, n, qbuf, T_PTR))) {
384 if (hp == NULL) 746 free(buf);
385 break; 747 break;
386 hp->h_addrtype = type; 748 }
749 free(buf);
750 hp->h_addrtype = af;
387 hp->h_length = len; 751 hp->h_length = len;
388 h_addr_ptrs[0] = (char *)&host_addr; 752 bcopy(uaddr, host_addr, len);
389 h_addr_ptrs[1] = (char *)0; 753 h_addr_ptrs[0] = (char *)host_addr;
390 host_addr = *(struct in_addr *)addr; 754 h_addr_ptrs[1] = NULL;
755 if (af == AF_INET && (_resp->options & RES_USE_INET6)) {
756 map_v4v6_address((char*)host_addr,
757 (char*)host_addr);
758 hp->h_addrtype = AF_INET6;
759 hp->h_length = IN6ADDRSZ;
760 }
761 h_errno = NETDB_SUCCESS;
391 break; 762 break;
392 case 'f': 763 case 'f':
393 hp = _gethtbyaddr(addr, len, type); 764 hp = _gethtbyaddr(uaddr, len, af);
394 break; 765 break;
395 } 766 }
396 } 767 }
768 /* XXX h_errno not correct in all cases... */
397 return (hp); 769 return (hp);
398} 770}
399 771
400void 772void
401_sethtent(f) 773_sethtent(int f)
402 int f;
403{ 774{
404 if (hostf == NULL) 775 if (hostf == NULL)
405 hostf = fopen(_PATH_HOSTS, "r" ); 776 hostf = fopen(_PATH_HOSTS, "r" );
@@ -409,7 +780,7 @@ _sethtent(f)
409} 780}
410 781
411void 782void
412_endhtent() 783_endhtent(void)
413{ 784{
414 if (hostf && !stayopen) { 785 if (hostf && !stayopen) {
415 (void) fclose(hostf); 786 (void) fclose(hostf);
@@ -417,40 +788,66 @@ _endhtent()
417 } 788 }
418} 789}
419 790
420struct hostent * 791static struct hostent *
421_gethtent() 792_gethtent(void)
422{ 793{
423 char *p; 794 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
424 register char *cp, **q; 795 char *p, *cp, **q;
796 int af;
797 size_t len;
425 798
426 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) 799 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
800 h_errno = NETDB_INTERNAL;
427 return (NULL); 801 return (NULL);
428again: 802 }
429 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) 803 again:
804 if ((p = fgetln(hostf, &len)) == NULL) {
805 h_errno = HOST_NOT_FOUND;
430 return (NULL); 806 return (NULL);
431 if (*p == '#') 807 }
808 if (p[len-1] == '\n')
809 len--;
810 if (len >= sizeof(hostbuf) || len == 0)
432 goto again; 811 goto again;
433 cp = strpbrk(p, "#\n"); 812 p = memcpy(hostbuf, p, len);
434 if (cp == NULL) 813 hostbuf[len] = '\0';
814 if (*p == '#')
435 goto again; 815 goto again;
436 *cp = '\0'; 816 if ((cp = strchr(p, '#')))
437 cp = strpbrk(p, " \t"); 817 *cp = '\0';
438 if (cp == NULL) 818 if (!(cp = strpbrk(p, " \t")))
439 goto again; 819 goto again;
440 *cp++ = '\0'; 820 *cp++ = '\0';
441 /* THIS STUFF IS INTERNET SPECIFIC */ 821 if (inet_pton(AF_INET6, p, host_addr) > 0) {
442 h_addr_ptrs[0] = (char *)&host_addr; 822 af = AF_INET6;
823 len = IN6ADDRSZ;
824 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
825 if (_resp->options & RES_USE_INET6) {
826 map_v4v6_address((char*)host_addr, (char*)host_addr);
827 af = AF_INET6;
828 len = IN6ADDRSZ;
829 } else {
830 af = AF_INET;
831 len = INADDRSZ;
832 }
833 } else {
834 goto again;
835 }
836 /* if this is not something we're looking for, skip it. */
837 if (host.h_addrtype != AF_UNSPEC && host.h_addrtype != af)
838 goto again;
839 if (host.h_length != 0 && host.h_length != len)
840 goto again;
841 h_addr_ptrs[0] = (char *)host_addr;
443 h_addr_ptrs[1] = NULL; 842 h_addr_ptrs[1] = NULL;
444 (void) inet_aton(p, &host_addr);
445 host.h_addr_list = h_addr_ptrs; 843 host.h_addr_list = h_addr_ptrs;
446 host.h_length = sizeof(u_int32_t); 844 host.h_length = len;
447 host.h_addrtype = AF_INET; 845 host.h_addrtype = af;
448 while (*cp == ' ' || *cp == '\t') 846 while (*cp == ' ' || *cp == '\t')
449 cp++; 847 cp++;
450 host.h_name = cp; 848 host.h_name = cp;
451 q = host.h_aliases = host_aliases; 849 q = host.h_aliases = host_aliases;
452 cp = strpbrk(cp, " \t"); 850 if ((cp = strpbrk(cp, " \t")))
453 if (cp != NULL)
454 *cp++ = '\0'; 851 *cp++ = '\0';
455 while (cp && *cp) { 852 while (cp && *cp) {
456 if (*cp == ' ' || *cp == '\t') { 853 if (*cp == ' ' || *cp == '\t') {
@@ -459,74 +856,61 @@ again:
459 } 856 }
460 if (q < &host_aliases[MAXALIASES - 1]) 857 if (q < &host_aliases[MAXALIASES - 1])
461 *q++ = cp; 858 *q++ = cp;
462 cp = strpbrk(cp, " \t"); 859 if ((cp = strpbrk(cp, " \t")))
463 if (cp != NULL)
464 *cp++ = '\0'; 860 *cp++ = '\0';
465 } 861 }
466 *q = NULL; 862 *q = NULL;
863 if (_resp->options & RES_USE_INET6) {
864 char *bp = hostbuf;
865 char *ep = hostbuf + sizeof hostbuf;
866
867 map_v4v6_hostent(&host, &bp, ep);
868 }
869 h_errno = NETDB_SUCCESS;
467 return (&host); 870 return (&host);
468} 871}
469 872
470struct hostent * 873struct hostent *
471_gethtbyname(name) 874_gethtbyname2(const char *name, int af)
472 char *name;
473{ 875{
474 register struct hostent *p; 876 struct hostent *p;
475 register char **cp; 877 char **cp;
476 878
477 _sethtent(0); 879 _sethtent(0);
478 while (p = _gethtent()) { 880 while ((p = _gethtent())) {
881 if (p->h_addrtype != af)
882 continue;
479 if (strcasecmp(p->h_name, name) == 0) 883 if (strcasecmp(p->h_name, name) == 0)
480 break; 884 break;
481 for (cp = p->h_aliases; *cp != 0; cp++) 885 for (cp = p->h_aliases; *cp != 0; cp++)
482 if (strcasecmp(*cp, name) == 0) 886 if (strcasecmp(*cp, name) == 0)
483 goto found; 887 goto found;
484 } 888 }
485found: 889 found:
486 _endhtent(); 890 _endhtent();
487 if (p==NULL)
488 h_errno = HOST_NOT_FOUND;
489 return (p); 891 return (p);
490} 892}
491 893
492struct hostent * 894struct hostent *
493_gethtbyaddr(addr, len, type) 895_gethtbyaddr(const void *addr, socklen_t len, int af)
494 const char *addr;
495 int len, type;
496{ 896{
497 register struct hostent *p; 897 struct hostent *p;
898
899 host.h_length = len;
900 host.h_addrtype = af;
498 901
499 _sethtent(0); 902 _sethtent(0);
500 while (p = _gethtent()) 903 while ((p = _gethtent()))
501 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 904 if (p->h_addrtype == af && p->h_length == len &&
905 !bcmp(p->h_addr, addr, len))
502 break; 906 break;
503 _endhtent(); 907 _endhtent();
504 if (p==NULL)
505 h_errno = HOST_NOT_FOUND;
506 return (p); 908 return (p);
507} 909}
508 910
509static int
510qcomp(a1, a2)
511 struct in_addr **a1, **a2;
512{
513 int pos1, pos2;
514
515 for (pos1 = 0; pos1 < _res.nsort; pos1++)
516 if (_res.sort_list[pos1].addr.s_addr ==
517 ((*a1)->s_addr & _res.sort_list[pos1].mask))
518 break;
519 for (pos2 = 0; pos2 < _res.nsort; pos2++)
520 if (_res.sort_list[pos2].addr.s_addr ==
521 ((*a2)->s_addr & _res.sort_list[pos2].mask))
522 break;
523 return pos1 - pos2;
524}
525
526#ifdef YP 911#ifdef YP
527struct hostent * 912struct hostent *
528_yphostent(line) 913_yphostent(char *line)
529 char *line;
530{ 914{
531 static struct in_addr host_addrs[MAXADDRS]; 915 static struct in_addr host_addrs[MAXADDRS];
532 char *p = line; 916 char *p = line;
@@ -537,21 +921,21 @@ _yphostent(line)
537 921
538 host.h_name = NULL; 922 host.h_name = NULL;
539 host.h_addr_list = h_addr_ptrs; 923 host.h_addr_list = h_addr_ptrs;
540 host.h_length = sizeof(u_int32_t); 924 host.h_length = INADDRSZ;
541 host.h_addrtype = AF_INET; 925 host.h_addrtype = AF_INET;
542 hap = h_addr_ptrs; 926 hap = h_addr_ptrs;
543 buf = host_addrs; 927 buf = host_addrs;
544 q = host.h_aliases = host_aliases; 928 q = host.h_aliases = host_aliases;
545 929
546nextline: 930nextline:
931 /* check for host_addrs overflow */
932 if (buf >= &host_addrs[sizeof(host_addrs) / sizeof(host_addrs[0])])
933 goto done;
934
547 more = 0; 935 more = 0;
548 cp = strpbrk(p, " \t"); 936 cp = strpbrk(p, " \t");
549 if (cp == NULL) { 937 if (cp == NULL)
550 if (host.h_name == NULL) 938 goto done;
551 return (NULL);
552 else
553 goto done;
554 }
555 *cp++ = '\0'; 939 *cp++ = '\0';
556 940
557 *hap++ = (char *)buf; 941 *hap++ = (char *)buf;
@@ -592,30 +976,28 @@ nextline:
592 *cp++ = '\0'; 976 *cp++ = '\0';
593 } 977 }
594done: 978done:
979 if (host.h_name == NULL)
980 return (NULL);
595 *q = NULL; 981 *q = NULL;
596 *hap = NULL; 982 *hap = NULL;
597 return (&host); 983 return (&host);
598} 984}
599 985
600struct hostent * 986struct hostent *
601_yp_gethtbyaddr(addr, len, type) 987_yp_gethtbyaddr(const void *addr)
602 const char *addr;
603 int len, type;
604{ 988{
605 struct hostent *hp = (struct hostent *)NULL; 989 struct hostent *hp = NULL;
990 const u_char *uaddr = (const u_char *)addr;
606 static char *__ypcurrent; 991 static char *__ypcurrent;
607 int __ypcurrentlen, r; 992 int __ypcurrentlen, r;
608 char name[sizeof("xxx.xxx.xxx.xxx") + 1]; 993 char name[sizeof("xxx.xxx.xxx.xxx")];
609 994
610 if (!__ypdomain) { 995 if (!__ypdomain) {
611 if (_yp_check(&__ypdomain) == 0) 996 if (_yp_check(&__ypdomain) == 0)
612 return (hp); 997 return (hp);
613 } 998 }
614 sprintf(name, "%u.%u.%u.%u", 999 snprintf(name, sizeof name, "%u.%u.%u.%u", (uaddr[0] & 0xff),
615 ((unsigned)addr[0] & 0xff), 1000 (uaddr[1] & 0xff), (uaddr[2] & 0xff), (uaddr[3] & 0xff));
616 ((unsigned)addr[1] & 0xff),
617 ((unsigned)addr[2] & 0xff),
618 ((unsigned)addr[3] & 0xff));
619 if (__ypcurrent) 1001 if (__ypcurrent)
620 free(__ypcurrent); 1002 free(__ypcurrent);
621 __ypcurrent = NULL; 1003 __ypcurrent = NULL;
@@ -629,13 +1011,14 @@ _yp_gethtbyaddr(addr, len, type)
629} 1011}
630 1012
631struct hostent * 1013struct hostent *
632_yp_gethtbyname(name) 1014_yp_gethtbyname(const char *name)
633 const char *name;
634{ 1015{
635 struct hostent *hp = (struct hostent *)NULL; 1016 struct hostent *hp = (struct hostent *)NULL;
636 static char *__ypcurrent; 1017 static char *__ypcurrent;
637 int __ypcurrentlen, r; 1018 int __ypcurrentlen, r;
638 1019
1020 if (strlen(name) >= MAXHOSTNAMELEN)
1021 return (NULL);
639 if (!__ypdomain) { 1022 if (!__ypdomain) {
640 if (_yp_check(&__ypdomain) == 0) 1023 if (_yp_check(&__ypdomain) == 0)
641 return (hp); 1024 return (hp);
@@ -645,10 +1028,104 @@ _yp_gethtbyname(name)
645 __ypcurrent = NULL; 1028 __ypcurrent = NULL;
646 r = yp_match(__ypdomain, "hosts.byname", name, 1029 r = yp_match(__ypdomain, "hosts.byname", name,
647 strlen(name), &__ypcurrent, &__ypcurrentlen); 1030 strlen(name), &__ypcurrent, &__ypcurrentlen);
648 if (r==0) 1031 if (r == 0)
649 hp = _yphostent(__ypcurrent); 1032 hp = _yphostent(__ypcurrent);
650 if (hp==NULL) 1033 if (hp == NULL)
651 h_errno = HOST_NOT_FOUND; 1034 h_errno = HOST_NOT_FOUND;
652 return (hp); 1035 return (hp);
653} 1036}
654#endif 1037#endif
1038
1039static void
1040map_v4v6_address(const char *src, char *dst)
1041{
1042 u_char *p = (u_char *)dst;
1043 char tmp[INADDRSZ];
1044 int i;
1045
1046 /* Stash a temporary copy so our caller can update in place. */
1047 bcopy(src, tmp, INADDRSZ);
1048 /* Mark this ipv6 addr as a mapped ipv4. */
1049 for (i = 0; i < 10; i++)
1050 *p++ = 0x00;
1051 *p++ = 0xff;
1052 *p++ = 0xff;
1053 /* Retrieve the saved copy and we're done. */
1054 bcopy(tmp, (void*)p, INADDRSZ);
1055}
1056
1057static void
1058map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1059{
1060 char **ap;
1061
1062 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1063 return;
1064 hp->h_addrtype = AF_INET6;
1065 hp->h_length = IN6ADDRSZ;
1066 for (ap = hp->h_addr_list; *ap; ap++) {
1067 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
1068
1069 if (ep - *bpp < (i + IN6ADDRSZ)) {
1070 /* Out of memory. Truncate address list here. XXX */
1071 *ap = NULL;
1072 return;
1073 }
1074 *bpp += i;
1075 map_v4v6_address(*ap, *bpp);
1076 *ap = *bpp;
1077 *bpp += IN6ADDRSZ;
1078 }
1079}
1080
1081struct hostent *
1082gethostent(void)
1083{
1084 host.h_addrtype = AF_UNSPEC;
1085 host.h_length = 0;
1086 return (_gethtent());
1087}
1088
1089#ifdef RESOLVSORT
1090static void
1091addrsort(char **ap, int num)
1092{
1093 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
1094 int i, j;
1095 char **p;
1096 short aval[MAXADDRS];
1097 int needsort = 0;
1098
1099 p = ap;
1100 for (i = 0; i < num; i++, p++) {
1101 for (j = 0 ; (unsigned)j < _resp->nsort; j++)
1102 if (_resp->sort_list[j].addr.s_addr ==
1103 (((struct in_addr *)(*p))->s_addr &
1104 _resp->sort_list[j].mask))
1105 break;
1106 aval[i] = j;
1107 if (needsort == 0 && i > 0 && j < aval[i-1])
1108 needsort = i;
1109 }
1110 if (!needsort)
1111 return;
1112
1113 while (needsort < num) {
1114 for (j = needsort - 1; j >= 0; j--) {
1115 if (aval[j] > aval[j+1]) {
1116 char *hp;
1117
1118 i = aval[j];
1119 aval[j] = aval[j+1];
1120 aval[j+1] = i;
1121
1122 hp = ap[j];
1123 ap[j] = ap[j+1];
1124 ap[j+1] = hp;
1125 } else
1126 break;
1127 }
1128 needsort++;
1129 }
1130}
1131#endif