2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
36 * Functions common to all TCP/IP modules, such as the Internet checksum and the
37 * byte order functions.
44 #include "lwip/arch.h"
47 #include "lwip/inet.h"
52 lwip_chksum(void *dataptr, int len)
56 LWIP_DEBUGF(INET_DEBUG, ("lwip_chksum(%p, %d)\n", (void *)dataptr, len));
57 for(acc = 0; len > 1; len -= 2) {
58 /* acc = acc + *((u16_t *)dataptr)++;*/
59 acc += *(u16_t *)dataptr;
60 dataptr = (void *)((u16_t *)dataptr + 1);
63 /* add up any odd byte */
65 acc += htons((u16_t)((*(u8_t *)dataptr) & 0xff) << 8);
66 LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: odd byte %d\n", (unsigned int)(*(u8_t *)dataptr)));
68 LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: no odd byte\n"));
70 acc = (acc >> 16) + (acc & 0xffffUL);
72 if ((acc & 0xffff0000) != 0) {
73 acc = (acc >> 16) + (acc & 0xffffUL);
79 /* inet_chksum_pseudo:
81 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
85 inet_chksum_pseudo(struct pbuf *p,
86 struct ip_addr *src, struct ip_addr *dest,
87 u8_t proto, u16_t proto_len)
95 /* iterate through all pbuf in chain */
96 for(q = p; q != NULL; q = q->next) {
97 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
98 (void *)q, (void *)q->next));
99 acc += lwip_chksum(q->payload, q->len);
100 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%lx \n", acc));*/
102 acc = (acc & 0xffffUL) + (acc >> 16);
104 if (q->len % 2 != 0) {
105 swapped = 1 - swapped;
106 acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
108 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%lx \n", acc));*/
112 acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
114 acc += (src->addr & 0xffffUL);
115 acc += ((src->addr >> 16) & 0xffffUL);
116 acc += (dest->addr & 0xffffUL);
117 acc += ((dest->addr >> 16) & 0xffffUL);
118 acc += (u32_t)htons((u16_t)proto);
119 acc += (u32_t)htons(proto_len);
122 acc = (acc & 0xffffUL) + (acc >> 16);
124 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%lx\n", acc));
125 return ~(acc & 0xffffUL);
130 * Calculates the Internet checksum over a portion of memory. Used primarely for IP
135 inet_chksum(void *dataptr, u16_t len)
139 acc = lwip_chksum(dataptr, len);
141 acc = (acc & 0xffff) + (acc >> 16);
143 return ~(acc & 0xffff);
147 inet_chksum_pbuf(struct pbuf *p)
155 for(q = p; q != NULL; q = q->next) {
156 acc += lwip_chksum(q->payload, q->len);
158 acc = (acc & 0xffffUL) + (acc >> 16);
160 if (q->len % 2 != 0) {
161 swapped = 1 - swapped;
162 acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8);
167 acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8);
169 return ~(acc & 0xffffUL);
172 /* Here for now until needed in other places in lwIP */
174 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
175 #define isascii(c) in_range(c, 0x20, 0x7f)
176 #define isdigit(c) in_range(c, '0', '9')
177 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
178 #define islower(c) in_range(c, 'a', 'z')
179 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
184 * Ascii internet address interpretation routine.
185 * The value returned is in network order.
190 u32_t inet_addr(const char *cp)
194 if (inet_aton(cp, &val)) {
197 return (INADDR_NONE);
201 * Check whether "cp" is a valid ascii representation
202 * of an Internet address and convert to a binary address.
203 * Returns 1 if the address is valid, 0 if not.
204 * This replaces inet_addr, the return value from which
205 * cannot distinguish between failure and a local broadcast address.
209 int inet_aton(const char *cp, struct in_addr *addr)
220 * Collect number up to ``.''.
221 * Values are specified as for C:
222 * 0x=hex, 0=octal, isdigit=decimal.
229 if (c == 'x' || c == 'X')
230 base = 16, c = *++cp;
235 if (isascii(c) && isdigit(c)) {
236 val = (val * base) + (c - '0');
238 } else if (base == 16 && isascii(c) && isxdigit(c)) {
240 (c + 10 - (islower(c) ? 'a' : 'A'));
249 * a.b.c (with c treated as 16 bits)
250 * a.b (with b treated as 24 bits)
260 * Check for trailing characters.
262 if (c != '\0' && (!isascii(c) || !isspace(c)))
265 * Concoct the address according to
266 * the number of parts specified.
272 return (0); /* initial nondigit */
274 case 1: /* a -- 32 bits */
277 case 2: /* a.b -- 8.24 bits */
280 val |= parts[0] << 24;
283 case 3: /* a.b.c -- 8.8.16 bits */
286 val |= (parts[0] << 24) | (parts[1] << 16);
289 case 4: /* a.b.c.d -- 8.8.8.8 bits */
292 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
296 addr->s_addr = htonl(val);
300 /* Convert numeric IP address into decimal dotted ASCII representation.
301 * returns ptr to static buffer; not reentrant!
303 u8_t *inet_ntoa(u32_t addr)
315 for(n = 0; n < 4; n++) {
318 rem = *ap % (u8_t)10;
320 inv[i++] = '0' + rem;
333 #error BYTE_ORDER is not defined
335 #if BYTE_ORDER == LITTLE_ENDIAN
340 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
352 return ((n & 0xff) << 24) |
353 ((n & 0xff00) << 8) |
354 ((n & 0xff0000) >> 8) |
355 ((n & 0xff000000) >> 24);
364 #endif /* BYTE_ORDER == LITTLE_ENDIAN */