Calculating the Tcp Checksum in C#

Posted on 2003-03-11
Medium Priority
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
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[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[9]=ipHeader[9];//copy the protocol number out of the ip header
byte[] z=BitConverter.GetBytes((ushort)(tcpHeader.Length+data.Length));
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);

Question by:CoronaB

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?

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…

579 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