NetBurner 3.5.0
PDF Version
 
UDPPacket Class Reference

UDP Packet Class. More...

#include <udp.h>

Public Member Functions

 UDPPacket (OS_FIFO *pFifo, TickTimeout timeout)
 Constructor to create a UDP Packet object from a UDP FIFO entry.
 
 UDPPacket (int sock)
 Constructor to create a UDP Packet object from an open UDP socket.
 
 UDPPacket (PoolPtr p)
 Constructor to create a UDP packet from a system pool buffer.
 
 UDPPacket (UDPPacket &pkt)
 Constructor to create a new UDP packet from an existing UDP packet.
 
 ~UDPPacket ()
 UDP packet object destructor. Frees any associated memory.
 
void SetSourcePort (uint16_t port)
 Set the source port number of a UDP Packet object.
 
uint16_t GetSourcePort (void) const
 Get the source port number of a UDP Packet object.
 
MACADR GetMacSource ()
 Get the source MAC address a UDP Packet object.
 
IPADDR GetSourceAddress (void)
 Get the source IP address a UDP Packet object.
 
IPADDR GetDestinationAddress (void)
 Get the destination IP address a UDP Packet object.
 
bool bIsIPV6 ()
 Check if the IPADDR holds an IPv6 IP address.
 
void SetDestinationPort (uint16_t port)
 Set the destination port number of a UDP Packet object.
 
uint16_t GetDestinationPort (void) const
 Get the destination port number of a UDP Packet object.
 
uint16_t GetPacketId (void)
 Get UDP packet ID.
 
puint8_t GetDataBuffer (bool bReAllocateIfNeeded=false)
 Get a pointer to the UDP Packet object's data buffer.
 
void SetDataSize (uint16_t numBytes)
 Set the UDP Packet data size.
 
uint16_t GetDataSize (void) const
 Get the UDP Packet object data size.
 
void AddData (puint8_t pData, uint16_t len)
 Add a number of data bytes to a UDP Packet object.
 
void AddData (PCSTR pData)
 Add data to a UDP Packet object as a NULL terminated ASCII string.
 
void AddDataWord (uint16_t w)
 Add a 16-bit unsigned integer to a UDP Packet object.
 
void AddDataByte (uint8_t b)
 Add an 8-bit unsigned integer to a UDP Packet object.
 
BOOL Validate (void)
 Verify a received UDP packet is valid.
 
void ResetData (void)
 Set the data size of a UDP Packet object to 0.
 
void SendAndKeep (const IPADDR &to, uint8_t ttl=0)
 Make a copy of a UDP Packet and send it. The original packet will remain intact.
 
void Send (const IPADDR &to, uint8_t ttl=0)
 Send the UDP Packet and free the pool buffer.
 
void SendAndKeepViaInterfaceNum (const IPADDR &to, int interface, uint8_t ttl=0)
 Make a copy of a UDP Packet and send it using the specified network interface. The original packet will remain intact.
 
void SendViaInterfaceNum (const IPADDR &to, int interface, uint8_t ttl=0)
 Send the UDP Packet using the specified network interface and free the pool buffer.
 
void SendAndKeepViaIfAddr (const IPADDR &to, const IPADDR &from_ip, uint8_t ttl=0)
 Make a copy of a UDP Packet and send it through the network interface specified by the from_ip IP address parameter. If more than one interface has the same IP address, the lower interface number will be used. The original packet will remain intact.
 
void SendViaIfAddr (const IPADDR &to, const IPADDR &from_ip, uint8_t ttl=0)
 Send a UDP packet through the network interface specified by the from_ip IP address parameter. If more than one interface has the same IP address, the lower interface number will be used. The UDP pool buffer will be freed.
 

Detailed Description

UDP Packet Class.

This class holds PoolBuffers and treats them as UDP Packets. The process for sending a UDP packet is as follows:

UDPPacket myUdpPacket; // Create a UDPPacket object
myUdpPacket.SetSourcePort(123); // Set the source port number
myUdpPacket.SetDestinatioPort(456); // Set destination port number
UDP Packet Class.
Definition udp.h:81
void SetSourcePort(uint16_t port)
Set the source port number of a UDP Packet object.

Data can be put in the packet two ways:

  1. Obtain a pointer to the object's data buffer and copy the data in. Be certain to set the data length.
    uint16_t len = siprintf( myUdpPacket.GetDataBuffer(), "Using sprintf() to copy data at time = %ld", TickCount );
    myUdpPacket.SetDataSize(len);
    void SetDataSize(uint16_t numBytes)
    Set the UDP Packet data size.
    puint8_t GetDataBuffer(bool bReAllocateIfNeeded=false)
    Get a pointer to the UDP Packet object's data buffer.
  2. Copy the data directly to the packet buffer:
    AddData("This is the data to add"); // Add a constant ASCII null terminated string
    AddData(pData, len); // Add with a pointer to the data and the data length
    void AddData(puint8_t pData, uint16_t len)
    Add a number of data bytes to a UDP Packet object.

