[Winpcap-users] Calculating TCP checksum after altering payload

Vasily Borovyak vbor at isd.dp.ua
Wed Feb 8 07:54:57 GMT 2006


Here is the code of checksum I'm using. Hope it'll help

inline u_short in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
register int sum = 0;
u_short answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(u_char *)(&answer) = *(u_char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}


Vidar Evenrud Seeberg wrote:
> Hello experts!
>
> I am lost in my Masters Thesis and I hope some of you gurus can hsuelp me.
> The issue is about calculating the right TCP checksum after changing payload
> of a packet.
> The function csum below calculates the checksum correctly for the (IP
> header) and the (TCP header + no payload). However it calculates an
> incorrect TCP checksum when payload is present. Can anybody help me getting
> the checksum calculated correctly?
> Here is my code:
>   

-- 
Best regards. Vasily Borovyak <vbor at isd.dp.ua>



More information about the Winpcap-users mailing list