2 * Copyright (c) 1985, 1988 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)gethostnamadr.c 6.45 (Berkeley) 2/24/91";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <arpa/nameser.h>
53 static char *h_addr_ptrs[MAXADDRS + 1];
55 static struct hostent host;
56 static char *host_aliases[MAXALIASES];
57 static char hostbuf[BUFSIZ+1];
58 static struct in_addr host_addr;
59 static FILE *hostf = NULL;
60 static char hostaddr[MAXADDRS];
61 static char *host_addrs[2];
62 static int stayopen = 0;
66 #define MAXPACKET PACKETSZ
68 #define MAXPACKET 1024
73 u_char buf[MAXPACKET];
83 static struct hostent *
84 getanswer(answer, anslen, iquery)
94 int type, class, buflen, ancount, qdcount;
95 int haveanswer, getclass = C_ANY;
98 eom = answer->buf + anslen;
100 * find first satisfactory answer
103 ancount = ntohs(hp->ancount);
104 qdcount = ntohs(hp->qdcount);
106 buflen = sizeof(hostbuf);
107 cp = answer->buf + sizeof(HEADER);
110 if ((n = dn_expand((u_char *)answer->buf,
111 (u_char *)eom, (u_char *)cp, (u_char *)bp,
113 h_errno = NO_RECOVERY;
114 return ((struct hostent *) NULL);
122 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
123 while (--qdcount > 0)
124 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
127 h_errno = HOST_NOT_FOUND;
130 return ((struct hostent *) NULL);
134 host.h_aliases = host_aliases;
137 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
138 host.h_addr_list = h_addr_ptrs;
141 while (--ancount >= 0 && cp < eom) {
142 if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom,
143 (u_char *)cp, (u_char *)bp, buflen)) < 0)
146 type = _getshort(cp);
147 cp += sizeof(u_short);
148 class = _getshort(cp);
149 cp += sizeof(u_short) + sizeof(u_long);
151 cp += sizeof(u_short);
152 if (type == T_CNAME) {
154 if (ap >= &host_aliases[MAXALIASES-1])
162 if (iquery && type == T_PTR) {
163 if ((n = dn_expand((u_char *)answer->buf,
164 (u_char *)eom, (u_char *)cp, (u_char *)bp,
173 if (iquery || type != T_A) {
175 if (_res.options & RES_DEBUG)
176 printf("unexpected answer type %d, size %d\n",
183 if (n != host.h_length) {
187 if (class != getclass) {
194 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
197 bp += strlen(bp) + 1;
201 bp += sizeof(align) - ((u_long)bp % sizeof(align));
203 if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
205 if (_res.options & RES_DEBUG)
206 printf("size (%d) too big\n", n);
210 bcopy(cp, *hap++ = bp, n);
217 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
220 host.h_addr = h_addr_ptrs[0];
225 return ((struct hostent *) NULL);
236 extern struct hostent *_gethtbyname();
239 * disallow names consisting only of digits/dots, unless
242 if (isdigit(name[0]))
243 for (cp = name;; ++cp) {
248 * All-numeric, no dot at the end.
249 * Fake up a hostent as if we'd actually
250 * done a lookup. What if someone types
251 * 255.255.255.255? The test below will
252 * succeed spuriously... ???
254 if ((host_addr.s_addr = inet_addr(name)) == -1) {
255 h_errno = HOST_NOT_FOUND;
256 return((struct hostent *) NULL);
259 host.h_aliases = host_aliases;
260 host_aliases[0] = NULL;
261 host.h_addrtype = AF_INET;
262 host.h_length = sizeof(u_long);
263 h_addr_ptrs[0] = (char *)&host_addr;
264 h_addr_ptrs[1] = (char *)0;
265 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
266 host.h_addr_list = h_addr_ptrs;
268 host.h_addr = h_addr_ptrs[0];
272 if (!isdigit(*cp) && *cp != '.')
276 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
278 if (_res.options & RES_DEBUG)
279 printf("res_search failed\n");
281 if (errno == ECONNREFUSED)
282 return (_gethtbyname(name));
284 return ((struct hostent *) NULL);
286 return (getanswer(&buf, n, 0));
290 gethostbyaddr(addr, len, type)
296 register struct hostent *hp;
298 extern struct hostent *_gethtbyaddr();
301 return ((struct hostent *) NULL);
302 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
303 ((unsigned)addr[3] & 0xff),
304 ((unsigned)addr[2] & 0xff),
305 ((unsigned)addr[1] & 0xff),
306 ((unsigned)addr[0] & 0xff));
307 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
310 if (_res.options & RES_DEBUG)
311 printf("res_query failed\n");
313 if (errno == ECONNREFUSED)
314 return (_gethtbyaddr(addr, len, type));
315 return ((struct hostent *) NULL);
317 hp = getanswer(&buf, n, 1);
319 return ((struct hostent *) NULL);
320 hp->h_addrtype = type;
322 h_addr_ptrs[0] = (char *)&host_addr;
323 h_addr_ptrs[1] = (char *)0;
324 host_addr = *(struct in_addr *)addr;
325 #if BSD < 43 && !defined(h_addr) /* new-style hostent structure */
326 hp->h_addr = h_addr_ptrs[0];
335 hostf = fopen(_PATH_HOSTS, "r" );
343 if (hostf && !stayopen) {
344 (void) fclose(hostf);
353 register char *cp, **q;
355 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
358 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
362 cp = strpbrk(p, "#\n");
366 cp = strpbrk(p, " \t");
370 /* THIS STUFF IS INTERNET SPECIFIC */
371 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
372 host.h_addr_list = host_addrs;
374 host.h_addr = hostaddr;
375 *((u_long *)host.h_addr) = inet_addr(p);
376 host.h_length = sizeof (u_long);
377 host.h_addrtype = AF_INET;
378 while (*cp == ' ' || *cp == '\t')
381 q = host.h_aliases = host_aliases;
382 cp = strpbrk(cp, " \t");
386 if (*cp == ' ' || *cp == '\t') {
390 if (q < &host_aliases[MAXALIASES - 1])
392 cp = strpbrk(cp, " \t");
404 register struct hostent *p;
408 while (p = _gethtent()) {
409 if (strcasecmp(p->h_name, name) == 0)
411 for (cp = p->h_aliases; *cp != 0; cp++)
412 if (strcasecmp(*cp, name) == 0)
421 _gethtbyaddr(addr, len, type)
425 register struct hostent *p;
428 while (p = _gethtent())
429 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))