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