Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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