Click to See Complete Forum and Search --> : TCP checksum adjustment due to TCP data and length change


smohanty
September 3rd, 2003, 09:51 AM
I want to pass the portion of the TCP data (i.e 10,10,10,11 will be replaced with 100,100,100,100) with older checksum to get the updated new checksum. I have a routine works fine with even number of bytes for both ole and new buffer.

Attached:
void checksumadjust(unsigned char *chksum, unsigned char *optr,
int olen, unsigned char *nptr, int nlen)
{
long x, old, new;

x=ntohs( *(unsigned short *)chksum );
if (x==0xffff) x=0;
x=~x; x &= 0xffff;
while (olen) {
old=ntohs(*(unsigned short *)optr); optr+=2;
x-=old;
if (x<=0) { x--; x&=0xffff; }
olen-=2;
}
while (nlen) {
new=ntohs(*(unsigned short *)nptr); nptr+=2;
x+=new;
if (x & 0xffff0000) { x++; x &= 0xffff; }
nlen-=2;
}
x=~x;
if (x==0) x=0xffff;
chksum[0]=(x>>8)&0xff; chksum[1]=x & 0xff;
}

It works fine if the IP address change in TCP header. Changed to handle odd length, but no success. Some help will be great...