Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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