Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / resolv / gethnamaddr.c
blob87b4db1a6f1570ae09fdc4d65413a85fb4dabd71
1 /*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 * -
53 * --Copyright--
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
67 #include <stdio.h>
68 #include <netdb.h>
69 #include <resolv.h>
70 #include <ctype.h>
71 #include <errno.h>
72 #include <syslog.h>
74 #ifndef LOG_AUTH
75 # define LOG_AUTH 0
76 #endif
78 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
80 #if defined(BSD) && (BSD >= 199103)
81 # include <string.h>
82 #else
83 # include "../conf/portability.h"
84 #endif
85 #if defined(USE_OPTIONS_H)
86 # include <../conf/options.h>
87 #endif
89 #define MAXALIASES 35
90 #define MAXADDRS 35
92 static const char AskedForGot[] =
93 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
95 static char *h_addr_ptrs[MAXADDRS + 1];
96 static struct hostent *gethostbyname_ipv4 __P((const char *));
98 static struct hostent host;
99 static char *host_aliases[MAXALIASES];
100 static char hostbuf[8*1024];
101 static struct in_addr host_addr;
102 static FILE *hostf = NULL;
103 static int stayopen = 0;
105 #ifdef RESOLVSORT
106 static void addrsort __P((char **, int));
107 #endif
109 #if PACKETSZ > 1024
110 #define MAXPACKET PACKETSZ
111 #else
112 #define MAXPACKET 1024
113 #endif
115 typedef union {
116 HEADER hdr;
117 u_char buf[MAXPACKET];
118 } querybuf;
120 typedef union {
121 int32_t al;
122 char ac;
123 } align;
125 extern int h_errno;
127 #ifdef DEBUG
128 static void
129 dprintf(msg, num)
130 char *msg;
131 int num;
133 if (_res.options & RES_DEBUG) {
134 int save = errno;
136 printf(msg, num);
137 errno = save;
140 #else
141 # define dprintf(msg, num) /*nada*/
142 #endif
144 static struct hostent *
145 getanswer(answer, anslen, qname, qclass, qtype)
146 const querybuf *answer;
147 int anslen;
148 const char *qname;
149 int qclass, qtype;
151 register const HEADER *hp;
152 register const u_char *cp;
153 register int n;
154 const u_char *eom;
155 char *bp, **ap, **hap;
156 int type, class, buflen, ancount, qdcount;
157 int haveanswer, had_error;
158 int toobig = 0;
159 char tbuf[MAXDNAME+1];
160 const char *tname;
162 tname = qname;
163 host.h_name = NULL;
164 eom = answer->buf + anslen;
166 * find first satisfactory answer
168 hp = &answer->hdr;
169 ancount = ntohs(hp->ancount);
170 qdcount = ntohs(hp->qdcount);
171 bp = hostbuf;
172 buflen = sizeof hostbuf;
173 cp = answer->buf + HFIXEDSZ;
174 if (qdcount != 1) {
175 h_errno = NO_RECOVERY;
176 return (NULL);
178 if ((n = dn_expand(answer->buf, eom, cp, bp, buflen)) < 0) {
179 h_errno = NO_RECOVERY;
180 return (NULL);
182 cp += n + QFIXEDSZ;
183 if (qtype == T_A) {
184 /* res_send() has already verified that the query name is the
185 * same as the one we sent; this just gets the expanded name
186 * (i.e., with the succeeding search-domain tacked on).
188 n = strlen(bp) + 1; /* for the \0 */
189 host.h_name = bp;
190 bp += n;
191 buflen -= n;
192 /* The qname can be abbreviated, but h_name is now absolute. */
193 qname = host.h_name;
195 ap = host_aliases;
196 *ap = NULL;
197 host.h_aliases = host_aliases;
198 hap = h_addr_ptrs;
199 *hap = NULL;
200 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
201 host.h_addr_list = h_addr_ptrs;
202 #endif
203 haveanswer = 0;
204 had_error = 0;
205 while (ancount-- > 0 && cp < eom && !had_error) {
206 n = dn_expand(answer->buf, eom, cp, bp, buflen);
207 if (n < 0) {
208 had_error++;
209 continue;
211 cp += n; /* name */
212 type = _getshort(cp);
213 cp += INT16SZ; /* type */
214 class = _getshort(cp);
215 cp += INT16SZ + INT32SZ; /* class, TTL */
216 n = _getshort(cp);
217 cp += INT16SZ; /* len */
218 if (class != qclass) {
219 /* XXX - debug? syslog? */
220 cp += n;
221 continue; /* XXX - had_error++ ? */
223 if (qtype == T_A && type == T_CNAME) {
224 if (ap >= &host_aliases[MAXALIASES-1])
225 continue;
226 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
227 if (n < 0) {
228 had_error++;
229 continue;
231 cp += n;
232 if (host.h_name && strcasecmp(host.h_name, bp) != 0) {
233 syslog(LOG_NOTICE|LOG_AUTH,
234 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
235 host.h_name, bp);
236 continue; /* XXX - had_error++ ? */
238 /* Store alias. */
239 *ap++ = bp;
240 n = strlen(bp) + 1; /* for the \0 */
241 bp += n;
242 buflen -= n;
243 /* Get canonical name. */
244 n = strlen(tbuf) + 1; /* for the \0 */
245 if (n > buflen) {
246 had_error++;
247 continue;
249 strcpy(bp, tbuf);
250 host.h_name = bp;
251 bp += n;
252 buflen -= n;
253 continue;
255 if (qtype == T_PTR && type == T_CNAME) {
256 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
257 if (n < 0) {
258 had_error++;
259 continue;
261 cp += n;
262 /* Get canonical name. */
263 n = strlen(tbuf) + 1; /* for the \0 */
264 if (n > buflen) {
265 had_error++;
266 continue;
268 strcpy(bp, tbuf);
269 tname = bp;
270 bp += n;
271 buflen -= n;
272 continue;
274 if (type != qtype) {
275 syslog(LOG_NOTICE|LOG_AUTH,
276 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
277 qname, p_class(qclass), p_type(qtype),
278 p_type(type));
279 cp += n;
280 continue; /* XXX - had_error++ ? */
282 switch (type) {
283 case T_PTR:
284 if (strcasecmp(tname, bp) != 0) {
285 syslog(LOG_NOTICE|LOG_AUTH,
286 AskedForGot, qname, bp);
287 cp += n;
288 continue; /* XXX - had_error++ ? */
290 n = dn_expand(answer->buf, eom, cp, bp, buflen);
291 if (n < 0) {
292 had_error++;
293 break;
295 #if MULTI_PTRS_ARE_ALIASES
296 cp += n;
297 if (!haveanswer)
298 host.h_name = bp;
299 else if (ap < &host_aliases[MAXALIASES-1])
300 *ap++ = bp;
301 else
302 n = -1;
303 if (n != -1) {
304 n = strlen(bp) + 1; /* for the \0 */
305 bp += n;
306 buflen -= n;
308 break;
309 #else
310 host.h_name = bp;
311 h_errno = NETDB_SUCCESS;
312 return (&host);
313 #endif
314 case T_A:
315 if (strcasecmp(host.h_name, bp) != 0) {
316 syslog(LOG_NOTICE|LOG_AUTH,
317 AskedForGot, host.h_name, bp);
318 cp += n;
319 continue; /* XXX - had_error++ ? */
321 if (haveanswer) {
322 if (n != host.h_length) {
323 cp += n;
324 continue;
326 } else {
327 register int nn;
329 host.h_length = n;
330 host.h_addrtype = (class == C_IN)
331 ? AF_INET
332 : AF_UNSPEC;
333 host.h_name = bp;
334 nn = strlen(bp) + 1; /* for the \0 */
335 bp += nn;
336 buflen -= nn;
339 bp += sizeof(align) - ((u_long)bp % sizeof(align));
341 if (bp + n >= &hostbuf[sizeof hostbuf]) {
342 dprintf("size (%d) too big\n", n);
343 had_error++;
344 continue;
346 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
347 if (!toobig++)
348 dprintf("Too many addresses (%d)\n",
349 MAXADDRS);
350 cp += n;
351 continue;
353 bcopy(cp, *hap++ = bp, n);
354 bp += n;
355 cp += n;
356 break;
357 default:
358 dprintf("Impossible condition (type=%d)\n", type);
359 h_errno = NO_RECOVERY;
360 return (NULL);
361 } /*switch*/
362 if (!had_error)
363 haveanswer++;
364 } /*while*/
365 if (haveanswer) {
366 *ap = NULL;
367 *hap = NULL;
368 # if defined(RESOLVSORT)
370 * Note: we sort even if host can take only one address
371 * in its return structures - should give it the "best"
372 * address in that case, not some random one
374 if (_res.nsort && haveanswer > 1 &&
375 qclass == C_IN && qtype == T_A)
376 addrsort(h_addr_ptrs, haveanswer);
377 # endif /*RESOLVSORT*/
378 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
379 /* nothing */
380 #else
381 host.h_addr = h_addr_ptrs[0];
382 #endif /*BSD*/
383 if (!host.h_name) {
384 n = strlen(qname) + 1; /* for the \0 */
385 strcpy(bp, qname);
386 host.h_name = bp;
388 h_errno = NETDB_SUCCESS;
389 return (&host);
390 } else {
391 h_errno = TRY_AGAIN;
392 return (NULL);
396 struct hostent *
397 gethostbyname(name)
398 const char *name;
400 struct hostent *hp;
402 #if defined(AF_INET6) && defined(RES_TRY_INET6)
403 if (_res.options & RES_TRY_INET6) {
404 hp = gethostbyname2(name, AF_INET6);
405 if (hp)
406 return (hp);
408 #endif
409 return (gethostbyname2(name, AF_INET));
412 struct hostent *
413 gethostbyname2(name, af)
414 const char *name;
415 int af;
417 switch (af) {
418 case AF_INET:
419 return (gethostbyname_ipv4(name));
421 errno = EAFNOSUPPORT;
422 h_errno = NETDB_INTERNAL;
423 return (NULL);
426 static struct hostent *
427 gethostbyname_ipv4(name)
428 const char *name;
430 querybuf buf;
431 register const char *cp;
432 int n;
433 extern struct hostent *_gethtbyname();
435 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
436 h_errno = NETDB_INTERNAL;
437 return (NULL);
441 * if there aren't any dots, it could be a user-level alias.
442 * this is also done in res_query() since we are not the only
443 * function that looks up host names.
445 if (!strchr(name, '.') && (cp = __hostalias(name)))
446 name = cp;
449 * disallow names consisting only of digits/dots, unless
450 * they end in a dot.
452 if (isdigit(name[0]))
453 for (cp = name;; ++cp) {
454 if (!*cp) {
455 if (*--cp == '.')
456 break;
458 * All-numeric, no dot at the end.
459 * Fake up a hostent as if we'd actually
460 * done a lookup.
462 if (!inet_aton(name, &host_addr)) {
463 h_errno = HOST_NOT_FOUND;
464 return (NULL);
466 strncpy(hostbuf, name, MAXDNAME);
467 hostbuf[MAXDNAME] = '\0';
468 host.h_name = hostbuf;
469 host.h_aliases = host_aliases;
470 host_aliases[0] = NULL;
471 host.h_addrtype = AF_INET;
472 host.h_length = INT32SZ;
473 h_addr_ptrs[0] = (char *)&host_addr;
474 h_addr_ptrs[1] = NULL;
475 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
476 host.h_addr_list = h_addr_ptrs;
477 #else
478 host.h_addr = h_addr_ptrs[0];
479 #endif
480 h_errno = NETDB_SUCCESS;
481 return (&host);
483 if (!isdigit(*cp) && *cp != '.')
484 break;
487 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
488 dprintf("res_search failed (%d)\n", n);
489 if (errno == ECONNREFUSED)
490 return (_gethtbyname(name));
491 return (NULL);
493 return (getanswer(&buf, n, name, C_IN, T_A));
496 struct hostent *
497 gethostbyaddr(addr, len, type)
498 const char *addr;
499 int len, type;
501 int n;
502 querybuf buf;
503 register struct hostent *hp;
504 char qbuf[MAXDNAME+1];
505 #ifdef SUNSECURITY
506 register struct hostent *rhp;
507 char **haddr;
508 u_long old_options;
509 char hname2[MAXDNAME+1];
510 #endif /*SUNSECURITY*/
511 extern struct hostent *_gethtbyaddr();
513 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
514 h_errno = NETDB_INTERNAL;
515 return (NULL);
517 if (type != AF_INET) {
518 errno = EAFNOSUPPORT;
519 h_errno = NETDB_INTERNAL;
520 return (NULL);
522 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
523 ((unsigned)addr[3] & 0xff),
524 ((unsigned)addr[2] & 0xff),
525 ((unsigned)addr[1] & 0xff),
526 ((unsigned)addr[0] & 0xff));
527 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
528 if (n < 0) {
529 dprintf("res_query failed (%d)\n", n);
530 if (errno == ECONNREFUSED)
531 return (_gethtbyaddr(addr, len, type));
532 return (NULL);
534 if (!(hp = getanswer(&buf, n, qbuf, C_IN, T_PTR)))
535 return (NULL); /* h_errno was set by getanswer() */
536 #ifdef SUNSECURITY
538 * turn off search as the name should be absolute,
539 * 'localhost' should be matched by defnames
541 strncpy(hname2, hp->h_name, MAXDNAME);
542 hname2[MAXDNAME] = '\0';
543 old_options = _res.options;
544 _res.options &= ~RES_DNSRCH;
545 _res.options |= RES_DEFNAMES;
546 if (!(rhp = gethostbyname(hname2))) {
547 syslog(LOG_NOTICE|LOG_AUTH,
548 "gethostbyaddr: No A record for %s (verifying [%s])",
549 hname2, inet_ntoa(*((struct in_addr *)addr)));
550 _res.options = old_options;
551 h_errno = HOST_NOT_FOUND;
552 return (NULL);
554 _res.options = old_options;
555 for (haddr = rhp->h_addr_list; *haddr; haddr++)
556 if (!memcmp(*haddr, addr, INADDRSZ))
557 break;
558 if (!*haddr) {
559 syslog(LOG_NOTICE|LOG_AUTH,
560 "gethostbyaddr: A record of %s != PTR record [%s]",
561 hname2, inet_ntoa(*((struct in_addr *)addr)));
562 h_errno = HOST_NOT_FOUND;
563 return (NULL);
565 #endif /*SUNSECURITY*/
566 hp->h_addrtype = type;
567 hp->h_length = len;
568 h_addr_ptrs[0] = (char *)&host_addr;
569 h_addr_ptrs[1] = NULL;
570 host_addr = *(struct in_addr *)addr;
571 h_errno = NETDB_SUCCESS;
572 return (hp);
575 void
576 _sethtent(f)
577 int f;
579 if (!hostf)
580 hostf = fopen(_PATH_HOSTS, "r" );
581 else
582 rewind(hostf);
583 stayopen = f;
586 void
587 _endhtent()
589 if (hostf && !stayopen) {
590 (void) fclose(hostf);
591 hostf = NULL;
595 struct hostent *
596 _gethtent()
598 char *p;
599 register char *cp, **q;
601 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
602 h_errno = NETDB_INTERNAL;
603 return (NULL);
605 again:
606 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
607 h_errno = HOST_NOT_FOUND;
608 return (NULL);
610 if (*p == '#')
611 goto again;
612 if (!(cp = strpbrk(p, "#\n")))
613 goto again;
614 *cp = '\0';
615 if (!(cp = strpbrk(p, " \t")))
616 goto again;
617 *cp++ = '\0';
618 /* THIS STUFF IS INTERNET SPECIFIC */
619 if (!inet_aton(p, &host_addr))
620 goto again;
621 h_addr_ptrs[0] = (char *)&host_addr;
622 h_addr_ptrs[1] = NULL;
623 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
624 host.h_addr_list = h_addr_ptrs;
625 #else
626 host.h_addr = h_addr_ptrs[0];
627 #endif
628 host.h_length = INT32SZ;
629 host.h_addrtype = AF_INET;
630 while (*cp == ' ' || *cp == '\t')
631 cp++;
632 host.h_name = cp;
633 q = host.h_aliases = host_aliases;
634 if (cp = strpbrk(cp, " \t"))
635 *cp++ = '\0';
636 while (cp && *cp) {
637 if (*cp == ' ' || *cp == '\t') {
638 cp++;
639 continue;
641 if (q < &host_aliases[MAXALIASES - 1])
642 *q++ = cp;
643 if (cp = strpbrk(cp, " \t"))
644 *cp++ = '\0';
646 *q = NULL;
647 h_errno = NETDB_SUCCESS;
648 return (&host);
651 struct hostent *
652 _gethtbyname(name)
653 char *name;
655 register struct hostent *p;
656 register char **cp;
658 _sethtent(0);
659 while (p = _gethtent()) {
660 if (strcasecmp(p->h_name, name) == 0)
661 break;
662 for (cp = p->h_aliases; *cp != 0; cp++)
663 if (strcasecmp(*cp, name) == 0)
664 goto found;
666 found:
667 _endhtent();
668 return (p);
671 struct hostent *
672 _gethtbyaddr(addr, len, type)
673 const char *addr;
674 int len, type;
676 register struct hostent *p;
678 _sethtent(0);
679 while (p = _gethtent())
680 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
681 break;
682 _endhtent();
683 return (p);
686 #ifdef RESOLVSORT
687 static void
688 addrsort(ap, num)
689 char **ap;
690 int num;
692 int i, j;
693 char **p;
694 short aval[MAXADDRS];
695 int needsort = 0;
697 p = ap;
698 for (i = 0; i < num; i++, p++) {
699 for (j = 0 ; (unsigned)j < _res.nsort; j++)
700 if (_res.sort_list[j].addr.s_addr ==
701 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
702 break;
703 aval[i] = j;
704 if (needsort == 0 && i > 0 && j < aval[i-1])
705 needsort = i;
707 if (!needsort)
708 return;
710 while (needsort < num) {
711 for (j = needsort - 1; j >= 0; j--) {
712 if (aval[j] > aval[j+1]) {
713 char *hp;
715 i = aval[j];
716 aval[j] = aval[j+1];
717 aval[j+1] = i;
719 hp = ap[j];
720 ap[j] = ap[j+1];
721 ap[j+1] = hp;
723 } else
724 break;
726 needsort++;
729 #endif
731 #if defined(BSD43_BSD43_NFS) || defined(sun)
732 /* some libc's out there are bound internally to these names (UMIPS) */
733 void
734 ht_sethostent(stayopen)
735 int stayopen;
737 _sethtent(stayopen);
740 void
741 ht_endhostent()
743 _endhtent();
746 struct hostent *
747 ht_gethostbyname(name)
748 char *name;
750 return (_gethtbyname(name));
753 struct hostent *
754 ht_gethostbyaddr(addr, len, type)
755 const char *addr;
756 int len, type;
758 return (_gethtbyaddr(addr, len, type));
761 struct hostent *
762 gethostent()
764 return (_gethtent());
767 void
768 dns_service()
770 return;
773 #undef dn_skipname
774 dn_skipname(comp_dn, eom)
775 const u_char *comp_dn, *eom;
777 return (__dn_skipname(comp_dn, eom));
779 #endif /*old-style libc with yp junk in it*/