gethnamaddr.c revision 1.27 1 /* $NetBSD: gethnamaddr.c,v 1.27 1999/10/24 12:36:02 lukem 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.27 1999/10/24 12:36:02 lukem Exp $");
65 #endif
66 #endif /* LIBC_SCCS and not lint */
67
68 #if defined(_LIBC)
69 #include "namespace.h"
70 #endif
71 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 #include <arpa/nameser.h>
76
77 #include <assert.h>
78 #include <ctype.h>
79 #include <errno.h>
80 #include <netdb.h>
81 #include <resolv.h>
82 #include <stdio.h>
83 #include <syslog.h>
84
85 #ifdef __STDC__
86 #include <stdarg.h>
87 #else
88 #include <varargs.h>
89 #endif
90
91 #ifndef LOG_AUTH
92 # define LOG_AUTH 0
93 #endif
94
95 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
96
97 #include <nsswitch.h>
98 #include <stdlib.h>
99 #include <string.h>
100
101 #ifdef YP
102 #include <rpc/rpc.h>
103 #include <rpcsvc/yp_prot.h>
104 #include <rpcsvc/ypclnt.h>
105 #endif
106
107 #if defined(_LIBC) && defined(__weak_alias)
108 __weak_alias(gethostbyaddr,_gethostbyaddr);
109 __weak_alias(gethostbyname,_gethostbyname);
110 #endif
111
112 #define MAXALIASES 35
113 #define MAXADDRS 35
114
115 static const char AskedForGot[] =
116 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
117
118 static char *h_addr_ptrs[MAXADDRS + 1];
119
120 #ifdef YP
121 static char *__ypdomain;
122 #endif
123
124 static struct hostent host;
125 static char *host_aliases[MAXALIASES];
126 static char hostbuf[8*1024];
127 static u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
128 static FILE *hostf = NULL;
129 static int stayopen = 0;
130
131
132 #if PACKETSZ > 1024
133 #define MAXPACKET PACKETSZ
134 #else
135 #define MAXPACKET 1024
136 #endif
137
138 typedef union {
139 HEADER hdr;
140 u_char buf[MAXPACKET];
141 } querybuf;
142
143 typedef union {
144 int32_t al;
145 char ac;
146 } align;
147
148 extern int h_errno;
149
150 #ifdef DEBUG
151 static void dprintf __P((char *, int));
152 #endif
153 static struct hostent *getanswer __P((const querybuf *, int,
154 const char *, int));
155 static void map_v4v6_address __P((const char *, char *));
156 static void map_v4v6_hostent __P((struct hostent *, char **, int *));
157 #ifdef RESOLVSORT
158 static void addrsort __P((char **, int));
159 #endif
160
161 struct hostent *gethostbyname __P((const char *));
162 struct hostent *gethostbyname2 __P((const char *, int));
163 struct hostent *gethostbyaddr __P((const char *, int, int ));
164 void _sethtent __P((int));
165 void _endhtent __P((void));
166 struct hostent *_gethtent __P((void));
167 struct hostent *_gethtbyname2 __P((const char *, int));
168 void ht_sethostent __P((int));
169 void ht_endhostent __P((void));
170 struct hostent *ht_gethostbyname __P((char *));
171 struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
172 struct hostent *gethostent __P((void));
173 void dns_service __P((void));
174 int dn_skipname __P((const u_char *, const u_char *));
175 int _gethtbyaddr __P((void *, void *, va_list));
176 int _gethtbyname __P((void *, void *, va_list));
177 int _dns_gethtbyaddr __P((void *, void *, va_list));
178 int _dns_gethtbyname __P((void *, void *, va_list));
179 #ifdef YP
180 struct hostent *_yphostent __P((char *, int));
181 int _yp_gethtbyaddr __P((void *, void *, va_list));
182 int _yp_gethtbyname __P((void *, void *, va_list));
183 #endif
184
185 static const ns_src default_dns_files[] = {
186 { NSSRC_FILES, NS_SUCCESS },
187 { NSSRC_DNS, NS_SUCCESS },
188 { 0 }
189 };
190
191
192 #ifdef DEBUG
193 static void
194 dprintf(msg, num)
195 char *msg;
196 int num;
197 {
198
199 _DIAGASSERT(msg != NULL);
200
201 if (_res.options & RES_DEBUG) {
202 int save = errno;
203
204 printf(msg, num);
205 errno = save;
206 }
207 }
208 #else
209 # define dprintf(msg, num) /*nada*/
210 #endif
211
212 static struct hostent *
213 getanswer(answer, anslen, qname, qtype)
214 const querybuf *answer;
215 int anslen;
216 const char *qname;
217 int qtype;
218 {
219 const HEADER *hp;
220 const u_char *cp;
221 int n;
222 const u_char *eom;
223 char *bp, **ap, **hap;
224 int type, class, buflen, ancount, qdcount;
225 int haveanswer, had_error;
226 int toobig = 0;
227 char tbuf[MAXDNAME];
228 const char *tname;
229 int (*name_ok) __P((const char *));
230
231 _DIAGASSERT(answer != NULL);
232 _DIAGASSERT(qname != NULL);
233
234 tname = qname;
235 host.h_name = NULL;
236 eom = answer->buf + anslen;
237 switch (qtype) {
238 case T_A:
239 case T_AAAA:
240 name_ok = res_hnok;
241 break;
242 case T_PTR:
243 name_ok = res_dnok;
244 break;
245 default:
246 return (NULL); /* XXX should be abort(); */
247 }
248 /*
249 * find first satisfactory answer
250 */
251 hp = &answer->hdr;
252 ancount = ntohs(hp->ancount);
253 qdcount = ntohs(hp->qdcount);
254 bp = hostbuf;
255 buflen = sizeof hostbuf;
256 cp = answer->buf + HFIXEDSZ;
257 if (qdcount != 1) {
258 h_errno = NO_RECOVERY;
259 return (NULL);
260 }
261 n = dn_expand(answer->buf, eom, cp, bp, buflen);
262 if ((n < 0) || !(*name_ok)(bp)) {
263 h_errno = NO_RECOVERY;
264 return (NULL);
265 }
266 cp += n + QFIXEDSZ;
267 if (qtype == T_A || qtype == T_AAAA) {
268 /* res_send() has already verified that the query name is the
269 * same as the one we sent; this just gets the expanded name
270 * (i.e., with the succeeding search-domain tacked on).
271 */
272 n = strlen(bp) + 1; /* for the \0 */
273 if (n >= MAXHOSTNAMELEN) {
274 h_errno = NO_RECOVERY;
275 return (NULL);
276 }
277 host.h_name = bp;
278 bp += n;
279 buflen -= n;
280 /* The qname can be abbreviated, but h_name is now absolute. */
281 qname = host.h_name;
282 }
283 ap = host_aliases;
284 *ap = NULL;
285 host.h_aliases = host_aliases;
286 hap = h_addr_ptrs;
287 *hap = NULL;
288 host.h_addr_list = h_addr_ptrs;
289 haveanswer = 0;
290 had_error = 0;
291 while (ancount-- > 0 && cp < eom && !had_error) {
292 n = dn_expand(answer->buf, eom, cp, bp, buflen);
293 if ((n < 0) || !(*name_ok)(bp)) {
294 had_error++;
295 continue;
296 }
297 cp += n; /* name */
298 type = _getshort(cp);
299 cp += INT16SZ; /* type */
300 class = _getshort(cp);
301 cp += INT16SZ + INT32SZ; /* class, TTL */
302 n = _getshort(cp);
303 cp += INT16SZ; /* len */
304 if (class != C_IN) {
305 /* XXX - debug? syslog? */
306 cp += n;
307 continue; /* XXX - had_error++ ? */
308 }
309 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
310 if (ap >= &host_aliases[MAXALIASES-1])
311 continue;
312 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
313 if ((n < 0) || !(*name_ok)(tbuf)) {
314 had_error++;
315 continue;
316 }
317 cp += n;
318 /* Store alias. */
319 *ap++ = bp;
320 n = strlen(bp) + 1; /* for the \0 */
321 if (n >= MAXHOSTNAMELEN) {
322 had_error++;
323 continue;
324 }
325 bp += n;
326 buflen -= n;
327 /* Get canonical name. */
328 n = strlen(tbuf) + 1; /* for the \0 */
329 if (n > buflen || n >= MAXHOSTNAMELEN) {
330 had_error++;
331 continue;
332 }
333 strcpy(bp, tbuf);
334 host.h_name = bp;
335 bp += n;
336 buflen -= n;
337 continue;
338 }
339 if (qtype == T_PTR && type == T_CNAME) {
340 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
341 if (n < 0 || !res_dnok(tbuf)) {
342 had_error++;
343 continue;
344 }
345 cp += n;
346 /* Get canonical name. */
347 n = strlen(tbuf) + 1; /* for the \0 */
348 if (n > buflen || n >= MAXHOSTNAMELEN) {
349 had_error++;
350 continue;
351 }
352 strcpy(bp, tbuf);
353 tname = bp;
354 bp += n;
355 buflen -= n;
356 continue;
357 }
358 if (type != qtype) {
359 syslog(LOG_NOTICE|LOG_AUTH,
360 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
361 qname, p_class(C_IN), p_type(qtype),
362 p_type(type));
363 cp += n;
364 continue; /* XXX - had_error++ ? */
365 }
366 switch (type) {
367 case T_PTR:
368 if (strcasecmp(tname, bp) != 0) {
369 syslog(LOG_NOTICE|LOG_AUTH,
370 AskedForGot, qname, bp);
371 cp += n;
372 continue; /* XXX - had_error++ ? */
373 }
374 n = dn_expand(answer->buf, eom, cp, bp, buflen);
375 if ((n < 0) || !res_hnok(bp)) {
376 had_error++;
377 break;
378 }
379 #if MULTI_PTRS_ARE_ALIASES
380 cp += n;
381 if (!haveanswer)
382 host.h_name = bp;
383 else if (ap < &host_aliases[MAXALIASES-1])
384 *ap++ = bp;
385 else
386 n = -1;
387 if (n != -1) {
388 n = strlen(bp) + 1; /* for the \0 */
389 if (n >= MAXHOSTNAMELEN) {
390 had_error++;
391 break;
392 }
393 bp += n;
394 buflen -= n;
395 }
396 break;
397 #else
398 host.h_name = bp;
399 if (_res.options & RES_USE_INET6) {
400 n = strlen(bp) + 1; /* for the \0 */
401 if (n >= MAXHOSTNAMELEN) {
402 had_error++;
403 break;
404 }
405 bp += n;
406 buflen -= n;
407 map_v4v6_hostent(&host, &bp, &buflen);
408 }
409 h_errno = NETDB_SUCCESS;
410 return (&host);
411 #endif
412 case T_A:
413 case T_AAAA:
414 if (strcasecmp(host.h_name, bp) != 0) {
415 syslog(LOG_NOTICE|LOG_AUTH,
416 AskedForGot, host.h_name, bp);
417 cp += n;
418 continue; /* XXX - had_error++ ? */
419 }
420 if (n != host.h_length) {
421 cp += n;
422 continue;
423 }
424 if (!haveanswer) {
425 int nn;
426
427 host.h_name = bp;
428 nn = strlen(bp) + 1; /* for the \0 */
429 bp += nn;
430 buflen -= nn;
431 }
432
433 bp += sizeof(align) -
434 (size_t)((u_long)bp % sizeof(align));
435
436 if (bp + n >= &hostbuf[sizeof hostbuf]) {
437 dprintf("size (%d) too big\n", n);
438 had_error++;
439 continue;
440 }
441 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
442 if (!toobig++)
443 dprintf("Too many addresses (%d)\n",
444 MAXADDRS);
445 cp += n;
446 continue;
447 }
448 (void)memcpy(*hap++ = bp, cp, (size_t)n);
449 bp += n;
450 buflen -= n;
451 cp += n;
452 break;
453 default:
454 abort();
455 }
456 if (!had_error)
457 haveanswer++;
458 }
459 if (haveanswer) {
460 *ap = NULL;
461 *hap = NULL;
462 # if defined(RESOLVSORT)
463 /*
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
467 */
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;
479 }
480 if (_res.options & RES_USE_INET6)
481 map_v4v6_hostent(&host, &bp, &buflen);
482 h_errno = NETDB_SUCCESS;
483 return (&host);
484 }
485 no_recovery:
486 h_errno = NO_RECOVERY;
487 return (NULL);
488 }
489
490 struct hostent *
491 gethostbyname(name)
492 const char *name;
493 {
494 struct hostent *hp;
495
496 _DIAGASSERT(name != NULL);
497
498 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
499 h_errno = NETDB_INTERNAL;
500 return (NULL);
501 }
502 if (_res.options & RES_USE_INET6) {
503 hp = gethostbyname2(name, AF_INET6);
504 if (hp)
505 return (hp);
506 }
507 return (gethostbyname2(name, AF_INET));
508 }
509
510 struct hostent *
511 gethostbyname2(name, af)
512 const char *name;
513 int af;
514 {
515 const char *cp;
516 char *bp;
517 int size, len;
518 struct hostent *hp;
519 static const ns_dtab dtab[] = {
520 NS_FILES_CB(_gethtbyname, NULL)
521 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
522 NS_NIS_CB(_yp_gethtbyname, NULL)
523 { 0 }
524 };
525
526 _DIAGASSERT(name != NULL);
527
528 switch (af) {
529 case AF_INET:
530 size = INADDRSZ;
531 break;
532 case AF_INET6:
533 size = IN6ADDRSZ;
534 break;
535 default:
536 h_errno = NETDB_INTERNAL;
537 errno = EAFNOSUPPORT;
538 return (NULL);
539 }
540
541 host.h_addrtype = af;
542 host.h_length = size;
543
544 /*
545 * if there aren't any dots, it could be a user-level alias.
546 * this is also done in res_query() since we are not the only
547 * function that looks up host names.
548 */
549 if (!strchr(name, '.') && (cp = __hostalias(name)))
550 name = cp;
551
552 /*
553 * disallow names consisting only of digits/dots, unless
554 * they end in a dot.
555 */
556 if (isdigit(name[0]))
557 for (cp = name;; ++cp) {
558 if (!*cp) {
559 if (*--cp == '.')
560 break;
561 /*
562 * All-numeric, no dot at the end.
563 * Fake up a hostent as if we'd actually
564 * done a lookup.
565 */
566 if (inet_pton(af, name,
567 (char *)host_addr) <= 0) {
568 h_errno = HOST_NOT_FOUND;
569 return (NULL);
570 }
571 strncpy(hostbuf, name, MAXDNAME);
572 hostbuf[MAXDNAME] = '\0';
573 bp = hostbuf + MAXDNAME;
574 len = sizeof hostbuf - MAXDNAME;
575 host.h_name = hostbuf;
576 host.h_aliases = host_aliases;
577 host_aliases[0] = NULL;
578 h_addr_ptrs[0] = (char *)host_addr;
579 h_addr_ptrs[1] = NULL;
580 host.h_addr_list = h_addr_ptrs;
581 if (_res.options & RES_USE_INET6)
582 map_v4v6_hostent(&host, &bp, &len);
583 h_errno = NETDB_SUCCESS;
584 return (&host);
585 }
586 if (!isdigit(*cp) && *cp != '.')
587 break;
588 }
589 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
590 name[0] == ':')
591 for (cp = name;; ++cp) {
592 if (!*cp) {
593 if (*--cp == '.')
594 break;
595 /*
596 * All-IPv6-legal, no dot at the end.
597 * Fake up a hostent as if we'd actually
598 * done a lookup.
599 */
600 if (inet_pton(af, name,
601 (char *)host_addr) <= 0) {
602 h_errno = HOST_NOT_FOUND;
603 return (NULL);
604 }
605 strncpy(hostbuf, name, MAXDNAME);
606 hostbuf[MAXDNAME] = '\0';
607 bp = hostbuf + MAXDNAME;
608 len = sizeof hostbuf - MAXDNAME;
609 host.h_name = hostbuf;
610 host.h_aliases = host_aliases;
611 host_aliases[0] = NULL;
612 h_addr_ptrs[0] = (char *)host_addr;
613 h_addr_ptrs[1] = NULL;
614 host.h_addr_list = h_addr_ptrs;
615 h_errno = NETDB_SUCCESS;
616 return (&host);
617 }
618 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
619 break;
620 }
621
622 hp = (struct hostent *)NULL;
623 h_errno = NETDB_INTERNAL;
624 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname",
625 default_dns_files, name, len, af) != NS_SUCCESS)
626 return (struct hostent *)NULL;
627 h_errno = NETDB_SUCCESS;
628 return (hp);
629 }
630
631 struct hostent *
632 gethostbyaddr(addr, len, af)
633 const char *addr; /* XXX should have been def'd as u_char! */
634 int len, af;
635 {
636 const u_char *uaddr = (const u_char *)addr;
637 int size;
638 struct hostent *hp;
639 static const ns_dtab dtab[] = {
640 NS_FILES_CB(_gethtbyaddr, NULL)
641 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
642 NS_NIS_CB(_yp_gethtbyaddr, NULL)
643 { 0 }
644 };
645
646 _DIAGASSERT(addr != NULL);
647
648 if (af == AF_INET6 && len == IN6ADDRSZ &&
649 (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)uaddr) ||
650 IN6_IS_ADDR_V4COMPAT((struct in6_addr *)uaddr))) {
651 /* Unmap. */
652 addr += IN6ADDRSZ - INADDRSZ;
653 uaddr += IN6ADDRSZ - INADDRSZ;
654 af = AF_INET;
655 len = INADDRSZ;
656 }
657 switch (af) {
658 case AF_INET:
659 size = INADDRSZ;
660 break;
661 case AF_INET6:
662 size = IN6ADDRSZ;
663 break;
664 default:
665 errno = EAFNOSUPPORT;
666 h_errno = NETDB_INTERNAL;
667 return (NULL);
668 }
669 if (size != len) {
670 errno = EINVAL;
671 h_errno = NETDB_INTERNAL;
672 return (NULL);
673 }
674 hp = (struct hostent *)NULL;
675 h_errno = NETDB_INTERNAL;
676 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
677 default_dns_files, uaddr, len, af) != NS_SUCCESS)
678 return (struct hostent *)NULL;
679 h_errno = NETDB_SUCCESS;
680 return (hp);
681 }
682
683 void
684 _sethtent(f)
685 int f;
686 {
687 if (!hostf)
688 hostf = fopen(_PATH_HOSTS, "r" );
689 else
690 rewind(hostf);
691 stayopen = f;
692 }
693
694 void
695 _endhtent()
696 {
697 if (hostf && !stayopen) {
698 (void) fclose(hostf);
699 hostf = NULL;
700 }
701 }
702
703 struct hostent *
704 _gethtent()
705 {
706 char *p;
707 char *cp, **q;
708 int af, len;
709
710 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
711 h_errno = NETDB_INTERNAL;
712 return (NULL);
713 }
714 again:
715 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
716 h_errno = HOST_NOT_FOUND;
717 return (NULL);
718 }
719 if (*p == '#')
720 goto again;
721 if (!(cp = strpbrk(p, "#\n")))
722 goto again;
723 *cp = '\0';
724 if (!(cp = strpbrk(p, " \t")))
725 goto again;
726 *cp++ = '\0';
727 if (inet_pton(AF_INET6, p, (char *)host_addr) > 0) {
728 af = AF_INET6;
729 len = IN6ADDRSZ;
730 } else if (inet_pton(AF_INET, p, (char *)host_addr) > 0) {
731 if (_res.options & RES_USE_INET6) {
732 map_v4v6_address((char *)host_addr, (char *)host_addr);
733 af = AF_INET6;
734 len = IN6ADDRSZ;
735 } else {
736 af = AF_INET;
737 len = INADDRSZ;
738 }
739 } else {
740 goto again;
741 }
742 /* if this is not something we're looking for, skip it. */
743 if (host.h_addrtype != af)
744 goto again;
745 if (host.h_length != len)
746 goto again;
747 h_addr_ptrs[0] = (char *)host_addr;
748 h_addr_ptrs[1] = NULL;
749 host.h_addr_list = h_addr_ptrs;
750 host.h_length = len;
751 host.h_addrtype = af;
752 while (*cp == ' ' || *cp == '\t')
753 cp++;
754 host.h_name = cp;
755 q = host.h_aliases = host_aliases;
756 if ((cp = strpbrk(cp, " \t")) != NULL)
757 *cp++ = '\0';
758 while (cp && *cp) {
759 if (*cp == ' ' || *cp == '\t') {
760 cp++;
761 continue;
762 }
763 if (q < &host_aliases[MAXALIASES - 1])
764 *q++ = cp;
765 if ((cp = strpbrk(cp, " \t")) != NULL)
766 *cp++ = '\0';
767 }
768 *q = NULL;
769 h_errno = NETDB_SUCCESS;
770 return (&host);
771 }
772
773 /*ARGSUSED*/
774 int
775 _gethtbyname(rv, cb_data, ap)
776 void *rv;
777 void *cb_data;
778 va_list ap;
779 {
780 struct hostent *hp;
781 const char *name;
782 int len, af;
783
784 _DIAGASSERT(rv != NULL);
785
786 name = va_arg(ap, char *);
787 len = va_arg(ap, int);
788 af = va_arg(ap, int);
789
790 hp = NULL;
791 #if 0
792 if (_res.options & RES_USE_INET6)
793 hp = _gethtbyname2(name, AF_INET6);
794 if (hp==NULL)
795 hp = _gethtbyname2(name, AF_INET);
796 #else
797 hp = _gethtbyname2(name, af);
798 #endif
799 *((struct hostent **)rv) = hp;
800 if (hp==NULL) {
801 h_errno = HOST_NOT_FOUND;
802 return NS_NOTFOUND;
803 }
804 return NS_SUCCESS;
805 }
806
807 struct hostent *
808 _gethtbyname2(name, af)
809 const char *name;
810 int af;
811 {
812 struct hostent *p;
813 char *tmpbuf, *ptr, **cp;
814 int num;
815 size_t len;
816
817 _DIAGASSERT(name != NULL);
818
819 _sethtent(0);
820 ptr = tmpbuf = NULL;
821 num = 0;
822 while ((p = _gethtent()) != NULL && num < MAXADDRS) {
823 if (p->h_addrtype != af)
824 continue;
825 if (strcasecmp(p->h_name, name) != 0) {
826 for (cp = p->h_aliases; *cp != NULL; cp++)
827 if (strcasecmp(*cp, name) == 0)
828 break;
829 if (*cp == NULL) continue;
830 }
831
832 if (num == 0) {
833 size_t bufsize;
834 char *src;
835
836 bufsize = strlen(p->h_name) + 2 +
837 MAXADDRS * p->h_length +
838 ALIGNBYTES;
839 for (cp = p->h_aliases; *cp != NULL; cp++)
840 bufsize += strlen(*cp) + 1;
841
842 if ((tmpbuf = malloc(bufsize)) == NULL) {
843 h_errno = NETDB_INTERNAL;
844 return NULL;
845 }
846
847 ptr = tmpbuf;
848 src = p->h_name;
849 while ((*ptr++ = *src++) != '\0');
850 for (cp = p->h_aliases; *cp != NULL; cp++) {
851 src = *cp;
852 while ((*ptr++ = *src++) != '\0');
853 }
854 *ptr++ = '\0';
855
856 ptr = (char *)ALIGN(ptr);
857 }
858
859 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
860 ptr += p->h_length;
861 num++;
862 }
863 _endhtent();
864 if (num == 0) return NULL;
865
866 len = ptr - tmpbuf;
867 if (len > sizeof(hostbuf)) {
868 free(tmpbuf);
869 errno = ENOSPC;
870 h_errno = NETDB_INTERNAL;
871 return NULL;
872 }
873 ptr = memcpy(hostbuf, tmpbuf, len);
874 free(tmpbuf);
875
876 host.h_name = ptr;
877 while (*ptr++);
878
879 cp = host_aliases;
880 while (*ptr) {
881 *cp++ = ptr;
882 while (*ptr++);
883 }
884 ptr++;
885 *cp = NULL;
886
887 ptr = (char *)ALIGN(ptr);
888 cp = h_addr_ptrs;
889 while (num--) {
890 *cp++ = ptr;
891 ptr += host.h_length;
892 }
893 *cp = NULL;
894
895 return (&host);
896 }
897
898 /*ARGSUSED*/
899 int
900 _gethtbyaddr(rv, cb_data, ap)
901 void *rv;
902 void *cb_data;
903 va_list ap;
904 {
905 struct hostent *p;
906 const unsigned char *addr;
907 int len, af;
908
909 _DIAGASSERT(rv != NULL);
910
911 addr = va_arg(ap, unsigned char *);
912 len = va_arg(ap, int);
913 af = va_arg(ap, int);
914
915 host.h_length = len;
916 host.h_addrtype = af;
917
918 _sethtent(0);
919 while ((p = _gethtent()) != NULL)
920 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
921 (size_t)len))
922 break;
923 _endhtent();
924 *((struct hostent **)rv) = p;
925 if (p==NULL) {
926 h_errno = HOST_NOT_FOUND;
927 return NS_NOTFOUND;
928 }
929 return NS_SUCCESS;
930 }
931
932 static void
933 map_v4v6_address(src, dst)
934 const char *src;
935 char *dst;
936 {
937 u_char *p = (u_char *)dst;
938 char tmp[INADDRSZ];
939 int i;
940
941 _DIAGASSERT(src != NULL);
942 _DIAGASSERT(dst != NULL);
943
944 /* Stash a temporary copy so our caller can update in place. */
945 (void)memcpy(tmp, src, INADDRSZ);
946 /* Mark this ipv6 addr as a mapped ipv4. */
947 for (i = 0; i < 10; i++)
948 *p++ = 0x00;
949 *p++ = 0xff;
950 *p++ = 0xff;
951 /* Retrieve the saved copy and we're done. */
952 (void)memcpy((void *)p, tmp, INADDRSZ);
953 }
954
955 static void
956 map_v4v6_hostent(hp, bpp, lenp)
957 struct hostent *hp;
958 char **bpp;
959 int *lenp;
960 {
961 char **ap;
962
963 _DIAGASSERT(hp != NULL);
964 _DIAGASSERT(bpp != NULL);
965 _DIAGASSERT(lenp != NULL);
966
967 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
968 return;
969 hp->h_addrtype = AF_INET6;
970 hp->h_length = IN6ADDRSZ;
971 for (ap = hp->h_addr_list; *ap; ap++) {
972 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
973
974 if (*lenp < (i + IN6ADDRSZ)) {
975 /* Out of memory. Truncate address list here. XXX */
976 *ap = NULL;
977 return;
978 }
979 *bpp += i;
980 *lenp -= i;
981 map_v4v6_address(*ap, *bpp);
982 *ap = *bpp;
983 *bpp += IN6ADDRSZ;
984 *lenp -= IN6ADDRSZ;
985 }
986 }
987
988 #ifdef RESOLVSORT
989 static void
990 addrsort(ap, num)
991 char **ap;
992 int num;
993 {
994 int i, j;
995 char **p;
996 short aval[MAXADDRS];
997 int needsort = 0;
998
999 _DIAGASSERT(ap != NULL);
1000
1001 p = ap;
1002 for (i = 0; i < num; i++, p++) {
1003 for (j = 0 ; (unsigned)j < _res.nsort; j++)
1004 if (_res.sort_list[j].addr.s_addr ==
1005 (((struct in_addr *)(void *)(*p))->s_addr &
1006 _res.sort_list[j].mask))
1007 break;
1008 aval[i] = j;
1009 if (needsort == 0 && i > 0 && j < aval[i-1])
1010 needsort = i;
1011 }
1012 if (!needsort)
1013 return;
1014
1015 while (needsort < num) {
1016 for (j = needsort - 1; j >= 0; j--) {
1017 if (aval[j] > aval[j+1]) {
1018 char *hp;
1019
1020 i = aval[j];
1021 aval[j] = aval[j+1];
1022 aval[j+1] = i;
1023
1024 hp = ap[j];
1025 ap[j] = ap[j+1];
1026 ap[j+1] = hp;
1027
1028 } else
1029 break;
1030 }
1031 needsort++;
1032 }
1033 }
1034 #endif
1035
1036 #if defined(BSD43_BSD43_NFS) || defined(sun)
1037 /* XXX: should we remove this cruft? - lukem */
1038 /* some libc's out there are bound internally to these names (UMIPS) */
1039 void
1040 ht_sethostent(stayopen)
1041 int stayopen;
1042 {
1043 _sethtent(stayopen);
1044 }
1045
1046 void
1047 ht_endhostent()
1048 {
1049 _endhtent();
1050 }
1051
1052 struct hostent *
1053 ht_gethostbyname(name)
1054 char *name;
1055 {
1056 return (_gethtbyname(name));
1057 }
1058
1059 struct hostent *
1060 ht_gethostbyaddr(addr, len, af)
1061 const char *addr;
1062 int len, af;
1063 {
1064 return (_gethtbyaddr(addr, len, af));
1065 }
1066
1067 struct hostent *
1068 gethostent()
1069 {
1070 return (_gethtent());
1071 }
1072
1073 void
1074 dns_service()
1075 {
1076 return;
1077 }
1078
1079 #undef dn_skipname
1080 dn_skipname(comp_dn, eom)
1081 const u_char *comp_dn, *eom;
1082 {
1083 return (__dn_skipname(comp_dn, eom));
1084 }
1085 #endif /*old-style libc with yp junk in it*/
1086
1087 /*ARGSUSED*/
1088 int
1089 _dns_gethtbyname(rv, cb_data, ap)
1090 void *rv;
1091 void *cb_data;
1092 va_list ap;
1093 {
1094 querybuf buf;
1095 int n, type;
1096 struct hostent *hp;
1097 const char *name;
1098 int af;
1099
1100 _DIAGASSERT(rv != NULL);
1101
1102 name = va_arg(ap, char *);
1103 /* NOSTRICT skip len */(void)va_arg(ap, int);
1104 af = va_arg(ap, int);
1105
1106 switch (af) {
1107 case AF_INET:
1108 type = T_A;
1109 break;
1110 case AF_INET6:
1111 type = T_AAAA;
1112 break;
1113 default:
1114 return NS_UNAVAIL;
1115 }
1116 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
1117 dprintf("res_search failed (%d)\n", n);
1118 return NS_NOTFOUND;
1119 }
1120 hp = getanswer(&buf, n, name, type);
1121 if (hp == NULL)
1122 switch (h_errno) {
1123 case HOST_NOT_FOUND:
1124 return NS_NOTFOUND;
1125 case TRY_AGAIN:
1126 return NS_TRYAGAIN;
1127 default:
1128 return NS_UNAVAIL;
1129 }
1130 *((struct hostent **)rv) = hp;
1131 return NS_SUCCESS;
1132 }
1133
1134 /*ARGSUSED*/
1135 int
1136 _dns_gethtbyaddr(rv, cb_data, ap)
1137 void *rv;
1138 void *cb_data;
1139 va_list ap;
1140 {
1141 char qbuf[MAXDNAME + 1], *qp;
1142 int n;
1143 querybuf buf;
1144 struct hostent *hp;
1145 const unsigned char *uaddr;
1146 int len, af;
1147
1148 _DIAGASSERT(rv != NULL);
1149
1150 uaddr = va_arg(ap, unsigned char *);
1151 len = va_arg(ap, int);
1152 af = va_arg(ap, int);
1153
1154 switch (af) {
1155 case AF_INET:
1156 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
1157 (uaddr[3] & 0xff),
1158 (uaddr[2] & 0xff),
1159 (uaddr[1] & 0xff),
1160 (uaddr[0] & 0xff));
1161 break;
1162
1163 case AF_INET6:
1164 qp = qbuf;
1165 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1166 qp += sprintf(qp, "%x.%x.",
1167 uaddr[n] & 0xf,
1168 ((unsigned int)uaddr[n] >> 4) & 0xf);
1169 }
1170 strcpy(qp, "ip6.int");
1171 break;
1172 default:
1173 abort();
1174 }
1175
1176 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
1177 if (n < 0) {
1178 dprintf("res_query failed (%d)\n", n);
1179 return NS_NOTFOUND;
1180 }
1181 hp = getanswer(&buf, n, qbuf, T_PTR);
1182 if (hp == NULL)
1183 switch (h_errno) {
1184 case HOST_NOT_FOUND:
1185 return NS_NOTFOUND;
1186 case TRY_AGAIN:
1187 return NS_TRYAGAIN;
1188 default:
1189 return NS_UNAVAIL;
1190 }
1191 hp->h_addrtype = af;
1192 hp->h_length = len;
1193 (void)memcpy(host_addr, uaddr, (size_t)len);
1194 h_addr_ptrs[0] = (char *)host_addr;
1195 h_addr_ptrs[1] = (char *)0;
1196 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1197 map_v4v6_address((char *)host_addr, (char *)host_addr);
1198 hp->h_addrtype = AF_INET6;
1199 hp->h_length = IN6ADDRSZ;
1200 }
1201
1202 *((struct hostent **)rv) = hp;
1203 h_errno = NETDB_SUCCESS;
1204 return NS_SUCCESS;
1205 }
1206
1207 #ifdef YP
1208 /*ARGSUSED*/
1209 struct hostent *
1210 _yphostent(line, af)
1211 char *line;
1212 int af;
1213 {
1214 static struct in_addr host_addrs[MAXADDRS];
1215 char *p = line;
1216 char *cp, **q;
1217 char **hap;
1218 struct in_addr *buf;
1219 int more;
1220
1221 _DIAGASSERT(line != NULL);
1222
1223 host.h_name = NULL;
1224 host.h_addr_list = h_addr_ptrs;
1225 host.h_length = sizeof(u_int32_t);
1226 host.h_addrtype = AF_INET;
1227 hap = h_addr_ptrs;
1228 buf = host_addrs;
1229 q = host.h_aliases = host_aliases;
1230
1231 /*
1232 * XXX: maybe support IPv6 parsing, based on 'af' setting
1233 */
1234 nextline:
1235 more = 0;
1236 cp = strpbrk(p, " \t");
1237 if (cp == NULL) {
1238 if (host.h_name == NULL)
1239 return (NULL);
1240 else
1241 goto done;
1242 }
1243 *cp++ = '\0';
1244
1245 *hap++ = (char *)(void *)buf;
1246 (void) inet_aton(p, buf++);
1247
1248 while (*cp == ' ' || *cp == '\t')
1249 cp++;
1250 p = cp;
1251 cp = strpbrk(p, " \t\n");
1252 if (cp != NULL) {
1253 if (*cp == '\n')
1254 more = 1;
1255 *cp++ = '\0';
1256 }
1257 if (!host.h_name)
1258 host.h_name = p;
1259 else if (strcmp(host.h_name, p)==0)
1260 ;
1261 else if (q < &host_aliases[MAXALIASES - 1])
1262 *q++ = p;
1263 p = cp;
1264 if (more)
1265 goto nextline;
1266
1267 while (cp && *cp) {
1268 if (*cp == ' ' || *cp == '\t') {
1269 cp++;
1270 continue;
1271 }
1272 if (*cp == '\n') {
1273 cp++;
1274 goto nextline;
1275 }
1276 if (q < &host_aliases[MAXALIASES - 1])
1277 *q++ = cp;
1278 cp = strpbrk(cp, " \t");
1279 if (cp != NULL)
1280 *cp++ = '\0';
1281 }
1282 done:
1283 *q = NULL;
1284 *hap = NULL;
1285 return (&host);
1286 }
1287
1288 /*ARGSUSED*/
1289 int
1290 _yp_gethtbyaddr(rv, cb_data, ap)
1291 void *rv;
1292 void *cb_data;
1293 va_list ap;
1294 {
1295 struct hostent *hp = (struct hostent *)NULL;
1296 static char *__ypcurrent;
1297 int __ypcurrentlen, r;
1298 char name[sizeof("xxx.xxx.xxx.xxx") + 1];
1299 const unsigned char *uaddr;
1300 int af;
1301
1302 _DIAGASSERT(rv != NULL);
1303
1304 uaddr = va_arg(ap, unsigned char *);
1305 /* NOSTRICT skip len */(void)va_arg(ap, int);
1306 af = va_arg(ap, int);
1307
1308 if (!__ypdomain) {
1309 if (_yp_check(&__ypdomain) == 0)
1310 return NS_UNAVAIL;
1311 }
1312 /*
1313 * XXX: based on the value of af, it would be possible to lookup
1314 * IPv6 names in YP, by changing the following snprintf().
1315 * Is it worth it?
1316 */
1317 (void)snprintf(name, sizeof name, "%u.%u.%u.%u",
1318 (uaddr[0] & 0xff),
1319 (uaddr[1] & 0xff),
1320 (uaddr[2] & 0xff),
1321 (uaddr[3] & 0xff));
1322 if (__ypcurrent)
1323 free(__ypcurrent);
1324 __ypcurrent = NULL;
1325 r = yp_match(__ypdomain, "hosts.byaddr", name,
1326 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1327 if (r==0)
1328 hp = _yphostent(__ypcurrent, af);
1329 if (hp==NULL) {
1330 h_errno = HOST_NOT_FOUND;
1331 return NS_NOTFOUND;
1332 }
1333 *((struct hostent **)rv) = hp;
1334 return NS_SUCCESS;
1335 }
1336
1337 /*ARGSUSED*/
1338 int
1339 _yp_gethtbyname(rv, cb_data, ap)
1340 void *rv;
1341 void *cb_data;
1342 va_list ap;
1343 {
1344 struct hostent *hp = (struct hostent *)NULL;
1345 static char *__ypcurrent;
1346 int __ypcurrentlen, r;
1347 const char *name;
1348 int af;
1349
1350 _DIAGASSERT(rv != NULL);
1351
1352 name = va_arg(ap, char *);
1353 /* NOSTRICT skip len */(void)va_arg(ap, int);
1354 af = va_arg(ap, int);
1355
1356 if (!__ypdomain) {
1357 if (_yp_check(&__ypdomain) == 0)
1358 return NS_UNAVAIL;
1359 }
1360 if (__ypcurrent)
1361 free(__ypcurrent);
1362 __ypcurrent = NULL;
1363 r = yp_match(__ypdomain, "hosts.byname", name,
1364 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1365 if (r==0)
1366 hp = _yphostent(__ypcurrent, af);
1367 if (hp==NULL) {
1368 h_errno = HOST_NOT_FOUND;
1369 return NS_NOTFOUND;
1370 }
1371 *((struct hostent **)rv) = hp;
1372 return NS_SUCCESS;
1373 }
1374 #endif
1375