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) */
64 if (~addr == IPADDR_ANY) {
65 return 1;
66 /* no broadcast support on this network interface? */
67 } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
68 /* the given address cannot be a broadcast address
69 * nor can we check against any broadcast addresses */
70 return 0;
71 /* address matches network interface address exactly? => no broadcast */
72 } else if (addr == ip4_addr_get_u32(netif_ip4_addr(netif))) {
73 return 0;
74 /* on the same (sub) network... */
75 } else if (ip4_addr_netcmp(&ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif))
76 /* ...and host identifier bits are all ones? =>... */
77 && ((addr & ~ip4_addr_get_u32(netif_ip4_netmask(netif))) ==
78 (IPADDR_BROADCAST & ~ip4_addr_get_u32(netif_ip4_netmask(netif))))) {
79 /* => network broadcast address */
80 return 1;
81 } else {
82 return 0;
83 }
84 }
85
86 /** Checks if a netmask is valid (starting with ones, then only zeros)
87 *
88 * @param netmask the IPv4 netmask to check (in network byte order!)
89 * @return 1 if the netmask is valid, 0 if it is not
90 */
91 u8_t
ip4_addr_netmask_valid(u32_t netmask)92 ip4_addr_netmask_valid(u32_t netmask)
93 {
94 u32_t mask;
95 u32_t nm_hostorder = lwip_htonl(netmask);
96
97 /* first, check for the first zero */
98 for (mask = 1UL << 31 ; mask != 0; mask >>= 1) {
99 if ((nm_hostorder & mask) == 0) {
100 break;
101 }
102 }
103 /* then check that there is no one */
104 for (; mask != 0; mask >>= 1) {
105 if ((nm_hostorder & mask) != 0) {
106 /* there is a one after the first zero -> invalid */
107 return 0;
108 }
109 }
110 /* no one after the first zero -> valid */
111 return 1;
112 }
113
114 /**
115 * Ascii internet address interpretation routine.
116 * The value returned is in network order.
117 *
118 * @param cp IP address in ascii representation (e.g. "127.0.0.1")
119 * @return ip address in network order
120 */
121 u32_t
ipaddr_addr(const char * cp)122 ipaddr_addr(const char *cp)
123 {
124 ip4_addr_t val;
125
126 LWIP_ERROR("ipaddr_addr:cp is NULL", (cp != NULL), return IPADDR_NONE);
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[IP4ADDR_STR_PARTS];
151 u32_t *pp = parts;
152
153 LWIP_ERROR("ip4addr_aton : invalid cp", (cp != NULL), return 0);
154
155 c = *cp;
156 for (;;) {
157 /*
158 * Collect number up to ``.''.
159 * Values are specified as for C:
160 * 0x=hex, 0=octal, 1-9=decimal.
161 */
162 if (!lwip_isdigit(c)) {
163 return 0;
164 }
165 val = 0;
166 base = 10;
167 if (c == '0') {
168 c = *++cp;
169 if (c == 'x' || c == 'X') {
170 base = 16;
171 c = *++cp;
172 } else {
173 base = 8;
174 }
175 }
176 for (;;) {
177 if (lwip_isdigit(c)) {
178 if((base == 8) && ((u32_t)(c - '0') >= 8))
179 break;
180 val = (val * base) + (u32_t)(c - '0');
181 c = *++cp;
182 } else if (base == 16 && lwip_isxdigit(c)) {
183 val = (val << 4) | (u32_t)(c + 10 - (lwip_islower(c) ? 'a' : 'A'));
184 c = *++cp;
185 } else {
186 break;
187 }
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 /*
206 * Check for trailing characters.
207 */
208 if (c != '\0' && !lwip_isspace(c)) {
209 return 0;
210 }
211 /*
212 * Concoct the address according to
213 * the number of parts specified.
214 */
215 switch (pp - parts + 1) {
216
217 case 0:
218 return 0; /* initial nondigit */
219
220 case 1: /* a -- 32 bits */
221 break;
222
223 case 2: /* a.b -- 8.24 bits */
224 if (val > 0xffffffUL) {
225 return 0;
226 }
227 if (parts[0] > 0xff) {
228 return 0;
229 }
230 val |= parts[0] << 24;
231 break;
232
233 case 3: /* a.b.c -- 8.8.16 bits */
234 if (val > 0xffff) {
235 return 0;
236 }
237 if ((parts[0] > 0xff) || (parts[1] > 0xff)) {
238 return 0;
239 }
240 val |= (parts[0] << 24) | (parts[1] << 16);
241 break;
242
243 case 4: /* a.b.c.d -- 8.8.8.8 bits */
244 if (val > 0xff) {
245 return 0;
246 }
247 if ((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
248 return 0;
249 }
250 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
251 break;
252 default:
253 LWIP_ASSERT("unhandled", 0);
254 break;
255 }
256 if (addr) {
257 ip4_addr_set_u32(addr, lwip_htonl(val));
258 }
259 return 1;
260 }
261
262 /**
263 * Convert numeric IP address into decimal dotted ASCII representation.
264 * returns ptr to static buffer; not reentrant!
265 *
266 * @param addr ip address in network order to convert
267 * @return pointer to a global static (!) buffer that holds the ASCII
268 * representation of addr
269 */
270 char *
ip4addr_ntoa(const ip4_addr_t * addr)271 ip4addr_ntoa(const ip4_addr_t *addr)
272 {
273 static char str[IP4ADDR_STRLEN_MAX];
274 return ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
275 }
276
277 /**
278 * Same as ip4addr_ntoa, but reentrant since a user-supplied buffer is used.
279 *
280 * @param addr ip address in network order to convert
281 * @param buf target buffer where the string is stored
282 * @param buflen length of buf
283 * @return either pointer to buf which now holds the ASCII
284 * representation of addr or NULL if buf was too small
285 */
286 char *
ip4addr_ntoa_r(const ip4_addr_t * addr,char * buf,int buflen)287 ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
288 {
289 u32_t s_addr;
290 char inv[IP4ADDR_STR_PARTS];
291 char *rp;
292 u8_t *ap;
293 u8_t rem;
294 u8_t n;
295 u8_t i;
296 int len = 0;
297
298 LWIP_ERROR("Either addr or buf is NULL", ((addr != NULL) && (buf != NULL)),
299 return NULL);
300
301 s_addr = ip4_addr_get_u32(addr);
302
303 rp = buf;
304 ap = (u8_t *)&s_addr;
305 for (n = 0; n < IP4ADDR_STR_PARTS; n++) {
306 i = 0;
307 do {
308 rem = *ap % (u8_t)10;
309 *ap /= (u8_t)10;
310 inv[i++] = (char)('0' + rem);
311 } while (*ap);
312 while (i--) {
313 if (len++ >= buflen) {
314 return NULL;
315 }
316 *rp++ = inv[i];
317 }
318 if (len++ >= buflen) {
319 return NULL;
320 }
321 *rp++ = '.';
322 ap++;
323 }
324 *--rp = 0;
325 return buf;
326 }
327
328 #endif /* LWIP_IPV4 */
329