* sysdeps/generic/dl-lookupcfg.h: Include <tls.h>.
[glibc.git] / resolv / gethnamaddr.c
blob797af94874c5c0864beaace6ac28e73b5b6cec9f
1 /*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.
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--
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
54 static char rcsid[] = "$Id$";
55 #endif /* LIBC_SCCS and not lint */
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <arpa/nameser.h>
64 #include <stdio.h>
65 #include <netdb.h>
66 #include <resolv.h>
67 #include <ctype.h>
68 #include <errno.h>
69 #include <syslog.h>
71 #define RESOLVSORT
73 #ifndef LOG_AUTH
74 # define LOG_AUTH 0
75 #endif
77 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
79 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
80 # include <stdlib.h>
81 # include <string.h>
82 #else
83 # include "../conf/portability.h"
84 #endif
86 #if defined(USE_OPTIONS_H)
87 # include <../conf/options.h>
88 #endif
90 #ifdef SPRINTF_CHAR
91 # define SPRINTF(x) strlen(sprintf/**/x)
92 #else
93 # define SPRINTF(x) ((size_t)sprintf x)
94 #endif
96 #define MAXALIASES 35
97 #define MAXADDRS 35
99 static const char AskedForGot[] =
100 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
102 static char *h_addr_ptrs[MAXADDRS + 1];
104 static struct hostent host;
105 static char *host_aliases[MAXALIASES];
106 static char hostbuf[8*1024];
107 static u_char host_addr[16]; /* IPv4 or IPv6 */
108 static FILE *hostf = NULL;
109 static int stayopen = 0;
111 static void map_v4v6_address __P((const char *src, char *dst));
112 static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len));
114 #ifdef RESOLVSORT
115 extern void addrsort __P((char **, int));
116 #endif
118 #if PACKETSZ > 65536
119 #define MAXPACKET PACKETSZ
120 #else
121 #define MAXPACKET 65536
122 #endif
124 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
125 #ifdef MAXHOSTNAMELEN
126 # undef MAXHOSTNAMELEN
127 #endif
128 #define MAXHOSTNAMELEN 256
130 typedef union {
131 HEADER hdr;
132 u_char buf[MAXPACKET];
133 } querybuf;
135 typedef union {
136 int32_t al;
137 char ac;
138 } align;
140 #ifndef h_errno
141 extern int h_errno;
142 #endif
144 #ifdef DEBUG
145 static void
146 dprintf(msg, num)
147 char *msg;
148 int num;
150 if (_res.options & RES_DEBUG) {
151 int save = errno;
153 printf(msg, num);
154 __set_errno (save);
157 #else
158 # define dprintf(msg, num) /*nada*/
159 #endif
161 #define BOUNDED_INCR(x) \
162 do { \
163 cp += x; \
164 if (cp > eom) { \
165 __set_h_errno (NO_RECOVERY); \
166 return (NULL); \
168 } while (0)
170 #define BOUNDS_CHECK(ptr, count) \
171 do { \
172 if ((ptr) + (count) > eom) { \
173 __set_h_errno (NO_RECOVERY); \
174 return (NULL); \
176 } while (0)
179 static struct hostent *
180 getanswer(answer, anslen, qname, qtype)
181 const querybuf *answer;
182 int anslen;
183 const char *qname;
184 int qtype;
186 register const HEADER *hp;
187 register const u_char *cp;
188 register int n;
189 const u_char *eom, *erdata;
190 char *bp, **ap, **hap;
191 int type, class, buflen, ancount, qdcount;
192 int haveanswer, had_error;
193 int toobig = 0;
194 char tbuf[MAXDNAME];
195 const char *tname;
196 int (*name_ok) __P((const char *));
198 tname = qname;
199 host.h_name = NULL;
200 eom = answer->buf + anslen;
201 switch (qtype) {
202 case T_A:
203 case T_AAAA:
204 name_ok = res_hnok;
205 break;
206 case T_PTR:
207 name_ok = res_dnok;
208 break;
209 default:
210 return (NULL); /* XXX should be abort(); */
213 * find first satisfactory answer
215 hp = &answer->hdr;
216 ancount = ntohs(hp->ancount);
217 qdcount = ntohs(hp->qdcount);
218 bp = hostbuf;
219 buflen = sizeof hostbuf;
220 cp = answer->buf;
221 BOUNDED_INCR(HFIXEDSZ);
222 if (qdcount != 1) {
223 __set_h_errno (NO_RECOVERY);
224 return (NULL);
226 n = dn_expand(answer->buf, eom, cp, bp, buflen);
227 if ((n < 0) || !(*name_ok)(bp)) {
228 __set_h_errno (NO_RECOVERY);
229 return (NULL);
231 BOUNDED_INCR(n + QFIXEDSZ);
232 if (qtype == T_A || qtype == T_AAAA) {
233 /* res_send() has already verified that the query name is the
234 * same as the one we sent; this just gets the expanded name
235 * (i.e., with the succeeding search-domain tacked on).
237 n = strlen(bp) + 1; /* for the \0 */
238 if (n >= MAXHOSTNAMELEN) {
239 __set_h_errno (NO_RECOVERY);
240 return (NULL);
242 host.h_name = bp;
243 bp += n;
244 buflen -= n;
245 /* The qname can be abbreviated, but h_name is now absolute. */
246 qname = host.h_name;
248 ap = host_aliases;
249 *ap = NULL;
250 host.h_aliases = host_aliases;
251 hap = h_addr_ptrs;
252 *hap = NULL;
253 host.h_addr_list = h_addr_ptrs;
254 haveanswer = 0;
255 had_error = 0;
256 while (ancount-- > 0 && cp < eom && !had_error) {
257 n = dn_expand(answer->buf, eom, cp, bp, buflen);
258 if ((n < 0) || !(*name_ok)(bp)) {
259 had_error++;
260 continue;
262 cp += n; /* name */
263 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
264 type = ns_get16(cp);
265 cp += INT16SZ; /* type */
266 class = ns_get16(cp);
267 cp += INT16SZ + INT32SZ; /* class, TTL */
268 n = ns_get16(cp);
269 cp += INT16SZ; /* len */
270 BOUNDS_CHECK(cp, n);
271 erdata = cp + n;
272 if (class != C_IN) {
273 /* XXX - debug? syslog? */
274 cp += n;
275 continue; /* XXX - had_error++ ? */
277 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
278 if (ap >= &host_aliases[MAXALIASES-1])
279 continue;
280 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
281 if ((n < 0) || !(*name_ok)(tbuf)) {
282 had_error++;
283 continue;
285 cp += n;
286 if (cp != erdata) {
287 __set_h_errno (NO_RECOVERY);
288 return (NULL);
290 /* Store alias. */
291 *ap++ = bp;
292 n = strlen(bp) + 1; /* for the \0 */
293 if (n >= MAXHOSTNAMELEN) {
294 had_error++;
295 continue;
297 bp += n;
298 buflen -= n;
299 /* Get canonical name. */
300 n = strlen(tbuf) + 1; /* for the \0 */
301 if (n > buflen || n >= MAXHOSTNAMELEN) {
302 had_error++;
303 continue;
305 strcpy(bp, tbuf);
306 host.h_name = bp;
307 bp += n;
308 buflen -= n;
309 continue;
311 if (qtype == T_PTR && type == T_CNAME) {
312 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
313 if (n < 0 || !res_dnok(tbuf)) {
314 had_error++;
315 continue;
317 cp += n;
318 if (cp != erdata) {
319 __set_h_errno (NO_RECOVERY);
320 return (NULL);
322 /* Get canonical name. */
323 n = strlen(tbuf) + 1; /* for the \0 */
324 if (n > buflen || n >= MAXHOSTNAMELEN) {
325 had_error++;
326 continue;
328 strcpy(bp, tbuf);
329 tname = bp;
330 bp += n;
331 buflen -= n;
332 continue;
334 if ((type == T_SIG) || (type == T_KEY) || (type == T_NXT)) {
335 /* We don't support DNSSEC yet. For now, ignore
336 * the record and send a low priority message
337 * to syslog.
339 syslog(LOG_DEBUG|LOG_AUTH,
340 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
341 qname, p_class(C_IN), p_type(qtype),
342 p_type(type));
343 cp += n;
344 continue;
346 if (type != qtype) {
347 syslog(LOG_NOTICE|LOG_AUTH,
348 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
349 qname, p_class(C_IN), p_type(qtype),
350 p_type(type));
351 cp += n;
352 continue; /* XXX - had_error++ ? */
354 switch (type) {
355 case T_PTR:
356 if (strcasecmp(tname, bp) != 0) {
357 syslog(LOG_NOTICE|LOG_AUTH,
358 AskedForGot, qname, bp);
359 cp += n;
360 continue; /* XXX - had_error++ ? */
362 n = dn_expand(answer->buf, eom, cp, bp, buflen);
363 if ((n < 0) || !res_hnok(bp)) {
364 had_error++;
365 break;
367 #if MULTI_PTRS_ARE_ALIASES
368 cp += n;
369 if (cp != erdata) {
370 __set_h_errno (NO_RECOVERY);
371 return (NULL);
373 if (!haveanswer)
374 host.h_name = bp;
375 else if (ap < &host_aliases[MAXALIASES-1])
376 *ap++ = bp;
377 else
378 n = -1;
379 if (n != -1) {
380 n = strlen(bp) + 1; /* for the \0 */
381 if (n >= MAXHOSTNAMELEN) {
382 had_error++;
383 break;
385 bp += n;
386 buflen -= n;
388 break;
389 #else
390 host.h_name = bp;
391 if (_res.options & RES_USE_INET6) {
392 n = strlen(bp) + 1; /* for the \0 */
393 if (n >= MAXHOSTNAMELEN) {
394 had_error++;
395 break;
397 bp += n;
398 buflen -= n;
399 map_v4v6_hostent(&host, &bp, &buflen);
401 __set_h_errno (NETDB_SUCCESS);
402 return (&host);
403 #endif
404 case T_A:
405 case T_AAAA:
406 if (strcasecmp(host.h_name, bp) != 0) {
407 syslog(LOG_NOTICE|LOG_AUTH,
408 AskedForGot, host.h_name, bp);
409 cp += n;
410 continue; /* XXX - had_error++ ? */
412 if (n != host.h_length) {
413 cp += n;
414 continue;
416 if (!haveanswer) {
417 register int nn;
419 host.h_name = bp;
420 nn = strlen(bp) + 1; /* for the \0 */
421 bp += nn;
422 buflen -= nn;
425 /* XXX: when incrementing bp, we have to decrement
426 * buflen by the same amount --okir */
427 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
429 bp += sizeof(align) - ((u_long)bp % sizeof(align));
431 if (bp + n >= &hostbuf[sizeof hostbuf]) {
432 dprintf("size (%d) too big\n", n);
433 had_error++;
434 continue;
436 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
437 if (!toobig++) {
438 dprintf("Too many addresses (%d)\n",
439 MAXADDRS);
441 cp += n;
442 continue;
444 memmove(*hap++ = bp, cp, n);
445 bp += n;
446 buflen -= n;
447 cp += n;
448 if (cp != erdata) {
449 __set_h_errno (NO_RECOVERY);
450 return (NULL);
452 break;
453 default:
454 abort();
456 if (!had_error)
457 haveanswer++;
459 if (haveanswer) {
460 *ap = NULL;
461 *hap = NULL;
462 # if defined(RESOLVSORT)
464 * Note: we sort even if host can take only one address
465 * in its return structures - should give it the "best"
466 * address in that case, not some random one
468 if (_res.nsort && haveanswer > 1 && qtype == T_A)
469 addrsort(h_addr_ptrs, haveanswer);
470 # endif /*RESOLVSORT*/
471 if (!host.h_name) {
472 n = strlen(qname) + 1; /* for the \0 */
473 if (n > buflen || n >= MAXHOSTNAMELEN)
474 goto no_recovery;
475 strcpy(bp, qname);
476 host.h_name = bp;
477 bp += n;
478 buflen -= n;
480 if (_res.options & RES_USE_INET6)
481 map_v4v6_hostent(&host, &bp, &buflen);
482 __set_h_errno (NETDB_SUCCESS);
483 return (&host);
485 no_recovery:
486 __set_h_errno (NO_RECOVERY);
487 return (NULL);
490 struct hostent *
491 gethostbyname(name)
492 const char *name;
494 struct hostent *hp;
496 if ((_res.options & RES_INIT) == 0 && __res_ninit(&_res) == -1) {
497 __set_h_errno (NETDB_INTERNAL);
498 return (NULL);
500 if (_res.options & RES_USE_INET6) {
501 hp = gethostbyname2(name, AF_INET6);
502 if (hp)
503 return (hp);
505 return (gethostbyname2(name, AF_INET));
508 struct hostent *
509 gethostbyname2(name, af)
510 const char *name;
511 int af;
513 querybuf *buf;
514 register const char *cp;
515 char *bp;
516 int n, size, type, len, use_malloc = 0;
517 struct hostent *ret;
518 extern struct hostent *_gethtbyname2();
520 if ((_res.options & RES_INIT) == 0 && __res_ninit(&_res) == -1) {
521 __set_h_errno (NETDB_INTERNAL);
522 return (NULL);
525 switch (af) {
526 case AF_INET:
527 size = INADDRSZ;
528 type = T_A;
529 break;
530 case AF_INET6:
531 size = IN6ADDRSZ;
532 type = T_AAAA;
533 break;
534 default:
535 __set_h_errno (NETDB_INTERNAL);
536 __set_errno (EAFNOSUPPORT);
537 return (NULL);
540 host.h_addrtype = af;
541 host.h_length = size;
544 * if there aren't any dots, it could be a user-level alias.
545 * this is also done in res_query() since we are not the only
546 * function that looks up host names.
548 if (!strchr(name, '.') && (cp = __hostalias(name)))
549 name = cp;
552 * disallow names consisting only of digits/dots, unless
553 * they end in a dot.
555 if (isdigit(name[0]))
556 for (cp = name;; ++cp) {
557 if (!*cp) {
558 if (*--cp == '.')
559 break;
561 * All-numeric, no dot at the end.
562 * Fake up a hostent as if we'd actually
563 * done a lookup.
565 if (inet_pton(af, name, host_addr) <= 0) {
566 __set_h_errno (HOST_NOT_FOUND);
567 return (NULL);
569 strncpy(hostbuf, name, MAXDNAME);
570 hostbuf[MAXDNAME] = '\0';
571 bp = hostbuf + MAXDNAME;
572 len = sizeof hostbuf - MAXDNAME;
573 host.h_name = hostbuf;
574 host.h_aliases = host_aliases;
575 host_aliases[0] = NULL;
576 h_addr_ptrs[0] = (char *)host_addr;
577 h_addr_ptrs[1] = NULL;
578 host.h_addr_list = h_addr_ptrs;
579 if (_res.options & RES_USE_INET6)
580 map_v4v6_hostent(&host, &bp, &len);
581 __set_h_errno (NETDB_SUCCESS);
582 return (&host);
584 if (!isdigit(*cp) && *cp != '.')
585 break;
587 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
588 name[0] == ':')
589 for (cp = name;; ++cp) {
590 if (!*cp) {
591 if (*--cp == '.')
592 break;
594 * All-IPv6-legal, no dot at the end.
595 * Fake up a hostent as if we'd actually
596 * done a lookup.
598 if (inet_pton(af, name, host_addr) <= 0) {
599 __set_h_errno (HOST_NOT_FOUND);
600 return (NULL);
602 strncpy(hostbuf, name, MAXDNAME);
603 hostbuf[MAXDNAME] = '\0';
604 bp = hostbuf + MAXDNAME;
605 len = sizeof hostbuf - MAXDNAME;
606 host.h_name = hostbuf;
607 host.h_aliases = host_aliases;
608 host_aliases[0] = NULL;
609 h_addr_ptrs[0] = (char *)host_addr;
610 h_addr_ptrs[1] = NULL;
611 host.h_addr_list = h_addr_ptrs;
612 __set_h_errno (NETDB_SUCCESS);
613 return (&host);
615 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
616 break;
619 if (!__libc_use_alloca (MAXPACKET)) {
620 buf = (querybuf *) malloc (sizeof (*buf));
621 if (buf == NULL) {
622 __set_h_errno (NETDB_INTERNAL);
623 return NULL;
625 use_malloc = 1;
626 } else
627 buf = (querybuf *) alloca (sizeof (*buf));
629 if ((n = res_nsearch(&_res, name, C_IN, type, buf->buf, sizeof(buf->buf))) < 0) {
630 dprintf("res_nsearch failed (%d)\n", n);
631 if (errno == ECONNREFUSED)
632 return (_gethtbyname2(name, af));
633 return (NULL);
635 ret = getanswer(buf, n, name, type);
636 if (use_malloc)
637 free (buf);
638 return ret;
641 struct hostent *
642 gethostbyaddr(addr, len, af)
643 const void *addr;
644 socklen_t len;
645 int af;
647 const u_char *uaddr = (const u_char *)addr;
648 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
649 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
650 int n, use_malloc = 0;
651 socklen_t size;
652 querybuf *buf;
653 register struct hostent *hp;
654 char qbuf[MAXDNAME+1], *qp = NULL;
655 #ifdef SUNSECURITY
656 register struct hostent *rhp;
657 char **haddr;
658 u_long old_options;
659 char hname2[MAXDNAME+1];
660 #endif /*SUNSECURITY*/
661 extern struct hostent *_gethtbyaddr();
663 if ((_res.options & RES_INIT) == 0 && __res_ninit(&_res) == -1) {
664 __set_h_errno (NETDB_INTERNAL);
665 return (NULL);
667 if (af == AF_INET6 && len == IN6ADDRSZ &&
668 (!bcmp(uaddr, mapped, sizeof mapped) ||
669 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
670 /* Unmap. */
671 addr += sizeof mapped;
672 uaddr += sizeof mapped;
673 af = AF_INET;
674 len = INADDRSZ;
676 switch (af) {
677 case AF_INET:
678 size = INADDRSZ;
679 break;
680 case AF_INET6:
681 size = IN6ADDRSZ;
682 break;
683 default:
684 __set_errno (EAFNOSUPPORT);
685 __set_h_errno (NETDB_INTERNAL);
686 return (NULL);
688 if (size != len) {
689 __set_errno (EINVAL);
690 __set_h_errno (NETDB_INTERNAL);
691 return (NULL);
693 switch (af) {
694 case AF_INET:
695 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
696 (uaddr[3] & 0xff),
697 (uaddr[2] & 0xff),
698 (uaddr[1] & 0xff),
699 (uaddr[0] & 0xff));
700 break;
701 case AF_INET6:
702 qp = qbuf;
703 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
704 qp += SPRINTF((qp, "%x.%x.",
705 uaddr[n] & 0xf,
706 (uaddr[n] >> 4) & 0xf));
708 strcpy(qp, "ip6.arpa");
709 break;
710 default:
711 abort();
714 if (!__libc_use_alloca (MAXPACKET)) {
715 buf = (querybuf *) malloc (sizeof (*buf));
716 if (buf == NULL) {
717 __set_h_errno (NETDB_INTERNAL);
718 return NULL;
720 use_malloc = 1;
721 } else
722 buf = (querybuf *) alloca (sizeof (*buf));
724 n = res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
725 if (n < 0 && af == AF_INET6) {
726 strcpy(qp, "ip6.int");
727 n = res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
729 if (n < 0) {
730 if (use_malloc)
731 free (buf);
732 dprintf("res_nquery failed (%d)\n", n);
733 if (errno == ECONNREFUSED)
734 return (_gethtbyaddr(addr, len, af));
735 return (NULL);
737 hp = getanswer(buf, n, qbuf, T_PTR);
738 if (use_malloc)
739 free (buf);
740 if (!hp)
741 return (NULL); /* h_errno was set by getanswer() */
742 #ifdef SUNSECURITY
743 if (af == AF_INET) {
745 * turn off search as the name should be absolute,
746 * 'localhost' should be matched by defnames
748 strncpy(hname2, hp->h_name, MAXDNAME);
749 hname2[MAXDNAME] = '\0';
750 old_options = _res.options;
751 _res.options &= ~RES_DNSRCH;
752 _res.options |= RES_DEFNAMES;
753 if (!(rhp = gethostbyname(hname2))) {
754 syslog(LOG_NOTICE|LOG_AUTH,
755 "gethostbyaddr: No A record for %s (verifying [%s])",
756 hname2, inet_ntoa(*((struct in_addr *)addr)));
757 _res.options = old_options;
758 __set_h_errno (HOST_NOT_FOUND);
759 return (NULL);
761 _res.options = old_options;
762 for (haddr = rhp->h_addr_list; *haddr; haddr++)
763 if (!memcmp(*haddr, addr, INADDRSZ))
764 break;
765 if (!*haddr) {
766 syslog(LOG_NOTICE|LOG_AUTH,
767 "gethostbyaddr: A record of %s != PTR record [%s]",
768 hname2, inet_ntoa(*((struct in_addr *)addr)));
769 __set_h_errno (HOST_NOT_FOUND);
770 return (NULL);
773 #endif /*SUNSECURITY*/
774 hp->h_addrtype = af;
775 hp->h_length = len;
776 memmove(host_addr, addr, len);
777 h_addr_ptrs[0] = (char *)host_addr;
778 h_addr_ptrs[1] = NULL;
779 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
780 map_v4v6_address((char*)host_addr, (char*)host_addr);
781 hp->h_addrtype = AF_INET6;
782 hp->h_length = IN6ADDRSZ;
784 __set_h_errno (NETDB_SUCCESS);
785 return (hp);
788 void
789 _sethtent(f)
790 int f;
792 if (!hostf)
793 hostf = fopen(_PATH_HOSTS, "r" );
794 else
795 rewind(hostf);
796 stayopen = f;
799 void
800 _endhtent()
802 if (hostf && !stayopen) {
803 (void) fclose(hostf);
804 hostf = NULL;
808 struct hostent *
809 _gethtent()
811 char *p;
812 register char *cp, **q;
813 int af, len;
815 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
816 __set_h_errno (NETDB_INTERNAL);
817 return (NULL);
819 again:
820 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
821 __set_h_errno (HOST_NOT_FOUND);
822 return (NULL);
824 if (*p == '#')
825 goto again;
826 if (!(cp = strpbrk(p, "#\n")))
827 goto again;
828 *cp = '\0';
829 if (!(cp = strpbrk(p, " \t")))
830 goto again;
831 *cp++ = '\0';
832 if (inet_pton(AF_INET6, p, host_addr) > 0) {
833 af = AF_INET6;
834 len = IN6ADDRSZ;
835 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
836 if (_res.options & RES_USE_INET6) {
837 map_v4v6_address((char*)host_addr, (char*)host_addr);
838 af = AF_INET6;
839 len = IN6ADDRSZ;
840 } else {
841 af = AF_INET;
842 len = INADDRSZ;
844 } else {
845 goto again;
847 h_addr_ptrs[0] = (char *)host_addr;
848 h_addr_ptrs[1] = NULL;
849 host.h_addr_list = h_addr_ptrs;
850 host.h_length = len;
851 host.h_addrtype = af;
852 while (*cp == ' ' || *cp == '\t')
853 cp++;
854 host.h_name = cp;
855 q = host.h_aliases = host_aliases;
856 if ((cp = strpbrk(cp, " \t")))
857 *cp++ = '\0';
858 while (cp && *cp) {
859 if (*cp == ' ' || *cp == '\t') {
860 cp++;
861 continue;
863 if (q < &host_aliases[MAXALIASES - 1])
864 *q++ = cp;
865 if ((cp = strpbrk(cp, " \t")))
866 *cp++ = '\0';
868 *q = NULL;
869 __set_h_errno (NETDB_SUCCESS);
870 return (&host);
873 struct hostent *
874 _gethtbyname(name)
875 const char *name;
877 extern struct hostent *_gethtbyname2();
878 struct hostent *hp;
880 if (_res.options & RES_USE_INET6) {
881 hp = _gethtbyname2(name, AF_INET6);
882 if (hp)
883 return (hp);
885 return (_gethtbyname2(name, AF_INET));
888 struct hostent *
889 _gethtbyname2(name, af)
890 const char *name;
891 int af;
893 register struct hostent *p;
894 register char **cp;
896 _sethtent(0);
897 while ((p = _gethtent())) {
898 if (p->h_addrtype != af)
899 continue;
900 if (strcasecmp(p->h_name, name) == 0)
901 break;
902 for (cp = p->h_aliases; *cp != 0; cp++)
903 if (strcasecmp(*cp, name) == 0)
904 goto found;
906 found:
907 _endhtent();
908 return (p);
911 struct hostent *
912 _gethtbyaddr(addr, len, af)
913 const char *addr;
914 size_t len;
915 int af;
917 register struct hostent *p;
919 _sethtent(0);
920 while ((p = _gethtent()))
921 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
922 break;
923 _endhtent();
924 return (p);
927 static void
928 map_v4v6_address(src, dst)
929 const char *src;
930 char *dst;
932 u_char *p = (u_char *)dst;
933 char tmp[INADDRSZ];
934 int i;
936 /* Stash a temporary copy so our caller can update in place. */
937 memcpy(tmp, src, INADDRSZ);
938 /* Mark this ipv6 addr as a mapped ipv4. */
939 for (i = 0; i < 10; i++)
940 *p++ = 0x00;
941 *p++ = 0xff;
942 *p++ = 0xff;
943 /* Retrieve the saved copy and we're done. */
944 memcpy((void*)p, tmp, INADDRSZ);
947 static void
948 map_v4v6_hostent(hp, bpp, lenp)
949 struct hostent *hp;
950 char **bpp;
951 int *lenp;
953 char **ap;
955 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
956 return;
957 hp->h_addrtype = AF_INET6;
958 hp->h_length = IN6ADDRSZ;
959 for (ap = hp->h_addr_list; *ap; ap++) {
960 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
962 if (*lenp < (i + IN6ADDRSZ)) {
963 /* Out of memory. Truncate address list here. XXX */
964 *ap = NULL;
965 return;
967 *bpp += i;
968 *lenp -= i;
969 map_v4v6_address(*ap, *bpp);
970 *ap = *bpp;
971 *bpp += IN6ADDRSZ;
972 *lenp -= IN6ADDRSZ;
976 #ifdef RESOLVSORT
977 extern void
978 addrsort(ap, num)
979 char **ap;
980 int num;
982 int i, j;
983 char **p;
984 short aval[MAXADDRS];
985 int needsort = 0;
987 p = ap;
988 for (i = 0; i < num; i++, p++) {
989 for (j = 0 ; (unsigned)j < _res.nsort; j++)
990 if (_res.sort_list[j].addr.s_addr ==
991 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
992 break;
993 aval[i] = j;
994 if (needsort == 0 && i > 0 && j < aval[i-1])
995 needsort = i;
997 if (!needsort)
998 return;
1000 while (needsort < num) {
1001 for (j = needsort - 1; j >= 0; j--) {
1002 if (aval[j] > aval[j+1]) {
1003 char *hp;
1005 i = aval[j];
1006 aval[j] = aval[j+1];
1007 aval[j+1] = i;
1009 hp = ap[j];
1010 ap[j] = ap[j+1];
1011 ap[j+1] = hp;
1013 } else
1014 break;
1016 needsort++;
1019 #endif
1021 #if defined(BSD43_BSD43_NFS) || defined(sun)
1022 /* some libc's out there are bound internally to these names (UMIPS) */
1023 void
1024 ht_sethostent(stayopen)
1025 int stayopen;
1027 _sethtent(stayopen);
1030 void
1031 ht_endhostent()
1033 _endhtent();
1036 struct hostent *
1037 ht_gethostbyname(name)
1038 char *name;
1040 return (_gethtbyname(name));
1043 struct hostent *
1044 ht_gethostbyaddr(addr, len, af)
1045 const char *addr;
1046 size_t len;
1047 int af;
1049 return (_gethtbyaddr(addr, len, af));
1052 struct hostent *
1053 gethostent()
1055 return (_gethtent());
1058 void
1059 dns_service()
1061 return;
1064 #undef dn_skipname
1065 dn_skipname(comp_dn, eom)
1066 const u_char *comp_dn, *eom;
1068 return (__dn_skipname(comp_dn, eom));
1070 #endif /*old-style libc with yp junk in it*/