3 * This is the IPv4 address tools implementation.
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
40 #include "lwip/ip_addr.h"
41 #include "lwip/netif.h"
43 /* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
44 const ip_addr_t ip_addr_any = { IPADDR_ANY };
45 const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST };
48 * Determine if an address is a broadcast address on a network interface
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
55 ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
58 ip4_addr_set_u32(&ipaddr, addr);
60 /* all ones (broadcast) or all zeroes (old skool broadcast) */
61 if ((~addr == IPADDR_ANY) ||
62 (addr == IPADDR_ANY)) {
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 */
69 /* address matches network interface address exactly? => no broadcast */
70 } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) {
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 */
84 /** Checks if a netmask is valid (starting with ones, then only zeros)
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
90 ip4_addr_netmask_valid(u32_t netmask)
93 u32_t nm_hostorder = lwip_htonl(netmask);
95 /* first, check for the first zero */
96 for (mask = 1UL << 31 ; mask != 0; mask >>= 1) {
97 if ((nm_hostorder & mask) == 0) {
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 */
108 /* no one after the first zero -> valid */
112 /* Here for now until needed in other places in lwIP */
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')
123 * Ascii internet address interpretation routine.
124 * The value returned is in network order.
126 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
127 * @return ip address in network order
130 ipaddr_addr(const char *cp)
134 if (ipaddr_aton(cp, &val)) {
135 return ip4_addr_get_u32(&val);
137 return (IPADDR_NONE);
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.
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
152 ipaddr_aton(const char *cp, ip_addr_t *addr)
163 * Collect number up to ``.''.
164 * Values are specified as for C:
165 * 0x=hex, 0=octal, 1-9=decimal.
173 if (c == 'x' || c == 'X') {
181 val = (val * base) + (int)(c - '0');
183 } else if (base == 16 && isxdigit(c)) {
184 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
193 * a.b.c (with c treated as 16 bits)
194 * a.b (with b treated as 24 bits)
196 if (pp >= parts + 3) {
205 * Check for trailing characters.
207 if (c != '\0' && !isspace(c)) {
211 * Concoct the address according to
212 * the number of parts specified.
214 switch (pp - parts + 1) {
217 return (0); /* initial nondigit */
219 case 1: /* a -- 32 bits */
222 case 2: /* a.b -- 8.24 bits */
223 if (val > 0xffffffUL) {
226 val |= parts[0] << 24;
229 case 3: /* a.b.c -- 8.8.16 bits */
233 val |= (parts[0] << 24) | (parts[1] << 16);
236 case 4: /* a.b.c.d -- 8.8.8.8 bits */
240 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
243 LWIP_ASSERT("unhandled", 0);
247 ip4_addr_set_u32(addr, htonl(val));
253 * Convert numeric IP address into decimal dotted ASCII representation.
254 * returns ptr to static buffer; not reentrant!
256 * @param addr ip address in network order to convert
257 * @return pointer to a global static (!) buffer that holds the ASCII
258 * represenation of addr
261 ipaddr_ntoa(const ip_addr_t *addr)
264 return ipaddr_ntoa_r(addr, str, 16);
268 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
270 * @param addr ip address in network order to convert
271 * @param buf target buffer where the string is stored
272 * @param buflen length of buf
273 * @return either pointer to buf which now holds the ASCII
274 * representation of addr or NULL if buf was too small
276 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
287 s_addr = ip4_addr_get_u32(addr);
290 ap = (u8_t *)&s_addr;
291 for(n = 0; n < 4; n++) {
294 rem = *ap % (u8_t)10;
296 inv[i++] = '0' + rem;
299 if (len++ >= buflen) {
304 if (len++ >= buflen) {