gethnamaddr.c revision 1.5 1 /* $NetBSD: gethnamaddr.c,v 1.5 1998/10/13 20:54:02 kleink Exp $ */
2
3 /*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 * -
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 * -
55 * --Copyright--
56 */
57
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 #if 0
61 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
62 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
63 #else
64 __RCSID("$NetBSD: gethnamaddr.c,v 1.5 1998/10/13 20:54:02 kleink Exp $");
65 #endif
66 #endif /* LIBC_SCCS and not lint */
67
68 #include "namespace.h"
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/socket.h>
72 #include <netinet/in.h>
73 #include <arpa/inet.h>
74 #include <arpa/nameser.h>
75
76 #include <stdio.h>
77 #include <netdb.h>
78 #include <resolv.h>
79 #include <ctype.h>
80 #include <errno.h>
81 #include <syslog.h>
82
83 #ifndef LOG_AUTH
84 # define LOG_AUTH 0
85 #endif
86
87 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
88
89 #include <stdlib.h>
90 #include <string.h>
91
92 #ifdef YP
93 #include <rpc/rpc.h>
94 #include <rpcsvc/yp_prot.h>
95 #include <rpcsvc/ypclnt.h>
96 #endif
97
98 #ifdef __weak_alias
99 __weak_alias(gethostbyaddr,_gethostbyaddr);
100 __weak_alias(gethostbyname,_gethostbyname);
101 #endif
102
103 #define MAXALIASES 35
104 #define MAXADDRS 35
105
106 static const char AskedForGot[] =
107 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
108
109 static char *h_addr_ptrs[MAXADDRS + 1];
110
111 #ifdef YP
112 static char *__ypdomain;
113 #endif
114
115 static struct hostent host;
116 static char *host_aliases[MAXALIASES];
117 static char hostbuf[8*1024];
118 static u_char host_addr[16]; /* IPv4 or IPv6 */
119 static FILE *hostf = NULL;
120 static int stayopen = 0;
121
122
123 #if PACKETSZ > 1024
124 #define MAXPACKET PACKETSZ
125 #else
126 #define MAXPACKET 1024
127 #endif
128
129 typedef union {
130 HEADER hdr;
131 u_char buf[MAXPACKET];
132 } querybuf;
133
134 typedef union {
135 int32_t al;
136 char ac;
137 } align;
138
139 extern int h_errno;
140
141 #ifdef DEBUG
142 static void dprintf __P((char *, int));
143 #endif
144 static struct hostent *getanswer __P((const querybuf *, int,
145 const char *, int));
146 static void map_v4v6_address __P((const char *, char *));
147 static void map_v4v6_hostent __P((struct hostent *, char **, int *));
148 #ifdef RESOLVSORT
149 static void addrsort __P((char **, int));
150 #endif
151
152 struct hostent *gethostbyname __P((const char *));
153 struct hostent *gethostbyname2 __P((const char *, int));
154 struct hostent *gethostbyaddr __P((const char *, int, int ));
155 void _sethtent __P((int));
156 void _endhtent __P((void));
157 struct hostent *_gethtent __P((void));
158 struct hostent *_gethtbyname __P((const char *));
159 struct hostent *_gethtbyname2 __P((const char *, int));
160 struct hostent *_gethtbyaddr __P((const char *, int, int ));
161 void ht_sethostent __P((int));
162 void ht_endhostent __P((void));
163 struct hostent *ht_gethostbyname __P((char *));
164 struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
165 struct hostent *gethostent __P((void));
166 void dns_service __P((void));
167 int dn_skipname __P((const u_char *, const u_char *));
168 #ifdef YP
169 struct hostent *_yphostent __P((char *));
170 struct hostent *_yp_gethtbyaddr __P((const char *, int, int ));
171 struct hostent *_yp_gethtbyname __P((const char *));
172 #endif
173
174
175 #ifdef DEBUG
176 static void
177 dprintf(msg, num)
178 char *msg;
179 int num;
180 {
181 if (_res.options & RES_DEBUG) {
182 int save = errno;
183
184 printf(msg, num);
185 errno = save;
186 }
187 }
188 #else
189 # define dprintf(msg, num) /*nada*/
190 #endif
191
192 static struct hostent *
193 getanswer(answer, anslen, qname, qtype)
194 const querybuf *answer;
195 int anslen;
196 const char *qname;
197 int qtype;
198 {
199 register const HEADER *hp;
200 register const u_char *cp;
201 register int n;
202 const u_char *eom;
203 char *bp, **ap, **hap;
204 int type, class, buflen, ancount, qdcount;
205 int haveanswer, had_error;
206 int toobig = 0;
207 char tbuf[MAXDNAME];
208 const char *tname;
209 int (*name_ok) __P((const char *));
210
211 tname = qname;
212 host.h_name = NULL;
213 eom = answer->buf + anslen;
214 switch (qtype) {
215 case T_A:
216 case T_AAAA:
217 name_ok = res_hnok;
218 break;
219 case T_PTR:
220 name_ok = res_dnok;
221 break;
222 default:
223 return (NULL); /* XXX should be abort(); */
224 }
225 /*
226 * find first satisfactory answer
227 */
228 hp = &answer->hdr;
229 ancount = ntohs(hp->ancount);
230 qdcount = ntohs(hp->qdcount);
231 bp = hostbuf;
232 buflen = sizeof hostbuf;
233 cp = answer->buf + HFIXEDSZ;
234 if (qdcount != 1) {
235 h_errno = NO_RECOVERY;
236 return (NULL);
237 }
238 n = dn_expand(answer->buf, eom, cp, bp, buflen);
239 if ((n < 0) || !(*name_ok)(bp)) {
240 h_errno = NO_RECOVERY;
241 return (NULL);
242 }
243 cp += n + QFIXEDSZ;
244 if (qtype == T_A || qtype == T_AAAA) {
245 /* res_send() has already verified that the query name is the
246 * same as the one we sent; this just gets the expanded name
247 * (i.e., with the succeeding search-domain tacked on).
248 */
249 n = strlen(bp) + 1; /* for the \0 */
250 if (n >= MAXHOSTNAMELEN) {
251 h_errno = NO_RECOVERY;
252 return (NULL);
253 }
254 host.h_name = bp;
255 bp += n;
256 buflen -= n;
257 /* The qname can be abbreviated, but h_name is now absolute. */
258 qname = host.h_name;
259 }
260 ap = host_aliases;
261 *ap = NULL;
262 host.h_aliases = host_aliases;
263 hap = h_addr_ptrs;
264 *hap = NULL;
265 host.h_addr_list = h_addr_ptrs;
266 haveanswer = 0;
267 had_error = 0;
268 while (ancount-- > 0 && cp < eom && !had_error) {
269 n = dn_expand(answer->buf, eom, cp, bp, buflen);
270 if ((n < 0) || !(*name_ok)(bp)) {
271 had_error++;
272 continue;
273 }
274 cp += n; /* name */
275 type = _getshort(cp);
276 cp += INT16SZ; /* type */
277 class = _getshort(cp);
278 cp += INT16SZ + INT32SZ; /* class, TTL */
279 n = _getshort(cp);
280 cp += INT16SZ; /* len */
281 if (class != C_IN) {
282 /* XXX - debug? syslog? */
283 cp += n;
284 continue; /* XXX - had_error++ ? */
285 }
286 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
287 if (ap >= &host_aliases[MAXALIASES-1])
288 continue;
289 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
290 if ((n < 0) || !(*name_ok)(tbuf)) {
291 had_error++;
292 continue;
293 }
294 cp += n;
295 /* Store alias. */
296 *ap++ = bp;
297 n = strlen(bp) + 1; /* for the \0 */
298 if (n >= MAXHOSTNAMELEN) {
299 had_error++;
300 continue;
301 }
302 bp += n;
303 buflen -= n;
304 /* Get canonical name. */
305 n = strlen(tbuf) + 1; /* for the \0 */
306 if (n > buflen || n >= MAXHOSTNAMELEN) {
307 had_error++;
308 continue;
309 }
310 strcpy(bp, tbuf);
311 host.h_name = bp;
312 bp += n;
313 buflen -= n;
314 continue;
315 }
316 if (qtype == T_PTR && type == T_CNAME) {
317 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
318 if (n < 0 || !res_dnok(tbuf)) {
319 had_error++;
320 continue;
321 }
322 cp += n;
323 /* Get canonical name. */
324 n = strlen(tbuf) + 1; /* for the \0 */
325 if (n > buflen || n >= MAXHOSTNAMELEN) {
326 had_error++;
327 continue;
328 }
329 strcpy(bp, tbuf);
330 tname = bp;
331 bp += n;
332 buflen -= n;
333 continue;
334 }
335 if (type != qtype) {
336 syslog(LOG_NOTICE|LOG_AUTH,
337 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
338 qname, p_class(C_IN), p_type(qtype),
339 p_type(type));
340 cp += n;
341 continue; /* XXX - had_error++ ? */
342 }
343 switch (type) {
344 case T_PTR:
345 if (strcasecmp(tname, bp) != 0) {
346 syslog(LOG_NOTICE|LOG_AUTH,
347 AskedForGot, qname, bp);
348 cp += n;
349 continue; /* XXX - had_error++ ? */
350 }
351 n = dn_expand(answer->buf, eom, cp, bp, buflen);
352 if ((n < 0) || !res_hnok(bp)) {
353 had_error++;
354 break;
355 }
356 #if MULTI_PTRS_ARE_ALIASES
357 cp += n;
358 if (!haveanswer)
359 host.h_name = bp;
360 else if (ap < &host_aliases[MAXALIASES-1])
361 *ap++ = bp;
362 else
363 n = -1;
364 if (n != -1) {
365 n = strlen(bp) + 1; /* for the \0 */
366 if (n >= MAXHOSTNAMELEN) {
367 had_error++;
368 break;
369 }
370 bp += n;
371 buflen -= n;
372 }
373 break;
374 #else
375 host.h_name = bp;
376 if (_res.options & RES_USE_INET6) {
377 n = strlen(bp) + 1; /* for the \0 */
378 if (n >= MAXHOSTNAMELEN) {
379 had_error++;
380 break;
381 }
382 bp += n;
383 buflen -= n;
384 map_v4v6_hostent(&host, &bp, &buflen);
385 }
386 h_errno = NETDB_SUCCESS;
387 return (&host);
388 #endif
389 case T_A:
390 case T_AAAA:
391 if (strcasecmp(host.h_name, bp) != 0) {
392 syslog(LOG_NOTICE|LOG_AUTH,
393 AskedForGot, host.h_name, bp);
394 cp += n;
395 continue; /* XXX - had_error++ ? */
396 }
397 if (n != host.h_length) {
398 cp += n;
399 continue;
400 }
401 if (!haveanswer) {
402 register int nn;
403
404 host.h_name = bp;
405 nn = strlen(bp) + 1; /* for the \0 */
406 bp += nn;
407 buflen -= nn;
408 }
409
410 bp += sizeof(align) - ((u_long)bp % sizeof(align));
411
412 if (bp + n >= &hostbuf[sizeof hostbuf]) {
413 dprintf("size (%d) too big\n", n);
414 had_error++;
415 continue;
416 }
417 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
418 if (!toobig++)
419 dprintf("Too many addresses (%d)\n",
420 MAXADDRS);
421 cp += n;
422 continue;
423 }
424 bcopy(cp, *hap++ = bp, n);
425 bp += n;
426 buflen -= n;
427 cp += n;
428 break;
429 default:
430 abort();
431 }
432 if (!had_error)
433 haveanswer++;
434 }
435 if (haveanswer) {
436 *ap = NULL;
437 *hap = NULL;
438 # if defined(RESOLVSORT)
439 /*
440 * Note: we sort even if host can take only one address
441 * in its return structures - should give it the "best"
442 * address in that case, not some random one
443 */
444 if (_res.nsort && haveanswer > 1 && qtype == T_A)
445 addrsort(h_addr_ptrs, haveanswer);
446 # endif /*RESOLVSORT*/
447 if (!host.h_name) {
448 n = strlen(qname) + 1; /* for the \0 */
449 if (n > buflen || n >= MAXHOSTNAMELEN)
450 goto no_recovery;
451 strcpy(bp, qname);
452 host.h_name = bp;
453 bp += n;
454 buflen -= n;
455 }
456 if (_res.options & RES_USE_INET6)
457 map_v4v6_hostent(&host, &bp, &buflen);
458 h_errno = NETDB_SUCCESS;
459 return (&host);
460 }
461 no_recovery:
462 h_errno = NO_RECOVERY;
463 return (NULL);
464 }
465
466 struct hostent *
467 gethostbyname(name)
468 const char *name;
469 {
470 struct hostent *hp;
471
472 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
473 h_errno = NETDB_INTERNAL;
474 return (NULL);
475 }
476 if (_res.options & RES_USE_INET6) {
477 hp = gethostbyname2(name, AF_INET6);
478 if (hp)
479 return (hp);
480 }
481 return (gethostbyname2(name, AF_INET));
482 }
483
484 struct hostent *
485 gethostbyname2(name, af)
486 const char *name;
487 int af;
488 {
489 querybuf buf;
490 const char *cp;
491 char *bp;
492 int n, size, type, len, i;
493 char lookups[MAXDNSLUS];
494 struct hostent *hp;
495
496 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
497 return (_gethtbyname(name));
498
499 switch (af) {
500 case AF_INET:
501 size = INADDRSZ;
502 type = T_A;
503 break;
504 case AF_INET6:
505 size = IN6ADDRSZ;
506 type = T_AAAA;
507 break;
508 default:
509 h_errno = NETDB_INTERNAL;
510 errno = EAFNOSUPPORT;
511 return (NULL);
512 }
513
514 host.h_addrtype = af;
515 host.h_length = size;
516
517 /*
518 * if there aren't any dots, it could be a user-level alias.
519 * this is also done in res_query() since we are not the only
520 * function that looks up host names.
521 */
522 if (!strchr(name, '.') && (cp = __hostalias(name)))
523 name = cp;
524
525 /*
526 * disallow names consisting only of digits/dots, unless
527 * they end in a dot.
528 */
529 if (isdigit(name[0]))
530 for (cp = name;; ++cp) {
531 if (!*cp) {
532 if (*--cp == '.')
533 break;
534 /*
535 * All-numeric, no dot at the end.
536 * Fake up a hostent as if we'd actually
537 * done a lookup.
538 */
539 if (inet_pton(af, name, host_addr) <= 0) {
540 h_errno = HOST_NOT_FOUND;
541 return (NULL);
542 }
543 strncpy(hostbuf, name, MAXDNAME);
544 hostbuf[MAXDNAME] = '\0';
545 bp = hostbuf + MAXDNAME;
546 len = sizeof hostbuf - MAXDNAME;
547 host.h_name = hostbuf;
548 host.h_aliases = host_aliases;
549 host_aliases[0] = NULL;
550 h_addr_ptrs[0] = (char *)host_addr;
551 h_addr_ptrs[1] = NULL;
552 host.h_addr_list = h_addr_ptrs;
553 if (_res.options & RES_USE_INET6)
554 map_v4v6_hostent(&host, &bp, &len);
555 h_errno = NETDB_SUCCESS;
556 return (&host);
557 }
558 if (!isdigit(*cp) && *cp != '.')
559 break;
560 }
561 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
562 name[0] == ':')
563 for (cp = name;; ++cp) {
564 if (!*cp) {
565 if (*--cp == '.')
566 break;
567 /*
568 * All-IPv6-legal, no dot at the end.
569 * Fake up a hostent as if we'd actually
570 * done a lookup.
571 */
572 if (inet_pton(af, name, host_addr) <= 0) {
573 h_errno = HOST_NOT_FOUND;
574 return (NULL);
575 }
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 h_errno = NETDB_SUCCESS;
587 return (&host);
588 }
589 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
590 break;
591 }
592
593 bcopy(_res.lookups, lookups, sizeof lookups);
594 if (lookups[0] == '\0')
595 strncpy(lookups, "bf", sizeof lookups);
596
597 hp = (struct hostent *)NULL;
598 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
599 switch (lookups[i]) {
600 #ifdef YP
601 case 'y':
602 hp = _yp_gethtbyname(name);
603 break;
604 #endif
605 case 'b':
606 if ((n = res_search(name, C_IN, T_A, buf.buf,
607 sizeof(buf))) < 0) {
608 dprintf("res_search failed (%d)\n", n);
609 break;
610 }
611 hp = getanswer(&buf, n, name, type);
612 break;
613 case 'f':
614 hp = _gethtbyname(name);
615 break;
616 }
617 }
618 return (hp);
619 }
620
621 struct hostent *
622 gethostbyaddr(addr, len, af)
623 const char *addr; /* XXX should have been def'd as u_char! */
624 int len, af;
625 {
626 const u_char *uaddr = (const u_char *)addr;
627 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
628 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
629 int n, size, i;
630 querybuf buf;
631 struct hostent *hp;
632 char qbuf[MAXDNAME+1], *qp;
633 char lookups[MAXDNSLUS];
634
635 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
636 return (_gethtbyaddr(addr, len, af));
637
638 if (af == AF_INET6 && len == IN6ADDRSZ &&
639 (!bcmp(uaddr, mapped, sizeof mapped) ||
640 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
641 /* Unmap. */
642 addr += sizeof mapped;
643 uaddr += sizeof mapped;
644 af = AF_INET;
645 len = INADDRSZ;
646 }
647 switch (af) {
648 case AF_INET:
649 size = INADDRSZ;
650 break;
651 case AF_INET6:
652 size = IN6ADDRSZ;
653 break;
654 default:
655 errno = EAFNOSUPPORT;
656 h_errno = NETDB_INTERNAL;
657 return (NULL);
658 }
659 if (size != len) {
660 errno = EINVAL;
661 h_errno = NETDB_INTERNAL;
662 return (NULL);
663 }
664 switch (af) {
665 case AF_INET:
666 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
667 (uaddr[3] & 0xff),
668 (uaddr[2] & 0xff),
669 (uaddr[1] & 0xff),
670 (uaddr[0] & 0xff));
671 break;
672 case AF_INET6:
673 qp = qbuf;
674 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
675 qp += sprintf(qp, "%x.%x.",
676 uaddr[n] & 0xf,
677 (uaddr[n] >> 4) & 0xf);
678 }
679 strcpy(qp, "ip6.int");
680 break;
681 default:
682 abort();
683 }
684
685 bcopy(_res.lookups, lookups, sizeof lookups);
686 if (lookups[0] == '\0')
687 strncpy(lookups, "bf", sizeof lookups);
688
689 hp = (struct hostent *)NULL;
690 for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
691 switch (lookups[i]) {
692 #ifdef YP
693 case 'y':
694 hp = _yp_gethtbyaddr(addr, len, af);
695 break;
696 #endif
697 case 'b':
698 n = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf,
699 sizeof(buf));
700 if (n < 0) {
701 dprintf("res_query failed (%d)\n", n);
702 break;
703 }
704 hp = getanswer(&buf, n, qbuf, T_PTR);
705 if (hp == NULL)
706 break;
707 hp->h_addrtype = af;
708 hp->h_length = len;
709 bcopy(addr, host_addr, len);
710 h_addr_ptrs[0] = (char *)&host_addr;
711 h_addr_ptrs[1] = (char *)0;
712 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
713 map_v4v6_address((char*)host_addr,
714 (char*)host_addr);
715 hp->h_addrtype = AF_INET6;
716 hp->h_length = IN6ADDRSZ;
717 }
718 h_errno = NETDB_SUCCESS;
719 break;
720 case 'f':
721 hp = _gethtbyaddr(addr, len, af);
722 break;
723 }
724 }
725 return (hp);
726 }
727
728 void
729 _sethtent(f)
730 int f;
731 {
732 if (!hostf)
733 hostf = fopen(_PATH_HOSTS, "r" );
734 else
735 rewind(hostf);
736 stayopen = f;
737 }
738
739 void
740 _endhtent()
741 {
742 if (hostf && !stayopen) {
743 (void) fclose(hostf);
744 hostf = NULL;
745 }
746 }
747
748 struct hostent *
749 _gethtent()
750 {
751 char *p;
752 register char *cp, **q;
753 int af, len;
754
755 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
756 h_errno = NETDB_INTERNAL;
757 return (NULL);
758 }
759 again:
760 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
761 h_errno = HOST_NOT_FOUND;
762 return (NULL);
763 }
764 if (*p == '#')
765 goto again;
766 if (!(cp = strpbrk(p, "#\n")))
767 goto again;
768 *cp = '\0';
769 if (!(cp = strpbrk(p, " \t")))
770 goto again;
771 *cp++ = '\0';
772 if (inet_pton(AF_INET6, p, host_addr) > 0) {
773 af = AF_INET6;
774 len = IN6ADDRSZ;
775 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
776 if (_res.options & RES_USE_INET6) {
777 map_v4v6_address((char*)host_addr, (char*)host_addr);
778 af = AF_INET6;
779 len = IN6ADDRSZ;
780 } else {
781 af = AF_INET;
782 len = INADDRSZ;
783 }
784 } else {
785 goto again;
786 }
787 h_addr_ptrs[0] = (char *)host_addr;
788 h_addr_ptrs[1] = NULL;
789 host.h_addr_list = h_addr_ptrs;
790 host.h_length = len;
791 host.h_addrtype = af;
792 while (*cp == ' ' || *cp == '\t')
793 cp++;
794 host.h_name = cp;
795 q = host.h_aliases = host_aliases;
796 if ((cp = strpbrk(cp, " \t")) != NULL)
797 *cp++ = '\0';
798 while (cp && *cp) {
799 if (*cp == ' ' || *cp == '\t') {
800 cp++;
801 continue;
802 }
803 if (q < &host_aliases[MAXALIASES - 1])
804 *q++ = cp;
805 if ((cp = strpbrk(cp, " \t")) != NULL)
806 *cp++ = '\0';
807 }
808 *q = NULL;
809 h_errno = NETDB_SUCCESS;
810 return (&host);
811 }
812
813 struct hostent *
814 _gethtbyname(name)
815 const char *name;
816 {
817 struct hostent *hp;
818
819 if (_res.options & RES_USE_INET6) {
820 hp = _gethtbyname2(name, AF_INET6);
821 if (hp)
822 return (hp);
823 }
824 return (_gethtbyname2(name, AF_INET));
825 }
826
827 struct hostent *
828 _gethtbyname2(name, af)
829 const char *name;
830 int af;
831 {
832 register struct hostent *p;
833 register char **cp;
834
835 _sethtent(0);
836 while ((p = _gethtent()) != NULL) {
837 if (p->h_addrtype != af)
838 continue;
839 if (strcasecmp(p->h_name, name) == 0)
840 break;
841 for (cp = p->h_aliases; *cp != 0; cp++)
842 if (strcasecmp(*cp, name) == 0)
843 goto found;
844 }
845 found:
846 _endhtent();
847 return (p);
848 }
849
850 struct hostent *
851 _gethtbyaddr(addr, len, af)
852 const char *addr;
853 int len, af;
854 {
855 register struct hostent *p;
856
857 _sethtent(0);
858 while ((p = _gethtent()) != NULL)
859 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
860 break;
861 _endhtent();
862 return (p);
863 }
864
865 static void
866 map_v4v6_address(src, dst)
867 const char *src;
868 char *dst;
869 {
870 u_char *p = (u_char *)dst;
871 char tmp[INADDRSZ];
872 int i;
873
874 /* Stash a temporary copy so our caller can update in place. */
875 bcopy(src, tmp, INADDRSZ);
876 /* Mark this ipv6 addr as a mapped ipv4. */
877 for (i = 0; i < 10; i++)
878 *p++ = 0x00;
879 *p++ = 0xff;
880 *p++ = 0xff;
881 /* Retrieve the saved copy and we're done. */
882 bcopy(tmp, (void*)p, INADDRSZ);
883 }
884
885 static void
886 map_v4v6_hostent(hp, bpp, lenp)
887 struct hostent *hp;
888 char **bpp;
889 int *lenp;
890 {
891 char **ap;
892
893 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
894 return;
895 hp->h_addrtype = AF_INET6;
896 hp->h_length = IN6ADDRSZ;
897 for (ap = hp->h_addr_list; *ap; ap++) {
898 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
899
900 if (*lenp < (i + IN6ADDRSZ)) {
901 /* Out of memory. Truncate address list here. XXX */
902 *ap = NULL;
903 return;
904 }
905 *bpp += i;
906 *lenp -= i;
907 map_v4v6_address(*ap, *bpp);
908 *ap = *bpp;
909 *bpp += IN6ADDRSZ;
910 *lenp -= IN6ADDRSZ;
911 }
912 }
913
914 #ifdef RESOLVSORT
915 static void
916 addrsort(ap, num)
917 char **ap;
918 int num;
919 {
920 int i, j;
921 char **p;
922 short aval[MAXADDRS];
923 int needsort = 0;
924
925 p = ap;
926 for (i = 0; i < num; i++, p++) {
927 for (j = 0 ; (unsigned)j < _res.nsort; j++)
928 if (_res.sort_list[j].addr.s_addr ==
929 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
930 break;
931 aval[i] = j;
932 if (needsort == 0 && i > 0 && j < aval[i-1])
933 needsort = i;
934 }
935 if (!needsort)
936 return;
937
938 while (needsort < num) {
939 for (j = needsort - 1; j >= 0; j--) {
940 if (aval[j] > aval[j+1]) {
941 char *hp;
942
943 i = aval[j];
944 aval[j] = aval[j+1];
945 aval[j+1] = i;
946
947 hp = ap[j];
948 ap[j] = ap[j+1];
949 ap[j+1] = hp;
950
951 } else
952 break;
953 }
954 needsort++;
955 }
956 }
957 #endif
958
959 #if defined(BSD43_BSD43_NFS) || defined(sun)
960 /* some libc's out there are bound internally to these names (UMIPS) */
961 void
962 ht_sethostent(stayopen)
963 int stayopen;
964 {
965 _sethtent(stayopen);
966 }
967
968 void
969 ht_endhostent()
970 {
971 _endhtent();
972 }
973
974 struct hostent *
975 ht_gethostbyname(name)
976 char *name;
977 {
978 return (_gethtbyname(name));
979 }
980
981 struct hostent *
982 ht_gethostbyaddr(addr, len, af)
983 const char *addr;
984 int len, af;
985 {
986 return (_gethtbyaddr(addr, len, af));
987 }
988
989 struct hostent *
990 gethostent()
991 {
992 return (_gethtent());
993 }
994
995 void
996 dns_service()
997 {
998 return;
999 }
1000
1001 #undef dn_skipname
1002 dn_skipname(comp_dn, eom)
1003 const u_char *comp_dn, *eom;
1004 {
1005 return (__dn_skipname(comp_dn, eom));
1006 }
1007 #endif /*old-style libc with yp junk in it*/
1008
1009 #ifdef YP
1010 struct hostent *
1011 _yphostent(line)
1012 char *line;
1013 {
1014 static struct in_addr host_addrs[MAXADDRS];
1015 char *p = line;
1016 char *cp, **q;
1017 char **hap;
1018 struct in_addr *buf;
1019 int more;
1020
1021 host.h_name = NULL;
1022 host.h_addr_list = h_addr_ptrs;
1023 host.h_length = sizeof(u_int32_t);
1024 host.h_addrtype = AF_INET;
1025 hap = h_addr_ptrs;
1026 buf = host_addrs;
1027 q = host.h_aliases = host_aliases;
1028
1029 nextline:
1030 more = 0;
1031 cp = strpbrk(p, " \t");
1032 if (cp == NULL) {
1033 if (host.h_name == NULL)
1034 return (NULL);
1035 else
1036 goto done;
1037 }
1038 *cp++ = '\0';
1039
1040 *hap++ = (char *)buf;
1041 (void) inet_aton(p, buf++);
1042
1043 while (*cp == ' ' || *cp == '\t')
1044 cp++;
1045 p = cp;
1046 cp = strpbrk(p, " \t\n");
1047 if (cp != NULL) {
1048 if (*cp == '\n')
1049 more = 1;
1050 *cp++ = '\0';
1051 }
1052 if (!host.h_name)
1053 host.h_name = p;
1054 else if (strcmp(host.h_name, p)==0)
1055 ;
1056 else if (q < &host_aliases[MAXALIASES - 1])
1057 *q++ = p;
1058 p = cp;
1059 if (more)
1060 goto nextline;
1061
1062 while (cp && *cp) {
1063 if (*cp == ' ' || *cp == '\t') {
1064 cp++;
1065 continue;
1066 }
1067 if (*cp == '\n') {
1068 cp++;
1069 goto nextline;
1070 }
1071 if (q < &host_aliases[MAXALIASES - 1])
1072 *q++ = cp;
1073 cp = strpbrk(cp, " \t");
1074 if (cp != NULL)
1075 *cp++ = '\0';
1076 }
1077 done:
1078 *q = NULL;
1079 *hap = NULL;
1080 return (&host);
1081 }
1082
1083 struct hostent *
1084 _yp_gethtbyaddr(addr, len, type)
1085 const char *addr;
1086 int len, type;
1087 {
1088 struct hostent *hp = (struct hostent *)NULL;
1089 static char *__ypcurrent;
1090 int __ypcurrentlen, r;
1091 char name[sizeof("xxx.xxx.xxx.xxx") + 1];
1092
1093 if (!__ypdomain) {
1094 if (_yp_check(&__ypdomain) == 0)
1095 return (hp);
1096 }
1097 (void)snprintf(name, sizeof name, "%u.%u.%u.%u",
1098 ((unsigned)addr[0] & 0xff),
1099 ((unsigned)addr[1] & 0xff),
1100 ((unsigned)addr[2] & 0xff),
1101 ((unsigned)addr[3] & 0xff));
1102 if (__ypcurrent)
1103 free(__ypcurrent);
1104 __ypcurrent = NULL;
1105 r = yp_match(__ypdomain, "hosts.byaddr", name,
1106 strlen(name), &__ypcurrent, &__ypcurrentlen);
1107 if (r==0)
1108 hp = _yphostent(__ypcurrent);
1109 if (hp==NULL)
1110 h_errno = HOST_NOT_FOUND;
1111 return (hp);
1112 }
1113
1114 struct hostent *
1115 _yp_gethtbyname(name)
1116 const char *name;
1117 {
1118 struct hostent *hp = (struct hostent *)NULL;
1119 static char *__ypcurrent;
1120 int __ypcurrentlen, r;
1121
1122 if (!__ypdomain) {
1123 if (_yp_check(&__ypdomain) == 0)
1124 return (hp);
1125 }
1126 if (__ypcurrent)
1127 free(__ypcurrent);
1128 __ypcurrent = NULL;
1129 r = yp_match(__ypdomain, "hosts.byname", name,
1130 strlen(name), &__ypcurrent, &__ypcurrentlen);
1131 if (r==0)
1132 hp = _yphostent(__ypcurrent);
1133 if (hp==NULL)
1134 h_errno = HOST_NOT_FOUND;
1135 return (hp);
1136 }
1137 #endif
1138