1*0Sstevel@tonic-gate /* $OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $ */
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate * ++Copyright++ 1983, 1990, 1993
5*0Sstevel@tonic-gate * -
6*0Sstevel@tonic-gate * Copyright (c) 1983, 1990, 1993
7*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
10*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
11*0Sstevel@tonic-gate * are met:
12*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
15*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
16*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
17*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
18*0Sstevel@tonic-gate * must display the following acknowledgement:
19*0Sstevel@tonic-gate * This product includes software developed by the University of
20*0Sstevel@tonic-gate * California, Berkeley and its contributors.
21*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
22*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
23*0Sstevel@tonic-gate * without specific prior written permission.
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*0Sstevel@tonic-gate * SUCH DAMAGE.
36*0Sstevel@tonic-gate * -
37*0Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38*0Sstevel@tonic-gate *
39*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
40*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
41*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that
42*0Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
43*0Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
44*0Sstevel@tonic-gate * specific, written prior permission.
45*0Sstevel@tonic-gate *
46*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47*0Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49*0Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53*0Sstevel@tonic-gate * SOFTWARE.
54*0Sstevel@tonic-gate * -
55*0Sstevel@tonic-gate * --Copyright--
56*0Sstevel@tonic-gate */
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate #include "includes.h"
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate #if !defined(HAVE_INET_ATON)
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
63*0Sstevel@tonic-gate #if 0
64*0Sstevel@tonic-gate static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
65*0Sstevel@tonic-gate static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $";
66*0Sstevel@tonic-gate #else
67*0Sstevel@tonic-gate static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $";
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate #include <sys/types.h>
72*0Sstevel@tonic-gate #include <sys/param.h>
73*0Sstevel@tonic-gate #include <netinet/in.h>
74*0Sstevel@tonic-gate #include <arpa/inet.h>
75*0Sstevel@tonic-gate #include <ctype.h>
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate #if 0
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate * Ascii internet address interpretation routine.
80*0Sstevel@tonic-gate * The value returned is in network order.
81*0Sstevel@tonic-gate */
82*0Sstevel@tonic-gate in_addr_t
83*0Sstevel@tonic-gate inet_addr(cp)
84*0Sstevel@tonic-gate register const char *cp;
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate struct in_addr val;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate if (inet_aton(cp, &val))
89*0Sstevel@tonic-gate return (val.s_addr);
90*0Sstevel@tonic-gate return (INADDR_NONE);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate #endif
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate * Check whether "cp" is a valid ascii representation
96*0Sstevel@tonic-gate * of an Internet address and convert to a binary address.
97*0Sstevel@tonic-gate * Returns 1 if the address is valid, 0 if not.
98*0Sstevel@tonic-gate * This replaces inet_addr, the return value from which
99*0Sstevel@tonic-gate * cannot distinguish between failure and a local broadcast address.
100*0Sstevel@tonic-gate */
101*0Sstevel@tonic-gate int
inet_aton(const char * cp,struct in_addr * addr)102*0Sstevel@tonic-gate inet_aton(const char *cp, struct in_addr *addr)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate register u_int32_t val;
105*0Sstevel@tonic-gate register int base, n;
106*0Sstevel@tonic-gate register char c;
107*0Sstevel@tonic-gate unsigned int parts[4];
108*0Sstevel@tonic-gate register unsigned int *pp = parts;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate c = *cp;
111*0Sstevel@tonic-gate for (;;) {
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate * Collect number up to ``.''.
114*0Sstevel@tonic-gate * Values are specified as for C:
115*0Sstevel@tonic-gate * 0x=hex, 0=octal, isdigit=decimal.
116*0Sstevel@tonic-gate */
117*0Sstevel@tonic-gate if (!isdigit(c))
118*0Sstevel@tonic-gate return (0);
119*0Sstevel@tonic-gate val = 0; base = 10;
120*0Sstevel@tonic-gate if (c == '0') {
121*0Sstevel@tonic-gate c = *++cp;
122*0Sstevel@tonic-gate if (c == 'x' || c == 'X')
123*0Sstevel@tonic-gate base = 16, c = *++cp;
124*0Sstevel@tonic-gate else
125*0Sstevel@tonic-gate base = 8;
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate for (;;) {
128*0Sstevel@tonic-gate if (isascii(c) && isdigit(c)) {
129*0Sstevel@tonic-gate val = (val * base) + (c - '0');
130*0Sstevel@tonic-gate c = *++cp;
131*0Sstevel@tonic-gate } else if (base == 16 && isascii(c) && isxdigit(c)) {
132*0Sstevel@tonic-gate val = (val << 4) |
133*0Sstevel@tonic-gate (c + 10 - (islower(c) ? 'a' : 'A'));
134*0Sstevel@tonic-gate c = *++cp;
135*0Sstevel@tonic-gate } else
136*0Sstevel@tonic-gate break;
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate if (c == '.') {
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate * Internet format:
141*0Sstevel@tonic-gate * a.b.c.d
142*0Sstevel@tonic-gate * a.b.c (with c treated as 16 bits)
143*0Sstevel@tonic-gate * a.b (with b treated as 24 bits)
144*0Sstevel@tonic-gate */
145*0Sstevel@tonic-gate if (pp >= parts + 3)
146*0Sstevel@tonic-gate return (0);
147*0Sstevel@tonic-gate *pp++ = val;
148*0Sstevel@tonic-gate c = *++cp;
149*0Sstevel@tonic-gate } else
150*0Sstevel@tonic-gate break;
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate * Check for trailing characters.
154*0Sstevel@tonic-gate */
155*0Sstevel@tonic-gate if (c != '\0' && (!isascii(c) || !isspace(c)))
156*0Sstevel@tonic-gate return (0);
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate * Concoct the address according to
159*0Sstevel@tonic-gate * the number of parts specified.
160*0Sstevel@tonic-gate */
161*0Sstevel@tonic-gate n = pp - parts + 1;
162*0Sstevel@tonic-gate switch (n) {
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate case 0:
165*0Sstevel@tonic-gate return (0); /* initial nondigit */
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate case 1: /* a -- 32 bits */
168*0Sstevel@tonic-gate break;
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate case 2: /* a.b -- 8.24 bits */
171*0Sstevel@tonic-gate if ((val > 0xffffff) || (parts[0] > 0xff))
172*0Sstevel@tonic-gate return (0);
173*0Sstevel@tonic-gate val |= parts[0] << 24;
174*0Sstevel@tonic-gate break;
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate case 3: /* a.b.c -- 8.8.16 bits */
177*0Sstevel@tonic-gate if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
178*0Sstevel@tonic-gate return (0);
179*0Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16);
180*0Sstevel@tonic-gate break;
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate case 4: /* a.b.c.d -- 8.8.8.8 bits */
183*0Sstevel@tonic-gate if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
184*0Sstevel@tonic-gate return (0);
185*0Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
186*0Sstevel@tonic-gate break;
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate if (addr)
189*0Sstevel@tonic-gate addr->s_addr = htonl(val);
190*0Sstevel@tonic-gate return (1);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate #endif /* !defined(HAVE_INET_ATON) */
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
196