libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Tins::TCP Class Reference

Represents a TCP PDU. More...

#include <tcp.h>

Inheritance diagram for Tins::TCP:
Tins::PDU

Public Types

enum  Flags {
  FIN = 1, SYN = 2, RST = 4, PSH = 8,
  ACK = 16, URG = 32, ECE = 64, CWR = 128
}
 TCP flags enum. More...
 
enum  OptionTypes {
  EOL = 0, NOP = 1, MSS = 2, WSCALE = 3,
  SACK_OK = 4, SACK = 5, TSOPT = 8, ALTCHK = 14
}
 TCP options enum. More...
 
enum  AltChecksums { CHK_TCP, CHK_8FLETCHER, CHK_16FLETCHER }
 Alternate checksum enum.
 
typedef PDUOption< uint8_t, TCPoption
 
typedef std::list< optionoptions_type
 
typedef std::vector< uint32_t > sack_type
 
- Public Types inherited from Tins::PDU
enum  endian_type { BE, LE }
 
enum  PDUType {
  RAW, ETHERNET_II, IEEE802_3, DOT3 = IEEE802_3,
  RADIOTAP, DOT11, DOT11_ACK, DOT11_ASSOC_REQ,
  DOT11_ASSOC_RESP, DOT11_AUTH, DOT11_BEACON, DOT11_BLOCK_ACK,
  DOT11_BLOCK_ACK_REQ, DOT11_CF_END, DOT11_DATA, DOT11_CONTROL,
  DOT11_DEAUTH, DOT11_DIASSOC, DOT11_END_CF_ACK, DOT11_MANAGEMENT,
  DOT11_PROBE_REQ, DOT11_PROBE_RESP, DOT11_PS_POLL, DOT11_REASSOC_REQ,
  DOT11_REASSOC_RESP, DOT11_RTS, DOT11_QOS_DATA, LLC,
  SNAP, IP, ARP, TCP,
  UDP, ICMP, BOOTP, DHCP,
  EAPOL, RC4EAPOL, RSNEAPOL, DNS,
  LOOPBACK, IPv6, ICMPv6, SLL,
  DHCPv6, DOT1Q, PPPOE, STP,
  PPI, IPSEC_AH, IPSEC_ESP, PKTAP,
  MPLS, UNKNOWN = 999, USER_DEFINED_PDU = 1000
}
 Enum which identifies each type of PDU. More...
 
typedef byte_array serialization_type
 

Public Member Functions

 TCP (uint16_t dport=0, uint16_t sport=0)
 TCP constructor. More...
 
 TCP (const uint8_t *buffer, uint32_t total_sz)
 Constructs TCP object from a buffer. More...
 
uint16_t dport () const
 Getter for the destination port field. More...
 
uint16_t sport () const
 Getter for the source port field. More...
 
uint32_t seq () const
 Getter for the sequence number field. More...
 
uint32_t ack_seq () const
 Getter for the acknowledge number field. More...
 
uint16_t window () const
 Getter for the window size field. More...
 
uint16_t checksum () const
 Getter for the checksum field. More...
 
uint16_t urg_ptr () const
 Getter for the urgent pointer field. More...
 
small_uint< 4 > data_offset () const
 Getter for the data offset field. More...
 
const options_typeoptions () const
 Getter for the option list. More...
 
small_uint< 1 > get_flag (Flags tcp_flag) const
 Gets the value of a flag. More...
 
small_uint< 12 > flags () const
 Gets the flags' values. More...
 
void dport (uint16_t new_dport)
 Setter for the destination port field. More...
 
void sport (uint16_t new_sport)
 Setter for the source port field. More...
 
void seq (uint32_t new_seq)
 Setter for the sequence number. More...
 
void ack_seq (uint32_t new_ack_seq)
 Setter for the acknowledge number. More...
 
void window (uint16_t new_window)
 Setter for the window size. More...
 
void urg_ptr (uint16_t new_urg_ptr)
 Setter for the urgent pointer field. More...
 
void data_offset (small_uint< 4 > new_doff)
 Setter for the data offset pointer field. More...
 
void mss (uint16_t value)
 Add a maximum segment size option. More...
 
uint16_t mss () const
 Searchs for a maximum segment size option. More...
 
void winscale (uint8_t value)
 Add a window scale option. More...
 
uint8_t winscale () const
 Searchs for a window scale option. More...
 
void sack_permitted ()
 Add a sack permitted option.
 
bool has_sack_permitted () const
 Searchs for a sack permitted option. More...
 
void sack (const sack_type &edges)
 Add a sack option. More...
 
sack_type sack () const
 Searchs for a sack option. More...
 
