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
7
Medium Priority
?
495 Views
Last Modified: 2010-04-15
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
Comment
Question by:pm0615
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
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

by:gj62
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

by:pm0615
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:jcaldwel
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
in_cksum(u_short *addr, int len)
{
        register int nleft = len;
        register u_short *w = addr;
        register int sum = 0;
        u_short answer = 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 ;
                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);
}
0
 
LVL 1

Expert Comment

by:jcaldwel
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
in_cksum(u_short *addr, int len)
{
        register int nleft = len;
        register u_short *w = addr;
        register int sum = 0;
        u_short answer = 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 ;
                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);
}
0
 
LVL 1

Expert Comment

by:jcaldwel
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.
 * All rights reserved.
 */
0
 

Author Comment

by:pm0615
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question