blob: 645c0398eba86e942ab78c3497b2850dd7be15b4 [file] [log] [blame]
adamdunkels1103ef92003-04-02 09:17:18 +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 *
oliverschmidtac923df2005-02-24 22:06:56 +000034 * $Id: uip_arch.c,v 1.3 2005/02/24 22:06:56 oliverschmidt Exp $
adamdunkels1103ef92003-04-02 09:17:18 +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/*-----------------------------------------------------------------------------------*/
adamdunkels1103ef92003-04-02 09:17:18 +000046void
47uip_add32(u8_t *op32, u16_t op16)
48{
49
50 uip_acc32[3] = op32[3] + (op16 & 0xff);
51 uip_acc32[2] = op32[2] + (op16 >> 8);
52 uip_acc32[1] = op32[1];
53 uip_acc32[0] = op32[0];
54
55 if(uip_acc32[2] < (op16 >> 8)) {
56 ++uip_acc32[1];
57 if(uip_acc32[1] == 0) {
58 ++uip_acc32[0];
59 }
60 }
61
62
63 if(uip_acc32[3] < (op16 & 0xff)) {
64 ++uip_acc32[2];
65 if(uip_acc32[2] == 0) {
66 ++uip_acc32[1];
67 if(uip_acc32[1] == 0) {
68 ++uip_acc32[0];
69 }
70 }
71 }
72}
73/*-----------------------------------------------------------------------------------*/
adamdunkels1103ef92003-04-02 09:17:18 +000074u16_t
75uip_chksum(u16_t *sdata, u16_t len)
76{
adamdunkels6e4840a2004-06-06 07:07:25 +000077 u8_t *dataptr;
78 static u16_t acc, tmp;
adamdunkels1103ef92003-04-02 09:17:18 +000079
adamdunkels6e4840a2004-06-06 07:07:25 +000080 dataptr = sdata;
adamdunkels1103ef92003-04-02 09:17:18 +000081 for(acc = 0; len > 1; len -= 2) {
adamdunkels6e4840a2004-06-06 07:07:25 +000082 tmp = HTONS((((u16_t)*dataptr) << 8)) + HTONS((u16_t)*(dataptr + 1));
83 acc += tmp;
84 if(acc < tmp) {
adamdunkels1103ef92003-04-02 09:17:18 +000085 ++acc;
86 }
adamdunkels6e4840a2004-06-06 07:07:25 +000087 dataptr += 2;
adamdunkels1103ef92003-04-02 09:17:18 +000088 }
adamdunkels6e4840a2004-06-06 07:07:25 +000089
adamdunkels1103ef92003-04-02 09:17:18 +000090 /* add up any odd byte */
91 if(len == 1) {
adamdunkels6e4840a2004-06-06 07:07:25 +000092 tmp = HTONS(((u16_t)(*dataptr)) << 8);
93 acc += tmp;
94 if(acc < tmp) {
adamdunkels1103ef92003-04-02 09:17:18 +000095 ++acc;
96 }
97 }
98
99 return acc;
100}
101/*-----------------------------------------------------------------------------------*/
102u16_t
103uip_ipchksum(void)
104{
oliverschmidtac923df2005-02-24 22:06:56 +0000105 return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
adamdunkels1103ef92003-04-02 09:17:18 +0000106}
107/*-----------------------------------------------------------------------------------*/
108u16_t
109uip_tcpchksum(void)
110{
111 u16_t hsum, sum;
112
113
114 /* Compute the checksum of the TCP header. */
oliverschmidtac923df2005-02-24 22:06:56 +0000115 hsum = uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN], UIP_TCPH_LEN);
adamdunkels1103ef92003-04-02 09:17:18 +0000116
117 /* Compute the checksum of the data in the TCP packet and add it to
118 the TCP header checksum. */
119 sum = uip_chksum((u16_t *)uip_appdata,
oliverschmidtac923df2005-02-24 22:06:56 +0000120 (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) -
121 UIP_IPTCPH_LEN)));
adamdunkels6e4840a2004-06-06 07:07:25 +0000122
adamdunkels1103ef92003-04-02 09:17:18 +0000123
124 if((sum += hsum) < hsum) {
125 ++sum;
126 }
127
128 if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
129 ++sum;
130 }
131 if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
132 ++sum;
133 }
134 if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
135 ++sum;
136 }
137 if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
138 ++sum;
139 }
adamdunkels6e4840a2004-06-06 07:07:25 +0000140 if((sum += (u16_t)HTONS((u16_t)IP_PROTO_TCP)) < (u16_t)HTONS((u16_t)IP_PROTO_TCP)) {
adamdunkels1103ef92003-04-02 09:17:18 +0000141 ++sum;
142 }
143
oliverschmidtac923df2005-02-24 22:06:56 +0000144 hsum = (u16_t)HTONS((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN);
adamdunkels1103ef92003-04-02 09:17:18 +0000145
146 if((sum += hsum) < hsum) {
147 ++sum;
148 }
149
150 return sum;
151}
152/*-----------------------------------------------------------------------------------*/