Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Checksum program

Posted on 2003-03-10
Medium Priority
495 Views
Could anyone please provide me with a simple C program (or sources of any)to calculate the checksum over a string(input being a pointer to the string and the length of the string). I have to use it in another program (to manipulate IP datagrams)
Thanks

0
Question by:pm0615
• 3
• 2
• 2

LVL 6

Accepted Solution

gj62 earned 75 total points
ID: 8104632
This is about as simple a checksum as you can have - it uses a full unsigned int...  Do you want this, or a CRC?

unsigned int simpleChecksum(char *p, unsigned int strLength)
{
unsigned int i,sum = 0;
for(i=0;i<strLength;++i)
{
sum += *p++;
}
return(sum);
}
0

LVL 6

Expert Comment

ID: 8104746
This is about as simple a checksum as you can have - it uses a full unsigned int...  Do you want this, or a CRC?

unsigned int simpleChecksum(char *p, unsigned int strLength)
{
unsigned int i,sum = 0;
for(i=0;i<strLength;++i)
{
sum += *p++;
}
return(sum);
}
0

Author Comment

ID: 8105486
basically I need a checksum program over 32 bit IP headers. So I have to perform the ones complement at the end. Any help?
0

LVL 1

Expert Comment

ID: 8105507
If you mean THE TCP/IP Header Checksum Algo, here it is:

/*
* in_cksum --
*      Checksum routine for Internet Protocol family headers (C Version)
*/
static int
{
register int nleft = len;
register int sum = 0;

/*
* 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 ;
}

/* 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 */
}
0

LVL 1

Expert Comment

ID: 8105509
If you mean THE TCP/IP Header Checksum Algo, here it is:

/*
* in_cksum --
*      Checksum routine for Internet Protocol family headers (C Version)
*/
static int
{
register int nleft = len;
register int sum = 0;

/*
* 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 ;
}

/* 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 */
}
0

LVL 1

Expert Comment

ID: 8105524
Noticed you accepted the answer... I forgot to put Berkley's Copyright message on the previous post:

/*
* Copyright (c) 1989 The Regents of the University of California.
*/
0

Author Comment

ID: 8106129
ok..and then I just write  a main program accepting from the user the above arguments and call this routine?

Thanks!!

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month11 days, left to enroll