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