## modules/up/src/Core/network/Headers.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. cksum
2. cksum
3. cksum

       1 //
       2 // $Id: Headers.cc,v 1.1.1.1 2000/03/10 16:32:19 engin Exp $
       3 //
       4 // Author(s): Ramesh Govindan
       5
       6 #ifdef HAVE_CONFIG_H
       7 #include <config.h>
       8 #endif
       9
      10 #include <netinet/in.h>
      11 #include "util/Types.hh"
      12 #include "util/Trail.hh"
      13 #include "network/Headers.hh"
      14
      15 U16
      16 ICMP::cksum(int size)
         /* [<][>][^][v][top][bottom][index][help] */
      17 {
      18     unsigned short* addr = (unsigned short*) this;
      19     int len = size;
      20     int nleft = len;
      21     u_short *w = addr;
      22     u_short answer;
      23     u_short odd_byte = 0;
      24     register int sum = 0;
      25
      26     ASSERT((size % 2) == 0);    // For now, we assume even length
      27
      28     /*
      29      *  Our algorithm is simple, using a 32 bit accumulator (sum),
      30      *  we add sequential 16 bit words to it, and at the end, fold
      31      *  back all the carry bits from the top 16 bits into the lower
      32      *  16 bits.
      33      */
      34     while( nleft > 1 )  {
      35         sum += *w++;
      36         nleft -= 2;
      37     }
      38
      39     /* mop up an odd byte, if necessary */
      40     if( nleft == 1 ) {
      41         *(u_char *)(&odd_byte) = *(u_char *)w;
      42         sum += odd_byte;
      43     }
      44
      45     /*
      46      * add back carry outs from top 16 bits to low 16 bits
      47      */
      48     sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
      49     sum += (sum >> 16);                     /* add carry */
      50     answer = ~sum;                          /* truncate to 16 bits */
      51     return (answer);
      52 }
      53
      54 U16
      55 IP::cksum()
         /* [<][>][^][v][top][bottom][index][help] */
      56 {
      57     unsigned short* addr = (unsigned short*) this;
      58     int len = sizeof(IP);
      59     int nleft = len;
      60     u_short *w = addr;
      61     u_short answer;
      62     u_short odd_byte = 0;
      63     register int sum = 0;
      64
      65     /*
      66      *  Our algorithm is simple, using a 32 bit accumulator (sum),
      67      *  we add sequential 16 bit words to it, and at the end, fold
      68      *  back all the carry bits from the top 16 bits into the lower
      69      *  16 bits.
      70      */
      71     while( nleft > 1 )  {
      72         sum += *w++;
      73         nleft -= 2;
      74     }
      75
      76     /* mop up an odd byte, if necessary */
      77     if( nleft == 1 ) {
      78         *(u_char *)(&odd_byte) = *(u_char *)w;
      79         sum += odd_byte;
      80     }
      81
      82     /*
      83      * add back carry outs from top 16 bits to low 16 bits
      84      */
      85     sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
      86     sum += (sum >> 16);                     /* add carry */
      87     answer = ~sum;                          /* truncate to 16 bits */
      88     return (answer);
      89 }
      90
      91 U16
      92 IGMP::cksum(int size)
         /* [<][>][^][v][top][bottom][index][help] */
      93 {
      94     unsigned short* addr = (unsigned short*) this;
      95     int len = size;
      96     int nleft = len;
      97     u_short *w = addr;
      98     u_short answer;
      99     u_short odd_byte = 0;
     100     register int sum = 0;
     101
     102     ASSERT((size % 2) == 0);    // For now, we assume even length
     103
     104     /*
     105      *  Our algorithm is simple, using a 32 bit accumulator (sum),
     106      *  we add sequential 16 bit words to it, and at the end, fold
     107      *  back all the carry bits from the top 16 bits into the lower
     108      *  16 bits.
     109      */
     110     while( nleft > 1 )  {
     111         sum += *w++;
     112         nleft -= 2;
     113     }
     114
     115     /* mop up an odd byte, if necessary */
     116     if( nleft == 1 ) {
     117         *(u_char *)(&odd_byte) = *(u_char *)w;
     118         sum += odd_byte;
     119     }
     120
     121     /*
     122      * add back carry outs from top 16 bits to low 16 bits
     123      */
     124     sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
     125     sum += (sum >> 16);                     /* add carry */
     126     answer = ~sum;                          /* truncate to 16 bits */
     127     return (answer);
     128 }
     129
     130 //  Copyright (c) 1994 by the University of Southern California.
     131 //  All rights reserved.
     132 //
     133 //  Permission to use, copy, modify, and distribute this software and
     134 //  its documentation in source and binary forms for lawful
     135 //  non-commercial purposes and without fee is hereby granted, provided
     136 //  that the above copyright notice appear in all copies and that both
     137 //  the copyright notice and this permission notice appear in supporting
     138 //  documentation, and that any documentation, advertising materials,
     139 //  and other materials related to such distribution and use acknowledge
     140 //  that the software was developed by the University of Southern
     141 //  California and/or Information Sciences Institute.
     142 //  The name of the University of Southern California may not
     143 //  be used to endorse or promote products derived from this software
     144 //  without specific prior written permission.
     145 //
     146 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
     147 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
     148 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     149 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     150 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
     151 //  NON-INFRINGEMENT.
     152 //
     153 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
     154 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
     155 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
     156 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
     157 //
     158 //  Questions concerning this software should be directed to
     159 //  scan@isi.edu.
     160 //
     161
     162

/* [<][>][^][v][top][bottom][index][help] */