SAMV71 Xplained Ultra Software Package 1.3

inet6.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Functions common to all TCP/IPv6 modules, such as the Internet checksum and the
00004  * byte order functions.
00005  *
00006  */
00007 
00008 /*
00009  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00010  * All rights reserved. 
00011  * 
00012  * Redistribution and use in source and binary forms, with or without modification, 
00013  * are permitted provided that the following conditions are met:
00014  *
00015  * 1. Redistributions of source code must retain the above copyright notice,
00016  *    this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above copyright notice,
00018  *    this list of conditions and the following disclaimer in the documentation
00019  *    and/or other materials provided with the distribution.
00020  * 3. The name of the author may not be used to endorse or promote products
00021  *    derived from this software without specific prior written permission. 
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00024  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00025  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00026  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00028  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00031  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00032  * OF SUCH DAMAGE.
00033  *
00034  * This file is part of the lwIP TCP/IP stack.
00035  * 
00036  * Author: Adam Dunkels <adam@sics.se>
00037  *
00038  */
00039 
00040 #include "lwip/opt.h"
00041 
00042 #include "lwip/def.h"
00043 #include "lwip/inet.h"
00044 
00045 /* chksum:
00046  *
00047  * Sums up all 16 bit words in a memory portion. Also includes any odd byte.
00048  * This function is used by the other checksum functions.
00049  *
00050  * For now, this is not optimized. Must be optimized for the particular processor
00051  * arcitecture on which it is to run. Preferebly coded in assembler.
00052  */
00053 
00054 static u32_t
00055 chksum(void *dataptr, u16_t len)
00056 {
00057   u16_t *sdataptr = dataptr;
00058   u32_t acc;
00059   
00060   
00061   for(acc = 0; len > 1; len -= 2) {
00062     acc += *sdataptr++;
00063   }
00064 
00065   /* add up any odd byte */
00066   if (len == 1) {
00067     acc += htons((u16_t)(*(u8_t *)dataptr) << 8);
00068   }
00069 
00070   return acc;
00071 
00072 }
00073 
00074 /* inet_chksum_pseudo:
00075  *
00076  * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
00077  */
00078 
00079 u16_t
00080 inet_chksum_pseudo(struct pbuf *p,
00081        struct ip_addr *src, struct ip_addr *dest,
00082        u8_t proto, u32_t proto_len)
00083 {
00084   u32_t acc;
00085   struct pbuf *q;
00086   u8_t swapped, i;
00087 
00088   acc = 0;
00089   swapped = 0;
00090   for(q = p; q != NULL; q = q->next) {    
00091     acc += chksum(q->payload, q->len);
00092     while (acc >> 16) {
00093       acc = (acc & 0xffff) + (acc >> 16);
00094     }
00095     if (q->len % 2 != 0) {
00096       swapped = 1 - swapped;
00097       acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
00098     }
00099   }
00100 
00101   if (swapped) {
00102     acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
00103   }
00104   
00105   for(i = 0; i < 8; i++) {
00106     acc += ((u16_t *)src->addr)[i] & 0xffff;
00107     acc += ((u16_t *)dest->addr)[i] & 0xffff;
00108     while (acc >> 16) {
00109       acc = (acc & 0xffff) + (acc >> 16);
00110     }
00111   }
00112   acc += (u16_t)htons((u16_t)proto);
00113   acc += ((u16_t *)&proto_len)[0] & 0xffff;
00114   acc += ((u16_t *)&proto_len)[1] & 0xffff;
00115 
00116   while (acc >> 16) {
00117     acc = (acc & 0xffff) + (acc >> 16);
00118   }
00119   return ~(acc & 0xffff);
00120 }
00121 
00122 /* inet_chksum:
00123  *
00124  * Calculates the Internet checksum over a portion of memory. Used primarely for IP
00125  * and ICMP.
00126  */
00127 
00128 u16_t
00129 inet_chksum(void *dataptr, u16_t len)
00130 {
00131   u32_t acc, sum;
00132 
00133   acc = chksum(dataptr, len);
00134   sum = (acc & 0xffff) + (acc >> 16);
00135   sum += (sum >> 16);
00136   return ~(sum & 0xffff);
00137 }
00138 
00139 u16_t
00140 inet_chksum_pbuf(struct pbuf *p)
00141 {
00142   u32_t acc;
00143   struct pbuf *q;
00144   u8_t swapped;
00145   
00146   acc = 0;
00147   swapped = 0;
00148   for(q = p; q != NULL; q = q->next) {
00149     acc += chksum(q->payload, q->len);
00150     while (acc >> 16) {
00151       acc = (acc & 0xffff) + (acc >> 16);
00152     }    
00153     if (q->len % 2 != 0) {
00154       swapped = 1 - swapped;
00155       acc = (acc & 0xff << 8) | (acc & 0xff00 >> 8);
00156     }
00157   }
00158  
00159   if (swapped) {
00160     acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
00161   }
00162   return ~(acc & 0xffff);
00163 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines