|
NetBurner 3.5.8
PDF Version |
NetBurner User Datagram Protocol (UDP) Implementation. More...
Topics | |
| UDP Error Codes | |
| UDP Packet API | |
| UDP Socket API | |
| BSD-style socket functions for UDP communication. | |
Classes | |
| class | UDPPacket |
| UDP Packet Class - Complete UDP packet management. More... | |
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. | |
NetBurner User Datagram Protocol (UDP) Implementation.
#include< udp.h>
Complete UDP protocol implementation providing both object-oriented C++ interface (UDPPacket class) and BSD-style socket functions for flexible network communication.
UDP is ideal for applications requiring:
Not suitable for: File transfers, critical data, ordered delivery requirements (use TCP for these scenarios)
The UDPPacket class uses RAII (Resource Acquisition Is Initialization):
Common UDP Ports:
Use SetDSCP() to prioritize traffic in QoS-enabled networks:
DSCP Value | Traffic Class | Use Case
| typedef void udp_data_notify(OS_FIFO *pfifo, uint16_t port) |
#include <udp.h>
UDP packet arrival notification callback function type.
Defines the function signature for callbacks that are invoked when a UDP packet arrives on a registered port. The callback is executed in interrupt context, allowing immediate notification of packet arrival without polling.
Use this callback type when registering a UDP FIFO with notification functions like RegisterUDPFifoWithNotify() or RegisterUDPFifoWithNotifyVia().
| pfifo | Pointer to the OS_FIFO where the packet will be queued. This is the same FIFO that was registered with the port. |
| port | The UDP port number on which the packet was received. Useful when the same callback handles multiple ports. |
Expand for Example Usage
Example implementation:
Example with task notification:
Example with multiple ports:
| uint16_t RegisterEphemeralFifo | ( | OS_FIFO * | pfifo, |
| int | ifn = -1 ) |
#include <udp.h>
Register UDP FIFO on a random unused ephemeral port.
Automatically selects and registers an available UDP port from the ephemeral port range (typically 49152-65535). This is particularly useful for client applications that need a source port for receiving replies but don't require a specific port number.
The function ensures the selected port is not already in use and associates it with the provided FIFO for packet queuing. This is the recommended approach for client-side UDP communication where the remote server will reply to the source port.
| pfifo | Pointer to an OS_FIFO structure for queuing received packets. The FIFO must be initialized before calling this function. Recommended queue depth: 5-20 packets depending on traffic volume. |
| ifn | Network interface number to bind to. Use -1 for default interface (recommended). Specify a specific interface (0, 1, 2, etc.) to receive only on that interface. |
Expand for Example Usage
Example (multiple concurrent requests):
| bool RegisterUDPFifo | ( | uint16_t | listenPort, |
| OS_FIFO * | pFifo ) |
#include <udp.h>
Register FIFO to receive UDP packets on specified port.
Registers a UDP port for receiving packets and associates it with a FIFO queue for packet buffering. This is the standard method for setting up UDP reception on NetBurner devices. Incoming packets are automatically queued to the FIFO and can be retrieved using ReadUDPPacket().
The FIFO depth determines how many packets can be buffered before new packets are dropped. Choose a depth based on your expected traffic volume and processing speed. A depth of 5-10 is typical for low-traffic applications, while 20+ may be needed for high-traffic scenarios.
| listenPort | UDP port number to register (1-65535). Common ports: 80 (HTTP), 443 (HTTPS), 161 (SNMP), etc. Must not already be registered. Port becomes unavailable to other applications. |
| pFifo | Pointer to an initialized OS_FIFO structure for queuing received packets. Must be initialized with OSFifoInit() before calling this function. The FIFO depth should match your traffic expectations (typically 5-20). The FIFO must remain valid until UnregisterUDPFifo() is called. |
Expand for Example Usage
Basic example:
Example with custom FIFO depth:
Example with proper cleanup:
Example - multiple ports:
Example - error handling:
| bool RegisterUDPFifoVia | ( | uint16_t | listenPort, |
| OS_FIFO * | pFifo, | ||
| int | interface ) |
#include <udp.h>
Register FIFO to receive UDP packets on specified port via specific network interface.
Similar to RegisterUDPFifo(), but allows explicit selection of which network interface to listen on. This is essential for multi-homed systems (devices with multiple network connections) where you need to receive UDP packets on a specific interface only, such as separating management traffic from data traffic, or implementing failover systems.
Use this function when your device has multiple active network interfaces (Ethernet, WiFi, cellular, etc.) and you need to control which interface receives UDP traffic on a given port. The same port number can be registered on different interfaces simultaneously.
| listenPort | UDP port number to register (1-65535). Must not already be registered on the specified interface. The same port can be registered on different interfaces. |
| pFifo | Pointer to an initialized OS_FIFO structure for queuing received packets. Must be initialized with OSFifoInit() before calling. Recommended depth: 5-20 packets depending on traffic. The FIFO must remain valid until UnregisterUDPFifo() is called. |
| interface | Network interface number (0-based index):
|
Expand for Example Usage
Example (dual-homed device - management on eth0, data on eth1):
Example (same port on multiple interfaces):
Example (interface availability checking):
Example (failover system):
Example (WiFi vs Ethernet separation):
| bool RegisterUDPFifoWithNotify | ( | uint16_t | listenPort, |
| OS_FIFO * | pFifo, | ||
| udp_data_notify * | pNotifyFunction ) |
#include <udp.h>
Register UDP FIFO with callback notification on packet arrival.
Registers a UDP port for receiving packets and associates it with both a FIFO queue and a notification callback function. When a packet arrives, the callback is invoked immediately (typically from an interrupt context), allowing for prompt handling of time-sensitive data without polling.
The notification function is called before the packet is queued to the FIFO, enabling priority processing or filtering. This is useful for protocols requiring immediate response or for implementing custom packet filtering logic.
| listenPort | UDP port number to register (1-65535). Must not already be registered. |
| pFifo | Pointer to an OS_FIFO structure for queuing packets. The FIFO must be initialized before calling this function. Can be NULL if you only want notification callbacks. |
| pNotifyFunction | Pointer to callback function invoked when packet arrives. Function signature: void callback(uint16_t port, uint8_t* data, int len, IPADDR& srcAddr) Called in interrupt context - keep processing minimal and fast. Can be NULL if you only want FIFO queuing without notification. |
Expand for Example Usage
Example (immediate response protocol):
Example (packet filtering and statistics):
Example (notification only, no FIFO):
| bool RegisterUDPFifoWithNotifyVia | ( | uint16_t | listenPort, |
| OS_FIFO * | pFifo, | ||
| udp_data_notify * | pNotifyFunction, | ||
| int | interface ) |
#include <udp.h>
Register UDP FIFO with callback notification on specific network interface.
Similar to RegisterUDPFifoWithNotify(), but allows explicit selection of which network interface to listen on. This is essential for multi-homed systems (devices with multiple network connections) where you need to receive UDP packets on a specific interface only.
Use this function when your device has multiple network interfaces (Ethernet, WiFi, etc.) and you need to isolate UDP traffic by interface. For example, listening for management traffic only on the Ethernet interface while ignoring similar packets from WiFi.
| listenPort | UDP port number to register (1-65535). Must not already be registered on the specified interface. |
| pFifo | Pointer to an OS_FIFO structure for queuing packets. Must be initialized before calling. Can be NULL for notification-only. |
| pNotifyFunction | Pointer to callback function invoked when packet arrives. Function signature: void callback(uint16_t port, uint8_t* data, int len, IPADDR& srcAddr) Called in interrupt context. Can be NULL for FIFO-only operation. |
| interface | Network interface number (0-based):
|
Expand for Example Usage
Example (dual-homed device - different ports per interface):
Example (management vs. data network separation):
Example (interface-specific notification):
Example (checking interface availability first):
| void UnregisterUDPFifo | ( | uint16_t | listenPort, |
| bool | drain = false ) |
#include <udp.h>
Unregister UDP FIFO and stop receiving packets on specified port.
Stops listening for UDP packets on the specified port and removes the associated FIFO queue from the system. This function should be called when a UDP port is no longer needed to free system resources.
If the FIFO has queued packets waiting to be processed, the drain parameter controls whether these packets are discarded or left in the queue. Typically, you should drain the queue unless you have already processed all packets.
| listenPort | The UDP port number to unregister (must match a previously registered port) |
| drain | If true, frees all queued packets in the FIFO and releases their memory. If false, leaves queued packets in memory (default: false). In most cases, you should set this to true to prevent memory leaks. |
Expand for Example Usage
Example (typical cleanup):
Example (graceful shutdown - process remaining packets first):
Example (application shutdown):