ns-allinone-2.35/ns-2.35/emulate/tcptap.cc
unsigned short 
TCPTapAgent::in_cksum(unsigned short *addr, int len)
{
  register int sum = 0;
  u_short answer = 0;
  register u_short *w = addr;
  register int nleft = len;
  
  /*
   * Our algorithm is simple, using a 32 bit accumulator (sum), we add
   * sequential 16 bit words to it, and at the end, fold back all the
   * carry bits from the top 16 bits into the lower 16 bits.
   */
  while (nleft > 1)  {
    sum += *w++;
    nleft -= 2;
  }
  
  /* mop up an odd byte, if necessary */
  if (nleft == 1) {
    *(u_char *)(&answer) = *(u_char *)w ;
    sum += answer;
  }
  
  /* add back carry outs from top 16 bits to low 16 bits */
  sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
  sum += (sum >> 16);                     /* add carry */
  answer = ~sum;                          /* truncate to 16 bits */
  return(answer);
  
}
/*
 * Taken from the raw-sock program
 */
Taken from the raw-sock program
Taken from the raw - sock program
unsigned short 
TCPTapAgent::in_cksum(unsigned short *addr, int len)
{
  register int sum = 0;
  u_short answer = 0;
  register u_short *w = addr;
  register int nleft = len;
  while (nleft > 1)  {
    sum += *w++;
    nleft -= 2;
  }
  if (nleft == 1) {
    *(u_char *)(&answer) = *(u_char *)w ;
    sum += answer;
  }
  sum = (sum >> 16) + (sum & 0xffff);     
  sum += (sum >> 16);                     
  answer = ~sum;                          
  return(answer);
}
unsigned short TCPTapAgent :: in_cksum ( unsigned short * addr , int len ) { register int sum = 0 ; u_short answer = 0 ; register u_short * w = addr ; register int nleft = len ; while ( nleft > 1 ) { sum += * w ++ ; nleft -= 2 ; } if ( nleft == 1 ) { * ( u_char * ) ( & answer ) = * ( u_char * ) w ; sum += answer ; } sum = ( sum >> 16 ) + ( sum & 0xffff ) ; sum += ( sum >> 16 ) ; answer = ~ sum ; return ( answer ) ; }
Taken from the raw-sock program
Taken from the raw - sock program
/*
   * Our algorithm is simple, using a 32 bit accumulator (sum), we add
   * sequential 16 bit words to it, and at the end, fold back all the
   * carry bits from the top 16 bits into the lower 16 bits.
   */ /* mop up an odd byte, if necessary */ /* add back carry outs from top 16 bits to low 16 bits */ /* add hi 16 to low 16 */ /* add carry */ /* truncate to 16 bits */