MeterLogger
ip_addr.c
Go to the documentation of this file.
1 /**
2  * @file
3  * This is the IPv4 address tools implementation.
4  *
5  */
6 
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 
47 /**
48  * Determine if an address is a broadcast address on a network interface
49  *
50  * @param addr address to be checked
51  * @param netif the network interface against which the address is checked
52  * @return returns non-zero if the address is a broadcast address
53  */
54 u8_t
55 ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
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)) ==
76  (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) {
77  /* => network broadcast address */
78  return 1;
79  } else {
80  return 0;
81  }
82 }
83 
84 /** Checks if a netmask is valid (starting with ones, then only zeros)
85  *
86  * @param netmask the IPv4 netmask to check (in network byte order!)
87  * @return 1 if the netmask is valid, 0 if it is not
88  */
89 u8_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 = 1U << 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 
122 /**
123  * Ascii internet address interpretation routine.
124  * The value returned is in network order.
125  *
126  * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
127  * @return ip address in network order
128  */
129 u32_t
130 ipaddr_addr(const char *cp)
131 {
132  ip_addr_t val;
133 
134  if (ipaddr_aton(cp, &val)) {
135  return ip4_addr_get_u32(&val);
136  }
137  return (IPADDR_NONE);
138 }
139 
140 /**
141  * Check whether "cp" is a valid ascii representation
142  * of an Internet address and convert to a binary address.
143  * Returns 1 if the address is valid, 0 if not.
144  * This replaces inet_addr, the return value from which
145  * cannot distinguish between failure and a local broadcast address.
146  *
147  * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
148  * @param addr pointer to which to save the ip address in network order
149  * @return 1 if cp could be converted to addr, 0 on failure
150  */
151 int
152 ipaddr_aton(const char *cp, ip_addr_t *addr)
153 {
154  u32_t val;
155  u8_t base;
156  char c;
157  char ch;
158  unsigned long cutoff;
159  int cutlim;
160  u32_t parts[4];
161  u32_t *pp = parts;
162 
163  c = *cp;
164  for (;;) {
165  /*
166  * Collect number up to ``.''.
167  * Values are specified as for C:
168  * 0x=hex, 0=octal, 1-9=decimal.
169  */
170  if (!isdigit(c))
171  return (0);
172  val = 0;
173  base = 10;
174  if (c == '0') {
175  c = *++cp;
176  if (c == 'x' || c == 'X') {
177  base = 16;
178  c = *++cp;
179  } else
180  base = 8;
181  }
182 
183  cutoff =(unsigned long)0xffffffff / (unsigned long)base;
184  cutlim =(unsigned long)0xffffffff % (unsigned long)base;
185 
186  for (;;) {
187  if (isdigit(c)) {
188  ch = (int)(c - '0');
189 
190  if (val > cutoff || (val == cutoff && ch > cutlim))
191  return (0);
192 
193  val = (val * base) + (int)(c - '0');
194  c = *++cp;
195  } else if (base == 16 && isxdigit(c)) {
196  ch = (int)(c + 10 - (islower(c) ? 'a' : 'A'));
197 
198  if (val > cutoff || (val == cutoff && ch > cutlim))
199  return (0);
200 
201  val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
202  c = *++cp;
203  } else
204  break;
205  }
206  if (c == '.') {
207  /*
208  * Internet format:
209  * a.b.c.d
210  * a.b.c (with c treated as 16 bits)
211  * a.b (with b treated as 24 bits)
212  */
213  if (pp >= parts + 3) {
214  return (0);
215  }
216  *pp++ = val;
217  c = *++cp;
218  } else
219  break;
220  }
221  /*
222  * Check for trailing characters.
223  */
224  if (c != '\0' && !isspace(c)) {
225  return (0);
226  }
227  /*
228  * Concoct the address according to
229  * the number of parts specified.
230  */
231  switch (pp - parts + 1) {
232 
233  case 0:
234  return (0); /* initial nondigit */
235 
236  case 1: /* a -- 32 bits */
237  break;
238 
239  case 2: /* a.b -- 8.24 bits */
240  if ((val > 0xffffffUL) || (parts[0] > 0xff)) {
241  return (0);
242  }
243  val |= parts[0] << 24;
244  break;
245 
246  case 3: /* a.b.c -- 8.8.16 bits */
247  if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) {
248  return (0);
249  }
250  val |= (parts[0] << 24) | (parts[1] << 16);
251  break;
252 
253  case 4: /* a.b.c.d -- 8.8.8.8 bits */
254  if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
255  return (0);
256  }
257  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
258  break;
259  default:
260  LWIP_ASSERT("unhandled", 0);
261  break;
262  }
263  if (addr) {
264  ip4_addr_set_u32(addr, htonl(val));
265  }
266  return (1);
267 }
268 
269 /**
270  * Convert numeric IP address into decimal dotted ASCII representation.
271  * returns ptr to static buffer; not reentrant!
272  *
273  * @param addr ip address in network order to convert
274  * @return pointer to a global static (!) buffer that holds the ASCII
275  * represenation of addr
276  */
277 char *
278 ipaddr_ntoa(const ip_addr_t *addr)
279 {
280  static char str[16];
281  return ipaddr_ntoa_r(addr, str, 16);
282 }
283 
284 /**
285  * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
286  *
287  * @param addr ip address in network order to convert
288  * @param buf target buffer where the string is stored
289  * @param buflen length of buf
290  * @return either pointer to buf which now holds the ASCII
291  * representation of addr or NULL if buf was too small
292  */
293 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
294 {
295  u32_t s_addr;
296  char inv[3];
297  char *rp;
298  u8_t *ap;
299  u8_t rem;
300  u8_t n;
301  u8_t i;
302  int len = 0;
303 
304  s_addr = ip4_addr_get_u32(addr);
305 
306  rp = buf;
307  ap = (u8_t *)&s_addr;
308  for(n = 0; n < 4; n++) {
309  i = 0;
310  do {
311  rem = *ap % (u8_t)10;
312  *ap /= (u8_t)10;
313  inv[i++] = '0' + rem;
314  } while(*ap);
315  while(i--) {
316  if (len++ >= buflen) {
317  return NULL;
318  }
319  *rp++ = inv[i];
320  }
321  if (len++ >= buflen) {
322  return NULL;
323  }
324  *rp++ = '.';
325  ap++;
326  }
327  *--rp = 0;
328  return buf;
329 }
#define ip4_addr_get_u32(src_ipaddr)
Definition: ip_addr.h:181
#define isspace(c)
Definition: ip_addr.c:119
#define isxdigit(c)
Definition: ip_addr.c:117
#define ip_addr_netcmp(addr1, addr2, mask)
Definition: ip_addr.h:194
u8_t ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
Definition: ip_addr.c:55
#define NULL
Definition: def.h:47
char * ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
Definition: ip_addr.c:293
const ip_addr_t ip_addr_any ICACHE_RODATA_ATTR
Definition: ip_addr.c:44
char * ipaddr_ntoa(const ip_addr_t *addr)
Definition: ip_addr.c:278
u32_t lwip_htonl(u32_t n)
Definition: def.c:88
#define IPADDR_ANY
Definition: ip_addr.h:100
unsigned long u32_t
Definition: cc.h:56
u8_t flags
Definition: netif.h:193
#define NETIF_FLAG_BROADCAST
Definition: netif.h:72
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
Definition: netif.h:139
#define IPADDR_BROADCAST
Definition: ip_addr.h:102
ip_addr_t ip_addr
Definition: netif.h:144
u32_t ipaddr_addr(const char *cp)
Definition: ip_addr.c:130
u8_t ip4_addr_netmask_valid(u32_t netmask)
Definition: ip_addr.c:90
#define islower(c)
Definition: ip_addr.c:118
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
int ipaddr_aton(const char *cp, ip_addr_t *addr)
Definition: ip_addr.c:152
ip_addr_t netmask
Definition: netif.h:145
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
Definition: ip_addr.h:179
const ip_addr_t ip_addr_broadcast
#define htonl(x)
Definition: def.h:83
const ip_addr_t ip_addr_any
#define IPADDR_NONE
Definition: ip_addr.h:96