Remove some references to bcopy/bcmp/bzero.
[glibc.git] / resolv / gethnamaddr.c
bloba861a847cecd2a500b7a0b7dbaff4ff7e7e02426
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 /* 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. */
57 #if defined(LIBC_SCCS) && !defined(lint)
58 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
68 #include <stdio.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <errno.h>
73 #include <syslog.h>
75 #define RESOLVSORT
77 #ifndef LOG_AUTH
78 # define LOG_AUTH 0
79 #endif
81 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
83 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
84 # include <stdlib.h>
85 # include <string.h>
86 #else
87 # include "../conf/portability.h"
88 #endif
90 #if defined(USE_OPTIONS_H)
91 # include <../conf/options.h>
92 #endif
94 #ifdef SPRINTF_CHAR
95 # define SPRINTF(x) strlen(sprintf/**/x)
96 #else
97 # define SPRINTF(x) ((size_t)sprintf x)
98 #endif
100 #define MAXALIASES 35
101 #define MAXADDRS 35
103 static const char AskedForGot[] =
104 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
106 static char *h_addr_ptrs[MAXADDRS + 1];
108 static struct hostent host;
109 static char *host_aliases[MAXALIASES];
110 static char hostbuf[8*1024];
111 static u_char host_addr[16]; /* IPv4 or IPv6 */
112 static FILE *hostf = NULL;
113 static int stayopen = 0;
115 static void map_v4v6_address (const char *src, char *dst) __THROW;
116 static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
118 #ifdef RESOLVSORT
119 extern void addrsort (char **, int) __THROW;
120 #endif
122 #if PACKETSZ > 65536
123 #define MAXPACKET PACKETSZ
124 #else
125 #define MAXPACKET 65536
126 #endif
128 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
129 #ifdef MAXHOSTNAMELEN
130 # undef MAXHOSTNAMELEN
131 #endif
132 #define MAXHOSTNAMELEN 256
134 typedef union {
135 HEADER hdr;
136 u_char buf[MAXPACKET];
137 } querybuf;
139 typedef union {
140 int32_t al;
141 char ac;
142 } align;
144 #ifndef h_errno
145 extern int h_errno;
146 #endif
148 #ifdef DEBUG
149 static void
150 Dprintf(msg, num)
151 char *msg;
152 int num;
154 if (_res.options & RES_DEBUG) {
155 int save = errno;
157 printf(msg, num);
158 __set_errno (save);
161 #else
162 # define Dprintf(msg, num) /*nada*/
163 #endif
165 #define BOUNDED_INCR(x) \
166 do { \
167 cp += x; \
168 if (cp > eom) { \
169 __set_h_errno (NO_RECOVERY); \
170 return (NULL); \
172 } while (0)
174 #define BOUNDS_CHECK(ptr, count) \
175 do { \
176 if ((ptr) + (count) > eom) { \
177 __set_h_errno (NO_RECOVERY); \
178 return (NULL); \
180 } while (0)
183 static struct hostent *
184 getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
186 const HEADER *hp;
187 const u_char *cp;
188 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) (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 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 extern struct hostent *gethostbyname2(const char *name, int af);
491 libresolv_hidden_proto (gethostbyname2)
493 struct hostent *
494 gethostbyname(name)
495 const char *name;
497 struct hostent *hp;
499 if (__res_maybe_init (&_res, 0) == -1) {
500 __set_h_errno (NETDB_INTERNAL);
501 return (NULL);
503 if (_res.options & RES_USE_INET6) {
504 hp = gethostbyname2(name, AF_INET6);
505 if (hp)
506 return (hp);
508 return (gethostbyname2(name, AF_INET));
511 struct hostent *
512 gethostbyname2(name, af)
513 const char *name;
514 int af;
516 union
518 querybuf *buf;
519 u_char *ptr;
520 } buf;
521 querybuf *origbuf;
522 const char *cp;
523 char *bp;
524 int n, size, type, len;
525 struct hostent *ret;
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, NULL, NULL, NULL, NULL)) < 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 struct hostent *hp;
662 char qbuf[MAXDNAME+1], *qp = NULL;
663 #ifdef SUNSECURITY
664 struct hostent *rhp;
665 char **haddr;
666 u_long old_options;
667 char hname2[MAXDNAME+1];
668 #endif /*SUNSECURITY*/
670 if (__res_maybe_init (&_res, 0) == -1) {
671 __set_h_errno (NETDB_INTERNAL);
672 return (NULL);
674 if (af == AF_INET6 && len == IN6ADDRSZ &&
675 (!memcmp(uaddr, mapped, sizeof mapped) ||
676 !memcmp(uaddr, tunnelled, sizeof tunnelled))) {
677 /* Unmap. */
678 addr += sizeof mapped;
679 uaddr += sizeof mapped;
680 af = AF_INET;
681 len = INADDRSZ;
683 switch (af) {
684 case AF_INET:
685 size = INADDRSZ;
686 break;
687 case AF_INET6:
688 size = IN6ADDRSZ;
689 break;
690 default:
691 __set_errno (EAFNOSUPPORT);
692 __set_h_errno (NETDB_INTERNAL);
693 return (NULL);
695 if (size != len) {
696 __set_errno (EINVAL);
697 __set_h_errno (NETDB_INTERNAL);
698 return (NULL);
700 switch (af) {
701 case AF_INET:
702 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
703 (uaddr[3] & 0xff),
704 (uaddr[2] & 0xff),
705 (uaddr[1] & 0xff),
706 (uaddr[0] & 0xff));
707 break;
708 case AF_INET6:
709 qp = qbuf;
710 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
711 qp += SPRINTF((qp, "%x.%x.",
712 uaddr[n] & 0xf,
713 (uaddr[n] >> 4) & 0xf));
715 strcpy(qp, "ip6.arpa");
716 break;
717 default:
718 abort();
721 buf.buf = orig_buf = (querybuf *) alloca (1024);
723 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
724 &buf.ptr, NULL, NULL, NULL, NULL);
725 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) {
726 strcpy(qp, "ip6.int");
727 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf,
728 buf.buf != orig_buf ? MAXPACKET : 1024,
729 &buf.ptr, NULL, NULL, NULL, NULL);
731 if (n < 0) {
732 if (buf.buf != orig_buf)
733 free (buf.buf);
734 Dprintf("res_nquery failed (%d)\n", n);
735 if (errno == ECONNREFUSED)
736 return (_gethtbyaddr(addr, len, af));
737 return (NULL);
739 hp = getanswer(buf.buf, n, qbuf, T_PTR);
740 if (buf.buf != orig_buf)
741 free (buf.buf);
742 if (!hp)
743 return (NULL); /* h_errno was set by getanswer() */
744 #ifdef SUNSECURITY
745 if (af == AF_INET) {
747 * turn off search as the name should be absolute,
748 * 'localhost' should be matched by defnames
750 strncpy(hname2, hp->h_name, MAXDNAME);
751 hname2[MAXDNAME] = '\0';
752 old_options = _res.options;
753 _res.options &= ~RES_DNSRCH;
754 _res.options |= RES_DEFNAMES;
755 if (!(rhp = gethostbyname(hname2))) {
756 syslog(LOG_NOTICE|LOG_AUTH,
757 "gethostbyaddr: No A record for %s (verifying [%s])",
758 hname2, inet_ntoa(*((struct in_addr *)addr)));
759 _res.options = old_options;
760 __set_h_errno (HOST_NOT_FOUND);
761 return (NULL);
763 _res.options = old_options;
764 for (haddr = rhp->h_addr_list; *haddr; haddr++)
765 if (!memcmp(*haddr, addr, INADDRSZ))
766 break;
767 if (!*haddr) {
768 syslog(LOG_NOTICE|LOG_AUTH,
769 "gethostbyaddr: A record of %s != PTR record [%s]",
770 hname2, inet_ntoa(*((struct in_addr *)addr)));
771 __set_h_errno (HOST_NOT_FOUND);
772 return (NULL);
775 #endif /*SUNSECURITY*/
776 hp->h_addrtype = af;
777 hp->h_length = len;
778 memmove(host_addr, addr, len);
779 h_addr_ptrs[0] = (char *)host_addr;
780 h_addr_ptrs[1] = NULL;
781 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
782 map_v4v6_address((char*)host_addr, (char*)host_addr);
783 hp->h_addrtype = AF_INET6;
784 hp->h_length = IN6ADDRSZ;
786 __set_h_errno (NETDB_SUCCESS);
787 return (hp);
790 void
791 _sethtent(f)
792 int f;
794 if (!hostf)
795 hostf = fopen(_PATH_HOSTS, "rce" );
796 else
797 rewind(hostf);
798 stayopen = f;
800 libresolv_hidden_def (_sethtent)
802 void
803 _endhtent (void)
805 if (hostf && !stayopen) {
806 (void) fclose(hostf);
807 hostf = NULL;
811 struct hostent *
812 _gethtent (void)
814 char *p;
815 char *cp, **q;
816 int af, len;
818 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) {
819 __set_h_errno (NETDB_INTERNAL);
820 return (NULL);
822 again:
823 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
824 __set_h_errno (HOST_NOT_FOUND);
825 return (NULL);
827 if (*p == '#')
828 goto again;
829 if (!(cp = strpbrk(p, "#\n")))
830 goto again;
831 *cp = '\0';
832 if (!(cp = strpbrk(p, " \t")))
833 goto again;
834 *cp++ = '\0';
835 if (inet_pton(AF_INET6, p, host_addr) > 0) {
836 af = AF_INET6;
837 len = IN6ADDRSZ;
838 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
839 if (_res.options & RES_USE_INET6) {
840 map_v4v6_address((char*)host_addr, (char*)host_addr);
841 af = AF_INET6;
842 len = IN6ADDRSZ;
843 } else {
844 af = AF_INET;
845 len = INADDRSZ;
847 } else {
848 goto again;
850 h_addr_ptrs[0] = (char *)host_addr;
851 h_addr_ptrs[1] = NULL;
852 host.h_addr_list = h_addr_ptrs;
853 host.h_length = len;
854 host.h_addrtype = af;
855 while (*cp == ' ' || *cp == '\t')
856 cp++;
857 host.h_name = cp;
858 q = host.h_aliases = host_aliases;
859 if ((cp = strpbrk(cp, " \t")))
860 *cp++ = '\0';
861 while (cp && *cp) {
862 if (*cp == ' ' || *cp == '\t') {
863 cp++;
864 continue;
866 if (q < &host_aliases[MAXALIASES - 1])
867 *q++ = cp;
868 if ((cp = strpbrk(cp, " \t")))
869 *cp++ = '\0';
871 *q = NULL;
872 __set_h_errno (NETDB_SUCCESS);
873 return (&host);
875 libresolv_hidden_def (_gethtent)
877 struct hostent *
878 _gethtbyname(name)
879 const char *name;
881 struct hostent *hp;
883 if (_res.options & RES_USE_INET6) {
884 hp = _gethtbyname2(name, AF_INET6);
885 if (hp)
886 return (hp);
888 return (_gethtbyname2(name, AF_INET));
891 struct hostent *
892 _gethtbyname2(name, af)
893 const char *name;
894 int af;
896 struct hostent *p;
897 char **cp;
899 _sethtent(0);
900 while ((p = _gethtent())) {
901 if (p->h_addrtype != af)
902 continue;
903 if (strcasecmp(p->h_name, name) == 0)
904 break;
905 for (cp = p->h_aliases; *cp != 0; cp++)
906 if (strcasecmp(*cp, name) == 0)
907 goto found;
909 found:
910 _endhtent();
911 return (p);
913 libresolv_hidden_def (_gethtbyname2)
915 struct hostent *
916 _gethtbyaddr(addr, len, af)
917 const char *addr;
918 size_t len;
919 int af;
921 struct hostent *p;
923 _sethtent(0);
924 while ((p = _gethtent()))
925 if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len))
926 break;
927 _endhtent();
928 return (p);
930 libresolv_hidden_def (_gethtbyaddr)
932 static void
933 map_v4v6_address(src, dst)
934 const char *src;
935 char *dst;
937 u_char *p = (u_char *)dst;
938 char tmp[INADDRSZ];
939 int i;
941 /* Stash a temporary copy so our caller can update in place. */
942 memcpy(tmp, src, INADDRSZ);
943 /* Mark this ipv6 addr as a mapped ipv4. */
944 for (i = 0; i < 10; i++)
945 *p++ = 0x00;
946 *p++ = 0xff;
947 *p++ = 0xff;
948 /* Retrieve the saved copy and we're done. */
949 memcpy((void*)p, tmp, INADDRSZ);
952 static void
953 map_v4v6_hostent(hp, bpp, lenp)
954 struct hostent *hp;
955 char **bpp;
956 int *lenp;
958 char **ap;
960 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
961 return;
962 hp->h_addrtype = AF_INET6;
963 hp->h_length = IN6ADDRSZ;
964 for (ap = hp->h_addr_list; *ap; ap++) {
965 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
967 if (*lenp < (i + IN6ADDRSZ)) {
968 /* Out of memory. Truncate address list here. XXX */
969 *ap = NULL;
970 return;
972 *bpp += i;
973 *lenp -= i;
974 map_v4v6_address(*ap, *bpp);
975 *ap = *bpp;
976 *bpp += IN6ADDRSZ;
977 *lenp -= IN6ADDRSZ;
981 #ifdef RESOLVSORT
982 extern void
983 addrsort(ap, num)
984 char **ap;
985 int num;
987 int i, j;
988 char **p;
989 short aval[MAXADDRS];
990 int needsort = 0;
992 p = ap;
993 for (i = 0; i < num; i++, p++) {
994 for (j = 0 ; (unsigned)j < _res.nsort; j++)
995 if (_res.sort_list[j].addr.s_addr ==
996 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
997 break;
998 aval[i] = j;
999 if (needsort == 0 && i > 0 && j < aval[i-1])
1000 needsort = i;
1002 if (!needsort)
1003 return;
1005 while (needsort < num) {
1006 for (j = needsort - 1; j >= 0; j--) {
1007 if (aval[j] > aval[j+1]) {
1008 char *hp;
1010 i = aval[j];
1011 aval[j] = aval[j+1];
1012 aval[j+1] = i;
1014 hp = ap[j];
1015 ap[j] = ap[j+1];
1016 ap[j+1] = hp;
1018 } else
1019 break;
1021 needsort++;
1024 #endif
1026 #if defined(BSD43_BSD43_NFS) || defined(sun)
1027 /* some libc's out there are bound internally to these names (UMIPS) */
1028 void
1029 ht_sethostent(stayopen)
1030 int stayopen;
1032 _sethtent(stayopen);
1035 void
1036 ht_endhostent (void)
1038 _endhtent();
1041 struct hostent *
1042 ht_gethostbyname(name)
1043 char *name;
1045 return (_gethtbyname(name));
1048 struct hostent *
1049 ht_gethostbyaddr(addr, len, af)
1050 const char *addr;
1051 size_t len;
1052 int af;
1054 return (_gethtbyaddr(addr, len, af));
1057 struct hostent *
1058 gethostent (void)
1060 return (_gethtent());
1063 void
1064 dns_service (void)
1066 return;
1069 #undef dn_skipname
1070 dn_skipname(comp_dn, eom)
1071 const u_char *comp_dn, *eom;
1073 return (__dn_skipname(comp_dn, eom));
1075 #endif /*old-style libc with yp junk in it*/