void timestamp (uint32_t value, uint32_t reply)
 Add a timestamp option. More...
 
std::pair< uint32_t, uint32_t > timestamp () const
 Searchs for a timestamp option. More...
 
void altchecksum (AltChecksums value)
 Add a alternate checksum option. More...
 
AltChecksums altchecksum () const
 Searchs for a alternate checksum option. More...
 
void set_flag (Flags tcp_flag, small_uint< 1 > value)
 Set a TCP flag value. More...
 
void flags (small_uint< 12 > value)
 Sets the value of the flag fields. More...
 
void add_option (const option &opt)
 Adds a TCP option. More...
 
void add_option (option &&opt)
 Adds a TCP option. More...
 
template<typename... Args>
void add_option (Args &&...args)
 Adds a TCP option using the provided arguments. More...
 
bool remove_option (OptionTypes type)
 Removes a TCP option. More...
 
uint32_t header_size () const
 Returns the header size. More...
 
bool matches_response (const uint8_t *ptr, uint32_t total_sz) const
 Check whether ptr points to a valid response for this PDU. More...
 
PDUType pdu_type () const
 Getter for the PDU's type. More...
 
const optionsearch_option (OptionTypes type) const
 Searchs for an option that matchs the given type. More...
 
TCPclone () const
 
- Public Member Functions inherited from Tins::PDU
 PDU ()
 Default constructor.
 
 PDU (PDU &&rhs) TINS_NOEXCEPT
 Move constructor. More...
 
PDUoperator= (PDU &&rhs) TINS_NOEXCEPT
 Move assignment operator. More...
 
virtual ~PDU ()
 PDU destructor. More...
 
virtual uint32_t trailer_size () const
 Trailer's size. More...
 
uint32_t size () const
 The whole chain of PDU's size, including this one. More...
 
PDUinner_pdu () const
 Getter for the inner PDU. More...
 
PDUrelease_inner_pdu ()
 Releases the inner PDU. More...
 
void inner_pdu (PDU *next_pdu)
 Sets the child PDU. More...
 
void inner_pdu (const PDU &next_pdu)
 Sets the child PDU. More...
 
serialization_type serialize ()
 Serializes the whole chain of PDU's, including this one. More...
 
template<typename T >
T * find_pdu (PDUType type=T::pdu_flag)
 Finds and returns the first PDU that matches the given flag. More...
 
template<typename T >
const T * find_pdu (PDUType type=T::pdu_flag) const
 Finds and returns the first PDU that matches the given flag. More...
 
template<typename T >
T & rfind_pdu (PDUType type=T::pdu_flag)
 Finds and returns the first PDU that matches the given flag. More...
 
template<typename T >
const T & rfind_pdu (PDUType type=T::pdu_flag) const
 Finds and returns the first PDU that matches the given flag. More...
 
virtual void send (PacketSender &sender, const NetworkInterface &iface)
 Send the stack of PDUs through a PacketSender. More...
 
virtual PDUrecv_response (PacketSender &sender, const NetworkInterface &iface)
 Receives a matching response for this packet. More...
 
virtual bool matches_flag (PDUType flag) const
 Check whether this PDU matches the specified flag. More...
 

Static Public Member Functions

static metadata extract_metadata (const uint8_t *buffer, uint32_t total_sz)
 Extracts metadata for this protocol based on the buffer provided. More...
 

Static Public Attributes

static const PDU::PDUType pdu_flag = PDU::TCP
 
- Static Public Attributes inherited from Tins::PDU
static const endian_type endianness = BE
 

Additional Inherited Members

- Protected Member Functions inherited from Tins::PDU
 PDU (const PDU &other)
 Copy constructor.
 
PDUoperator= (const PDU &other)
 Copy assignment operator.
 
void copy_inner_pdu (const PDU &pdu)
 Copy other PDU's inner PDU(if any). More...
 
virtual void prepare_for_serialize (const PDU *parent)
 Prepares this PDU for serialization. More...
 
void serialize (uint8_t *buffer, uint32_t total_sz, const PDU *parent)
 Serializes this PDU and propagates this action to child PDUs. More...
 

Detailed Description

Represents a TCP PDU.

This class represents a TCP PDU.

When sending TCP PDUs, the checksum is calculated automatically every time you send the packet.

While sniffing, the payload sent in each packet will be wrapped in a RawPDU, which is set as the TCP object's inner_pdu. Therefore, if you are sniffing and want to see the TCP packet's payload, you need to do the following:

