Click here to Skip to main content
14,976,314 members

Comments by vikramlinux (Top 1 by date)

vikramlinux 6-Feb-17 10:17am View
   
Deleted
Tried below code

char src_addr[] = {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xa1, 0xe4, 0x22, 0x2c, 0x0c, 0x9b, 0x57, 0x22
};

char dest_addr[] = {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0xd7, 0xc9, 0x57, 0x52, 0xd7, 0x0f, 0xcd
};

char TCP_len[] = {
0x00, 0x20
};

char protocol[] = {
0x06
};

int tcplen = 32;

_inline unsigned short TOWORD(unsigned int a)
{
return (unsigned short)( ((a>>8)&0x00FFL) + ((a<<8)&0xFF00L) );
}


int _tmain(int argc, _TCHAR* argv[])
{
unsigned int sum=0;

for(int i = 0; i < 16; i += 2)
{
sum += src_addr[i] + (src_addr[i+1] << 8U);
}

for(int i = 0; i < 16; i += 2)
{
sum += dest_addr[i] + (dest_addr[i+1] << 8U);
}

unsigned short type = 0x06; // TCP protocol
sum += TOWORD(type);

unsigned int tcplen = 0x1c; // 28 bytes length
sum += ((tcplen <<8) & 0xFFFF) + (tcplen >> 8);

sum = (sum>>16)+(sum&0xffff);
sum += (sum>>16);

sum = (~sum & 0xFFFF);
unsigned short sum1 = sum;

printf("Checksum = %x\n", sum1);
return 0;
}

Results into 3e9c.