NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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