?
Solved

Calculating the Tcp Checksum in C#

Posted on 2003-03-11
4
Medium Priority
?
2,974 Views
Last Modified: 2010-05-18
Im trying to calculate tcp checksums for packets. with the alg im using now their being sent with incorrect checksums (according to ethereal). Here is the func im using...
/*
tcpHeader, ipHeader, and data are all arrays of bytes
tcpHeader and ipHeader are each 20 bytes(the length of each header without options)
data's length changes depending on the quanity of data being sent
*/

private UInt16 calcTCPCheckSum()
{
tcpHeader[16]=0;//set the checksum field in the tcp header to 0
tcpHeader[17]=0;//
byte[] temp=new byte[12+tcpHeader.Length+data.Length];/*create the temp array for use when calculating the checksum*/
if(temp.Length%2!=0)//make sure the array is a multiple of 2 (b\c of the 16 bit word size)
{
     temp=new byte[13+tcpHeader.Length+data.Length];
}
temp[0]=ipHeader[12];
temp[1]=ipHeader[13];
temp[2]=ipHeader[14];
temp[3]=ipHeader[15];//copy the source ip bytes out of the ipHeader to the temp
temp[4]=ipHeader[16];
temp[5]=ipHeader[17];
temp[6]=ipHeader[18];
temp[7]=ipHeader[19];//copy the dest ip bytes out of the ip Header into the temp
temp[8]=0;//set the 0
temp[9]=ipHeader[9];//copy the protocol number out of the ip header
byte[] z=BitConverter.GetBytes((ushort)(tcpHeader.Length+data.Length));
temp[10]=z[1];
temp[11]=z[0];//copy bytes that correspond to # of bytes in the tcp header and data
Array.Copy(tcpHeader,0,temp,12,tcpHeader.Length);//copy the tcp header in
Array.Copy(data,0,temp,tcpHeader.Length+12,data.Length);//copy the data in
               
Int32 cksum = 0;
int counter = 0;
int size=temp.Length;
while ( size > 0 )
{
     UInt16 val = BitConverter.ToUInt16(temp,counter);
     cksum += Convert.ToInt32(val);
     counter += 2;
     size -= 2;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (UInt16)(~cksum);
}

0
Comment
Question by:CoronaB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
SimesA earned 240 total points
ID: 8118792
While I don't know specifically how TCP checksums should be calculated, this looks a little odd. Are you sure
    cksum = (cksum >> 16) + (cksum & 0xffff);
shouldn't be inside the loop?

So it looks like:

while ( size > 0 )
{
    UInt16 val = BitConverter.ToUInt16(temp,counter);
    cksum = (cksum >> 16) + (Convert.ToInt32(val) & 0xffff);
    counter += 2;
    size -= 2;
}

Could you post a description of the algorithm?
0
 

Author Comment

by:CoronaB
ID: 8120692
here's a link with a description of the tcp checksum alg

http://www.spacerobots.org/dennis/Headers/TCPheader.htm#TCPchecksum

the reason i do the shift at the end is to "fold" the 32 bit int on top of itself. that way you end up with a 16bit ones complement sum of the whole thing. then return the complement of that.

i figured it out and got it working, thanks for the comment though
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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

800 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