Uninitialized vector entry?
[minix3.git] / lib / ip / gethnmadr.c
blob5f14875ace66f21568c2c946fd4a072aaf38f1a3
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 <errno.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include <net/hton.h>
32 #include <net/gen/nameser.h>
33 #include <net/gen/netdb.h>
34 #include <net/gen/in.h>
35 #include <net/gen/inet.h>
36 #include <net/gen/resolv.h>
37 #include <net/gen/socket.h>
38 #else
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <ctype.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <errno.h>
46 #include <arpa/inet.h>
47 #include <arpa/nameser.h>
48 #include <resolv.h>
49 #endif /* AMOEABA */
51 #define MAXALIASES 35
52 #define MAXADDRS 35
54 static char *h_addr_ptrs[MAXADDRS + 1];
56 #ifdef _MINIX
57 struct in_addr
59 ipaddr_t s_addr;
61 union querybuf;
63 extern int dn_skipname _ARGS(( const u_char *comp_dn, const u_char *eom ));
64 #define getshort _getshort
65 static struct hostent *getanswer _ARGS(( union querybuf *answer, int anslen,
66 int iquery ));
67 #define bcmp memcmp
68 #define bcopy(s, d, l) memcpy(d, s, l)
69 #endif /* _MINIX */
71 static struct hostent host;
72 static char *host_aliases[MAXALIASES];
73 static char hostbuf[BUFSIZ+1];
74 static struct in_addr host_addr;
76 #ifndef _MINIX
77 char *strpbrk();
78 #endif /* !_MINIX */
80 #if PACKETSZ > 1024
81 #define MAXPACKET PACKETSZ
82 #else
83 #define MAXPACKET 1024
84 #endif
86 typedef union querybuf
88 dns_hdr_t hdr;
89 u_char buf[MAXPACKET];
90 } querybuf_t;
92 typedef union align {
93 long al;
94 char ac;
95 } align_t;
97 static struct hostent *
98 getanswer(answer, anslen, iquery)
99 querybuf_t *answer;
100 int anslen;
101 int iquery;
103 register dns_hdr_t *hp;
104 register u_char *cp;
105 register int n;
106 u_char *eom;
107 char *bp, **ap;
108 int type, class, buflen, ancount, qdcount;
109 int haveanswer, getclass = C_ANY;
110 char **hap;
112 eom = answer->buf + anslen;
114 * find first satisfactory answer
116 hp = &answer->hdr;
117 ancount = ntohs(hp->dh_ancount);
118 qdcount = ntohs(hp->dh_qdcount);
119 bp = hostbuf;
120 buflen = sizeof(hostbuf);
121 cp = answer->buf + sizeof(dns_hdr_t);
122 if (qdcount) {
123 if (iquery) {
124 if ((n = dn_expand((u_char *)answer->buf, eom,
125 cp, (u_char *)bp, buflen)) < 0) {
126 h_errno = NO_RECOVERY;
127 return ((struct hostent *) NULL);
129 cp += n + QFIXEDSZ;
130 host.h_name = bp;
131 n = strlen(bp) + 1;
132 bp += n;
133 buflen -= n;
134 } else
135 cp += dn_skipname(cp, eom) + QFIXEDSZ;
136 while (--qdcount > 0)
137 cp += dn_skipname(cp, eom) + QFIXEDSZ;
138 } else if (iquery) {
139 if (hp->dh_flag1 & DHF_AA)
140 h_errno = HOST_NOT_FOUND;
141 else
142 h_errno = TRY_AGAIN;
143 return ((struct hostent *) NULL);
145 ap = host_aliases;
146 *ap = NULL;
147 host.h_aliases = host_aliases;
148 hap = h_addr_ptrs;
149 *hap = NULL;
150 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
151 host.h_addr_list = h_addr_ptrs;
152 #endif
153 haveanswer = 0;
154 while (--ancount >= 0 && cp < eom) {
155 if ((n = dn_expand((u_char *)answer->buf, eom, cp, (u_char *)bp,
156 buflen)) < 0)
157 break;
158 cp += n;
159 type = getshort(cp);
160 cp += sizeof(u_short);
161 class = getshort(cp);
162 cp += sizeof(u_short) + sizeof(u_long);
163 n = getshort(cp);
164 cp += sizeof(u_short);
165 if (type == T_CNAME) {
166 cp += n;
167 if (ap >= &host_aliases[MAXALIASES-1])
168 continue;
169 *ap++ = bp;
170 n = strlen(bp) + 1;
171 bp += n;
172 buflen -= n;
173 continue;
175 if (iquery && type == T_PTR) {
176 if ((n = dn_expand((u8_t *)answer->buf, eom,
177 cp, (u8_t *)bp, buflen)) < 0) {
178 cp += n;
179 continue;
181 cp += n;
182 host.h_name = bp;
183 return(&host);
185 if (iquery || type != T_A) {
186 #ifdef DEBUG
187 if (_res.options & RES_DEBUG)
188 printf("unexpected answer type %d, size %d\n",
189 type, n);
190 #endif
191 cp += n;
192 continue;
194 if (haveanswer) {
195 if (n != host.h_length) {
196 cp += n;
197 continue;
199 if (class != getclass) {
200 cp += n;
201 continue;
203 } else {
204 host.h_length = n;
205 getclass = class;
206 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
207 if (!iquery) {
208 host.h_name = bp;
209 bp += strlen(bp) + 1;
213 bp += (size_t)(sizeof(align_t) -
214 ((u_long)bp % sizeof(align_t)));
216 if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
217 #ifdef DEBUG
218 if (_res.options & RES_DEBUG)
219 printf("size (%d) too big\n", n);
220 #endif
221 break;
223 bcopy(cp, *hap++ = bp, n);
224 bp +=n;
225 cp += n;
226 haveanswer++;
228 if (haveanswer) {
229 *ap = NULL;
230 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
231 *hap = NULL;
232 #else
233 host.h_addr = h_addr_ptrs[0];
234 #endif
235 return (&host);
236 } else {
237 h_errno = TRY_AGAIN;
238 return ((struct hostent *) NULL);
242 struct hostent *
243 gethostbyname(name)
244 _CONST char *name;
246 querybuf_t buf;
247 register _CONST char *cp;
248 int n;
251 * disallow names consisting only of digits/dots, unless
252 * they end in a dot.
254 if (isdigit(name[0]))
255 for (cp = name;; ++cp) {
256 if (!*cp) {
257 if (*--cp == '.')
258 break;
260 * All-numeric, no dot at the end.
261 * Fake up a hostent as if we'd actually
262 * done a lookup. What if someone types
263 * 255.255.255.255? The test below will
264 * succeed spuriously... ???
266 if ((host_addr.s_addr = inet_addr(name)) == -1) {
267 h_errno = HOST_NOT_FOUND;
268 return((struct hostent *) NULL);
270 host.h_name = (char *) name;
271 host.h_aliases = host_aliases;
272 host_aliases[0] = NULL;
273 host.h_addrtype = AF_INET;
274 host.h_length = sizeof(u_long);
275 h_addr_ptrs[0] = (char *)&host_addr;
276 h_addr_ptrs[1] = (char *)0;
277 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
278 host.h_addr_list = h_addr_ptrs;
279 #else
280 host.h_addr = h_addr_ptrs[0];
281 #endif
282 return (&host);
284 if (!isdigit(*cp) && *cp != '.')
285 break;
288 if ((n = res_search((char*)name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
289 #ifdef DEBUG
290 if (_res.options & RES_DEBUG)
291 printf("res_search failed\n");
292 #endif
293 return ((struct hostent *) NULL);
295 return (getanswer(&buf, n, 0));
298 struct hostent *
299 gethostbyaddr(addr, len, type)
300 const char *addr;
301 int len, type;
303 int n;
304 querybuf_t buf;
305 register struct hostent *hp;
306 char qbuf[MAXDNAME];
308 if (type != AF_INET)
309 return ((struct hostent *) NULL);
310 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
311 ((unsigned)addr[3] & 0xff),
312 ((unsigned)addr[2] & 0xff),
313 ((unsigned)addr[1] & 0xff),
314 ((unsigned)addr[0] & 0xff));
315 n = res_query(qbuf, C_IN, T_PTR, (u8_t *)&buf, sizeof(buf));
316 if (n < 0) {
317 #ifdef DEBUG
318 if (_res.options & RES_DEBUG)
319 printf("res_query failed\n");
320 #endif
321 return ((struct hostent *) NULL);
323 hp = getanswer(&buf, n, 1);
324 if (hp == NULL)
325 return ((struct hostent *) NULL);
326 hp->h_addrtype = type;
327 hp->h_length = len;
328 h_addr_ptrs[0] = (char *)&host_addr;
329 h_addr_ptrs[1] = (char *)0;
330 host_addr = *(struct in_addr *)addr;
331 #if BSD < 43 && !defined(h_addr) /* new-style hostent structure */
332 hp->h_addr = h_addr_ptrs[0];
333 #endif
334 return(hp);