Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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