TSDuck v3.40-4058
MPEG Transport Stream Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages Concepts
ts::IPPacket Class Reference

Representation of a raw IPv4 or IPv6 packet. More...

#include <tsIPPacket.h>

Public Types

using Port = AbstractNetworkAddress::Port
 The concept of port is used by TCP and UDP.
 

Public Member Functions

 IPPacket ()=default
 Default constructor.
 
 IPPacket (const void *data, size_t size)
 Constructor from raw content.
 
void clear ()
 Clear the packet content.
 
const uint8_t * data () const
 Get the address of the IP packet content.
 
const IPSocketAddressdestination () const
 Get the destination IP socket address.
 
bool fragmented () const
 Check if the IP packet is fragmented.
 
IP generation () const
 Get the current generation of IP addresses.
 
const uint8_t * ipHeader () const
 Get the address of the IP header.
 
size_t ipHeaderSize () const
 Get the size in bytes of the IP header.
 
bool isTCP () const
 Check if the packet is a valid TCP packet.
 
bool isUDP () const
 Check if the packet is a valid UDP packet.
 
bool isValid () const
 Check if the IPv4 packet is valid.
 
uint8_t protocol () const
 Get the sub-protocol type (TCP, UDP, etc).
 
const uint8_t * protocolData () const
 Get the address of the sub-protocol payload data (TCP data, UDP data, etc).
 
size_t protocolDataSize () const
 Get the size in bytes of the sub-protocol payload data (TCP data, UDP data, etc).
 
const uint8_t * protocolHeader () const
 Get the address of the sub-protocol header (TCP header, UDP header, etc).
 
size_t protocolHeaderSize () const
 Get the size in bytes of the sub-protocol header (TCP header, UDP header, etc).
 
bool reset (const void *data, size_t size)
 Reinitialize the IP4 packet with new content.
 
size_t size () const
 Get the size in bytes of the IP packet content.
 
const IPSocketAddresssource () const
 Get the source IP socket address.
 
bool tcpACK () const
 Get the TCP ACK flag in the packet.
 
bool tcpFIN () const
 Get the TCP FIN flag in the packet.
 
bool tcpRST () const
 Get the TCP RST flag in the packet.
 
uint32_t tcpSequenceNumber () const
 Get the TCP sequence number in the packet.
 
bool tcpSYN () const
 Get the TCP SYN flag in the packet.
 

Static Public Member Functions

static uint16_t IPHeaderChecksum (const void *data, size_t size)
 Compute the checksum of an IPv4 header from raw data.
 
static size_t IPHeaderSize (const void *data, size_t size, uint8_t *protocol=nullptr)
 Get the size in bytes of an IP header from raw data.
 
static bool UpdateIPHeaderChecksum (void *data, size_t size)
 Update the checksum of an IPv4 header as raw data.
 
static bool VerifyIPHeaderChecksum (const void *data, size_t size)
 Verify the checksum of an IPv4 header from raw data.
 

Detailed Description

Representation of a raw IPv4 or IPv6 packet.

Constructor & Destructor Documentation

◆ IPPacket()

ts::IPPacket::IPPacket ( const void *  data,
size_t  size 
)

Constructor from raw content.

Parameters
[in]dataAddress of the IP packet data.
[in]sizeSize of the IP packet data.

Member Function Documentation

◆ reset()

bool ts::IPPacket::reset ( const void *  data,
size_t  size 
)

Reinitialize the IP4 packet with new content.

Parameters
[in]dataAddress of the IP packet data.
[in]sizeSize of the IP packet data.
Returns
True on success, false if the packet is invalid.

◆ isValid()

bool ts::IPPacket::isValid ( ) const
inline

Check if the IPv4 packet is valid.

Returns
True if the packet is valid, false otherwise.

◆ generation()

IP ts::IPPacket::generation ( ) const
inline

Get the current generation of IP addresses.

Returns
The IP generation of the packet. Return IP::Any if the packet is invalid.

◆ protocol()

uint8_t ts::IPPacket::protocol ( ) const
inline

Get the sub-protocol type (TCP, UDP, etc).

Returns
The sub-protocol type, as defined by constants IPv4_PROTO_*.

◆ isTCP()

bool ts::IPPacket::isTCP ( ) const
inline

Check if the packet is a valid TCP packet.

Returns
True if the packet is a valid TCP packet.

◆ isUDP()

bool ts::IPPacket::isUDP ( ) const
inline

Check if the packet is a valid UDP packet.

Returns
True if the packet is a valid UDP packet.

◆ data()

const uint8_t * ts::IPPacket::data ( ) const
inline

Get the address of the IP packet content.

Returns
The address of the IP packet content or a null pointer if the packet is invalid.

◆ size()

size_t ts::IPPacket::size ( ) const
inline

Get the size in bytes of the IP packet content.

Returns
The size in bytes of the IP packet content.

