unsigned short checksumudp(PseudoHeader* pPseudoHeader, unsigned short* usBuf, unsigned int nSize)
{
unsigned long usChksum = 0;
unsigned long nPseudoHeader = sizeof(PseudoHeader);
unsigned short* usPsBuf = (unsigned short*)pPseudoHeader;
while( nPseudoHeader > 1 )
{
usChksum += *usPsBuf++;
nPseudoHeader -= sizeof(unsigned short);
}
while( nSize > 1 )
{
usChksum += *usBuf++;
nSize -= sizeof(unsigned short);
}
if( nSize )
{
usChksum += *(unsigned char*)usBuf;
}
usChksum=(usChksum>>16)+(usChksum&0xFFFF);
usChksum+=(usChksum>>16);
return (unsigned short)(~usChksum);
}
伪首:
struct PseudoHeader
{
DWORD dwSrcIP;
DWORD dwDestIP;
char cReserved;
char cProtocol;
WORD wLength;
};
wLength为udp的长度不包括伪首