ReactOS 0.4.15-dev-7693-gd0e2924
ip_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#include "lwip/ip_addr.h"
41#include "lwip/netif.h"
42
43/* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
46
54u8_t
56{
57 ip_addr_t ipaddr;
58 ip4_addr_set_u32(&ipaddr, addr);
59
60 /* all ones (broadcast) or all zeroes (old skool broadcast) */
61 if ((~addr == IPADDR_ANY) ||
62 (addr == IPADDR_ANY)) {
63 return 1;
64 /* no broadcast support on this network interface? */
65 } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
66 /* the given address cannot be a broadcast address
67 * nor can we check against any broadcast addresses */
68 return 0;
69 /* address matches network interface address exactly? => no broadcast */
70 } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) {
71 return 0;
72 /* on the same (sub) network... */
73 } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask))
74 /* ...and host identifier bits are all ones? =>... */
75 && ((addr & ~ip4_addr_get_u32(&netif->netmask)) ==
77 /* => network broadcast address */
78 return 1;
79 } else {
80 return 0;
81 }
82}
83
89u8_t
91{
92 u32_t mask;
93 u32_t nm_hostorder = lwip_htonl(netmask);
94
95 /* first, check for the first zero */
96 for (mask = 1UL << 31 ; mask != 0; mask >>= 1) {
97 if ((nm_hostorder & mask) == 0) {
98 break;
99 }
100 }
101 /* then check that there is no one */
102 for (; mask != 0; mask >>= 1) {
103 if ((nm_hostorder & mask) != 0) {
104 /* there is a one after the first zero -> invalid */
105 return 0;
106 }
107 }
108 /* no one after the first zero -> valid */
109 return 1;
110}
111
112/* Here for now until needed in other places in lwIP */
113#ifndef isprint
114#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
115#define isprint(c) in_range(c, 0x20, 0x7f)
116#define isdigit(c) in_range(c, '0', '9')
117#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
118#define islower(c) in_range(c, 'a', 'z')
119#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
120#endif
121
129u32_t
130ipaddr_addr(const char *cp)
131{
133
134 if (ipaddr_aton(cp, &val)) {
135 return ip4_addr_get_u32(&val);
136 }
137 return (IPADDR_NONE);
138}
139
151int
153{
154 u32_t val;
155 u8_t base;
156 char c;
157 u32_t parts[4];
158 u32_t *pp = parts;
159
160 c = *cp;
161 for (;;) {
162 /*
163 * Collect number up to ``.''.
164 * Values are specified as for C:
165 * 0x=hex, 0=octal, 1-9=decimal.
166 */
167 if (!isdigit(c))
168 return (0);
169 val = 0;
170 base = 10;
171 if (c == '0') {
172 c = *++cp;
173 if (c == 'x' || c == 'X') {
174 base = 16;
175 c = *++cp;
176 } else
177 base = 8;
178 }
179 for (;;) {
180 if (isdigit(c)) {
181 val = (val * base) + (int)(c - '0');
182 c = *++cp;
183 } else if (base == 16 && isxdigit(c)) {
184 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
185 c = *++cp;
186 } else
187 break;
188 }
189 if (c == '.') {
190 /*
191 * Internet format:
192 * a.b.c.d
193 * a.b.c (with c treated as 16 bits)
194 * a.b (with b treated as 24 bits)
195 */
196 if (pp >= parts + 3) {
197 return (0);
198 }
199 *pp++ = val;
200 c = *++cp;
201 } else
202 break;
203 }
204 /*
205 * Check for trailing characters.
206 */
207 if (c != '\0' && !isspace(c)) {
208 return (0);
209 }
210 /*
211 * Concoct the address according to
212 * the number of parts specified.
213 */
214 switch (pp - parts + 1) {
215
216 case 0:
217 return (0); /* initial nondigit */
218
219 case 1: /* a -- 32 bits */
220 break;
221
222 case 2: /* a.b -- 8.24 bits */
223 if (val > 0xffffffUL) {
224 return (0);
225 }
226 val |= parts[0] << 24;
227 break;
228
229 case 3: /* a.b.c -- 8.8.16 bits */
230 if (val > 0xffff) {
231 return (0);
232 }
233 val |= (parts[0] << 24) | (parts[1] << 16);
234 break;
235
236 case 4: /* a.b.c.d -- 8.8.8.8 bits */
237 if (val > 0xff) {
238 return (0);
239 }
240 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
241 break;
242 default:
243 LWIP_ASSERT("unhandled", 0);
244 break;
245 }
246 if (addr) {
248 }
249 return (1);
250}
251
260char *
262{
263 static char str[16];
264 return ipaddr_ntoa_r(addr, str, 16);
265}
266
276char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
277{
279 char inv[3];
280 char *rp;
281 u8_t *ap;
282 u8_t rem;
283 u8_t n;
284 u8_t i;
285 int len = 0;
286
288
289 rp = buf;
290 ap = (u8_t *)&s_addr;
291 for(n = 0; n < 4; n++) {
292 i = 0;
293 do {
294 rem = *ap % (u8_t)10;
295 *ap /= (u8_t)10;
296 inv[i++] = '0' + rem;
297 } while(*ap);
298 while(i--) {
299 if (len++ >= buflen) {
300 return NULL;
301 }
302 *rp++ = inv[i];
303 }
304 if (len++ >= buflen) {
305 return NULL;
306 }
307 *rp++ = '.';
308 ap++;
309 }
310 *--rp = 0;
311 return buf;
312}
#define lwip_htonl(x)
Definition: def.h:86
#define NULL
Definition: types.h:112
unsigned long u32_t
Definition: cc.h:25
unsigned char u8_t
Definition: cc.h:23
#define s_addr
Definition: tcpip.h:133
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
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 isspace(c)
Definition: ip_addr.c:119
char * ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
Definition: ip_addr.c:276
u32_t ipaddr_addr(const char *cp)
Definition: ip_addr.c:130
#define islower(c)
Definition: ip_addr.c:118
int ipaddr_aton(const char *cp, ip_addr_t *addr)
Definition: ip_addr.c:152
const ip_addr_t ip_addr_any
Definition: ip_addr.c:44
u8_t ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
Definition: ip_addr.c:55
#define isdigit(c)
Definition: ip_addr.c:116
#define isxdigit(c)
Definition: ip_addr.c:117
char * ipaddr_ntoa(const ip_addr_t *addr)
Definition: ip_addr.c:261
u8_t ip4_addr_netmask_valid(u32_t netmask)
Definition: ip_addr.c:90
const ip_addr_t ip_addr_broadcast
Definition: ip_addr.c:45
#define ip_addr_netcmp(addr1, addr2, mask)
Definition: ip_addr.h:194
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define IPADDR_NONE
Definition: ip_addr.h:96
#define IPADDR_BROADCAST
Definition: ip_addr.h:102
#define ip4_addr_get_u32(src_ipaddr)
Definition: ip_addr.h:181
#define IPADDR_ANY
Definition: ip_addr.h:100
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
Definition: ip_addr.h:179
#define c
Definition: ke_i.h:80
POINT cp
Definition: magnifier.c:59
#define htonl(x)
Definition: module.h:214
static const D3D_BLOB_PART parts[]
Definition: blob.c:76
#define NETIF_FLAG_BROADCAST
Definition: netif.h:72
const WCHAR * str
Definition: netif.h:136
u8_t flags
Definition: netif.h:192
ip_addr_t netmask
Definition: netif.h:142
ip_addr_t ip_addr
Definition: netif.h:141
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36