udp.h
Go to the documentation of this file.
1 /**
2  * @file udp.h
3  * @brief UDP (User Datagram Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 #ifndef _UDP_H
32 #define _UDP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/tcp.h"
37 
38 //UDP support
39 #ifndef UDP_SUPPORT
40  #define UDP_SUPPORT ENABLED
41 #elif (UDP_SUPPORT != ENABLED && UDP_SUPPORT != DISABLED)
42  #error UDP_SUPPORT parameter is not valid
43 #endif
44 
45 //Maximum number of callback functions that can be registered
46 //to process incoming UDP datagrams
47 #ifndef UDP_CALLBACK_TABLE_SIZE
48  #define UDP_CALLBACK_TABLE_SIZE 20
49 #elif (UDP_CALLBACK_TABLE_SIZE < 1)
50  #error UDP_CALLBACK_TABLE_SIZE parameter is not valid
51 #endif
52 
53 //Receive queue depth for connectionless sockets
54 #ifndef UDP_RX_QUEUE_SIZE
55  #define UDP_RX_QUEUE_SIZE 4
56 #elif (UDP_RX_QUEUE_SIZE < 1)
57  #error UDP_RX_QUEUE_SIZE parameter is not valid
58 #endif
59 
60 //C++ guard
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
66 //CC-RX, CodeWarrior or Win32 compiler?
67 #if defined(__CCRX__)
68  #pragma pack
69 #elif defined(__CWCC__) || defined(_WIN32)
70  #pragma pack(push, 1)
71 #endif
72 
73 
74 /**
75  * @brief UDP header
76  **/
77 
79 {
80  uint16_t srcPort; //0-1
81  uint16_t destPort; //2-3
82  uint16_t length; //4-5
83  uint16_t checksum; //6-7
84  uint8_t data[]; //8
86 
87 
88 //CC-RX, CodeWarrior or Win32 compiler?
89 #if defined(__CCRX__)
90  #pragma unpack
91 #elif defined(__CWCC__) || defined(_WIN32)
92  #pragma pack(pop)
93 #endif
94 
95 
96 /**
97  * @brief UDP receive callback
98  **/
99 
100 typedef void (*UdpRxCallback)(NetInterface *interface,
101  const IpPseudoHeader *pseudoHeader, const UdpHeader *header,
102  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
103  void *param);
104 
105 
106 /**
107  * @brief UDP receive callback entry
108  **/
109 
110 typedef struct
111 {
113  uint16_t port;
115  void *param;
117 
118 
119 //Global variables
121 
122 //UDP related functions
123 error_t udpInit(NetContext *context);
124 uint16_t udpGetDynamicPort(NetContext *context);
125 
127  const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset,
128  const NetRxAncillary *ancillary);
129 
131 
132 error_t udpSendBuffer(NetContext *context, NetInterface *interface,
133  const IpAddr *srcIpAddr, uint16_t srcPort, const IpAddr *destIpAddr,
134  uint16_t destPort, NetBuffer *buffer, size_t offset,
135  NetTxAncillary *ancillary);
136 
138 
139 NetBuffer *udpAllocBuffer(size_t length, size_t *offset);
140 
142 
143 error_t udpRegisterRxCallback(NetInterface *interface, uint16_t port,
144  UdpRxCallback callback, void *param);
145 
146 error_t udpUnregisterRxCallback(NetInterface *interface, uint16_t port);
147 
149  const IpPseudoHeader *pseudoHeader, const UdpHeader *header,
150  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary);
151 
152 void udpDumpHeader(const UdpHeader *datagram);
153 
154 //C++ guard
155 #ifdef __cplusplus
156 }
157 #endif
158 
159 #endif
#define NetContext
Definition: net.h:36
IP network address.
Definition: ip.h:90
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
error_t udpSendDatagram(Socket *socket, const SocketMsg *message, uint_t flags)
Send a UDP datagram.
Definition: udp.c:514
Message and ancillary data.
Definition: socket.h:241
uint16_t checksum
Definition: udp.h:83
uint16_t port
Definition: udp.h:113
uint16_t destPort
Definition: udp.h:81
Ipv4Addr srcIpAddr
Definition: ipcp.h:79
error_t udpInvokeRxCallback(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *header, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Invoke user callback.
Definition: udp.c:1108
error_t udpRegisterRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1021
error_t udpUnregisterRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
error_t udpInit(NetContext *context)
UDP related initialization.
Definition: udp.c:61
IP pseudo header.
Definition: ip.h:110
void udpUpdateEvents(Socket *socket)
Update UDP related events.
Definition: udp.c:971
void * param
Definition: udp.h:115
void(* UdpRxCallback)(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *header, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary, void *param)
UDP receive callback.
Definition: udp.h:100
error_t
Error codes.
Definition: error.h:43
UdpRxCallback callback
Definition: udp.h:114
error_t udpReceiveDatagram(Socket *socket, SocketMsg *message, uint_t flags)
Receive data from a UDP socket.
Definition: udp.c:843
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
UDP receive callback entry.
Definition: udp.h:111
NetInterface * interface
Definition: udp.h:112
error_t udpProcessDatagram(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Incoming UDP datagram processing.
Definition: udp.c:123
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
void udpDumpHeader(const UdpHeader *datagram)
Dump UDP header for debugging purpose.
Definition: udp.c:1171
uint16_t udpGetDynamicPort(NetContext *context)
Get an ephemeral port number.
Definition: udp.c:80
#define NetTxAncillary
Definition: net_misc.h:36
error_t udpSendBuffer(NetContext *context, NetInterface *interface, const IpAddr *srcIpAddr, uint16_t srcPort, const IpAddr *destIpAddr, uint16_t destPort, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a UDP datagram.
Definition: udp.c:657
uint16_t length
Definition: udp.h:82
#define UDP_CALLBACK_TABLE_SIZE
Definition: udp.h:48
UdpHeader
Definition: udp.h:85
uint16_t port
Definition: dns_common.h:270
typedef __packed_struct
UDP header.
Definition: udp.h:79
TCP (Transmission Control Protocol)
#define Socket
Definition: socket.h:36
NetBuffer * udpAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold a UDP packet.
Definition: udp.c:948
UdpRxCallbackEntry udpCallbackTable[UDP_CALLBACK_TABLE_SIZE]
Definition: udp.c:52
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
uint8_t data[]
Definition: udp.h:84
Ipv4Addr destIpAddr
Definition: ipcp.h:80