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/def.h"
\r
45 #include "lwip/inet.h"
\r
51 * Sums up all 16 bit words in a memory portion. Also includes any odd byte.
\r
52 * This function is used by the other checksum functions.
\r
54 * For now, this is not optimized. Must be optimized for the particular processor
\r
55 * arcitecture on which it is to run. Preferebly coded in assembler.
\r
59 chksum(void *dataptr, u16_t len)
\r
61 u16_t *sdataptr = dataptr;
\r
65 for(acc = 0; len > 1; len -= 2) {
\r
69 /* add up any odd byte */
\r
71 acc += htons((u16_t)(*(u8_t *)dataptr) << 8);
\r
78 /* inet_chksum_pseudo:
\r
80 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
\r
84 inet_chksum_pseudo(struct pbuf *p,
\r
85 struct ip_addr *src, struct ip_addr *dest,
\r
86 u8_t proto, u32_t proto_len)
\r
94 for(q = p; q != NULL; q = q->next) {
\r
95 acc += chksum(q->payload, q->len);
\r
97 acc = (acc & 0xffff) + (acc >> 16);
\r
99 if (q->len % 2 != 0) {
\r
100 swapped = 1 - swapped;
\r
101 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
\r
106 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
\r
109 for(i = 0; i < 8; i++) {
\r
110 acc += ((u16_t *)src->addr)[i] & 0xffff;
\r
111 acc += ((u16_t *)dest->addr)[i] & 0xffff;
\r
112 while (acc >> 16) {
\r
113 acc = (acc & 0xffff) + (acc >> 16);
\r
116 acc += (u16_t)htons((u16_t)proto);
\r
117 acc += ((u16_t *)&proto_len)[0] & 0xffff;
\r
118 acc += ((u16_t *)&proto_len)[1] & 0xffff;
\r
120 while (acc >> 16) {
\r
121 acc = (acc & 0xffff) + (acc >> 16);
\r
123 return ~(acc & 0xffff);
\r
128 * Calculates the Internet checksum over a portion of memory. Used primarely for IP
\r
133 inet_chksum(void *dataptr, u16_t len)
\r
137 acc = chksum(dataptr, len);
\r
138 sum = (acc & 0xffff) + (acc >> 16);
\r
139 sum += (sum >> 16);
\r
140 return ~(sum & 0xffff);
\r
144 inet_chksum_pbuf(struct pbuf *p)
\r
152 for(q = p; q != NULL; q = q->next) {
\r
153 acc += chksum(q->payload, q->len);
\r
154 while (acc >> 16) {
\r
155 acc = (acc & 0xffff) + (acc >> 16);
\r
157 if (q->len % 2 != 0) {
\r
158 swapped = 1 - swapped;
\r
159 acc = (acc & 0xff << 8) | (acc & 0xff00 >> 8);
\r
164 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
\r
166 return ~(acc & 0xffff);
\r