C Programming TCP Checksum

后端 未结 4 440
不知归路
不知归路 2020-12-10 17:12

I have been having trouble doing the checksum for TCP for several days now. I have looked at many sources on the Internet but none of the examples that I have seen show you

4条回答
  •  猫巷女王i
    2020-12-10 17:43

    I found a fairly good example on the winpcap-users mailing list which should address Greg's comment about odd length data and give you something to compare your code against.

    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);
    }
    

提交回复
热议问题