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

Class that represents an IP PDU. More...

#include <ip.h>

Inheritance diagram for Tins::IP:
Tins::PDU

Classes

struct  generic_route_option_type
 
struct  option_identifier
 The type used to represent an option's type. More...
 
struct  security_type
 

Public Types

enum  Flags { FLAG_RESERVED = 4, DONT_FRAGMENT = 2, MORE_FRAGMENTS = 1 }
 
enum  OptionClass { CONTROL = 0, MEASUREMENT = 2 }
 Enum indicating the option's class. More...
 
enum  OptionNumber {
  END = 0, NOOP = 1, SEC = 2, LSRR = 3,
  TIMESTAMP = 4, EXTSEC = 5, RR = 7, SID = 8,
  SSRR = 9, MTUPROBE = 11, MTUREPLY = 12, EIP = 17,
  TR = 18, ADDEXT = 19, RTRALT = 20, SDB = 21,
  DPS = 23, UMP = 24, QS = 25
}
 Enum indicating the option's id number. More...
 
typedef IPv4Address address_type
 
typedef PDUOption
< option_identifier, IP
option
 
typedef generic_route_option_type lsrr_type
 
typedef generic_route_option_type ssrr_type
 
typedef generic_route_option_type record_route_type
 
typedef std::vector< optionoptions_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

 IP (address_type ip_dst=address_type(), address_type ip_src=address_type())
 Constructor for building the IP PDU. More...
 
 IP (const uint8_t *buffer, uint32_t total_sz)
 Constructs an IP object from a buffer and adds all identifiable PDUs found in the buffer as children of this one. More...
 
small_uint< 4 > head_len () const
 Getter for the header length field. More...
 
uint8_t tos () const
 Getter for the type of service field. More...
 
uint16_t tot_len () const
 Getter for the total length field. More...
 
uint16_t id () const
 Getter for the id field. More...
 
 TINS_DEPRECATED (uint16_t frag_off() const)
 Getter for the fragment offset field. More...
 
small_uint< 13 > fragment_offset () const
 Getter for the fragment offset field. More...
 
Flags flags () const
 Getter for the flags field. More...
 
uint8_t ttl () const
 Getter for the time to live field. More...
 
uint8_t protocol () const
 Getter for the protocol field. More...
 
uint16_t checksum () const
 Getter for the checksum field. More...
 
address_type src_addr () const
 Getter for the source address field. More...
 
address_type dst_addr () const
 Getter for the destination address field. More...
 
small_uint< 4 > version () const
 Getter for the version field. More...
 
const options_typeoptions () const
 Getter for the IP options. More...
 
void tos (uint8_t new_tos)
 Setter for the type of service field. More...
 
void id (uint16_t new_id)
 Setter for the id field. More...
 
 TINS_DEPRECATED (void frag_off(uint16_t new_frag_off))
 Setter for the fragment offset field. More...
 
void fragment_offset (small_uint< 13 > new_frag_off)
 Setter for the fragment offset field. More...
 
void flags (Flags new_flags)
 Setter for the flags field. More...
 
void ttl (uint8_t new_ttl)
 Setter for the time to live field. More...
 
void protocol (uint8_t new_protocol)
 Setter for the protocol field. More...
 
void src_addr (address_type ip)
 Setter for the source address field. More...
 
void dst_addr (address_type ip)
 Setter for the destination address field. More...
 
void version (small_uint< 4 > ver)
 Setter for the version field. More...
 
void add_option (const option &opt)
 Adds an IP option. More...
 
void add_option (option &&opt)
 Adds an IP option. More...
 
template<typename... Args>
void add_option (Args &&...args)
 Adds an IP option. More...
 
bool remove_option (option_identifier id)
 Removes an IP option. More...
 
const optionsearch_option (option_identifier id) const
 Searchs for an option that matchs the given flag. More...
 
void eol ()
 Adds an End Of List option.
 
void noop ()
 Adds a NOP option.
 
void security (const security_type &data)
 Adds a security option. More...
 
void lsrr (const lsrr_type &data)
 Adds a Loose Source and Record Route option. More...
 
void ssrr (const ssrr_type &data)
 Adds a Strict Source and Record Route option. More...
 
void record_route (const record_route_type &data)
 Adds a Record Route option. More...
 
void stream_identifier (uint16_t stream_id)
 Adds a Stream Identifier option. More...
 
security_type security () const
 Searchs and returns a security option. More...
 
lsrr_type lsrr () const
 Searchs and returns a Loose Source and Record Route option. More...
 
ssrr_type ssrr () const
 Searchs and returns a Strict Source and Record Route option. More...
 
record_route_type record_route () const
 Searchs and returns a Record Route option. More...
 
uint16_t stream_identifier () const
 Searchs and returns a Stream Identifier option. More...
 
uint32_t header_size () const
 Returns the header size. More...
 
void send (PacketSender &sender, const NetworkInterface &)
 
bool matches_response (const uint8_t *ptr, uint32_t total_sz) const
 Check whether ptr points to a valid response for this PDU. More...
 
