xref: /onnv-gate/usr/src/stand/lib/wanboot/http_aux.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/salib.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/socket.h>
32*0Sstevel@tonic-gate #include <sys/bootvfs.h>
33*0Sstevel@tonic-gate #include <netinet/in.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * This structure defines the static area where gethostbyname()
37*0Sstevel@tonic-gate  * stores the hostent data that it returns to the caller.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate static struct {
40*0Sstevel@tonic-gate 	struct hostent	he;
41*0Sstevel@tonic-gate 	in_addr_t	*ha_list[2];
42*0Sstevel@tonic-gate 	in_addr_t	ha_addr;
43*0Sstevel@tonic-gate 	char		ha_name[MAXHOSTNAMELEN+1];
44*0Sstevel@tonic-gate } hostinfo;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate int h_errno;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static in_addr_t inet_addr(const char *);
49*0Sstevel@tonic-gate static in_addr_t nam2addr(const char *);
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /* Very stripped-down gethostbyname() */
52*0Sstevel@tonic-gate struct hostent *
gethostbyname(const char * nam)53*0Sstevel@tonic-gate gethostbyname(const char *nam)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	bzero(&hostinfo, sizeof (hostinfo));
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	hostinfo.ha_addr = inet_addr(nam);
58*0Sstevel@tonic-gate 	if ((int32_t)hostinfo.ha_addr == -1) {
59*0Sstevel@tonic-gate 		if (get_default_fs() == NULL) {
60*0Sstevel@tonic-gate 			h_errno = HOST_NOT_FOUND;
61*0Sstevel@tonic-gate 			return (NULL);
62*0Sstevel@tonic-gate 		}
63*0Sstevel@tonic-gate 		hostinfo.ha_addr = nam2addr(nam);
64*0Sstevel@tonic-gate 		if ((int32_t)hostinfo.ha_addr == -1) {
65*0Sstevel@tonic-gate 			h_errno = HOST_NOT_FOUND;
66*0Sstevel@tonic-gate 			return (NULL);
67*0Sstevel@tonic-gate 		}
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	hostinfo.he.h_addrtype = AF_INET;
71*0Sstevel@tonic-gate 	(void) strlcpy(hostinfo.ha_name, nam, MAXHOSTNAMELEN);
72*0Sstevel@tonic-gate 	hostinfo.he.h_name = hostinfo.ha_name;
73*0Sstevel@tonic-gate 	hostinfo.he.h_length = sizeof (struct in_addr);
74*0Sstevel@tonic-gate 	hostinfo.ha_list[0] = &hostinfo.ha_addr;
75*0Sstevel@tonic-gate 	hostinfo.he.h_addr_list = (char **)&hostinfo.ha_list;
76*0Sstevel@tonic-gate 	return (&hostinfo.he);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	SKIP_SPACE(_p)							\
80*0Sstevel@tonic-gate 	{								\
81*0Sstevel@tonic-gate 		char	_c;						\
82*0Sstevel@tonic-gate 		while ((_c = *(_p)) != '\0' && isspace(_c))		\
83*0Sstevel@tonic-gate 			p++;						\
84*0Sstevel@tonic-gate 		if (_c == '\0')						\
85*0Sstevel@tonic-gate 			goto next_line;					\
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate #define	SKIP_TOKEN(_p)							\
89*0Sstevel@tonic-gate 	{								\
90*0Sstevel@tonic-gate 		char	_c;						\
91*0Sstevel@tonic-gate 		while ((_c = *(_p)) != '\0' && !isspace(_c))		\
92*0Sstevel@tonic-gate 			p++;						\
93*0Sstevel@tonic-gate 		if (_c == '\0')						\
94*0Sstevel@tonic-gate 			goto next_line;					\
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate #define	TRIM_LINE(_l)							\
98*0Sstevel@tonic-gate 	{								\
99*0Sstevel@tonic-gate 		char	_c, *_p = (_l);					\
100*0Sstevel@tonic-gate 		while ((_c = *_p) != '#' && _c != '\n' && _c != '\0')	\
101*0Sstevel@tonic-gate 			_p++;						\
102*0Sstevel@tonic-gate 		*_p = '\0';						\
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate #define	BUFSZ	1024
106*0Sstevel@tonic-gate #define	HOSTDB	"/etc/inet/hosts"
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static in_addr_t
nam2addr(const char * nam)109*0Sstevel@tonic-gate nam2addr(const char *nam)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	FILE *h;
112*0Sstevel@tonic-gate 	char c, buf[BUFSZ];
113*0Sstevel@tonic-gate 	char *l, *p, *s;
114*0Sstevel@tonic-gate 	boolean_t first_token;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if ((h = fopen(HOSTDB, "r")) == NULL) {
117*0Sstevel@tonic-gate 		return ((in_addr_t)-1);
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate next_line:
121*0Sstevel@tonic-gate 	if ((l = fgets(buf, BUFSZ, h)) == NULL) {
122*0Sstevel@tonic-gate 		(void) fclose(h);
123*0Sstevel@tonic-gate 		return ((in_addr_t)-1);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 	TRIM_LINE(l);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	p = l;
128*0Sstevel@tonic-gate 	first_token = B_TRUE;
129*0Sstevel@tonic-gate next_token:
130*0Sstevel@tonic-gate 	SKIP_SPACE(p);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (first_token) {
133*0Sstevel@tonic-gate 		first_token = B_FALSE;
134*0Sstevel@tonic-gate 		SKIP_TOKEN(p);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		*p++ = '\0';
137*0Sstevel@tonic-gate 		goto next_token;
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	s = (char *)nam;
141*0Sstevel@tonic-gate 	if (*p++ == *s++) {
142*0Sstevel@tonic-gate 		while ((c = *s++) == *p && c != '\0')
143*0Sstevel@tonic-gate 			p++;
144*0Sstevel@tonic-gate 		if (c == '\0' && (isspace(*p) || *p == '\0'))
145*0Sstevel@tonic-gate 			goto match;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	SKIP_TOKEN(p);
149*0Sstevel@tonic-gate 	goto next_token;
150*0Sstevel@tonic-gate match:
151*0Sstevel@tonic-gate 	(void) fclose(h);
152*0Sstevel@tonic-gate 	return (inet_addr((const char *)l));
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate static in_addr_t
inet_addr(const char * cp)156*0Sstevel@tonic-gate inet_addr(const char *cp)
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate 	in_addr_t val;
159*0Sstevel@tonic-gate 	uint_t base, n;
160*0Sstevel@tonic-gate 	char c;
161*0Sstevel@tonic-gate 	in_addr_t parts[4], *pp = parts;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate again:
164*0Sstevel@tonic-gate 	/*
165*0Sstevel@tonic-gate 	 * Collect number up to ``.''.
166*0Sstevel@tonic-gate 	 * Values are specified as for C:
167*0Sstevel@tonic-gate 	 * 0x=hex, 0=octal, other=decimal.
168*0Sstevel@tonic-gate 	 */
169*0Sstevel@tonic-gate 	val = 0; base = 10;
170*0Sstevel@tonic-gate 	if (*cp == '0') {
171*0Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
172*0Sstevel@tonic-gate 			base = 16, cp++;
173*0Sstevel@tonic-gate 		else
174*0Sstevel@tonic-gate 			base = 8;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	while ((c = *cp) != 0) {
178*0Sstevel@tonic-gate 		if (isdigit(c)) {
179*0Sstevel@tonic-gate 			if ((c - '0') >= base)
180*0Sstevel@tonic-gate 				break;
181*0Sstevel@tonic-gate 			val = (val * base) + (c - '0');
182*0Sstevel@tonic-gate 			cp++;
183*0Sstevel@tonic-gate 			continue;
184*0Sstevel@tonic-gate 		}
185*0Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
186*0Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
187*0Sstevel@tonic-gate 			cp++;
188*0Sstevel@tonic-gate 			continue;
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 		break;
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 	if (*cp == '.') {
193*0Sstevel@tonic-gate 		/*
194*0Sstevel@tonic-gate 		 * Internet format:
195*0Sstevel@tonic-gate 		 *	a.b.c.d
196*0Sstevel@tonic-gate 		 *	a.b.c	(with c treated as 16-bits)
197*0Sstevel@tonic-gate 		 *	a.b	(with b treated as 24 bits)
198*0Sstevel@tonic-gate 		 */
199*0Sstevel@tonic-gate 		if (pp >= parts + 4)
200*0Sstevel@tonic-gate 			return ((in_addr_t)-1);
201*0Sstevel@tonic-gate 		*pp++ = val, cp++;
202*0Sstevel@tonic-gate 		goto again;
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 	/*
205*0Sstevel@tonic-gate 	 * Check for trailing characters.
206*0Sstevel@tonic-gate 	 */
207*0Sstevel@tonic-gate 	if (*cp && !isspace(*cp))
208*0Sstevel@tonic-gate 		return ((in_addr_t)-1);
209*0Sstevel@tonic-gate 	*pp++ = val;
210*0Sstevel@tonic-gate 	/*
211*0Sstevel@tonic-gate 	 * Concoct the address according to
212*0Sstevel@tonic-gate 	 * the number of parts specified.
213*0Sstevel@tonic-gate 	 */
214*0Sstevel@tonic-gate 	n = pp - parts;
215*0Sstevel@tonic-gate 	switch (n) {
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	case 1:			 /* a -- 32 bits */
218*0Sstevel@tonic-gate 		val = parts[0];
219*0Sstevel@tonic-gate 		break;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	case 2:			 /* a.b -- 8.24 bits */
222*0Sstevel@tonic-gate 		val = (parts[0] << 24) | (parts[1] & 0xffffff);
223*0Sstevel@tonic-gate 		break;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	case 3:			 /* a.b.c -- 8.8.16 bits */
226*0Sstevel@tonic-gate 		val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
227*0Sstevel@tonic-gate 			(parts[2] & 0xffff);
228*0Sstevel@tonic-gate 		break;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	case 4:			 /* a.b.c.d -- 8.8.8.8 bits */
231*0Sstevel@tonic-gate 		val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
232*0Sstevel@tonic-gate 			((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
233*0Sstevel@tonic-gate 		break;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	default:
236*0Sstevel@tonic-gate 		return ((in_addr_t)-1);
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 	val = htonl(val);
239*0Sstevel@tonic-gate 	return (val);
240*0Sstevel@tonic-gate }
241