2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without modification,
\r
6 * are permitted provided that the following conditions are met:
\r
8 * 1. Redistributions of source code must retain the above copyright notice,
\r
9 * this list of conditions and the following disclaimer.
\r
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
11 * this list of conditions and the following disclaimer in the documentation
\r
12 * and/or other materials provided with the distribution.
\r
13 * 3. The name of the author may not be used to endorse or promote products
\r
14 * derived from this software without specific prior written permission.
\r
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
\r
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
27 * This file is part of the lwIP TCP/IP stack.
\r
29 * Author: Adam Dunkels <adam@sics.se>
\r
36 * Functions common to all TCP/IP modules, such as the Internet checksum and the
\r
37 * byte order functions.
\r
42 #include "lwip/opt.h"
\r
44 #include "lwip/arch.h"
\r
46 #include "lwip/def.h"
\r
47 #include "lwip/inet.h"
\r
49 #include "lwip/sys.h"
\r
51 /* These are some reference implementations of the checksum algorithm, with the
\r
52 * aim of being simple, correct and fully portable. Checksumming is the
\r
53 * first thing you would want to optimize for your platform. If you create
\r
54 * your own version, link it in and in your sys_arch.h put:
\r
56 * #define LWIP_CHKSUM <your_checksum_routine>
\r
59 #define LWIP_CHKSUM lwip_standard_chksum
\r
61 #if 1 /* Version A */
\r
65 * @param dataptr points to start of data to be summed at any boundary
\r
66 * @param len length of data to be summed
\r
67 * @return host order (!) lwip checksum (non-inverted Internet sum)
\r
69 * @note accumulator size limits summable length to 64k
\r
70 * @note host endianess is irrelevant (p3 RFC1071)
\r
73 lwip_standard_chksum(void *dataptr, u16_t len)
\r
80 /* dataptr may be at odd or even addresses */
\r
81 octetptr = (u8_t*)dataptr;
\r
84 /* declare first octet as most significant
\r
85 thus assume network order, ignoring host order */
\r
86 src = (*octetptr) << 8;
\r
88 /* declare second octet as least significant */
\r
96 /* accumulate remaining octet */
\r
97 src = (*octetptr) << 8;
\r
100 /* add deferred carry bits */
\r
101 acc = (acc >> 16) + (acc & 0x0000ffffUL);
\r
102 if ((acc & 0xffff0000) != 0) {
\r
103 acc = (acc >> 16) + (acc & 0x0000ffffUL);
\r
105 /* This maybe a little confusing: reorder sum using htons()
\r
106 instead of ntohs() since it has a little less call overhead.
\r
107 The caller must invert bits for Internet sum ! */
\r
108 return htons((u16_t)acc);
\r
112 #if 0 /* Version B */
\r
118 * IP checksum two bytes at a time with support for
\r
119 * unaligned buffer.
\r
120 * Works for len up to and including 0x20000.
\r
121 * by Curt McDowell, Broadcom Corp. 12/08/2005
\r
125 lwip_standard_chksum(void *dataptr, int len)
\r
127 u8_t *pb = dataptr;
\r
130 int odd = ((u32_t)pb & 1);
\r
132 /* Get aligned to u16_t */
\r
133 if (odd && len > 0) {
\r
134 ((u8_t *)&t)[1] = *pb++;
\r
138 /* Add the bulk of the data */
\r
145 /* Consume left-over byte, if any */
\r
147 ((u8_t *)&t)[0] = *(u8_t *)ps;;
\r
149 /* Add end bytes */
\r
152 /* Fold 32-bit sum to 16 bits */
\r
154 sum = (sum & 0xffff) + (sum >> 16);
\r
156 /* Swap if alignment was odd */
\r
158 sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8);
\r
164 #if 0 /* Version C */
\r
166 * An optimized checksum routine. Basically, it uses loop-unrolling on
\r
167 * the checksum loop, treating the head and tail bytes specially, whereas
\r
168 * the inner loop acts on 8 bytes at a time.
\r
170 * @arg start of buffer to be checksummed. May be an odd byte address.
\r
171 * @len number of bytes in the buffer to be checksummed.
\r
173 * by Curt McDowell, Broadcom Corp. December 8th, 2005
\r
177 lwip_standard_chksum(void *dataptr, int len)
\r
179 u8_t *pb = dataptr;
\r
182 u32_t sum = 0, tmp;
\r
183 /* starts at odd byte address? */
\r
184 int odd = ((u32_t)pb & 1);
\r
186 if (odd && len > 0) {
\r
187 ((u8_t *)&t)[1] = *pb++;
\r
193 if (((u32_t)ps & 3) && len > 1) {
\r
201 tmp = sum + *pl++; /* ping */
\r
203 tmp++; /* add back carry */
\r
205 sum = tmp + *pl++; /* pong */
\r
207 sum++; /* add back carry */
\r
212 /* make room in upper bits */
\r
213 sum = (sum >> 16) + (sum & 0xffff);
\r
217 /* 16-bit aligned word remaining? */
\r
223 /* dangling tail byte remaining? */
\r
224 if (len > 0) /* include odd byte */
\r
225 ((u8_t *)&t)[0] = *(u8_t *)ps;
\r
227 sum += t; /* add end bytes */
\r
229 while (sum >> 16) /* combine halves */
\r
230 sum = (sum >> 16) + (sum & 0xffff);
\r
233 sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8);
\r
239 #endif /* LWIP_CHKSUM */
\r
241 /* inet_chksum_pseudo:
\r
243 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
\r
247 inet_chksum_pseudo(struct pbuf *p,
\r
248 struct ip_addr *src, struct ip_addr *dest,
\r
249 u8_t proto, u16_t proto_len)
\r
257 /* iterate through all pbuf in chain */
\r
258 for(q = p; q != NULL; q = q->next) {
\r
259 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
\r
260 (void *)q, (void *)q->next));
\r
261 acc += LWIP_CHKSUM(q->payload, q->len);
\r
262 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
\r
263 while (acc >> 16) {
\r
264 acc = (acc & 0xffffUL) + (acc >> 16);
\r
266 if (q->len % 2 != 0) {
\r
267 swapped = 1 - swapped;
\r
268 acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
\r
270 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
\r
274 acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
\r
276 acc += (src->addr & 0xffffUL);
\r
277 acc += ((src->addr >> 16) & 0xffffUL);
\r
278 acc += (dest->addr & 0xffffUL);
\r
279 acc += ((dest->addr >> 16) & 0xffffUL);
\r
280 acc += (u32_t)htons((u16_t)proto);
\r
281 acc += (u32_t)htons(proto_len);
\r
283 while (acc >> 16) {
\r
284 acc = (acc & 0xffffUL) + (acc >> 16);
\r
286 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
\r
287 return (u16_t)~(acc & 0xffffUL);
\r
292 * Calculates the Internet checksum over a portion of memory. Used primarily for IP
\r
297 inet_chksum(void *dataptr, u16_t len)
\r
301 acc = LWIP_CHKSUM(dataptr, len);
\r
302 while (acc >> 16) {
\r
303 acc = (acc & 0xffff) + (acc >> 16);
\r
305 return (u16_t)~(acc & 0xffff);
\r
309 inet_chksum_pbuf(struct pbuf *p)
\r
317 for(q = p; q != NULL; q = q->next) {
\r
318 acc += LWIP_CHKSUM(q->payload, q->len);
\r
319 while (acc >> 16) {
\r
320 acc = (acc & 0xffffUL) + (acc >> 16);
\r
322 if (q->len % 2 != 0) {
\r
323 swapped = 1 - swapped;
\r
324 acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8);
\r
329 acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8);
\r
331 return (u16_t)~(acc & 0xffffUL);
\r
334 /* Here for now until needed in other places in lwIP */
\r
336 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
\r
337 #define isprint(c) in_range(c, 0x20, 0x7f)
\r
338 #define isdigit(c) in_range(c, '0', '9')
\r
339 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
\r
340 #define islower(c) in_range(c, 'a', 'z')
\r
341 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
\r
345 * Ascii internet address interpretation routine.
\r
346 * The value returned is in network order.
\r
350 inet_addr(const char *cp)
\r
352 struct in_addr val;
\r
354 if (inet_aton(cp, &val)) {
\r
355 return (val.s_addr);
\r
357 return (INADDR_NONE);
\r
361 * Check whether "cp" is a valid ascii representation
\r
362 * of an Internet address and convert to a binary address.
\r
363 * Returns 1 if the address is valid, 0 if not.
\r
364 * This replaces inet_addr, the return value from which
\r
365 * cannot distinguish between failure and a local broadcast address.
\r
368 inet_aton(const char *cp, struct in_addr *addr)
\r
378 * Collect number up to ``.''.
\r
379 * Values are specified as for C:
\r
380 * 0x=hex, 0=octal, 1-9=decimal.
\r
388 if (c == 'x' || c == 'X') {
\r
396 val = (val * base) + (int)(c - '0');
\r
398 } else if (base == 16 && isxdigit(c)) {
\r
399 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
\r
408 * a.b.c (with c treated as 16 bits)
\r
409 * a.b (with b treated as 24 bits)
\r
411 if (pp >= parts + 3)
\r
419 * Check for trailing characters.
\r
421 if (c != '\0' && (!isprint(c) || !isspace(c)))
\r
424 * Concoct the address according to
\r
425 * the number of parts specified.
\r
427 n = pp - parts + 1;
\r
431 return (0); /* initial nondigit */
\r
433 case 1: /* a -- 32 bits */
\r
436 case 2: /* a.b -- 8.24 bits */
\r
437 if (val > 0xffffff)
\r
439 val |= parts[0] << 24;
\r
442 case 3: /* a.b.c -- 8.8.16 bits */
\r
445 val |= (parts[0] << 24) | (parts[1] << 16);
\r
448 case 4: /* a.b.c.d -- 8.8.8.8 bits */
\r
451 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
\r
455 addr->s_addr = htonl(val);
\r
459 /* Convert numeric IP address into decimal dotted ASCII representation.
\r
460 * returns ptr to static buffer; not reentrant!
\r
463 inet_ntoa(struct in_addr addr)
\r
465 static char str[16];
\r
466 u32_t s_addr = addr.s_addr;
\r
475 ap = (u8_t *)&s_addr;
\r
476 for(n = 0; n < 4; n++) {
\r
479 rem = *ap % (u8_t)10;
\r
481 inv[i++] = '0' + rem;
\r
493 * These are reference implementations of the byte swapping functions.
\r
494 * Again with the aim of being simple, correct and fully portable.
\r
495 * Byte swapping is the second thing you would want to optimize. You will
\r
496 * need to port it to your architecture and in your cc.h:
\r
498 * #define LWIP_PLATFORM_BYTESWAP 1
\r
499 * #define LWIP_PLATFORM_HTONS(x) <your_htons>
\r
500 * #define LWIP_PLATFORM_HTONL(x) <your_htonl>
\r
502 * Note ntohs() and ntohl() are merely references to the htonx counterparts.
\r
506 #error BYTE_ORDER is not defined
\r
508 #if (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN)
\r
513 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
\r
525 return ((n & 0xff) << 24) |
\r
526 ((n & 0xff00) << 8) |
\r
527 ((n & 0xff0000) >> 8) |
\r
528 ((n & 0xff000000) >> 24);
\r
537 #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */
\r