Whisper Library
 All Classes Functions Variables Typedefs Enumerations Friends
whisper_library::TcpPacket Class Reference

Represents a tcp packet. More...

Inheritance diagram for whisper_library::TcpPacket:
Inheritance graph

Public Member Functions

 TcpPacket ()
 The empty constructor creates an empty tcp packet.
 
 TcpPacket (vector< bool > content)
 Create a packet based of a given boolean vector. More...
 
 TcpPacket (uint inSourcePort, uint inDestPort, ulong inSequenceNumber, ulong inAckNumber, bitset< 4 > inDataOffset, uint inWindowSize, vector< bool > inOptions)
 This constructor is used to fill in some of the important fields beforehand. More...
 
uint sourcePort () const
 
uint destPort () const
 
ulong sequenceNumber () const
 
ulong acknowledgeNumber () const
 
bitset< 4 > dataOffset () const
 
bitset< 3 > reserved () const
 
bitset< 9 > flags () const
 
bool nonceSumFlag () const
 
bool congestionWindowReducedFlag () const
 
bool ecnEchoFlag () const
 
bool urgentFlag () const
 
bool acknowledgementFlag () const
 
bool pushFlag () const
 
bool resetFlag () const
 
bool synchronisationFlag () const
 
bool finishFlag () const
 
uint windowSize () const
 
uint checksum () const
 
uint urgentPointer () const
 
vector< bool > options () const
 
vector< bool > packet () const
 
vector< bool > data () const
 
void setSourcePort (uint val)
 
void setDestPort (uint val)
 
void setSequenceNumber (ulong val)
 
void setAcknowledgeNumber (ulong val)
 
void setDataOffset (bitset< 4 > val)
 
void setReserved (bitset< 3 > val)
 
void setFlags (bitset< 9 > val)
 
void setNonceSumFlag (bool val)
 
void setCongestionWindowReducedFlag (bool val)
 
void setEcnEchoFlag (bool val)
 
void setUrgentFlag (bool val)
 
void setAcknowledgementFlag (bool val)
 
void setPushFlag (bool val)
 
void setResetFlag (bool val)
 
void setSynchronisationFlag (bool val)
 
void setFinishFlag (bool val)
 
void setWindowSize (uint val)
 
void setChecksum (uint val)
 
void setUrgentPointer (uint val)
 
void setOptions (vector< bool > val)
 
void setData (vector< bool > val)
 
void setData (string data)
 
void setPacket (vector< bool > val)
 
void calculateChecksum (ulong sourceIp, ulong destIp, uint reservedBits, uint protocol)
 This function is used to calculate the tcp checksum. More...
 
- Public Member Functions inherited from whisper_library::GenericPacket
 GenericPacket ()
 Creates a GenericPacket.
 
 GenericPacket (vector< bool > content)
 Creates a GenericPacket with the given content. More...
 
void setPacket (vector< bool > content)
 Sets the content of the packet. More...
 
vector< bool > packet () const
 

Private Member Functions

ulong vectorToULong (uint start, uint end, const vector< bool > &vec) const
 This function is used to convert a vector into an unsigned integer value. More...
 
template<class T >
void uIntToVector (uint start, uint end, vector< bool > &vec, T val)
 This function is used to insert an unsigned integer value into a given boolean vector. More...
 
template<class T >
vector< bool > intToBoolVector (T val, uint size)
 This function is used to convert a given integer value into a boolean vector. More...
 
vector< bool > oneComplementAdd (vector< bool > vec1, vector< bool > vec2)
 This function uses the one complement addition to add two 16 bit boolean vectors. More...
 
vector< vector< bool > > split32BitVector (vector< bool > vec)
 This function splits a 32 bit boolean vector into two 16 bit vectors. More...
 
vector< vector< bool > > splitHeaderTo16Bit ()
 This function splits the whole TCP packet into a vector of 16bit boolean vectors. More...
 
vector< bool > trimBigEndianVector (vector< bool > vec, uint size)
 This function trims a big endian boolean vector to a fixed size. More...
 

Private Attributes

vector< bool > m_header
 This vector stores the bits 0-159 of the TCP header. These are fields but the options.
 
vector< bool > m_options
 This vector stores the options field of the TCP header.
 
vector< bool > m_data
 This vector stores data carrying bits of the TCP packet.
 

Detailed Description

Represents a tcp packet.

All the different fields of the TCP packet can be accessed using getter and setter methods. To make the TCP packet valid, you must invoke calculate checksum first, after setting all the fields. Keep in mind that all information that is passed in binary format uses big endian notation.

Constructor & Destructor Documentation

whisper_library::TcpPacket::TcpPacket ( vector< bool >  content)

Create a packet based of a given boolean vector.

