Flow 1.0.2
Flow project: Full implementation reference.
|
An internal net_flow
sequence number identifying a piece of data.
More...
#include <seq_num.hpp>
Classes | |
class | Generator |
An object of this type generates a series of initial sequence numbers (ISN) that are meant to be sufficiently secure to protect against ISN attacks and confusion among different connections between the same endpoints occurring in series. More... | |
Public Types | |
using | seq_num_t = uint64_t |
Raw sequence number type. More... | |
using | seq_num_delta_t = int64_t |
Integer type used to express differences (distances) between Sequence_numbers. More... | |
Public Member Functions | |
Sequence_number () | |
Constructs sequence number that is zero (not a valid sequence number; less than all others). More... | |
Sequence_number (const Sequence_number &source) | |
Copy constructor. More... | |
Sequence_number & | operator= (const Sequence_number &source) |
Copy operator. More... | |
bool | valid () const |
Returns true if and only if *this != Sequence_number() (i.e., is non-zero). More... | |
seq_num_delta_t | operator- (const Sequence_number &rhs) const |
Returns the distance from *this to rhs . More... | |
Sequence_number | operator+ (seq_num_delta_t delta) const |
Returns new sequence number that is *this advanced (or reversed) by the given distance. More... | |
Sequence_number | operator- (seq_num_delta_t delta) const |
Equivalent to operator+(-delta) . More... | |
Sequence_number & | operator+= (seq_num_delta_t delta) |
Advances (or reverses) this sequence number by the given distance. More... | |
Sequence_number & | operator-= (seq_num_delta_t delta) |
Equivalent to: operator+=(-delta) . More... | |
bool | operator== (const Sequence_number &rhs) const |
Whether *this is the same sequence number as rhs . More... | |
bool | operator!= (const Sequence_number &rhs) const |
Return !(*this == rhs) . More... | |
bool | operator< (const Sequence_number &rhs) const |
Whether *this is less than rhs . More... | |
bool | operator> (const Sequence_number &rhs) const |
Return rhs < *this . More... | |
bool | operator<= (const Sequence_number &rhs) const |
Return !(*this > rhs) . More... | |
bool | operator>= (const Sequence_number &rhs) const |
Return rhs <= *this . More... | |
size_t | hash () const |
Hash value of this Sequence_number for unordered_*<> . More... | |
const seq_num_t & | raw_num_ref () const |
Provides the raw sequence number. More... | |
void | set_raw_num (seq_num_t num) |
Sets the raw sequence number. More... | |
void | set_metadata (char num_line_id=0, const Sequence_number &zero_point=Sequence_number(), seq_num_delta_t multiple_size=0) |
Updates the full set of metadata (used at least for convenient convention-based logging but not actual algorihtms) for this number. More... | |
Private Attributes | |
seq_num_t | m_num |
The raw sequence number. This is the only datum used in algorithms. The others are only for logging and similar. More... | |
char | m_num_line_id |
Identifies the owner number line; char(0) = unknown/none. See set_metadata(). More... | |
seq_num_t | m_zero_point_num |
Value for m_num such that the magnitude is zero. See set_metadata(). More... | |
seq_num_delta_t | m_multiple_size |
Expected size of a full contiguous "block" of these numbers; 0 = unknown/blocks not expected. See set_metadata(). More... | |
Friends | |
std::ostream & | operator<< (std::ostream &os, const Sequence_number &seq_num) |
Prints given sequence number to given ostream . More... | |
Related Functions | |
(Note that these are not member functions.) | |
size_t | hash_value (const Sequence_number &seq_num) |
Free function that returns seq_num.hash() ; has to be a free function named hash_value for boost.hash to pick it up. More... | |
An internal net_flow
sequence number identifying a piece of data.
While it would not be hard to just deal with raw sequence numbers everywhere, it seemed prudent to wrap the raw number in a light-weight object in order to make the interface abstract and be able to change the implementation later. For example, we may change the size of the number and how wrapping works (and even whether there is wrapping), at which point we'd rather only change this class's internals as opposed to code all over the place. For example, operator+() may change from not worrying about wrapping to worrying about it (and similarly for operator-()).
The companion nested class Generator is used to generate the initial sequence number (ISN) for a connection.
What does one Sequence_number represent? A byte or a packet? This class is intentionally ignorant of this. It just deals with a number on the number line. The user of the class is responsible for such matters.
Sequence numbers being used for a given purpose always have certain features to make them useful. The following is a (n arguably overfly formal and detailed) discussion of those features and how we support them via "metadata."
N
, +x(N + 1)
) (where N
>= 0); and every single contiguous-packet range of I = 1 or more packets will thus have form [+xN
, +x(N + I)
). Of course, B = max-block-size.Sequence_number has optional support for the above conventions. While m_num stores the basic absolute value S, we also store the following, called metadata:
ostream
-serialization – since raw number is unsigned, and no non-zero numbers less than 0 exist.ostream
-serialization will avoid mentioning multiples/remainders.char(0)
= default, "none chosen".ostream
-serialization will simply feature no prefix.Use set_metadata() to set the full set of metadata. For maximum effectiveness, consider these tips – especially vis-a-vis DATA/ACK-pertinent sequence numbers.
operator=()
, copy constructor), including many implicit invocations thereof (operator+()
, associative storage, etc.), will always copy the metadata too. Be sure to rely on this strongly! If you set an ISN with its Z = ISN + 1 and B = socket's max-block-size; and then all other Sequence_number objects are copies (direct or otherwise) thereof, then they should have the proper metadata and will be beautifully logged as a result. So just set the induction base and let the machinery work.Equally thread-safe as built-in types (not safe to read/write or write/write one object simultaneously).
Generator and Sequence_number work based on the same assumptions (like the structure of the sequence number space), and thus their implementations may be related. So if one is changed the other may also need to be changed (internally).
Definition at line 125 of file seq_num.hpp.
using flow::net_flow::Sequence_number::seq_num_delta_t = int64_t |
Integer type used to express differences (distances) between Sequence_numbers.
double
or a big-integer type from boost.multiprecision? Definition at line 150 of file seq_num.hpp.
using flow::net_flow::Sequence_number::seq_num_t = uint64_t |
Raw sequence number type.
64 bits used because sequence number wrap-around would take many centuries at high network speeds to occur if the initial sequence number is picked to be sufficiently small. See also Generator::S_MAX_INIT_SEQ_NUM.
Definition at line 138 of file seq_num.hpp.
|
explicit |
Constructs sequence number that is zero (not a valid sequence number; less than all others).
Definition at line 96 of file seq_num.cpp.
Referenced by operator+().
|
default |
Copy constructor.
Identical to calling: operator=(source);
.
Implementation detail: Default implementation used. This is basically here for documentation purposes.
source | Source object. |
size_t flow::net_flow::Sequence_number::hash | ( | ) | const |
Hash value of this Sequence_number for unordered_*<>
.
Definition at line 252 of file seq_num.cpp.
References hash_value(), flow::net_flow::hash_value(), and m_num.
Referenced by hash_value().
bool flow::net_flow::Sequence_number::operator!= | ( | const Sequence_number & | rhs | ) | const |
Return !(*this == rhs)
.
rhs | Object to compare. |
Definition at line 144 of file seq_num.cpp.
Sequence_number flow::net_flow::Sequence_number::operator+ | ( | seq_num_delta_t | delta | ) | const |
Returns new sequence number that is *this
advanced (or reversed) by the given distance.
delta | How much to advance (or reverse, if negative) the result. |
Definition at line 129 of file seq_num.cpp.
References Sequence_number().
Referenced by operator-().
Sequence_number & flow::net_flow::Sequence_number::operator+= | ( | seq_num_delta_t | delta | ) |
Advances (or reverses) this sequence number by the given distance.
delta | How much to advance (or reverse, if negative) *this . |
*this
. Definition at line 118 of file seq_num.cpp.
References m_num.
Referenced by operator-=().
Sequence_number::seq_num_delta_t flow::net_flow::Sequence_number::operator- | ( | const Sequence_number & | rhs | ) | const |
Returns the distance from *this
to rhs
.
This may be negative, meaning rhs > *this
.
rhs | Object to compare. |
Definition at line 111 of file seq_num.cpp.
References m_num.
Sequence_number flow::net_flow::Sequence_number::operator- | ( | seq_num_delta_t | delta | ) | const |
Equivalent to operator+(-delta)
.
delta | How much to reverse (or advance, if negative) the result. |
Definition at line 134 of file seq_num.cpp.
References operator+().
Sequence_number & flow::net_flow::Sequence_number::operator-= | ( | seq_num_delta_t | delta | ) |
Equivalent to: operator+=(-delta)
.
delta | How much to reverse (or advance, if negative) *this. |
Definition at line 124 of file seq_num.cpp.
References operator+=().
bool flow::net_flow::Sequence_number::operator< | ( | const Sequence_number & | rhs | ) | const |
Whether *this
is less than rhs
.
rhs | Object to compare. |
Definition at line 149 of file seq_num.cpp.
References m_num.
bool flow::net_flow::Sequence_number::operator<= | ( | const Sequence_number & | rhs | ) | const |
Return !(*this > rhs)
.
rhs | Object to compare. |
Definition at line 164 of file seq_num.cpp.
|
default |
Copy operator.
Copies both the data (the essential absolute value) and all the metadata.
Implementation detail: Default implementation used. This is basically here for documentation purposes.
source | Source object. *this is allowed but pointless. |
*this
. bool flow::net_flow::Sequence_number::operator== | ( | const Sequence_number & | rhs | ) | const |
Whether *this
is the same sequence number as rhs
.
rhs | Object to compare. |
Definition at line 139 of file seq_num.cpp.
References m_num.
bool flow::net_flow::Sequence_number::operator> | ( | const Sequence_number & | rhs | ) | const |
Return rhs < *this
.
rhs | Object to compare. |
Definition at line 154 of file seq_num.cpp.
bool flow::net_flow::Sequence_number::operator>= | ( | const Sequence_number & | rhs | ) | const |
Return rhs <= *this
.
rhs | Object to compare. |
Definition at line 159 of file seq_num.cpp.
const Sequence_number::seq_num_t & flow::net_flow::Sequence_number::raw_num_ref | ( | ) | const |
Provides the raw sequence number.
This method is primarily intended for serialization.
Definition at line 258 of file seq_num.cpp.
References m_num.
void flow::net_flow::Sequence_number::set_metadata | ( | char | num_line_id = 0 , |
const Sequence_number & | zero_point = Sequence_number() , |
||
seq_num_delta_t | multiple_size = 0 |
||
) |
Updates the full set of metadata (used at least for convenient convention-based logging but not actual algorihtms) for this number.
See class Sequence_number doc header for important tips and even more detail before using this surprisingly powerful utility. It's all quite intuitive, but there are various nuances one might not expect.
operator=()
, and many implicit calls thereto), so it is important to call this method with final values before the propagating begins. See class Sequence_number doc header for discussion. num_line_id | A 1-character descriptive identifier (conventionally, an upper-case letter such as L for Local) for the number line of which this Sequence_number is a part. char(0) means "none" or "unknown" and is default at construction. |
zero_point | The Sequence_number Z such that the relative sequence number is expressed depending on this->m_num versus Z.m_num ; its sign is +, -, or = if the latter is less than, greater than, or equal to the former, respectively; its magnitude is the absolute value of their difference. 0 is legal, but see class Sequence_number for nuance(s). 0 is default at construction. |
multiple_size | The block size, or multiple size, which is a positive value given when data are generally expected to be segmented in whole multiples of this many contiguous Sequence_numbers. 0 means "data are not expected to be such multiples" or "unknown"; this is the default at construction. |
Definition at line 268 of file seq_num.cpp.
References m_multiple_size, m_num, m_num_line_id, and m_zero_point_num.
Referenced by flow::net_flow::Node::connect_worker(), flow::net_flow::Syn_packet::deserialize_type_specific_data_from_raw_data_packet(), flow::net_flow::Syn_ack_packet::deserialize_type_specific_data_from_raw_data_packet(), flow::net_flow::Data_packet::deserialize_type_specific_data_from_raw_data_packet(), flow::net_flow::Ack_packet::deserialize_type_specific_data_from_raw_data_packet(), and flow::net_flow::Node::handle_syn_to_listening_server().
void flow::net_flow::Sequence_number::set_raw_num | ( | seq_num_t | num | ) |
Sets the raw sequence number.
This method is primarily intended for deserialization.
num | The raw sequence number. |
Definition at line 263 of file seq_num.cpp.
References m_num.
Referenced by flow::net_flow::Syn_packet::deserialize_type_specific_data_from_raw_data_packet(), flow::net_flow::Syn_ack_packet::deserialize_type_specific_data_from_raw_data_packet(), flow::net_flow::Data_packet::deserialize_type_specific_data_from_raw_data_packet(), and flow::net_flow::Ack_packet::deserialize_type_specific_data_from_raw_data_packet().
bool flow::net_flow::Sequence_number::valid | ( | ) | const |
Returns true if and only if *this != Sequence_number()
(i.e., is non-zero).
Definition at line 106 of file seq_num.cpp.
References m_num.
|
related |
Free function that returns seq_num.hash()
; has to be a free function named hash_value
for boost.hash to pick it up.
seq_num | Object to hash. |
seq_num.hash()
. Definition at line 275 of file seq_num.cpp.
References hash().
Referenced by hash().
|
friend |
Prints given sequence number to given ostream
.
set_metadata()'s effects are felt here; see Sequence_number class doc header for details.
os | Stream to which to print. |
seq_num | Object to serialize. |
os
. Definition at line 169 of file seq_num.cpp.
|
private |
Expected size of a full contiguous "block" of these numbers; 0 = unknown/blocks not expected. See set_metadata().
Definition at line 356 of file seq_num.hpp.
Referenced by set_metadata().
|
private |
The raw sequence number. This is the only datum used in algorithms. The others are only for logging and similar.
Definition at line 347 of file seq_num.hpp.
Referenced by hash(), operator+=(), operator-(), operator<(), operator==(), raw_num_ref(), set_metadata(), set_raw_num(), and valid().
|
private |
Identifies the owner number line; char(0)
= unknown/none. See set_metadata().
Definition at line 350 of file seq_num.hpp.
Referenced by set_metadata().
|
private |
Value for m_num
such that the magnitude is zero. See set_metadata().
Definition at line 353 of file seq_num.hpp.
Referenced by set_metadata().