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#include <arranet_impl.h> 43 44/* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */ 45const ip_addr_t ip_addr_any = { IPADDR_ANY }; 46const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST }; 47 48/** 49 * Determine if an address is a broadcast address on a network interface 50 * 51 * @param addr address to be checked 52 * @param netif the network interface against which the address is checked 53 * @return returns non-zero if the address is a broadcast address 54 */ 55u8_t 56ip4_addr_isbroadcast(u32_t addr, const struct netif *netif) 57{ 58 ip_addr_t ipaddr; 59 ip4_addr_set_u32(&ipaddr, addr); 60 61 /* all ones (broadcast) or all zeroes (old skool broadcast) */ 62 if ((~addr == IPADDR_ANY) || 63 (addr == IPADDR_ANY)) { 64 return 1; 65 /* no broadcast support on this network interface? */ 66 } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) { 67 /* the given address cannot be a broadcast address 68 * nor can we check against any broadcast addresses */ 69 return 0; 70 /* address matches network interface address exactly? => no broadcast */ 71 } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) { 72 return 0; 73 /* on the same (sub) network... */ 74 } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask)) 75 /* ...and host identifier bits are all ones? =>... */ 76 && ((addr & ~ip4_addr_get_u32(&netif->netmask)) == 77 (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) { 78 /* => network broadcast address */ 79 return 1; 80 } else { 81 return 0; 82 } 83} 84 85/** Checks if a netmask is valid (starting with ones, then only zeros) 86 * 87 * @param netmask the IPv4 netmask to check (in network byte order!) 88 * @return 1 if the netmask is valid, 0 if it is not 89 */ 90u8_t 91ip4_addr_netmask_valid(u32_t netmask) 92{ 93 u32_t mask; 94 u32_t nm_hostorder = lwip_htonl(netmask); 95 96 /* first, check for the first zero */ 97 for (mask = 1UL << 31 ; mask != 0; mask >>= 1) { 98 if ((nm_hostorder & mask) == 0) { 99 break; 100 } 101 } 102 /* then check that there is no one */ 103 for (; mask != 0; mask >>= 1) { 104 if ((nm_hostorder & mask) != 0) { 105 /* there is a one after the first zero -> invalid */ 106 return 0; 107 } 108 } 109 /* no one after the first zero -> valid */ 110 return 1; 111} 112 113/* Here for now until needed in other places in lwIP */ 114#ifndef isprint 115#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) 116#define isprint(c) in_range(c, 0x20, 0x7f) 117#define isdigit(c) in_range(c, '0', '9') 118#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F')) 119#define islower(c) in_range(c, 'a', 'z') 120#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') 121#endif 122 123/** 124 * Ascii internet address interpretation routine. 125 * The value returned is in network order. 126 * 127 * @param cp IP address in ascii represenation (e.g. "127.0.0.1") 128 * @return ip address in network order 129 */ 130u32_t 131ipaddr_addr(const char *cp) 132{ 133 ip_addr_t val; 134 135 if (ipaddr_aton(cp, &val)) { 136 return ip4_addr_get_u32(&val); 137 } 138 return (IPADDR_NONE); 139} 140 141/** 142 * Check whether "cp" is a valid ascii representation 143 * of an Internet address and convert to a binary address. 144 * Returns 1 if the address is valid, 0 if not. 145 * This replaces inet_addr, the return value from which 146 * cannot distinguish between failure and a local broadcast address. 147 * 148 * @param cp IP address in ascii represenation (e.g. "127.0.0.1") 149 * @param addr pointer to which to save the ip address in network order 150 * @return 1 if cp could be converted to addr, 0 on failure 151 */ 152int 153ipaddr_aton(const char *cp, ip_addr_t *addr) 154{ 155 u32_t val; 156 u8_t base; 157 char c; 158 u32_t parts[4]; 159 u32_t *pp = parts; 160 161 c = *cp; 162 for (;;) { 163 /* 164 * Collect number up to ``.''. 165 * Values are specified as for C: 166 * 0x=hex, 0=octal, 1-9=decimal. 167 */ 168 if (!isdigit(c)) 169 return (0); 170 val = 0; 171 base = 10; 172 if (c == '0') { 173 c = *++cp; 174 if (c == 'x' || c == 'X') { 175 base = 16; 176 c = *++cp; 177 } else 178 base = 8; 179 } 180 for (;;) { 181 if (isdigit(c)) { 182 val = (val * base) + (int)(c - '0'); 183 c = *++cp; 184 } else if (base == 16 && isxdigit(c)) { 185 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A')); 186 c = *++cp; 187 } else 188 break; 189 } 190 if (c == '.') { 191 /* 192 * Internet format: 193 * a.b.c.d 194 * a.b.c (with c treated as 16 bits) 195 * a.b (with b treated as 24 bits) 196 */ 197 if (pp >= parts + 3) { 198 return (0); 199 } 200 *pp++ = val; 201 c = *++cp; 202 } else 203 break; 204 } 205 /* 206 * Check for trailing characters. 207 */ 208 if (c != '\0' && !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 val |= parts[0] << 24; 228 break; 229 230 case 3: /* a.b.c -- 8.8.16 bits */ 231 if (val > 0xffff) { 232 return (0); 233 } 234 val |= (parts[0] << 24) | (parts[1] << 16); 235 break; 236 237 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 238 if (val > 0xff) { 239 return (0); 240 } 241 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 242 break; 243 default: 244 LWIP_ASSERT("unhandled", 0); 245 break; 246 } 247 if (addr) { 248 ip4_addr_set_u32(addr, htonl(val)); 249 } 250 return (1); 251} 252 253/** 254 * Convert numeric IP address into decimal dotted ASCII representation. 255 * returns ptr to static buffer; not reentrant! 256 * 257 * @param addr ip address in network order to convert 258 * @return pointer to a global static (!) buffer that holds the ASCII 259 * represenation of addr 260 */ 261char * 262ipaddr_ntoa(const ip_addr_t *addr) 263{ 264 static char str[16]; 265 return ipaddr_ntoa_r(addr, str, 16); 266} 267 268/** 269 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used. 270 * 271 * @param addr ip address in network order to convert 272 * @param buf target buffer where the string is stored 273 * @param buflen length of buf 274 * @return either pointer to buf which now holds the ASCII 275 * representation of addr or NULL if buf was too small 276 */ 277char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen) 278{ 279 u32_t s_addr; 280 char inv[3]; 281 char *rp; 282 u8_t *ap; 283 u8_t rem; 284 u8_t n; 285 u8_t i; 286 int len = 0; 287 288 s_addr = ip4_addr_get_u32(addr); 289 290 rp = buf; 291 ap = (u8_t *)&s_addr; 292 for(n = 0; n < 4; n++) { 293 i = 0; 294 do { 295 rem = *ap % (u8_t)10; 296 *ap /= (u8_t)10; 297 inv[i++] = '0' + rem; 298 } while(*ap); 299 while(i--) { 300 if (len++ >= buflen) { 301 return NULL; 302 } 303 *rp++ = inv[i]; 304 } 305 if (len++ >= buflen) { 306 return NULL; 307 } 308 *rp++ = '.'; 309 ap++; 310 } 311 *--rp = 0; 312 return buf; 313} 314