Parameters
contentThe content to fill the packet
whisper_library::TcpPacket::TcpPacket ( uint  inSourcePort,
uint  inDestPort,
ulong  inSequenceNumber,
ulong  inAckNumber,
bitset< 4 >  inDataOffset,
uint  inWindowSize,
vector< bool >  inOptions 
)

This constructor is used to fill in some of the important fields beforehand.

Parameters
inSourcePortThe source port as integer. Max value 2^16.
inDestPortThe destination port as integer. Max value 2^16.
inSequenceNumberThe sequence number of the packet. Max value 2^32.
inAckNumberThe acknowledgement number of the packet. Max value 2^32.
inDataOffsetThe length of the options field in bytes. Max value 2^4.
inWindowSizeThe size of the packet in bytes. Max value 2^16.
inOptionsThe options as a vector of boolean values. Max size .

Member Function Documentation

bool whisper_library::TcpPacket::acknowledgementFlag ( ) const
Returns
the acknowledgement flag
ulong whisper_library::TcpPacket::acknowledgeNumber ( ) const
Returns
the acknowledge number
void whisper_library::TcpPacket::calculateChecksum ( ulong  sourceIp,
ulong  destIp,
uint  reservedBits,
uint  protocol 
)

This function is used to calculate the tcp checksum.

To do this it needs some additional information of the IP header as parameters.

Parameters
sourceIpIP adress of the sender as integer
destIpIP adress of the reciever as integer
reservedBitsthe value of the reserved bits of the IP header as integer
protocolthe protocol used to send the data as integer value
uint whisper_library::TcpPacket::checksum ( ) const
Returns
the checksum
bool whisper_library::TcpPacket::congestionWindowReducedFlag ( ) const
Returns
the congestion window reduced flag
vector< bool > whisper_library::TcpPacket::data ( ) const
Returns
the data payload
bitset< 4 > whisper_library::TcpPacket::dataOffset ( ) const
Returns
the data offset
uint whisper_library::TcpPacket::destPort ( ) const
Returns
the destination port
bool whisper_library::TcpPacket::ecnEchoFlag ( ) const
Returns
the ecn echo flag
bool whisper_library::TcpPacket::finishFlag ( ) const
Returns
the finish flag
bitset< 9 > whisper_library::TcpPacket::flags ( ) const
Returns
the flags
template<class T >
vector< bool > whisper_library::TcpPacket::intToBoolVector ( val,
uint  size 
)
private

This function is used to convert a given integer value into a boolean vector.

Parameters
valThe value to be converted
sizelength of returned vector
Returns
A vector of booleans with a binary representation of the value
bool whisper_library::TcpPacket::nonceSumFlag ( ) const
Returns
the nonce sum flag
vector< bool > whisper_library::TcpPacket::oneComplementAdd ( vector< bool >  vec1,
vector< bool >  vec2 
)
private

This function uses the one complement addition to add two 16 bit boolean vectors.

Parameters
vec1The first 16bit boolean vector to be added.
vec2The second 16bit boolean vector to be added.
Returns
The 16bit one complement sum of the two given vectors.
vector< bool > whisper_library::TcpPacket::options ( ) const
Returns
the options
vector< bool > whisper_library::TcpPacket::packet ( ) const
Returns
the whole packet
bool whisper_library::TcpPacket::pushFlag ( ) const
Returns
the push flag
bitset< 3 > whisper_library::TcpPacket::reserved ( ) const
Returns
the reserved bits
bool whisper_library::TcpPacket::resetFlag ( ) const
Returns
the reset flag
ulong whisper_library::TcpPacket::sequenceNumber ( ) const
Returns
the sequence number
void whisper_library::TcpPacket::setAcknowledgementFlag ( bool  val)
Parameters
valIndicates, that the acknowledgement field is significant.
All packets after the initial SYN packet sent by the client should have this flag set.
void whisper_library::TcpPacket::setAcknowledgeNumber ( ulong  val)
Parameters
valThe acknowledgement number of the packet.
If the acknowledgement number is set, this is the next packet the reciever is expecting.
All packets prior the acknowledgement number have been received.
The first ACK sent by each end acknowledges the initial sequence number, but no data.
void whisper_library::TcpPacket::setChecksum ( uint  val)
Parameters
valThe 16-bit checksum field is used for error checking of the header and data.
void whisper_library::TcpPacket::setCongestionWindowReducedFlag ( bool  val)
Parameters
valThis flag is set by the sending host, to indicate that it received a TCP segment with the ECE flag set and that it responded using a congestion control mechanism. (RFC 3168)
void whisper_library::TcpPacket::setData ( vector< bool >  val)
Parameters
valSet the data of the packet.
void whisper_library::TcpPacket::setData ( string  data)
Parameters
dataSet the data of the packet.
void whisper_library::TcpPacket::setDataOffset ( bitset< 4 >  val)
Parameters
valThe size of the tcp header in 32-bit words.
The smallest size without options is 5.
The maximum size is 15.
void whisper_library::TcpPacket::setDestPort ( uint  val)
Parameters
valThe port at which the packet is received.
Max value is 2^16.
void whisper_library::TcpPacket::setEcnEchoFlag ( bool  val)
Parameters
val- If the SYN flag is set, the TCP peer is ECN capable.
  • If the SYN flag is clear, that a packet with Congestion Experienced flag in the IP header set is received during normal transmission (RFC 3168).
