Rev Author Line No. Line
3328 povik 1 /*
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. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * This file is part of the uIP TCP/IP stack.
30 *
31 * $Id: uip_arch.c,v 1.2.2.1 2003/10/04 22:54:17 adam Exp $
32 *
33 */
34  
35  
36 #include "uip.h"
37 #include "uip_arch.h"
38  
39 #define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
40 #define IP_PROTO_TCP 6
41  
42 /*-----------------------------------------------------------------------------------*/
43 void uip_add32(u8_t *op32, u16_t op16)
44 {
45  
46 uip_acc32[3] = op32[3] + (op16 & 0xff);
47 uip_acc32[2] = op32[2] + (op16 >> 8);
48 uip_acc32[1] = op32[1];
49 uip_acc32[0] = op32[0];
50  
51 if(uip_acc32[2] < (op16 >> 8)) {
52 ++uip_acc32[1];
53 if(uip_acc32[1] == 0) {
54 ++uip_acc32[0];
55 }
56 }
57  
58  
59 if(uip_acc32[3] < (op16 & 0xff)) {
60 ++uip_acc32[2];
61 if(uip_acc32[2] == 0) {
62 ++uip_acc32[1];
63 if(uip_acc32[1] == 0) {
64 ++uip_acc32[0];
65 }
66 }
67 }
68 }
69 /*-----------------------------------------------------------------------------------*/
70 u16_t uip_chksum(u16_t *sdata, u16_t len)
71 {
72 u16_t acc;
73  
74 for (acc = 0; len > 1; len -= 2) {
75 u16_t u = ((unsigned char *)sdata)[0] + (((unsigned char *)sdata)[1] << 8);
76 if ((acc += u) < u) {
77 /* Overflow, so we add the carry to acc (i.e., increase by
78 one). */
79 ++acc;
80 }
81 ++sdata;
82 }
83  
84 /* add up any odd byte */
85 if(len == 1) {
86 acc += htons(((u16_t)(*(u8_t *)sdata)) << 8);
87 if(acc < htons(((u16_t)(*(u8_t *)sdata)) << 8)) {
88 ++acc;
89 }
90 }
91  
92 return acc;
93 }
94 /*-----------------------------------------------------------------------------------*/
95 u16_t uip_ipchksum(void)
96 {
97 return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], 20);
98 }
99 /*-----------------------------------------------------------------------------------*/
100 u16_t uip_tcpchksum(void)
101 {
102 u16_t hsum, sum;
103  
104  
105 /* Compute the checksum of the TCP header. */
106 hsum = uip_chksum((u16_t *)&uip_buf[20 + UIP_LLH_LEN], 20);
107  
108 /* Compute the checksum of the data in the TCP packet and add it to
109 the TCP header checksum. */
110 sum = uip_chksum((u16_t *)uip_appdata,
111 (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 40)));
112  
113 if((sum += hsum) < hsum) {
114 ++sum;
115 }
116  
117 if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
118 ++sum;
119 }
120 if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
121 ++sum;
122 }
123 if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
124 ++sum;
125 }
126 if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
127 ++sum;
128 }
129  
130 if((sum += (u16_t)htons((u16_t)IP_PROTO_TCP)) < (u16_t)htons((u16_t)IP_PROTO_TCP)) {
131 ++sum;
132 }
133  
134 hsum = (u16_t)htons((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 20);
135  
136 if((sum += hsum) < hsum) {
137 ++sum;
138 }
139  
140 return sum;
141 }
142  
143