C Programming TCP Checksum

后端 未结 4 434
不知归路
不知归路 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条回答
  • 2020-12-10 17:23

    I see a couple of things:

    • You are not accounting for odd length data by padding with zero.
    • You will need to account for network byte order when reading each word from the packet.
    • Your use of htonl(0x0000ffff) seems suspicious. Why are you converting a constant to network byte order to combine it with data in host byte order?
    0 讨论(0)
  • 2020-12-10 17:24

    RFC 793 says "If a segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right with zeros to form a 16 bit word for checksum purposes." Your code above does not handle that case. I think the loop conditional should be i > 1 and then check for i == 1 outside the loop and do the special handling for the last octet.

    0 讨论(0)
  • 2020-12-10 17:39

    I too struggled to find c++/c code that computes it, until I found How to Calculate IP/TCP/UDP Checksum–Part 2 Implementation – roman10, and it worked! Tested it with Wireshark's validation.

    UPDATE

    Link broke meanwhile, recovered it and put it as a gist in my account - How to Calculate IP/TCP/UDP Checksum

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题