blob: a6b63c1276f5f695a205201aecf61e8d67c0fa3e [file] [log] [blame]
adamdunkels9fcf9d62003-09-04 19:46:32 +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 *
oliverschmidtad6b7ec2005-02-24 22:07:16 +000034 * $Id: uip_arch.c,v 1.3 2005/02/24 22:07:16 oliverschmidt Exp $
adamdunkels9fcf9d62003-09-04 19:46:32 +000035 *
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 */
138
adamdunkels45019c32003-10-01 08:06:41 +0000139u16_t
140uip_chksum(u16_t *sdata, u16_t len)
adamdunkels9fcf9d62003-09-04 19:46:32 +0000141{
142 u8_t *dataptr;
143 u16_t acc;
144
145 for(acc = 0; len > 1; len -= 2) {
146 acc += *sdata;
147 if(acc < *sdata) {
148 /* Overflow, so we add the carry to acc (i.e., increase by
149 one). */
150 ++acc;
151 }
152 ++sdata;
153 }
154
155 dataptr = sdata;
156
157 /* add up any odd byte */
158 if(len == 1) {
159 acc += htons(((u16_t)(*dataptr)) << 8);
160 if(acc < htons(((u16_t)(*dataptr)) << 8)) {
161 ++acc;
162 }
163 }
164
165 return acc;
166}
167/*-----------------------------------------------------------------------------------*/
168u16_t
169uip_ipchksum(void)
170{
oliverschmidtad6b7ec2005-02-24 22:07:16 +0000171 return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
adamdunkels9fcf9d62003-09-04 19:46:32 +0000172}
173/*-----------------------------------------------------------------------------------*/
174u16_t
175uip_tcpchksum(void)
176{
177 u16_t hsum, sum;
178
179
180 /* Compute the checksum of the TCP header. */
oliverschmidtad6b7ec2005-02-24 22:07:16 +0000181 hsum = uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN], UIP_TCPH_LEN);
adamdunkels9fcf9d62003-09-04 19:46:32 +0000182
183 /* Compute the checksum of the data in the TCP packet and add it to
184 the TCP header checksum. */
adamdunkels45019c32003-10-01 08:06:41 +0000185 sum = uip_chksum((u16_t *)uip_appdata,
oliverschmidtad6b7ec2005-02-24 22:07:16 +0000186 (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) -
187 UIP_IPTCPH_LEN)));
adamdunkels9fcf9d62003-09-04 19:46:32 +0000188
189
190 if((sum += hsum) < hsum) {
191 ++sum;
192 }
193
194 if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
195 ++sum;
196 }
197 if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
198 ++sum;
199 }
200 if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
201 ++sum;
202 }
203 if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
204 ++sum;
205 }
206 if((sum += (u16_t)HTONS((u16_t)IP_PROTO_TCP)) < (u16_t)HTONS((u16_t)IP_PROTO_TCP)) {
207 ++sum;
208 }
209
oliverschmidtad6b7ec2005-02-24 22:07:16 +0000210 hsum = (u16_t)HTONS((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN);
adamdunkels9fcf9d62003-09-04 19:46:32 +0000211
212 if((sum += hsum) < hsum) {
213 ++sum;
214 }
215
216 return sum;
217}
218/*-----------------------------------------------------------------------------------*/