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