blob: f83e5e4b73d95d35e5341feaad4f1204bc1327b2 [file] [log] [blame]
gpzbc285042003-05-19 09:13:37 +00001/*
2 * Copyright (c) 2001, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Adam Dunkels.
16 * 4. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * This file is part of the uIP TCP/IP stack.
33 *
34 * $Id: uip_arch.c,v 1.1 2003/05/19 09:13:42 gpz Exp $
35 *
36 */
37
38
39#include "uip.h"
40#include "uip_arch.h"
41
42#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
43#define IP_PROTO_TCP 6
44
45/*-----------------------------------------------------------------------------------*/
46#if UIP_BUFSIZE > 255
47/*-----------------------------------------------------------------------------------*/
48void
49uip_add_rcv_nxt(u16_t n)
50{
51 uip_conn->rcv_nxt[3] += (n & 0xff);
52 uip_conn->rcv_nxt[2] += (n >> 8);
53
54 if(uip_conn->rcv_nxt[2] < (n >> 8)) {
55 ++uip_conn->rcv_nxt[1];
56 if(uip_conn->rcv_nxt[1] == 0) {
57 ++uip_conn->rcv_nxt[0];
58 }
59 }
60
61
62 if(uip_conn->rcv_nxt[3] < (n & 0xff)) {
63 ++uip_conn->rcv_nxt[2];
64 if(uip_conn->rcv_nxt[2] == 0) {
65 ++uip_conn->rcv_nxt[1];
66 if(uip_conn->rcv_nxt[1] == 0) {
67 ++uip_conn->rcv_nxt[0];
68 }
69 }
70 }
71}
72/*-----------------------------------------------------------------------------------*/
73void
74uip_add32(u8_t *op32, u16_t op16)
75{
76
77 uip_acc32[3] = op32[3] + (op16 & 0xff);
78 uip_acc32[2] = op32[2] + (op16 >> 8);
79 uip_acc32[1] = op32[1];
80 uip_acc32[0] = op32[0];
81
82 if(uip_acc32[2] < (op16 >> 8)) {
83 ++uip_acc32[1];
84 if(uip_acc32[1] == 0) {
85 ++uip_acc32[0];
86 }
87 }
88
89
90 if(uip_acc32[3] < (op16 & 0xff)) {
91 ++uip_acc32[2];
92 if(uip_acc32[2] == 0) {
93 ++uip_acc32[1];
94 if(uip_acc32[1] == 0) {
95 ++uip_acc32[0];
96 }
97 }
98 }
99}
100/*-----------------------------------------------------------------------------------*/
101#else /* UIP_BUFSIZE > 255 */
102/*-----------------------------------------------------------------------------------*/
103void
104uip_add_rcv_nxt(u8_t n)
105{
106 uip_conn->rcv_nxt[3] += n;
107 if(uip_conn->rcv_nxt[3] < n) {
108 ++uip_conn->rcv_nxt[2];
109 if(uip_conn->rcv_nxt[2] == 0) {
110 ++uip_conn->rcv_nxt[1];
111 if(uip_conn->rcv_nxt[1] == 0) {
112 ++uip_conn->rcv_nxt[0];
113 }
114 }
115 }
116}
117/*-----------------------------------------------------------------------------------*/
118void
119uip_add32(u8_t *op32, u8_t op8)
120{
121 uip_acc32[3] = op32[3] + op8;
122 uip_acc32[2] = op32[2];
123 uip_acc32[1] = op32[1];
124 uip_acc32[0] = op32[0];
125
126 if(uip_acc32[3] < op8) {
127 ++uip_acc32[2];
128 if(uip_acc32[2] == 0) {
129 ++uip_acc32[1];
130 if(uip_acc32[1] == 0) {
131 ++uip_acc32[0];
132 }
133 }
134 }
135}
136/*-----------------------------------------------------------------------------------*/
137#endif /* UIP_BUFSIZE > 255 */
138u16_t
139uip_chksum(u16_t *sdata, u16_t len)
140{
141 u16_t acc;
142
143 for(acc = 0; len > 1; len -= 2) {
144 acc += *sdata;
145 if(acc < *sdata) {
146 /* Overflow, so we add the carry to acc (i.e., increase by
147 one). */
148 ++acc;
149 }
150 ++sdata;
151 }
152
153 /* add up any odd byte */
154 if(len == 1) {
155 acc += htons(((u16_t)(*(u8_t *)sdata)) << 8);
156 if(acc < htons(((u16_t)(*(u8_t *)sdata)) << 8)) {
157 ++acc;
158 }
159 }
160
161 return acc;
162}
163/*-----------------------------------------------------------------------------------*/
164u16_t
165uip_ipchksum(void)
166{
167 return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], 20);
168}
169/*-----------------------------------------------------------------------------------*/
170u16_t
171uip_tcpchksum(void)
172{
173 u16_t hsum, sum;
174
175
176 /* Compute the checksum of the TCP header. */
177 hsum = uip_chksum((u16_t *)&uip_buf[20 + UIP_LLH_LEN], 20);
178
179 /* Compute the checksum of the data in the TCP packet and add it to
180 the TCP header checksum. */
181 sum = uip_chksum((u16_t *)uip_appdata,
182 (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 40)));
183
184 if((sum += hsum) < hsum) {
185 ++sum;
186 }
187
188 if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
189 ++sum;
190 }
191 if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
192 ++sum;
193 }
194 if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
195 ++sum;
196 }
197 if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
198 ++sum;
199 }
200 if((sum += (u16_t)htons((u16_t)IP_PROTO_TCP)) < (u16_t)htons((u16_t)IP_PROTO_TCP)) {
201 ++sum;
202 }
203
204 hsum = (u16_t)htons((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 20);
205
206 if((sum += hsum) < hsum) {
207 ++sum;
208 }
209
210 return sum;
211}
212/*-----------------------------------------------------------------------------------*/