Solved

TCP checksum in delphi

Posted on 2008-09-30
1
1,226 Views
Last Modified: 2012-06-27
Can anyone tell me if there is a Delphi version of this routine out there to calculate the TCP checksum

/*
**************************************************************************
Function: tcp_sum_calc()
**************************************************************************
Description:
      Calculate TCP checksum
***************************************************************************
*/

typedef unsigned short u16;
typedef unsigned long u32;

u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], BOOL padding, u16 buff[])
{
u16 prot_tcp=6;
u16 padd=0;
u16 word16;
u32 sum;      
      
      // Find out if the length of data is even or odd number. If odd,
      // add a padding byte = 0 at the end of packet
      if (padding&1==1){
            padd=1;
            buff[len_tcp]=0;
      }
      
      //initialize sum to zero
      sum=0;
      
      // make 16 bit words out of every two adjacent 8 bit words and
      // calculate the sum of all 16 vit words
      for (i=0;i<len_tcp+padd;i=i+2){
            word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
            sum = sum + (unsigned long)word16;
      }      
      // add the TCP pseudo header which contains:
      // the IP source and destinationn addresses,
      for (i=0;i<4;i=i+2){
            word16 =((src_addr[i]<<8)&0xFF00)+(src_addr[i+1]&0xFF);
            sum=sum+word16;      
      }
      for (i=0;i<4;i=i+2){
            word16 =((dest_addr[i]<<8)&0xFF00)+(dest_addr[i+1]&0xFF);
            sum=sum+word16;       
      }
      // the protocol number and the length of the TCP packet
      sum = sum + prot_tcp + len_tcp;

      // keep only the last 16 bits of the 32 bit calculated sum and add the carries
          while (sum>>16)
            sum = (sum & 0xFFFF)+(sum >> 16);
            
      // Take the one's complement of sum
      sum = ~sum;

return ((unsigned short) sum);
}

Also how do I determine the len_tcp value and how do I get the buff[].
0
Comment
Question by:rayman3411
1 Comment
 

Accepted Solution

by:
rayman3411 earned 0 total points
ID: 22619277
I have answered my own question.
// Calculate TCP checkcum

function RERFilter.RecalculateTCPChecksum(aLen: Integer;

                                          aSrcAddr, aDstAddr: Cardinal;

                                          buff: PByteArray): Word;

type

  Tbyte = array of byte;
 

var

  w16,padd: word;

  i,sum: integer;

  sIP,dIP: in_addr;
 

begin

  padd := 0;

  sum := 0;

  try

    try

      if (aLen div 2) * 2 <> aLen then begin

        padd := 1;

        buff[aLen] := 0;

      end;

      i := 0;

      while i < aLen+padd do begin

        w16 := ((buff[i] shl 8)and $FF00) + (buff[i+1]and $FF);

        sum := sum + integer(w16);

        i := i + 2;

      end;
 

      //add IP length

      sIP := in_addr(aSrcAddr);

      dIP := in_addr(aDstAddr);

      sum := sum + ntohs(sIP.S_un_w.s_w1) + ntohs(sIP.S_un_w.s_w2);

      sum := sum + ntohs(dIP.S_un_w.s_w1) + ntohs(dIP.S_un_w.s_w2);

      sum := sum + IPPROTO_TCP + word(aLen);
 

      while (sum shr 16)>0 do

        sum := (sum and $FFFF)+(sum shr 16);

      sum := not sum;

    except on E : Exception do

      MessageDlg('Error Calculating TCP Checksum.'+#10#13+

                 'Exception: '+E.Message, mtError, [mbOk], 0);

    end;

  finally

    Result := sum;

  end;

end;

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now