libtins  4.0
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Tins::UDP Class Reference

Represents an UDP PDU. More...

#include <udp.h>

Inheritance diagram for Tins::UDP:
Tins::PDU

Public Member Functions

 UDP (uint16_t dport=0, uint16_t sport=0)
 UDP constructor. More...
 
 UDP (const uint8_t *buffer, uint32_t total_sz)
 Constructs an UDP object from a buffer. More...
 
uint16_t dport () const
 Getter for the destination port. More...
 
uint16_t sport () const
 Getter for the source port. More...
 
uint16_t length () const
 Getter for the length of the datagram. More...
 
uint16_t checksum () const
 Getter for the checksum of the datagram. More...
 
void dport (uint16_t new_dport)
 Set the destination port. More...
 
void sport (uint16_t new_sport)
 Set the source port. More...
 
void length (uint16_t new_len)
 Set the length field. 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...
 
uint32_t header_size () const
 Returns the header size. More...
 
PDUType pdu_type () const
 Getter for the PDU's type. More...
 
UDPclone () 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 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::UDP
 This PDU's flag.
 
- Static Public Attributes inherited from Tins::PDU
static const endian_type endianness = BE
 

Additional Inherited Members

- 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
 
- 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 ()
 Prepares this PDU for serialization. More...
 
void serialize (uint8_t *buffer, uint32_t total_sz)
 Serializes this PDU and propagates this action to child PDUs. More...
 

Detailed Description

Represents an UDP PDU.

This class represents an UDP PDU.

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

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

Constructor & Destructor Documentation

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

UDP constructor.

Constructs an instance of UDP. The destination and source port can be provided, otherwise both of them will be 0.

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

Constructs an UDP object from a buffer.

If there is not enough size for a UDP header 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

uint16_t Tins::UDP::checksum ( ) const
inline

Getter for the checksum of the datagram.

Returns
The datagram's checksum.
UDP* Tins::UDP::clone ( ) const
inlinevirtual
See also
PDU::clone

Implements Tins::PDU.

uint16_t Tins::UDP::dport ( ) const
inline

Getter for the destination port.

Returns
The datagram's destination port.
void Tins::UDP::dport ( uint16_t  new_dport)

Set the destination port.

Parameters
new_dportThe new destination port.
PDU::metadata Tins::UDP::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
uint32_t Tins::UDP::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.

uint16_t Tins::UDP::length ( ) const
inline

Getter for the length of the datagram.

Returns
The length of the datagram.
void Tins::UDP::length ( uint16_t  new_len)

Set the length field.

Parameters
new_lenThe new length field.
bool Tins::UDP::matches_response ( const uint8_t *  ptr,
uint32_t  total_sz 
) const
virtual

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

This compares the source and destination ports in the provided response with those stored in this PDU.

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

Reimplemented from Tins::PDU.

PDUType Tins::UDP::pdu_type ( ) const
inlinevirtual

Getter for the PDU's type.

See also
PDU::pdu_type

Implements Tins::PDU.

uint16_t Tins::UDP::sport ( ) const
inline

Getter for the source port.

Returns
The datagram's source port.
void Tins::UDP::sport ( uint16_t  new_sport)

Set the source port.

Parameters
new_sportThe new source port.

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