blob: 0ffa6d5340d16d5917e622cf9aace37488a614db [file] [log] [blame]
oliverschmidt06437bc2004-07-15 00:31:10 +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 *
oliverschmidtc6948dd2005-02-24 22:09:03 +000034 * $Id: uip_arch.c,v 1.2 2005/02/24 22:09:03 oliverschmidt Exp $
oliverschmidt06437bc2004-07-15 00:31:10 +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/*-----------------------------------------------------------------------------------*/
46void
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/*-----------------------------------------------------------------------------------*/
74u16_t
75uip_chksum(u16_t *sdata, u16_t len)
76{
77 u8_t *dataptr;
78 static u16_t acc, tmp;
79
80 dataptr = sdata;
81 for(acc = 0; len > 1; len -= 2) {
82 tmp = HTONS((((u16_t)*dataptr) << 8)) + HTONS((u16_t)*(dataptr + 1));
83 acc += tmp;
84 if(acc < tmp) {
85 ++acc;
86 }
87 dataptr += 2;
88 }
89
90 /* add up any odd byte */
91 if(len == 1) {
92 tmp = HTONS(((u16_t)(*dataptr)) << 8);
93 acc += tmp;
94 if(acc < tmp) {
95 ++acc;
96 }
97 }
98
99 return acc;
100}
101/*-----------------------------------------------------------------------------------*/
102u16_t
103uip_ipchksum(void)
104{
oliverschmidtc6948dd2005-02-24 22:09:03 +0000105 return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
oliverschmidt06437bc2004-07-15 00:31:10 +0000106}
107/*-----------------------------------------------------------------------------------*/
108u16_t
109uip_tcpchksum(void)
110{
111 u16_t hsum, sum;
112
113
114 /* Compute the checksum of the TCP header. */
oliverschmidtc6948dd2005-02-24 22:09:03 +0000115 hsum = uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN], UIP_TCPH_LEN);
oliverschmidt06437bc2004-07-15 00:31:10 +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,
oliverschmidtc6948dd2005-02-24 22:09:03 +0000120 (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) -
121 UIP_IPTCPH_LEN)));
oliverschmidt06437bc2004-07-15 00:31:10 +0000122
123
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 }
140 if((sum += (u16_t)HTONS((u16_t)IP_PROTO_TCP)) < (u16_t)HTONS((u16_t)IP_PROTO_TCP)) {
141 ++sum;
142 }
143
oliverschmidtc6948dd2005-02-24 22:09:03 +0000144 hsum = (u16_t)HTONS((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN);
oliverschmidt06437bc2004-07-15 00:31:10 +0000145
146 if((sum += hsum) < hsum) {
147 ++sum;
148 }
149
150 return sum;
151}
152/*-----------------------------------------------------------------------------------*/