// Get a packet from somewhere.
TCP tcp = ...;
// Extract the RawPDU object.
const RawPDU& raw = tcp.rfind_pdu<RawPDU>();
// Finally, take the payload (this is a vector<uint8_t>)
const RawPDU::payload_type& payload = raw.payload();
See Also
RawPDU

Member Typedef Documentation

typedef PDUOption<uint8_t, TCP> Tins::TCP::option

The type used to store TCP options.

typedef std::list<option> Tins::TCP::options_type

The type used to store the options.

typedef std::vector<uint32_t> Tins::TCP::sack_type

The type used to store the sack option.

Member Enumeration Documentation

TCP flags enum.

These flags identify those supported by the TCP PDU.

TCP options enum.

This enum defines option types supported by TCP PDU.

Constructor & Destructor Documentation

Tins::TCP::TCP ( uint16_t  dport = 0,
uint16_t  sport = 0 
)

TCP constructor.

Creates an instance of TCP. Destination and source port can be provided, otherwise both will be 0.

Parameters
dportDestination port.
sportSource port.
Tins::TCP::TCP ( const uint8_t *  buffer,
uint32_t  total_sz 
)

Constructs TCP object from a buffer.

If there is not enough size for a TCP header, or any of the TLV options are malformed, a malformed_packet exception is thrown.

Any extra data will be stored in a RawPDU.

Parameters
bufferThe buffer from which this PDU will be constructed.
total_szThe total size of the buffer.

Member Function Documentation

uint32_t Tins::TCP::ack_seq ( ) const
inline

Getter for the acknowledge number field.

Returns
The acknowledge number field value.
void Tins::TCP::ack_seq ( uint32_t  new_ack_seq)

Setter for the acknowledge number.

Parameters
new_ack_seqThe new acknowledge number.
void Tins::TCP::add_option ( const option opt)

Adds a TCP option.

Parameters
optionThe option to be added.
void Tins::TCP::add_option ( option &&  opt)
inline

Adds a TCP option.

This move-constructs the option.

Parameters
optionThe option to be added.
template<typename... Args>
void Tins::TCP::add_option ( Args &&...  args)
inline

Adds a TCP option using the provided arguments.

The option is constructed from the provided parameters.

Parameters
argsThe arguments to be used in the option's constructor.
void Tins::TCP::altchecksum ( AltChecksums  value)

Add a alternate checksum option.

Parameters
valueThe new alternate checksum scale.
TCP::AltChecksums Tins::TCP::altchecksum ( ) const

Searchs for a alternate checksum option.

Parameters
valueA pointer in which the option's value will be stored.
Returns
True if the option was found, false otherwise.
uint16_t Tins::TCP::checksum ( ) const
inline

Getter for the checksum field.

Returns
The checksum field value.
TCP* Tins::TCP::clone ( ) const
inlinevirtual
See Also
PDU::clone

Implements Tins::PDU.

small_uint<4> Tins::TCP::data_offset ( ) const
inline

Getter for the data offset field.

Returns
The data offset field value.
void Tins::TCP::data_offset ( small_uint< 4 >  new_doff)

Setter for the data offset pointer field.

Parameters
new_doffThe new data offset pointer.
uint16_t Tins::TCP::dport ( ) const
inline

Getter for the destination port field.

Returns
The destination port field value.
void Tins::TCP::dport ( uint16_t  new_dport)

Setter for the destination port field.

Parameters
new_dportThe new destination port.
PDU::metadata Tins::TCP::extract_metadata ( const uint8_t *  buffer,
uint32_t  total_sz 
)
static

Extracts metadata for this protocol based on the buffer provided.

Parameters
bufferPointer to a buffer
total_szSize of the buffer pointed by buffer
small_uint< 12 > Tins::TCP::flags ( ) const

Gets the flags' values.

All of the set flags will be joined together into a 12 bit value. This way, you can check for multiple flags at the same time:

TCP tcp = ...;
if(tcp.flags() == (TCP::SYN | TCP::ACK)) {
// It's a SYN+ACK!
}
Returns
The value of the flags field.
void Tins::TCP::flags ( small_uint< 12 >  value)

Sets the value of the flag fields.

This method can be used to set several flags at the same time.

// Get a TCP packet from somewhere and set the flags to SYN && ACK
TCP tcp = ...;
tcp.flags(TCP::SYN | TCP::ACK);
// Now also set the PSH flag, without modifying
// the rest of the flags.
tcp.flags(tcp.flags() | TCP::PSH);
Parameters
valueThe new value of the flags.
small_uint< 1 > Tins::TCP::get_flag ( Flags  tcp_flag) const

Gets the value of a flag.

This method gets the value of a specific flag. If you want to check for multiple flags at the same time, use TCP::flags.