PDUrecv_response (PacketSender &sender, const NetworkInterface &)
 Receives a matching response for this packet. More...
 
bool is_fragmented () const
 
PDUType pdu_type () const
 Getter for the PDU's type. More...
 
IPclone () 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...
 
PDUparent_pdu () const
 
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 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...
 

Public Attributes

TINS_BEGIN_PACK struct
Tins::IP::option_identifier 
TINS_END_PACK
 

Static Public Attributes

static const PDU::PDUType pdu_flag = PDU::IP
 
- 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...
 
void serialize (uint8_t *buffer, uint32_t total_sz)
 Serializes this PDU and propagates this action to child PDUs. More...
 

Detailed Description

Class that represents an IP PDU.

By default, IP PDUs are initialized, setting TTL to IP::DEFAULT_TTL, id field to 1 and version to 4. Taking this into account, users should set destination and source port and would be enough to send one.

When IP is the lowest layer on a packet, and the packet is serialized this willc heck if the source address is different than 0.0.0.0. If it is, the address of the interface in which the packet is going to be sent is retrieved (by using the routing table and the destination address) and set as the source address. If you don't want this behaviour, simply set the source address to 0.0.0.0.

Member Typedef Documentation

The type used to store addresses.

The type of the Loose Source and Record Route

The IP options type.

typedef std::vector<option> Tins::IP::options_type

The type used to store IP options.

The type of the Record Route

The type of the Strict Source and Record Route

Member Enumeration Documentation

Type used to represent the different IP flags.

Enum indicating the option's class.

Enum OptionClass represents the different classes of IP Options.

Enum indicating the option's id number.

Enum Option indicates the possible IP Options.

Constructor & Destructor Documentation

Tins::IP::IP ( address_type  ip_dst = address_type(),
address_type  ip_src = address_type() 
)

Constructor for building the IP PDU.

Both the destination and source IP address can be supplied. By default, those fields are initialized using the IP address 0.0.0.0.

Parameters
ip_dstThe destination ip address(optional).
ip_srcThe source ip address(optional).
Tins::IP::IP ( const uint8_t *  buffer,
uint32_t  total_sz 
)

Constructs an IP object from a buffer and adds all identifiable PDUs found in the buffer as children of this one.

If there is not enough size for an IP header, a malformed_packet exception is thrown.

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

Member Function Documentation

void Tins::IP::add_option ( const option opt)

Adds an IP option.

The option is added after the last option in the option fields.

Parameters
optThe option to be added
void Tins::IP::add_option ( option &&  opt)
inline

Adds an IP option.

The option is move-constructed.

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

Adds an IP option.

The option is constructed from the provided parameters.

Parameters
argsThe arguments to be used in the option's constructor.
uint16_t Tins::IP::checksum ( ) const
inline

Getter for the checksum field.

Returns
The checksum for this IP PDU.
IP* Tins::IP::clone ( ) const
inlinevirtual
See Also
PDU::clone

Implements Tins::PDU.

address_type Tins::IP::dst_addr ( ) const
inline

Getter for the destination address field.

Returns
The destination address for this IP PDU.
void Tins::IP::dst_addr ( address_type  ip)

Setter for the destination address field.

Parameters
ipThe destination address to be set.
PDU::metadata Tins::IP::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
Flags Tins::IP::flags ( ) const
inline

Getter for the flags field.

Returns
The IP flags field
void Tins::IP::flags ( Flags  new_flags)

Setter for the flags field.

Parameters
new_flagsThe new IP flags field value.
small_uint<13> Tins::IP::fragment_offset ( ) const
inline

Getter for the fragment offset field.

This will return the fragment offset field, as present in the packet, which indicates the offset of this fragment in blocks of 8 bytes.

Returns
The fragment offset, measured in units of 8 byte blocks
void Tins::IP::fragment_offset ( small_uint< 13 >  new_frag_off)

Setter for the fragment offset field.

The value provided is measured in units of 8 byte blocks. This means that if you want this packet to have a fragment offset of X, you need to provide X / 8 as the argument to this method.

Parameters
new_frag_offThe new fragment offset, measured in units of 8 byte blocks.
small_uint<4> Tins::IP::head_len ( ) const
inline

Getter for the header length field.

Returns
The number of dwords the header occupies in an uin8_t.
uint32_t Tins::IP::header_size ( ) const
virtual

Returns the header size.

This method overrides PDU::header_size.

See Also
PDU::header_size

Implements Tins::PDU.

uint16_t Tins::IP::id ( ) const
inline

Getter for the id field.

Returns
The id for this IP PDU.
void Tins::IP::id ( uint16_t  new_id)

Setter for the id field.

Parameters
new_idThe new id.
bool Tins::IP::is_fragmented ( ) const

Indicates whether this PDU is fragmented.

Returns
true if this PDU is fragmented, false otherwise.
void Tins::IP::lsrr ( const lsrr_type data)
inline

Adds a Loose Source and Record Route option.

Parameters
dataThe data to be stored in this option.
lsrr_type Tins::IP::lsrr ( ) const
inline

