Pantek Library
Hosting Provided By
CybrHost
High Speed Hosting

[Q] cksum of UDP packet

From: Qin An <anqin(at)anqin.com>
Date: Tue Jul 29 2003 - 23:41:29 EDT


Hi, Glad to meet you all!

I got a problem in cksum computation for UDP packet. At the end of this mail, there is a function copied from a textbook. I believe it is a universal cksum calculation function. I captured a UDP packet on the net:

0x07, 0x2c, 0x00, 0x35, 0x00, 0x0c, 0x98, 0x8e, 0x64, 0x69, 0x72, 0x0a

The 6th and 7th element is the cksum computed by the sender's kernel. My question is that when I use in_cksum to compute the cksum, it returns 0x1f22, different from 0x98, 0x8e. What's wrong here? Is there any other algorithm to compute cksum?

Thank you in advance!

Qin An


unsigned short in_cksum (unsigned short *ptr, int nbytes) {
  unsigned long sum; /* assumes long == 32 bits */   unsigned short oddbyte, answer; /* assumes short == 16 bits */

  sum = 0;
  while (nbytes > 1){
 sum += *ptr++;
 nbytes -= 2;
 }

Do you need help?X

/* mop up an odd byte, if necessary */
  if (nbytes == 1){
/* make sure top half is zero */

 oddbyte = 0;
/* one byte only */

  • ((unsigned char *) &oddbyte) = *(unsigned char *) ptr; sum += oddbyte; }

 /* add back carry outs from top 16 bits to low 16 bits */
/* add high-16 to low-16 */

  sum = (sum >> 16) + (sum & 0xffff);
/* add carry */

  sum += (sum >> 16);
/* ones-compliment, then truncate to 16 bits */
  answer = (unsigned short)(~sum);
  return answer;
}

unsigned char s[] = {
0x07, 0x2c, 0x00, 0x35, 0x00, 0x0c, 0x98, 0x8e, 0x64, 0x69, 0x72, 0x0a};

int main (int argc, char *argv[])
{
  int i;

  s[6] = s[7] = 0;

  i = in_cksum ((unsigned short *) &s[0], 12); } Received on Wed Jul 30 10:55:13 2003

This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 14:02:46 EDT


Contact Us  Legal Notices  Order Services Online 
Pantek Home  Privacy Policy  IT news  Site Map  Pantek Library