gethnamaddr.c revision 1.23 1 /* $NetBSD: gethnamaddr.c,v 1.23 1999/07/19 19:42:27 christos 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.23 1999/07/19 19:42:27 christos 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_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_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,
557 (char *)host_addr) <= 0) {
558 h_errno = HOST_NOT_FOUND;
559 return (NULL);
560 }
561 strncpy(hostbuf, name, MAXDNAME);
562 hostbuf[MAXDNAME] = '\0';
563 bp = hostbuf + MAXDNAME;
564 len = sizeof hostbuf - MAXDNAME;
565 host.h_name = hostbuf;
566 host.h_aliases = host_aliases;
567 host_aliases[0] = NULL;
568 h_addr_ptrs[0] = (char *)host_addr;
569 h_addr_ptrs[1] = NULL;
570 host.h_addr_list = h_addr_ptrs;
571 if (_res.options & RES_USE_INET6)
572 map_v4v6_hostent(&host, &bp, &len);
573 h_errno = NETDB_SUCCESS;
574 return (&host);
575 }
576 if (!isdigit(*cp) && *cp != '.')
577 break;
578 }
579 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
580 name[0] == ':')
581 for (cp = name;; ++cp) {
582 if (!*cp) {
583 if (*--cp == '.')
584 break;
585 /*
586 * All-IPv6-legal, no dot at the end.
587 * Fake up a hostent as if we'd actually
588 * done a lookup.
589 */
590 if (inet_pton(af, name,
591 (char *)host_addr) <= 0) {
592 h_errno = HOST_NOT_FOUND;
593 return (NULL);
594 }
595 strncpy(hostbuf, name, MAXDNAME);
596 hostbuf[MAXDNAME] = '\0';
597 bp = hostbuf + MAXDNAME;
598 len = sizeof hostbuf - MAXDNAME;
599 host.h_name = hostbuf;
600 host.h_aliases = host_aliases;
601 host_aliases[0] = NULL;
602 h_addr_ptrs[0] = (char *)host_addr;
603 h_addr_ptrs[1] = NULL;
604 host.h_addr_list = h_addr_ptrs;
605 h_errno = NETDB_SUCCESS;
606 return (&host);
607 }
608 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
609 break;
610 }
611
612 hp = (struct hostent *)NULL;
613 h_errno = NETDB_INTERNAL;
614 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname",
615 default_dns_files, name, len, af) != NS_SUCCESS)
616 return (struct hostent *)NULL;
617 h_errno = NETDB_SUCCESS;
618 return (hp);
619 }
620
621 struct hostent *
622 gethostbyaddr(addr, len, af)
623 const char *addr; /* XXX should have been def'd as u_char! */
624 int len, af;
625 {
626 const u_char *uaddr = (const u_char *)addr;
627 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
628 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
629 int size;
630 struct hostent *hp;
631 static const ns_dtab dtab[] = {
632 NS_FILES_CB(_gethtbyaddr, NULL)
633 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
634 NS_NIS_CB(_yp_gethtbyaddr, NULL)
635 { 0 }
636 };
637
638 if (af == AF_INET6 && len == IN6ADDRSZ &&
639 (!memcmp(uaddr, mapped, sizeof mapped) ||
640 !memcmp(uaddr, tunnelled, sizeof tunnelled))) {
641 /* Unmap. */
642 addr += sizeof mapped;
643 uaddr += sizeof mapped;
644 af = AF_INET;
645 len = INADDRSZ;
646 }
647 switch (af) {
648 case AF_INET:
649 size = INADDRSZ;
650 break;
651 case AF_INET6:
652 size = IN6ADDRSZ;
653 break;
654 default:
655 errno = EAFNOSUPPORT;
656 h_errno = NETDB_INTERNAL;
657 return (NULL);
658 }
659 if (size != len) {
660 errno = EINVAL;
661 h_errno = NETDB_INTERNAL;
662 return (NULL);
663 }
664 hp = (struct hostent *)NULL;
665 h_errno = NETDB_INTERNAL;
666 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
667 default_dns_files, uaddr, len, af) != NS_SUCCESS)
668 return (struct hostent *)NULL;
669 h_errno = NETDB_SUCCESS;
670 return (hp);
671 }
672
673 void
674 _sethtent(f)
675 int f;
676 {
677 if (!hostf)
678 hostf = fopen(_PATH_HOSTS, "r" );
679 else
680 rewind(hostf);
681 stayopen = f;
682 }
683
684 void
685 _endhtent()
686 {
687 if (hostf && !stayopen) {
688 (void) fclose(hostf);
689 hostf = NULL;
690 }
691 }
692
693 struct hostent *
694 _gethtent()
695 {
696 char *p;
697 register char *cp, **q;
698 int af, len;
699
700 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
701 h_errno = NETDB_INTERNAL;
702 return (NULL);
703 }
704 again:
705 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
706 h_errno = HOST_NOT_FOUND;
707 return (NULL);
708 }
709 if (*p == '#')
710 goto again;
711 if (!(cp = strpbrk(p, "#\n")))
712 goto again;
713 *cp = '\0';
714 if (!(cp = strpbrk(p, " \t")))
715 goto again;
716 *cp++ = '\0';
717 if (inet_pton(AF_INET6, p, (char *)host_addr) > 0) {
718 af = AF_INET6;
719 len = IN6ADDRSZ;
720 } else if (inet_pton(AF_INET, p, (char *)host_addr) > 0) {
721 if (_res.options & RES_USE_INET6) {
722 map_v4v6_address((char *)host_addr, (char *)host_addr);
723 af = AF_INET6;
724 len = IN6ADDRSZ;
725 } else {
726 af = AF_INET;
727 len = INADDRSZ;
728 }
729 } else {
730 goto again;
731 }
732 /* if this is not something we're looking for, skip it. */
733 if (host.h_addrtype != af)
734 goto again;
735 if (host.h_length != len)
736 goto again;
737 h_addr_ptrs[0] = (char *)host_addr;
738 h_addr_ptrs[1] = NULL;
739 host.h_addr_list = h_addr_ptrs;
740 host.h_length = len;
741 host.h_addrtype = af;
742 while (*cp == ' ' || *cp == '\t')
743 cp++;
744 host.h_name = cp;
745 q = host.h_aliases = host_aliases;
746 if ((cp = strpbrk(cp, " \t")) != NULL)
747 *cp++ = '\0';
748 while (cp && *cp) {
749 if (*cp == ' ' || *cp == '\t') {
750 cp++;
751 continue;
752 }
753 if (q < &host_aliases[MAXALIASES - 1])
754 *q++ = cp;
755 if ((cp = strpbrk(cp, " \t")) != NULL)
756 *cp++ = '\0';
757 }
758 *q = NULL;
759 h_errno = NETDB_SUCCESS;
760 return (&host);
761 }
762
763 /*ARGSUSED*/
764 int
765 _gethtbyname(rv, cb_data, ap)
766 void *rv;
767 void *cb_data;
768 va_list ap;
769 {
770 struct hostent *hp;
771 const char *name;
772 int len, af;
773
774 name = va_arg(ap, char *);
775 len = va_arg(ap, int);
776 af = va_arg(ap, int);
777
778 hp = NULL;
779 #if 0
780 if (_res.options & RES_USE_INET6)
781 hp = _gethtbyname2(name, AF_INET6);
782 if (hp==NULL)
783 hp = _gethtbyname2(name, AF_INET);
784 #else
785 hp = _gethtbyname2(name, af);
786 #endif
787 *((struct hostent **)rv) = hp;
788 if (hp==NULL) {
789 h_errno = HOST_NOT_FOUND;
790 return NS_NOTFOUND;
791 }
792 return NS_SUCCESS;
793 }
794
795 struct hostent *
796 _gethtbyname2(name, af)
797 const char *name;
798 int af;
799 {
800 register struct hostent *p;
801 register char *tmpbuf, *ptr, **cp;
802 register int num;
803 register size_t len;
804
805 _sethtent(0);
806 ptr = tmpbuf = NULL;
807 num = 0;
808 while ((p = _gethtent()) != NULL && num < MAXADDRS) {
809 if (p->h_addrtype != af)
810 continue;
811 if (strcasecmp(p->h_name, name) != 0) {
812 for (cp = p->h_aliases; *cp != NULL; cp++)
813 if (strcasecmp(*cp, name) == 0)
814 break;
815 if (*cp == NULL) continue;
816 }
817
818 if (num == 0) {
819 register size_t bufsize;
820 register char *src;
821
822 bufsize = strlen(p->h_name) + 2 +
823 MAXADDRS * p->h_length +
824 ALIGNBYTES;
825 for (cp = p->h_aliases; *cp != NULL; cp++)
826 bufsize += strlen(*cp) + 1;
827
828 if ((tmpbuf = malloc(bufsize)) == NULL) {
829 h_errno = NETDB_INTERNAL;
830 return NULL;
831 }
832
833 ptr = tmpbuf;
834 src = p->h_name;
835 while ((*ptr++ = *src++) != '\0');
836 for (cp = p->h_aliases; *cp != NULL; cp++) {
837 src = *cp;
838 while ((*ptr++ = *src++) != '\0');
839 }
840 *ptr++ = '\0';
841
842 ptr = (char *)ALIGN(ptr);
843 }
844
845 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
846 ptr += p->h_length;
847 num++;
848 }
849 _endhtent();
850 if (num == 0) return NULL;
851
852 len = ptr - tmpbuf;
853 if (len > sizeof(hostbuf)) {
854 free(tmpbuf);
855 errno = ENOSPC;
856 h_errno = NETDB_INTERNAL;
857 return NULL;
858 }
859 ptr = memcpy(hostbuf, tmpbuf, len);
860 free(tmpbuf);
861
862 host.h_name = ptr;
863 while (*ptr++);
864
865 cp = host_aliases;
866 while (*ptr) {
867 *cp++ = ptr;
868 while (*ptr++);
869 }
870 ptr++;
871 *cp = NULL;
872
873 ptr = (char *)ALIGN(ptr);
874 cp = h_addr_ptrs;
875 while (num--) {
876 *cp++ = ptr;
877 ptr += host.h_length;
878 }
879 *cp = NULL;
880
881 return (&host);
882 }
883
884 /*ARGSUSED*/
885 int
886 _gethtbyaddr(rv, cb_data, ap)
887 void *rv;
888 void *cb_data;
889 va_list ap;
890 {
891 register struct hostent *p;
892 const unsigned char *addr;
893 int len, af;
894
895 addr = va_arg(ap, unsigned char *);
896 len = va_arg(ap, int);
897 af = va_arg(ap, int);
898
899 host.h_length = len;
900 host.h_addrtype = af;
901
902 _sethtent(0);
903 while ((p = _gethtent()) != NULL)
904 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
905 (size_t)len))
906 break;
907 _endhtent();
908 *((struct hostent **)rv) = p;
909 if (p==NULL) {
910 h_errno = HOST_NOT_FOUND;
911 return NS_NOTFOUND;
912 }
913 return NS_SUCCESS;
914 }
915
916 static void
917 map_v4v6_address(src, dst)
918 const char *src;
919 char *dst;
920 {
921 u_char *p = (u_char *)dst;
922 char tmp[INADDRSZ];
923 int i;
924
925 /* Stash a temporary copy so our caller can update in place. */
926 (void)memcpy(tmp, src, INADDRSZ);
927 /* Mark this ipv6 addr as a mapped ipv4. */
928 for (i = 0; i < 10; i++)
929 *p++ = 0x00;
930 *p++ = 0xff;
931 *p++ = 0xff;
932 /* Retrieve the saved copy and we're done. */
933 (void)memcpy((void *)p, tmp, INADDRSZ);
934 }
935
936 static void
937 map_v4v6_hostent(hp, bpp, lenp)
938 struct hostent *hp;
939 char **bpp;
940 int *lenp;
941 {
942 char **ap;
943
944 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
945 return;
946 hp->h_addrtype = AF_INET6;
947 hp->h_length = IN6ADDRSZ;
948 for (ap = hp->h_addr_list; *ap; ap++) {
949 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
950
951 if (*lenp < (i + IN6ADDRSZ)) {
952 /* Out of memory. Truncate address list here. XXX */
953 *ap = NULL;
954 return;
955 }
956 *bpp += i;
957 *lenp -= i;
958 map_v4v6_address(*ap, *bpp);
959 *ap = *bpp;
960 *bpp += IN6ADDRSZ;
961 *lenp -= IN6ADDRSZ;
962 }
963 }
964
965 #ifdef RESOLVSORT
966 static void
967 addrsort(ap, num)
968 char **ap;
969 int num;
970 {
971 int i, j;
972 char **p;
973 short aval[MAXADDRS];
974 int needsort = 0;
975
976 p = ap;
977 for (i = 0; i < num; i++, p++) {
978 for (j = 0 ; (unsigned)j < _res.nsort; j++)
979 if (_res.sort_list[j].addr.s_addr ==
980 (((struct in_addr *)(void *)(*p))->s_addr &
981 _res.sort_list[j].mask))
982 break;
983 aval[i] = j;
984 if (needsort == 0 && i > 0 && j < aval[i-1])
985 needsort = i;
986 }
987 if (!needsort)
988 return;
989
990 while (needsort < num) {
991 for (j = needsort - 1; j >= 0; j--) {
992 if (aval[j] > aval[j+1]) {
993 char *hp;
994
995 i = aval[j];
996 aval[j] = aval[j+1];
997 aval[j+1] = i;
998
999 hp = ap[j];
1000 ap[j] = ap[j+1];
1001 ap[j+1] = hp;
1002
1003 } else
1004 break;
1005 }
1006 needsort++;
1007 }
1008 }
1009 #endif
1010
1011 #if defined(BSD43_BSD43_NFS) || defined(sun)
1012 /* some libc's out there are bound internally to these names (UMIPS) */
1013 void
1014 ht_sethostent(stayopen)
1015 int stayopen;
1016 {
1017 _sethtent(stayopen);
1018 }
1019
1020 void
1021 ht_endhostent()
1022 {
1023 _endhtent();
1024 }
1025
1026 struct hostent *
1027 ht_gethostbyname(name)
1028 char *name;
1029 {
1030 return (_gethtbyname(name));
1031 }
1032
1033 struct hostent *
1034 ht_gethostbyaddr(addr, len, af)
1035 const char *addr;
1036 int len, af;
1037 {
1038 return (_gethtbyaddr(addr, len, af));
1039 }
1040
1041 struct hostent *
1042 gethostent()
1043 {
1044 return (_gethtent());
1045 }
1046
1047 void
1048 dns_service()
1049 {
1050 return;
1051 }
1052
1053 #undef dn_skipname
1054 dn_skipname(comp_dn, eom)
1055 const u_char *comp_dn, *eom;
1056 {
1057 return (__dn_skipname(comp_dn, eom));
1058 }
1059 #endif /*old-style libc with yp junk in it*/
1060
1061 /*ARGSUSED*/
1062 int
1063 _dns_gethtbyname(rv, cb_data, ap)
1064 void *rv;
1065 void *cb_data;
1066 va_list ap;
1067 {
1068 querybuf buf;
1069 int n, type;
1070 struct hostent *hp;
1071 const char *name;
1072 int af;
1073
1074 name = va_arg(ap, char *);
1075 /* NOSTRICT skip len */(void)va_arg(ap, int);
1076 af = va_arg(ap, int);
1077
1078 switch (af) {
1079 case AF_INET:
1080 type = T_A;
1081 break;
1082 case AF_INET6:
1083 type = T_AAAA;
1084 break;
1085 default:
1086 return NS_UNAVAIL;
1087 }
1088 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
1089 dprintf("res_search failed (%d)\n", n);
1090 return NS_NOTFOUND;
1091 }
1092 hp = getanswer(&buf, n, name, type);
1093 if (hp == NULL)
1094 switch (h_errno) {
1095 case HOST_NOT_FOUND:
1096 return NS_NOTFOUND;
1097 case TRY_AGAIN:
1098 return NS_TRYAGAIN;
1099 default:
1100 return NS_UNAVAIL;
1101 }
1102 *((struct hostent **)rv) = hp;
1103 return NS_SUCCESS;
1104 }
1105
1106 /*ARGSUSED*/
1107 int
1108 _dns_gethtbyaddr(rv, cb_data, ap)
1109 void *rv;
1110 void *cb_data;
1111 va_list ap;
1112 {
1113 char qbuf[MAXDNAME + 1], *qp;
1114 int n;
1115 querybuf buf;
1116 struct hostent *hp;
1117 const unsigned char *uaddr;
1118 int len, af;
1119
1120 uaddr = va_arg(ap, unsigned char *);
1121 len = va_arg(ap, int);
1122 af = va_arg(ap, int);
1123
1124 switch (af) {
1125 case AF_INET:
1126 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
1127 (uaddr[3] & 0xff),
1128 (uaddr[2] & 0xff),
1129 (uaddr[1] & 0xff),
1130 (uaddr[0] & 0xff));
1131 break;
1132
1133 case AF_INET6:
1134 qp = qbuf;
1135 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1136 qp += sprintf(qp, "%x.%x.",
1137 uaddr[n] & 0xf,
1138 ((unsigned int)uaddr[n] >> 4) & 0xf);
1139 }
1140 strcpy(qp, "ip6.int");
1141 break;
1142 default:
1143 abort();
1144 }
1145
1146 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
1147 if (n < 0) {
1148 dprintf("res_query failed (%d)\n", n);
1149 return NS_NOTFOUND;
1150 }
1151 hp = getanswer(&buf, n, qbuf, T_PTR);
1152 if (hp == NULL)
1153 switch (h_errno) {
1154 case HOST_NOT_FOUND:
1155 return NS_NOTFOUND;
1156 case TRY_AGAIN:
1157 return NS_TRYAGAIN;
1158 default:
1159 return NS_UNAVAIL;
1160 }
1161 hp->h_addrtype = af;
1162 hp->h_length = len;
1163 (void)memcpy(host_addr, uaddr, (size_t)len);
1164 h_addr_ptrs[0] = (char *)host_addr;
1165 h_addr_ptrs[1] = (char *)0;
1166 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1167 map_v4v6_address((char *)host_addr, (char *)host_addr);
1168 hp->h_addrtype = AF_INET6;
1169 hp->h_length = IN6ADDRSZ;
1170 }
1171
1172 *((struct hostent **)rv) = hp;
1173 h_errno = NETDB_SUCCESS;
1174 return NS_SUCCESS;
1175 }
1176
1177 #ifdef YP
1178 /*ARGSUSED*/
1179 struct hostent *
1180 _yphostent(line, af)
1181 char *line;
1182 int af;
1183 {
1184 static struct in_addr host_addrs[MAXADDRS];
1185 char *p = line;
1186 char *cp, **q;
1187 char **hap;
1188 struct in_addr *buf;
1189 int more;
1190
1191 host.h_name = NULL;
1192 host.h_addr_list = h_addr_ptrs;
1193 host.h_length = sizeof(u_int32_t);
1194 host.h_addrtype = AF_INET;
1195 hap = h_addr_ptrs;
1196 buf = host_addrs;
1197 q = host.h_aliases = host_aliases;
1198
1199 /*
1200 * XXX: maybe support IPv6 parsing, based on 'af' setting
1201 */
1202 nextline:
1203 more = 0;
1204 cp = strpbrk(p, " \t");
1205 if (cp == NULL) {
1206 if (host.h_name == NULL)
1207 return (NULL);
1208 else
1209 goto done;
1210 }
1211 *cp++ = '\0';
1212
1213 *hap++ = (char *)(void *)buf;
1214 (void) inet_aton(p, buf++);
1215
1216 while (*cp == ' ' || *cp == '\t')
1217 cp++;
1218 p = cp;
1219 cp = strpbrk(p, " \t\n");
1220 if (cp != NULL) {
1221 if (*cp == '\n')
1222 more = 1;
1223 *cp++ = '\0';
1224 }
1225 if (!host.h_name)
1226 host.h_name = p;
1227 else if (strcmp(host.h_name, p)==0)
1228 ;
1229 else if (q < &host_aliases[MAXALIASES - 1])
1230 *q++ = p;
1231 p = cp;
1232 if (more)
1233 goto nextline;
1234
1235 while (cp && *cp) {
1236 if (*cp == ' ' || *cp == '\t') {
1237 cp++;
1238 continue;
1239 }
1240 if (*cp == '\n') {
1241 cp++;
1242 goto nextline;
1243 }
1244 if (q < &host_aliases[MAXALIASES - 1])
1245 *q++ = cp;
1246 cp = strpbrk(cp, " \t");
1247 if (cp != NULL)
1248 *cp++ = '\0';
1249 }
1250 done:
1251 *q = NULL;
1252 *hap = NULL;
1253 return (&host);
1254 }
1255
1256 /*ARGSUSED*/
1257 int
1258 _yp_gethtbyaddr(rv, cb_data, ap)
1259 void *rv;
1260 void *cb_data;
1261 va_list ap;
1262 {
1263 struct hostent *hp = (struct hostent *)NULL;
1264 static char *__ypcurrent;
1265 int __ypcurrentlen, r;
1266 char name[sizeof("xxx.xxx.xxx.xxx") + 1];
1267 const unsigned char *uaddr;
1268 int af;
1269
1270 uaddr = va_arg(ap, unsigned char *);
1271 /* NOSTRICT skip len */(void)va_arg(ap, int);
1272 af = va_arg(ap, int);
1273
1274 if (!__ypdomain) {
1275 if (_yp_check(&__ypdomain) == 0)
1276 return NS_UNAVAIL;
1277 }
1278 /*
1279 * XXX: based on the value of af, it would be possible to lookup
1280 * IPv6 names in YP, by changing the following snprintf().
1281 * Is it worth it?
1282 */
1283 (void)snprintf(name, sizeof name, "%u.%u.%u.%u",
1284 (uaddr[0] & 0xff),
1285 (uaddr[1] & 0xff),
1286 (uaddr[2] & 0xff),
1287 (uaddr[3] & 0xff));
1288 if (__ypcurrent)
1289 free(__ypcurrent);
1290 __ypcurrent = NULL;
1291 r = yp_match(__ypdomain, "hosts.byaddr", name,
1292 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1293 if (r==0)
1294 hp = _yphostent(__ypcurrent, af);
1295 if (hp==NULL) {
1296 h_errno = HOST_NOT_FOUND;
1297 return NS_NOTFOUND;
1298 }
1299 *((struct hostent **)rv) = hp;
1300 return NS_SUCCESS;
1301 }
1302
1303 /*ARGSUSED*/
1304 int
1305 _yp_gethtbyname(rv, cb_data, ap)
1306 void *rv;
1307 void *cb_data;
1308 va_list ap;
1309 {
1310 struct hostent *hp = (struct hostent *)NULL;
1311 static char *__ypcurrent;
1312 int __ypcurrentlen, r;
1313 const char *name;
1314 int af;
1315
1316 name = va_arg(ap, char *);
1317 /* NOSTRICT skip len */(void)va_arg(ap, int);
1318 af = va_arg(ap, int);
1319
1320 if (!__ypdomain) {
1321 if (_yp_check(&__ypdomain) == 0)
1322 return NS_UNAVAIL;
1323 }
1324 if (__ypcurrent)
1325 free(__ypcurrent);
1326 __ypcurrent = NULL;
1327 r = yp_match(__ypdomain, "hosts.byname", name,
1328 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1329 if (r==0)
1330 hp = _yphostent(__ypcurrent, af);
1331 if (hp==NULL) {
1332 h_errno = HOST_NOT_FOUND;
1333 return NS_NOTFOUND;
1334 }
1335 *((struct hostent **)rv) = hp;
1336 return NS_SUCCESS;
1337 }
1338 #endif
1339