When I receive a UDP packet from my OS, none of my checksums (I tried multiple variations, such as only header, header and data, only data, full packet and pseudo header) that I try to get off the received packet match.
This is really frustrating, and Windows just drops packets with invalid checksums - apparently even a checksum of 0 doesn't work...
This is what I use for UDP checksums:
Code: Select all
// calculates a UDP checksum - http://www.netfor2.com/udpsum.htm
uint16_t udpChecksum( uint16_t udplen, char* udpdat, unsigned int srcIp, unsigned int destIp )
{
// sum
uint32_t sum = 0;
// 16-bit pointer
uint16_t* ptr = (uint16_t*) udpdat;
// make 16-bit words and add them all
int i;
for( i = 0; i < udplen; i++ )
{
sum += *ptr++;
}
// add the ip stuff
sum += srcIp + destIp;
// add the protocol and udp length
sum += ( (short) 0x11 ) + udplen;
// keep last 16 bits of 32 bit calculated sum, add carries
while( sum >> 16 )
sum = ( sum & 0xFFFF ) + ( sum >> 16 );
// return the one's complement
return (uint16_t) ~sum;
}
Now this checksum code, working from RFC768, doesn't work... Packets arriving have their checksum printed, then their checksum field is changed to 0. After all this I print out my calculated checksum, which always comes out unequal... Any ideas?