Don't confuse unwinder by loading mangled values in %rbp and %rsp registers and by...
[glibc.git] / resolv / gethnamaddr.c
blobbab2535cebadb4bfc982596926a211e788f11e83
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 #endif /* LIBC_SCCS and not lint */
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <arpa/nameser.h>
63 #include <stdio.h>
64 #include <netdb.h>
65 #include <resolv.h>
66 #include <ctype.h>
67 #include <errno.h>
68 #include <syslog.h>
70 #define RESOLVSORT
72 #ifndef LOG_AUTH
73 # define LOG_AUTH 0
74 #endif
76 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
78 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
79 # include <stdlib.h>
80 # include <string.h>
81 #else
82 # include "../conf/portability.h"
83 #endif
85 #if defined(USE_OPTIONS_H)
86 # include <../conf/options.h>
87 #endif
89 #ifdef SPRINTF_CHAR
90 # define SPRINTF(x) strlen(sprintf/**/x)
91 #else
92 # define SPRINTF(x) ((size_t)sprintf x)
93 #endif
95 #define MAXALIASES 35
96 #define MAXADDRS 35
98 static const char AskedForGot[] =
99 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
101 static char *h_addr_ptrs[MAXADDRS + 1];
103 static struct hostent host;
104 static char *host_aliases[MAXALIASES];
105 static char hostbuf[8*1024];
106 static u_char host_addr[16]; /* IPv4 or IPv6 */
107 static FILE *hostf = NULL;
108 static int stayopen = 0;
110 static void map_v4v6_address (const char *src, char *dst) __THROW;
111 static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
113 #ifdef RESOLVSORT
114 extern void addrsort (char **, int) __THROW;
115 #endif
117 #if PACKETSZ > 65536
118 #define MAXPACKET PACKETSZ
119 #else
120 #define MAXPACKET 65536
121 #endif
123 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
124 #ifdef MAXHOSTNAMELEN
125 # undef MAXHOSTNAMELEN
126 #endif
127 #define MAXHOSTNAMELEN 256
129 typedef union {
130 HEADER hdr;
131 u_char buf[MAXPACKET];
132 } querybuf;
134 typedef union {
135 int32_t al;
136 char ac;
137 } align;
139 #ifndef h_errno
140 extern int h_errno;
141 #endif
143 #ifdef DEBUG
144 static void
145 Dprintf(msg, num)
146 char *msg;
147 int num;
149 if (_res.options & RES_DEBUG) {
150 int save = errno;
152 printf(msg, num);
153 __set_errno (save);
156 #else
157 # define Dprintf(msg, num) /*nada*/
158 #endif
160 #define BOUNDED_INCR(x) \
161 do { \
162 cp += x; \
163 if (cp > eom) { \
164 __set_h_errno (NO_RECOVERY); \
165 return (NULL); \
167 } while (0)
169 #define BOUNDS_CHECK(ptr, count) \
170 do { \
171 if ((ptr) + (count) > eom) { \
172 __set_h_errno (NO_RECOVERY); \
173 return (NULL); \
175 } while (0)
178 static struct hostent *
179 getanswer(answer, anslen, qname, qtype)
180 const querybuf *answer;
181 int anslen;
182 const char *qname;
183 int qtype;
185 register const HEADER *hp;
186 register const u_char *cp;
187 register int n;
188 const u_char *eom, *erdata;
189 char *bp, **ap, **hap;
190 int type, class, buflen, ancount, qdcount;
191 int haveanswer, had_error;
192 int toobig = 0;
193 char tbuf[MAXDNAME];
194 const char *tname;
195 int (*name_ok) (const char *);
197 tname = qname;
198 host.h_name = NULL;
199 eom = answer->buf + anslen;
200 switch (qtype) {
201 case T_A:
202 case T_AAAA:
203 name_ok = res_hnok;
204 break;
205 case T_PTR:
206 name_ok = res_dnok;
207 break;
208 default:
209 return (NULL); /* XXX should be abort(); */
212 * find first satisfactory answer
214 hp = &answer->hdr;
215 ancount = ntohs(hp->ancount);
216 qdcount = ntohs(hp->qdcount);
217 bp = hostbuf;
218 buflen = sizeof hostbuf;
219 cp = answer->buf;
220 BOUNDED_INCR(HFIXEDSZ);
221 if (qdcount != 1) {
222 __set_h_errno (NO_RECOVERY);
223 return (NULL);
225 n = dn_expand(answer->buf, eom, cp, bp, buflen);
226 if ((n < 0) || !(*name_ok)(bp)) {
227 __set_h_errno (NO_RECOVERY);
228 return (NULL);
230 BOUNDED_INCR(n + QFIXEDSZ);
231 if (qtype == T_A || qtype == T_AAAA) {
232 /* res_send() has already verified that the query name is the
233 * same as the one we sent; this just gets the expanded name
234 * (i.e., with the succeeding search-domain tacked on).
236 n = strlen(bp) + 1; /* for the \0 */
237 if (n >= MAXHOSTNAMELEN) {
238 __set_h_errno (NO_RECOVERY);
239 return (NULL);
241 host.h_name = bp;
242 bp += n;
243 buflen -= n;
244 /* The qname can be abbreviated, but h_name is now absolute. */
245 qname = host.h_name;
247 ap = host_aliases;
248 *ap = NULL;
249 host.h_aliases = host_aliases;
250 hap = h_addr_ptrs;
251 *hap = NULL;
252 host.h_addr_list = h_addr_ptrs;
253 haveanswer = 0;
254 had_error = 0;
255 while (ancount-- > 0 && cp < eom && !had_error) {
256 n = dn_expand(answer->buf, eom, cp, bp, buflen);
257 if ((n < 0) || !(*name_ok)(bp)) {
258 had_error++;
259 continue;
261 cp += n; /* name */
262 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
263 type = ns_get16(cp);
264 cp += INT16SZ; /* type */
265 class = ns_get16(cp);
266 cp += INT16SZ + INT32SZ; /* class, TTL */
267 n = ns_get16(cp);
268 cp += INT16SZ; /* len */
269 BOUNDS_CHECK(cp, n);
270 erdata = cp + n;
271 if (class != C_IN) {
272 /* XXX - debug? syslog? */
273 cp += n;
274 continue; /* XXX - had_error++ ? */
276 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
277 if (ap >= &host_aliases[MAXALIASES-1])
278 continue;
279 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
280 if ((n < 0) || !(*name_ok)(tbuf)) {
281 had_error++;
282 continue;
284 cp += n;
285 if (cp != erdata) {
286 __set_h_errno (NO_RECOVERY);
287 return (NULL);
289 /* Store alias. */
290 *ap++ = bp;
291 n = strlen(bp) + 1; /* for the \0 */
292 if (n >= MAXHOSTNAMELEN) {
293 had_error++;
294 continue;
296 bp += n;
297 buflen -= n;
298 /* Get canonical name. */
299 n = strlen(tbuf) + 1; /* for the \0 */
300 if (n > buflen || n >= MAXHOSTNAMELEN) {
301 had_error++;
302 continue;
304 strcpy(bp, tbuf);
305 host.h_name = bp;
306 bp += n;
307 buflen -= n;
308 continue;
310 if (qtype == T_PTR && type == T_CNAME) {
311 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
312 if (n < 0 || !res_dnok(tbuf)) {
313 had_error++;
314 continue;
316 cp += n;
317 if (cp != erdata) {
318 __set_h_errno (NO_RECOVERY);
319 return (NULL);
321 /* Get canonical name. */
322 n = strlen(tbuf) + 1; /* for the \0 */
323 if (n > buflen || n >= MAXHOSTNAMELEN) {
324 had_error++;
325 continue;
327 strcpy(bp, tbuf);
328 tname = bp;
329 bp += n;
330 buflen -= n;
331 continue;
333 if ((type == T_SIG) || (type == T_KEY) || (type == T_NXT)) {
334 /* We don't support DNSSEC yet. For now, ignore
335 * the record and send a low priority message
336 * to syslog.
338 syslog(LOG_DEBUG|LOG_AUTH,
339 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
340 qname, p_class(C_IN), p_type(qtype),
341 p_type(type));
342 cp += n;
343 continue;
345 if (type != qtype) {
346 syslog(LOG_NOTICE|LOG_AUTH,
347 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
348 qname, p_class(C_IN), p_type(qtype),
349 p_type(type));
350 cp += n;
351 continue; /* XXX - had_error++ ? */
353 switch (type) {
354 case T_PTR:
355 if (strcasecmp(tname, bp) != 0) {
356 syslog(LOG_NOTICE|LOG_AUTH,
357 AskedForGot, qname, bp);
358 cp += n;
359 continue; /* XXX - had_error++ ? */
361 n = dn_expand(answer->buf, eom, cp, bp, buflen);
362 if ((n < 0) || !res_hnok(bp)) {
363 had_error++;
364 break;
366 #if MULTI_PTRS_ARE_ALIASES
367 cp += n;
368 if (cp != erdata) {
369 __set_h_errno (NO_RECOVERY);
370 return (NULL);
372 if (!haveanswer)
373 host.h_name = bp;
374 else if (ap < &host_aliases[MAXALIASES-1])
375 *ap++ = bp;
376 else
377 n = -1;
378 if (n != -1) {
379 n = strlen(bp) + 1; /* for the \0 */
380 if (n >= MAXHOSTNAMELEN) {
381 had_error++;
382 break;
384 bp += n;
385 buflen -= n;
387 break;
388 #else
389 host.h_name = bp;
390 if (_res.options & RES_USE_INET6) {
391 n = strlen(bp) + 1; /* for the \0 */
392 if (n >= MAXHOSTNAMELEN) {
393 had_error++;
394 break;
396 bp += n;
397 buflen -= n;
398 map_v4v6_hostent(&host, &bp, &buflen);
400 __set_h_errno (NETDB_SUCCESS);
401 return (&host);
402 #endif
403 case T_A:
404 case T_AAAA:
405 if (strcasecmp(host.h_name, bp) != 0) {
406 syslog(LOG_NOTICE|LOG_AUTH,
407 AskedForGot, host.h_name, bp);
408 cp += n;
409 continue; /* XXX - had_error++ ? */
411 if (n != host.h_length) {
412 cp += n;
413 continue;
415 if (!haveanswer) {
416 register int nn;
418 host.h_name = bp;
419 nn = strlen(bp) + 1; /* for the \0 */
420 bp += nn;
421 buflen -= nn;
424 /* XXX: when incrementing bp, we have to decrement
425 * buflen by the same amount --okir */
426 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
428 bp += sizeof(align) - ((u_long)bp % sizeof(align));
430 if (bp + n >= &hostbuf[sizeof hostbuf]) {
431 Dprintf("size (%d) too big\n", n);
432 had_error++;
433 continue;
435 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
436 if (!toobig++) {
437 Dprintf("Too many addresses (%d)\n",
438 MAXADDRS);
440 cp += n;
441 continue;
443 memmove(*hap++ = bp, cp, n);
444 bp += n;
445 buflen -= n;
446 cp += n;
447 if (cp != erdata) {
448 __set_h_errno (NO_RECOVERY);
449 return (NULL);
451 break;
452 default:
453 abort();
455 if (!had_error)
456 haveanswer++;
458 if (haveanswer) {
459 *ap = NULL;
460 *hap = NULL;
461 # if defined(RESOLVSORT)
463 * Note: we sort even if host can take only one address
464 * in its return structures - should give it the "best"
465 * address in that case, not some random one
467 if (_res.nsort && haveanswer > 1 && qtype == T_A)
468 addrsort(h_addr_ptrs, haveanswer);
469 # endif /*RESOLVSORT*/
470 if (!host.h_name) {
471 n = strlen(qname) + 1; /* for the \0 */
472 if (n > buflen || n >= MAXHOSTNAMELEN)
473 goto no_recovery;
474 strcpy(bp, qname);
475 host.h_name = bp;
476 bp += n;
477 buflen -= n;
479 if (_res.options & RES_USE_INET6)
480 map_v4v6_hostent(&host, &bp, &buflen);
481 __set_h_errno (NETDB_SUCCESS);
482 return (&host);
484 no_recovery:
485 __set_h_errno (NO_RECOVERY);
486 return (NULL);
489 extern struct hostent *gethostbyname2(const char *name, int af);
490 libresolv_hidden_proto (gethostbyname2)
492 struct hostent *
493 gethostbyname(name)
494 const char *name;
496 struct hostent *hp;
498 if (__res_maybe_init (&_res, 0) == -1) {
499 __set_h_errno (NETDB_INTERNAL);
500 return (NULL);
502 if (_res.options & RES_USE_INET6) {
503 hp = gethostbyname2(name, AF_INET6);
504 if (hp)
505 return (hp);
507 return (gethostbyname2(name, AF_INET));
510 struct hostent *
511 gethostbyname2(name, af)
512 const char *name;
513 int af;
515 union
517 querybuf *buf;
518 u_char *ptr;
519 } buf;
520 querybuf *origbuf;
521 register const char *cp;
522 char *bp;
523 int n, size, type, len;
524 struct hostent *ret;
525 extern struct hostent *_gethtbyname2();
527 if (__res_maybe_init (&_res, 0) == -1) {
528 __set_h_errno (NETDB_INTERNAL);
529 return (NULL);
532 switch (af) {
533 case AF_INET:
534 size = INADDRSZ;
535 type = T_A;
536 break;
537 case AF_INET6:
538 size = IN6ADDRSZ;
539 type = T_AAAA;
540 break;
541 default:
542 __set_h_errno (NETDB_INTERNAL);
543 __set_errno (EAFNOSUPPORT);
544 return (NULL);
547 host.h_addrtype = af;
548 host.h_length = size;
551 * if there aren't any dots, it could be a user-level alias.
552 * this is also done in res_query() since we are not the only
553 * function that looks up host names.
555 if (!strchr(name, '.') && (cp = __hostalias(name)))
556 name = cp;
559 * disallow names consisting only of digits/dots, unless
560 * they end in a dot.
562 if (isdigit(name[0]))
563 for (cp = name;; ++cp) {
564 if (!*cp) {
565 if (*--cp == '.')
566 break;
568 * All-numeric, no dot at the end.
569 * Fake up a hostent as if we'd actually
570 * done a lookup.
572 if (inet_pton(af, name, host_addr) <= 0) {
573 __set_h_errno (HOST_NOT_FOUND);
574 return (NULL);
576 strncpy(hostbuf, name, MAXDNAME);
577 hostbuf[MAXDNAME] = '\0';
578 bp = hostbuf + MAXDNAME;
579 len = sizeof hostbuf - MAXDNAME;
580 host.h_name = hostbuf;
581 host.h_aliases = host_aliases;
582 host_aliases[0] = NULL;
583 h_addr_ptrs[0] = (char *)host_addr;
584 h_addr_ptrs[1] = NULL;
585 host.h_addr_list = h_addr_ptrs;
586 if (_res.options & RES_USE_INET6)
587 map_v4v6_hostent(&host, &bp, &len);
588 __set_h_errno (NETDB_SUCCESS);
589 return (&host);
591 if (!isdigit(*cp) && *cp != '.')
592 break;
594 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
595 name[0] == ':')
596 for (cp = name;; ++cp) {
597 if (!*cp) {
598 if (*--cp == '.')
599 break;
601 * All-IPv6-legal, no dot at the end.
602 * Fake up a hostent as if we'd actually
603 * done a lookup.
605 if (inet_pton(af, name, host_addr) <= 0) {
606 __set_h_errno (HOST_NOT_FOUND);
607 return (NULL);
609 strncpy(hostbuf, name, MAXDNAME);
610 hostbuf[MAXDNAME] = '\0';
611 bp = hostbuf + MAXDNAME;
612 len = sizeof hostbuf - MAXDNAME;
613 host.h_name = hostbuf;
614 host.h_aliases = host_aliases;
615 host_aliases[0] = NULL;
616 h_addr_ptrs[0] = (char *)host_addr;
617 h_addr_ptrs[1] = NULL;
618 host.h_addr_list = h_addr_ptrs;
619 __set_h_errno (NETDB_SUCCESS);
620 return (&host);
622 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
623 break;
626 buf.buf = origbuf = (querybuf *) alloca (1024);
628 if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024,
629 &buf.ptr)) < 0) {
630 if (buf.buf != origbuf)
631 free (buf.buf);
632 Dprintf("res_nsearch failed (%d)\n", n);
633 if (errno == ECONNREFUSED)
634 return (_gethtbyname2(name, af));
635 return (NULL);
637 ret = getanswer(buf.buf, n, name, type);
638 if (buf.buf != origbuf)
639 free (buf.buf);
640 return ret;
642 libresolv_hidden_def (gethostbyname2)
644 struct hostent *
645 gethostbyaddr(addr, len, af)
646 const void *addr;
647 socklen_t len;
648 int af;
650 const u_char *uaddr = (const u_char *)addr;
651 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
652 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
653 int n;
654 socklen_t size;
655 union
657 querybuf *buf;
658 u_char *ptr;
659 } buf;
660 querybuf *orig_buf;
661 register struct hostent *hp;
662 char qbuf[MAXDNAME+1], *qp = NULL;
663 #ifdef SUNSECURITY
664 register struct hostent *rhp;
665 char **haddr;
666 u_long old_options;
667 char hname2[MAXDNAME+1];
668 #endif /*SUNSECURITY*/
669 extern struct hostent *_gethtbyaddr();
671 if (__res_maybe_init (&_res, 0) == -1) {
672 __set_h_errno (NETDB_INTERNAL);
673 return (NULL);
675 if (af == AF_INET6 && len == IN6ADDRSZ &&
676 (!bcmp(uaddr, mapped, sizeof mapped) ||
677 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
678 /* Unmap. */
679 addr += sizeof mapped;
680 uaddr += sizeof mapped;
681 af = AF_INET;
682 len = INADDRSZ;
684 switch (af) {
685 case AF_INET:
686 size = INADDRSZ;
687 break;
688 case AF_INET6:
689 size = IN6ADDRSZ;
690 break;
691 default:
692 __set_errno (EAFNOSUPPORT);
693 __set_h_errno (NETDB_INTERNAL);
694 return (NULL);
696 if (size != len) {
697 __set_errno (EINVAL);
698 __set_h_errno (NETDB_INTERNAL);
699 return (NULL);
701 switch (af) {
702 case AF_INET:
703 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
704 (uaddr[3] & 0xff),
705 (uaddr[2] & 0xff),
706 (uaddr[1] & 0xff),
707 (uaddr[0] & 0xff));
708 break;
709 case AF_INET6:
710 qp = qbuf;
711 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
712 qp += SPRINTF((qp, "%x.%x.",
713 uaddr[n] & 0xf,
714 (uaddr[n] >> 4) & 0xf));
716 strcpy(qp, "ip6.arpa");
717 break;
718 default:
719 abort();
722 buf.buf = orig_buf = (querybuf *) alloca (1024);
724 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
725 &buf.ptr);
726 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) {
727 strcpy(qp, "ip6.int");
728 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf,
729 buf.buf != orig_buf ? MAXPACKET : 1024,
730 &buf.ptr);
732 if (n < 0) {
733 if (buf.buf != orig_buf)
734 free (buf.buf);
735 Dprintf("res_nquery failed (%d)\n", n);
736 if (errno == ECONNREFUSED)
737 return (_gethtbyaddr(addr, len, af));
738 return (NULL);
740 hp = getanswer(buf.buf, n, qbuf, T_PTR);
741 if (buf.buf != orig_buf)
742 free (buf.buf);
743 if (!hp)
744 return (NULL); /* h_errno was set by getanswer() */
745 #ifdef SUNSECURITY
746 if (af == AF_INET) {
748 * turn off search as the name should be absolute,
749 * 'localhost' should be matched by defnames
751 strncpy(hname2, hp->h_name, MAXDNAME);
752 hname2[MAXDNAME] = '\0';
753 old_options = _res.options;
754 _res.options &= ~RES_DNSRCH;
755 _res.options |= RES_DEFNAMES;
756 if (!(rhp = gethostbyname(hname2))) {
757 syslog(LOG_NOTICE|LOG_AUTH,
758 "gethostbyaddr: No A record for %s (verifying [%s])",
759 hname2, inet_ntoa(*((struct in_addr *)addr)));
760 _res.options = old_options;
761 __set_h_errno (HOST_NOT_FOUND);
762 return (NULL);
764 _res.options = old_options;
765 for (haddr = rhp->h_addr_list; *haddr; haddr++)
766 if (!memcmp(*haddr, addr, INADDRSZ))
767 break;
768 if (!*haddr) {
769 syslog(LOG_NOTICE|LOG_AUTH,
770 "gethostbyaddr: A record of %s != PTR record [%s]",
771 hname2, inet_ntoa(*((struct in_addr *)addr)));
772 __set_h_errno (HOST_NOT_FOUND);
773 return (NULL);
776 #endif /*SUNSECURITY*/
777 hp->h_addrtype = af;
778 hp->h_length = len;
779 memmove(host_addr, addr, len);
780 h_addr_ptrs[0] = (char *)host_addr;
781 h_addr_ptrs[1] = NULL;
782 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
783 map_v4v6_address((char*)host_addr, (char*)host_addr);
784 hp->h_addrtype = AF_INET6;
785 hp->h_length = IN6ADDRSZ;
787 __set_h_errno (NETDB_SUCCESS);
788 return (hp);
791 void
792 _sethtent(f)
793 int f;
795 if (!hostf)
796 hostf = fopen(_PATH_HOSTS, "r" );
797 else
798 rewind(hostf);
799 stayopen = f;
801 libresolv_hidden_def (_sethtent)
803 void
804 _endhtent()
806 if (hostf && !stayopen) {
807 (void) fclose(hostf);
808 hostf = NULL;
812 struct hostent *
813 _gethtent()
815 char *p;
816 register char *cp, **q;
817 int af, len;
819 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
820 __set_h_errno (NETDB_INTERNAL);
821 return (NULL);
823 again:
824 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
825 __set_h_errno (HOST_NOT_FOUND);
826 return (NULL);
828 if (*p == '#')
829 goto again;
830 if (!(cp = strpbrk(p, "#\n")))
831 goto again;
832 *cp = '\0';
833 if (!(cp = strpbrk(p, " \t")))
834 goto again;
835 *cp++ = '\0';
836 if (inet_pton(AF_INET6, p, host_addr) > 0) {
837 af = AF_INET6;
838 len = IN6ADDRSZ;
839 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
840 if (_res.options & RES_USE_INET6) {
841 map_v4v6_address((char*)host_addr, (char*)host_addr);
842 af = AF_INET6;
843 len = IN6ADDRSZ;
844 } else {
845 af = AF_INET;
846 len = INADDRSZ;
848 } else {
849 goto again;
851 h_addr_ptrs[0] = (char *)host_addr;
852 h_addr_ptrs[1] = NULL;
853 host.h_addr_list = h_addr_ptrs;
854 host.h_length = len;
855 host.h_addrtype = af;
856 while (*cp == ' ' || *cp == '\t')
857 cp++;
858 host.h_name = cp;
859 q = host.h_aliases = host_aliases;
860 if ((cp = strpbrk(cp, " \t")))
861 *cp++ = '\0';
862 while (cp && *cp) {
863 if (*cp == ' ' || *cp == '\t') {
864 cp++;
865 continue;
867 if (q < &host_aliases[MAXALIASES - 1])
868 *q++ = cp;
869 if ((cp = strpbrk(cp, " \t")))
870 *cp++ = '\0';
872 *q = NULL;
873 __set_h_errno (NETDB_SUCCESS);
874 return (&host);
876 libresolv_hidden_def (_gethtent)
878 struct hostent *
879 _gethtbyname(name)
880 const char *name;
882 extern struct hostent *_gethtbyname2();
883 struct hostent *hp;
885 if (_res.options & RES_USE_INET6) {
886 hp = _gethtbyname2(name, AF_INET6);
887 if (hp)
888 return (hp);
890 return (_gethtbyname2(name, AF_INET));
893 struct hostent *
894 _gethtbyname2(name, af)
895 const char *name;
896 int af;
898 register struct hostent *p;
899 register char **cp;
901 _sethtent(0);
902 while ((p = _gethtent())) {
903 if (p->h_addrtype != af)
904 continue;
905 if (strcasecmp(p->h_name, name) == 0)
906 break;
907 for (cp = p->h_aliases; *cp != 0; cp++)
908 if (strcasecmp(*cp, name) == 0)
909 goto found;
911 found:
912 _endhtent();
913 return (p);
915 libresolv_hidden_def (_gethtbyname2)
917 struct hostent *
918 _gethtbyaddr(addr, len, af)
919 const char *addr;
920 size_t len;
921 int af;
923 register struct hostent *p;
925 _sethtent(0);
926 while ((p = _gethtent()))
927 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
928 break;
929 _endhtent();
930 return (p);
932 libresolv_hidden_def (_gethtbyaddr)
934 static void
935 map_v4v6_address(src, dst)
936 const char *src;
937 char *dst;
939 u_char *p = (u_char *)dst;
940 char tmp[INADDRSZ];
941 int i;
943 /* Stash a temporary copy so our caller can update in place. */
944 memcpy(tmp, src, INADDRSZ);
945 /* Mark this ipv6 addr as a mapped ipv4. */
946 for (i = 0; i < 10; i++)
947 *p++ = 0x00;
948 *p++ = 0xff;
949 *p++ = 0xff;
950 /* Retrieve the saved copy and we're done. */
951 memcpy((void*)p, tmp, INADDRSZ);
954 static void
955 map_v4v6_hostent(hp, bpp, lenp)
956 struct hostent *hp;
957 char **bpp;
958 int *lenp;
960 char **ap;
962 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
963 return;
964 hp->h_addrtype = AF_INET6;
965 hp->h_length = IN6ADDRSZ;
966 for (ap = hp->h_addr_list; *ap; ap++) {
967 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
969 if (*lenp < (i + IN6ADDRSZ)) {
970 /* Out of memory. Truncate address list here. XXX */
971 *ap = NULL;
972 return;
974 *bpp += i;
975 *lenp -= i;
976 map_v4v6_address(*ap, *bpp);
977 *ap = *bpp;
978 *bpp += IN6ADDRSZ;
979 *lenp -= IN6ADDRSZ;
983 #ifdef RESOLVSORT
984 extern void
985 addrsort(ap, num)
986 char **ap;
987 int num;
989 int i, j;
990 char **p;
991 short aval[MAXADDRS];
992 int needsort = 0;
994 p = ap;
995 for (i = 0; i < num; i++, p++) {
996 for (j = 0 ; (unsigned)j < _res.nsort; j++)
997 if (_res.sort_list[j].addr.s_addr ==
998 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
999 break;
1000 aval[i] = j;
1001 if (needsort == 0 && i > 0 && j < aval[i-1])
1002 needsort = i;
1004 if (!needsort)
1005 return;
1007 while (needsort < num) {
1008 for (j = needsort - 1; j >= 0; j--) {
1009 if (aval[j] > aval[j+1]) {
1010 char *hp;
1012 i = aval[j];
1013 aval[j] = aval[j+1];
1014 aval[j+1] = i;
1016 hp = ap[j];
1017 ap[j] = ap[j+1];
1018 ap[j+1] = hp;
1020 } else
1021 break;
1023 needsort++;
1026 #endif
1028 #if defined(BSD43_BSD43_NFS) || defined(sun)
1029 /* some libc's out there are bound internally to these names (UMIPS) */
1030 void
1031 ht_sethostent(stayopen)
1032 int stayopen;
1034 _sethtent(stayopen);
1037 void
1038 ht_endhostent()
1040 _endhtent();
1043 struct hostent *
1044 ht_gethostbyname(name)
1045 char *name;
1047 return (_gethtbyname(name));
1050 struct hostent *
1051 ht_gethostbyaddr(addr, len, af)
1052 const char *addr;
1053 size_t len;
1054 int af;
1056 return (_gethtbyaddr(addr, len, af));
1059 struct hostent *
1060 gethostent()
1062 return (_gethtent());
1065 void
1066 dns_service()
1068 return;
1071 #undef dn_skipname
1072 dn_skipname(comp_dn, eom)
1073 const u_char *comp_dn, *eom;
1075 return (__dn_skipname(comp_dn, eom));
1077 #endif /*old-style libc with yp junk in it*/