Update.
[glibc.git] / resolv / gethnamaddr.c
blob755fc52ea8d533b9d708ea3742dbae99eae9ce48
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 * -
53 * --Copyright--
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
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 #ifndef LOG_AUTH
76 # define LOG_AUTH 0
77 #endif
79 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
81 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
82 # include <stdlib.h>
83 # include <string.h>
84 #else
85 # include "../conf/portability.h"
86 #endif
88 #if defined(USE_OPTIONS_H)
89 # include <../conf/options.h>
90 #endif
92 #ifdef SPRINTF_CHAR
93 # define SPRINTF(x) strlen(sprintf/**/x)
94 #else
95 # define SPRINTF(x) ((size_t)sprintf x)
96 #endif
98 #define MAXALIASES 35
99 #define MAXADDRS 35
101 static const char AskedForGot[] =
102 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
104 static char *h_addr_ptrs[MAXADDRS + 1];
106 static struct hostent host;
107 static char *host_aliases[MAXALIASES];
108 static char hostbuf[8*1024];
109 static u_char host_addr[16]; /* IPv4 or IPv6 */
110 static FILE *hostf = NULL;
111 static int stayopen = 0;
113 static void map_v4v6_address __P((const char *src, char *dst));
114 static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len));
116 #ifdef RESOLVSORT
117 static void addrsort __P((char **, int));
118 #endif
120 #if PACKETSZ > 1024
121 #define MAXPACKET PACKETSZ
122 #else
123 #define MAXPACKET 1024
124 #endif
126 typedef union {
127 HEADER hdr;
128 u_char buf[MAXPACKET];
129 } querybuf;
131 typedef union {
132 int32_t al;
133 char ac;
134 } align;
136 extern int h_errno;
138 #ifdef DEBUG
139 static void
140 dprintf(msg, num)
141 char *msg;
142 int num;
144 if (_res.options & RES_DEBUG) {
145 int save = errno;
147 printf(msg, num);
148 errno = save;
151 #else
152 # define dprintf(msg, num) /*nada*/
153 #endif
155 static struct hostent *
156 getanswer(answer, anslen, qname, qtype)
157 const querybuf *answer;
158 int anslen;
159 const char *qname;
160 int qtype;
162 register const HEADER *hp;
163 register const u_char *cp;
164 register int n;
165 const u_char *eom;
166 char *bp, **ap, **hap;
167 int type, class, buflen, ancount, qdcount;
168 int haveanswer, had_error;
169 int toobig = 0;
170 char tbuf[MAXDNAME+1];
171 const char *tname;
172 int (*name_ok) __P((const char *));
174 tname = qname;
175 host.h_name = NULL;
176 eom = answer->buf + anslen;
177 switch (qtype) {
178 case T_A:
179 case T_AAAA:
180 name_ok = res_hnok;
181 break;
182 case T_PTR:
183 name_ok = res_dnok;
184 break;
185 default:
186 return (NULL); /* XXX should be abort(); */
189 * find first satisfactory answer
191 hp = &answer->hdr;
192 ancount = ntohs(hp->ancount);
193 qdcount = ntohs(hp->qdcount);
194 bp = hostbuf;
195 buflen = sizeof hostbuf;
196 cp = answer->buf + HFIXEDSZ;
197 if (qdcount != 1) {
198 h_errno = NO_RECOVERY;
199 return (NULL);
201 n = dn_expand(answer->buf, eom, cp, bp, buflen);
202 if ((n < 0) || !(*name_ok)(bp)) {
203 h_errno = NO_RECOVERY;
204 return (NULL);
206 cp += n + QFIXEDSZ;
207 if (qtype == T_A || qtype == T_AAAA) {
208 /* res_send() has already verified that the query name is the
209 * same as the one we sent; this just gets the expanded name
210 * (i.e., with the succeeding search-domain tacked on).
212 n = strlen(bp) + 1; /* for the \0 */
213 host.h_name = bp;
214 bp += n;
215 buflen -= n;
216 /* The qname can be abbreviated, but h_name is now absolute. */
217 qname = host.h_name;
219 ap = host_aliases;
220 *ap = NULL;
221 host.h_aliases = host_aliases;
222 hap = h_addr_ptrs;
223 *hap = NULL;
224 host.h_addr_list = h_addr_ptrs;
225 haveanswer = 0;
226 had_error = 0;
227 while (ancount-- > 0 && cp < eom && !had_error) {
228 n = dn_expand(answer->buf, eom, cp, bp, buflen);
229 if ((n < 0) || !(*name_ok)(bp)) {
230 had_error++;
231 continue;
233 cp += n; /* name */
234 type = _getshort(cp);
235 cp += INT16SZ; /* type */
236 class = _getshort(cp);
237 cp += INT16SZ + INT32SZ; /* class, TTL */
238 n = _getshort(cp);
239 cp += INT16SZ; /* len */
240 if (class != C_IN) {
241 /* XXX - debug? syslog? */
242 cp += n;
243 continue; /* XXX - had_error++ ? */
245 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
246 if (ap >= &host_aliases[MAXALIASES-1])
247 continue;
248 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
249 if ((n < 0) || !(*name_ok)(tbuf)) {
250 had_error++;
251 continue;
253 cp += n;
254 /* Store alias. */
255 *ap++ = bp;
256 n = strlen(bp) + 1; /* for the \0 */
257 bp += n;
258 buflen -= n;
259 /* Get canonical name. */
260 n = strlen(tbuf) + 1; /* for the \0 */
261 if (n > buflen) {
262 had_error++;
263 continue;
265 strcpy(bp, tbuf);
266 host.h_name = bp;
267 bp += n;
268 buflen -= n;
269 continue;
271 if (qtype == T_PTR && type == T_CNAME) {
272 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
273 if ((n < 0) || !res_hnok(tbuf)) {
274 had_error++;
275 continue;
277 cp += n;
278 /* Get canonical name. */
279 n = strlen(tbuf) + 1; /* for the \0 */
280 if (n > buflen) {
281 had_error++;
282 continue;
284 strcpy(bp, tbuf);
285 tname = bp;
286 bp += n;
287 buflen -= n;
288 continue;
290 if (type != qtype) {
291 syslog(LOG_NOTICE|LOG_AUTH,
292 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
293 qname, p_class(C_IN), p_type(qtype),
294 p_type(type));
295 cp += n;
296 continue; /* XXX - had_error++ ? */
298 switch (type) {
299 case T_PTR:
300 if (strcasecmp(tname, bp) != 0) {
301 syslog(LOG_NOTICE|LOG_AUTH,
302 AskedForGot, qname, bp);
303 cp += n;
304 continue; /* XXX - had_error++ ? */
306 n = dn_expand(answer->buf, eom, cp, bp, buflen);
307 if ((n < 0) || !res_hnok(bp)) {
308 had_error++;
309 break;
311 #if MULTI_PTRS_ARE_ALIASES
312 cp += n;
313 if (!haveanswer)
314 host.h_name = bp;
315 else if (ap < &host_aliases[MAXALIASES-1])
316 *ap++ = bp;
317 else
318 n = -1;
319 if (n != -1) {
320 n = strlen(bp) + 1; /* for the \0 */
321 bp += n;
322 buflen -= n;
324 break;
325 #else
326 host.h_name = bp;
327 if (_res.options & RES_USE_INET6) {
328 n = strlen(bp) + 1; /* for the \0 */
329 bp += n;
330 buflen -= n;
331 map_v4v6_hostent(&host, &bp, &buflen);
333 h_errno = NETDB_SUCCESS;
334 return (&host);
335 #endif
336 case T_A:
337 case T_AAAA:
338 if (strcasecmp(host.h_name, bp) != 0) {
339 syslog(LOG_NOTICE|LOG_AUTH,
340 AskedForGot, host.h_name, bp);
341 cp += n;
342 continue; /* XXX - had_error++ ? */
344 if (haveanswer) {
345 if (n != host.h_length) {
346 cp += n;
347 continue;
349 } else {
350 register int nn;
352 host.h_name = bp;
353 nn = strlen(bp) + 1; /* for the \0 */
354 bp += nn;
355 buflen -= nn;
358 bp += sizeof(align) - ((u_long)bp % sizeof(align));
360 if (bp + n >= &hostbuf[sizeof hostbuf]) {
361 dprintf("size (%d) too big\n", n);
362 had_error++;
363 continue;
365 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
366 if (!toobig++)
367 dprintf("Too many addresses (%d)\n",
368 MAXADDRS);
369 cp += n;
370 continue;
372 bcopy(cp, *hap++ = bp, n);
373 bp += n;
374 buflen -= n;
375 cp += n;
376 break;
377 default:
378 abort();
380 if (!had_error)
381 haveanswer++;
383 if (haveanswer) {
384 *ap = NULL;
385 *hap = NULL;
386 # if defined(RESOLVSORT)
388 * Note: we sort even if host can take only one address
389 * in its return structures - should give it the "best"
390 * address in that case, not some random one
392 if (_res.nsort && haveanswer > 1 && qtype == T_A)
393 addrsort(h_addr_ptrs, haveanswer);
394 # endif /*RESOLVSORT*/
395 if (!host.h_name) {
396 n = strlen(qname) + 1; /* for the \0 */
397 if (n > buflen)
398 goto try_again;
399 strcpy(bp, qname);
400 host.h_name = bp;
401 bp += n;
402 buflen -= n;
404 if (_res.options & RES_USE_INET6)
405 map_v4v6_hostent(&host, &bp, &buflen);
406 h_errno = NETDB_SUCCESS;
407 return (&host);
409 try_again:
410 h_errno = TRY_AGAIN;
411 return (NULL);
414 struct hostent *
415 gethostbyname(name)
416 const char *name;
418 struct hostent *hp;
420 if (_res.options & RES_USE_INET6) {
421 hp = gethostbyname2(name, AF_INET6);
422 if (hp)
423 return (hp);
425 return (gethostbyname2(name, AF_INET));
428 struct hostent *
429 gethostbyname2(name, af)
430 const char *name;
431 int af;
433 querybuf buf;
434 register const char *cp;
435 char *bp;
436 int n, size, type, len;
437 extern struct hostent *_gethtbyname2();
439 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
440 h_errno = NETDB_INTERNAL;
441 return (NULL);
444 switch (af) {
445 case AF_INET:
446 size = INADDRSZ;
447 type = T_A;
448 break;
449 case AF_INET6:
450 size = IN6ADDRSZ;
451 type = T_AAAA;
452 break;
453 default:
454 h_errno = NETDB_INTERNAL;
455 errno = EAFNOSUPPORT;
456 return (NULL);
459 host.h_addrtype = af;
460 host.h_length = size;
463 * if there aren't any dots, it could be a user-level alias.
464 * this is also done in res_query() since we are not the only
465 * function that looks up host names.
467 if (!strchr(name, '.') && (cp = __hostalias(name)))
468 name = cp;
471 * disallow names consisting only of digits/dots, unless
472 * they end in a dot.
474 if (isdigit(name[0]))
475 for (cp = name;; ++cp) {
476 if (!*cp) {
477 if (*--cp == '.')
478 break;
480 * All-numeric, no dot at the end.
481 * Fake up a hostent as if we'd actually
482 * done a lookup.
484 if (inet_pton(af, name, host_addr) <= 0) {
485 h_errno = HOST_NOT_FOUND;
486 return (NULL);
488 strncpy(hostbuf, name, MAXDNAME);
489 hostbuf[MAXDNAME] = '\0';
490 bp = hostbuf + MAXDNAME;
491 len = sizeof hostbuf - MAXDNAME;
492 host.h_name = hostbuf;
493 host.h_aliases = host_aliases;
494 host_aliases[0] = NULL;
495 h_addr_ptrs[0] = (char *)host_addr;
496 h_addr_ptrs[1] = NULL;
497 host.h_addr_list = h_addr_ptrs;
498 if (_res.options & RES_USE_INET6)
499 map_v4v6_hostent(&host, &bp, &len);
500 h_errno = NETDB_SUCCESS;
501 return (&host);
503 if (!isdigit(*cp) && *cp != '.')
504 break;
507 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
508 dprintf("res_search failed (%d)\n", n);
509 if (errno == ECONNREFUSED)
510 return (_gethtbyname2(name, af));
511 return (NULL);
513 return (getanswer(&buf, n, name, type));
516 struct hostent *
517 gethostbyaddr(addr, len, af)
518 const char *addr; /* XXX should have been def'd as u_char! */
519 int len, af;
521 const u_char *uaddr = (const u_char *)addr;
522 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
523 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
524 int n, size;
525 querybuf buf;
526 register struct hostent *hp;
527 char qbuf[MAXDNAME+1], *qp;
528 #ifdef SUNSECURITY
529 register struct hostent *rhp;
530 char **haddr;
531 u_long old_options;
532 char hname2[MAXDNAME+1];
533 #endif /*SUNSECURITY*/
534 extern struct hostent *_gethtbyaddr();
536 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
537 h_errno = NETDB_INTERNAL;
538 return (NULL);
540 if (af == AF_INET6 && len == IN6ADDRSZ &&
541 (!bcmp(uaddr, mapped, sizeof mapped) ||
542 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
543 /* Unmap. */
544 addr += sizeof mapped;
545 uaddr += sizeof mapped;
546 af = AF_INET;
547 len = INADDRSZ;
549 switch (af) {
550 case AF_INET:
551 size = INADDRSZ;
552 break;
553 case AF_INET6:
554 size = IN6ADDRSZ;
555 break;
556 default:
557 errno = EAFNOSUPPORT;
558 h_errno = NETDB_INTERNAL;
559 return (NULL);
561 if (size != len) {
562 errno = EINVAL;
563 h_errno = NETDB_INTERNAL;
564 return (NULL);
566 switch (af) {
567 case AF_INET:
568 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
569 (uaddr[3] & 0xff),
570 (uaddr[2] & 0xff),
571 (uaddr[1] & 0xff),
572 (uaddr[0] & 0xff));
573 break;
574 case AF_INET6:
575 qp = qbuf;
576 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
577 qp += SPRINTF((qp, "%x.%x.",
578 uaddr[n] & 0xf,
579 (uaddr[n] >> 4) & 0xf));
581 strcpy(qp, "ip6.int");
582 break;
583 default:
584 abort();
586 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
587 if (n < 0) {
588 dprintf("res_query failed (%d)\n", n);
589 if (errno == ECONNREFUSED)
590 return (_gethtbyaddr(addr, len, af));
591 return (NULL);
593 if (!(hp = getanswer(&buf, n, qbuf, T_PTR)))
594 return (NULL); /* h_errno was set by getanswer() */
595 #ifdef SUNSECURITY
596 if (af == AF_INET) {
598 * turn off search as the name should be absolute,
599 * 'localhost' should be matched by defnames
601 strncpy(hname2, hp->h_name, MAXDNAME);
602 hname2[MAXDNAME] = '\0';
603 old_options = _res.options;
604 _res.options &= ~RES_DNSRCH;
605 _res.options |= RES_DEFNAMES;
606 if (!(rhp = gethostbyname(hname2))) {
607 syslog(LOG_NOTICE|LOG_AUTH,
608 "gethostbyaddr: No A record for %s (verifying [%s])",
609 hname2, inet_ntoa(*((struct in_addr *)addr)));
610 _res.options = old_options;
611 h_errno = HOST_NOT_FOUND;
612 return (NULL);
614 _res.options = old_options;
615 for (haddr = rhp->h_addr_list; *haddr; haddr++)
616 if (!memcmp(*haddr, addr, INADDRSZ))
617 break;
618 if (!*haddr) {
619 syslog(LOG_NOTICE|LOG_AUTH,
620 "gethostbyaddr: A record of %s != PTR record [%s]",
621 hname2, inet_ntoa(*((struct in_addr *)addr)));
622 h_errno = HOST_NOT_FOUND;
623 return (NULL);
626 #endif /*SUNSECURITY*/
627 hp->h_addrtype = af;
628 hp->h_length = len;
629 bcopy(addr, host_addr, len);
630 h_addr_ptrs[0] = (char *)host_addr;
631 h_addr_ptrs[1] = NULL;
632 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
633 map_v4v6_address((char*)host_addr, (char*)host_addr);
634 hp->h_addrtype = AF_INET6;
635 hp->h_length = IN6ADDRSZ;
637 h_errno = NETDB_SUCCESS;
638 return (hp);
641 void
642 _sethtent(f)
643 int f;
645 if (!hostf)
646 hostf = fopen(_PATH_HOSTS, "r" );
647 else
648 rewind(hostf);
649 stayopen = f;
652 void
653 _endhtent()
655 if (hostf && !stayopen) {
656 (void) fclose(hostf);
657 hostf = NULL;
661 struct hostent *
662 _gethtent()
664 char *p;
665 register char *cp, **q;
666 int af, len;
668 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
669 h_errno = NETDB_INTERNAL;
670 return (NULL);
672 again:
673 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
674 h_errno = HOST_NOT_FOUND;
675 return (NULL);
677 if (*p == '#')
678 goto again;
679 if (!(cp = strpbrk(p, "#\n")))
680 goto again;
681 *cp = '\0';
682 if (!(cp = strpbrk(p, " \t")))
683 goto again;
684 *cp++ = '\0';
685 if ((_res.options & RES_USE_INET6) &&
686 inet_pton(AF_INET6, p, host_addr) > 0) {
687 af = AF_INET6;
688 len = IN6ADDRSZ;
689 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
690 if (_res.options & RES_USE_INET6) {
691 map_v4v6_address((char*)host_addr, (char*)host_addr);
692 af = AF_INET6;
693 len = IN6ADDRSZ;
694 } else {
695 af = AF_INET;
696 len = INADDRSZ;
698 } else {
699 goto again;
701 h_addr_ptrs[0] = (char *)host_addr;
702 h_addr_ptrs[1] = NULL;
703 host.h_addr_list = h_addr_ptrs;
704 host.h_length = len;
705 host.h_addrtype = af;
706 while (*cp == ' ' || *cp == '\t')
707 cp++;
708 host.h_name = cp;
709 q = host.h_aliases = host_aliases;
710 if (cp = strpbrk(cp, " \t"))
711 *cp++ = '\0';
712 while (cp && *cp) {
713 if (*cp == ' ' || *cp == '\t') {
714 cp++;
715 continue;
717 if (q < &host_aliases[MAXALIASES - 1])
718 *q++ = cp;
719 if (cp = strpbrk(cp, " \t"))
720 *cp++ = '\0';
722 *q = NULL;
723 if (_res.options & RES_USE_INET6) {
724 char *bp = hostbuf;
725 int buflen = sizeof hostbuf;
727 map_v4v6_hostent(&host, &bp, &buflen);
729 h_errno = NETDB_SUCCESS;
730 return (&host);
733 struct hostent *
734 _gethtbyname(name)
735 const char *name;
737 extern struct hostent *_gethtbyname2();
738 struct hostent *hp;
740 if (_res.options & RES_USE_INET6) {
741 hp = _gethtbyname2(name, AF_INET6);
742 if (hp)
743 return (hp);
745 return (_gethtbyname2(name, AF_INET));
748 struct hostent *
749 _gethtbyname2(name, af)
750 const char *name;
751 int af;
753 register struct hostent *p;
754 register char **cp;
756 _sethtent(0);
757 while (p = _gethtent()) {
758 if (p->h_addrtype != af)
759 continue;
760 if (strcasecmp(p->h_name, name) == 0)
761 break;
762 for (cp = p->h_aliases; *cp != 0; cp++)
763 if (strcasecmp(*cp, name) == 0)
764 goto found;
766 found:
767 _endhtent();
768 return (p);
771 struct hostent *
772 _gethtbyaddr(addr, len, af)
773 const char *addr;
774 int len, af;
776 register struct hostent *p;
778 _sethtent(0);
779 while (p = _gethtent())
780 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
781 break;
782 _endhtent();
783 return (p);
786 static void
787 map_v4v6_address(src, dst)
788 const char *src;
789 char *dst;
791 u_char *p = (u_char *)dst;
792 char tmp[INADDRSZ];
793 int i;
795 /* Stash a temporary copy so our caller can update in place. */
796 bcopy(src, tmp, INADDRSZ);
797 /* Mark this ipv6 addr as a mapped ipv4. */
798 for (i = 0; i < 10; i++)
799 *p++ = 0x00;
800 *p++ = 0xff;
801 *p++ = 0xff;
802 /* Retrieve the saved copy and we're done. */
803 bcopy(tmp, (void*)p, INADDRSZ);
806 static void
807 map_v4v6_hostent(hp, bpp, lenp)
808 struct hostent *hp;
809 char **bpp;
810 int *lenp;
812 char **ap;
814 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
815 return;
816 hp->h_addrtype = AF_INET6;
817 hp->h_length = IN6ADDRSZ;
818 for (ap = hp->h_addr_list; *ap; ap++) {
819 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
821 if (*lenp < (i + IN6ADDRSZ)) {
822 /* Out of memory. Truncate address list here. XXX */
823 *ap = NULL;
824 return;
826 *bpp += i;
827 *lenp -= i;
828 map_v4v6_address(*ap, *bpp);
829 *ap = *bpp;
830 *bpp += IN6ADDRSZ;
831 *lenp -= IN6ADDRSZ;
835 #ifdef RESOLVSORT
836 static void
837 addrsort(ap, num)
838 char **ap;
839 int num;
841 int i, j;
842 char **p;
843 short aval[MAXADDRS];
844 int needsort = 0;
846 p = ap;
847 for (i = 0; i < num; i++, p++) {
848 for (j = 0 ; (unsigned)j < _res.nsort; j++)
849 if (_res.sort_list[j].addr.s_addr ==
850 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
851 break;
852 aval[i] = j;
853 if (needsort == 0 && i > 0 && j < aval[i-1])
854 needsort = i;
856 if (!needsort)
857 return;
859 while (needsort < num) {
860 for (j = needsort - 1; j >= 0; j--) {
861 if (aval[j] > aval[j+1]) {
862 char *hp;
864 i = aval[j];
865 aval[j] = aval[j+1];
866 aval[j+1] = i;
868 hp = ap[j];
869 ap[j] = ap[j+1];
870 ap[j+1] = hp;
872 } else
873 break;
875 needsort++;
878 #endif
880 #if defined(BSD43_BSD43_NFS) || defined(sun)
881 /* some libc's out there are bound internally to these names (UMIPS) */
882 void
883 ht_sethostent(stayopen)
884 int stayopen;
886 _sethtent(stayopen);
889 void
890 ht_endhostent()
892 _endhtent();
895 struct hostent *
896 ht_gethostbyname(name)
897 char *name;
899 return (_gethtbyname(name));
902 struct hostent *
903 ht_gethostbyaddr(addr, len, af)
904 const char *addr;
905 int len, af;
907 return (_gethtbyaddr(addr, len, af));
910 struct hostent *
911 gethostent()
913 return (_gethtent());
916 void
917 dns_service()
919 return;
922 #undef dn_skipname
923 dn_skipname(comp_dn, eom)
924 const u_char *comp_dn, *eom;
926 return (__dn_skipname(comp_dn, eom));
928 #endif /*old-style libc with yp junk in it*/