Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 25 | * OF SUCH DAMAGE. |
| 26 | * |
| 27 | * This file is part of the lwIP TCP/IP stack. |
| 28 | * |
| 29 | * Author: Adam Dunkels <adam@sics.se> |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | /* inet.c |
| 35 | * |
| 36 | * Functions common to all TCP/IP modules, such as the Internet checksum and the |
| 37 | * byte order functions. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | #include "lwip/opt.h" |
| 43 | |
| 44 | #include "lwip/arch.h" |
| 45 | |
| 46 | #include "lwip/def.h" |
| 47 | #include "lwip/inet.h" |
| 48 | |
| 49 | #include "lwip/sys.h" |
| 50 | |
| 51 | /* This is a reference implementation of the checksum algorithm, with the |
| 52 | * aim of being simple, correct and fully portable. Checksumming is the |
| 53 | * first thing you would want to optimize for your platform. You will |
| 54 | * need to port it to your architecture and in your sys_arch.h: |
| 55 | * |
| 56 | * #define LWIP_CHKSUM <your_checksum_routine> |
| 57 | */ |
| 58 | #ifndef LWIP_CHKSUM |
| 59 | #define LWIP_CHKSUM lwip_standard_chksum |
| 60 | |
| 61 | /** |
| 62 | * lwip checksum |
| 63 | * |
| 64 | * @param dataptr points to start of data to be summed at any boundary |
| 65 | * @param len length of data to be summed |
| 66 | * @return host order (!) lwip checksum (non-inverted Internet sum) |
| 67 | * |
| 68 | * @note accumulator size limits summable lenght to 64k |
| 69 | * @note host endianess is irrelevant (p3 RFC1071) |
| 70 | */ |
| 71 | static u16_t |
| 72 | lwip_standard_chksum(void *dataptr, u16_t len) |
| 73 | { |
| 74 | u32_t acc; |
| 75 | u16_t src; |
| 76 | u8_t *octetptr; |
| 77 | |
| 78 | acc = 0; |
| 79 | /* dataptr may be at odd or even addresses */ |
| 80 | octetptr = (u8_t*)dataptr; |
| 81 | while (len > 1) |
| 82 | { |
| 83 | /* declare first octet as most significant |
| 84 | thus assume network order, ignoring host order */ |
| 85 | src = (*octetptr) << 8; |
| 86 | octetptr++; |
| 87 | /* declare second octet as least significant */ |
| 88 | src |= (*octetptr); |
| 89 | octetptr++; |
| 90 | acc += src; |
| 91 | len -= 2; |
| 92 | } |
| 93 | if (len > 0) |
| 94 | { |
| 95 | /* accumulate remaining octet */ |
| 96 | src = (*octetptr) << 8; |
| 97 | acc += src; |
| 98 | } |
| 99 | /* add deferred carry bits */ |
| 100 | acc = (acc >> 16) + (acc & 0x0000ffffUL); |
| 101 | if ((acc & 0xffff0000) != 0) { |
| 102 | acc = (acc >> 16) + (acc & 0x0000ffffUL); |
| 103 | } |
| 104 | /* This maybe a little confusing: reorder sum using htons() |
| 105 | instead of ntohs() since it has a little less call overhead. |
| 106 | The caller must invert bits for Internet sum ! */ |
| 107 | return htons((u16_t)acc); |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | #if 0 |
| 113 | /* |
| 114 | * Curt McDowell |
| 115 | * Broadcom Corp. |
| 116 | * csm@broadcom.com |
| 117 | * |
| 118 | * IP checksum two bytes at a time with support for |
| 119 | * unaligned buffer. |
| 120 | * Works for len up to and including 0x20000. |
| 121 | * by Curt McDowell, Broadcom Corp. 12/08/2005 |
| 122 | */ |
| 123 | |
| 124 | static u16_t |
| 125 | lwip_standard_chksum2(void *dataptr, int len) |
| 126 | { |
| 127 | u8_t *pb = dataptr; |
| 128 | u16_t *ps, t = 0; |
| 129 | u32_t sum = 0; |
| 130 | int odd = ((u32_t)pb & 1); |
| 131 | |
| 132 | /* Get aligned to u16_t */ |
| 133 | if (odd && len > 0) { |
| 134 | ((u8_t *)&t)[1] = *pb++; |
| 135 | len--; |
| 136 | } |
| 137 | |
| 138 | /* Add the bulk of the data */ |
| 139 | ps = (u16_t *)pb; |
| 140 | while (len > 1) { |
| 141 | sum += *ps++; |
| 142 | len -= 2; |
| 143 | } |
| 144 | |
| 145 | /* Consume left-over byte, if any */ |
| 146 | if (len > 0) |
| 147 | ((u8_t *)&t)[0] = *(u8_t *)ps;; |
| 148 | |
| 149 | /* Add end bytes */ |
| 150 | sum += t; |
| 151 | |
| 152 | /* Fold 32-bit sum to 16 bits */ |
| 153 | while (sum >> 16) |
| 154 | sum = (sum & 0xffff) + (sum >> 16); |
| 155 | |
| 156 | /* Swap if alignment was odd */ |
| 157 | if (odd) |
| 158 | sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8); |
| 159 | |
| 160 | return sum; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * An optimized checksum routine. Basically, it uses loop-unrolling on |
| 165 | * the checksum loop, treating the head and tail bytes specially, whereas |
| 166 | * the inner loop acts on 8 bytes at a time. |
| 167 | * |
| 168 | * @arg start of buffer to be checksummed. May be an odd byte address. |
| 169 | * @len number of bytes in the buffer to be checksummed. |
| 170 | * |
| 171 | * @todo First argument type conflicts with generic checksum routine. |
| 172 | * |
| 173 | * by Curt McDowell, Broadcom Corp. December 8th, 2005 |
| 174 | */ |
| 175 | |
| 176 | static u16_t |
| 177 | lwip_standard_chksum4(u8_t *pb, int len) |
| 178 | { |
| 179 | u16_t *ps, t = 0; |
| 180 | u32_t *pl; |
| 181 | u32_t sum = 0, tmp; |
| 182 | /* starts at odd byte address? */ |
| 183 | int odd = ((u32_t)pb & 1); |
| 184 | |
| 185 | if (odd && len > 0) { |
| 186 | ((u8_t *)&t)[1] = *pb++; |
| 187 | len--; |
| 188 | } |
| 189 | |
| 190 | ps = (u16_t *)pb; |
| 191 | |
| 192 | if (((u32_t)ps & 3) && len > 1) { |
| 193 | sum += *ps++; |
| 194 | len -= 2; |
| 195 | } |
| 196 | |
| 197 | pl = (u32_t *)ps; |
| 198 | |
| 199 | while (len > 7) { |
| 200 | tmp = sum + *pl++; /* ping */ |
| 201 | if (tmp < sum) |
| 202 | tmp++; /* add back carry */ |
| 203 | |
| 204 | sum = tmp + *pl++; /* pong */ |
| 205 | if (sum < tmp) |
| 206 | sum++; /* add back carry */ |
| 207 | |
| 208 | len -= 8; |
| 209 | } |
| 210 | |
| 211 | /* make room in upper bits */ |
| 212 | sum = (sum >> 16) + (sum & 0xffff); |
| 213 | |
| 214 | ps = (u16_t *)pl; |
| 215 | |
| 216 | /* 16-bit aligned word remaining? */ |
| 217 | while (len > 1) { |
| 218 | sum += *ps++; |
| 219 | len -= 2; |
| 220 | } |
| 221 | |
| 222 | /* dangling tail byte remaining? */ |
| 223 | if (len > 0) /* include odd byte */ |
| 224 | ((u8_t *)&t)[0] = *(u8_t *)ps; |
| 225 | |
| 226 | sum += t; /* add end bytes */ |
| 227 | |
| 228 | while (sum >> 16) /* combine halves */ |
| 229 | sum = (sum >> 16) + (sum & 0xffff); |
| 230 | |
| 231 | if (odd) |
| 232 | sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8); |
| 233 | |
| 234 | return sum; |
| 235 | } |
| 236 | #endif |
| 237 | |
| 238 | /* inet_chksum_pseudo: |
| 239 | * |
| 240 | * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain. |
| 241 | */ |
| 242 | |
| 243 | u16_t |
| 244 | inet_chksum_pseudo(struct pbuf *p, |
| 245 | struct ip_addr *src, struct ip_addr *dest, |
| 246 | u8_t proto, u16_t proto_len) |
| 247 | { |
| 248 | u32_t acc; |
| 249 | struct pbuf *q; |
| 250 | u8_t swapped; |
| 251 | |
| 252 | acc = 0; |
| 253 | swapped = 0; |
| 254 | /* iterate through all pbuf in chain */ |
| 255 | for(q = p; q != NULL; q = q->next) { |
| 256 | LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n", |
| 257 | (void *)q, (void *)q->next)); |
| 258 | acc += LWIP_CHKSUM(q->payload, q->len); |
| 259 | /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/ |
| 260 | while (acc >> 16) { |
| 261 | acc = (acc & 0xffffUL) + (acc >> 16); |
| 262 | } |
| 263 | if (q->len % 2 != 0) { |
| 264 | swapped = 1 - swapped; |
| 265 | acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8); |
| 266 | } |
| 267 | /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/ |
| 268 | } |
| 269 | |
| 270 | if (swapped) { |
| 271 | acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8); |
| 272 | } |
| 273 | acc += (src->addr & 0xffffUL); |
| 274 | acc += ((src->addr >> 16) & 0xffffUL); |
| 275 | acc += (dest->addr & 0xffffUL); |
| 276 | acc += ((dest->addr >> 16) & 0xffffUL); |
| 277 | acc += (u32_t)htons((u16_t)proto); |
| 278 | acc += (u32_t)htons(proto_len); |
| 279 | |
| 280 | while (acc >> 16) { |
| 281 | acc = (acc & 0xffffUL) + (acc >> 16); |
| 282 | } |
| 283 | LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc)); |
| 284 | return (u16_t)~(acc & 0xffffUL); |
| 285 | } |
| 286 | |
| 287 | /* inet_chksum: |
| 288 | * |
| 289 | * Calculates the Internet checksum over a portion of memory. Used primarely for IP |
| 290 | * and ICMP. |
| 291 | */ |
| 292 | |
| 293 | u16_t |
| 294 | inet_chksum(void *dataptr, u16_t len) |
| 295 | { |
| 296 | u32_t acc; |
| 297 | |
| 298 | acc = LWIP_CHKSUM(dataptr, len); |
| 299 | while (acc >> 16) { |
| 300 | acc = (acc & 0xffff) + (acc >> 16); |
| 301 | } |
| 302 | return (u16_t)~(acc & 0xffff); |
| 303 | } |
| 304 | |
| 305 | u16_t |
| 306 | inet_chksum_pbuf(struct pbuf *p) |
| 307 | { |
| 308 | u32_t acc; |
| 309 | struct pbuf *q; |
| 310 | u8_t swapped; |
| 311 | |
| 312 | acc = 0; |
| 313 | swapped = 0; |
| 314 | for(q = p; q != NULL; q = q->next) { |
| 315 | acc += LWIP_CHKSUM(q->payload, q->len); |
| 316 | while (acc >> 16) { |
| 317 | acc = (acc & 0xffffUL) + (acc >> 16); |
| 318 | } |
| 319 | if (q->len % 2 != 0) { |
| 320 | swapped = 1 - swapped; |
| 321 | acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (swapped) { |
| 326 | acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8); |
| 327 | } |
| 328 | return (u16_t)~(acc & 0xffffUL); |
| 329 | } |
| 330 | |
| 331 | /* Here for now until needed in other places in lwIP */ |
| 332 | #ifndef isascii |
| 333 | #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) |
| 334 | #define isascii(c) in_range(c, 0x20, 0x7f) |
| 335 | #define isdigit(c) in_range(c, '0', '9') |
| 336 | #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F')) |
| 337 | #define islower(c) in_range(c, 'a', 'z') |
| 338 | #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') |
| 339 | #endif |
| 340 | |
| 341 | |
| 342 | /* |
| 343 | * Ascii internet address interpretation routine. |
| 344 | * The value returned is in network order. |
| 345 | */ |
| 346 | |
| 347 | /* */ |
| 348 | /* inet_addr */ |
| 349 | u32_t inet_addr(const char *cp) |
| 350 | { |
| 351 | struct in_addr val; |
| 352 | |
| 353 | if (inet_aton(cp, &val)) { |
| 354 | return (val.s_addr); |
| 355 | } |
| 356 | return (INADDR_NONE); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Check whether "cp" is a valid ascii representation |
| 361 | * of an Internet address and convert to a binary address. |
| 362 | * Returns 1 if the address is valid, 0 if not. |
| 363 | * This replaces inet_addr, the return value from which |
| 364 | * cannot distinguish between failure and a local broadcast address. |
| 365 | */ |
| 366 | /* */ |
| 367 | /* inet_aton */ |
| 368 | s8_t |
| 369 | inet_aton(const char *cp, struct in_addr *addr) |
| 370 | { |
| 371 | u32_t val; |
| 372 | s32_t base, n; |
| 373 | char c; |
| 374 | u32_t parts[4]; |
| 375 | u32_t* pp = parts; |
| 376 | |
| 377 | c = *cp; |
| 378 | for (;;) { |
| 379 | /* |
| 380 | * Collect number up to ``.''. |
| 381 | * Values are specified as for C: |
| 382 | * 0x=hex, 0=octal, isdigit=decimal. |
| 383 | */ |
| 384 | if (!isdigit(c)) |
| 385 | return (0); |
| 386 | val = 0; base = 10; |
| 387 | if (c == '0') { |
| 388 | c = *++cp; |
| 389 | if (c == 'x' || c == 'X') |
| 390 | base = 16, c = *++cp; |
| 391 | else |
| 392 | base = 8; |
| 393 | } |
| 394 | for (;;) { |
| 395 | if (isdigit(c)) { |
| 396 | val = (val * base) + (s16_t)(c - '0'); |
| 397 | c = *++cp; |
| 398 | } else if (base == 16 && isxdigit(c)) { |
| 399 | val = (val << 4) | |
| 400 | (s16_t)(c + 10 - (islower(c) ? 'a' : 'A')); |
| 401 | c = *++cp; |
| 402 | } else |
| 403 | break; |
| 404 | } |
| 405 | if (c == '.') { |
| 406 | /* |
| 407 | * Internet format: |
| 408 | * a.b.c.d |
| 409 | * a.b.c (with c treated as 16 bits) |
| 410 | * a.b (with b treated as 24 bits) |
| 411 | */ |
| 412 | if (pp >= parts + 3) |
| 413 | return (0); |
| 414 | *pp++ = val; |
| 415 | c = *++cp; |
| 416 | } else |
| 417 | break; |
| 418 | } |
| 419 | /* |
| 420 | * Check for trailing characters. |
| 421 | */ |
| 422 | if (c != '\0' && (!isascii(c) || !isspace(c))) |
| 423 | return (0); |
| 424 | /* |
| 425 | * Concoct the address according to |
| 426 | * the number of parts specified. |
| 427 | */ |
| 428 | n = pp - parts + 1; |
| 429 | switch (n) { |
| 430 | |
| 431 | case 0: |
| 432 | return (0); /* initial nondigit */ |
| 433 | |
| 434 | case 1: /* a -- 32 bits */ |
| 435 | break; |
| 436 | |
| 437 | case 2: /* a.b -- 8.24 bits */ |
| 438 | if (val > 0xffffff) |
| 439 | return (0); |
| 440 | val |= parts[0] << 24; |
| 441 | break; |
| 442 | |
| 443 | case 3: /* a.b.c -- 8.8.16 bits */ |
| 444 | if (val > 0xffff) |
| 445 | return (0); |
| 446 | val |= (parts[0] << 24) | (parts[1] << 16); |
| 447 | break; |
| 448 | |
| 449 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ |
| 450 | if (val > 0xff) |
| 451 | return (0); |
| 452 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); |
| 453 | break; |
| 454 | } |
| 455 | if (addr) |
| 456 | addr->s_addr = htonl(val); |
| 457 | return (1); |
| 458 | } |
| 459 | |
| 460 | /* Convert numeric IP address into decimal dotted ASCII representation. |
| 461 | * returns ptr to static buffer; not reentrant! |
| 462 | */ |
| 463 | char *inet_ntoa(struct in_addr addr) |
| 464 | { |
| 465 | static char str[16]; |
| 466 | u32_t s_addr = addr.s_addr; |
| 467 | char inv[3]; |
| 468 | char *rp; |
| 469 | u8_t *ap; |
| 470 | u8_t rem; |
| 471 | u8_t n; |
| 472 | u8_t i; |
| 473 | |
| 474 | rp = str; |
| 475 | ap = (u8_t *)&s_addr; |
| 476 | for(n = 0; n < 4; n++) { |
| 477 | i = 0; |
| 478 | do { |
| 479 | rem = *ap % (u8_t)10; |
| 480 | *ap /= (u8_t)10; |
| 481 | inv[i++] = '0' + rem; |
| 482 | } while(*ap); |
| 483 | while(i--) |
| 484 | *rp++ = inv[i]; |
| 485 | *rp++ = '.'; |
| 486 | ap++; |
| 487 | } |
| 488 | *--rp = 0; |
| 489 | return str; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | #ifndef BYTE_ORDER |
| 494 | #error BYTE_ORDER is not defined |
| 495 | #endif |
| 496 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 497 | |
| 498 | u16_t |
| 499 | htons(u16_t n) |
| 500 | { |
| 501 | return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); |
| 502 | } |
| 503 | |
| 504 | u16_t |
| 505 | ntohs(u16_t n) |
| 506 | { |
| 507 | return htons(n); |
| 508 | } |
| 509 | |
| 510 | u32_t |
| 511 | htonl(u32_t n) |
| 512 | { |
| 513 | return ((n & 0xff) << 24) | |
| 514 | ((n & 0xff00) << 8) | |
| 515 | ((n & 0xff0000) >> 8) | |
| 516 | ((n & 0xff000000) >> 24); |
| 517 | } |
| 518 | |
| 519 | u32_t |
| 520 | ntohl(u32_t n) |
| 521 | { |
| 522 | return htonl(n); |
| 523 | } |
| 524 | |
| 525 | #endif /* BYTE_ORDER == LITTLE_ENDIAN */ |