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-2019 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 1.9.6
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 10
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 //CodeWarrior or Win32 compiler?
67 #if defined(__CWCC__) || defined(_WIN32)
68  #pragma pack(push, 1)
69 #endif
70 
71 
72 /**
73  * @brief UDP header
74  **/
75 
76 typedef __start_packed struct
77 {
78  uint16_t srcPort; //0-1
79  uint16_t destPort; //2-3
80  uint16_t length; //4-5
81  uint16_t checksum; //6-7
82  uint8_t data[]; //8
84 
85 
86 //CodeWarrior or Win32 compiler?
87 #if defined(__CWCC__) || defined(_WIN32)
88  #pragma pack(pop)
89 #endif
90 
91 
92 /**
93  * @brief Data received callback
94  **/
95 
96 typedef void (*UdpRxCallback)(NetInterface *interface, const IpPseudoHeader *pseudoHeader,
97  const UdpHeader *header, const NetBuffer *buffer, size_t offset, void *param);
98 
99 
100 /**
101  * @brief Entry describing a user callback
102  **/
103 
104 typedef struct
105 {
107  uint16_t port;
109  void *param;
111 
112 
113 //Global variables
116 
117 //UDP related functions
118 error_t udpInit(void);
119 uint16_t udpGetDynamicPort(void);
120 
122  IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset);
123 
125  uint16_t destPort, const void *data, size_t length, size_t *written,
126  uint_t flags);
127 
129  uint16_t srcPort, const IpAddr *destIpAddr, uint16_t destPort,
130  NetBuffer *buffer, size_t offset, uint_t flags);
131 
133  IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags);
134 
135 NetBuffer *udpAllocBuffer(size_t length, size_t *offset);
136 
138 
140  uint16_t port, UdpRxCallback callback, void *param);
141 
143 
145  const UdpHeader *header, const NetBuffer *buffer, size_t offset);
146 
147 void udpDumpHeader(const UdpHeader *datagram);
148 
149 //C++ guard
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif
IP network address.
Definition: ip.h:71
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
error_t udpSendDatagramEx(NetInterface *interface, const IpAddr *srcIpAddr, uint16_t srcPort, const IpAddr *destIpAddr, uint16_t destPort, NetBuffer *buffer, size_t offset, uint_t flags)
Send a UDP datagram (raw interface)
Definition: udp.c:463
OsMutex udpCallbackMutex
Definition: udp.c:54
uint16_t checksum
Definition: udp.h:81
Entry describing a user callback.
Definition: udp.h:104
uint16_t destPort
Definition: udp.h:79
Ipv4Addr srcIpAddr
Definition: ipcp.h:77
UdpRxCallbackDesc udpCallbackTable[UDP_CALLBACK_TABLE_SIZE]
Definition: udp.c:56
IP pseudo header.
Definition: ip.h:90
void udpUpdateEvents(Socket *socket)
Update UDP related events.
Definition: udp.c:720
error_t udpDetachRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:811
error_t udpReceiveDatagram(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a UDP socket.
Definition: udp.c:622
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:764
error_t
Error codes.
Definition: error.h:42
void * param
Definition: udp.h:109
error_t udpInit(void)
UDP related initialization.
Definition: udp.c:64
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:108
#define NetInterface
Definition: net.h:36
uint16_t udpGetDynamicPort(void)
Get an ephemeral port number.
Definition: udp.c:89
NetInterface * interface
Definition: tcp.h:375
void udpDumpHeader(const UdpHeader *datagram)
Dump UDP header for debugging purpose.
Definition: udp.c:941
__start_packed struct _Ipv4Header __end_packed
__start_packed struct @122 UdpHeader
UDP header.
UdpRxCallback callback
Definition: udp.h:108
uint16_t length
Definition: udp.h:80
#define UDP_CALLBACK_TABLE_SIZE
Definition: udp.h:48
Mutex object.
uint16_t port
Definition: dns_common.h:223
uint8_t flags
Definition: tcp.h:314
uint16_t port
Definition: udp.h:107
TCP (Transmission Control Protocol)
error_t udpProcessDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming UDP datagram processing.
Definition: udp.c:130
#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:697
NetInterface * interface
Definition: udp.h:106
uint16_t srcPort
Definition: udp.h:78
void(* UdpRxCallback)(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *header, const NetBuffer *buffer, size_t offset, void *param)
Data received callback.
Definition: udp.h:96
error_t udpSendDatagram(Socket *socket, const IpAddr *destIpAddr, uint16_t destPort, const void *data, size_t length, size_t *written, uint_t flags)
Send a UDP datagram.
Definition: udp.c:401
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint8_t data[]
Definition: udp.h:82
error_t udpInvokeRxCallback(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *header, const NetBuffer *buffer, size_t offset)
Invoke user callback.
Definition: udp.c:861
Ipv4Addr destIpAddr
Definition: ipcp.h:78