When the UDP packet has been configured it can be sent two ways:

  1. Send and keep the packet and keep the constructed pool buffer: SendAndKeep(IPADDR destinationIP);. You must free the buffer manually.
  2. Send and automatically free the pool buffer (recommended and more efficient): Send(IPADDR4 destinationIP);

There are many UDP examples, and we recommend reviewing them to determine the best method to use for your application.

Constructor & Destructor Documentation

◆ UDPPacket() [1/4]

UDPPacket::UDPPacket ( OS_FIFO * pFifo,
TickTimeout timeout )

Constructor to create a UDP Packet object from a UDP FIFO entry.

UDP packets are received in a OS_FIFO. This constructor create a UDP packet from the next entry in the FIFO, which also removes the entry from the FIFO. If there is not a packet in the FIFO, this constructor will block until one is available, or the specified timeout occurs. If a timeout occurs, an invalid UDP packet will be created. The UDP Validate() function must always be called after this constructor to verify a valid UDP packet has been created.

Parameters
pFifoPointer to an OS_FIFO object
timeoutTimeout in system Time Ticks
See also
UDPPacket(int sock), UDPPacket( PoolPtr p )

◆ UDPPacket() [2/4]

UDPPacket::UDPPacket ( int sock)

Constructor to create a UDP Packet object from an open UDP socket.

This constructor will block until a UDP packet is received. It is useful for situations such as a select() call that will block on a UDP file descriptor until a packet is received. After the select() returns, this constructor can be used to create the packet.

Parameters
sockOpen UDP socket
See also
UDPPacket( OS_FIFO *pFifo, uint32_t timeout ), UDPPacket( PoolPtr p )

◆ UDPPacket() [3/4]

UDPPacket::UDPPacket ( PoolPtr p)

Constructor to create a UDP packet from a system pool buffer.

This constructor can be used in unique situations in which an application is operating directly on the system pool buffers. This is a rare situation.

Parameters
pPointer to a pool buffer
See also
UDPPacket( OS_FIFO *pFifo, uint32_t timeout ), UDPPacket(int sock)

◆ UDPPacket() [4/4]

UDPPacket::UDPPacket ( UDPPacket & pkt)

Constructor to create a new UDP packet from an existing UDP packet.

The UDP packet passed in the argument will be destroyed.

Parameters
pktUDP packet object
See also
UDPPacket( OS_FIFO *pFifo, uint32_t timeout ), UDPPacket(int sock)

◆ ~UDPPacket()

UDPPacket::~UDPPacket ( )

UDP packet object destructor. Frees any associated memory.

See also
UDPPacket( OS_FIFO *pFifo, uint32_t timeout ), UDPPacket(int sock), UDPPacket( PoolPtr p )

Member Function Documentation

◆ AddData() [1/2]

void UDPPacket::AddData ( PCSTR pData)

Add data to a UDP Packet object as a NULL terminated ASCII string.

Parameters
pDataPointer to the null terminated string
See also
AddDataByte(), AddDataWord()

◆ AddData() [2/2]

void UDPPacket::AddData ( puint8_t pData,
uint16_t len )

Add a number of data bytes to a UDP Packet object.

Parameters
pDataPointer to the data to add
lenNumber of bytes
See also
AddDataByte(), AddDataWord()

◆ AddDataByte()

void UDPPacket::AddDataByte ( uint8_t b)

Add an 8-bit unsigned integer to a UDP Packet object.

Parameters
b8-bit unsigned integer
See also
AddDataWord(), AddData()

◆ AddDataWord()

void UDPPacket::AddDataWord ( uint16_t w)

Add a 16-bit unsigned integer to a UDP Packet object.

Parameters
w16 bit unsigned integer
See also
AddDataByte(), AddData()

◆ bIsIPV6()

bool UDPPacket::bIsIPV6 ( )
inline

Check if the IPADDR holds an IPv6 IP address.

Returns
true if the IP address is an IPv6 address

◆ GetDataBuffer()

puint8_t UDPPacket::GetDataBuffer ( bool bReAllocateIfNeeded = false)

Get a pointer to the UDP Packet object's data buffer.

Returns
Pointer to the data buffer

◆ GetDataSize()

uint16_t UDPPacket::GetDataSize ( void ) const

Get the UDP Packet object data size.

Returns
The data size as number of bytes
See also
SetDataSize()

