Solved

Calculating the Tcp Checksum in C#

Posted on 2003-03-11
Medium Priority
3,050 Views
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...
/*
data's length changes depending on the quanity of data being sent
*/

private UInt16 calcTCPCheckSum()
{
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[3]=ipHeader[15];//copy the source ip bytes out of the ipHeader to the temp
temp[7]=ipHeader[19];//copy the dest ip bytes out of the ip Header into the temp
temp[8]=0;//set the 0
temp[10]=z[1];
temp[11]=z[0];//copy bytes that correspond to # of bytes in the tcp header and data

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

LVL 1

Accepted Solution

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

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

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

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll