gethnamaddr.c revision 1.1 1 /*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 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.
22 *
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.
36 *
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.
43 *
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--
54 */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
59 #endif /* LIBC_SCCS and not lint */
60
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>
67
68 #include <stdio.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <errno.h>
73 #include <syslog.h>
74
75 #ifndef LOG_AUTH
76 # define LOG_AUTH 0
77 #endif
78
79 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
80
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
87
88 #if defined(USE_OPTIONS_H)
89 # include <../conf/options.h>
90 #endif
91
92 #ifdef SPRINTF_CHAR
93 # define SPRINTF(x) strlen(sprintf/**/x)
94 #else
95 # define SPRINTF(x) ((size_t)sprintf x)
96 #endif
97
98 #define MAXALIASES 35
99 #define MAXADDRS 35
100
101 static const char AskedForGot[] =
102 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
103
104 static char *h_addr_ptrs[MAXADDRS + 1];
105
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;
112
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));
115
116 #ifdef RESOLVSORT
117 static void addrsort __P((char **, int));
118 #endif
119
120 #if PACKETSZ > 1024
121 #define MAXPACKET PACKETSZ
122 #else
123 #define MAXPACKET 1024
124 #endif
125
126 typedef union {
127 HEADER hdr;
128 u_char buf[MAXPACKET];
129 } querybuf;
130
131 typedef union {
132 int32_t al;
133 char ac;
134 } align;
135
136 extern int h_errno;
137
138 #ifdef DEBUG
139 static void
140 dprintf(msg, num)
141 char *msg;
142 int num;
143 {
144 if (_res.options & RES_DEBUG) {
145 int save = errno;
146
147 printf(msg, num);
148 errno = save;
149 }
150 }
151 #else
152 # define dprintf(msg, num) /*nada*/
153 #endif
154
155 static struct hostent *
156 getanswer(answer, anslen, qname, qtype)
157 const querybuf *answer;
158 int anslen;
159 const char *qname;
160 int qtype;
161 {
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];
171 const char *tname;
172 int (*name_ok) __P((const char *));
173
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(); */
187 }
188 /*
189 * find first satisfactory answer
190 */
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);
200 }
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);
205 }
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).
211 */
212 n = strlen(bp) + 1; /* for the \0 */
213 if (n >= MAXHOSTNAMELEN) {
214 h_errno = NO_RECOVERY;
215 return (NULL);
216 }
217 host.h_name = bp;
218 bp += n;
219 buflen -= n;
220 /* The qname can be abbreviated, but h_name is now absolute. */
221 qname = host.h_name;
222 }
223 ap = host_aliases;
224 *ap = NULL;
225 host.h_aliases = host_aliases;
226 hap = h_addr_ptrs;
227 *hap = NULL;
228 host.h_addr_list = h_addr_ptrs;
229 haveanswer = 0;
230 had_error = 0;
231 while (ancount-- > 0 && cp < eom && !had_error) {
232 n = dn_expand(answer->buf, eom, cp, bp, buflen);
233 if ((n < 0) || !(*name_ok)(bp)) {
234 had_error++;
235 continue;
236 }
237 cp += n; /* name */
238 type = _getshort(cp);
239 cp += INT16SZ; /* type */
240 class = _getshort(cp);
241 cp += INT16SZ + INT32SZ; /* class, TTL */
242 n = _getshort(cp);
243 cp += INT16SZ; /* len */
244 if (class != C_IN) {
245 /* XXX - debug? syslog? */
246 cp += n;
247 continue; /* XXX - had_error++ ? */
248 }
249 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
250 if (ap >= &host_aliases[MAXALIASES-1])
251 continue;
252 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
253 if ((n < 0) || !(*name_ok)(tbuf)) {
254 had_error++;
255 continue;
256 }
257 cp += n;
258 /* Store alias. */
259 *ap++ = bp;
260 n = strlen(bp) + 1; /* for the \0 */
261 if (n >= MAXHOSTNAMELEN) {
262 had_error++;
263 continue;
264 }
265 bp += n;
266 buflen -= n;
267 /* Get canonical name. */
268 n = strlen(tbuf) + 1; /* for the \0 */
269 if (n > buflen || n >= MAXHOSTNAMELEN) {
270 had_error++;
271 continue;
272 }
273 strcpy(bp, tbuf);
274 host.h_name = bp;
275 bp += n;
276 buflen -= n;
277 continue;
278 }
279 if (qtype == T_PTR && type == T_CNAME) {
280 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
281 if (n < 0 || !res_dnok(tbuf)) {
282 had_error++;
283 continue;
284 }
285 cp += n;
286 /* Get canonical name. */
287 n = strlen(tbuf) + 1; /* for the \0 */
288 if (n > buflen || n >= MAXHOSTNAMELEN) {
289 had_error++;
290 continue;
291 }
292 strcpy(bp, tbuf);
293 tname = bp;
294 bp += n;
295 buflen -= n;
296 continue;
297 }
298 if (type != qtype) {
299 syslog(LOG_NOTICE|LOG_AUTH,
300 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
301 qname, p_class(C_IN), p_type(qtype),
302 p_type(type));
303 cp += n;
304 continue; /* XXX - had_error++ ? */
305 }
306 switch (type) {
307 case T_PTR:
308 if (strcasecmp(tname, bp) != 0) {
309 syslog(LOG_NOTICE|LOG_AUTH,
310 AskedForGot, qname, bp);
311 cp += n;
312 continue; /* XXX - had_error++ ? */
313 }
314 n = dn_expand(answer->buf, eom, cp, bp, buflen);
315 if ((n < 0) || !res_hnok(bp)) {
316 had_error++;
317 break;
318 }
319 #if MULTI_PTRS_ARE_ALIASES
320 cp += n;
321 if (!haveanswer)
322 host.h_name = bp;
323 else if (ap < &host_aliases[MAXALIASES-1])
324 *ap++ = bp;
325 else
326 n = -1;
327 if (n != -1) {
328 n = strlen(bp) + 1; /* for the \0 */
329 if (n >= MAXHOSTNAMELEN) {
330 had_error++;
331 break;
332 }
333 bp += n;
334 buflen -= n;
335 }
336 break;
337 #else
338 host.h_name = bp;
339 if (_res.options & RES_USE_INET6) {
340 n = strlen(bp) + 1; /* for the \0 */
341 if (n >= MAXHOSTNAMELEN) {
342 had_error++;
343 break;
344 }
345 bp += n;
346 buflen -= n;
347 map_v4v6_hostent(&host, &bp, &buflen);
348 }
349 h_errno = NETDB_SUCCESS;
350 return (&host);
351 #endif
352 case T_A:
353 case T_AAAA:
354 if (strcasecmp(host.h_name, bp) != 0) {
355 syslog(LOG_NOTICE|LOG_AUTH,
356 AskedForGot, host.h_name, bp);
357 cp += n;
358 continue; /* XXX - had_error++ ? */
359 }
360 if (n != host.h_length) {
361 cp += n;
362 continue;
363 }
364 if (!haveanswer) {
365 register int nn;
366
367 host.h_name = bp;
368 nn = strlen(bp) + 1; /* for the \0 */
369 bp += nn;
370 buflen -= nn;
371 }
372
373 bp += sizeof(align) - ((u_long)bp % sizeof(align));
374
375 if (bp + n >= &hostbuf[sizeof hostbuf]) {
376 dprintf("size (%d) too big\n", n);
377 had_error++;
378 continue;
379 }
380 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
381 if (!toobig++)
382 dprintf("Too many addresses (%d)\n",
383 MAXADDRS);
384 cp += n;
385 continue;
386 }
387 bcopy(cp, *hap++ = bp, n);
388 bp += n;
389 buflen -= n;
390 cp += n;
391 break;
392 default:
393 abort();
394 }
395 if (!had_error)
396 haveanswer++;
397 }
398 if (haveanswer) {
399 *ap = NULL;
400 *hap = NULL;
401 # if defined(RESOLVSORT)
402 /*
403 * Note: we sort even if host can take only one address
404 * in its return structures - should give it the "best"
405 * address in that case, not some random one
406 */
407 if (_res.nsort && haveanswer > 1 && qtype == T_A)
408 addrsort(h_addr_ptrs, haveanswer);
409 # endif /*RESOLVSORT*/
410 if (!host.h_name) {
411 n = strlen(qname) + 1; /* for the \0 */
412 if (n > buflen || n >= MAXHOSTNAMELEN)
413 goto no_recovery;
414 strcpy(bp, qname);
415 host.h_name = bp;
416 bp += n;
417 buflen -= n;
418 }
419 if (_res.options & RES_USE_INET6)
420 map_v4v6_hostent(&host, &bp, &buflen);
421 h_errno = NETDB_SUCCESS;
422 return (&host);
423 }
424 no_recovery:
425 h_errno = NO_RECOVERY;
426 return (NULL);
427 }
428
429 struct hostent *
430 gethostbyname(name)
431 const char *name;
432 {
433 struct hostent *hp;
434
435 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
436 h_errno = NETDB_INTERNAL;
437 return (NULL);
438 }
439 if (_res.options & RES_USE_INET6) {
440 hp = gethostbyname2(name, AF_INET6);
441 if (hp)
442 return (hp);
443 }
444 return (gethostbyname2(name, AF_INET));
445 }
446
447 struct hostent *
448 gethostbyname2(name, af)
449 const char *name;
450 int af;
451 {
452 querybuf buf;
453 register const char *cp;
454 char *bp;
455 int n, size, type, len;
456 extern struct hostent *_gethtbyname2();
457
458 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
459 h_errno = NETDB_INTERNAL;
460 return (NULL);
461 }
462
463 switch (af) {
464 case AF_INET:
465 size = INADDRSZ;
466 type = T_A;
467 break;
468 case AF_INET6:
469 size = IN6ADDRSZ;
470 type = T_AAAA;
471 break;
472 default:
473 h_errno = NETDB_INTERNAL;
474 errno = EAFNOSUPPORT;
475 return (NULL);
476 }
477
478 host.h_addrtype = af;
479 host.h_length = size;
480
481 /*
482 * if there aren't any dots, it could be a user-level alias.
483 * this is also done in res_query() since we are not the only
484 * function that looks up host names.
485 */
486 if (!strchr(name, '.') && (cp = __hostalias(name)))
487 name = cp;
488
489 /*
490 * disallow names consisting only of digits/dots, unless
491 * they end in a dot.
492 */
493 if (isdigit(name[0]))
494 for (cp = name;; ++cp) {
495 if (!*cp) {
496 if (*--cp == '.')
497 break;
498 /*
499 * All-numeric, no dot at the end.
500 * Fake up a hostent as if we'd actually
501 * done a lookup.
502 */
503 if (inet_pton(af, name, host_addr) <= 0) {
504 h_errno = HOST_NOT_FOUND;
505 return (NULL);
506 }
507 strncpy(hostbuf, name, MAXDNAME);
508 hostbuf[MAXDNAME] = '\0';
509 bp = hostbuf + MAXDNAME;
510 len = sizeof hostbuf - MAXDNAME;
511 host.h_name = hostbuf;
512 host.h_aliases = host_aliases;
513 host_aliases[0] = NULL;
514 h_addr_ptrs[0] = (char *)host_addr;
515 h_addr_ptrs[1] = NULL;
516 host.h_addr_list = h_addr_ptrs;
517 if (_res.options & RES_USE_INET6)
518 map_v4v6_hostent(&host, &bp, &len);
519 h_errno = NETDB_SUCCESS;
520 return (&host);
521 }
522 if (!isdigit(*cp) && *cp != '.')
523 break;
524 }
525 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
526 name[0] == ':')
527 for (cp = name;; ++cp) {
528 if (!*cp) {
529 if (*--cp == '.')
530 break;
531 /*
532 * All-IPv6-legal, no dot at the end.
533 * Fake up a hostent as if we'd actually
534 * done a lookup.
535 */
536 if (inet_pton(af, name, host_addr) <= 0) {
537 h_errno = HOST_NOT_FOUND;
538 return (NULL);
539 }
540 strncpy(hostbuf, name, MAXDNAME);
541 hostbuf[MAXDNAME] = '\0';
542 bp = hostbuf + MAXDNAME;
543 len = sizeof hostbuf - MAXDNAME;
544 host.h_name = hostbuf;
545 host.h_aliases = host_aliases;
546 host_aliases[0] = NULL;
547 h_addr_ptrs[0] = (char *)host_addr;
548 h_addr_ptrs[1] = NULL;
549 host.h_addr_list = h_addr_ptrs;
550 h_errno = NETDB_SUCCESS;
551 return (&host);
552 }
553 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
554 break;
555 }
556
557 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
558 dprintf("res_search failed (%d)\n", n);
559 if (errno == ECONNREFUSED)
560 return (_gethtbyname2(name, af));
561 return (NULL);
562 }
563 return (getanswer(&buf, n, name, type));
564 }
565
566 struct hostent *
567 gethostbyaddr(addr, len, af)
568 const char *addr; /* XXX should have been def'd as u_char! */
569 int len, af;
570 {
571 const u_char *uaddr = (const u_char *)addr;
572 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
573 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
574 int n, size;
575 querybuf buf;
576 register struct hostent *hp;
577 char qbuf[MAXDNAME+1], *qp;
578 #ifdef SUNSECURITY
579 register struct hostent *rhp;
580 char **haddr;
581 u_long old_options;
582 char hname2[MAXDNAME+1];
583 #endif /*SUNSECURITY*/
584 extern struct hostent *_gethtbyaddr();
585
586 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
587 h_errno = NETDB_INTERNAL;
588 return (NULL);
589 }
590 if (af == AF_INET6 && len == IN6ADDRSZ &&
591 (!bcmp(uaddr, mapped, sizeof mapped) ||
592 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
593 /* Unmap. */
594 addr += sizeof mapped;
595 uaddr += sizeof mapped;
596 af = AF_INET;
597 len = INADDRSZ;
598 }
599 switch (af) {
600 case AF_INET:
601 size = INADDRSZ;
602 break;
603 case AF_INET6:
604 size = IN6ADDRSZ;
605 break;
606 default:
607 errno = EAFNOSUPPORT;
608 h_errno = NETDB_INTERNAL;
609 return (NULL);
610 }
611 if (size != len) {
612 errno = EINVAL;
613 h_errno = NETDB_INTERNAL;
614 return (NULL);
615 }
616 switch (af) {
617 case AF_INET:
618 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
619 (uaddr[3] & 0xff),
620 (uaddr[2] & 0xff),
621 (uaddr[1] & 0xff),
622 (uaddr[0] & 0xff));
623 break;
624 case AF_INET6:
625 qp = qbuf;
626 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
627 qp += SPRINTF((qp, "%x.%x.",
628 uaddr[n] & 0xf,
629 (uaddr[n] >> 4) & 0xf));
630 }
631 strcpy(qp, "ip6.int");
632 break;
633 default:
634 abort();
635 }
636 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
637 if (n < 0) {
638 dprintf("res_query failed (%d)\n", n);
639 if (errno == ECONNREFUSED)
640 return (_gethtbyaddr(addr, len, af));
641 return (NULL);
642 }
643 if (!(hp = getanswer(&buf, n, qbuf, T_PTR)))
644 return (NULL); /* h_errno was set by getanswer() */
645 #ifdef SUNSECURITY
646 if (af == AF_INET) {
647 /*
648 * turn off search as the name should be absolute,
649 * 'localhost' should be matched by defnames
650 */
651 strncpy(hname2, hp->h_name, MAXDNAME);
652 hname2[MAXDNAME] = '\0';
653 old_options = _res.options;
654 _res.options &= ~RES_DNSRCH;
655 _res.options |= RES_DEFNAMES;
656 if (!(rhp = gethostbyname(hname2))) {
657 syslog(LOG_NOTICE|LOG_AUTH,
658 "gethostbyaddr: No A record for %s (verifying [%s])",
659 hname2, inet_ntoa(*((struct in_addr *)addr)));
660 _res.options = old_options;
661 h_errno = HOST_NOT_FOUND;
662 return (NULL);
663 }
664 _res.options = old_options;
665 for (haddr = rhp->h_addr_list; *haddr; haddr++)
666 if (!memcmp(*haddr, addr, INADDRSZ))
667 break;
668 if (!*haddr) {
669 syslog(LOG_NOTICE|LOG_AUTH,
670 "gethostbyaddr: A record of %s != PTR record [%s]",
671 hname2, inet_ntoa(*((struct in_addr *)addr)));
672 h_errno = HOST_NOT_FOUND;
673 return (NULL);
674 }
675 }
676 #endif /*SUNSECURITY*/
677 hp->h_addrtype = af;
678 hp->h_length = len;
679 bcopy(addr, host_addr, len);
680 h_addr_ptrs[0] = (char *)host_addr;
681 h_addr_ptrs[1] = NULL;
682 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
683 map_v4v6_address((char*)host_addr, (char*)host_addr);
684 hp->h_addrtype = AF_INET6;
685 hp->h_length = IN6ADDRSZ;
686 }
687 h_errno = NETDB_SUCCESS;
688 return (hp);
689 }
690
691 void
692 _sethtent(f)
693 int f;
694 {
695 if (!hostf)
696 hostf = fopen(_PATH_HOSTS, "r" );
697 else
698 rewind(hostf);
699 stayopen = f;
700 }
701
702 void
703 _endhtent()
704 {
705 if (hostf && !stayopen) {
706 (void) fclose(hostf);
707 hostf = NULL;
708 }
709 }
710
711 struct hostent *
712 _gethtent()
713 {
714 char *p;
715 register char *cp, **q;
716 int af, len;
717
718 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
719 h_errno = NETDB_INTERNAL;
720 return (NULL);
721 }
722 again:
723 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
724 h_errno = HOST_NOT_FOUND;
725 return (NULL);
726 }
727 if (*p == '#')
728 goto again;
729 if (!(cp = strpbrk(p, "#\n")))
730 goto again;
731 *cp = '\0';
732 if (!(cp = strpbrk(p, " \t")))
733 goto again;
734 *cp++ = '\0';
735 if (inet_pton(AF_INET6, p, host_addr) > 0) {
736 af = AF_INET6;
737 len = IN6ADDRSZ;
738 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
739 if (_res.options & RES_USE_INET6) {
740 map_v4v6_address((char*)host_addr, (char*)host_addr);
741 af = AF_INET6;
742 len = IN6ADDRSZ;
743 } else {
744 af = AF_INET;
745 len = INADDRSZ;
746 }
747 } else {
748 goto again;
749 }
750 h_addr_ptrs[0] = (char *)host_addr;
751 h_addr_ptrs[1] = NULL;
752 host.h_addr_list = h_addr_ptrs;
753 host.h_length = len;
754 host.h_addrtype = af;
755 while (*cp == ' ' || *cp == '\t')
756 cp++;
757 host.h_name = cp;
758 q = host.h_aliases = host_aliases;
759 if (cp = strpbrk(cp, " \t"))
760 *cp++ = '\0';
761 while (cp && *cp) {
762 if (*cp == ' ' || *cp == '\t') {
763 cp++;
764 continue;
765 }
766 if (q < &host_aliases[MAXALIASES - 1])
767 *q++ = cp;
768 if (cp = strpbrk(cp, " \t"))
769 *cp++ = '\0';
770 }
771 *q = NULL;
772 h_errno = NETDB_SUCCESS;
773 return (&host);
774 }
775
776 struct hostent *
777 _gethtbyname(name)
778 const char *name;
779 {
780 extern struct hostent *_gethtbyname2();
781 struct hostent *hp;
782
783 if (_res.options & RES_USE_INET6) {
784 hp = _gethtbyname2(name, AF_INET6);
785 if (hp)
786 return (hp);
787 }
788 return (_gethtbyname2(name, AF_INET));
789 }
790
791 struct hostent *
792 _gethtbyname2(name, af)
793 const char *name;
794 int af;
795 {
796 register struct hostent *p;
797 register char **cp;
798
799 _sethtent(0);
800 while (p = _gethtent()) {
801 if (p->h_addrtype != af)
802 continue;
803 if (strcasecmp(p->h_name, name) == 0)
804 break;
805 for (cp = p->h_aliases; *cp != 0; cp++)
806 if (strcasecmp(*cp, name) == 0)
807 goto found;
808 }
809 found:
810 _endhtent();
811 return (p);
812 }
813
814 struct hostent *
815 _gethtbyaddr(addr, len, af)
816 const char *addr;
817 int len, af;
818 {
819 register struct hostent *p;
820
821 _sethtent(0);
822 while (p = _gethtent())
823 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
824 break;
825 _endhtent();
826 return (p);
827 }
828
829 static void
830 map_v4v6_address(src, dst)
831 const char *src;
832 char *dst;
833 {
834 u_char *p = (u_char *)dst;
835 char tmp[INADDRSZ];
836 int i;
837
838 /* Stash a temporary copy so our caller can update in place. */
839 bcopy(src, tmp, INADDRSZ);
840 /* Mark this ipv6 addr as a mapped ipv4. */
841 for (i = 0; i < 10; i++)
842 *p++ = 0x00;
843 *p++ = 0xff;
844 *p++ = 0xff;
845 /* Retrieve the saved copy and we're done. */
846 bcopy(tmp, (void*)p, INADDRSZ);
847 }
848
849 static void
850 map_v4v6_hostent(hp, bpp, lenp)
851 struct hostent *hp;
852 char **bpp;
853 int *lenp;
854 {
855 char **ap;
856
857 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
858 return;
859 hp->h_addrtype = AF_INET6;
860 hp->h_length = IN6ADDRSZ;
861 for (ap = hp->h_addr_list; *ap; ap++) {
862 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
863
864 if (*lenp < (i + IN6ADDRSZ)) {
865 /* Out of memory. Truncate address list here. XXX */
866 *ap = NULL;
867 return;
868 }
869 *bpp += i;
870 *lenp -= i;
871 map_v4v6_address(*ap, *bpp);
872 *ap = *bpp;
873 *bpp += IN6ADDRSZ;
874 *lenp -= IN6ADDRSZ;
875 }
876 }
877
878 #ifdef RESOLVSORT
879 static void
880 addrsort(ap, num)
881 char **ap;
882 int num;
883 {
884 int i, j;
885 char **p;
886 short aval[MAXADDRS];
887 int needsort = 0;
888
889 p = ap;
890 for (i = 0; i < num; i++, p++) {
891 for (j = 0 ; (unsigned)j < _res.nsort; j++)
892 if (_res.sort_list[j].addr.s_addr ==
893 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
894 break;
895 aval[i] = j;
896 if (needsort == 0 && i > 0 && j < aval[i-1])
897 needsort = i;
898 }
899 if (!needsort)
900 return;
901
902 while (needsort < num) {
903 for (j = needsort - 1; j >= 0; j--) {
904 if (aval[j] > aval[j+1]) {
905 char *hp;
906
907 i = aval[j];
908 aval[j] = aval[j+1];
909 aval[j+1] = i;
910
911 hp = ap[j];
912 ap[j] = ap[j+1];
913 ap[j+1] = hp;
914
915 } else
916 break;
917 }
918 needsort++;
919 }
920 }
921 #endif
922
923 #if defined(BSD43_BSD43_NFS) || defined(sun)
924 /* some libc's out there are bound internally to these names (UMIPS) */
925 void
926 ht_sethostent(stayopen)
927 int stayopen;
928 {
929 _sethtent(stayopen);
930 }
931
932 void
933 ht_endhostent()
934 {
935 _endhtent();
936 }
937
938 struct hostent *
939 ht_gethostbyname(name)
940 char *name;
941 {
942 return (_gethtbyname(name));
943 }
944
945 struct hostent *
946 ht_gethostbyaddr(addr, len, af)
947 const char *addr;
948 int len, af;
949 {
950 return (_gethtbyaddr(addr, len, af));
951 }
952
953 struct hostent *
954 gethostent()
955 {
956 return (_gethtent());
957 }
958
959 void
960 dns_service()
961 {
962 return;
963 }
964
965 #undef dn_skipname
966 dn_skipname(comp_dn, eom)
967 const u_char *comp_dn, *eom;
968 {
969 return (__dn_skipname(comp_dn, eom));
970 }
971 #endif /*old-style libc with yp junk in it*/
972