◆ GetDestinationAddress()

IPADDR UDPPacket::GetDestinationAddress ( void )
inline

Get the destination IP address a UDP Packet object.

Returns
The destination IP address

◆ GetDestinationPort()

uint16_t UDPPacket::GetDestinationPort ( void ) const

Get the destination port number of a UDP Packet object.

Returns
The destination port number
See also
SetDestinationPort()

◆ GetMacSource()

MACADR UDPPacket::GetMacSource ( )

Get the source MAC address a UDP Packet object.

Returns
The source MAC address

◆ GetPacketId()

uint16_t UDPPacket::GetPacketId ( void )

Get UDP packet ID.

Returns
The UDP packet ID

◆ GetSourceAddress()

IPADDR UDPPacket::GetSourceAddress ( void )
inline

Get the source IP address a UDP Packet object.

Returns
The source IP address

◆ GetSourcePort()

uint16_t UDPPacket::GetSourcePort ( void ) const

Get the source port number of a UDP Packet object.

Returns
The source port number
See also
SetSourcePort()

◆ Send()

void UDPPacket::Send ( const IPADDR & to,
uint8_t ttl = 0 )
inline

Send the UDP Packet and free the pool buffer.

Parameters
toDestination IP address
ttlOptional. If not specified the system default will be used
See also
SendAndKeep(), SendViaInterfaceNum(), SendViaIfAddr()

◆ SendAndKeep()

void UDPPacket::SendAndKeep ( const IPADDR & to,
uint8_t ttl = 0 )
inline

Make a copy of a UDP Packet and send it. The original packet will remain intact.

Parameters
toDestination IP address
ttlOptional. If not specified the system default will be used
See also
Send(), SendViaInterfaceNum(), SendAndKeepViaIfAddr()

◆ SendAndKeepViaIfAddr()

void UDPPacket::SendAndKeepViaIfAddr ( const IPADDR & to,
const IPADDR & from_ip,
uint8_t ttl = 0 )
inline

Make a copy of a UDP Packet and send it through the network interface specified by the from_ip IP address parameter. If more than one interface has the same IP address, the lower interface number will be used. The original packet will remain intact.

Parameters
toDestination IP address
from_ipIP address to identify the local network interface as the source
ttlOptional. If not specified the system default will be used
See also
Send(), SendViaInterfaceNum()

◆ SendAndKeepViaInterfaceNum()

void UDPPacket::SendAndKeepViaInterfaceNum ( const IPADDR & to,
int interface,
uint8_t ttl = 0 )
inline

Make a copy of a UDP Packet and send it using the specified network interface. The original packet will remain intact.

Parameters
toDestination IP address
interfaceInterface number
ttlOptional. If not specified the system default will be used
See also
Send(), SendAndKeep(), SendViaInterfaceNum(), SendAndKeepViaIfAddr()

◆ SendViaIfAddr()

void UDPPacket::SendViaIfAddr ( const IPADDR & to,
const IPADDR & from_ip,
uint8_t ttl = 0 )
inline

Send a UDP packet through the network interface specified by the from_ip IP address parameter. If more than one interface has the same IP address, the lower interface number will be used. The UDP pool buffer will be freed.

Parameters
toDestination IP address
from_ipIP address to identify the local network interface as the source
ttlOptional. If not specified the system default will be used
See also
Send(), SendAndKeep(), SendAndKeepViaIfAddr()

◆ SendViaInterfaceNum()

void UDPPacket::SendViaInterfaceNum ( const IPADDR & to,
int interface,
uint8_t ttl = 0 )
inline

Send the UDP Packet using the specified network interface and free the pool buffer.

Parameters
toDestination IP address
interfaceInterface number
ttlOptional. If not specified the system default will be used
See also
Send(), SendAndKeep(), SendAndKeepViaIfAddr()

◆ SetDataSize()

void UDPPacket::SetDataSize ( uint16_t numBytes)

Set the UDP Packet data size.

Parameters
numBytesData size as number of bytes
See also
GetDataSize()

◆ SetDestinationPort()

void UDPPacket::SetDestinationPort ( uint16_t port)

Set the destination port number of a UDP Packet object.

Parameters
portDestination port number
See also
GetDestinationPort()

◆ SetSourcePort()

void UDPPacket::SetSourcePort ( uint16_t port)

Set the source port number of a UDP Packet object.

Parameters
portSource port number
See also
GetSourcePort()

◆ Validate()

BOOL UDPPacket::Validate ( void )

Verify a received UDP packet is valid.

Verifies a received UDP packet has data and validates the checksum. This function should be called anytime a UDP packet is received.

Returns
true if packet is valid, otherwise false

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