]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/lwip-1.1.0/src/core/inet6.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / lwip-1.1.0 / src / core / inet6.c
1 /*\r
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
3  * All rights reserved. \r
4  * \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
7  *\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
15  *\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
25  * OF SUCH DAMAGE.\r
26  *\r
27  * This file is part of the lwIP TCP/IP stack.\r
28  * \r
29  * Author: Adam Dunkels <adam@sics.se>\r
30  *\r
31  */\r
32 \r
33 \r
34 /* inet6.c\r
35  *\r
36  * Functions common to all TCP/IP modules, such as the Internet checksum and the\r
37  * byte order functions.\r
38  *\r
39  */\r
40 \r
41 \r
42 #include "lwip/opt.h"\r
43 \r
44 #include "lwip/def.h"\r
45 #include "lwip/inet.h"\r
46 \r
47 \r
48 \r
49 /* chksum:\r
50  *\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
53  *\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
56  */\r
57 \r
58 static u32_t\r
59 chksum(void *dataptr, u16_t len)\r
60 {\r
61   u16_t *sdataptr = dataptr;\r
62   u32_t acc;\r
63   \r
64   \r
65   for(acc = 0; len > 1; len -= 2) {\r
66     acc += *sdataptr++;\r
67   }\r
68 \r
69   /* add up any odd byte */\r
70   if (len == 1) {\r
71     acc += htons((u16_t)(*(u8_t *)dataptr) << 8);\r
72   }\r
73 \r
74   return acc;\r
75 \r
76 }\r
77 \r
78 /* inet_chksum_pseudo:\r
79  *\r
80  * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.\r
81  */\r
82 \r
83 u16_t\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
87 {\r
88   u32_t acc;\r
89   struct pbuf *q;\r
90   u8_t swapped, i;\r
91 \r
92   acc = 0;\r
93   swapped = 0;\r
94   for(q = p; q != NULL; q = q->next) {    \r
95     acc += chksum(q->payload, q->len);\r
96     while (acc >> 16) {\r
97       acc = (acc & 0xffff) + (acc >> 16);\r
98     }\r
99     if (q->len % 2 != 0) {\r
100       swapped = 1 - swapped;\r
101       acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);\r
102     }\r
103   }\r
104 \r
105   if (swapped) {\r
106     acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);\r
107   }\r
108   \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
114     }\r
115   }\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
119 \r
120   while (acc >> 16) {\r
121     acc = (acc & 0xffff) + (acc >> 16);\r
122   }\r
123   return ~(acc & 0xffff);\r
124 }\r
125 \r
126 /* inet_chksum:\r
127  *\r
128  * Calculates the Internet checksum over a portion of memory. Used primarely for IP\r
129  * and ICMP.\r
130  */\r
131 \r
132 u16_t\r
133 inet_chksum(void *dataptr, u16_t len)\r
134 {\r
135   u32_t acc, sum;\r
136 \r
137   acc = chksum(dataptr, len);\r
138   sum = (acc & 0xffff) + (acc >> 16);\r
139   sum += (sum >> 16);\r
140   return ~(sum & 0xffff);\r
141 }\r
142 \r
143 u16_t\r
144 inet_chksum_pbuf(struct pbuf *p)\r
145 {\r
146   u32_t acc;\r
147   struct pbuf *q;\r
148   u8_t swapped;\r
149   \r
150   acc = 0;\r
151   swapped = 0;\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
156     }    \r
157     if (q->len % 2 != 0) {\r
158       swapped = 1 - swapped;\r
159       acc = (acc & 0xff << 8) | (acc & 0xff00 >> 8);\r
160     }\r
161   }\r
162  \r
163   if (swapped) {\r
164     acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);\r
165   }\r
166   return ~(acc & 0xffff);\r
167 }\r
168 \r