libtins
4.0
|
Base class for protocol data units. More...
#include <pdu.h>
Classes | |
struct | metadata |
Type used to store a PDU header's data. More... | |
Public Types | |
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 | |
PDU () | |
Default constructor. | |
PDU (PDU &&rhs) TINS_NOEXCEPT | |
Move constructor. More... | |
PDU & | operator= (PDU &&rhs) TINS_NOEXCEPT |
Move assignment operator. More... | |
virtual | ~PDU () |
PDU destructor. More... | |
virtual uint32_t | header_size () const =0 |
The header's size. | |
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... | |
PDU * | inner_pdu () const |
Getter for the inner PDU. More... | |
PDU * | parent_pdu () const |
PDU * | release_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 PDU * | clone () const =0 |
Clones this packet. More... | |
virtual void | send (PacketSender &sender, const NetworkInterface &iface) |
Send the stack of PDUs through a PacketSender. More... | |
virtual PDU * | recv_response (PacketSender &sender, const NetworkInterface &iface) |
Receives a matching response for this packet. More... | |
virtual bool | matches_response (const uint8_t *ptr, uint32_t total_sz) const |
Check whether ptr points to a valid response for this PDU. More... | |
virtual bool | matches_flag (PDUType flag) const |
Check whether this PDU matches the specified flag. More... | |
virtual PDUType | pdu_type () const =0 |
Getter for the PDU's type. More... | |
Static Public Attributes | |
static const endian_type | endianness = BE |
Protected Member Functions | |
PDU (const PDU &other) | |
Copy constructor. | |
PDU & | operator= (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... | |
virtual void | write_serialization (uint8_t *buffer, uint32_t total_sz)=0 |
Serializes this TCP PDU. More... | |
Base class for protocol data units.
Represents a PDU which holds raw data.
Every PDU implementation inherits from this class.
PDUs can contain 0 or 1 inner PDU. By stacking several PDUs together, you can construct packets. These are created upwards: upper layers will be children of the lower ones.
If you want to find a specific protocol within a PDU chain, you can use PDU::find_pdu and PDU::rfind_pdu. Both of them take a template parameter that indicates the PDU type you are looking for. The first one returns a pointer to the first object of that type, and the second one returns a reference (and throws if it is not found).
For example:
PDU objects can be serialized. Serialization converts the entire PDU stack into a vector of bytes. This process might modify some parameters on packets depending on which protocols are used in it. For example:
If you want to serialize a packet, just use PDU::serialize:
This class is a wrapper over a byte array. It can be used to hold the payload sent over transport layer protocols (such as TCP or UDP).
While sniffing, this class is the one that will hold transport layer protocols' payload. You can simply convert a RawPDU into a specific application layer protocol using the RawPDU::to method:
The type that will be returned when serializing PDUs.
The typep used to identify the endianness of every PDU.
enum Tins::PDU::PDUType |
|
virtual |
PDU destructor.
Deletes the inner pdu, as a consequence every child pdu is deleted.
|
pure virtual |
Clones this packet.
This method clones this PDU and clones every inner PDU, therefore obtaining a clone of the whole inner PDU chain. The pointer returned must be deleted by the user.
Implemented in Tins::ICMPv6, Tins::DNS, Tins::DHCPv6, Tins::RSNEAPOL, Tins::Dot11BlockAck, Tins::IP, Tins::Dot11ReAssocResponse, Tins::Dot11BlockAckRequest, Tins::TCP, Tins::Dot11ReAssocRequest, Tins::DHCP, Tins::Dot11, Tins::ICMP, Tins::Dot11Ack, Tins::RadioTap, Tins::Dot11EndCFAck, Tins::Dot11AssocResponse, Tins::RC4EAPOL, Tins::LLC, Tins::Dot11QoSData, Tins::IPv6, Tins::Dot11CFEnd, Tins::ARP, Tins::BootP, Tins::Dot11PSPoll, Tins::Dot11Deauthentication, Tins::IPSecESP, Tins::Dot11Data, Tins::Dot11AssocRequest, Tins::Dot11ProbeResponse, Tins::RawPDU, Tins::Dot11RTS, Tins::EthernetII, Tins::STP, Tins::Dot3, Tins::PPPoE, Tins::UDP, Tins::SNAP, Tins::SLL, Tins::IPSecAH, Tins::Dot11Authentication, Tins::Dot11Beacon, Tins::MPLS, Tins::Dot1Q, Tins::Dot11Disassoc, Tins::Loopback, Tins::Dot11ProbeRequest, and Tins::PDUCacher< T >.
|
protected |
|
inline |
|
inline |
Finds and returns the first PDU that matches the given flag.
flag | The flag which being searched. |
|
inline |
void Tins::PDU::inner_pdu | ( | PDU * | next_pdu | ) |
void Tins::PDU::inner_pdu | ( | const PDU & | next_pdu | ) |
Sets the child PDU.
The PDU parameter is cloned using PDU::clone.
next_pdu | The new child PDU. |
|
inlinevirtual |
Check whether this PDU matches the specified flag.
This method should be reimplemented in PDU classes which have subclasses, and try to match the given PDU to each of its parent classes' flag.
flag | The flag to match. |
Reimplemented in Tins::Dot11ManagementFrame, Tins::RSNEAPOL, Tins::Dot11BlockAck, Tins::Dot11ReAssocResponse, Tins::Dot11BlockAckRequest, Tins::Dot11ReAssocRequest, Tins::Dot11, Tins::Dot11Ack, Tins::Dot11EndCFAck, Tins::Dot11AssocResponse, Tins::RC4EAPOL, Tins::Dot11QoSData, Tins::Dot11CFEnd, Tins::Dot11PSPoll, Tins::Dot11Deauthentication, Tins::Dot11Data, Tins::Dot11AssocRequest, Tins::Dot11ProbeResponse, Tins::Dot11RTS, Tins::Dot11Authentication, Tins::Dot11Beacon, Tins::PDUCacher< T >, Tins::Dot11Disassoc, Tins::Dot11ProbeRequest, and Tins::Dot11Control.
|
virtual |
Check whether ptr points to a valid response for this PDU.
This method must check whether the buffer pointed by ptr is a valid response for this PDU. If it is valid, then it might want to propagate the call to the next PDU. Note that in some cases, such as ICMP Host Unreachable, there is no need to ask the next layer for matching.
ptr | The pointer to the buffer. |
total_sz | The size of the buffer. |
Reimplemented in Tins::ICMPv6, Tins::DNS, Tins::DHCPv6, Tins::IP, Tins::TCP, Tins::ICMP, Tins::RadioTap, Tins::IPv6, Tins::ARP, Tins::BootP, Tins::Dot1Q, Tins::RawPDU, Tins::EthernetII, Tins::Dot3, Tins::UDP, Tins::PDUCacher< T >, and Tins::Loopback.
Move assignment operator.
rhs | The PDU to be moved. |
|
inline |
|
pure virtual |
Getter for the PDU's type.
Implemented in Tins::Dot11ManagementFrame, Tins::ICMPv6, Tins::DHCPv6, Tins::DNS, Tins::RSNEAPOL, Tins::Dot11BlockAck, Tins::IP, Tins::Dot11ReAssocResponse, Tins::Dot11BlockAckRequest, Tins::TCP, Tins::Dot11ReAssocRequest, Tins::DHCP, Tins::Dot11, Tins::ICMP, Tins::Dot11Ack, Tins::RadioTap, Tins::Dot11EndCFAck, Tins::Dot11AssocResponse, Tins::RC4EAPOL, Tins::Dot11QoSData, Tins::IPv6, Tins::Dot11CFEnd, Tins::LLC, Tins::BootP, Tins::ARP, Tins::Dot11PSPoll, Tins::IPSecESP, Tins::Dot11Deauthentication, Tins::Dot11Data, Tins::Dot11AssocRequest, Tins::Dot11ProbeResponse, Tins::Dot11RTS, Tins::RawPDU, Tins::PPPoE, Tins::EthernetII, Tins::Dot3, Tins::STP, Tins::UDP, Tins::Dot11Beacon, Tins::EAPOL, Tins::SNAP, Tins::IPSecAH, Tins::MPLS, Tins::PDUCacher< T >, Tins::Dot11Authentication, Tins::Dot1Q, Tins::SLL, Tins::Dot11Disassoc, Tins::Loopback, Tins::Dot11ProbeRequest, and Tins::Dot11Control.
|
protectedvirtual |
Prepares this PDU for serialization.
This method is called before the inner PDUs are serialized. It's useful in situations such as when serializing IP PDUs, which don't contain any link layer encapsulation, and therefore require to set the source IP address before the TCP/UDP checksum is calculated.
By default, this method does nothing
|
virtual |
Receives a matching response for this packet.
This method should act as a proxy for PacketSender::recv_lX methods.
sender | The packet sender which will receive the packet. |
iface | The interface in which to expect the response. |
Reimplemented in Tins::IP, Tins::IPv6, Tins::EthernetII, Tins::Dot3, and Tins::PDUCacher< T >.
PDU * Tins::PDU::release_inner_pdu | ( | ) |
Releases the inner PDU.
This method makes this PDU to no longer own the inner PDU. The current inner PDU is returned, and is not destroyed. That means after calling this function, you are responsible for using operator delete on the returned pointer.
Use this method if you want to somehow re-use a PDU that is already owned by another PDU.
|
inline |
Finds and returns the first PDU that matches the given flag.
If the PDU is not found, a pdu_not_found exception is thrown.
flag | The flag which being searched. |
|
inline |
Finds and returns the first PDU that matches the given flag.
flag | The flag which being searched. |
|
virtual |
Send the stack of PDUs through a PacketSender.
This method will be called only for the PDU on the bottom of the stack, therefore it should only implement this method if it can be sent.
PacketSender implements specific methods to send packets which start on every valid TCP/IP stack layer; this should only be a proxy for those methods.
If this PDU does not represent a link layer protocol, then the interface argument will be ignored.
sender | The PacketSender which will send the packet. |
iface | The network interface in which this packet will be sent. |
Reimplemented in Tins::IP, Tins::Dot11, Tins::IPv6, Tins::RadioTap, Tins::EthernetII, Tins::Dot3, and Tins::PDUCacher< T >.
PDU::serialization_type Tins::PDU::serialize | ( | ) |
|
protected |
uint32_t Tins::PDU::size | ( | ) | const |
The whole chain of PDU's size, including this one.
Returns the sum of this and all children PDUs' size.
|
inlinevirtual |
Trailer's size.
Some protocols require a trailer(like Ethernet). This defaults to 0.
Reimplemented in Tins::ICMPv6, Tins::RadioTap, Tins::ICMP, Tins::EthernetII, and Tins::Dot1Q.
|
protectedpure virtual |
Each PDU must override this method and implement it's own serialization.
buffer | The buffer in which the PDU will be serialized. |
total_sz | The size available in the buffer. |
Implemented in Tins::BootP.
|
static |
The endianness used by this PDU. This can be overriden by subclasses.