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
54 u8_t ip_addr_isbroadcast(const ip_addr_t *addr, const struct netif *netif)
58 addr2test = ip4_addr_get_u32(addr);
59 /* all ones (broadcast) or all zeroes (old skool broadcast) */
60 if ((~addr2test == IPADDR_ANY) ||
61 (addr2test == IPADDR_ANY))
63 /* no broadcast support on this network interface? */
64 else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0)
65 /* the given address cannot be a broadcast address
66 * nor can we check against any broadcast addresses */
68 /* address matches network interface address exactly? => no broadcast */
69 else if (addr2test == ip4_addr_get_u32(&netif->ip_addr))
71 /* on the same (sub) network... */
72 else if (ip_addr_netcmp(addr, &(netif->ip_addr), &(netif->netmask))
73 /* ...and host identifier bits are all ones? =>... */
74 && ((addr2test & ~ip4_addr_get_u32(&netif->netmask)) ==
75 (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask))))
76 /* => network broadcast address */
82 /* Here for now until needed in other places in lwIP */
84 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
85 #define isprint(c) in_range(c, 0x20, 0x7f)
86 #define isdigit(c) in_range(c, '0', '9')
87 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
88 #define islower(c) in_range(c, 'a', 'z')
89 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
93 * Ascii internet address interpretation routine.
94 * The value returned is in network order.
96 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
97 * @return ip address in network order
100 ipaddr_addr(const char *cp)
104 if (ipaddr_aton(cp, &val)) {
105 return ip4_addr_get_u32(&val);
107 return (IPADDR_NONE);
111 * Check whether "cp" is a valid ascii representation
112 * of an Internet address and convert to a binary address.
113 * Returns 1 if the address is valid, 0 if not.
114 * This replaces inet_addr, the return value from which
115 * cannot distinguish between failure and a local broadcast address.
117 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
118 * @param addr pointer to which to save the ip address in network order
119 * @return 1 if cp could be converted to addr, 0 on failure
122 ipaddr_aton(const char *cp, ip_addr_t *addr)
133 * Collect number up to ``.''.
134 * Values are specified as for C:
135 * 0x=hex, 0=octal, 1-9=decimal.
143 if (c == 'x' || c == 'X') {
151 val = (val * base) + (int)(c - '0');
153 } else if (base == 16 && isxdigit(c)) {
154 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
163 * a.b.c (with c treated as 16 bits)
164 * a.b (with b treated as 24 bits)
166 if (pp >= parts + 3) {
175 * Check for trailing characters.
177 if (c != '\0' && !isspace(c)) {
181 * Concoct the address according to
182 * the number of parts specified.
184 switch (pp - parts + 1) {
187 return (0); /* initial nondigit */
189 case 1: /* a -- 32 bits */
192 case 2: /* a.b -- 8.24 bits */
193 if (val > 0xffffffUL) {
196 val |= parts[0] << 24;
199 case 3: /* a.b.c -- 8.8.16 bits */
203 val |= (parts[0] << 24) | (parts[1] << 16);
206 case 4: /* a.b.c.d -- 8.8.8.8 bits */
210 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
213 LWIP_ASSERT("unhandled", 0);
217 ip4_addr_set_u32(addr, htonl(val));
223 * Convert numeric IP address into decimal dotted ASCII representation.
224 * returns ptr to static buffer; not reentrant!
226 * @param addr ip address in network order to convert
227 * @return pointer to a global static (!) buffer that holds the ASCII
228 * represenation of addr
231 ipaddr_ntoa(const ip_addr_t *addr)
234 return ipaddr_ntoa_r(addr, str, 16);
238 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
240 * @param addr ip address in network order to convert
241 * @param buf target buffer where the string is stored
242 * @param buflen length of buf
243 * @return either pointer to buf which now holds the ASCII
244 * representation of addr or NULL if buf was too small
246 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
257 s_addr = ip4_addr_get_u32(addr);
260 ap = (u8_t *)&s_addr;
261 for(n = 0; n < 4; n++) {
264 rem = *ap % (u8_t)10;
266 inv[i++] = '0' + rem;
269 if (len++ >= buflen) {
274 if (len++ >= buflen) {