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