fix oops in MLINKS for link_addr.3
[unix-history] / usr / src / lib / libc / net / gethostnamadr.c
CommitLineData
8ea4199d 1/*
5cfeec12
KB
2 * Copyright (c) 1985, 1988 Regents of the University of California.
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
8ea4199d
DF
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
9e408d7f 9static char sccsid[] = "@(#)gethostnamadr.c 6.48 (Berkeley) %G%";
5cfeec12 10#endif /* LIBC_SCCS and not lint */
b423e985 11
7f2f8dda 12#include <sys/param.h>
f784656b
RC
13#include <sys/socket.h>
14#include <netinet/in.h>
2795c575 15#include <arpa/inet.h>
aa678832 16#include <arpa/nameser.h>
24fac7d8 17#include <netdb.h>
f94b2f50 18#include <resolv.h>
24fac7d8 19#include <stdio.h>
dcdb79c9 20#include <ctype.h>
24fac7d8
KB
21#include <errno.h>
22#include <string.h>
337257b2
RC
23
24#define MAXALIASES 35
0e9a6a67 25#define MAXADDRS 35
337257b2 26
412fa3e3
JB
27static char *h_addr_ptrs[MAXADDRS + 1];
28
29static struct hostent host;
337257b2 30static char *host_aliases[MAXALIASES];
291cd39e 31static char hostbuf[BUFSIZ+1];
4682c40f 32static struct in_addr host_addr;
12379d92 33static FILE *hostf = NULL;
12379d92
KD
34static char hostaddr[MAXADDRS];
35static char *host_addrs[2];
36static int stayopen = 0;
81f99a97 37char *strpbrk();
3a87f357 38
b6a43c3e 39#if PACKETSZ > 1024
0e9a6a67 40#define MAXPACKET PACKETSZ
b6a43c3e 41#else
0e9a6a67 42#define MAXPACKET 1024
b6a43c3e
MK
43#endif
44
c675dcaa 45typedef union {
0e9a6a67
KB
46 HEADER hdr;
47 u_char buf[MAXPACKET];
c675dcaa
KD
48} querybuf;
49
e714052f 50typedef union {
c675dcaa
KD
51 long al;
52 char ac;
53} align;
54
fe8457be
KD
55int h_errno;
56
337257b2 57static struct hostent *
0e9a6a67
KB
58getanswer(answer, anslen, iquery)
59 querybuf *answer;
60 int anslen;
61 int iquery;
337257b2 62{
f784656b 63 register HEADER *hp;
0e9a6a67 64 register u_char *cp;
f784656b 65 register int n;
0e9a6a67
KB
66 u_char *eom;
67 char *bp, **ap;
2795c575
KD
68 int type, class, buflen, ancount, qdcount;
69 int haveanswer, getclass = C_ANY;
412fa3e3 70 char **hap;
337257b2 71
0e9a6a67 72 eom = answer->buf + anslen;
f784656b
RC
73 /*
74 * find first satisfactory answer
75 */
0e9a6a67 76 hp = &answer->hdr;
f784656b 77 ancount = ntohs(hp->ancount);
4682c40f 78 qdcount = ntohs(hp->qdcount);
f784656b
RC
79 bp = hostbuf;
80 buflen = sizeof(hostbuf);
0e9a6a67 81 cp = answer->buf + sizeof(HEADER);
4682c40f 82 if (qdcount) {
f784656b 83 if (iquery) {
24fac7d8
KB
84 if ((n = dn_expand((u_char *)answer->buf,
85 (u_char *)eom, (u_char *)cp, (u_char *)bp,
86 buflen)) < 0) {
fe8457be 87 h_errno = NO_RECOVERY;
e0313454 88 return ((struct hostent *) NULL);
fe8457be 89 }
f784656b
RC
90 cp += n + QFIXEDSZ;
91 host.h_name = bp;
92 n = strlen(bp) + 1;
93 bp += n;
94 buflen -= n;
95 } else
371d3c9b 96 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
4682c40f 97 while (--qdcount > 0)
371d3c9b 98 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
fe8457be
KD
99 } else if (iquery) {
100 if (hp->aa)
101 h_errno = HOST_NOT_FOUND;
102 else
103 h_errno = TRY_AGAIN;
e0313454 104 return ((struct hostent *) NULL);
fe8457be 105 }
412fa3e3 106 ap = host_aliases;
ee21fae8 107 *ap = NULL;
412fa3e3
JB
108 host.h_aliases = host_aliases;
109 hap = h_addr_ptrs;
ee21fae8 110 *hap = NULL;
15eda464 111#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
412fa3e3 112 host.h_addr_list = h_addr_ptrs;
da75bcd9 113#endif
412fa3e3 114 haveanswer = 0;
f784656b 115 while (--ancount >= 0 && cp < eom) {
24fac7d8
KB
116 if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom,
117 (u_char *)cp, (u_char *)bp, buflen)) < 0)
412fa3e3 118 break;
f784656b 119 cp += n;
b1772b2c 120 type = _getshort(cp);
f784656b 121 cp += sizeof(u_short);
b1772b2c 122 class = _getshort(cp);
f784656b 123 cp += sizeof(u_short) + sizeof(u_long);
b1772b2c 124 n = _getshort(cp);
f784656b
RC
125 cp += sizeof(u_short);
126 if (type == T_CNAME) {
127 cp += n;
128 if (ap >= &host_aliases[MAXALIASES-1])
129 continue;
130 *ap++ = bp;
131 n = strlen(bp) + 1;
132 bp += n;
133 buflen -= n;
134 continue;
135 }
0e9a6a67 136 if (iquery && type == T_PTR) {
24fac7d8
KB
137 if ((n = dn_expand((u_char *)answer->buf,
138 (u_char *)eom, (u_char *)cp, (u_char *)bp,
9e408d7f
KB
139 buflen)) < 0)
140 break;
4682c40f
JB
141 cp += n;
142 host.h_name = bp;
143 return(&host);
144 }
0e9a6a67 145 if (iquery || type != T_A) {
d1c1ab68 146#ifdef DEBUG
f784656b
RC
147 if (_res.options & RES_DEBUG)
148 printf("unexpected answer type %d, size %d\n",
149 type, n);
d1c1ab68 150#endif
412fa3e3 151 cp += n;
f784656b
RC
152 continue;
153 }
412fa3e3
JB
154 if (haveanswer) {
155 if (n != host.h_length) {
156 cp += n;
157 continue;
158 }
159 if (class != getclass) {
160 cp += n;
161 continue;
162 }
163 } else {
164 host.h_length = n;
165 getclass = class;
82a65531 166 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
412fa3e3
JB
167 if (!iquery) {
168 host.h_name = bp;
169 bp += strlen(bp) + 1;
170 }
f784656b 171 }
c675dcaa 172
8ad123ce 173 bp += sizeof(align) - ((u_long)bp % sizeof(align));
c675dcaa 174
f784656b 175 if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
d1c1ab68 176#ifdef DEBUG
f784656b
RC
177 if (_res.options & RES_DEBUG)
178 printf("size (%d) too big\n", n);
d1c1ab68 179#endif
412fa3e3 180 break;
f784656b 181 }
412fa3e3
JB
182 bcopy(cp, *hap++ = bp, n);
183 bp +=n;
184 cp += n;
185 haveanswer++;
f784656b 186 }
412fa3e3
JB
187 if (haveanswer) {
188 *ap = NULL;
15eda464 189#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
412fa3e3 190 *hap = NULL;
8ad123ce
MK
191#else
192 host.h_addr = h_addr_ptrs[0];
193#endif
412fa3e3 194 return (&host);
fe8457be
KD
195 } else {
196 h_errno = TRY_AGAIN;
e0313454 197 return ((struct hostent *) NULL);
fe8457be 198 }
337257b2
RC
199}
200
201struct hostent *
f784656b 202gethostbyname(name)
bd7985ff 203 const char *name;
337257b2 204{
0e9a6a67 205 querybuf buf;
bd7985ff 206 register const char *cp;
f784656b 207 int n;
12379d92 208 extern struct hostent *_gethtbyname();
337257b2 209
693999f7
KB
210 /*
211 * disallow names consisting only of digits/dots, unless
212 * they end in a dot.
213 */
214 if (isdigit(name[0]))
215 for (cp = name;; ++cp) {
216 if (!*cp) {
217 if (*--cp == '.')
218 break;
7468ba44
JB
219 /*
220 * All-numeric, no dot at the end.
221 * Fake up a hostent as if we'd actually
8a806b8a 222 * done a lookup.
7468ba44 223 */
8a806b8a 224 if (!inet_aton(name, &host_addr)) {
7468ba44
JB
225 h_errno = HOST_NOT_FOUND;
226 return((struct hostent *) NULL);
227 }
bd7985ff 228 host.h_name = (char *)name;
7468ba44
JB
229 host.h_aliases = host_aliases;
230 host_aliases[0] = NULL;
231 host.h_addrtype = AF_INET;
232 host.h_length = sizeof(u_long);
233 h_addr_ptrs[0] = (char *)&host_addr;
234 h_addr_ptrs[1] = (char *)0;
235#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
236 host.h_addr_list = h_addr_ptrs;
237#else
238 host.h_addr = h_addr_ptrs[0];
239#endif
240 return (&host);
693999f7
KB
241 }
242 if (!isdigit(*cp) && *cp != '.')
243 break;
244 }
216ea7e1 245
0e9a6a67 246 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
d1c1ab68 247#ifdef DEBUG
f784656b 248 if (_res.options & RES_DEBUG)
0e9a6a67 249 printf("res_search failed\n");
d1c1ab68 250#endif
0e9a6a67
KB
251 if (errno == ECONNREFUSED)
252 return (_gethtbyname(name));
253 else
254 return ((struct hostent *) NULL);
3a87f357 255 }
0e9a6a67 256 return (getanswer(&buf, n, 0));
337257b2
RC
257}
258
259struct hostent *
f784656b 260gethostbyaddr(addr, len, type)
24fac7d8 261 const char *addr;
f784656b 262 int len, type;
337257b2 263{
f784656b 264 int n;
c675dcaa 265 querybuf buf;
4682c40f
JB
266 register struct hostent *hp;
267 char qbuf[MAXDNAME];
12379d92
KD
268 extern struct hostent *_gethtbyaddr();
269
f784656b 270 if (type != AF_INET)
e0313454 271 return ((struct hostent *) NULL);
0d7830cc 272 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
fe8457be
KD
273 ((unsigned)addr[3] & 0xff),
274 ((unsigned)addr[2] & 0xff),
275 ((unsigned)addr[1] & 0xff),
276 ((unsigned)addr[0] & 0xff));
0e9a6a67 277 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
f784656b 278 if (n < 0) {
d1c1ab68 279#ifdef DEBUG
f784656b 280 if (_res.options & RES_DEBUG)
0e9a6a67 281 printf("res_query failed\n");
d1c1ab68 282#endif
0e9a6a67 283 if (errno == ECONNREFUSED)
6d1927cd 284 return (_gethtbyaddr(addr, len, type));
e0313454 285 return ((struct hostent *) NULL);
f268b9fa 286 }
0e9a6a67 287 hp = getanswer(&buf, n, 1);
12379d92 288 if (hp == NULL)
e0313454 289 return ((struct hostent *) NULL);
4682c40f
JB
290 hp->h_addrtype = type;
291 hp->h_length = len;
c7683dcd 292 h_addr_ptrs[0] = (char *)&host_addr;
4682c40f
JB
293 h_addr_ptrs[1] = (char *)0;
294 host_addr = *(struct in_addr *)addr;
7468ba44
JB
295#if BSD < 43 && !defined(h_addr) /* new-style hostent structure */
296 hp->h_addr = h_addr_ptrs[0];
297#endif
4682c40f 298 return(hp);
337257b2 299}
c675dcaa 300
12379d92
KD
301_sethtent(f)
302 int f;
303{
304 if (hostf == NULL)
29eb0a7d 305 hostf = fopen(_PATH_HOSTS, "r" );
12379d92
KD
306 else
307 rewind(hostf);
308 stayopen |= f;
309}
310
311_endhtent()
312{
313 if (hostf && !stayopen) {
2795c575 314 (void) fclose(hostf);
12379d92
KD
315 hostf = NULL;
316 }
317}
318
319struct hostent *
320_gethtent()
321{
322 char *p;
323 register char *cp, **q;
324
29eb0a7d 325 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
12379d92
KD
326 return (NULL);
327again:
0e9a6a67 328 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
12379d92
KD
329 return (NULL);
330 if (*p == '#')
331 goto again;
81f99a97 332 cp = strpbrk(p, "#\n");
12379d92
KD
333 if (cp == NULL)
334 goto again;
335 *cp = '\0';
81f99a97 336 cp = strpbrk(p, " \t");
12379d92
KD
337 if (cp == NULL)
338 goto again;
339 *cp++ = '\0';
340 /* THIS STUFF IS INTERNET SPECIFIC */
15eda464 341#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
12379d92 342 host.h_addr_list = host_addrs;
da75bcd9 343#endif
12379d92
KD
344 host.h_addr = hostaddr;
345 *((u_long *)host.h_addr) = inet_addr(p);
346 host.h_length = sizeof (u_long);
347 host.h_addrtype = AF_INET;
348 while (*cp == ' ' || *cp == '\t')
349 cp++;
350 host.h_name = cp;
351 q = host.h_aliases = host_aliases;
81f99a97 352 cp = strpbrk(cp, " \t");
12379d92
KD
353 if (cp != NULL)
354 *cp++ = '\0';
355 while (cp && *cp) {
356 if (*cp == ' ' || *cp == '\t') {
357 cp++;
358 continue;
359 }
360 if (q < &host_aliases[MAXALIASES - 1])
361 *q++ = cp;
81f99a97 362 cp = strpbrk(cp, " \t");
12379d92
KD
363 if (cp != NULL)
364 *cp++ = '\0';
365 }
366 *q = NULL;
367 return (&host);
368}
369
12379d92
KD
370struct hostent *
371_gethtbyname(name)
372 char *name;
373{
374 register struct hostent *p;
375 register char **cp;
1e26987f 376
12379d92
KD
377 _sethtent(0);
378 while (p = _gethtent()) {
46d92023 379 if (strcasecmp(p->h_name, name) == 0)
12379d92
KD
380 break;
381 for (cp = p->h_aliases; *cp != 0; cp++)
46d92023 382 if (strcasecmp(*cp, name) == 0)
12379d92
KD
383 goto found;
384 }
385found:
386 _endhtent();
387 return (p);
388}
389
390struct hostent *
391_gethtbyaddr(addr, len, type)
24fac7d8 392 const char *addr;
12379d92
KD
393 int len, type;
394{
395 register struct hostent *p;
396
397 _sethtent(0);
398 while (p = _gethtent())
3f2cac51 399 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
12379d92
KD
400 break;
401 _endhtent();
402 return (p);
403}