gethnamaddr.c revision 1.22 1 /* $NetBSD: gethnamaddr.c,v 1.22 1999/07/19 17:43:59 tron 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.22 1999/07/19 17:43:59 tron 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 ALIGNBYTES;
823 for (cp = p->h_aliases; *cp != NULL; cp++)
824 bufsize += strlen(*cp) + 1;
825
826 if ((tmpbuf = malloc(bufsize)) == NULL) {
827 h_errno = NETDB_INTERNAL;
828 return NULL;
829 }
830
831 ptr = tmpbuf;
832 src = p->h_name;
833 while ((*ptr++ = *src++) != '\0');
834 for (cp = p->h_aliases; *cp != NULL; cp++) {
835 src = *cp;
836 while ((*ptr++ = *src++) != '\0');
837 }
838 *ptr++ = '\0';
839
840 ptr = (char *)ALIGN(ptr);
841 }
842
843 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
844 ptr += p->h_length;
845 num++;
846 }
847 _endhtent();
848 if (num == 0) return NULL;
849
850 len = ptr - tmpbuf;
851 if (len > sizeof(hostbuf)) {
852 free(tmpbuf);
853 errno = ENOSPC;
854 h_errno = NETDB_INTERNAL;
855 return NULL;
856 }
857 ptr = memcpy(hostbuf, tmpbuf, len);
858 free(tmpbuf);
859
860 host.h_name = ptr;
861 while (*ptr++);
862
863 cp = host_aliases;
864 while (*ptr) {
865 *cp++ = ptr;
866 while (*ptr++);
867 }
868 ptr++;
869 *cp = NULL;
870
871 ptr = (char *)ALIGN(ptr);
872 cp = h_addr_ptrs;
873 while (num--) {
874 *cp++ = ptr;
875 ptr += host.h_length;
876 }
877 *cp = NULL;
878
879 return (&host);
880 }
881
882 /*ARGSUSED*/
883 int
884 _gethtbyaddr(rv, cb_data, ap)
885 void *rv;
886 void *cb_data;
887 va_list ap;
888 {
889 register struct hostent *p;
890 const unsigned char *addr;
891 int len, af;
892
893 addr = va_arg(ap, unsigned char *);
894 len = va_arg(ap, int);
895 af = va_arg(ap, int);
896
897 host.h_length = len;
898 host.h_addrtype = af;
899
900 _sethtent(0);
901 while ((p = _gethtent()) != NULL)
902 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
903 (size_t)len))
904 break;
905 _endhtent();
906 *((struct hostent **)rv) = p;
907 if (p==NULL) {
908 h_errno = HOST_NOT_FOUND;
909 return NS_NOTFOUND;
910 }
911 return NS_SUCCESS;
912 }
913
914 static void
915 map_v4v6_address(src, dst)
916 const char *src;
917 char *dst;
918 {
919 u_char *p = (u_char *)dst;
920 char tmp[INADDRSZ];
921 int i;
922
923 /* Stash a temporary copy so our caller can update in place. */
924 (void)memcpy(tmp, src, INADDRSZ);
925 /* Mark this ipv6 addr as a mapped ipv4. */
926 for (i = 0; i < 10; i++)
927 *p++ = 0x00;
928 *p++ = 0xff;
929 *p++ = 0xff;
930 /* Retrieve the saved copy and we're done. */
931 (void)memcpy((void *)p, tmp, INADDRSZ);
932 }
933
934 static void
935 map_v4v6_hostent(hp, bpp, lenp)
936 struct hostent *hp;
937 char **bpp;
938 int *lenp;
939 {
940 char **ap;
941
942 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
943 return;
944 hp->h_addrtype = AF_INET6;
945 hp->h_length = IN6ADDRSZ;
946 for (ap = hp->h_addr_list; *ap; ap++) {
947 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
948
949 if (*lenp < (i + IN6ADDRSZ)) {
950 /* Out of memory. Truncate address list here. XXX */
951 *ap = NULL;
952 return;
953 }
954 *bpp += i;
955 *lenp -= i;
956 map_v4v6_address(*ap, *bpp);
957 *ap = *bpp;
958 *bpp += IN6ADDRSZ;
959 *lenp -= IN6ADDRSZ;
960 }
961 }
962
963 #ifdef RESOLVSORT
964 static void
965 addrsort(ap, num)
966 char **ap;
967 int num;
968 {
969 int i, j;
970 char **p;
971 short aval[MAXADDRS];
972 int needsort = 0;
973
974 p = ap;
975 for (i = 0; i < num; i++, p++) {
976 for (j = 0 ; (unsigned)j < _res.nsort; j++)
977 if (_res.sort_list[j].addr.s_addr ==
978 (((struct in_addr *)(void *)(*p))->s_addr &
979 _res.sort_list[j].mask))
980 break;
981 aval[i] = j;
982 if (needsort == 0 && i > 0 && j < aval[i-1])
983 needsort = i;
984 }
985 if (!needsort)
986 return;
987
988 while (needsort < num) {
989 for (j = needsort - 1; j >= 0; j--) {
990 if (aval[j] > aval[j+1]) {
991 char *hp;
992
993 i = aval[j];
994 aval[j] = aval[j+1];
995 aval[j+1] = i;
996
997 hp = ap[j];
998 ap[j] = ap[j+1];
999 ap[j+1] = hp;
1000
1001 } else
1002 break;
1003 }
1004 needsort++;
1005 }
1006 }
1007 #endif
1008
1009 #if defined(BSD43_BSD43_NFS) || defined(sun)
1010 /* some libc's out there are bound internally to these names (UMIPS) */
1011 void
1012 ht_sethostent(stayopen)
1013 int stayopen;
1014 {
1015 _sethtent(stayopen);
1016 }
1017
1018 void
1019 ht_endhostent()
1020 {
1021 _endhtent();
1022 }
1023
1024 struct hostent *
1025 ht_gethostbyname(name)
1026 char *name;
1027 {
1028 return (_gethtbyname(name));
1029 }
1030
1031 struct hostent *
1032 ht_gethostbyaddr(addr, len, af)
1033 const char *addr;
1034 int len, af;
1035 {
1036 return (_gethtbyaddr(addr, len, af));
1037 }
1038
1039 struct hostent *
1040 gethostent()
1041 {
1042 return (_gethtent());
1043 }
1044
1045 void
1046 dns_service()
1047 {
1048 return;
1049 }
1050
1051 #undef dn_skipname
1052 dn_skipname(comp_dn, eom)
1053 const u_char *comp_dn, *eom;
1054 {
1055 return (__dn_skipname(comp_dn, eom));
1056 }
1057 #endif /*old-style libc with yp junk in it*/
1058
1059 /*ARGSUSED*/
1060 int
1061 _dns_gethtbyname(rv, cb_data, ap)
1062 void *rv;
1063 void *cb_data;
1064 va_list ap;
1065 {
1066 querybuf buf;
1067 int n, type;
1068 struct hostent *hp;
1069 const char *name;
1070 int af;
1071
1072 name = va_arg(ap, char *);
1073 /* NOSTRICT skip len */(void)va_arg(ap, int);
1074 af = va_arg(ap, int);
1075
1076 switch (af) {
1077 case AF_INET:
1078 type = T_A;
1079 break;
1080 case AF_INET6:
1081 type = T_AAAA;
1082 break;
1083 default:
1084 return NS_UNAVAIL;
1085 }
1086 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
1087 dprintf("res_search failed (%d)\n", n);
1088 return NS_NOTFOUND;
1089 }
1090 hp = getanswer(&buf, n, name, type);
1091 if (hp == NULL)
1092 switch (h_errno) {
1093 case HOST_NOT_FOUND:
1094 return NS_NOTFOUND;
1095 case TRY_AGAIN:
1096 return NS_TRYAGAIN;
1097 default:
1098 return NS_UNAVAIL;
1099 }
1100 *((struct hostent **)rv) = hp;
1101 return NS_SUCCESS;
1102 }
1103
1104 /*ARGSUSED*/
1105 int
1106 _dns_gethtbyaddr(rv, cb_data, ap)
1107 void *rv;
1108 void *cb_data;
1109 va_list ap;
1110 {
1111 char qbuf[MAXDNAME + 1], *qp;
1112 int n;
1113 querybuf buf;
1114 struct hostent *hp;
1115 const unsigned char *uaddr;
1116 int len, af;
1117
1118 uaddr = va_arg(ap, unsigned char *);
1119 len = va_arg(ap, int);
1120 af = va_arg(ap, int);
1121
1122 switch (af) {
1123 case AF_INET:
1124 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
1125 (uaddr[3] & 0xff),
1126 (uaddr[2] & 0xff),
1127 (uaddr[1] & 0xff),
1128 (uaddr[0] & 0xff));
1129 break;
1130
1131 case AF_INET6:
1132 qp = qbuf;
1133 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1134 qp += sprintf(qp, "%x.%x.",
1135 uaddr[n] & 0xf,
1136 ((unsigned int)uaddr[n] >> 4) & 0xf);
1137 }
1138 strcpy(qp, "ip6.int");
1139 break;
1140 default:
1141 abort();
1142 }
1143
1144 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
1145 if (n < 0) {
1146 dprintf("res_query failed (%d)\n", n);
1147 return NS_NOTFOUND;
1148 }
1149 hp = getanswer(&buf, n, qbuf, T_PTR);
1150 if (hp == NULL)
1151 switch (h_errno) {
1152 case HOST_NOT_FOUND:
1153 return NS_NOTFOUND;
1154 case TRY_AGAIN:
1155 return NS_TRYAGAIN;
1156 default:
1157 return NS_UNAVAIL;
1158 }
1159 hp->h_addrtype = af;
1160 hp->h_length = len;
1161 (void)memcpy(host_addr, uaddr, (size_t)len);
1162 h_addr_ptrs[0] = (char *)&host_addr;
1163 h_addr_ptrs[1] = (char *)0;
1164 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1165 map_v4v6_address((char*)host_addr, (char*)host_addr);
1166 hp->h_addrtype = AF_INET6;
1167 hp->h_length = IN6ADDRSZ;
1168 }
1169
1170 *((struct hostent **)rv) = hp;
1171 h_errno = NETDB_SUCCESS;
1172 return NS_SUCCESS;
1173 }
1174
1175 #ifdef YP
1176 /*ARGSUSED*/
1177 struct hostent *
1178 _yphostent(line, af)
1179 char *line;
1180 int af;
1181 {
1182 static struct in_addr host_addrs[MAXADDRS];
1183 char *p = line;
1184 char *cp, **q;
1185 char **hap;
1186 struct in_addr *buf;
1187 int more;
1188
1189 host.h_name = NULL;
1190 host.h_addr_list = h_addr_ptrs;
1191 host.h_length = sizeof(u_int32_t);
1192 host.h_addrtype = AF_INET;
1193 hap = h_addr_ptrs;
1194 buf = host_addrs;
1195 q = host.h_aliases = host_aliases;
1196
1197 /*
1198 * XXX: maybe support IPv6 parsing, based on 'af' setting
1199 */
1200 nextline:
1201 more = 0;
1202 cp = strpbrk(p, " \t");
1203 if (cp == NULL) {
1204 if (host.h_name == NULL)
1205 return (NULL);
1206 else
1207 goto done;
1208 }
1209 *cp++ = '\0';
1210
1211 *hap++ = (char *)(void *)buf;
1212 (void) inet_aton(p, buf++);
1213
1214 while (*cp == ' ' || *cp == '\t')
1215 cp++;
1216 p = cp;
1217 cp = strpbrk(p, " \t\n");
1218 if (cp != NULL) {
1219 if (*cp == '\n')
1220 more = 1;
1221 *cp++ = '\0';
1222 }
1223 if (!host.h_name)
1224 host.h_name = p;
1225 else if (strcmp(host.h_name, p)==0)
1226 ;
1227 else if (q < &host_aliases[MAXALIASES - 1])
1228 *q++ = p;
1229 p = cp;
1230 if (more)
1231 goto nextline;
1232
1233 while (cp && *cp) {
1234 if (*cp == ' ' || *cp == '\t') {
1235 cp++;
1236 continue;
1237 }
1238 if (*cp == '\n') {
1239 cp++;
1240 goto nextline;
1241 }
1242 if (q < &host_aliases[MAXALIASES - 1])
1243 *q++ = cp;
1244 cp = strpbrk(cp, " \t");
1245 if (cp != NULL)
1246 *cp++ = '\0';
1247 }
1248 done:
1249 *q = NULL;
1250 *hap = NULL;
1251 return (&host);
1252 }
1253
1254 /*ARGSUSED*/
1255 int
1256 _yp_gethtbyaddr(rv, cb_data, ap)
1257 void *rv;
1258 void *cb_data;
1259 va_list ap;
1260 {
1261 struct hostent *hp = (struct hostent *)NULL;
1262 static char *__ypcurrent;
1263 int __ypcurrentlen, r;
1264 char name[sizeof("xxx.xxx.xxx.xxx") + 1];
1265 const unsigned char *uaddr;
1266 int af;
1267
1268 uaddr = va_arg(ap, unsigned char *);
1269 /* NOSTRICT skip len */(void)va_arg(ap, int);
1270 af = va_arg(ap, int);
1271
1272 if (!__ypdomain) {
1273 if (_yp_check(&__ypdomain) == 0)
1274 return NS_UNAVAIL;
1275 }
1276 /*
1277 * XXX: based on the value of af, it would be possible to lookup
1278 * IPv6 names in YP, by changing the following snprintf().
1279 * Is it worth it?
1280 */
1281 (void)snprintf(name, sizeof name, "%u.%u.%u.%u",
1282 (uaddr[0] & 0xff),
1283 (uaddr[1] & 0xff),
1284 (uaddr[2] & 0xff),
1285 (uaddr[3] & 0xff));
1286 if (__ypcurrent)
1287 free(__ypcurrent);
1288 __ypcurrent = NULL;
1289 r = yp_match(__ypdomain, "hosts.byaddr", name,
1290 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1291 if (r==0)
1292 hp = _yphostent(__ypcurrent, af);
1293 if (hp==NULL) {
1294 h_errno = HOST_NOT_FOUND;
1295 return NS_NOTFOUND;
1296 }
1297 *((struct hostent **)rv) = hp;
1298 return NS_SUCCESS;
1299 }
1300
1301 /*ARGSUSED*/
1302 int
1303 _yp_gethtbyname(rv, cb_data, ap)
1304 void *rv;
1305 void *cb_data;
1306 va_list ap;
1307 {
1308 struct hostent *hp = (struct hostent *)NULL;
1309 static char *__ypcurrent;
1310 int __ypcurrentlen, r;
1311 const char *name;
1312 int af;
1313
1314 name = va_arg(ap, char *);
1315 /* NOSTRICT skip len */(void)va_arg(ap, int);
1316 af = va_arg(ap, int);
1317
1318 if (!__ypdomain) {
1319 if (_yp_check(&__ypdomain) == 0)
1320 return NS_UNAVAIL;
1321 }
1322 if (__ypcurrent)
1323 free(__ypcurrent);
1324 __ypcurrent = NULL;
1325 r = yp_match(__ypdomain, "hosts.byname", name,
1326 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1327 if (r==0)
1328 hp = _yphostent(__ypcurrent, af);
1329 if (hp==NULL) {
1330 h_errno = HOST_NOT_FOUND;
1331 return NS_NOTFOUND;
1332 }
1333 *((struct hostent **)rv) = hp;
1334 return NS_SUCCESS;
1335 }
1336 #endif
1337