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: ipcsum.c,v $ 00081 * Revision 1.6 2008/04/18 13:32:00 haraldkipp 00082 * Changed size parameter from u_short to int, which is easier to handle 00083 * for 32-bit targets. You need to recompile your ARM code. No impact on 00084 * AVR expected 00085 * I changed u_int to int at some places to avoid some warnings during 00086 * compilation of Nut/Net. 00087 * libs. 00088 * 00089 * Revision 1.5 2006/10/05 17:25:41 haraldkipp 00090 * Avoid possible alignment errors. Fixes bug #1567748. 00091 * 00092 * Revision 1.4 2005/04/05 17:39:56 haraldkipp 00093 * Replaced all this awful crap by a simple generic routine. 00094 * 00095 * Revision 1.3 2004/04/07 12:13:58 haraldkipp 00096 * Matthias Ringwald's *nix emulation added 00097 * 00098 * Revision 1.2 2004/03/16 16:48:45 haraldkipp 00099 * Added Jan Dubiec's H8/300 port. 00100 * 00101 * Revision 1.1.1.1 2003/05/09 14:41:32 haraldkipp 00102 * Initial using 3.2.1 00103 * 00104 * Revision 1.15 2003/03/31 14:53:27 harald 00105 * Prepare release 3.1 00106 * 00107 * Revision 1.14 2003/02/04 18:14:57 harald 00108 * Version 3 released 00109 * 00110 * Revision 1.13 2002/10/29 15:30:26 harald 00111 * Temporarly switched back from asm to C 00112 * 00113 * Revision 1.12 2002/09/15 16:48:34 harald 00114 * *** empty log message *** 00115 * 00116 * Revision 1.11 2002/08/11 12:39:34 harald 00117 * ICC Assembler version by Klaus Ummenhofer 00118 * 00119 * Revision 1.8 2002/06/26 17:29:36 harald 00120 * First pre-release with 2.4 stack 00121 * 00122 * Revision 1.10 2002/08/08 17:29:28 harald 00123 * Imagecraft support by Klaus Ummenhofer 00124 * 00125 */ 00126 00127 #include <netinet/ipcsum.h> 00128 00137 00138 00150 u_short NutIpChkSumPartial(u_short ics, CONST void *buf, int len) 00151 { 00152 register u_long sum = ics; 00153 register u_char *cp = (u_char *) buf; 00154 00155 /* Sum up 16 bit values. */ 00156 while (len > 1) { 00157 #ifdef __BIG_ENDIAN__ 00158 sum += ((u_short)*cp << 8) | *(cp + 1); 00159 #else 00160 sum += ((u_short)*(cp + 1) << 8) | *cp; 00161 #endif 00162 cp += 2; 00163 len -= 2; 00164 } 00165 00166 /* Add remaining byte on odd lengths. */ 00167 if (len) { 00168 #ifdef __BIG_ENDIAN__ 00169 sum += (u_short)*cp << 8; 00170 #else 00171 sum += *cp; 00172 #endif 00173 } 00174 00175 /* Fold upper 16 bits to lower ones. */ 00176 while (sum >> 16) { 00177 sum = (u_short)sum + (sum >> 16); 00178 } 00179 return (u_short) sum; 00180 } 00181 00182 00183 00190 u_short NutIpChkSum(u_short ics, CONST void *buf, int len) 00191 { 00192 return ~NutIpChkSumPartial(ics, buf, len); 00193 } 00194 00195 /* 00196 * Details of the pseudo header used as part of the 00197 * calculation of UDP and TCP header checksums. 00198 */ 00199 struct __attribute__ ((packed)) pseudo_hdr { 00200 u_long ph_src_addr; 00201 u_long ph_dest_addr; 00202 u_char ph_zero; 00203 u_char ph_protocol; 00204 u_short ph_len; 00205 }; 00206 00211 u_long NutIpPseudoChkSumPartial(u_long src_addr, u_long dest_addr, u_char protocol, int len) 00212 { 00213 struct pseudo_hdr ph; 00214 00215 ph.ph_src_addr = src_addr; 00216 ph.ph_dest_addr = dest_addr; 00217 ph.ph_zero = 0; 00218 ph.ph_protocol = protocol; 00219 ph.ph_len = len; 00220 00221 return NutIpChkSumPartial(0, &ph, sizeof(struct pseudo_hdr)); 00222 } 00223