• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 */
47 const ip_addr_t ip_addr_any = IPADDR4_INIT(IPADDR_ANY);
48 const ip_addr_t ip_addr_broadcast = IPADDR4_INIT(IPADDR_BROADCAST);
49 
50 /**
51  * Determine if an address is a broadcast address on a network interface
52  *
53  * @param addr address to be checked
54  * @param netif the network interface against which the address is checked
55  * @return returns non-zero if the address is a broadcast address
56  */
57 u8_t
ip4_addr_isbroadcast_u32(u32_t addr,const struct netif * netif)58 ip4_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_netcmp(&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 
87 /** Checks if a netmask is valid (starting with ones, then only zeros)
88  *
89  * @param netmask the IPv4 netmask to check (in network byte order!)
90  * @return 1 if the netmask is valid, 0 if it is not
91  */
92 u8_t
ip4_addr_netmask_valid(u32_t netmask)93 ip4_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 
115 /**
116  * Ascii internet address interpretation routine.
117  * The value returned is in network order.
118  *
119  * @param cp IP address in ascii representation (e.g. "127.0.0.1")
120  * @return ip address in network order
121  */
122 u32_t
ipaddr_addr(const char * cp)123 ipaddr_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 
133 /**
134  * Check whether "cp" is a valid ascii representation
135  * of an Internet address and convert to a binary address.
136  * Returns 1 if the address is valid, 0 if not.
137  * This replaces inet_addr, the return value from which
138  * cannot distinguish between failure and a local broadcast address.
139  *
140  * @param cp IP address in ascii representation (e.g. "127.0.0.1")
141  * @param addr pointer to which to save the ip address in network order
142  * @return 1 if cp could be converted to addr, 0 on failure
143  */
144 int
ip4addr_aton(const char * cp,ip4_addr_t * addr)145 ip4addr_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 
260 /**
261  * Convert numeric IP address into decimal dotted ASCII representation.
262  * returns ptr to static buffer; not reentrant!
263  *
264  * @param addr ip address in network order to convert
265  * @return pointer to a global static (!) buffer that holds the ASCII
266  *         representation of addr
267  */
268 char *
ip4addr_ntoa(const ip4_addr_t * addr)269 ip4addr_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 
275 /**
276  * Same as ip4addr_ntoa, but reentrant since a user-supplied buffer is used.
277  *
278  * @param addr ip address in network order to convert
279  * @param buf target buffer where the string is stored
280  * @param buflen length of buf
281  * @return either pointer to buf which now holds the ASCII
282  *         representation of addr or NULL if buf was too small
283  */
284 char *
ip4addr_ntoa_r(const ip4_addr_t * addr,char * buf,int buflen)285 ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
286 {
287   u32_t s_addr;
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 */
324