Working on a TCP implementation and have encountered the need to cast from a uint64_t to a wrapping uint32_t, but am really not sure how to account for possible overflows.
absolute seqno is the uint64_t and goes up to 2^64 - 1(max possible length) while seqno goes up to 2^32 - 1 and then goes back to 0. SYN is a uint32_t representing the starting of seqno.
I'm unsure as to how accounting for overflows must be done; my current conversion implementation is as follows:
uint32_t wrap(uint64_t n, uint32_t syn) {
if (n < std::pow(2, 32) - syn) {
return static_cast<uint32_t>(n) + syn;
}
return syn;
}
Is there a better, proper way to do this conversion to ensure resetting of seqno regardless of the value of absolute seqno ? Thanks.
LE for clarity: converting 260 to unsigned char should give me 4(when exceeding 255, the "counting" starts again from 0).