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.c975
1 files changed, 725 insertions, 250 deletions
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c
index ec3f14a900..d49b9cdd7d 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.72 2007/10/11 18:36:41 jakob 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,323 @@ 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 syslog(LOG_NOTICE|LOG_AUTH,
298 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
299 qname, p_class(C_IN), p_type(qtype),
300 p_type(type));
301 cp += n;
302 continue; /* XXX - had_error++ ? */
303 }
304 switch (type) {
305 case T_PTR:
306 if (strcasecmp(tname, bp) != 0) {
307 syslog(LOG_NOTICE|LOG_AUTH,
308 AskedForGot, qname, bp);
216 cp += n; 309 cp += n;
217 continue; 310 continue; /* XXX - had_error++ ? */
311 }
312 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
313#ifdef USE_RESOLV_NAME_OK
314 if ((n < 0) || !res_hnok(bp)) {
315#else
316 if ((n < 0) || !_hokchar(bp)) {
317#endif
318 had_error++;
319 break;
320 }
321#if MULTI_PTRS_ARE_ALIASES
322 cp += n;
323 if (!haveanswer)
324 host.h_name = bp;
325 else if (ap < &host_aliases[MAXALIASES-1])
326 *ap++ = bp;
327 else
328 n = -1;
329 if (n != -1) {
330 n = strlen(bp) + 1; /* for the \0 */
331 bp += n;
332 }
333 break;
334#else
335 host.h_name = bp;
336 if (_resp->options & RES_USE_INET6) {
337 n = strlen(bp) + 1; /* for the \0 */
338 bp += n;
339 map_v4v6_hostent(&host, &bp, ep);
340 }
341 h_errno = NETDB_SUCCESS;
342 return (&host);
343#endif
344 case T_A:
345 case T_AAAA:
346 if (strcasecmp(host.h_name, bp) != 0) {
347 syslog(LOG_NOTICE|LOG_AUTH,
348 AskedForGot, host.h_name, bp);
349 cp += n;
350 continue; /* XXX - had_error++ ? */
218 } 351 }
219 if (class != getclass) { 352 if (n != host.h_length) {
220 cp += n; 353 cp += n;
221 continue; 354 continue;
222 } 355 }
223 } else { 356 if (type == T_AAAA) {
224 host.h_length = n; 357 struct in6_addr in6;
225 getclass = class; 358 memcpy(&in6, cp, IN6ADDRSZ);
226 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 359 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
227 if (!iquery) { 360 cp += n;
361 continue;
362 }
363 }
364 if (!haveanswer) {
228 host.h_name = bp; 365 host.h_name = bp;
229 bp += strlen(bp) + 1; 366 bp += strlen(bp) + 1; /* for the \0 */
230 } 367 }
231 }
232 368
233 bp += sizeof(align) - ((u_long)bp % sizeof(align)); 369 bp += sizeof(align) - ((u_long)bp % sizeof(align));
234 370
235 if (bp + n >= &hostbuf[sizeof(hostbuf)]) { 371 if (bp + n >= &hostbuf[sizeof hostbuf]) {
236#ifdef DEBUG 372#ifdef DEBUG
237 if (_res.options & RES_DEBUG) 373 if (_resp->options & RES_DEBUG)
238 printf("size (%d) too big\n", n); 374 printf("size (%d) too big\n", n);
239#endif 375#endif
376 had_error++;
377 continue;
378 }
379 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
380 if (!toobig++)
381#ifdef DEBUG
382 if (_resp->options & RES_DEBUG)
383 printf("Too many addresses (%d)\n", MAXADDRS);
384#endif
385 cp += n;
386 continue;
387 }
388 bcopy(cp, *hap++ = bp, n);
389 bp += n;
390 cp += n;
240 break; 391 break;
241 } 392 }
242 bcopy(cp, *hap++ = bp, n); 393 if (!had_error)
243 bp +=n; 394 haveanswer++;
244 cp += n;
245 haveanswer++;
246 } 395 }
247 if (haveanswer) { 396 if (haveanswer) {
248 *ap = NULL; 397 *ap = NULL;
249 *hap = NULL; 398 *hap = NULL;
250 if (_res.nsort) { 399# if defined(RESOLVSORT)
251 qsort(host.h_addr_list, haveanswer, 400 /*
252 sizeof(struct in_addr), 401 * Note: we sort even if host can take only one address
253 (int (*)__P((const void *, const void *)))qcomp); 402 * in its return structures - should give it the "best"
403 * address in that case, not some random one
404 */
405 if (_resp->nsort && haveanswer > 1 && qtype == T_A)
406 addrsort(h_addr_ptrs, haveanswer);
407# endif /*RESOLVSORT*/
408 if (!host.h_name) {
409 size_t len;
410
411 len = strlen(qname) + 1;
412 if (len > ep - bp) /* for the \0 */
413 goto try_again;
414 strlcpy(bp, qname, ep - bp);
415 host.h_name = bp;
416 bp += len;
254 } 417 }
418 if (_resp->options & RES_USE_INET6)
419 map_v4v6_hostent(&host, &bp, ep);
420 h_errno = NETDB_SUCCESS;
255 return (&host); 421 return (&host);
256 } else {
257 h_errno = TRY_AGAIN;
258 return ((struct hostent *) NULL);
259 } 422 }
423 try_again:
424 h_errno = TRY_AGAIN;
425 return (NULL);
260} 426}
261 427
428#ifdef notyet
429/*
430 * XXX This is an extremely bogus implementation.
431 *
432 * FreeBSD has this interface:
433 * int gethostbyaddr_r(const char *addr, int len, int type,
434 * struct hostent *result, struct hostent_data *buffer)
435 */
436
437struct hostent *
438gethostbyname_r(const char *name, struct hostent *hp, char *buf, int buflen,
439 int *errorp)
440{
441 struct hostent *res;
442
443 res = gethostbyname(name);
444 *errorp = h_errno;
445 if (res == NULL)
446 return NULL;
447 memcpy(hp, res, sizeof *hp); /* XXX not sufficient */
448 return hp;
449}
450
451/*
452 * XXX This is an extremely bogus implementation.
453 */
262struct hostent * 454struct hostent *
263gethostbyname(name) 455gethostbyaddr_r(const char *addr, int len, int af, struct hostent *he,
264 const char *name; 456 char *buf, int buflen, int *errorp)
265{ 457{
266 querybuf buf; 458 struct hostent * res;
267 register const char *cp; 459
268 int n, i; 460 res = gethostbyaddr(addr, len, af);
269 extern struct hostent *_gethtbyname(), *_yp_gethtbyname(); 461 *errorp = h_errno;
270 register struct hostent *hp; 462 if (res == NULL)
463 return NULL;
464 memcpy(he, res, sizeof *he); /* XXX not sufficient */
465 return he;
466}
467
468/* XXX RFC2133 expects a gethostbyname2_r() -- unimplemented */
469#endif
470
471struct hostent *
472gethostbyname(const char *name)
473{
474 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
475 struct hostent *hp;
476 extern struct hostent *_gethtbyname2(const char *, int);
477
478 if (_res_init(0) == -1)
479 hp = _gethtbyname2(name, AF_INET);
480
481 else if (_resp->options & RES_USE_INET6) {
482 hp = gethostbyname2(name, AF_INET6);
483 if (hp == NULL)
484 hp = gethostbyname2(name, AF_INET);
485 }
486 else
487 hp = gethostbyname2(name, AF_INET);
488 return hp;
489}
490
491struct hostent *
492gethostbyname2(const char *name, int af)
493{
494 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
495 querybuf *buf;
496 const char *cp;
497 char *bp, *ep;
498 int n, size, type, i;
499 struct hostent *hp;
271 char lookups[MAXDNSLUS]; 500 char lookups[MAXDNSLUS];
501 extern struct hostent *_gethtbyname2(const char *, int);
502#ifdef YP
503 extern struct hostent *_yp_gethtbyname(const char *);
504#endif
505
506 if (_res_init(0) == -1)
507 return (_gethtbyname2(name, af));
508
509 switch (af) {
510 case AF_INET:
511 size = INADDRSZ;
512 type = T_A;
513 break;
514 case AF_INET6:
515 size = IN6ADDRSZ;
516 type = T_AAAA;
517 break;
518 default:
519 h_errno = NETDB_INTERNAL;
520 errno = EAFNOSUPPORT;
521 return (NULL);
522 }
523
524 host.h_addrtype = af;
525 host.h_length = size;
526
527 /*
528 * if there aren't any dots, it could be a user-level alias.
529 * this is also done in res_query() since we are not the only
530 * function that looks up host names.
531 */
532 if (!strchr(name, '.') && (cp = __hostalias(name)))
533 name = cp;
272 534
273 /* 535 /*
274 * disallow names consisting only of digits/dots, unless 536 * disallow names consisting only of digits/dots, unless
@@ -284,122 +546,229 @@ gethostbyname(name)
284 * Fake up a hostent as if we'd actually 546 * Fake up a hostent as if we'd actually
285 * done a lookup. 547 * done a lookup.
286 */ 548 */
287 if (!inet_aton(name, &host_addr)) { 549 if (inet_pton(af, name, host_addr) <= 0) {
288 h_errno = HOST_NOT_FOUND; 550 h_errno = HOST_NOT_FOUND;
289 return((struct hostent *) NULL); 551 return (NULL);
290 } 552 }
291 host.h_name = (char *)name; 553 strlcpy(hostbuf, name, MAXHOSTNAMELEN);
554 bp = hostbuf + MAXHOSTNAMELEN;
555 ep = hostbuf + sizeof(hostbuf);
556 host.h_name = hostbuf;
292 host.h_aliases = host_aliases; 557 host.h_aliases = host_aliases;
293 host_aliases[0] = NULL; 558 host_aliases[0] = NULL;
294 host.h_addrtype = AF_INET; 559 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; 560 h_addr_ptrs[1] = NULL;
298 host.h_addr_list = h_addr_ptrs; 561 host.h_addr_list = h_addr_ptrs;
562 if (_resp->options & RES_USE_INET6)
563 map_v4v6_hostent(&host, &bp, ep);
564 h_errno = NETDB_SUCCESS;
299 return (&host); 565 return (&host);
300 } 566 }
301 if (!isdigit(*cp) && *cp != '.') 567 if (!isdigit(*cp) && *cp != '.')
302 break; 568 break;
303 } 569 }
570 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
571 name[0] == ':')
572 for (cp = name;; ++cp) {
573 if (!*cp) {
574 if (*--cp == '.')
575 break;
576 /*
577 * All-IPv6-legal, no dot at the end.
578 * Fake up a hostent as if we'd actually
579 * done a lookup.
580 */
581 if (inet_pton(af, name, host_addr) <= 0) {
582 h_errno = HOST_NOT_FOUND;
583 return (NULL);
584 }
585 strlcpy(hostbuf, name, MAXHOSTNAMELEN);
586 bp = hostbuf + MAXHOSTNAMELEN;
587 ep = hostbuf + sizeof(hostbuf);
588 host.h_name = hostbuf;
589 host.h_aliases = host_aliases;
590 host_aliases[0] = NULL;
591 h_addr_ptrs[0] = (char *)host_addr;
592 h_addr_ptrs[1] = NULL;
593 host.h_addr_list = h_addr_ptrs;
594 h_errno = NETDB_SUCCESS;
595 return (&host);
596 }
597 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
598 break;
599 }
304 600
305 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 601 bcopy(_resp->lookups, lookups, sizeof lookups);
306 return (_gethtbyname(name));
307
308 bcopy(_res.lookups, lookups, sizeof lookups);
309 if (lookups[0] == '\0') 602 if (lookups[0] == '\0')
310 strncpy(lookups, "bf", sizeof lookups); 603 strlcpy(lookups, "bf", sizeof lookups);
311 604
312 hp = (struct hostent *)NULL; 605 hp = (struct hostent *)NULL;
313 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { 606 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
314 switch (lookups[i]) { 607 switch (lookups[i]) {
315#ifdef YP 608#ifdef YP
316 case 'y': 609 case 'y':
317 hp = _yp_gethtbyname(name); 610 /* YP only supports AF_INET. */
611 if (af == AF_INET)
612 hp = _yp_gethtbyname(name);
318 break; 613 break;
319#endif 614#endif
320 case 'b': 615 case 'b':
321 if ((n = res_search(name, C_IN, T_A, buf.buf, 616 buf = malloc(sizeof(*buf));
322 sizeof(buf))) < 0) { 617 if (buf == NULL)
618 break;
619 if ((n = res_search(name, C_IN, type, buf->buf,
620 sizeof(buf->buf))) < 0) {
621 free(buf);
323#ifdef DEBUG 622#ifdef DEBUG
324 if (_res.options & RES_DEBUG) 623 if (_resp->options & RES_DEBUG)
325 printf("res_search failed\n"); 624 printf("res_search failed\n");
326#endif 625#endif
327 break; 626 break;
328 } 627 }
329 hp = getanswer(&buf, n, 0); 628 hp = getanswer(buf, n, name, type);
629 free(buf);
330 break; 630 break;
331 case 'f': 631 case 'f':
332 hp = _gethtbyname(name); 632 hp = _gethtbyname2(name, af);
333 break; 633 break;
334 } 634 }
335 } 635 }
636 /* XXX h_errno not correct in all cases... */
336 return (hp); 637 return (hp);
337} 638}
338 639
339struct hostent * 640struct hostent *
340gethostbyaddr(addr, len, type) 641gethostbyaddr(const void *addr, socklen_t len, int af)
341 const char *addr;
342 int len, type;
343{ 642{
344 int n, i; 643 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
345 querybuf buf; 644 const u_char *uaddr = (const u_char *)addr;
346 register struct hostent *hp; 645 int n, size, i;
347 char qbuf[MAXDNAME]; 646 querybuf *buf;
348 extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); 647 struct hostent *hp;
648 char qbuf[MAXDNAME+1], *qp, *ep;
349 char lookups[MAXDNSLUS]; 649 char lookups[MAXDNSLUS];
650 struct hostent *res;
651 extern struct hostent *_gethtbyaddr(const void *, socklen_t, int);
652#ifdef YP
653 extern struct hostent *_yp_gethtbyaddr(const void *);
654#endif
350 655
351 if (type != AF_INET) 656 if (_res_init(0) == -1) {
352 return ((struct hostent *) NULL); 657 res = _gethtbyaddr(addr, len, af);
353 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", 658 return (res);
354 ((unsigned)addr[3] & 0xff), 659 }
355 ((unsigned)addr[2] & 0xff), 660
356 ((unsigned)addr[1] & 0xff), 661 if (af == AF_INET6 && len == IN6ADDRSZ &&
357 ((unsigned)addr[0] & 0xff)); 662 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)uaddr) ||
358 663 IN6_IS_ADDR_SITELOCAL((struct in6_addr *)uaddr))) {
359 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 664 h_errno = HOST_NOT_FOUND;
360 return (_gethtbyaddr(addr, len, type)); 665 return (NULL);
361 666 }
362 bcopy(_res.lookups, lookups, sizeof lookups); 667 if (af == AF_INET6 && len == IN6ADDRSZ &&
668 (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)uaddr) ||
669 IN6_IS_ADDR_V4COMPAT((struct in6_addr *)uaddr))) {
670 /* Unmap. */
671 uaddr += IN6ADDRSZ - INADDRSZ;
672 af = AF_INET;
673 len = INADDRSZ;
674 }
675 switch (af) {
676 case AF_INET:
677 size = INADDRSZ;
678 break;
679 case AF_INET6:
680 size = IN6ADDRSZ;
681 break;
682 default:
683 errno = EAFNOSUPPORT;
684 h_errno = NETDB_INTERNAL;
685 return (NULL);
686 }
687 if (size != len) {
688 errno = EINVAL;
689 h_errno = NETDB_INTERNAL;
690 return (NULL);
691 }
692 ep = qbuf + sizeof(qbuf);
693 switch (af) {
694 case AF_INET:
695 (void) snprintf(qbuf, sizeof qbuf, "%u.%u.%u.%u.in-addr.arpa",
696 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
697 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
698 break;
699 case AF_INET6:
700 qp = qbuf;
701 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
702 i = snprintf(qp, ep - qp, "%x.%x.",
703 uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
704 if (i <= 0 || i >= ep - qp) {
705 errno = EINVAL;
706 h_errno = NETDB_INTERNAL;
707 return (NULL);
708 }
709 qp += i;
710 }
711 strlcpy(qp, "ip6.arpa", ep - qp);
712 break;
713 }
714
715 bcopy(_resp->lookups, lookups, sizeof lookups);
363 if (lookups[0] == '\0') 716 if (lookups[0] == '\0')
364 strncpy(lookups, "bf", sizeof lookups); 717 strlcpy(lookups, "bf", sizeof lookups);
365 718
366 hp = (struct hostent *)NULL; 719 hp = (struct hostent *)NULL;
367 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { 720 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
368 switch (lookups[i]) { 721 switch (lookups[i]) {
369#ifdef YP 722#ifdef YP
370 case 'y': 723 case 'y':
371 hp = _yp_gethtbyaddr(addr, len, type); 724 /* YP only supports AF_INET. */
725 if (af == AF_INET)
726 hp = _yp_gethtbyaddr(uaddr);
372 break; 727 break;
373#endif 728#endif
374 case 'b': 729 case 'b':
375 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); 730 buf = malloc(sizeof(*buf));
731 if (!buf)
732 break;
733 n = res_query(qbuf, C_IN, T_PTR, buf->buf,
734 sizeof(buf->buf));
376 if (n < 0) { 735 if (n < 0) {
736 free(buf);
377#ifdef DEBUG 737#ifdef DEBUG
378 if (_res.options & RES_DEBUG) 738 if (_resp->options & RES_DEBUG)
379 printf("res_query failed\n"); 739 printf("res_query failed\n");
380#endif 740#endif
381 break; 741 break;
382 } 742 }
383 hp = getanswer(&buf, n, 1); 743 if (!(hp = getanswer(buf, n, qbuf, T_PTR))) {
384 if (hp == NULL) 744 free(buf);
385 break; 745 break;
386 hp->h_addrtype = type; 746 }
747 free(buf);
748 hp->h_addrtype = af;
387 hp->h_length = len; 749 hp->h_length = len;
388 h_addr_ptrs[0] = (char *)&host_addr; 750 bcopy(uaddr, host_addr, len);
389 h_addr_ptrs[1] = (char *)0; 751 h_addr_ptrs[0] = (char *)host_addr;
390 host_addr = *(struct in_addr *)addr; 752 h_addr_ptrs[1] = NULL;
753 if (af == AF_INET && (_resp->options & RES_USE_INET6)) {
754 map_v4v6_address((char*)host_addr,
755 (char*)host_addr);
756 hp->h_addrtype = AF_INET6;
757 hp->h_length = IN6ADDRSZ;
758 }
759 h_errno = NETDB_SUCCESS;
391 break; 760 break;
392 case 'f': 761 case 'f':
393 hp = _gethtbyaddr(addr, len, type); 762 hp = _gethtbyaddr(uaddr, len, af);
394 break; 763 break;
395 } 764 }
396 } 765 }
766 /* XXX h_errno not correct in all cases... */
397 return (hp); 767 return (hp);
398} 768}
399 769
400void 770void
401_sethtent(f) 771_sethtent(int f)
402 int f;
403{ 772{
404 if (hostf == NULL) 773 if (hostf == NULL)
405 hostf = fopen(_PATH_HOSTS, "r" ); 774 hostf = fopen(_PATH_HOSTS, "r" );
@@ -409,7 +778,7 @@ _sethtent(f)
409} 778}
410 779
411void 780void
412_endhtent() 781_endhtent(void)
413{ 782{
414 if (hostf && !stayopen) { 783 if (hostf && !stayopen) {
415 (void) fclose(hostf); 784 (void) fclose(hostf);
@@ -417,40 +786,66 @@ _endhtent()
417 } 786 }
418} 787}
419 788
420struct hostent * 789static struct hostent *
421_gethtent() 790_gethtent(void)
422{ 791{
423 char *p; 792 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
424 register char *cp, **q; 793 char *p, *cp, **q;
794 int af;
795 size_t len;
425 796
426 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) 797 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
798 h_errno = NETDB_INTERNAL;
427 return (NULL); 799 return (NULL);
428again: 800 }
429 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) 801 again:
802 if ((p = fgetln(hostf, &len)) == NULL) {
803 h_errno = HOST_NOT_FOUND;
430 return (NULL); 804 return (NULL);
431 if (*p == '#') 805 }
806 if (p[len-1] == '\n')
807 len--;
808 if (len >= sizeof(hostbuf) || len == 0)
432 goto again; 809 goto again;
433 cp = strpbrk(p, "#\n"); 810 p = memcpy(hostbuf, p, len);
434 if (cp == NULL) 811 hostbuf[len] = '\0';
812 if (*p == '#')
435 goto again; 813 goto again;
436 *cp = '\0'; 814 if ((cp = strchr(p, '#')))
437 cp = strpbrk(p, " \t"); 815 *cp = '\0';
438 if (cp == NULL) 816 if (!(cp = strpbrk(p, " \t")))
439 goto again; 817 goto again;
440 *cp++ = '\0'; 818 *cp++ = '\0';
441 /* THIS STUFF IS INTERNET SPECIFIC */ 819 if (inet_pton(AF_INET6, p, host_addr) > 0) {
442 h_addr_ptrs[0] = (char *)&host_addr; 820 af = AF_INET6;
821 len = IN6ADDRSZ;
822 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
823 if (_resp->options & RES_USE_INET6) {
824 map_v4v6_address((char*)host_addr, (char*)host_addr);
825 af = AF_INET6;
826 len = IN6ADDRSZ;
827 } else {
828 af = AF_INET;
829 len = INADDRSZ;
830 }
831 } else {
832 goto again;
833 }
834 /* if this is not something we're looking for, skip it. */
835 if (host.h_addrtype != AF_UNSPEC && host.h_addrtype != af)
836 goto again;
837 if (host.h_length != 0 && host.h_length != len)
838 goto again;
839 h_addr_ptrs[0] = (char *)host_addr;
443 h_addr_ptrs[1] = NULL; 840 h_addr_ptrs[1] = NULL;
444 (void) inet_aton(p, &host_addr);
445 host.h_addr_list = h_addr_ptrs; 841 host.h_addr_list = h_addr_ptrs;
446 host.h_length = sizeof(u_int32_t); 842 host.h_length = len;
447 host.h_addrtype = AF_INET; 843 host.h_addrtype = af;
448 while (*cp == ' ' || *cp == '\t') 844 while (*cp == ' ' || *cp == '\t')
449 cp++; 845 cp++;
450 host.h_name = cp; 846 host.h_name = cp;
451 q = host.h_aliases = host_aliases; 847 q = host.h_aliases = host_aliases;
452 cp = strpbrk(cp, " \t"); 848 if ((cp = strpbrk(cp, " \t")))
453 if (cp != NULL)
454 *cp++ = '\0'; 849 *cp++ = '\0';
455 while (cp && *cp) { 850 while (cp && *cp) {
456 if (*cp == ' ' || *cp == '\t') { 851 if (*cp == ' ' || *cp == '\t') {
@@ -459,74 +854,61 @@ again:
459 } 854 }
460 if (q < &host_aliases[MAXALIASES - 1]) 855 if (q < &host_aliases[MAXALIASES - 1])
461 *q++ = cp; 856 *q++ = cp;
462 cp = strpbrk(cp, " \t"); 857 if ((cp = strpbrk(cp, " \t")))
463 if (cp != NULL)
464 *cp++ = '\0'; 858 *cp++ = '\0';
465 } 859 }
466 *q = NULL; 860 *q = NULL;
861 if (_resp->options & RES_USE_INET6) {
862 char *bp = hostbuf;
863 char *ep = hostbuf + sizeof hostbuf;
864
865 map_v4v6_hostent(&host, &bp, ep);
866 }
867 h_errno = NETDB_SUCCESS;
467 return (&host); 868 return (&host);
468} 869}
469 870
470struct hostent * 871struct hostent *
471_gethtbyname(name) 872_gethtbyname2(const char *name, int af)
472 char *name;
473{ 873{
474 register struct hostent *p; 874 struct hostent *p;
475 register char **cp; 875 char **cp;
476 876
477 _sethtent(0); 877 _sethtent(0);
478 while (p = _gethtent()) { 878 while ((p = _gethtent())) {
879 if (p->h_addrtype != af)
880 continue;
479 if (strcasecmp(p->h_name, name) == 0) 881 if (strcasecmp(p->h_name, name) == 0)
480 break; 882 break;
481 for (cp = p->h_aliases; *cp != 0; cp++) 883 for (cp = p->h_aliases; *cp != 0; cp++)
482 if (strcasecmp(*cp, name) == 0) 884 if (strcasecmp(*cp, name) == 0)
483 goto found; 885 goto found;
484 } 886 }
485found: 887 found:
486 _endhtent(); 888 _endhtent();
487 if (p==NULL)
488 h_errno = HOST_NOT_FOUND;
489 return (p); 889 return (p);
490} 890}
491 891
492struct hostent * 892struct hostent *
493_gethtbyaddr(addr, len, type) 893_gethtbyaddr(const void *addr, socklen_t len, int af)
494 const char *addr;
495 int len, type;
496{ 894{
497 register struct hostent *p; 895 struct hostent *p;
896
897 host.h_length = len;
898 host.h_addrtype = af;
498 899
499 _sethtent(0); 900 _sethtent(0);
500 while (p = _gethtent()) 901 while ((p = _gethtent()))
501 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 902 if (p->h_addrtype == af && p->h_length == len &&
903 !bcmp(p->h_addr, addr, len))
502 break; 904 break;
503 _endhtent(); 905 _endhtent();
504 if (p==NULL)
505 h_errno = HOST_NOT_FOUND;
506 return (p); 906 return (p);
507} 907}
508 908
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 909#ifdef YP
527struct hostent * 910struct hostent *
528_yphostent(line) 911_yphostent(char *line)
529 char *line;
530{ 912{
531 static struct in_addr host_addrs[MAXADDRS]; 913 static struct in_addr host_addrs[MAXADDRS];
532 char *p = line; 914 char *p = line;
@@ -537,21 +919,21 @@ _yphostent(line)
537 919
538 host.h_name = NULL; 920 host.h_name = NULL;
539 host.h_addr_list = h_addr_ptrs; 921 host.h_addr_list = h_addr_ptrs;
540 host.h_length = sizeof(u_int32_t); 922 host.h_length = INADDRSZ;
541 host.h_addrtype = AF_INET; 923 host.h_addrtype = AF_INET;
542 hap = h_addr_ptrs; 924 hap = h_addr_ptrs;
543 buf = host_addrs; 925 buf = host_addrs;
544 q = host.h_aliases = host_aliases; 926 q = host.h_aliases = host_aliases;
545 927
546nextline: 928nextline:
929 /* check for host_addrs overflow */
930 if (buf >= &host_addrs[sizeof(host_addrs) / sizeof(host_addrs[0])])
931 goto done;
932
547 more = 0; 933 more = 0;
548 cp = strpbrk(p, " \t"); 934 cp = strpbrk(p, " \t");
549 if (cp == NULL) { 935 if (cp == NULL)
550 if (host.h_name == NULL) 936 goto done;
551 return (NULL);
552 else
553 goto done;
554 }
555 *cp++ = '\0'; 937 *cp++ = '\0';
556 938
557 *hap++ = (char *)buf; 939 *hap++ = (char *)buf;
@@ -592,30 +974,28 @@ nextline:
592 *cp++ = '\0'; 974 *cp++ = '\0';
593 } 975 }
594done: 976done:
977 if (host.h_name == NULL)
978 return (NULL);
595 *q = NULL; 979 *q = NULL;
596 *hap = NULL; 980 *hap = NULL;
597 return (&host); 981 return (&host);
598} 982}
599 983
600struct hostent * 984struct hostent *
601_yp_gethtbyaddr(addr, len, type) 985_yp_gethtbyaddr(const void *addr)
602 const char *addr;
603 int len, type;
604{ 986{
605 struct hostent *hp = (struct hostent *)NULL; 987 struct hostent *hp = NULL;
988 const u_char *uaddr = (const u_char *)addr;
606 static char *__ypcurrent; 989 static char *__ypcurrent;
607 int __ypcurrentlen, r; 990 int __ypcurrentlen, r;
608 char name[sizeof("xxx.xxx.xxx.xxx") + 1]; 991 char name[sizeof("xxx.xxx.xxx.xxx")];
609 992
610 if (!__ypdomain) { 993 if (!__ypdomain) {
611 if (_yp_check(&__ypdomain) == 0) 994 if (_yp_check(&__ypdomain) == 0)
612 return (hp); 995 return (hp);
613 } 996 }
614 sprintf(name, "%u.%u.%u.%u", 997 snprintf(name, sizeof name, "%u.%u.%u.%u", (uaddr[0] & 0xff),
615 ((unsigned)addr[0] & 0xff), 998 (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) 999 if (__ypcurrent)
620 free(__ypcurrent); 1000 free(__ypcurrent);
621 __ypcurrent = NULL; 1001 __ypcurrent = NULL;
@@ -629,13 +1009,14 @@ _yp_gethtbyaddr(addr, len, type)
629} 1009}
630 1010
631struct hostent * 1011struct hostent *
632_yp_gethtbyname(name) 1012_yp_gethtbyname(const char *name)
633 const char *name;
634{ 1013{
635 struct hostent *hp = (struct hostent *)NULL; 1014 struct hostent *hp = (struct hostent *)NULL;
636 static char *__ypcurrent; 1015 static char *__ypcurrent;
637 int __ypcurrentlen, r; 1016 int __ypcurrentlen, r;
638 1017
1018 if (strlen(name) >= MAXHOSTNAMELEN)
1019 return (NULL);
639 if (!__ypdomain) { 1020 if (!__ypdomain) {
640 if (_yp_check(&__ypdomain) == 0) 1021 if (_yp_check(&__ypdomain) == 0)
641 return (hp); 1022 return (hp);
@@ -645,10 +1026,104 @@ _yp_gethtbyname(name)
645 __ypcurrent = NULL; 1026 __ypcurrent = NULL;
646 r = yp_match(__ypdomain, "hosts.byname", name, 1027 r = yp_match(__ypdomain, "hosts.byname", name,
647 strlen(name), &__ypcurrent, &__ypcurrentlen); 1028 strlen(name), &__ypcurrent, &__ypcurrentlen);
648 if (r==0) 1029 if (r == 0)
649 hp = _yphostent(__ypcurrent); 1030 hp = _yphostent(__ypcurrent);
650 if (hp==NULL) 1031 if (hp == NULL)
651 h_errno = HOST_NOT_FOUND; 1032 h_errno = HOST_NOT_FOUND;
652 return (hp); 1033 return (hp);
653} 1034}
654#endif 1035#endif
1036
1037static void
1038map_v4v6_address(const char *src, char *dst)
1039{
1040 u_char *p = (u_char *)dst;
1041 char tmp[INADDRSZ];
1042 int i;
1043
1044 /* Stash a temporary copy so our caller can update in place. */
1045 bcopy(src, tmp, INADDRSZ);
1046 /* Mark this ipv6 addr as a mapped ipv4. */
1047 for (i = 0; i < 10; i++)
1048 *p++ = 0x00;
1049 *p++ = 0xff;
1050 *p++ = 0xff;
1051 /* Retrieve the saved copy and we're done. */
1052 bcopy(tmp, (void*)p, INADDRSZ);
1053}
1054
1055static void
1056map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1057{
1058 char **ap;
1059
1060 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1061 return;
1062 hp->h_addrtype = AF_INET6;
1063 hp->h_length = IN6ADDRSZ;
1064 for (ap = hp->h_addr_list; *ap; ap++) {
1065 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
1066
1067 if (ep - *bpp < (i + IN6ADDRSZ)) {
1068 /* Out of memory. Truncate address list here. XXX */
1069 *ap = NULL;
1070 return;
1071 }
1072 *bpp += i;
1073 map_v4v6_address(*ap, *bpp);
1074 *ap = *bpp;
1075 *bpp += IN6ADDRSZ;
1076 }
1077}
1078
1079struct hostent *
1080gethostent(void)
1081{
1082 host.h_addrtype = AF_UNSPEC;
1083 host.h_length = 0;
1084 return (_gethtent());
1085}
1086
1087#ifdef RESOLVSORT
1088static void
1089addrsort(char **ap, int num)
1090{
1091 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
1092 int i, j;
1093 char **p;
1094 short aval[MAXADDRS];
1095 int needsort = 0;
1096
1097 p = ap;
1098 for (i = 0; i < num; i++, p++) {
1099 for (j = 0 ; (unsigned)j < _resp->nsort; j++)
1100 if (_resp->sort_list[j].addr.s_addr ==
1101 (((struct in_addr *)(*p))->s_addr &
1102 _resp->sort_list[j].mask))
1103 break;
1104 aval[i] = j;
1105 if (needsort == 0 && i > 0 && j < aval[i-1])
1106 needsort = i;
1107 }
1108 if (!needsort)
1109 return;
1110
1111 while (needsort < num) {
1112 for (j = needsort - 1; j >= 0; j--) {
1113 if (aval[j] > aval[j+1]) {
1114 char *hp;
1115
1116 i = aval[j];
1117 aval[j] = aval[j+1];
1118 aval[j+1] = i;
1119
1120 hp = ap[j];
1121 ap[j] = ap[j+1];
1122 ap[j+1] = hp;
1123 } else
1124 break;
1125 }
1126 needsort++;
1127 }
1128}
1129#endif