blob: 50cd7f7a83566a5f366c6554b7003452cfb48dc1 [file] [log] [blame]
gpz452b5bd2003-05-19 08:21:01 +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 *
oliverschmidta05e0b22005-02-24 22:05:24 +000034 * $Id: uip_arch.c,v 1.3 2005/02/24 22:05:24 oliverschmidt Exp $
gpz452b5bd2003-05-19 08:21:01 +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/*-----------------------------------------------------------------------------------*/
gpz452b5bd2003-05-19 08:21:01 +000046void
47uip_add_rcv_nxt(u16_t n)
48{
49 uip_conn->rcv_nxt[3] += (n & 0xff);
50 uip_conn->rcv_nxt[2] += (n >> 8);
51
52 if(uip_conn->rcv_nxt[2] < (n >> 8)) {
53 ++uip_conn->rcv_nxt[1];
54 if(uip_conn->rcv_nxt[1] == 0) {
55 ++uip_conn->rcv_nxt[0];
56 }
57 }
58
59
60 if(uip_conn->rcv_nxt[3] < (n & 0xff)) {
61 ++uip_conn->rcv_nxt[2];
62 if(uip_conn->rcv_nxt[2] == 0) {
63 ++uip_conn->rcv_nxt[1];
64 if(uip_conn->rcv_nxt[1] == 0) {
65 ++uip_conn->rcv_nxt[0];
66 }
67 }
68 }
69}
70/*-----------------------------------------------------------------------------------*/
71void
72uip_add32(u8_t *op32, u16_t op16)
73{
74
75 uip_acc32[3] = op32[3] + (op16 & 0xff);
76 uip_acc32[2] = op32[2] + (op16 >> 8);
77 uip_acc32[1] = op32[1];
78 uip_acc32[0] = op32[0];
79
80 if(uip_acc32[2] < (op16 >> 8)) {
81 ++uip_acc32[1];
82 if(uip_acc32[1] == 0) {
83 ++uip_acc32[0];
84 }
85 }
86
87
88 if(uip_acc32[3] < (op16 & 0xff)) {
89 ++uip_acc32[2];
90 if(uip_acc32[2] == 0) {
91 ++uip_acc32[1];
92 if(uip_acc32[1] == 0) {
93 ++uip_acc32[0];
94 }
95 }
96 }
97}
98/*-----------------------------------------------------------------------------------*/
gpz452b5bd2003-05-19 08:21:01 +000099u16_t
100uip_chksum(u16_t *sdata, u16_t len)
101{
102 u16_t acc;
103
104 for(acc = 0; len > 1; len -= 2) {
105 acc += *sdata;
106 if(acc < *sdata) {
107 /* Overflow, so we add the carry to acc (i.e., increase by
108 one). */
109 ++acc;
110 }
111 ++sdata;
112 }
113
114 /* add up any odd byte */
115 if(len == 1) {
116 acc += htons(((u16_t)(*(u8_t *)sdata)) << 8);
117 if(acc < htons(((u16_t)(*(u8_t *)sdata)) << 8)) {
118 ++acc;
119 }
120 }
121
122 return acc;
123}
124/*-----------------------------------------------------------------------------------*/
125u16_t
126uip_ipchksum(void)
127{
oliverschmidta05e0b22005-02-24 22:05:24 +0000128 return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
gpz452b5bd2003-05-19 08:21:01 +0000129}
130/*-----------------------------------------------------------------------------------*/
131u16_t
132uip_tcpchksum(void)
133{
134 u16_t hsum, sum;
135
136
137 /* Compute the checksum of the TCP header. */
oliverschmidta05e0b22005-02-24 22:05:24 +0000138 hsum = uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN], UIP_TCPH_LEN);
gpz452b5bd2003-05-19 08:21:01 +0000139
140 /* Compute the checksum of the data in the TCP packet and add it to
141 the TCP header checksum. */
142 sum = uip_chksum((u16_t *)uip_appdata,
oliverschmidta05e0b22005-02-24 22:05:24 +0000143 (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) -
144 UIP_IPTCPH_LEN)));
gpz452b5bd2003-05-19 08:21:01 +0000145
146 if((sum += hsum) < hsum) {
147 ++sum;
148 }
149
150 if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
151 ++sum;
152 }
153 if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
154 ++sum;
155 }
156 if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
157 ++sum;
158 }
159 if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
160 ++sum;
161 }
162 if((sum += (u16_t)htons((u16_t)IP_PROTO_TCP)) < (u16_t)htons((u16_t)IP_PROTO_TCP)) {
163 ++sum;
164 }
165
oliverschmidta05e0b22005-02-24 22:05:24 +0000166 hsum = (u16_t)HTONS((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN);
gpz452b5bd2003-05-19 08:21:01 +0000167
168 if((sum += hsum) < hsum) {
169 ++sum;
170 }
171
172 return sum;
173}
174/*-----------------------------------------------------------------------------------*/