gethnamaddr.c revision 1.15 1 /* $NetBSD: gethnamaddr.c,v 1.15 1999/04/18 02:27:53 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.15 1999/04/18 02:27:53 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/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 h_addr_ptrs[0] = (char *)host_addr;
731 h_addr_ptrs[1] = NULL;
732 host.h_addr_list = h_addr_ptrs;
733 host.h_length = len;
734 host.h_addrtype = af;
735 while (*cp == ' ' || *cp == '\t')
736 cp++;
737 host.h_name = cp;
738 q = host.h_aliases = host_aliases;
739 if ((cp = strpbrk(cp, " \t")) != NULL)
740 *cp++ = '\0';
741 while (cp && *cp) {
742 if (*cp == ' ' || *cp == '\t') {
743 cp++;
744 continue;
745 }
746 if (q < &host_aliases[MAXALIASES - 1])
747 *q++ = cp;
748 if ((cp = strpbrk(cp, " \t")) != NULL)
749 *cp++ = '\0';
750 }
751 *q = NULL;
752 h_errno = NETDB_SUCCESS;
753 return (&host);
754 }
755
756 /*ARGSUSED*/
757 int
758 _gethtbyname(rv, cb_data, ap)
759 void *rv;
760 void *cb_data;
761 va_list ap;
762 {
763 struct hostent *hp;
764 const char *name;
765 #ifdef notyet
766 int len, af;
767 #endif
768
769 name = va_arg(ap, char *);
770 #ifdef notyet
771 len = va_arg(ap, int);
772 af = va_arg(ap, int);
773 #endif
774
775 hp = NULL;
776 if (_res.options & RES_USE_INET6)
777 hp = _gethtbyname2(name, AF_INET6);
778 if (hp==NULL)
779 hp = _gethtbyname2(name, AF_INET);
780 *((struct hostent **)rv) = hp;
781 if (hp==NULL) {
782 h_errno = HOST_NOT_FOUND;
783 return NS_NOTFOUND;
784 }
785 return NS_SUCCESS;
786 }
787
788 struct hostent *
789 _gethtbyname2(name, af)
790 const char *name;
791 int af;
792 {
793 register struct hostent *p;
794 register char **cp;
795
796 _sethtent(0);
797 while ((p = _gethtent()) != NULL) {
798 if (p->h_addrtype != af)
799 continue;
800 if (strcasecmp(p->h_name, name) == 0)
801 break;
802 for (cp = p->h_aliases; *cp != 0; cp++)
803 if (strcasecmp(*cp, name) == 0)
804 goto found;
805 }
806 found:
807 _endhtent();
808 return (p);
809 }
810
811 /*ARGSUSED*/
812 int
813 _gethtbyaddr(rv, cb_data, ap)
814 void *rv;
815 void *cb_data;
816 va_list ap;
817 {
818 register struct hostent *p;
819 const unsigned char *addr;
820 int len, af;
821
822 addr = va_arg(ap, unsigned char *);
823 len = va_arg(ap, int);
824 af = va_arg(ap, int);
825
826
827 _sethtent(0);
828 while ((p = _gethtent()) != NULL)
829 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
830 (size_t)len))
831 break;
832 _endhtent();
833 *((struct hostent **)rv) = p;
834 if (p==NULL) {
835 h_errno = HOST_NOT_FOUND;
836 return NS_NOTFOUND;
837 }
838 return NS_SUCCESS;
839 }
840
841 static void
842 map_v4v6_address(src, dst)
843 const char *src;
844 char *dst;
845 {
846 u_char *p = (u_char *)dst;
847 char tmp[INADDRSZ];
848 int i;
849
850 /* Stash a temporary copy so our caller can update in place. */
851 (void)memcpy(tmp, src, INADDRSZ);
852 /* Mark this ipv6 addr as a mapped ipv4. */
853 for (i = 0; i < 10; i++)
854 *p++ = 0x00;
855 *p++ = 0xff;
856 *p++ = 0xff;
857 /* Retrieve the saved copy and we're done. */
858 (void)memcpy((void *)p, tmp, INADDRSZ);
859 }
860
861 static void
862 map_v4v6_hostent(hp, bpp, lenp)
863 struct hostent *hp;
864 char **bpp;
865 int *lenp;
866 {
867 char **ap;
868
869 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
870 return;
871 hp->h_addrtype = AF_INET6;
872 hp->h_length = IN6ADDRSZ;
873 for (ap = hp->h_addr_list; *ap; ap++) {
874 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
875
876 if (*lenp < (i + IN6ADDRSZ)) {
877 /* Out of memory. Truncate address list here. XXX */
878 *ap = NULL;
879 return;
880 }
881 *bpp += i;
882 *lenp -= i;
883 map_v4v6_address(*ap, *bpp);
884 *ap = *bpp;
885 *bpp += IN6ADDRSZ;
886 *lenp -= IN6ADDRSZ;
887 }
888 }
889
890 #ifdef RESOLVSORT
891 static void
892 addrsort(ap, num)
893 char **ap;
894 int num;
895 {
896 int i, j;
897 char **p;
898 short aval[MAXADDRS];
899 int needsort = 0;
900
901 p = ap;
902 for (i = 0; i < num; i++, p++) {
903 for (j = 0 ; (unsigned)j < _res.nsort; j++)
904 if (_res.sort_list[j].addr.s_addr ==
905 (((struct in_addr *)(void *)(*p))->s_addr &
906 _res.sort_list[j].mask))
907 break;
908 aval[i] = j;
909 if (needsort == 0 && i > 0 && j < aval[i-1])
910 needsort = i;
911 }
912 if (!needsort)
913 return;
914
915 while (needsort < num) {
916 for (j = needsort - 1; j >= 0; j--) {
917 if (aval[j] > aval[j+1]) {
918 char *hp;
919
920 i = aval[j];
921 aval[j] = aval[j+1];
922 aval[j+1] = i;
923
924 hp = ap[j];
925 ap[j] = ap[j+1];
926 ap[j+1] = hp;
927
928 } else
929 break;
930 }
931 needsort++;
932 }
933 }
934 #endif
935
936 #if defined(BSD43_BSD43_NFS) || defined(sun)
937 /* some libc's out there are bound internally to these names (UMIPS) */
938 void
939 ht_sethostent(stayopen)
940 int stayopen;
941 {
942 _sethtent(stayopen);
943 }
944
945 void
946 ht_endhostent()
947 {
948 _endhtent();
949 }
950
951 struct hostent *
952 ht_gethostbyname(name)
953 char *name;
954 {
955 return (_gethtbyname(name));
956 }
957
958 struct hostent *
959 ht_gethostbyaddr(addr, len, af)
960 const char *addr;
961 int len, af;
962 {
963 return (_gethtbyaddr(addr, len, af));
964 }
965
966 struct hostent *
967 gethostent()
968 {
969 return (_gethtent());
970 }
971
972 void
973 dns_service()
974 {
975 return;
976 }
977
978 #undef dn_skipname
979 dn_skipname(comp_dn, eom)
980 const u_char *comp_dn, *eom;
981 {
982 return (__dn_skipname(comp_dn, eom));
983 }
984 #endif /*old-style libc with yp junk in it*/
985
986 /*ARGSUSED*/
987 int
988 _dns_gethtbyname(rv, cb_data, ap)
989 void *rv;
990 void *cb_data;
991 va_list ap;
992 {
993 querybuf buf;
994 int n, type;
995 struct hostent *hp;
996 const char *name;
997 int len, af;
998
999 name = va_arg(ap, char *);
1000 len = va_arg(ap, int);
1001 #if defined(__GNUC__) || defined(lint) /* to shut up gcc warnings */
1002 /*LINTED*/
1003 (void)(len ? &len : &len);
1004 #endif
1005 af = va_arg(ap, int);
1006
1007 switch (af) {
1008 case AF_INET:
1009 type = T_A;
1010 break;
1011 case AF_INET6:
1012 type = T_AAAA;
1013 break;
1014 default:
1015 return NS_UNAVAIL;
1016 }
1017 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
1018 dprintf("res_search failed (%d)\n", n);
1019 return NS_NOTFOUND;
1020 }
1021 hp = getanswer(&buf, n, name, type);
1022 if (hp == NULL)
1023 switch (h_errno) {
1024 case HOST_NOT_FOUND:
1025 return NS_NOTFOUND;
1026 case TRY_AGAIN:
1027 return NS_TRYAGAIN;
1028 default:
1029 return NS_UNAVAIL;
1030 }
1031 *((struct hostent **)rv) = hp;
1032 return NS_SUCCESS;
1033 }
1034
1035 /*ARGSUSED*/
1036 int
1037 _dns_gethtbyaddr(rv, cb_data, ap)
1038 void *rv;
1039 void *cb_data;
1040 va_list ap;
1041 {
1042 char qbuf[MAXDNAME + 1], *qp;
1043 int n;
1044 querybuf buf;
1045 struct hostent *hp;
1046 const unsigned char *uaddr;
1047 int len, af;
1048
1049 uaddr = va_arg(ap, unsigned char *);
1050 len = va_arg(ap, int);
1051 #ifdef __GNUC__ /* to shut up gcc warnings */
1052 (void)&len;
1053 #endif
1054 af = va_arg(ap, int);
1055
1056 switch (af) {
1057 case AF_INET:
1058 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
1059 (uaddr[3] & 0xff),
1060 (uaddr[2] & 0xff),
1061 (uaddr[1] & 0xff),
1062 (uaddr[0] & 0xff));
1063 break;
1064
1065 case AF_INET6:
1066 qp = qbuf;
1067 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1068 qp += sprintf(qp, "%x.%x.",
1069 uaddr[n] & 0xf,
1070 ((unsigned int)uaddr[n] >> 4) & 0xf);
1071 }
1072 strcpy(qp, "ip6.int");
1073 break;
1074 default:
1075 abort();
1076 }
1077
1078 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
1079 if (n < 0) {
1080 dprintf("res_query failed (%d)\n", n);
1081 return NS_NOTFOUND;
1082 }
1083 hp = getanswer(&buf, n, qbuf, T_PTR);
1084 if (hp == NULL)
1085 switch (h_errno) {
1086 case HOST_NOT_FOUND:
1087 return NS_NOTFOUND;
1088 case TRY_AGAIN:
1089 return NS_TRYAGAIN;
1090 default:
1091 return NS_UNAVAIL;
1092 }
1093 hp->h_addrtype = af;
1094 hp->h_length = len;
1095 (void)memcpy(host_addr, uaddr, (size_t)len);
1096 h_addr_ptrs[0] = (char *)&host_addr;
1097 h_addr_ptrs[1] = (char *)0;
1098 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1099 map_v4v6_address((char*)host_addr, (char*)host_addr);
1100 hp->h_addrtype = AF_INET6;
1101 hp->h_length = IN6ADDRSZ;
1102 }
1103
1104 *((struct hostent **)rv) = hp;
1105 h_errno = NETDB_SUCCESS;
1106 return NS_SUCCESS;
1107 }
1108
1109 #ifdef YP
1110 /*ARGSUSED*/
1111 struct hostent *
1112 _yphostent(line, af)
1113 char *line;
1114 int af;
1115 {
1116 static struct in_addr host_addrs[MAXADDRS];
1117 char *p = line;
1118 char *cp, **q;
1119 char **hap;
1120 struct in_addr *buf;
1121 int more;
1122
1123 host.h_name = NULL;
1124 host.h_addr_list = h_addr_ptrs;
1125 host.h_length = sizeof(u_int32_t);
1126 host.h_addrtype = AF_INET;
1127 hap = h_addr_ptrs;
1128 buf = host_addrs;
1129 q = host.h_aliases = host_aliases;
1130
1131 /*
1132 * XXX: maybe support IPv6 parsing, based on 'af' setting
1133 */
1134 nextline:
1135 more = 0;
1136 cp = strpbrk(p, " \t");
1137 if (cp == NULL) {
1138 if (host.h_name == NULL)
1139 return (NULL);
1140 else
1141 goto done;
1142 }
1143 *cp++ = '\0';
1144
1145 *hap++ = (char *)(void *)buf;
1146 (void) inet_aton(p, buf++);
1147
1148 while (*cp == ' ' || *cp == '\t')
1149 cp++;
1150 p = cp;
1151 cp = strpbrk(p, " \t\n");
1152 if (cp != NULL) {
1153 if (*cp == '\n')
1154 more = 1;
1155 *cp++ = '\0';
1156 }
1157 if (!host.h_name)
1158 host.h_name = p;
1159 else if (strcmp(host.h_name, p)==0)
1160 ;
1161 else if (q < &host_aliases[MAXALIASES - 1])
1162 *q++ = p;
1163 p = cp;
1164 if (more)
1165 goto nextline;
1166
1167 while (cp && *cp) {
1168 if (*cp == ' ' || *cp == '\t') {
1169 cp++;
1170 continue;
1171 }
1172 if (*cp == '\n') {
1173 cp++;
1174 goto nextline;
1175 }
1176 if (q < &host_aliases[MAXALIASES - 1])
1177 *q++ = cp;
1178 cp = strpbrk(cp, " \t");
1179 if (cp != NULL)
1180 *cp++ = '\0';
1181 }
1182 done:
1183 *q = NULL;
1184 *hap = NULL;
1185 return (&host);
1186 }
1187
1188 /*ARGSUSED*/
1189 int
1190 _yp_gethtbyaddr(rv, cb_data, ap)
1191 void *rv;
1192 void *cb_data;
1193 va_list ap;
1194 {
1195 struct hostent *hp = (struct hostent *)NULL;
1196 static char *__ypcurrent;
1197 int __ypcurrentlen, r;
1198 char name[sizeof("xxx.xxx.xxx.xxx") + 1];
1199 const unsigned char *uaddr;
1200 int len, af;
1201
1202 uaddr = va_arg(ap, unsigned char *);
1203 len = va_arg(ap, int);
1204 #if defined(__GNUC__) || defined(lint) /* to shut up gcc warnings */
1205 /*LINTED*/
1206 (void)(len ? &len : &len);
1207 #endif
1208 af = va_arg(ap, int);
1209
1210 if (!__ypdomain) {
1211 if (_yp_check(&__ypdomain) == 0)
1212 return NS_UNAVAIL;
1213 }
1214 /*
1215 * XXX: based on the value of af, it would be possible to lookup
1216 * IPv6 names in YP, by changing the following snprintf().
1217 * Is it worth it?
1218 */
1219 (void)snprintf(name, sizeof name, "%u.%u.%u.%u",
1220 (uaddr[0] & 0xff),
1221 (uaddr[1] & 0xff),
1222 (uaddr[2] & 0xff),
1223 (uaddr[3] & 0xff));
1224 if (__ypcurrent)
1225 free(__ypcurrent);
1226 __ypcurrent = NULL;
1227 r = yp_match(__ypdomain, "hosts.byaddr", name,
1228 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1229 if (r==0)
1230 hp = _yphostent(__ypcurrent, af);
1231 if (hp==NULL) {
1232 h_errno = HOST_NOT_FOUND;
1233 return NS_NOTFOUND;
1234 }
1235 *((struct hostent **)rv) = hp;
1236 return NS_SUCCESS;
1237 }
1238
1239 /*ARGSUSED*/
1240 int
1241 _yp_gethtbyname(rv, cb_data, ap)
1242 void *rv;
1243 void *cb_data;
1244 va_list ap;
1245 {
1246 struct hostent *hp = (struct hostent *)NULL;
1247 static char *__ypcurrent;
1248 int __ypcurrentlen, r;
1249 const char *name;
1250 int len, af;
1251
1252 name = va_arg(ap, char *);
1253 len = va_arg(ap, int);
1254 #if defined(__GNUC__) || defined(lint) /* to shut up gcc warnings */
1255 /*LINTED*/
1256 (void)(len ? &len : &len);
1257 #endif
1258 af = va_arg(ap, int);
1259
1260 if (!__ypdomain) {
1261 if (_yp_check(&__ypdomain) == 0)
1262 return NS_UNAVAIL;
1263 }
1264 if (__ypcurrent)
1265 free(__ypcurrent);
1266 __ypcurrent = NULL;
1267 r = yp_match(__ypdomain, "hosts.byname", name,
1268 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1269 if (r==0)
1270 hp = _yphostent(__ypcurrent, af);
1271 if (hp==NULL) {
1272 h_errno = HOST_NOT_FOUND;
1273 return NS_NOTFOUND;
1274 }
1275 *((struct hostent **)rv) = hp;
1276 return NS_SUCCESS;
1277 }
1278 #endif
1279