Solved

# TCP checksum in delphi

Posted on 2008-09-30
1,241 Views
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 prot_tcp=6;
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
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
word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
sum = sum + (unsigned long)word16;
}
// the IP source and destinationn addresses,
for (i=0;i<4;i=i+2){
sum=sum+word16;
}
for (i=0;i<4;i=i+2){
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
Question by:rayman3411
[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
1 Comment

Accepted Solution

rayman3411 earned 0 total points
ID: 22619277
I have answered my own question.
``````// Calculate TCP checkcum
function RERFilter.RecalculateTCPChecksum(aLen: Integer;
buff: PByteArray): Word;
type
Tbyte = array of byte;

var
i,sum: integer;

begin
sum := 0;
try
try
if (aLen div 2) * 2 <> aLen then begin
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;

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;
``````
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

SSL-VPN to IPSEC VPN Tunnel End Point 13 169
How do i get my L2TP/IPsec VPN to transmit traffic? 10 167
ASA to pfsense IPSec site to site tunnel 17 134
eigrp in site-to-site vpn 4 96
How to set-up an On Demand, IPSec, Site to SIte, VPN from a Draytek Vigor Router to a Cyberoam UTM Appliance. A concise guide to the settings required on both devices
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
###### Suggested Courses
Course of the Month2 days, 20 hours left to enroll