Flow 1.0.2
Flow project: Full implementation reference.
Classes | Public Types | Public Member Functions | Private Attributes | Friends | Related Functions | List of all members
flow::net_flow::Sequence_number Class Reference

An internal net_flow sequence number identifying a piece of data. More...

#include <seq_num.hpp>

Collaboration diagram for flow::net_flow::Sequence_number:
[legend]

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_numberoperator= (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_numberoperator+= (seq_num_delta_t delta)
 Advances (or reverses) this sequence number by the given distance. More...
 
Sequence_numberoperator-= (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_traw_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...
 

Detailed Description

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.

Metadata

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."

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:

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.

Thread safety

Equally thread-safe as built-in types (not safe to read/write or write/write one object simultaneously).

Implementation notes

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.

Member Typedef Documentation

◆ seq_num_delta_t

Integer type used to express differences (distances) between Sequence_numbers.

Note
This type can overflow for extremely large sequence number differences (those spanning over half of the range). This should not usually be a concern in practice.
Todo:
Allowing overflow by using this definition of seq_num_delta_t is somewhat unlike the rest of the implementation of Sequence_number which avoids overflow like the plague. Should we do something like use double or a big-integer type from boost.multiprecision?

Definition at line 150 of file seq_num.hpp.

◆ seq_num_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.

Constructor & Destructor Documentation

◆ Sequence_number() [1/2]

flow::net_flow::Sequence_number::Sequence_number ( )
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+().

Here is the caller graph for this function:

◆ Sequence_number() [2/2]

flow::net_flow::Sequence_number::Sequence_number ( const Sequence_number source)
default

Copy constructor.

Identical to calling: operator=(source);.

Implementation detail: Default implementation used. This is basically here for documentation purposes.

Parameters
sourceSource object.

Member Function Documentation

◆ hash()

size_t flow::net_flow::Sequence_number::hash ( ) const

Hash value of this Sequence_number for unordered_*<>.

Returns
Ditto.

Definition at line 252 of file seq_num.cpp.

References hash_value(), flow::net_flow::hash_value(), and m_num.

Referenced by hash_value().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator!=()

bool flow::net_flow::Sequence_number::operator!= ( const Sequence_number rhs) const

Return !(*this == rhs).

Parameters
rhsObject to compare.
Returns
See above.

Definition at line 144 of file seq_num.cpp.

◆ operator+()

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.

Parameters
deltaHow much to advance (or reverse, if negative) the result.
Returns
See above.

Definition at line 129 of file seq_num.cpp.

References Sequence_number().

Referenced by operator-().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator+=()

Sequence_number & flow::net_flow::Sequence_number::operator+= ( seq_num_delta_t  delta)

Advances (or reverses) this sequence number by the given distance.

Parameters
deltaHow much to advance (or reverse, if negative) *this.
Returns
*this.

Definition at line 118 of file seq_num.cpp.

References m_num.

Referenced by operator-=().

Here is the caller graph for this function:

◆ operator-() [1/2]

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.

Note
This will overflow if the difference is extremely large (over half of the sequence number range). This should not usually be a concern in practice... but see to-do on seq_num_delta_t.
Parameters
rhsObject to compare.
Returns
See above.

Definition at line 111 of file seq_num.cpp.

References m_num.

◆ operator-() [2/2]

Sequence_number flow::net_flow::Sequence_number::operator- ( seq_num_delta_t  delta) const

Equivalent to operator+(-delta).

Parameters
deltaHow much to reverse (or advance, if negative) the result.
Returns
See above.

Definition at line 134 of file seq_num.cpp.

References operator+().

Here is the call graph for this function:

◆ operator-=()

Sequence_number & flow::net_flow::Sequence_number::operator-= ( seq_num_delta_t  delta)

Equivalent to: operator+=(-delta).

Parameters
deltaHow much to reverse (or advance, if negative) *this.
Returns
*this.

Definition at line 124 of file seq_num.cpp.

References operator+=().

Here is the call graph for this function:

◆ operator<()

bool flow::net_flow::Sequence_number::operator< ( const Sequence_number rhs) const

Whether *this is less than rhs.

Parameters
rhsObject to compare.
Returns
See above.

Definition at line 149 of file seq_num.cpp.

References m_num.

◆ operator<=()

bool flow::net_flow::Sequence_number::operator<= ( const Sequence_number rhs) const

Return !(*this > rhs).

Parameters
rhsObject to compare.
Returns
See above.

Definition at line 164 of file seq_num.cpp.

◆ operator=()

Sequence_number & flow::net_flow::Sequence_number::operator= ( const Sequence_number source)
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.

Parameters
sourceSource object. *this is allowed but pointless.
Returns
*this.

◆ operator==()

bool flow::net_flow::Sequence_number::operator== ( const Sequence_number rhs) const

Whether *this is the same sequence number as rhs.

Parameters
rhsObject to compare.
Returns
See above.

Definition at line 139 of file seq_num.cpp.

References m_num.

◆ operator>()

bool flow::net_flow::Sequence_number::operator> ( const Sequence_number rhs) const

Return rhs < *this.

Parameters
rhsObject to compare.
Returns
See above.

Definition at line 154 of file seq_num.cpp.

◆ operator>=()

bool flow::net_flow::Sequence_number::operator>= ( const Sequence_number rhs) const

Return rhs <= *this.

Parameters
rhsObject to compare.
Returns
See above.

Definition at line 159 of file seq_num.cpp.

◆ raw_num_ref()

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.

Returns
The raw sequence number.

Definition at line 258 of file seq_num.cpp.

References m_num.

◆ set_metadata()

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.

Note
Metadata propagate via copy (copy construction, 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.
Parameters
num_line_idA 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_pointThe 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_sizeThe 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().

Here is the caller graph for this function:

◆ set_raw_num()

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.

Parameters
numThe 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().

Here is the caller graph for this function:

◆ valid()

bool flow::net_flow::Sequence_number::valid ( ) const

Returns true if and only if *this != Sequence_number() (i.e., is non-zero).

Returns
Ditto.

Definition at line 106 of file seq_num.cpp.

References m_num.

Friends And Related Function Documentation

◆ hash_value()

size_t hash_value ( const Sequence_number seq_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.

Parameters
seq_numObject to hash.
Returns
seq_num.hash().

Definition at line 275 of file seq_num.cpp.

References hash().

Referenced by hash().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const Sequence_number seq_num 
)
friend

Prints given sequence number to given ostream.

set_metadata()'s effects are felt here; see Sequence_number class doc header for details.

Parameters
osStream to which to print.
seq_numObject to serialize.
Returns
os.

Definition at line 169 of file seq_num.cpp.

Member Data Documentation

◆ m_multiple_size

seq_num_delta_t flow::net_flow::Sequence_number::m_multiple_size
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().

◆ m_num

seq_num_t flow::net_flow::Sequence_number::m_num
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().

◆ m_num_line_id

char flow::net_flow::Sequence_number::m_num_line_id
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().

◆ m_zero_point_num

seq_num_t flow::net_flow::Sequence_number::m_zero_point_num
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().


The documentation for this class was generated from the following files: