I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1 /**
tax 0:66300c77c6e9 2 * @file
tax 0:66300c77c6e9 3 * Incluse internet checksum functions.
tax 0:66300c77c6e9 4 *
tax 0:66300c77c6e9 5 */
tax 0:66300c77c6e9 6
tax 0:66300c77c6e9 7 /*
tax 0:66300c77c6e9 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
tax 0:66300c77c6e9 9 * All rights reserved.
tax 0:66300c77c6e9 10 *
tax 0:66300c77c6e9 11 * Redistribution and use in source and binary forms, with or without modification,
tax 0:66300c77c6e9 12 * are permitted provided that the following conditions are met:
tax 0:66300c77c6e9 13 *
tax 0:66300c77c6e9 14 * 1. Redistributions of source code must retain the above copyright notice,
tax 0:66300c77c6e9 15 * this list of conditions and the following disclaimer.
tax 0:66300c77c6e9 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
tax 0:66300c77c6e9 17 * this list of conditions and the following disclaimer in the documentation
tax 0:66300c77c6e9 18 * and/or other materials provided with the distribution.
tax 0:66300c77c6e9 19 * 3. The name of the author may not be used to endorse or promote products
tax 0:66300c77c6e9 20 * derived from this software without specific prior written permission.
tax 0:66300c77c6e9 21 *
tax 0:66300c77c6e9 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
tax 0:66300c77c6e9 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
tax 0:66300c77c6e9 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
tax 0:66300c77c6e9 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
tax 0:66300c77c6e9 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
tax 0:66300c77c6e9 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
tax 0:66300c77c6e9 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
tax 0:66300c77c6e9 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
tax 0:66300c77c6e9 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
tax 0:66300c77c6e9 31 * OF SUCH DAMAGE.
tax 0:66300c77c6e9 32 *
tax 0:66300c77c6e9 33 * This file is part of the lwIP TCP/IP stack.
tax 0:66300c77c6e9 34 *
tax 0:66300c77c6e9 35 * Author: Adam Dunkels <adam@sics.se>
tax 0:66300c77c6e9 36 *
tax 0:66300c77c6e9 37 */
tax 0:66300c77c6e9 38
tax 0:66300c77c6e9 39 #include "lwip/opt.h"
tax 0:66300c77c6e9 40
tax 0:66300c77c6e9 41 #include "lwip/inet_chksum.h"
tax 0:66300c77c6e9 42 #include "lwip/def.h"
tax 0:66300c77c6e9 43
tax 0:66300c77c6e9 44 #include <stddef.h>
tax 0:66300c77c6e9 45 #include <string.h>
tax 0:66300c77c6e9 46
tax 0:66300c77c6e9 47 /* These are some reference implementations of the checksum algorithm, with the
tax 0:66300c77c6e9 48 * aim of being simple, correct and fully portable. Checksumming is the
tax 0:66300c77c6e9 49 * first thing you would want to optimize for your platform. If you create
tax 0:66300c77c6e9 50 * your own version, link it in and in your cc.h put:
tax 0:66300c77c6e9 51 *
tax 0:66300c77c6e9 52 * #define LWIP_CHKSUM <your_checksum_routine>
tax 0:66300c77c6e9 53 *
tax 0:66300c77c6e9 54 * Or you can select from the implementations below by defining
tax 0:66300c77c6e9 55 * LWIP_CHKSUM_ALGORITHM to 1, 2 or 3.
tax 0:66300c77c6e9 56 */
tax 0:66300c77c6e9 57
tax 0:66300c77c6e9 58 #ifndef LWIP_CHKSUM
tax 0:66300c77c6e9 59 # define LWIP_CHKSUM lwip_standard_chksum
tax 0:66300c77c6e9 60 # ifndef LWIP_CHKSUM_ALGORITHM
tax 0:66300c77c6e9 61 # define LWIP_CHKSUM_ALGORITHM 2
tax 0:66300c77c6e9 62 # endif
tax 0:66300c77c6e9 63 #endif
tax 0:66300c77c6e9 64 /* If none set: */
tax 0:66300c77c6e9 65 #ifndef LWIP_CHKSUM_ALGORITHM
tax 0:66300c77c6e9 66 # define LWIP_CHKSUM_ALGORITHM 0
tax 0:66300c77c6e9 67 #endif
tax 0:66300c77c6e9 68
tax 0:66300c77c6e9 69 #if (LWIP_CHKSUM_ALGORITHM == 1) /* Version #1 */
tax 0:66300c77c6e9 70 /**
tax 0:66300c77c6e9 71 * lwip checksum
tax 0:66300c77c6e9 72 *
tax 0:66300c77c6e9 73 * @param dataptr points to start of data to be summed at any boundary
tax 0:66300c77c6e9 74 * @param len length of data to be summed
tax 0:66300c77c6e9 75 * @return host order (!) lwip checksum (non-inverted Internet sum)
tax 0:66300c77c6e9 76 *
tax 0:66300c77c6e9 77 * @note accumulator size limits summable length to 64k
tax 0:66300c77c6e9 78 * @note host endianess is irrelevant (p3 RFC1071)
tax 0:66300c77c6e9 79 */
tax 0:66300c77c6e9 80 static u16_t
tax 0:66300c77c6e9 81 lwip_standard_chksum(void *dataptr, u16_t len)
tax 0:66300c77c6e9 82 {
tax 0:66300c77c6e9 83 u32_t acc;
tax 0:66300c77c6e9 84 u16_t src;
tax 0:66300c77c6e9 85 u8_t *octetptr;
tax 0:66300c77c6e9 86
tax 0:66300c77c6e9 87 acc = 0;
tax 0:66300c77c6e9 88 /* dataptr may be at odd or even addresses */
tax 0:66300c77c6e9 89 octetptr = (u8_t*)dataptr;
tax 0:66300c77c6e9 90 while (len > 1) {
tax 0:66300c77c6e9 91 /* declare first octet as most significant
tax 0:66300c77c6e9 92 thus assume network order, ignoring host order */
tax 0:66300c77c6e9 93 src = (*octetptr) << 8;
tax 0:66300c77c6e9 94 octetptr++;
tax 0:66300c77c6e9 95 /* declare second octet as least significant */
tax 0:66300c77c6e9 96 src |= (*octetptr);
tax 0:66300c77c6e9 97 octetptr++;
tax 0:66300c77c6e9 98 acc += src;
tax 0:66300c77c6e9 99 len -= 2;
tax 0:66300c77c6e9 100 }
tax 0:66300c77c6e9 101 if (len > 0) {
tax 0:66300c77c6e9 102 /* accumulate remaining octet */
tax 0:66300c77c6e9 103 src = (*octetptr) << 8;
tax 0:66300c77c6e9 104 acc += src;
tax 0:66300c77c6e9 105 }
tax 0:66300c77c6e9 106 /* add deferred carry bits */
tax 0:66300c77c6e9 107 acc = (acc >> 16) + (acc & 0x0000ffffUL);
tax 0:66300c77c6e9 108 if ((acc & 0xffff0000UL) != 0) {
tax 0:66300c77c6e9 109 acc = (acc >> 16) + (acc & 0x0000ffffUL);
tax 0:66300c77c6e9 110 }
tax 0:66300c77c6e9 111 /* This maybe a little confusing: reorder sum using htons()
tax 0:66300c77c6e9 112 instead of ntohs() since it has a little less call overhead.
tax 0:66300c77c6e9 113 The caller must invert bits for Internet sum ! */
tax 0:66300c77c6e9 114 return htons((u16_t)acc);
tax 0:66300c77c6e9 115 }
tax 0:66300c77c6e9 116 #endif
tax 0:66300c77c6e9 117
tax 0:66300c77c6e9 118 #if (LWIP_CHKSUM_ALGORITHM == 2) /* Alternative version #2 */
tax 0:66300c77c6e9 119 /*
tax 0:66300c77c6e9 120 * Curt McDowell
tax 0:66300c77c6e9 121 * Broadcom Corp.
tax 0:66300c77c6e9 122 * csm@broadcom.com
tax 0:66300c77c6e9 123 *
tax 0:66300c77c6e9 124 * IP checksum two bytes at a time with support for
tax 0:66300c77c6e9 125 * unaligned buffer.
tax 0:66300c77c6e9 126 * Works for len up to and including 0x20000.
tax 0:66300c77c6e9 127 * by Curt McDowell, Broadcom Corp. 12/08/2005
tax 0:66300c77c6e9 128 *
tax 0:66300c77c6e9 129 * @param dataptr points to start of data to be summed at any boundary
tax 0:66300c77c6e9 130 * @param len length of data to be summed
tax 0:66300c77c6e9 131 * @return host order (!) lwip checksum (non-inverted Internet sum)
tax 0:66300c77c6e9 132 */
tax 0:66300c77c6e9 133
tax 0:66300c77c6e9 134 static u16_t
tax 0:66300c77c6e9 135 lwip_standard_chksum(void *dataptr, int len)
tax 0:66300c77c6e9 136 {
tax 0:66300c77c6e9 137 u8_t *pb = (u8_t *)dataptr;
tax 0:66300c77c6e9 138 u16_t *ps, t = 0;
tax 0:66300c77c6e9 139 u32_t sum = 0;
tax 0:66300c77c6e9 140 int odd = ((mem_ptr_t)pb & 1);
tax 0:66300c77c6e9 141
tax 0:66300c77c6e9 142 /* Get aligned to u16_t */
tax 0:66300c77c6e9 143 if (odd && len > 0) {
tax 0:66300c77c6e9 144 ((u8_t *)&t)[1] = *pb++;
tax 0:66300c77c6e9 145 len--;
tax 0:66300c77c6e9 146 }
tax 0:66300c77c6e9 147
tax 0:66300c77c6e9 148 /* Add the bulk of the data */
tax 0:66300c77c6e9 149 ps = (u16_t *)(void *)pb;
tax 0:66300c77c6e9 150 while (len > 1) {
tax 0:66300c77c6e9 151 sum += *ps++;
tax 0:66300c77c6e9 152 len -= 2;
tax 0:66300c77c6e9 153 }
tax 0:66300c77c6e9 154
tax 0:66300c77c6e9 155 /* Consume left-over byte, if any */
tax 0:66300c77c6e9 156 if (len > 0) {
tax 0:66300c77c6e9 157 ((u8_t *)&t)[0] = *(u8_t *)ps;
tax 0:66300c77c6e9 158 }
tax 0:66300c77c6e9 159
tax 0:66300c77c6e9 160 /* Add end bytes */
tax 0:66300c77c6e9 161 sum += t;
tax 0:66300c77c6e9 162
tax 0:66300c77c6e9 163 /* Fold 32-bit sum to 16 bits
tax 0:66300c77c6e9 164 calling this twice is propably faster than if statements... */
tax 0:66300c77c6e9 165 sum = FOLD_U32T(sum);
tax 0:66300c77c6e9 166 sum = FOLD_U32T(sum);
tax 0:66300c77c6e9 167
tax 0:66300c77c6e9 168 /* Swap if alignment was odd */
tax 0:66300c77c6e9 169 if (odd) {
tax 0:66300c77c6e9 170 sum = SWAP_BYTES_IN_WORD(sum);
tax 0:66300c77c6e9 171 }
tax 0:66300c77c6e9 172
tax 0:66300c77c6e9 173 return (u16_t)sum;
tax 0:66300c77c6e9 174 }
tax 0:66300c77c6e9 175 #endif
tax 0:66300c77c6e9 176
tax 0:66300c77c6e9 177 #if (LWIP_CHKSUM_ALGORITHM == 3) /* Alternative version #3 */
tax 0:66300c77c6e9 178 /**
tax 0:66300c77c6e9 179 * An optimized checksum routine. Basically, it uses loop-unrolling on
tax 0:66300c77c6e9 180 * the checksum loop, treating the head and tail bytes specially, whereas
tax 0:66300c77c6e9 181 * the inner loop acts on 8 bytes at a time.
tax 0:66300c77c6e9 182 *
tax 0:66300c77c6e9 183 * @arg start of buffer to be checksummed. May be an odd byte address.
tax 0:66300c77c6e9 184 * @len number of bytes in the buffer to be checksummed.
tax 0:66300c77c6e9 185 * @return host order (!) lwip checksum (non-inverted Internet sum)
tax 0:66300c77c6e9 186 *
tax 0:66300c77c6e9 187 * by Curt McDowell, Broadcom Corp. December 8th, 2005
tax 0:66300c77c6e9 188 */
tax 0:66300c77c6e9 189
tax 0:66300c77c6e9 190 static u16_t
tax 0:66300c77c6e9 191 lwip_standard_chksum(void *dataptr, int len)
tax 0:66300c77c6e9 192 {
tax 0:66300c77c6e9 193 u8_t *pb = (u8_t *)dataptr;
tax 0:66300c77c6e9 194 u16_t *ps, t = 0;
tax 0:66300c77c6e9 195 u32_t *pl;
tax 0:66300c77c6e9 196 u32_t sum = 0, tmp;
tax 0:66300c77c6e9 197 /* starts at odd byte address? */
tax 0:66300c77c6e9 198 int odd = ((mem_ptr_t)pb & 1);
tax 0:66300c77c6e9 199
tax 0:66300c77c6e9 200 if (odd && len > 0) {
tax 0:66300c77c6e9 201 ((u8_t *)&t)[1] = *pb++;
tax 0:66300c77c6e9 202 len--;
tax 0:66300c77c6e9 203 }
tax 0:66300c77c6e9 204
tax 0:66300c77c6e9 205 ps = (u16_t *)pb;
tax 0:66300c77c6e9 206
tax 0:66300c77c6e9 207 if (((mem_ptr_t)ps & 3) && len > 1) {
tax 0:66300c77c6e9 208 sum += *ps++;
tax 0:66300c77c6e9 209 len -= 2;
tax 0:66300c77c6e9 210 }
tax 0:66300c77c6e9 211
tax 0:66300c77c6e9 212 pl = (u32_t *)ps;
tax 0:66300c77c6e9 213
tax 0:66300c77c6e9 214 while (len > 7) {
tax 0:66300c77c6e9 215 tmp = sum + *pl++; /* ping */
tax 0:66300c77c6e9 216 if (tmp < sum) {
tax 0:66300c77c6e9 217 tmp++; /* add back carry */
tax 0:66300c77c6e9 218 }
tax 0:66300c77c6e9 219
tax 0:66300c77c6e9 220 sum = tmp + *pl++; /* pong */
tax 0:66300c77c6e9 221 if (sum < tmp) {
tax 0:66300c77c6e9 222 sum++; /* add back carry */
tax 0:66300c77c6e9 223 }
tax 0:66300c77c6e9 224
tax 0:66300c77c6e9 225 len -= 8;
tax 0:66300c77c6e9 226 }
tax 0:66300c77c6e9 227
tax 0:66300c77c6e9 228 /* make room in upper bits */
tax 0:66300c77c6e9 229 sum = FOLD_U32T(sum);
tax 0:66300c77c6e9 230
tax 0:66300c77c6e9 231 ps = (u16_t *)pl;
tax 0:66300c77c6e9 232
tax 0:66300c77c6e9 233 /* 16-bit aligned word remaining? */
tax 0:66300c77c6e9 234 while (len > 1) {
tax 0:66300c77c6e9 235 sum += *ps++;
tax 0:66300c77c6e9 236 len -= 2;
tax 0:66300c77c6e9 237 }
tax 0:66300c77c6e9 238
tax 0:66300c77c6e9 239 /* dangling tail byte remaining? */
tax 0:66300c77c6e9 240 if (len > 0) { /* include odd byte */
tax 0:66300c77c6e9 241 ((u8_t *)&t)[0] = *(u8_t *)ps;
tax 0:66300c77c6e9 242 }
tax 0:66300c77c6e9 243
tax 0:66300c77c6e9 244 sum += t; /* add end bytes */
tax 0:66300c77c6e9 245
tax 0:66300c77c6e9 246 /* Fold 32-bit sum to 16 bits
tax 0:66300c77c6e9 247 calling this twice is propably faster than if statements... */
tax 0:66300c77c6e9 248 sum = FOLD_U32T(sum);
tax 0:66300c77c6e9 249 sum = FOLD_U32T(sum);
tax 0:66300c77c6e9 250
tax 0:66300c77c6e9 251 if (odd) {
tax 0:66300c77c6e9 252 sum = SWAP_BYTES_IN_WORD(sum);
tax 0:66300c77c6e9 253 }
tax 0:66300c77c6e9 254
tax 0:66300c77c6e9 255 return (u16_t)sum;
tax 0:66300c77c6e9 256 }
tax 0:66300c77c6e9 257 #endif
tax 0:66300c77c6e9 258
tax 0:66300c77c6e9 259 /* inet_chksum_pseudo:
tax 0:66300c77c6e9 260 *
tax 0:66300c77c6e9 261 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
tax 0:66300c77c6e9 262 * IP addresses are expected to be in network byte order.
tax 0:66300c77c6e9 263 *
tax 0:66300c77c6e9 264 * @param p chain of pbufs over that a checksum should be calculated (ip data part)
tax 0:66300c77c6e9 265 * @param src source ip address (used for checksum of pseudo header)
tax 0:66300c77c6e9 266 * @param dst destination ip address (used for checksum of pseudo header)
tax 0:66300c77c6e9 267 * @param proto ip protocol (used for checksum of pseudo header)
tax 0:66300c77c6e9 268 * @param proto_len length of the ip data part (used for checksum of pseudo header)
tax 0:66300c77c6e9 269 * @return checksum (as u16_t) to be saved directly in the protocol header
tax 0:66300c77c6e9 270 */
tax 0:66300c77c6e9 271 u16_t
tax 0:66300c77c6e9 272 inet_chksum_pseudo(struct pbuf *p,
tax 0:66300c77c6e9 273 ip_addr_t *src, ip_addr_t *dest,
tax 0:66300c77c6e9 274 u8_t proto, u16_t proto_len)
tax 0:66300c77c6e9 275 {
tax 0:66300c77c6e9 276 u32_t acc;
tax 0:66300c77c6e9 277 u32_t addr;
tax 0:66300c77c6e9 278 struct pbuf *q;
tax 0:66300c77c6e9 279 u8_t swapped;
tax 0:66300c77c6e9 280
tax 0:66300c77c6e9 281 acc = 0;
tax 0:66300c77c6e9 282 swapped = 0;
tax 0:66300c77c6e9 283 /* iterate through all pbuf in chain */
tax 0:66300c77c6e9 284 for(q = p; q != NULL; q = q->next) {
tax 0:66300c77c6e9 285 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
tax 0:66300c77c6e9 286 (void *)q, (void *)q->next));
tax 0:66300c77c6e9 287 acc += LWIP_CHKSUM(q->payload, q->len);
tax 0:66300c77c6e9 288 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
tax 0:66300c77c6e9 289 /* just executing this next line is probably faster that the if statement needed
tax 0:66300c77c6e9 290 to check whether we really need to execute it, and does no harm */
tax 0:66300c77c6e9 291 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 292 if (q->len % 2 != 0) {
tax 0:66300c77c6e9 293 swapped = 1 - swapped;
tax 0:66300c77c6e9 294 acc = SWAP_BYTES_IN_WORD(acc);
tax 0:66300c77c6e9 295 }
tax 0:66300c77c6e9 296 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
tax 0:66300c77c6e9 297 }
tax 0:66300c77c6e9 298
tax 0:66300c77c6e9 299 if (swapped) {
tax 0:66300c77c6e9 300 acc = SWAP_BYTES_IN_WORD(acc);
tax 0:66300c77c6e9 301 }
tax 0:66300c77c6e9 302 addr = ip4_addr_get_u32(src);
tax 0:66300c77c6e9 303 acc += (addr & 0xffffUL);
tax 0:66300c77c6e9 304 acc += ((addr >> 16) & 0xffffUL);
tax 0:66300c77c6e9 305 addr = ip4_addr_get_u32(dest);
tax 0:66300c77c6e9 306 acc += (addr & 0xffffUL);
tax 0:66300c77c6e9 307 acc += ((addr >> 16) & 0xffffUL);
tax 0:66300c77c6e9 308 acc += (u32_t)htons((u16_t)proto);
tax 0:66300c77c6e9 309 acc += (u32_t)htons(proto_len);
tax 0:66300c77c6e9 310
tax 0:66300c77c6e9 311 /* Fold 32-bit sum to 16 bits
tax 0:66300c77c6e9 312 calling this twice is propably faster than if statements... */
tax 0:66300c77c6e9 313 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 314 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 315 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
tax 0:66300c77c6e9 316 return (u16_t)~(acc & 0xffffUL);
tax 0:66300c77c6e9 317 }
tax 0:66300c77c6e9 318
tax 0:66300c77c6e9 319 /* inet_chksum_pseudo:
tax 0:66300c77c6e9 320 *
tax 0:66300c77c6e9 321 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
tax 0:66300c77c6e9 322 * IP addresses are expected to be in network byte order.
tax 0:66300c77c6e9 323 *
tax 0:66300c77c6e9 324 * @param p chain of pbufs over that a checksum should be calculated (ip data part)
tax 0:66300c77c6e9 325 * @param src source ip address (used for checksum of pseudo header)
tax 0:66300c77c6e9 326 * @param dst destination ip address (used for checksum of pseudo header)
tax 0:66300c77c6e9 327 * @param proto ip protocol (used for checksum of pseudo header)
tax 0:66300c77c6e9 328 * @param proto_len length of the ip data part (used for checksum of pseudo header)
tax 0:66300c77c6e9 329 * @return checksum (as u16_t) to be saved directly in the protocol header
tax 0:66300c77c6e9 330 */
tax 0:66300c77c6e9 331 u16_t
tax 0:66300c77c6e9 332 inet_chksum_pseudo_partial(struct pbuf *p,
tax 0:66300c77c6e9 333 ip_addr_t *src, ip_addr_t *dest,
tax 0:66300c77c6e9 334 u8_t proto, u16_t proto_len, u16_t chksum_len)
tax 0:66300c77c6e9 335 {
tax 0:66300c77c6e9 336 u32_t acc;
tax 0:66300c77c6e9 337 u32_t addr;
tax 0:66300c77c6e9 338 struct pbuf *q;
tax 0:66300c77c6e9 339 u8_t swapped;
tax 0:66300c77c6e9 340 u16_t chklen;
tax 0:66300c77c6e9 341
tax 0:66300c77c6e9 342 acc = 0;
tax 0:66300c77c6e9 343 swapped = 0;
tax 0:66300c77c6e9 344 /* iterate through all pbuf in chain */
tax 0:66300c77c6e9 345 for(q = p; (q != NULL) && (chksum_len > 0); q = q->next) {
tax 0:66300c77c6e9 346 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
tax 0:66300c77c6e9 347 (void *)q, (void *)q->next));
tax 0:66300c77c6e9 348 chklen = q->len;
tax 0:66300c77c6e9 349 if (chklen > chksum_len) {
tax 0:66300c77c6e9 350 chklen = chksum_len;
tax 0:66300c77c6e9 351 }
tax 0:66300c77c6e9 352 acc += LWIP_CHKSUM(q->payload, chklen);
tax 0:66300c77c6e9 353 chksum_len -= chklen;
tax 0:66300c77c6e9 354 LWIP_ASSERT("delete me", chksum_len < 0x7fff);
tax 0:66300c77c6e9 355 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
tax 0:66300c77c6e9 356 /* fold the upper bit down */
tax 0:66300c77c6e9 357 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 358 if (q->len % 2 != 0) {
tax 0:66300c77c6e9 359 swapped = 1 - swapped;
tax 0:66300c77c6e9 360 acc = SWAP_BYTES_IN_WORD(acc);
tax 0:66300c77c6e9 361 }
tax 0:66300c77c6e9 362 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
tax 0:66300c77c6e9 363 }
tax 0:66300c77c6e9 364
tax 0:66300c77c6e9 365 if (swapped) {
tax 0:66300c77c6e9 366 acc = SWAP_BYTES_IN_WORD(acc);
tax 0:66300c77c6e9 367 }
tax 0:66300c77c6e9 368 addr = ip4_addr_get_u32(src);
tax 0:66300c77c6e9 369 acc += (addr & 0xffffUL);
tax 0:66300c77c6e9 370 acc += ((addr >> 16) & 0xffffUL);
tax 0:66300c77c6e9 371 addr = ip4_addr_get_u32(dest);
tax 0:66300c77c6e9 372 acc += (addr & 0xffffUL);
tax 0:66300c77c6e9 373 acc += ((addr >> 16) & 0xffffUL);
tax 0:66300c77c6e9 374 acc += (u32_t)htons((u16_t)proto);
tax 0:66300c77c6e9 375 acc += (u32_t)htons(proto_len);
tax 0:66300c77c6e9 376
tax 0:66300c77c6e9 377 /* Fold 32-bit sum to 16 bits
tax 0:66300c77c6e9 378 calling this twice is propably faster than if statements... */
tax 0:66300c77c6e9 379 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 380 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 381 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
tax 0:66300c77c6e9 382 return (u16_t)~(acc & 0xffffUL);
tax 0:66300c77c6e9 383 }
tax 0:66300c77c6e9 384
tax 0:66300c77c6e9 385 /* inet_chksum:
tax 0:66300c77c6e9 386 *
tax 0:66300c77c6e9 387 * Calculates the Internet checksum over a portion of memory. Used primarily for IP
tax 0:66300c77c6e9 388 * and ICMP.
tax 0:66300c77c6e9 389 *
tax 0:66300c77c6e9 390 * @param dataptr start of the buffer to calculate the checksum (no alignment needed)
tax 0:66300c77c6e9 391 * @param len length of the buffer to calculate the checksum
tax 0:66300c77c6e9 392 * @return checksum (as u16_t) to be saved directly in the protocol header
tax 0:66300c77c6e9 393 */
tax 0:66300c77c6e9 394
tax 0:66300c77c6e9 395 u16_t
tax 0:66300c77c6e9 396 inet_chksum(void *dataptr, u16_t len)
tax 0:66300c77c6e9 397 {
tax 0:66300c77c6e9 398 return ~LWIP_CHKSUM(dataptr, len);
tax 0:66300c77c6e9 399 }
tax 0:66300c77c6e9 400
tax 0:66300c77c6e9 401 /**
tax 0:66300c77c6e9 402 * Calculate a checksum over a chain of pbufs (without pseudo-header, much like
tax 0:66300c77c6e9 403 * inet_chksum only pbufs are used).
tax 0:66300c77c6e9 404 *
tax 0:66300c77c6e9 405 * @param p pbuf chain over that the checksum should be calculated
tax 0:66300c77c6e9 406 * @return checksum (as u16_t) to be saved directly in the protocol header
tax 0:66300c77c6e9 407 */
tax 0:66300c77c6e9 408 u16_t
tax 0:66300c77c6e9 409 inet_chksum_pbuf(struct pbuf *p)
tax 0:66300c77c6e9 410 {
tax 0:66300c77c6e9 411 u32_t acc;
tax 0:66300c77c6e9 412 struct pbuf *q;
tax 0:66300c77c6e9 413 u8_t swapped;
tax 0:66300c77c6e9 414
tax 0:66300c77c6e9 415 acc = 0;
tax 0:66300c77c6e9 416 swapped = 0;
tax 0:66300c77c6e9 417 for(q = p; q != NULL; q = q->next) {
tax 0:66300c77c6e9 418 acc += LWIP_CHKSUM(q->payload, q->len);
tax 0:66300c77c6e9 419 acc = FOLD_U32T(acc);
tax 0:66300c77c6e9 420 if (q->len % 2 != 0) {
tax 0:66300c77c6e9 421 swapped = 1 - swapped;
tax 0:66300c77c6e9 422 acc = SWAP_BYTES_IN_WORD(acc);
tax 0:66300c77c6e9 423 }
tax 0:66300c77c6e9 424 }
tax 0:66300c77c6e9 425
tax 0:66300c77c6e9 426 if (swapped) {
tax 0:66300c77c6e9 427 acc = SWAP_BYTES_IN_WORD(acc);
tax 0:66300c77c6e9 428 }
tax 0:66300c77c6e9 429 return (u16_t)~(acc & 0xffffUL);
tax 0:66300c77c6e9 430 }
tax 0:66300c77c6e9 431
tax 0:66300c77c6e9 432 /* These are some implementations for LWIP_CHKSUM_COPY, which copies data
tax 0:66300c77c6e9 433 * like MEMCPY but generates a checksum at the same time. Since this is a
tax 0:66300c77c6e9 434 * performance-sensitive function, you might want to create your own version
tax 0:66300c77c6e9 435 * in assembly targeted at your hardware by defining it in lwipopts.h:
tax 0:66300c77c6e9 436 * #define LWIP_CHKSUM_COPY(dst, src, len) your_chksum_copy(dst, src, len)
tax 0:66300c77c6e9 437 */
tax 0:66300c77c6e9 438
tax 0:66300c77c6e9 439 #if (LWIP_CHKSUM_COPY_ALGORITHM == 1) /* Version #1 */
tax 0:66300c77c6e9 440 /** Safe but slow: first call MEMCPY, then call LWIP_CHKSUM.
tax 0:66300c77c6e9 441 * For architectures with big caches, data might still be in cache when
tax 0:66300c77c6e9 442 * generating the checksum after copying.
tax 0:66300c77c6e9 443 */
tax 0:66300c77c6e9 444 u16_t
tax 0:66300c77c6e9 445 lwip_chksum_copy(void *dst, const void *src, u16_t len)
tax 0:66300c77c6e9 446 {
tax 0:66300c77c6e9 447 MEMCPY(dst, src, len);
tax 0:66300c77c6e9 448 return LWIP_CHKSUM(dst, len);
tax 0:66300c77c6e9 449 }
tax 0:66300c77c6e9 450 #endif /* (LWIP_CHKSUM_COPY_ALGORITHM == 1) */