Searchs and returns a Loose Source and Record Route option.

If no such option exists, an option_not_found exception is thrown.

Returns
lsrr_type containing the option found.
bool Tins::IP::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.

const options_type& Tins::IP::options ( ) const
inline

Getter for the IP options.

Returns
The stored options.
PDUType Tins::IP::pdu_type ( ) const
inlinevirtual

Getter for the PDU's type.

See Also
PDU::pdu_type

Implements Tins::PDU.

uint8_t Tins::IP::protocol ( ) const
inline

Getter for the protocol field.

Returns
The protocol for this IP PDU.
void Tins::IP::protocol ( uint8_t  new_protocol)

Setter for the protocol field.

Note that this protocol will be overwritten using the inner_pdu's protocol type during serialization unless the IP datagram is fragmented.

If the packet is fragmented and was originally sniffed, the original protocol type will be kept when serialized.

If this packet has been crafted manually and the inner_pdu is, for example, a RawPDU, then setting the protocol yourself is necessary.

Parameters
new_protocolThe new protocol.
void Tins::IP::record_route ( const record_route_type data)
inline

Adds a Record Route option.

Parameters
dataThe data to be stored in this option.
record_route_type Tins::IP::record_route ( ) const
inline

Searchs and returns a Record Route option.

If no such option exists, an option_not_found exception is thrown.

Returns
record_route_type containing the option found.
PDU * Tins::IP::recv_response ( PacketSender sender,
const NetworkInterface  
)
virtual

Receives a matching response for this packet.

See Also
PDU::recv_response
Parameters
senderThe packet sender which will receive the packet.

Reimplemented from Tins::PDU.

bool Tins::IP::remove_option ( option_identifier  id)

Removes an IP 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.
const IP::option * Tins::IP::search_option ( option_identifier  id) const

Searchs for an option that matchs the given flag.

If the option is not found, a null pointer is returned. Deleting the returned pointer will result in undefined behaviour.

Parameters
idThe option identifier to be searched.
void Tins::IP::security ( const security_type data)

Adds a security option.

Parameters
dataThe data to be stored in this option.
IP::security_type Tins::IP::security ( ) const

Searchs and returns a security option.

If no such option exists, an option_not_found exception is thrown.

Returns
security_type containing the option found.
void Tins::IP::send ( PacketSender sender,
const NetworkInterface  
)
virtual
See Also
PDU::send()

Reimplemented from Tins::PDU.

address_type Tins::IP::src_addr ( ) const
inline

Getter for the source address field.

Returns
The source address for this IP PDU.
void Tins::IP::src_addr ( address_type  ip)

Setter for the source address field.

Parameters
ipThe source address to be set.
void Tins::IP::ssrr ( const ssrr_type data)
inline

Adds a Strict Source and Record Route option.

Parameters
dataThe data to be stored in this option.
ssrr_type Tins::IP::ssrr ( ) const
inline

Searchs and returns a Strict Source and Record Route option.

If no such option exists, an option_not_found exception is thrown.

Returns
ssrr_type containing the option found.
void Tins::IP::stream_identifier ( uint16_t  stream_id)

Adds a Stream Identifier option.

Parameters
stream_idThe stream id to be stored in this option.
uint16_t Tins::IP::stream_identifier ( ) const

Searchs and returns a Stream Identifier option.

If no such option exists, an option_not_found exception is thrown.

Returns
uint16_t containing the option found.
Tins::IP::TINS_DEPRECATED ( uint16_t frag_off()  const)
inline

Getter for the fragment offset field.

This method is deprecated. Use IP::fragment_offset and IP::flags.

Deprecated:
Returns
The fragment offset for this IP PDU.
See Also
IP::fragment_offset
IP::flags
Tins::IP::TINS_DEPRECATED ( void   frag_offuint16_t new_frag_off)

Setter for the fragment offset field.

This method is deprecated. Use IP::fragment_offset and IP::flags.

Deprecated:
Parameters
new_frag_offThe new fragment offset.
See Also
IP::fragment_offset
IP::flags
uint8_t Tins::IP::tos ( ) const
inline

Getter for the type of service field.

Returns
The this IP PDU's type of service.
void Tins::IP::tos ( uint8_t  new_tos)

Setter for the type of service field.

Parameters
new_tosThe new type of service.
uint16_t Tins::IP::tot_len ( ) const
inline

Getter for the total length field.

Returns
The total length of this IP PDU.
uint8_t Tins::IP::ttl ( ) const
inline

Getter for the time to live field.

Returns
The time to live for this IP PDU.
void Tins::IP::ttl ( uint8_t  new_ttl)

Setter for the time to live field.

Parameters
new_ttlThe new time to live.
small_uint<4> Tins::IP::version ( ) const
inline

Getter for the version field.

Returns
The version for this IP PDU.
void Tins::IP::version ( small_uint< 4 >  ver)

Setter for the version field.

Parameters
verThe version field to be set.

Member Data Documentation

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

This PDU's flag.


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