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