secondary cache feature in vm.
[minix.git] / lib / libc / ip / gethnmadr.c
blobe7368eb0402e09e4cdedb032003047df7276b2ec
1 /*
2 * Copyright (c) 1985, 1988 Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)gethostnamadr.c 6.41 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
24 #ifdef _MINIX
25 #include <sys/types.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <net/hton.h>
31 #include <net/gen/nameser.h>
32 #include <net/gen/netdb.h>
33 #include <net/gen/in.h>
34 #include <net/gen/inet.h>
35 #include <net/gen/resolv.h>
36 #include <net/gen/socket.h>
37 #else
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <ctype.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <arpa/inet.h>
45 #include <arpa/nameser.h>
46 #include <resolv.h>
47 #endif /* AMOEABA */
49 #define MAXALIASES 35
50 #define MAXADDRS 35
52 static char *h_addr_ptrs[MAXADDRS + 1];
54 #ifdef _MINIX
55 union querybuf;
57 extern int dn_skipname _ARGS(( const u_char *comp_dn, const u_char *eom ));
58 #define getshort _getshort
59 static struct hostent *getanswer _ARGS(( union querybuf *answer, int anslen,
60 int iquery ));
61 #define bcmp memcmp
62 #define bcopy(s, d, l) memcpy(d, s, l)
63 #endif /* _MINIX */
65 static struct hostent host;
66 static char *host_aliases[MAXALIASES];
67 static char hostbuf[BUFSIZ+1];
68 static struct in_addr host_addr;
70 #ifndef _MINIX
71 char *strpbrk();
72 #endif /* !_MINIX */
74 #if PACKETSZ > 1024
75 #define MAXPACKET PACKETSZ
76 #else
77 #define MAXPACKET 1024
78 #endif
80 typedef union querybuf
82 dns_hdr_t hdr;
83 u_char buf[MAXPACKET];
84 } querybuf_t;
86 typedef union align {
87 long al;
88 char ac;
89 } align_t;
91 static struct hostent *
92 getanswer(answer, anslen, iquery)
93 querybuf_t *answer;
94 int anslen;
95 int iquery;
97 register dns_hdr_t *hp;
98 register u_char *cp;
99 register int n;
100 u_char *eom;
101 char *bp, **ap;
102 int type, class, buflen, ancount, qdcount;
103 int haveanswer, getclass = C_ANY;
104 char **hap;
106 eom = answer->buf + anslen;
108 * find first satisfactory answer
110 hp = &answer->hdr;
111 ancount = ntohs(hp->dh_ancount);
112 qdcount = ntohs(hp->dh_qdcount);
113 bp = hostbuf;
114 buflen = sizeof(hostbuf);
115 cp = answer->buf + sizeof(dns_hdr_t);
116 if (qdcount) {
117 if (iquery) {
118 if ((n = dn_expand((u_char *)answer->buf, eom,
119 cp, (u_char *)bp, buflen)) < 0) {
120 h_errno = NO_RECOVERY;
121 return ((struct hostent *) NULL);
123 cp += n + QFIXEDSZ;
124 host.h_name = bp;
125 n = strlen(bp) + 1;
126 bp += n;
127 buflen -= n;
128 } else
129 cp += dn_skipname(cp, eom) + QFIXEDSZ;
130 while (--qdcount > 0)
131 cp += dn_skipname(cp, eom) + QFIXEDSZ;
132 } else if (iquery) {
133 if (hp->dh_flag1 & DHF_AA)
134 h_errno = HOST_NOT_FOUND;
135 else
136 h_errno = TRY_AGAIN;
137 return ((struct hostent *) NULL);
139 ap = host_aliases;
140 *ap = NULL;
141 host.h_aliases = host_aliases;
142 hap = h_addr_ptrs;
143 *hap = NULL;
144 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
145 host.h_addr_list = h_addr_ptrs;
146 #endif
147 haveanswer = 0;
148 while (--ancount >= 0 && cp < eom) {
149 if ((n = dn_expand((u_char *)answer->buf, eom, cp, (u_char *)bp,
150 buflen)) < 0)
151 break;
152 cp += n;
153 type = getshort(cp);
154 cp += sizeof(u_short);
155 class = getshort(cp);
156 cp += sizeof(u_short) + sizeof(u_long);
157 n = getshort(cp);
158 cp += sizeof(u_short);
159 if (type == T_CNAME) {
160 cp += n;
161 if (ap >= &host_aliases[MAXALIASES-1])
162 continue;
163 *ap++ = bp;
164 n = strlen(bp) + 1;
165 bp += n;
166 buflen -= n;
167 continue;
169 if (iquery && type == T_PTR) {
170 if ((n = dn_expand((u8_t *)answer->buf, eom,
171 cp, (u8_t *)bp, buflen)) < 0) {
172 cp += n;
173 continue;
175 cp += n;
176 host.h_name = bp;
177 return(&host);
179 if (iquery || type != T_A) {
180 #ifdef DEBUG
181 if (_res.options & RES_DEBUG)
182 printf("unexpected answer type %d, size %d\n",
183 type, n);
184 #endif
185 cp += n;
186 continue;
188 if (haveanswer) {
189 if (n != host.h_length) {
190 cp += n;
191 continue;
193 if (class != getclass) {
194 cp += n;
195 continue;
197 } else {
198 host.h_length = n;
199 getclass = class;
200 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
201 if (!iquery) {
202 host.h_name = bp;
203 bp += strlen(bp) + 1;
207 bp += (size_t)(sizeof(align_t) -
208 ((u_long)bp % sizeof(align_t)));
210 if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
211 #ifdef DEBUG
212 if (_res.options & RES_DEBUG)
213 printf("size (%d) too big\n", n);
214 #endif
215 break;
217 bcopy(cp, *hap++ = bp, n);
218 bp +=n;
219 cp += n;
220 haveanswer++;
222 if (haveanswer) {
223 *ap = NULL;
224 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
225 *hap = NULL;
226 #else
227 host.h_addr = h_addr_ptrs[0];
228 #endif
229 return (&host);
230 } else {
231 h_errno = TRY_AGAIN;
232 return ((struct hostent *) NULL);
236 struct hostent *
237 gethostbyname(name)
238 _CONST char *name;
240 querybuf_t buf;
241 register _CONST char *cp;
242 int n;
245 * disallow names consisting only of digits/dots, unless
246 * they end in a dot.
248 if (isdigit(name[0]))
249 for (cp = name;; ++cp) {
250 if (!*cp) {
251 if (*--cp == '.')
252 break;
254 * All-numeric, no dot at the end.
255 * Fake up a hostent as if we'd actually
256 * done a lookup. What if someone types
257 * 255.255.255.255? The test below will
258 * succeed spuriously... ???
260 if ((host_addr.s_addr = inet_addr(name)) == -1) {
261 h_errno = HOST_NOT_FOUND;
262 return((struct hostent *) NULL);
264 host.h_name = (char *) name;
265 host.h_aliases = host_aliases;
266 host_aliases[0] = NULL;
267 host.h_addrtype = AF_INET;
268 host.h_length = sizeof(u_long);
269 h_addr_ptrs[0] = (char *)&host_addr;
270 h_addr_ptrs[1] = (char *)0;
271 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
272 host.h_addr_list = h_addr_ptrs;
273 #else
274 host.h_addr = h_addr_ptrs[0];
275 #endif
276 return (&host);
278 if (!isdigit(*cp) && *cp != '.')
279 break;
282 if ((n = res_search((char*)name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
283 #ifdef DEBUG
284 if (_res.options & RES_DEBUG)
285 printf("res_search failed\n");
286 #endif
287 return ((struct hostent *) NULL);
289 return (getanswer(&buf, n, 0));
292 struct hostent *
293 gethostbyaddr(addr, len, type)
294 const char *addr;
295 int len, type;
297 int n;
298 querybuf_t buf;
299 register struct hostent *hp;
300 char qbuf[MAXDNAME];
302 if (type != AF_INET)
303 return ((struct hostent *) NULL);
304 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
305 ((unsigned)addr[3] & 0xff),
306 ((unsigned)addr[2] & 0xff),
307 ((unsigned)addr[1] & 0xff),
308 ((unsigned)addr[0] & 0xff));
309 n = res_query(qbuf, C_IN, T_PTR, (u8_t *)&buf, sizeof(buf));
310 if (n < 0) {
311 #ifdef DEBUG
312 if (_res.options & RES_DEBUG)
313 printf("res_query failed\n");
314 #endif
315 return ((struct hostent *) NULL);
317 hp = getanswer(&buf, n, 1);
318 if (hp == NULL)
319 return ((struct hostent *) NULL);
320 hp->h_addrtype = type;
321 hp->h_length = len;
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];
327 #endif
328 return(hp);