1/*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 * -
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32 *
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 * SOFTWARE.
48 * -
49 * --Copyright--
50 */
51
52/* XXX This file is not used by any of the resolver functions implemented by
53 glibc (i.e. get*info and gethostby*). It cannot be removed however because
54 it exports symbols in the libresolv ABI. The file is not maintained any
55 more, nor are these functions. */
56
57#include <shlib-compat.h>
58#if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_25)
59
60# include <sys/types.h>
61# include <sys/param.h>
62# include <sys/socket.h>
63# include <netinet/in.h>
64# include <arpa/inet.h>
65# include <arpa/nameser.h>
66
67# include <stdio.h>
68# include <netdb.h>
69# include <resolv/resolv-internal.h>
70# include <resolv/resolv_context.h>
71# include <ctype.h>
72# include <errno.h>
73# include <stdlib.h>
74# include <string.h>
75
76# define MAXALIASES 35
77# define MAXADDRS 35
78
79static char *h_addr_ptrs[MAXADDRS + 1];
80
81static struct hostent host;
82static char *host_aliases[MAXALIASES];
83static char hostbuf[8*1024];
84static u_char host_addr[16]; /* IPv4 or IPv6 */
85static FILE *hostf = NULL;
86static int stayopen = 0;
87
88static struct hostent *res_gethostbyname2_context (struct resolv_context *,
89 const char *name, int af);
90
91static void map_v4v6_address (const char *src, char *dst) __THROW;
92static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
93
94extern void addrsort (char **, int) __THROW;
95
96# if PACKETSZ > 65536
97# define MAXPACKET PACKETSZ
98# else
99# define MAXPACKET 65536
100# endif
101
102/* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
103# ifdef MAXHOSTNAMELEN
104# undef MAXHOSTNAMELEN
105# endif
106# define MAXHOSTNAMELEN 256
107
108typedef union {
109 HEADER hdr;
110 u_char buf[MAXPACKET];
111} querybuf;
112
113typedef union {
114 int32_t al;
115 char ac;
116} align;
117
118# ifndef h_errno
119extern int h_errno;
120# endif
121
122# define BOUNDED_INCR(x) \
123 do { \
124 cp += x; \
125 if (cp > eom) { \
126 __set_h_errno (NO_RECOVERY); \
127 return (NULL); \
128 } \
129 } while (0)
130
131# define BOUNDS_CHECK(ptr, count) \
132 do { \
133 if ((ptr) + (count) > eom) { \
134 __set_h_errno (NO_RECOVERY); \
135 return (NULL); \
136 } \
137 } while (0)
138
139
140static struct hostent *
141getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
142{
143 const HEADER *hp;
144 const u_char *cp;
145 int n;
146 const u_char *eom, *erdata;
147 char *bp, **ap, **hap;
148 int type, class, buflen, ancount, qdcount;
149 int haveanswer, had_error;
150 char tbuf[MAXDNAME];
151 const char *tname;
152 int (*name_ok) (const char *);
153
154 tname = qname;
155 host.h_name = NULL;
156 eom = answer->buf + anslen;
157 switch (qtype) {
158 case T_A:
159 case T_AAAA:
160 name_ok = res_hnok;
161 break;
162 case T_PTR:
163 name_ok = res_dnok;
164 break;
165 default:
166 return (NULL); /* XXX should be abort(); */
167 }
168 /*
169 * find first satisfactory answer
170 */
171 hp = &answer->hdr;
172 ancount = ntohs(hp->ancount);
173 qdcount = ntohs(hp->qdcount);
174 bp = hostbuf;
175 buflen = sizeof hostbuf;
176 cp = answer->buf;
177 BOUNDED_INCR(HFIXEDSZ);
178 if (qdcount != 1) {
179 __set_h_errno (NO_RECOVERY);
180 return (NULL);
181 }
182 n = dn_expand(answer->buf, eom, cp, bp, buflen);
183 if ((n < 0) || !(*name_ok)(bp)) {
184 __set_h_errno (NO_RECOVERY);
185 return (NULL);
186 }
187 BOUNDED_INCR(n + QFIXEDSZ);
188 if (qtype == T_A || qtype == T_AAAA) {
189 /* res_send() has already verified that the query name is the
190 * same as the one we sent; this just gets the expanded name
191 * (i.e., with the succeeding search-domain tacked on).
192 */
193 n = strlen(bp) + 1; /* for the \0 */
194 if (n >= MAXHOSTNAMELEN) {
195 __set_h_errno (NO_RECOVERY);
196 return (NULL);
197 }
198 host.h_name = bp;
199 bp += n;
200 buflen -= n;
201 /* The qname can be abbreviated, but h_name is now absolute. */
202 qname = host.h_name;
203 }
204 ap = host_aliases;
205 *ap = NULL;
206 host.h_aliases = host_aliases;
207 hap = h_addr_ptrs;
208 *hap = NULL;
209 host.h_addr_list = h_addr_ptrs;
210 haveanswer = 0;
211 had_error = 0;
212 while (ancount-- > 0 && cp < eom && !had_error) {
213 n = dn_expand(answer->buf, eom, cp, bp, buflen);
214 if ((n < 0) || !(*name_ok)(bp)) {
215 had_error++;
216 continue;
217 }
218 cp += n; /* name */
219 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
220 type = ns_get16(cp);
221 cp += INT16SZ; /* type */
222 class = ns_get16(cp);
223 cp += INT16SZ + INT32SZ; /* class, TTL */
224 n = ns_get16(cp);
225 cp += INT16SZ; /* len */
226 BOUNDS_CHECK(cp, n);
227 erdata = cp + n;
228 if (class != C_IN) {
229 /* XXX - debug? syslog? */
230 cp += n;
231 continue; /* XXX - had_error++ ? */
232 }
233 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
234 if (ap >= &host_aliases[MAXALIASES-1])
235 continue;
236 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
237 if ((n < 0) || !(*name_ok)(tbuf)) {
238 had_error++;
239 continue;
240 }
241 cp += n;
242 if (cp != erdata) {
243 __set_h_errno (NO_RECOVERY);
244 return (NULL);
245 }
246 /* Store alias. */
247 *ap++ = bp;
248 n = strlen(bp) + 1; /* for the \0 */
249 if (n >= MAXHOSTNAMELEN) {
250 had_error++;
251 continue;
252 }
253 bp += n;
254 buflen -= n;
255 /* Get canonical name. */
256 n = strlen(tbuf) + 1; /* for the \0 */
257 if (n > buflen || n >= MAXHOSTNAMELEN) {
258 had_error++;
259 continue;
260 }
261 strcpy(bp, tbuf);
262 host.h_name = bp;
263 bp += n;
264 buflen -= n;
265 continue;
266 }
267 if (qtype == T_PTR && type == T_CNAME) {
268 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
269 if (n < 0 || !res_dnok(tbuf)) {
270 had_error++;
271 continue;
272 }
273 cp += n;
274 if (cp != erdata) {
275 __set_h_errno (NO_RECOVERY);
276 return (NULL);
277 }
278 /* Get canonical name. */
279 n = strlen(tbuf) + 1; /* for the \0 */
280 if (n > buflen || n >= MAXHOSTNAMELEN) {
281 had_error++;
282 continue;
283 }
284 strcpy(bp, tbuf);
285 tname = bp;
286 bp += n;
287 buflen -= n;
288 continue;
289 }
290 if (type != qtype) {
291 /* Log a low priority message if we get an unexpected
292 * record, but skip it if we are using DNSSEC since it
293 * uses many different types in responses that do not
294 * match QTYPE.
295 */
296 cp += n;
297 continue; /* XXX - had_error++ ? */
298 }
299 switch (type) {
300 case T_PTR:
301 if (strcasecmp(tname, bp) != 0) {
302 cp += n;
303 continue; /* XXX - had_error++ ? */
304 }
305 n = dn_expand(answer->buf, eom, cp, bp, buflen);
306 if ((n < 0) || !res_hnok(bp)) {
307 had_error++;
308 break;
309 }
310 cp += n;
311 if (cp != erdata) {
312 __set_h_errno (NO_RECOVERY);
313 return (NULL);
314 }
315 if (!haveanswer)
316 host.h_name = bp;
317 else if (ap < &host_aliases[MAXALIASES-1])
318 *ap++ = bp;
319 else
320 n = -1;
321 if (n != -1) {
322 n = strlen(bp) + 1; /* for the \0 */
323 if (n >= MAXHOSTNAMELEN) {
324 had_error++;
325 break;
326 }
327 bp += n;
328 buflen -= n;
329 }
330 break;
331 case T_A:
332 case T_AAAA:
333 if (strcasecmp(host.h_name, bp) != 0) {
334 cp += n;
335 continue; /* XXX - had_error++ ? */
336 }
337 if (n != host.h_length) {
338 cp += n;
339 continue;
340 }
341 if (!haveanswer) {
342 int nn;
343
344 host.h_name = bp;
345 nn = strlen(bp) + 1; /* for the \0 */
346 bp += nn;
347 buflen -= nn;
348 }
349
350 /* XXX: when incrementing bp, we have to decrement
351 * buflen by the same amount --okir */
352 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
353
354 bp += sizeof(align) - ((u_long)bp % sizeof(align));
355
356 if (bp + n >= &hostbuf[sizeof hostbuf]) {
357 had_error++;
358 continue;
359 }
360 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
361 cp += n;
362 continue;
363 }
364 memmove(*hap++ = bp, cp, n);
365 bp += n;
366 buflen -= n;
367 cp += n;
368 if (cp != erdata) {
369 __set_h_errno (NO_RECOVERY);
370 return (NULL);
371 }
372 break;
373 default:
374 abort();
375 }
376 if (!had_error)
377 haveanswer++;
378 }
379 if (haveanswer) {
380 *ap = NULL;
381 *hap = NULL;
382 /*
383 * Note: we sort even if host can take only one address
384 * in its return structures - should give it the "best"
385 * address in that case, not some random one
386 */
387 if (_res.nsort && haveanswer > 1 && qtype == T_A)
388 addrsort(h_addr_ptrs, haveanswer);
389 if (!host.h_name) {
390 n = strlen(qname) + 1; /* for the \0 */
391 if (n > buflen || n >= MAXHOSTNAMELEN)
392 goto no_recovery;
393 strcpy(bp, qname);
394 host.h_name = bp;
395 bp += n;
396 buflen -= n;
397 }
398 if (res_use_inet6 ())
399 map_v4v6_hostent(&host, &bp, &buflen);
400 __set_h_errno (NETDB_SUCCESS);
401 return (&host);
402 }
403 no_recovery:
404 __set_h_errno (NO_RECOVERY);
405 return (NULL);
406}
407
408extern struct hostent *res_gethostbyname2(const char *name, int af);
409libresolv_hidden_proto (res_gethostbyname2)
410
411struct hostent *
412res_gethostbyname (const char *name)
413{
414 struct resolv_context *ctx = __resolv_context_get ();
415 if (ctx == NULL)
416 {
417 __set_h_errno (NETDB_INTERNAL);
418 return NULL;
419 }
420
421 if (res_use_inet6 ())
422 {
423 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET6);
424 if (hp != NULL)
425 {
426 __resolv_context_put (ctx);
427 return hp;
428 }
429 }
430 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET);
431 __resolv_context_put (ctx);
432 return hp;
433}
434compat_symbol (libresolv, res_gethostbyname, res_gethostbyname, GLIBC_2_0);
435
436static struct hostent *
437res_gethostbyname2_context (struct resolv_context *ctx,
438 const char *name, int af)
439{
440 union
441 {
442 querybuf *buf;
443 u_char *ptr;
444 } buf;
445 querybuf *origbuf;
446 const char *cp;
447 char *bp;
448 int n, size, type, len;
449 struct hostent *ret;
450
451 switch (af) {
452 case AF_INET:
453 size = INADDRSZ;
454 type = T_A;
455 break;
456 case AF_INET6:
457 size = IN6ADDRSZ;
458 type = T_AAAA;
459 break;
460 default:
461 __set_h_errno (NETDB_INTERNAL);
462 __set_errno (EAFNOSUPPORT);
463 return (NULL);
464 }
465
466 host.h_addrtype = af;
467 host.h_length = size;
468
469 /*
470 * if there aren't any dots, it could be a user-level alias.
471 * this is also done in res_query() since we are not the only
472 * function that looks up host names.
473 */
474 char abuf[MAXDNAME];
475 if (strchr (name, '.') != NULL
476 && (cp = __res_context_hostalias (ctx, name, abuf, sizeof (abuf))))
477 name = cp;
478
479 /*
480 * disallow names consisting only of digits/dots, unless
481 * they end in a dot.
482 */
483 if (isdigit(name[0]))
484 for (cp = name;; ++cp) {
485 if (!*cp) {
486 if (*--cp == '.')
487 break;
488 /*
489 * All-numeric, no dot at the end.
490 * Fake up a hostent as if we'd actually
491 * done a lookup.
492 */
493 if (inet_pton(af, name, host_addr) <= 0) {
494 __set_h_errno (HOST_NOT_FOUND);
495 return (NULL);
496 }
497 strncpy(hostbuf, name, MAXDNAME);
498 hostbuf[MAXDNAME] = '\0';
499 bp = hostbuf + MAXDNAME;
500 len = sizeof hostbuf - MAXDNAME;
501 host.h_name = hostbuf;
502 host.h_aliases = host_aliases;
503 host_aliases[0] = NULL;
504 h_addr_ptrs[0] = (char *)host_addr;
505 h_addr_ptrs[1] = NULL;
506 host.h_addr_list = h_addr_ptrs;
507 if (res_use_inet6 ())
508 map_v4v6_hostent(&host, &bp, &len);
509 __set_h_errno (NETDB_SUCCESS);
510 return (&host);
511 }
512 if (!isdigit(*cp) && *cp != '.')
513 break;
514 }
515 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
516 name[0] == ':')
517 for (cp = name;; ++cp) {
518 if (!*cp) {
519 if (*--cp == '.')
520 break;
521 /*
522 * All-IPv6-legal, no dot at the end.
523 * Fake up a hostent as if we'd actually
524 * done a lookup.
525 */
526 if (inet_pton(af, name, host_addr) <= 0) {
527 __set_h_errno (HOST_NOT_FOUND);
528 return (NULL);
529 }
530 strncpy(hostbuf, name, MAXDNAME);
531 hostbuf[MAXDNAME] = '\0';
532 bp = hostbuf + MAXDNAME;
533 len = sizeof hostbuf - MAXDNAME;
534 host.h_name = hostbuf;
535 host.h_aliases = host_aliases;
536 host_aliases[0] = NULL;
537 h_addr_ptrs[0] = (char *)host_addr;
538 h_addr_ptrs[1] = NULL;
539 host.h_addr_list = h_addr_ptrs;
540 __set_h_errno (NETDB_SUCCESS);
541 return (&host);
542 }
543 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
544 break;
545 }
546
547 buf.buf = origbuf = (querybuf *) alloca (1024);
548
549 if ((n = __res_context_search
550 (ctx, name, C_IN, type, buf.buf->buf, 1024,
551 &buf.ptr, NULL, NULL, NULL, NULL)) < 0) {
552 if (buf.buf != origbuf)
553 free (buf.buf);
554 if (errno == ECONNREFUSED)
555 return (_gethtbyname2(name, af));
556 return (NULL);
557 }
558 ret = getanswer(buf.buf, n, name, type);
559 if (buf.buf != origbuf)
560 free (buf.buf);
561 return ret;
562}
563
564struct hostent *
565res_gethostbyname2 (const char *name, int af)
566{
567 struct resolv_context *ctx = __resolv_context_get ();
568 if (ctx == NULL)
569 {
570 __set_h_errno (NETDB_INTERNAL);
571 return NULL;
572 }
573 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET);
574 __resolv_context_put (ctx);
575 return hp;
576}
577libresolv_hidden_def (res_gethostbyname2)
578compat_symbol (libresolv, res_gethostbyname2, res_gethostbyname2, GLIBC_2_0);
579
580static struct hostent *
581res_gethostbyaddr_context (struct resolv_context *ctx,
582 const void *addr, socklen_t len, int af)
583{
584 const u_char *uaddr = (const u_char *)addr;
585 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
586 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
587 int n;
588 socklen_t size;
589 union
590 {
591 querybuf *buf;
592 u_char *ptr;
593 } buf;
594 querybuf *orig_buf;
595 struct hostent *hp;
596 char qbuf[MAXDNAME+1], *qp = NULL;
597
598 if (af == AF_INET6 && len == IN6ADDRSZ &&
599 (!memcmp(uaddr, mapped, sizeof mapped) ||
600 !memcmp(uaddr, tunnelled, sizeof tunnelled))) {
601 /* Unmap. */
602 addr += sizeof mapped;
603 uaddr += sizeof mapped;
604 af = AF_INET;
605 len = INADDRSZ;
606 }
607 switch (af) {
608 case AF_INET:
609 size = INADDRSZ;
610 break;
611 case AF_INET6:
612 size = IN6ADDRSZ;
613 break;
614 default:
615 __set_errno (EAFNOSUPPORT);
616 __set_h_errno (NETDB_INTERNAL);
617 return (NULL);
618 }
619 if (size != len) {
620 __set_errno (EINVAL);
621 __set_h_errno (NETDB_INTERNAL);
622 return (NULL);
623 }
624 switch (af) {
625 case AF_INET:
626 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
627 (uaddr[3] & 0xff),
628 (uaddr[2] & 0xff),
629 (uaddr[1] & 0xff),
630 (uaddr[0] & 0xff));
631 break;
632 case AF_INET6:
633 qp = qbuf;
634 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
635 qp += sprintf(qp, "%x.%x.",
636 uaddr[n] & 0xf,
637 (uaddr[n] >> 4) & 0xf);
638 }
639 strcpy(qp, "ip6.arpa");
640 break;
641 default:
642 abort();
643 }
644
645 buf.buf = orig_buf = (querybuf *) alloca (1024);
646
647 n = __res_context_query (ctx, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
648 &buf.ptr, NULL, NULL, NULL, NULL);
649 if (n < 0) {
650 if (buf.buf != orig_buf)
651 free (buf.buf);
652 if (errno == ECONNREFUSED)
653 return (_gethtbyaddr(addr, len, af));
654 return (NULL);
655 }
656 hp = getanswer(buf.buf, n, qbuf, T_PTR);
657 if (buf.buf != orig_buf)
658 free (buf.buf);
659 if (!hp)
660 return (NULL); /* h_errno was set by getanswer() */
661 hp->h_addrtype = af;
662 hp->h_length = len;
663 memmove(host_addr, addr, len);
664 h_addr_ptrs[0] = (char *)host_addr;
665 h_addr_ptrs[1] = NULL;
666 if (af == AF_INET && res_use_inet6 ()) {
667 map_v4v6_address((char*)host_addr, (char*)host_addr);
668 hp->h_addrtype = AF_INET6;
669 hp->h_length = IN6ADDRSZ;
670 }
671 __set_h_errno (NETDB_SUCCESS);
672 return (hp);
673}
674
675struct hostent *
676res_gethostbyaddr (const void *addr, socklen_t len, int af)
677{
678 struct resolv_context *ctx = __resolv_context_get ();
679 if (ctx == NULL)
680 {
681 __set_h_errno (NETDB_INTERNAL);
682 return NULL;
683 }
684 struct hostent *hp = res_gethostbyaddr_context (ctx, addr, len, af);
685 __resolv_context_put (ctx);
686 return hp;
687}
688compat_symbol (libresolv, res_gethostbyaddr, res_gethostbyaddr, GLIBC_2_0);
689
690void
691_sethtent (int f)
692{
693 if (!hostf)
694 hostf = fopen(_PATH_HOSTS, "rce" );
695 else
696 rewind(hostf);
697 stayopen = f;
698}
699libresolv_hidden_def (_sethtent)
700compat_symbol (libresolv, _sethtent, _sethtent, GLIBC_2_0);
701
702static void
703_endhtent (void)
704{
705 if (hostf && !stayopen) {
706 (void) fclose(hostf);
707 hostf = NULL;
708 }
709}
710
711struct hostent *
712_gethtent (void)
713{
714 char *p;
715 char *cp, **q;
716 int af, len;
717
718 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) {
719 __set_h_errno (NETDB_INTERNAL);
720 return (NULL);
721 }
722 again:
723 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
724 __set_h_errno (HOST_NOT_FOUND);
725 return (NULL);
726 }
727 if (*p == '#')
728 goto again;
729 if (!(cp = strpbrk(p, "#\n")))
730 goto again;
731 *cp = '\0';
732 if (!(cp = strpbrk(p, " \t")))
733 goto again;
734 *cp++ = '\0';
735 if (inet_pton(AF_INET6, p, host_addr) > 0) {
736 af = AF_INET6;
737 len = IN6ADDRSZ;
738 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
739 if (res_use_inet6 ()) {
740 map_v4v6_address((char*)host_addr, (char*)host_addr);
741 af = AF_INET6;
742 len = IN6ADDRSZ;
743 } else {
744 af = AF_INET;
745 len = INADDRSZ;
746 }
747 } else {
748 goto again;
749 }
750 h_addr_ptrs[0] = (char *)host_addr;
751 h_addr_ptrs[1] = NULL;
752 host.h_addr_list = h_addr_ptrs;
753 host.h_length = len;
754 host.h_addrtype = af;
755 while (*cp == ' ' || *cp == '\t')
756 cp++;
757 host.h_name = cp;
758 q = host.h_aliases = host_aliases;
759 if ((cp = strpbrk(cp, " \t")))
760 *cp++ = '\0';
761 while (cp && *cp) {
762 if (*cp == ' ' || *cp == '\t') {
763 cp++;
764 continue;
765 }
766 if (q < &host_aliases[MAXALIASES - 1])
767 *q++ = cp;
768 if ((cp = strpbrk(cp, " \t")))
769 *cp++ = '\0';
770 }
771 *q = NULL;
772 __set_h_errno (NETDB_SUCCESS);
773 return (&host);
774}
775libresolv_hidden_def (_gethtent)
776compat_symbol (libresolv, _gethtent, _gethtent, GLIBC_2_0);
777
778struct hostent *
779_gethtbyname (const char *name)
780{
781 struct hostent *hp;
782
783 if (res_use_inet6 ()) {
784 hp = _gethtbyname2(name, AF_INET6);
785 if (hp)
786 return (hp);
787 }
788 return (_gethtbyname2(name, AF_INET));
789}
790compat_symbol (libresolv, _gethtbyname, _gethtbyname, GLIBC_2_0);
791
792struct hostent *
793_gethtbyname2 (const char *name, int af)
794{
795 struct hostent *p;
796 char **cp;
797
798 _sethtent(0);
799 while ((p = _gethtent())) {
800 if (p->h_addrtype != af)
801 continue;
802 if (strcasecmp(p->h_name, name) == 0)
803 break;
804 for (cp = p->h_aliases; *cp != 0; cp++)
805 if (strcasecmp(*cp, name) == 0)
806 goto found;
807 }
808 found:
809 _endhtent();
810 return (p);
811}
812libresolv_hidden_def (_gethtbyname2)
813compat_symbol (libresolv, _gethtbyname2, _gethtbyname2, GLIBC_2_0);
814
815struct hostent *
816_gethtbyaddr (const char *addr, size_t len, int af)
817{
818 struct hostent *p;
819
820 _sethtent(0);
821 while ((p = _gethtent()))
822 if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len))
823 break;
824 _endhtent();
825 return (p);
826}
827libresolv_hidden_def (_gethtbyaddr)
828compat_symbol (libresolv, _gethtbyaddr, _gethtbyaddr, GLIBC_2_0);
829
830static void
831map_v4v6_address (const char *src, char *dst)
832{
833 u_char *p = (u_char *)dst;
834 char tmp[INADDRSZ];
835 int i;
836
837 /* Stash a temporary copy so our caller can update in place. */
838 memcpy(tmp, src, INADDRSZ);
839 /* Mark this ipv6 addr as a mapped ipv4. */
840 for (i = 0; i < 10; i++)
841 *p++ = 0x00;
842 *p++ = 0xff;
843 *p++ = 0xff;
844 /* Retrieve the saved copy and we're done. */
845 memcpy((void*)p, tmp, INADDRSZ);
846}
847
848static void
849map_v4v6_hostent (struct hostent *hp, char **bpp, int *lenp)
850{
851 char **ap;
852
853 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
854 return;
855 hp->h_addrtype = AF_INET6;
856 hp->h_length = IN6ADDRSZ;
857 for (ap = hp->h_addr_list; *ap; ap++) {
858 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
859
860 if (*lenp < (i + IN6ADDRSZ)) {
861 /* Out of memory. Truncate address list here. XXX */
862 *ap = NULL;
863 return;
864 }
865 *bpp += i;
866 *lenp -= i;
867 map_v4v6_address(*ap, *bpp);
868 *ap = *bpp;
869 *bpp += IN6ADDRSZ;
870 *lenp -= IN6ADDRSZ;
871 }
872}
873
874extern void
875addrsort (char **ap, int num)
876{
877 int i, j;
878 char **p;
879 short aval[MAXADDRS];
880 int needsort = 0;
881
882 p = ap;
883 for (i = 0; i < num; i++, p++) {
884 for (j = 0 ; (unsigned)j < _res.nsort; j++)
885 if (_res.sort_list[j].addr.s_addr ==
886 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
887 break;
888 aval[i] = j;
889 if (needsort == 0 && i > 0 && j < aval[i-1])
890 needsort = i;
891 }
892 if (!needsort)
893 return;
894
895 while (needsort < num) {
896 for (j = needsort - 1; j >= 0; j--) {
897 if (aval[j] > aval[j+1]) {
898 char *hp;
899
900 i = aval[j];
901 aval[j] = aval[j+1];
902 aval[j+1] = i;
903
904 hp = ap[j];
905 ap[j] = ap[j+1];
906 ap[j+1] = hp;
907
908 } else
909 break;
910 }
911 needsort++;
912 }
913}
914
915#endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */
916