Sponge
CS144's user-space TCP library
|
The internet checksum algorithm. More...
#include <util.hh>
Public Member Functions | |
InternetChecksum (const uint32_t initial_sum=0) | |
void | add (std::string_view data) |
uint16_t | value () const |
Private Attributes | |
bool | _parity {} |
uint32_t | _sum |
InternetChecksum::InternetChecksum | ( | const uint32_t | initial_sum = 0 | ) |
This class can be used to either check or compute an Internet checksum (e.g., for an IP datagram header or a TCP segment).
The Internet checksum is defined such that evaluating inet_cksum() on a TCP segment (IP datagram, etc) containing a correct checksum header will return zero. In other words, if you read a correct TCP segment off the wire and pass it untouched to inet_cksum(), the return value will be 0.
Meanwhile, to compute the checksum for an outgoing TCP segment (IP datagram, etc.), you must first set the checksum header to zero, then call inet_cksum(), and finally set the checksum header to the return value.
For more information, see the Wikipedia page on the Internet checksum, and consult the IP and TCP RFCs.