NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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