If you want to check if this PDU has the SYN flag on, you can do it like this:

// Get a TCP packet from somewhere.
TCP tcp = ...;
if(tcp.get_flag(TCP::SYN)) {
// The SYN flag is on!
}
See Also
TCP::flags
Parameters
tcp_flagThe polled flag.
Returns
The value of the flag.
bool Tins::TCP::has_sack_permitted ( ) const

Searchs for a sack permitted option.

Returns
True if the option was found, false otherwise.
uint32_t Tins::TCP::header_size ( ) const
virtual

Returns the header size.

This method overrides PDU::header_size. This size includes the payload and options size.

See Also
PDU::header_size

Implements Tins::PDU.

bool Tins::TCP::matches_response ( const uint8_t *  ptr,
uint32_t  total_sz 
) const
virtual

Check whether ptr points to a valid response for this PDU.

See Also
PDU::matches_response
Parameters
ptrThe pointer to the buffer.
total_szThe size of the buffer.

Reimplemented from Tins::PDU.

void Tins::TCP::mss ( uint16_t  value)

Add a maximum segment size option.

Parameters
valueThe new maximum segment size.
uint16_t Tins::TCP::mss ( ) const

Searchs for a maximum segment size option.

Parameters
valueA pointer in which the option's value will be stored.
Returns
True if the option was found, false otherwise.
const options_type& Tins::TCP::options ( ) const
inline

Getter for the option list.

Returns
The options list.
PDUType Tins::TCP::pdu_type ( ) const
inlinevirtual

Getter for the PDU's type.

See Also
PDU::pdu_type

Implements Tins::PDU.

bool Tins::TCP::remove_option ( OptionTypes  type)

Removes a TCP option.

If there are multiple options of the given type, only the first one will be removed.

Parameters
typeThe type of the option to be removed.
Returns
true if the option was removed, false otherwise.
void Tins::TCP::sack ( const sack_type edges)

Add a sack option.

Parameters
valueThe new window scale.
TCP::sack_type Tins::TCP::sack ( ) const

Searchs for a sack option.

Parameters
valueA pointer in which the option's value will be stored.
Returns
True if the option was found, false otherwise.
const TCP::option * Tins::TCP::search_option ( OptionTypes  type) const

Searchs for an option that matchs the given type.

Parameters
typeThe option type to be searched.
Returns
A pointer to the option, or 0 if it was not found.
uint32_t Tins::TCP::seq ( ) const
inline

Getter for the sequence number field.

Returns
The sequence number field value.
void Tins::TCP::seq ( uint32_t  new_seq)

Setter for the sequence number.

Parameters
new_seqThe new sequence number.
void Tins::TCP::set_flag ( Flags  tcp_flag,
small_uint< 1 >  value 
)

Set a TCP flag value.

Parameters
tcp_flagThe flag to be set.
valueThe new value for this flag. Must be 0 or 1.
uint16_t Tins::TCP::sport ( ) const
inline

Getter for the source port field.

Returns
The source port field value.
void Tins::TCP::sport ( uint16_t  new_sport)

Setter for the source port field.

Parameters
new_sportThe new source port.
void Tins::TCP::timestamp ( uint32_t  value,
uint32_t  reply 
)

Add a timestamp option.

Parameters
valueThe current value of the timestamp clock.
replyThe echo reply field.
pair< uint32_t, uint32_t > Tins::TCP::timestamp ( ) const

Searchs for a timestamp option.

Parameters
valueA pointer in which the option's value will be stored.
replyA pointer in which the option's reply value will be stored.
Returns
True if the option was found, false otherwise.
uint16_t Tins::TCP::urg_ptr ( ) const
inline

Getter for the urgent pointer field.

Returns
The urgent pointer field value.
void Tins::TCP::urg_ptr ( uint16_t  new_urg_ptr)

Setter for the urgent pointer field.

Parameters
new_urg_ptrThe new urgent pointer.
uint16_t Tins::TCP::window ( ) const
inline

Getter for the window size field.

Returns
The window size field value.
void Tins::TCP::window ( uint16_t  new_window)

Setter for the window size.

Parameters
new_windowThe new window size.
void Tins::TCP::winscale ( uint8_t  value)

Add a window scale option.

Parameters
valueThe new window scale.
uint8_t Tins::TCP::winscale ( ) const

Searchs for a window scale option.

Parameters
valueA pointer in which the option's value will be stored.
Returns
True if the option was found, false otherwise.

Member Data Documentation

const PDU::PDUType Tins::TCP::pdu_flag = PDU::TCP
static

This PDU's flag.


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