Nut/OS  4.10.3
API Reference
ipcsum.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  * -
00033  * Portions Copyright (c) 1983, 1993 by
00034  *  The Regents of the University of California.  All rights reserved.
00035  *
00036  * Redistribution and use in source and binary forms, with or without
00037  * modification, are permitted provided that the following conditions
00038  * are met:
00039  * 1. Redistributions of source code must retain the above copyright
00040  *    notice, this list of conditions and the following disclaimer.
00041  * 2. Redistributions in binary form must reproduce the above copyright
00042  *    notice, this list of conditions and the following disclaimer in the
00043  *    documentation and/or other materials provided with the distribution.
00044  * 3. Neither the name of the University nor the names of its contributors
00045  *    may be used to endorse or promote products derived from this software
00046  *    without specific prior written permission.
00047  *
00048  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00049  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00050  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00051  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00052  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00053  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00054  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00055  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00056  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00057  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00058  * SUCH DAMAGE.
00059  * -
00060  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00061  *
00062  * Permission to use, copy, modify, and distribute this software for any
00063  * purpose with or without fee is hereby granted, provided that the above
00064  * copyright notice and this permission notice appear in all copies, and that
00065  * the name of Digital Equipment Corporation not be used in advertising or
00066  * publicity pertaining to distribution of the document or software without
00067  * specific, written prior permission.
00068  * 
00069  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00070  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00071  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00072  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00073  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00074  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00075  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00076  * SOFTWARE.
00077  */
00078 
00079 /*
00080  * $Log$
00081  * Revision 1.7  2008/08/11 07:00:30  haraldkipp
00082  * BSD types replaced by stdint types (feature request #1282721).
00083  *
00084  * Revision 1.6  2008/04/18 13:32:00  haraldkipp
00085  * Changed size parameter from u_short to int, which is easier to handle
00086  * for 32-bit targets. You need to recompile your ARM code. No impact on
00087  * AVR expected
00088  * I changed u_int to int at some places to avoid some warnings during
00089  * compilation of Nut/Net.
00090  * libs.
00091  *
00092  * Revision 1.5  2006/10/05 17:25:41  haraldkipp
00093  * Avoid possible alignment errors. Fixes bug #1567748.
00094  *
00095  * Revision 1.4  2005/04/05 17:39:56  haraldkipp
00096  * Replaced all this awful crap by a simple generic routine.
00097  *
00098  * Revision 1.3  2004/04/07 12:13:58  haraldkipp
00099  * Matthias Ringwald's *nix emulation added
00100  *
00101  * Revision 1.2  2004/03/16 16:48:45  haraldkipp
00102  * Added Jan Dubiec's H8/300 port.
00103  *
00104  * Revision 1.1.1.1  2003/05/09 14:41:32  haraldkipp
00105  * Initial using 3.2.1
00106  *
00107  * Revision 1.15  2003/03/31 14:53:27  harald
00108  * Prepare release 3.1
00109  *
00110  * Revision 1.14  2003/02/04 18:14:57  harald
00111  * Version 3 released
00112  *
00113  * Revision 1.13  2002/10/29 15:30:26  harald
00114  * Temporarly switched back from asm to C
00115  *
00116  * Revision 1.12  2002/09/15 16:48:34  harald
00117  * *** empty log message ***
00118  *
00119  * Revision 1.11  2002/08/11 12:39:34  harald
00120  * ICC Assembler version by Klaus Ummenhofer
00121  *
00122  * Revision 1.8  2002/06/26 17:29:36  harald
00123  * First pre-release with 2.4 stack
00124  *
00125  * Revision 1.10  2002/08/08 17:29:28  harald
00126  * Imagecraft support by Klaus Ummenhofer
00127  *
00128  */
00129 
00130 #include <netinet/ipcsum.h>
00131 
00140 
00141 
00153 uint16_t NutIpChkSumPartial(uint16_t ics, CONST void *buf, int len)
00154 {
00155     register uint32_t sum = ics;
00156     register uint8_t *cp = (uint8_t *) buf;
00157 
00158     /* Sum up 16 bit values. */
00159     while (len > 1) {
00160 #ifdef __BIG_ENDIAN__
00161         sum += ((uint16_t)*cp << 8) | *(cp + 1);
00162 #else
00163         sum += ((uint16_t)*(cp + 1) << 8) | *cp;
00164 #endif
00165         cp += 2;
00166         len -= 2;
00167     }
00168 
00169     /* Add remaining byte on odd lengths. */
00170     if (len) {
00171 #ifdef __BIG_ENDIAN__
00172         sum += (uint16_t)*cp << 8;
00173 #else
00174         sum += *cp;
00175 #endif
00176     }
00177 
00178     /* Fold upper 16 bits to lower ones. */
00179     while (sum >> 16) {
00180         sum = (uint16_t)sum + (sum >> 16);
00181     }
00182     return (uint16_t) sum;
00183 }
00184 
00185 
00186 
00193 uint16_t NutIpChkSum(uint16_t ics, CONST void *buf, int len)
00194 {
00195     return ~NutIpChkSumPartial(ics, buf, len);
00196 }
00197 
00198 /*
00199  * Details of the pseudo header used as part of the
00200  * calculation of UDP and TCP header checksums.
00201  */
00202 struct __attribute__ ((packed)) pseudo_hdr {
00203     uint32_t ph_src_addr;
00204     uint32_t ph_dest_addr;
00205     uint8_t ph_zero;
00206     uint8_t ph_protocol;
00207     uint16_t ph_len;
00208 };
00209 
00214 uint32_t NutIpPseudoChkSumPartial(uint32_t src_addr, uint32_t dest_addr, uint8_t protocol, int len)
00215 {
00216     struct pseudo_hdr ph;
00217 
00218     ph.ph_src_addr = src_addr;
00219     ph.ph_dest_addr = dest_addr;
00220     ph.ph_zero = 0;
00221     ph.ph_protocol = protocol;
00222     ph.ph_len = len;
00223 
00224     return NutIpChkSumPartial(0, &ph, sizeof(struct pseudo_hdr));
00225 }
00226