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: ipcsum.c,v $
00081  * Revision 1.5  2006/10/05 17:25:41  haraldkipp
00082  * Avoid possible alignment errors. Fixes bug #1567748.
00083  *
00084  * Revision 1.4  2005/04/05 17:39:56  haraldkipp
00085  * Replaced all this awful crap by a simple generic routine.
00086  *
00087  * Revision 1.3  2004/04/07 12:13:58  haraldkipp
00088  * Matthias Ringwald's *nix emulation added
00089  *
00090  * Revision 1.2  2004/03/16 16:48:45  haraldkipp
00091  * Added Jan Dubiec's H8/300 port.
00092  *
00093  * Revision 1.1.1.1  2003/05/09 14:41:32  haraldkipp
00094  * Initial using 3.2.1
00095  *
00096  * Revision 1.15  2003/03/31 14:53:27  harald
00097  * Prepare release 3.1
00098  *
00099  * Revision 1.14  2003/02/04 18:14:57  harald
00100  * Version 3 released
00101  *
00102  * Revision 1.13  2002/10/29 15:30:26  harald
00103  * Temporarly switched back from asm to C
00104  *
00105  * Revision 1.12  2002/09/15 16:48:34  harald
00106  * *** empty log message ***
00107  *
00108  * Revision 1.11  2002/08/11 12:39:34  harald
00109  * ICC Assembler version by Klaus Ummenhofer
00110  *
00111  * Revision 1.8  2002/06/26 17:29:36  harald
00112  * First pre-release with 2.4 stack
00113  *
00114  * Revision 1.10  2002/08/08 17:29:28  harald
00115  * Imagecraft support by Klaus Ummenhofer
00116  *
00117  */
00118 
00119 #include <netinet/ipcsum.h>
00120 
00129 
00130 
00142 u_short NutIpChkSumPartial(u_short ics, CONST void *buf, size_t len)
00143 {
00144     register u_long sum = ics;
00145     register u_char *cp = (u_char *) buf;
00146 
00147     /* Sum up 16 bit values. */
00148     while (len > 1) {
00149 #ifdef __BIG_ENDIAN__
00150         sum += ((u_short)*cp << 8) | *(cp + 1);
00151 #else
00152         sum += ((u_short)*(cp + 1) << 8) | *cp;
00153 #endif
00154         cp += 2;
00155         len -= 2;
00156     }
00157 
00158     /* Add remaining byte on odd lengths. */
00159     if (len) {
00160 #ifdef __BIG_ENDIAN__
00161         sum += (u_short)*cp << 8;
00162 #else
00163         sum += *cp;
00164 #endif
00165     }
00166 
00167     /* Fold upper 16 bits to lower ones. */
00168     while (sum >> 16) {
00169         sum = (u_short)sum + (sum >> 16);
00170     }
00171     return (u_short) sum;
00172 }
00173 
00174 
00175 
00182 u_short NutIpChkSum(u_short ics, CONST void *buf, size_t len)
00183 {
00184     return ~NutIpChkSumPartial(ics, buf, len);
00185 }
00186 
00187 /*
00188  * Details of the pseudo header used as part of the
00189  * calculation of UDP and TCP header checksums.
00190  */
00191 struct __attribute__ ((packed)) pseudo_hdr {
00192     u_long ph_src_addr;
00193     u_long ph_dest_addr;
00194     u_char ph_zero;
00195     u_char ph_protocol;
00196     u_short ph_len;
00197 };
00198 
00203 u_long NutIpPseudoChkSumPartial(u_long src_addr, u_long dest_addr, u_char protocol, u_short len)
00204 {
00205     struct pseudo_hdr ph;
00206 
00207     ph.ph_src_addr = src_addr;
00208     ph.ph_dest_addr = dest_addr;
00209     ph.ph_zero = 0;
00210     ph.ph_protocol = protocol;
00211     ph.ph_len = len;
00212 
00213     return NutIpChkSumPartial(0, &ph, sizeof(struct pseudo_hdr));
00214 }
00215 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/