NetBurner 3.5.6
PDF Version
udp.h File Reference

NetBurner User Datagram Protocol (UDP) Comprehensive API. More...

#include <predef.h>
#include <ip.h>

Go to the source code of this file.

Classes

class  UDPPacket
 UDP Packet Class - Complete UDP packet management. More...
 

Macros

#define UDP_ERR_NOSUCH_SOCKET   (-1)
 Socket does not exist.
 
#define UDP_ERR_NOTOPEN_TO_WRITE   (-2)
 Socket not open for write.
 
#define UDP_ERR_NOTOPEN_TO_READ   (-3)
 Socket not open for read.
 

Typedefs

typedef void udp_data_notify(OS_FIFO *pfifo, uint16_t port)
 UDP packet arrival notification callback function type.
 

Functions

bool RegisterUDPFifo (uint16_t listenPort, OS_FIFO *pFifo)
 Register FIFO to receive UDP packets on specified port.
 
bool RegisterUDPFifoVia (uint16_t listenPort, OS_FIFO *pFifo, int interface)
 Register FIFO to receive UDP packets on specified port via specific network interface.
 
uint16_t RegisterEphemeralFifo (OS_FIFO *pfifo, int ifn=-1)
 Register UDP FIFO on a random unused ephemeral port.
 
bool RegisterUDPFifoWithNotify (uint16_t listenPort, OS_FIFO *pFifo, udp_data_notify *pNotifyFunction)
 Register UDP FIFO with callback notification on packet arrival.
 
bool RegisterUDPFifoWithNotifyVia (uint16_t listenPort, OS_FIFO *pFifo, udp_data_notify *pNotifyFunction, int interface)
 Register UDP FIFO with callback notification on specific network interface.
 
void UnregisterUDPFifo (uint16_t listenPort, bool drain=false)
 Unregister UDP FIFO and stop receiving packets on specified port.
 
int CreateRxUdpSocket (uint16_t listening_port)
 Create receive-only UDP socket bound to specified port.
 
int CreateTxUdpSocket (const IPADDR &send_to_addr, uint16_t remote_port, uint16_t local_port)
 Create transmit-only UDP socket for sending to specified destination.
 
int CreateRxTxUdpSocket (const IPADDR &send_to_addr, uint16_t send_to_remote_port, uint16_t local_port)
 Create bidirectional UDP socket for both sending and receiving.
 
int sendto (int sock, puint8_t what_to_send, int len_to_send, const IPADDR &to_addr, uint16_t remote_port)
 Send UDP packet through socket to specified destination.
 
int sendtovia (int sock, puint8_t what_to_send, int len_to_send, const IPADDR &to_addr, uint16_t remote_port, int intfnum)
 Send via specific interface.
 
int recvfrom (int sock, puint8_t buffer, int len, IPADDR *pAddr, uint16_t *pLocal_port, uint16_t *pRemote_port)
 Receive UDP packet from socket with sender information.
 
int SendFragmentedUdpPacket (const IPADDR &to, uint16_t source_port, uint16_t dest_port, puint8_t data, int length)
 Send large UDP packet (>MTU) with IP fragmentation.
 

Detailed Description

NetBurner User Datagram Protocol (UDP) Comprehensive API.

This header provides a complete UDP implementation for NetBurner embedded devices, supporting both IPv4 and IPv6 protocols with dual API design for maximum flexibility.

Dual API Architecture

1. Object-Oriented C++ Interface (UDPPacket Class)

  • Automatic memory management via RAII principles
  • Direct buffer access for zero-copy operations
  • Flexible send options (send-and-free vs send-and-keep)
  • Quality of Service (QoS) control through DSCP
  • Multi-interface routing support

2. BSD-Style Socket Functions

  • Familiar sendto()/recvfrom() interface
  • File descriptor support for select()/poll()
  • Standard socket creation patterns
  • Interface-specific binding

Quick Start Examples

Sending UDP Packets

UDPPacket packet;
packet.SetSourcePort(5000);
packet.SetDestinationPort(6000);
uint16_t len = sprintf(packet.GetDataBuffer(), "Sensor: %d", value);
packet.SetDataSize(len);
packet.Send(destinationIP); // Automatically frees buffer
UDP Packet Class - Complete UDP packet management.
Definition udp.h:584
void Send(const IPADDR &to, uint8_t ttl=0)
Send and free buffer (recommended - most efficient)
Definition udp.h:3373
void SetDestinationPort(uint16_t port)
Set destination port (standard services: 53=DNS, 80=HTTP, 123=NTP, 161=SNMP)
void SetSourcePort(uint16_t port)
Set source port (0=auto-assign ephemeral port, 1024-65535=specific port)
void SetDataSize(uint16_t numBytes)
Set data size (REQUIRED after GetDataBuffer() writes)
puint8_t GetDataBuffer(bool bReAllocateIfNeeded=false)
Get data buffer pointer for reading or writing.

Receiving UDP Packets

OS_FIFO udpFifo;
OSFifoInit(&udpFifo);
RegisterUDPFifo(1234, &udpFifo);
UDPPacket rxPacket(&udpFifo, TICKS_PER_SECOND * 5);
if (rxPacket.Validate()) {
puint8_t data = rxPacket.GetDataBuffer();
uint16_t len = rxPacket.GetDataSize();
// Process data...
}
#define TICKS_PER_SECOND
System clock ticks per second.
Definition constants.h:49
uint8_t OSFifoInit(OS_FIFO *pFifo)
Initialize a FIFO, which is used to pass structures from one task to another.
Definition nbrtos.h:2214
bool RegisterUDPFifo(uint16_t listenPort, OS_FIFO *pFifo)
Register FIFO to receive UDP packets on specified port.
Definition nbrtos.h:932