- Posts: 2
- Thank you received: 0
tcp checksum
- CoNsTrIcToR
- Topic Author
- Offline
- New Member
Less
More
20 years 4 months ago #4513
by CoNsTrIcToR
tcp checksum was created by CoNsTrIcToR
i've done some searching on how to calculate the tcp checksum, and this is what i came up with...
NOTE: i used the checksum function below to calculate the ip header checksum and it gave me correct results (i used a packet sniffer and looked at iphdr checksums and recalculated them to verify that)
// pseudo header
struct pseudohdr
{
unsigned int sourceIP;
unsigned int destIP;
unsigned char placholder;
unsigned char proto;
unsigned short tcp_len;
} pshdr;
///////////////////
// checksum function:
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
} // special thanks to the author (who i don't know) for
// providing this function
/////////rest of code///////////////
pTcpheader->th_sum = 0;
pshdr.destIP = pIpheader->destIP;
pshdr.sourceIP = pIpheader->sourceIP;
pshdr.placholder = 0;
pshdr.proto = IPPROTO_TCP;
pshdr.tcp_len = htons(ntohs(pIpheader->total_len) - sizeof(IP_HDR));
void *pphdr;
pphdr=malloc( sizeof(pseudohdr)+ntohs(pshdr.tcp_len) );
memcpy(pphdr,&pshdr,sizeof(pseudohdr));
memcpy((char *)pphdr+sizeof(pseudohdr),pTcpheader,
ntohs(pshdr.tcp_len));
printf("mytcpchksum=%hx\n",
checksum((USHORT *)pphdr,sizeof(pseudohdr)+ntohs(pshdr.tcp_len)) );
// unfortunately this gives me the wrong result
NOTE: i used the checksum function below to calculate the ip header checksum and it gave me correct results (i used a packet sniffer and looked at iphdr checksums and recalculated them to verify that)
// pseudo header
struct pseudohdr
{
unsigned int sourceIP;
unsigned int destIP;
unsigned char placholder;
unsigned char proto;
unsigned short tcp_len;
} pshdr;
///////////////////
// checksum function:
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
} // special thanks to the author (who i don't know) for
// providing this function
/////////rest of code///////////////
pTcpheader->th_sum = 0;
pshdr.destIP = pIpheader->destIP;
pshdr.sourceIP = pIpheader->sourceIP;
pshdr.placholder = 0;
pshdr.proto = IPPROTO_TCP;
pshdr.tcp_len = htons(ntohs(pIpheader->total_len) - sizeof(IP_HDR));
void *pphdr;
pphdr=malloc( sizeof(pseudohdr)+ntohs(pshdr.tcp_len) );
memcpy(pphdr,&pshdr,sizeof(pseudohdr));
memcpy((char *)pphdr+sizeof(pseudohdr),pTcpheader,
ntohs(pshdr.tcp_len));
printf("mytcpchksum=%hx\n",
checksum((USHORT *)pphdr,sizeof(pseudohdr)+ntohs(pshdr.tcp_len)) );
// unfortunately this gives me the wrong result
20 years 4 months ago #4515
by Chris
Chris Partsenidis.
Founder & Editor-in-Chief
www.Firewall.cx
Replied by Chris on topic Re: tcp checksum
Hmm.... Tfs and Sahir have programming knowledge, they should be able to help you out on this one. You could also consider posting the question to a revelant news groups.
Cheers,
Cheers,
Chris Partsenidis.
Founder & Editor-in-Chief
www.Firewall.cx
- CoNsTrIcToR
- Topic Author
- Offline
- New Member
Less
More
- Posts: 2
- Thank you received: 0
20 years 4 months ago #4522
by CoNsTrIcToR
Replied by CoNsTrIcToR on topic got it....stupid me
i'm so0o0o0o0o0o sorry for bothering you guys...
the mistake was somewhere else.
The code i posted is correct. Hope someone could make use of it.
Now i have just done my first packet crafting program in C, thanx to the full raw socket support billy has provided windows users with, we don't have to do it the hard way to craft packets.
the mistake was somewhere else.
The code i posted is correct. Hope someone could make use of it.
Now i have just done my first packet crafting program in C, thanx to the full raw socket support billy has provided windows users with, we don't have to do it the hard way to craft packets.
Time to create page: 0.118 seconds