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