◆ ipHeader()

const uint8_t * ts::IPPacket::ipHeader ( ) const
inline

Get the address of the IP header.

Returns
The address of the IP header or a null pointer if the packet is invalid.

◆ ipHeaderSize()

size_t ts::IPPacket::ipHeaderSize ( ) const
inline

Get the size in bytes of the IP header.

Returns
The size in bytes of the IP header.

◆ protocolHeader()

const uint8_t * ts::IPPacket::protocolHeader ( ) const
inline

Get the address of the sub-protocol header (TCP header, UDP header, etc).

Returns
The address of the sub-protocol header or a null pointer if the packet is invalid.

◆ protocolHeaderSize()

size_t ts::IPPacket::protocolHeaderSize ( ) const
inline

Get the size in bytes of the sub-protocol header (TCP header, UDP header, etc).

Returns
The size in bytes of the sub-protocol header.

◆ protocolData()

const uint8_t * ts::IPPacket::protocolData ( ) const
inline

Get the address of the sub-protocol payload data (TCP data, UDP data, etc).

Returns
The address of the sub-protocol header payload data or a null pointer if the packet is invalid.

◆ protocolDataSize()

size_t ts::IPPacket::protocolDataSize ( ) const
inline

Get the size in bytes of the sub-protocol payload data (TCP data, UDP data, etc).

Returns
The size in bytes of the sub-protocol payload data.

◆ fragmented()

bool ts::IPPacket::fragmented ( ) const

Check if the IP packet is fragmented.

Returns
True if the packet is just a fragment of a larger packet. Only IPv4 packets can be fragmented. Always false on IPv6 packets.

◆ source()

const IPSocketAddress & ts::IPPacket::source ( ) const
inline

Get the source IP socket address.

Returns
A constant reference to the source IP socket address. The port is valid only in the case of UDP or TCP packet.

◆ destination()

const IPSocketAddress & ts::IPPacket::destination ( ) const
inline

Get the destination IP socket address.

Returns
A constant reference to the destination IP socket address. The port is valid only in the case of UDP or TCP packet.

◆ tcpSequenceNumber()

uint32_t ts::IPPacket::tcpSequenceNumber ( ) const

Get the TCP sequence number in the packet.

Returns
The TCP sequence number or zero if this is not a TCP packet.

◆ tcpSYN()

bool ts::IPPacket::tcpSYN ( ) const

Get the TCP SYN flag in the packet.

Returns
The TCP SYN flag.

◆ tcpACK()

bool ts::IPPacket::tcpACK ( ) const

Get the TCP ACK flag in the packet.

Returns
The TCP ACK flag.

◆ tcpRST()

bool ts::IPPacket::tcpRST ( ) const

Get the TCP RST flag in the packet.

Returns
The TCP RST flag.

◆ tcpFIN()

bool ts::IPPacket::tcpFIN ( ) const

Get the TCP FIN flag in the packet.

Returns
The TCP FIN flag.

◆ IPHeaderSize()

static size_t ts::IPPacket::IPHeaderSize ( const void *  data,
size_t  size,
uint8_t *  protocol = nullptr 
)
static

Get the size in bytes of an IP header from raw data.

Parameters
[in]dataAddress of the IP packet.
[in]sizeSize of the IP packet or header (must be larger than the header size).
[out]protocolIf not null, receives the type of the next protocol layer (TCP, UDP, etc).
Returns
The size in bytes of the IP header or zero on error.

◆ IPHeaderChecksum()

static uint16_t ts::IPPacket::IPHeaderChecksum ( const void *  data,
size_t  size 
)
static

Compute the checksum of an IPv4 header from raw data.

The concept of header checksum is specific to IPv4. IPv6 headers do not have checksums.

Parameters
[in]dataAddress of the IP packet.
[in]sizeSize of the IP packet or header (must be larger than the header size).
Returns
The computed checksum of the IPv4 header or zero for IPv6 header.

◆ VerifyIPHeaderChecksum()

static bool ts::IPPacket::VerifyIPHeaderChecksum ( const void *  data,
size_t  size 
)
static

Verify the checksum of an IPv4 header from raw data.

Parameters
[in]dataAddress of the IP packet.
[in]sizeSize of the IP packet or header (must be larger than the header size).
Returns
True if the checksum of the header if correct, false otherwise. Always true for IPv6 headers (there is no header checksum in IPv6).

◆ UpdateIPHeaderChecksum()

static bool ts::IPPacket::UpdateIPHeaderChecksum ( void *  data,
size_t  size 
)
static

Update the checksum of an IPv4 header as raw data.

Parameters
[in,out]dataAddress of the IP packet.
[in]sizeSize of the IP packet or header (must be larger than the header size).
Returns
True if the checksum was updated, false on incorrect buffer. Always true for IPv6 headers (there is no header checksum in IPv6).

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