void whisper_library::TcpPacket::setFinishFlag ( bool  val)
Parameters
valNo more data from sender.
void whisper_library::TcpPacket::setFlags ( bitset< 9 >  val)
Parameters
valSet all the flags of the tcp header with one bitset.
Their order is: NS, CWR, ECN, URG, ACK, PSH, SYN, FIN. For more detailed information see the corresponding setters.
void whisper_library::TcpPacket::setNonceSumFlag ( bool  val)
Parameters
valECN-nounce concealment protection. (RFC 3540)
void whisper_library::TcpPacket::setOptions ( vector< bool >  val)
Parameters
valA set of options. The size has to be dividable by 32.
void whisper_library::TcpPacket::setPacket ( vector< bool >  val)
Parameters
valConvert a boolean vector into the packet object.
void whisper_library::TcpPacket::setPushFlag ( bool  val)
Parameters
valAsks to push the buffered data to the recieving application.
void whisper_library::TcpPacket::setReserved ( bitset< 3 >  val)
Parameters
valThe reserved bits of the tcp header. Keep in mind that this is, like all information in the header, in big endian notation.
void whisper_library::TcpPacket::setResetFlag ( bool  val)
Parameters
valReset the connection.
void whisper_library::TcpPacket::setSequenceNumber ( ulong  val)
Parameters
valThe sequence number of the packet.
  • If the syn flag is set this is the initial sequence number. And the actual first sequence number and acknowledgement number are this number + 1.
  • Else this is the accumulated sequence number of all the data packets based on the initial sequence number.
void whisper_library::TcpPacket::setSourcePort ( uint  val)
Parameters
valThe port that is used to send the packet.
Max value is 2^16.
void whisper_library::TcpPacket::setSynchronisationFlag ( bool  val)
Parameters
valSynchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags change the meaning based on this flag and some are only valid for when it is set and others when it´s clear.
void whisper_library::TcpPacket::setUrgentFlag ( bool  val)
Parameters
valIndicates, that the urgent pointer field is significant.
void whisper_library::TcpPacket::setUrgentPointer ( uint  val)
Parameters
valIf the URG flag is set, then this 16-bit field is an offset from the sequence number indicating the last urgent data byte.
void whisper_library::TcpPacket::setWindowSize ( uint  val)
Parameters
valThe size of the recieve window, which specifies the number of window size units (by default, bytes) (beyond the sequence number in the acknowledgement field) that the sender of this segment is currently willing to recieve.
uint whisper_library::TcpPacket::sourcePort ( ) const
Returns
the source port
vector< vector< bool > > whisper_library::TcpPacket::split32BitVector ( vector< bool >  vec)
private

This function splits a 32 bit boolean vector into two 16 bit vectors.

Parameters
vecThe vector to be split up.
Returns
A vector of two 16 bit boolean vectors.
vector< vector< bool > > whisper_library::TcpPacket::splitHeaderTo16Bit ( )
private

This function splits the whole TCP packet into a vector of 16bit boolean vectors.

Returns
The TCP packet as a vector of 16bit boolean vectors.
bool whisper_library::TcpPacket::synchronisationFlag ( ) const
Returns
the synchronisation flag
vector< bool > whisper_library::TcpPacket::trimBigEndianVector ( vector< bool >  vec,
uint  size 
)
private

This function trims a big endian boolean vector to a fixed size.

Parameters
vecThe vector to be trimmed.
sizeThe new size of the vector.
Returns
The vector trimmed to the given size.
template<class T >
void whisper_library::TcpPacket::uIntToVector ( uint  start,
uint  end,
vector< bool > &  vec,
val 
)
private

This function is used to insert an unsigned integer value into a given boolean vector.

Parameters
startThe index where the first bit will be inserted
endThe index where the last bit will be inserted
vecThe vector the value will be inserted in
valThe value to be inserted
bool whisper_library::TcpPacket::urgentFlag ( ) const
Returns
the urgent flag
uint whisper_library::TcpPacket::urgentPointer ( ) const
Returns
the urgent pointer
ulong whisper_library::TcpPacket::vectorToULong ( uint  start,
uint  end,
const vector< bool > &  vec 
) const
private

This function is used to convert a vector into an unsigned integer value.

Parameters
startthe starting bit of the value in the given vector
endthe last bit of the value in the given vector
vecthe vector the value is stored in
Returns
the value of the bits as integer
uint whisper_library::TcpPacket::windowSize ( ) const
Returns
the window size