ReactOS 0.4.16-dev-338-g34e76ad
ip4_addr.c
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 *
37 */
38
39#include "lwip/opt.h"
40
41#if LWIP_IPV4
42
43#include "lwip/ip_addr.h"
44#include "lwip/netif.h"
45
46/* used by IP4_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
47const ip_addr_t ip_addr_any = IPADDR4_INIT(IPADDR_ANY);
48const ip_addr_t ip_addr_broadcast = IPADDR4_INIT(IPADDR_BROADCAST);
49
57u8_t
58ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif)
59{
60 ip4_addr_t ipaddr;
61 ip4_addr_set_u32(&ipaddr, addr);
62
63 /* all ones (broadcast) or all zeroes (old skool broadcast) */
64 if ((~addr == IPADDR_ANY) ||
65 (addr == IPADDR_ANY)) {
66 return 1;
67 /* no broadcast support on this network interface? */
68 } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
69 /* the given address cannot be a broadcast address
70 * nor can we check against any broadcast addresses */
71 return 0;
72 /* address matches network interface address exactly? => no broadcast */
73 } else if (addr == ip4_addr_get_u32(netif_ip4_addr(netif))) {
74 return 0;
75 /* on the same (sub) network... */
76 } else if (ip4_addr_net_eq(&ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif))
77 /* ...and host identifier bits are all ones? =>... */
78 && ((addr & ~ip4_addr_get_u32(netif_ip4_netmask(netif))) ==
79 (IPADDR_BROADCAST & ~ip4_addr_get_u32(netif_ip4_netmask(netif))))) {
80 /* => network broadcast address */
81 return 1;
82 } else {
83 return 0;
84 }
85}
86
92u8_t
93ip4_addr_netmask_valid(u32_t netmask)
94{
95 u32_t mask;
96 u32_t nm_hostorder = lwip_htonl(netmask);
97
98 /* first, check for the first zero */
99 for (mask = 1UL << 31 ; mask != 0; mask >>= 1) {
100 if ((nm_hostorder & mask) == 0) {
101 break;
102 }
103 }
104 /* then check that there is no one */
105 for (; mask != 0; mask >>= 1) {
106 if ((nm_hostorder & mask) != 0) {
107 /* there is a one after the first zero -> invalid */
108 return 0;
109 }
110 }
111 /* no one after the first zero -> valid */
112 return 1;
113}
114
122u32_t
123ipaddr_addr(const char *cp)
124{
125 ip4_addr_t val;
126
127 if (ip4addr_aton(cp, &val)) {
128 return ip4_addr_get_u32(&val);
129 }
130 return (IPADDR_NONE);
131}
132
144int
145ip4addr_aton(const char *cp, ip4_addr_t *addr)
146{
147 u32_t val;
148 u8_t base;
149 char c;
150 u32_t parts[4];
151 u32_t *pp = parts;
152
153 c = *cp;
154 for (;;) {
155 /*
156 * Collect number up to ``.''.
157 * Values are specified as for C:
158 * 0x=hex, 0=octal, 1-9=decimal.
159 */
160 if (!lwip_isdigit(c)) {
161 return 0;
162 }
163 val = 0;
164 base = 10;
165 if (c == '0') {
166 c = *++cp;
167 if (c == 'x' || c == 'X') {
168 base = 16;
169 c = *++cp;
170 } else {
171 base = 8;
172 }
173 }
174 for (;;) {
175 if (lwip_isdigit(c)) {
176 if((base == 8) && ((u32_t)(c - '0') >= 8))
177 break;
178 val = (val * base) + (u32_t)(c - '0');
179 c = *++cp;
180 } else if (base == 16 && lwip_isxdigit(c)) {
181 val = (val << 4) | (u32_t)(c + 10 - (lwip_islower(c) ? 'a' : 'A'));
182 c = *++cp;
183 } else {
184 break;
185 }
186 }
187 if (c == '.') {
188 /*
189 * Internet format:
190 * a.b.c.d
191 * a.b.c (with c treated as 16 bits)
192 * a.b (with b treated as 24 bits)
193 */
194 if (pp >= parts + 3) {
195 return 0;
196 }
197 *pp++ = val;
198 c = *++cp;
199 } else {
200 break;
201 }
202 }
203 /*
204 * Check for trailing characters.
205 */
206 if (c != '\0' && !lwip_isspace(c)) {
207 return 0;
208 }
209 /*
210 * Concoct the address according to
211 * the number of parts specified.
212 */
213 switch (pp - parts + 1) {
214
215 case 0:
216 return 0; /* initial nondigit */
217
218 case 1: /* a -- 32 bits */
219 break;
220
221 case 2: /* a.b -- 8.24 bits */
222 if (val > 0xffffffUL) {
223 return 0;
224 }
225 if (parts[0] > 0xff) {
226 return 0;
227 }
228 val |= parts[0] << 24;
229 break;
230
231 case 3: /* a.b.c -- 8.8.16 bits */
232 if (val > 0xffff) {
233 return 0;
234 }
235 if ((parts[0] > 0xff) || (parts[1] > 0xff)) {
236 return 0;
237 }
238 val |= (parts[0] << 24) | (parts[1] << 16);
239 break;
240
241 case 4: /* a.b.c.d -- 8.8.8.8 bits */
242 if (val > 0xff) {
243 return 0;
244 }
245 if ((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
246 return 0;
247 }
248 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
249 break;
250 default:
251 LWIP_ASSERT("unhandled", 0);
252 break;
253 }
254 if (addr) {
255 ip4_addr_set_u32(addr, lwip_htonl(val));
256 }
257 return 1;
258}
259
268char *
269ip4addr_ntoa(const ip4_addr_t *addr)
270{
271 static char str[IP4ADDR_STRLEN_MAX];
272 return ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
273}
274
284char *
285ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
286{
288 char inv[3];
289 char *rp;
290 u8_t *ap;
291 u8_t rem;
292 u8_t n;
293 u8_t i;
294 int len = 0;
295
296 s_addr = ip4_addr_get_u32(addr);
297
298 rp = buf;
299 ap = (u8_t *)&s_addr;
300 for (n = 0; n < 4; n++) {
301 i = 0;
302 do {
303 rem = *ap % (u8_t)10;
304 *ap /= (u8_t)10;
305 inv[i++] = (char)('0' + rem);
306 } while (*ap);
307 while (i--) {
308 if (len++ >= buflen) {
309 return NULL;
310 }
311 *rp++ = inv[i];
312 }
313 if (len++ >= buflen) {
314 return NULL;
315 }
316 *rp++ = '.';
317 ap++;
318 }
319 *--rp = 0;
320 return buf;
321}
322
323#endif /* LWIP_IPV4 */
std::map< E_STRING, PART_TEST > parts
Definition: LocaleTests.cpp:67
#define lwip_htonl(x)
Definition: def.h:88
#define NULL
Definition: types.h:112
unsigned char
Definition: typeof.h:29
#define s_addr
Definition: tcpip.h:133
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
GLdouble n
Definition: glext.h:7729
const GLubyte * c
Definition: glext.h:8905
GLenum GLint GLuint mask
Definition: glext.h:6028
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum const GLvoid * addr
Definition: glext.h:9621
GLuint GLfloat * val
Definition: glext.h:7180
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define lwip_isdigit(c)
Definition: arch.h:229
uint32_t u32_t
Definition: arch.h:129
uint8_t u8_t
Definition: arch.h:125
#define lwip_islower(c)
Definition: arch.h:231
#define lwip_isxdigit(c)
Definition: arch.h:230
#define lwip_isspace(c)
Definition: arch.h:232
#define NETIF_FLAG_BROADCAST
Definition: netif.h:87
ip6_addr_t ip_addr_t
Definition: ip_addr.h:344
#define c
Definition: ke_i.h:80
POINT cp
Definition: magnifier.c:59
const WCHAR * str
Definition: netif.h:269
u8_t flags
Definition: netif.h:354
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36