Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
segundo 0:ac1725ba162c 1 /**
segundo 0:ac1725ba162c 2 * @file
segundo 0:ac1725ba162c 3 * This is the IPv4 address tools implementation.
segundo 0:ac1725ba162c 4 *
segundo 0:ac1725ba162c 5 */
segundo 0:ac1725ba162c 6
segundo 0:ac1725ba162c 7 /*
segundo 0:ac1725ba162c 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
segundo 0:ac1725ba162c 9 * All rights reserved.
segundo 0:ac1725ba162c 10 *
segundo 0:ac1725ba162c 11 * Redistribution and use in source and binary forms, with or without modification,
segundo 0:ac1725ba162c 12 * are permitted provided that the following conditions are met:
segundo 0:ac1725ba162c 13 *
segundo 0:ac1725ba162c 14 * 1. Redistributions of source code must retain the above copyright notice,
segundo 0:ac1725ba162c 15 * this list of conditions and the following disclaimer.
segundo 0:ac1725ba162c 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
segundo 0:ac1725ba162c 17 * this list of conditions and the following disclaimer in the documentation
segundo 0:ac1725ba162c 18 * and/or other materials provided with the distribution.
segundo 0:ac1725ba162c 19 * 3. The name of the author may not be used to endorse or promote products
segundo 0:ac1725ba162c 20 * derived from this software without specific prior written permission.
segundo 0:ac1725ba162c 21 *
segundo 0:ac1725ba162c 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
segundo 0:ac1725ba162c 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
segundo 0:ac1725ba162c 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
segundo 0:ac1725ba162c 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
segundo 0:ac1725ba162c 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
segundo 0:ac1725ba162c 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
segundo 0:ac1725ba162c 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
segundo 0:ac1725ba162c 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
segundo 0:ac1725ba162c 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
segundo 0:ac1725ba162c 31 * OF SUCH DAMAGE.
segundo 0:ac1725ba162c 32 *
segundo 0:ac1725ba162c 33 * This file is part of the lwIP TCP/IP stack.
segundo 0:ac1725ba162c 34 *
segundo 0:ac1725ba162c 35 * Author: Adam Dunkels <adam@sics.se>
segundo 0:ac1725ba162c 36 *
segundo 0:ac1725ba162c 37 */
segundo 0:ac1725ba162c 38
segundo 0:ac1725ba162c 39 #include "lwip/opt.h"
segundo 0:ac1725ba162c 40 #include "lwip/ip_addr.h"
segundo 0:ac1725ba162c 41 #include "lwip/netif.h"
segundo 0:ac1725ba162c 42
segundo 0:ac1725ba162c 43 /* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
segundo 0:ac1725ba162c 44 const ip_addr_t ip_addr_any = { IPADDR_ANY };
segundo 0:ac1725ba162c 45 const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST };
segundo 0:ac1725ba162c 46
segundo 0:ac1725ba162c 47 /**
segundo 0:ac1725ba162c 48 * Determine if an address is a broadcast address on a network interface
segundo 0:ac1725ba162c 49 *
segundo 0:ac1725ba162c 50 * @param addr address to be checked
segundo 0:ac1725ba162c 51 * @param netif the network interface against which the address is checked
segundo 0:ac1725ba162c 52 * @return returns non-zero if the address is a broadcast address
segundo 0:ac1725ba162c 53 */
segundo 0:ac1725ba162c 54 u8_t
segundo 0:ac1725ba162c 55 ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
segundo 0:ac1725ba162c 56 {
segundo 0:ac1725ba162c 57 ip_addr_t ipaddr;
segundo 0:ac1725ba162c 58 ip4_addr_set_u32(&ipaddr, addr);
segundo 0:ac1725ba162c 59
segundo 0:ac1725ba162c 60 /* all ones (broadcast) or all zeroes (old skool broadcast) */
segundo 0:ac1725ba162c 61 if ((~addr == IPADDR_ANY) ||
segundo 0:ac1725ba162c 62 (addr == IPADDR_ANY)) {
segundo 0:ac1725ba162c 63 return 1;
segundo 0:ac1725ba162c 64 /* no broadcast support on this network interface? */
segundo 0:ac1725ba162c 65 } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
segundo 0:ac1725ba162c 66 /* the given address cannot be a broadcast address
segundo 0:ac1725ba162c 67 * nor can we check against any broadcast addresses */
segundo 0:ac1725ba162c 68 return 0;
segundo 0:ac1725ba162c 69 /* address matches network interface address exactly? => no broadcast */
segundo 0:ac1725ba162c 70 } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) {
segundo 0:ac1725ba162c 71 return 0;
segundo 0:ac1725ba162c 72 /* on the same (sub) network... */
segundo 0:ac1725ba162c 73 } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask))
segundo 0:ac1725ba162c 74 /* ...and host identifier bits are all ones? =>... */
segundo 0:ac1725ba162c 75 && ((addr & ~ip4_addr_get_u32(&netif->netmask)) ==
segundo 0:ac1725ba162c 76 (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) {
segundo 0:ac1725ba162c 77 /* => network broadcast address */
segundo 0:ac1725ba162c 78 return 1;
segundo 0:ac1725ba162c 79 } else {
segundo 0:ac1725ba162c 80 return 0;
segundo 0:ac1725ba162c 81 }
segundo 0:ac1725ba162c 82 }
segundo 0:ac1725ba162c 83
segundo 0:ac1725ba162c 84 /** Checks if a netmask is valid (starting with ones, then only zeros)
segundo 0:ac1725ba162c 85 *
segundo 0:ac1725ba162c 86 * @param netmask the IPv4 netmask to check (in network byte order!)
segundo 0:ac1725ba162c 87 * @return 1 if the netmask is valid, 0 if it is not
segundo 0:ac1725ba162c 88 */
segundo 0:ac1725ba162c 89 u8_t
segundo 0:ac1725ba162c 90 ip4_addr_netmask_valid(u32_t netmask)
segundo 0:ac1725ba162c 91 {
segundo 0:ac1725ba162c 92 u32_t mask;
segundo 0:ac1725ba162c 93 u32_t nm_hostorder = lwip_htonl(netmask);
segundo 0:ac1725ba162c 94
segundo 0:ac1725ba162c 95 /* first, check for the first zero */
segundo 0:ac1725ba162c 96 for (mask = 1U << 31 ; mask != 0; mask >>= 1) {
segundo 0:ac1725ba162c 97 if ((nm_hostorder & mask) == 0) {
segundo 0:ac1725ba162c 98 break;
segundo 0:ac1725ba162c 99 }
segundo 0:ac1725ba162c 100 }
segundo 0:ac1725ba162c 101 /* then check that there is no one */
segundo 0:ac1725ba162c 102 for (; mask != 0; mask >>= 1) {
segundo 0:ac1725ba162c 103 if ((nm_hostorder & mask) != 0) {
segundo 0:ac1725ba162c 104 /* there is a one after the first zero -> invalid */
segundo 0:ac1725ba162c 105 return 0;
segundo 0:ac1725ba162c 106 }
segundo 0:ac1725ba162c 107 }
segundo 0:ac1725ba162c 108 /* no one after the first zero -> valid */
segundo 0:ac1725ba162c 109 return 1;
segundo 0:ac1725ba162c 110 }
segundo 0:ac1725ba162c 111
segundo 0:ac1725ba162c 112 /* Here for now until needed in other places in lwIP */
segundo 0:ac1725ba162c 113 #ifndef isprint
segundo 0:ac1725ba162c 114 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
segundo 0:ac1725ba162c 115 #define isprint(c) in_range(c, 0x20, 0x7f)
segundo 0:ac1725ba162c 116 #define isdigit(c) in_range(c, '0', '9')
segundo 0:ac1725ba162c 117 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
segundo 0:ac1725ba162c 118 #define islower(c) in_range(c, 'a', 'z')
segundo 0:ac1725ba162c 119 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
segundo 0:ac1725ba162c 120 #endif
segundo 0:ac1725ba162c 121
segundo 0:ac1725ba162c 122 /**
segundo 0:ac1725ba162c 123 * Ascii internet address interpretation routine.
segundo 0:ac1725ba162c 124 * The value returned is in network order.
segundo 0:ac1725ba162c 125 *
segundo 0:ac1725ba162c 126 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
segundo 0:ac1725ba162c 127 * @return ip address in network order
segundo 0:ac1725ba162c 128 */
segundo 0:ac1725ba162c 129 u32_t
segundo 0:ac1725ba162c 130 ipaddr_addr(const char *cp)
segundo 0:ac1725ba162c 131 {
segundo 0:ac1725ba162c 132 ip_addr_t val;
segundo 0:ac1725ba162c 133
segundo 0:ac1725ba162c 134 if (ipaddr_aton(cp, &val)) {
segundo 0:ac1725ba162c 135 return ip4_addr_get_u32(&val);
segundo 0:ac1725ba162c 136 }
segundo 0:ac1725ba162c 137 return (IPADDR_NONE);
segundo 0:ac1725ba162c 138 }
segundo 0:ac1725ba162c 139
segundo 0:ac1725ba162c 140 /**
segundo 0:ac1725ba162c 141 * Check whether "cp" is a valid ascii representation
segundo 0:ac1725ba162c 142 * of an Internet address and convert to a binary address.
segundo 0:ac1725ba162c 143 * Returns 1 if the address is valid, 0 if not.
segundo 0:ac1725ba162c 144 * This replaces inet_addr, the return value from which
segundo 0:ac1725ba162c 145 * cannot distinguish between failure and a local broadcast address.
segundo 0:ac1725ba162c 146 *
segundo 0:ac1725ba162c 147 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
segundo 0:ac1725ba162c 148 * @param addr pointer to which to save the ip address in network order
segundo 0:ac1725ba162c 149 * @return 1 if cp could be converted to addr, 0 on failure
segundo 0:ac1725ba162c 150 */
segundo 0:ac1725ba162c 151 int
segundo 0:ac1725ba162c 152 ipaddr_aton(const char *cp, ip_addr_t *addr)
segundo 0:ac1725ba162c 153 {
segundo 0:ac1725ba162c 154 u32_t val;
segundo 0:ac1725ba162c 155 u8_t base;
segundo 0:ac1725ba162c 156 char c;
segundo 0:ac1725ba162c 157 u32_t parts[4];
segundo 0:ac1725ba162c 158 u32_t *pp = parts;
segundo 0:ac1725ba162c 159
segundo 0:ac1725ba162c 160 c = *cp;
segundo 0:ac1725ba162c 161 for (;;) {
segundo 0:ac1725ba162c 162 /*
segundo 0:ac1725ba162c 163 * Collect number up to ``.''.
segundo 0:ac1725ba162c 164 * Values are specified as for C:
segundo 0:ac1725ba162c 165 * 0x=hex, 0=octal, 1-9=decimal.
segundo 0:ac1725ba162c 166 */
segundo 0:ac1725ba162c 167 if (!isdigit(c))
segundo 0:ac1725ba162c 168 return (0);
segundo 0:ac1725ba162c 169 val = 0;
segundo 0:ac1725ba162c 170 base = 10;
segundo 0:ac1725ba162c 171 if (c == '0') {
segundo 0:ac1725ba162c 172 c = *++cp;
segundo 0:ac1725ba162c 173 if (c == 'x' || c == 'X') {
segundo 0:ac1725ba162c 174 base = 16;
segundo 0:ac1725ba162c 175 c = *++cp;
segundo 0:ac1725ba162c 176 } else
segundo 0:ac1725ba162c 177 base = 8;
segundo 0:ac1725ba162c 178 }
segundo 0:ac1725ba162c 179 for (;;) {
segundo 0:ac1725ba162c 180 if (isdigit(c)) {
segundo 0:ac1725ba162c 181 val = (val * base) + (int)(c - '0');
segundo 0:ac1725ba162c 182 c = *++cp;
segundo 0:ac1725ba162c 183 } else if (base == 16 && isxdigit(c)) {
segundo 0:ac1725ba162c 184 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
segundo 0:ac1725ba162c 185 c = *++cp;
segundo 0:ac1725ba162c 186 } else
segundo 0:ac1725ba162c 187 break;
segundo 0:ac1725ba162c 188 }
segundo 0:ac1725ba162c 189 if (c == '.') {
segundo 0:ac1725ba162c 190 /*
segundo 0:ac1725ba162c 191 * Internet format:
segundo 0:ac1725ba162c 192 * a.b.c.d
segundo 0:ac1725ba162c 193 * a.b.c (with c treated as 16 bits)
segundo 0:ac1725ba162c 194 * a.b (with b treated as 24 bits)
segundo 0:ac1725ba162c 195 */
segundo 0:ac1725ba162c 196 if (pp >= parts + 3) {
segundo 0:ac1725ba162c 197 return (0);
segundo 0:ac1725ba162c 198 }
segundo 0:ac1725ba162c 199 *pp++ = val;
segundo 0:ac1725ba162c 200 c = *++cp;
segundo 0:ac1725ba162c 201 } else
segundo 0:ac1725ba162c 202 break;
segundo 0:ac1725ba162c 203 }
segundo 0:ac1725ba162c 204 /*
segundo 0:ac1725ba162c 205 * Check for trailing characters.
segundo 0:ac1725ba162c 206 */
segundo 0:ac1725ba162c 207 if (c != '\0' && !isspace(c)) {
segundo 0:ac1725ba162c 208 return (0);
segundo 0:ac1725ba162c 209 }
segundo 0:ac1725ba162c 210 /*
segundo 0:ac1725ba162c 211 * Concoct the address according to
segundo 0:ac1725ba162c 212 * the number of parts specified.
segundo 0:ac1725ba162c 213 */
segundo 0:ac1725ba162c 214 switch (pp - parts + 1) {
segundo 0:ac1725ba162c 215
segundo 0:ac1725ba162c 216 case 0:
segundo 0:ac1725ba162c 217 return (0); /* initial nondigit */
segundo 0:ac1725ba162c 218
segundo 0:ac1725ba162c 219 case 1: /* a -- 32 bits */
segundo 0:ac1725ba162c 220 break;
segundo 0:ac1725ba162c 221
segundo 0:ac1725ba162c 222 case 2: /* a.b -- 8.24 bits */
segundo 0:ac1725ba162c 223 if (val > 0xffffffUL) {
segundo 0:ac1725ba162c 224 return (0);
segundo 0:ac1725ba162c 225 }
segundo 0:ac1725ba162c 226 val |= parts[0] << 24;
segundo 0:ac1725ba162c 227 break;
segundo 0:ac1725ba162c 228
segundo 0:ac1725ba162c 229 case 3: /* a.b.c -- 8.8.16 bits */
segundo 0:ac1725ba162c 230 if (val > 0xffff) {
segundo 0:ac1725ba162c 231 return (0);
segundo 0:ac1725ba162c 232 }
segundo 0:ac1725ba162c 233 val |= (parts[0] << 24) | (parts[1] << 16);
segundo 0:ac1725ba162c 234 break;
segundo 0:ac1725ba162c 235
segundo 0:ac1725ba162c 236 case 4: /* a.b.c.d -- 8.8.8.8 bits */
segundo 0:ac1725ba162c 237 if (val > 0xff) {
segundo 0:ac1725ba162c 238 return (0);
segundo 0:ac1725ba162c 239 }
segundo 0:ac1725ba162c 240 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
segundo 0:ac1725ba162c 241 break;
segundo 0:ac1725ba162c 242 default:
segundo 0:ac1725ba162c 243 LWIP_ASSERT("unhandled", 0);
segundo 0:ac1725ba162c 244 break;
segundo 0:ac1725ba162c 245 }
segundo 0:ac1725ba162c 246 if (addr) {
segundo 0:ac1725ba162c 247 ip4_addr_set_u32(addr, htonl(val));
segundo 0:ac1725ba162c 248 }
segundo 0:ac1725ba162c 249 return (1);
segundo 0:ac1725ba162c 250 }
segundo 0:ac1725ba162c 251
segundo 0:ac1725ba162c 252 /**
segundo 0:ac1725ba162c 253 * Convert numeric IP address into decimal dotted ASCII representation.
segundo 0:ac1725ba162c 254 * returns ptr to static buffer; not reentrant!
segundo 0:ac1725ba162c 255 *
segundo 0:ac1725ba162c 256 * @param addr ip address in network order to convert
segundo 0:ac1725ba162c 257 * @return pointer to a global static (!) buffer that holds the ASCII
segundo 0:ac1725ba162c 258 * represenation of addr
segundo 0:ac1725ba162c 259 */
segundo 0:ac1725ba162c 260 char *
segundo 0:ac1725ba162c 261 ipaddr_ntoa(const ip_addr_t *addr)
segundo 0:ac1725ba162c 262 {
segundo 0:ac1725ba162c 263 static char str[16];
segundo 0:ac1725ba162c 264 return ipaddr_ntoa_r(addr, str, 16);
segundo 0:ac1725ba162c 265 }
segundo 0:ac1725ba162c 266
segundo 0:ac1725ba162c 267 /**
segundo 0:ac1725ba162c 268 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
segundo 0:ac1725ba162c 269 *
segundo 0:ac1725ba162c 270 * @param addr ip address in network order to convert
segundo 0:ac1725ba162c 271 * @param buf target buffer where the string is stored
segundo 0:ac1725ba162c 272 * @param buflen length of buf
segundo 0:ac1725ba162c 273 * @return either pointer to buf which now holds the ASCII
segundo 0:ac1725ba162c 274 * representation of addr or NULL if buf was too small
segundo 0:ac1725ba162c 275 */
segundo 0:ac1725ba162c 276 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
segundo 0:ac1725ba162c 277 {
segundo 0:ac1725ba162c 278 u32_t s_addr;
segundo 0:ac1725ba162c 279 char inv[3];
segundo 0:ac1725ba162c 280 char *rp;
segundo 0:ac1725ba162c 281 u8_t *ap;
segundo 0:ac1725ba162c 282 u8_t rem;
segundo 0:ac1725ba162c 283 u8_t n;
segundo 0:ac1725ba162c 284 u8_t i;
segundo 0:ac1725ba162c 285 int len = 0;
segundo 0:ac1725ba162c 286
segundo 0:ac1725ba162c 287 s_addr = ip4_addr_get_u32(addr);
segundo 0:ac1725ba162c 288
segundo 0:ac1725ba162c 289 rp = buf;
segundo 0:ac1725ba162c 290 ap = (u8_t *)&s_addr;
segundo 0:ac1725ba162c 291 for(n = 0; n < 4; n++) {
segundo 0:ac1725ba162c 292 i = 0;
segundo 0:ac1725ba162c 293 do {
segundo 0:ac1725ba162c 294 rem = *ap % (u8_t)10;
segundo 0:ac1725ba162c 295 *ap /= (u8_t)10;
segundo 0:ac1725ba162c 296 inv[i++] = '0' + rem;
segundo 0:ac1725ba162c 297 } while(*ap);
segundo 0:ac1725ba162c 298 while(i--) {
segundo 0:ac1725ba162c 299 if (len++ >= buflen) {
segundo 0:ac1725ba162c 300 return NULL;
segundo 0:ac1725ba162c 301 }
segundo 0:ac1725ba162c 302 *rp++ = inv[i];
segundo 0:ac1725ba162c 303 }
segundo 0:ac1725ba162c 304 if (len++ >= buflen) {
segundo 0:ac1725ba162c 305 return NULL;
segundo 0:ac1725ba162c 306 }
segundo 0:ac1725ba162c 307 *rp++ = '.';
segundo 0:ac1725ba162c 308 ap++;
segundo 0:ac1725ba162c 309 }
segundo 0:ac1725ba162c 310 *--rp = 0;
segundo 0:ac1725ba162c 311 return buf;
segundo 0:ac1725ba162c 312 }