The Pedigree Project  0.1
ip4_addr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
26 /*
27  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without modification,
31  * are permitted provided that the following conditions are met:
32  *
33  * 1. Redistributions of source code must retain the above copyright notice,
34  * this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright notice,
36  * this list of conditions and the following disclaimer in the documentation
37  * and/or other materials provided with the distribution.
38  * 3. The name of the author may not be used to endorse or promote products
39  * derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
44  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
46  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
49  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
50  * OF SUCH DAMAGE.
51  *
52  * This file is part of the lwIP TCP/IP stack.
53  *
54  * Author: Adam Dunkels <adam@sics.se>
55  *
56  */
57 
58 #include "lwip/opt.h"
59 
60 #if LWIP_IPV4
61 
62 #include "lwip/ip_addr.h"
63 #include "lwip/netif.h"
64 
65 /* used by IP4_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
66 const ip_addr_t ip_addr_any = IPADDR4_INIT(IPADDR_ANY);
67 const ip_addr_t ip_addr_broadcast = IPADDR4_INIT(IPADDR_BROADCAST);
68 
76 u8_t
77 ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif)
78 {
79  ip4_addr_t ipaddr;
80  ip4_addr_set_u32(&ipaddr, addr);
81 
82  /* all ones (broadcast) or all zeroes (old skool broadcast) */
83  if ((~addr == IPADDR_ANY) ||
84  (addr == IPADDR_ANY)) {
85  return 1;
86  /* no broadcast support on this network interface? */
87  } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
88  /* the given address cannot be a broadcast address
89  * nor can we check against any broadcast addresses */
90  return 0;
91  /* address matches network interface address exactly? => no broadcast */
92  } else if (addr == ip4_addr_get_u32(netif_ip4_addr(netif))) {
93  return 0;
94  /* on the same (sub) network... */
95  } else if (ip4_addr_netcmp(&ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif))
96  /* ...and host identifier bits are all ones? =>... */
97  && ((addr & ~ip4_addr_get_u32(netif_ip4_netmask(netif))) ==
98  (IPADDR_BROADCAST & ~ip4_addr_get_u32(netif_ip4_netmask(netif))))) {
99  /* => network broadcast address */
100  return 1;
101  } else {
102  return 0;
103  }
104 }
105 
111 u8_t
112 ip4_addr_netmask_valid(u32_t netmask)
113 {
114  u32_t mask;
115  u32_t nm_hostorder = lwip_htonl(netmask);
116 
117  /* first, check for the first zero */
118  for (mask = 1UL << 31 ; mask != 0; mask >>= 1) {
119  if ((nm_hostorder & mask) == 0) {
120  break;
121  }
122  }
123  /* then check that there is no one */
124  for (; mask != 0; mask >>= 1) {
125  if ((nm_hostorder & mask) != 0) {
126  /* there is a one after the first zero -> invalid */
127  return 0;
128  }
129  }
130  /* no one after the first zero -> valid */
131  return 1;
132 }
133 
134 /* Here for now until needed in other places in lwIP */
135 #ifndef isprint
136 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
137 #define isprint(c) in_range(c, 0x20, 0x7f)
138 #define isdigit(c) in_range(c, '0', '9')
139 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
140 #define islower(c) in_range(c, 'a', 'z')
141 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
142 #endif
143 
151 u32_t
152 ipaddr_addr(const char *cp)
153 {
154  ip4_addr_t val;
155 
156  if (ip4addr_aton(cp, &val)) {
157  return ip4_addr_get_u32(&val);
158  }
159  return (IPADDR_NONE);
160 }
161 
173 int
174 ip4addr_aton(const char *cp, ip4_addr_t *addr)
175 {
176  u32_t val;
177  u8_t base;
178  char c;
179  u32_t parts[4];
180  u32_t *pp = parts;
181 
182  c = *cp;
183  for (;;) {
184  /*
185  * Collect number up to ``.''.
186  * Values are specified as for C:
187  * 0x=hex, 0=octal, 1-9=decimal.
188  */
189  if (!isdigit(c)) {
190  return 0;
191  }
192  val = 0;
193  base = 10;
194  if (c == '0') {
195  c = *++cp;
196  if (c == 'x' || c == 'X') {
197  base = 16;
198  c = *++cp;
199  } else {
200  base = 8;
201  }
202  }
203  for (;;) {
204  if (isdigit(c)) {
205  val = (val * base) + (u32_t)(c - '0');
206  c = *++cp;
207  } else if (base == 16 && isxdigit(c)) {
208  val = (val << 4) | (u32_t)(c + 10 - (islower(c) ? 'a' : 'A'));
209  c = *++cp;
210  } else {
211  break;
212  }
213  }
214  if (c == '.') {
215  /*
216  * Internet format:
217  * a.b.c.d
218  * a.b.c (with c treated as 16 bits)
219  * a.b (with b treated as 24 bits)
220  */
221  if (pp >= parts + 3) {
222  return 0;
223  }
224  *pp++ = val;
225  c = *++cp;
226  } else {
227  break;
228  }
229  }
230  /*
231  * Check for trailing characters.
232  */
233  if (c != '\0' && !isspace(c)) {
234  return 0;
235  }
236  /*
237  * Concoct the address according to
238  * the number of parts specified.
239  */
240  switch (pp - parts + 1) {
241 
242  case 0:
243  return 0; /* initial nondigit */
244 
245  case 1: /* a -- 32 bits */
246  break;
247 
248  case 2: /* a.b -- 8.24 bits */
249  if (val > 0xffffffUL) {
250  return 0;
251  }
252  if (parts[0] > 0xff) {
253  return 0;
254  }
255  val |= parts[0] << 24;
256  break;
257 
258  case 3: /* a.b.c -- 8.8.16 bits */
259  if (val > 0xffff) {
260  return 0;
261  }
262  if ((parts[0] > 0xff) || (parts[1] > 0xff)) {
263  return 0;
264  }
265  val |= (parts[0] << 24) | (parts[1] << 16);
266  break;
267 
268  case 4: /* a.b.c.d -- 8.8.8.8 bits */
269  if (val > 0xff) {
270  return 0;
271  }
272  if ((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
273  return 0;
274  }
275  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
276  break;
277  default:
278  LWIP_ASSERT("unhandled", 0);
279  break;
280  }
281  if (addr) {
282  ip4_addr_set_u32(addr, lwip_htonl(val));
283  }
284  return 1;
285 }
286 
295 char*
296 ip4addr_ntoa(const ip4_addr_t *addr)
297 {
298  static char str[IP4ADDR_STRLEN_MAX];
299  return ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
300 }
301 
311 char*
312 ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
313 {
314  u32_t s_addr;
315  char inv[3];
316  char *rp;
317  u8_t *ap;
318  u8_t rem;
319  u8_t n;
320  u8_t i;
321  int len = 0;
322 
323  s_addr = ip4_addr_get_u32(addr);
324 
325  rp = buf;
326  ap = (u8_t *)&s_addr;
327  for (n = 0; n < 4; n++) {
328  i = 0;
329  do {
330  rem = *ap % (u8_t)10;
331  *ap /= (u8_t)10;
332  inv[i++] = (char)('0' + rem);
333  } while (*ap);
334  while (i--) {
335  if (len++ >= buflen) {
336  return NULL;
337  }
338  *rp++ = inv[i];
339  }
340  if (len++ >= buflen) {
341  return NULL;
342  }
343  *rp++ = '.';
344  ap++;
345  }
346  *--rp = 0;
347  return buf;
348 }
349 
350 #endif /* LWIP_IPV4 */
#define NETIF_FLAG_BROADCAST
Definition: netif.h:100
u8_t flags
Definition: netif.h:324
Definition: netif.h:244