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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _UDP_H
30 #define _UDP_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "core/tcp.h"
35 
36 //UDP support
37 #ifndef UDP_SUPPORT
38  #define UDP_SUPPORT ENABLED
39 #elif (UDP_SUPPORT != ENABLED && UDP_SUPPORT != DISABLED)
40  #error UDP_SUPPORT parameter is not valid
41 #endif
42 
43 //Maximum number of callback functions that can be registered
44 //to process incoming UDP datagrams
45 #ifndef UDP_CALLBACK_TABLE_SIZE
46  #define UDP_CALLBACK_TABLE_SIZE 10
47 #elif (UDP_CALLBACK_TABLE_SIZE < 1)
48  #error UDP_CALLBACK_TABLE_SIZE parameter is not valid
49 #endif
50 
51 //Receive queue depth for connectionless sockets
52 #ifndef UDP_RX_QUEUE_SIZE
53  #define UDP_RX_QUEUE_SIZE 4
54 #elif (UDP_RX_QUEUE_SIZE < 1)
55  #error UDP_RX_QUEUE_SIZE parameter is not valid
56 #endif
57 
58 //C++ guard
59 #ifdef __cplusplus
60  extern "C" {
61 #endif
62 
63 
64 //CodeWarrior or Win32 compiler?
65 #if defined(__CWCC__) || defined(_WIN32)
66  #pragma pack(push, 1)
67 #endif
68 
69 
70 /**
71  * @brief UDP header
72  **/
73 
74 typedef __start_packed struct
75 {
76  uint16_t srcPort; //0-1
77  uint16_t destPort; //2-3
78  uint16_t length; //4-5
79  uint16_t checksum; //6-7
80  uint8_t data[]; //8
82 
83 
84 //CodeWarrior or Win32 compiler?
85 #if defined(__CWCC__) || defined(_WIN32)
86  #pragma pack(pop)
87 #endif
88 
89 
90 /**
91  * @brief Data received callback
92  **/
93 
94 typedef void (*UdpRxCallback)(NetInterface *interface, const IpPseudoHeader *pseudoHeader,
95  const UdpHeader *header, const NetBuffer *buffer, size_t offset, void *param);
96 
97 
98 /**
99  * @brief Entry describing a user callback
100  **/
101 
102 typedef struct
103 {
105  uint16_t port;
107  void *param;
109 
110 
111 //Global variables
114 
115 //UDP related functions
116 error_t udpInit(void);
117 uint16_t udpGetDynamicPort(void);
118 
120  IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset);
121 
123  uint16_t destPort, const void *data, size_t length, size_t *written);
124 
126  uint16_t destPort, NetBuffer *buffer, size_t offset, uint8_t ttl);
127 
129  IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags);
130 
131 NetBuffer *udpAllocBuffer(size_t length, size_t *offset);
132 
134 
136  uint16_t port, UdpRxCallback callback, void *param);
137 
139 
141  const UdpHeader *header, const NetBuffer *buffer, size_t offset);
142 
143 void udpDumpHeader(const UdpHeader *datagram);
144 
145 //C++ guard
146 #ifdef __cplusplus
147  }
148 #endif
149 
150 #endif
NetInterface * interface
Definition: tcp.h:373
uint16_t destPort
Definition: udp.h:77
error_t udpDetachRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:770
uint8_t flags
Definition: tcp.h:312
uint16_t checksum
Definition: udp.h:79
TCP/IP stack core.
error_t udpSendDatagram(Socket *socket, const IpAddr *destIpAddr, uint16_t destPort, const void *data, size_t length, size_t *written)
Send a UDP datagram.
Definition: udp.c:397
uint8_t data[]
Definition: udp.h:80
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:581
uint16_t length
Definition: udp.h:78
uint16_t udpGetDynamicPort(void)
Get an ephemeral port number.
Definition: udp.c:86
IP network address.
Definition: ip.h:57
error_t udpInvokeRxCallback(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *header, const NetBuffer *buffer, size_t offset)
Invoke user callback.
Definition: udp.c:820
void udpUpdateEvents(Socket *socket)
Update UDP related events.
Definition: udp.c:679
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:106
UdpRxCallback callback
Definition: udp.h:106
#define UDP_CALLBACK_TABLE_SIZE
Definition: udp.h:46
#define Socket
Definition: socket.h:34
void(* UdpRxCallback)(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *header, const NetBuffer *buffer, size_t offset, void *param)
Data received callback.
Definition: udp.h:94
uint32_t ttl
Definition: dns_common.h:203
void udpDumpHeader(const UdpHeader *datagram)
Dump UDP header for debugging purpose.
Definition: udp.c:900
__start_packed struct @126 UdpHeader
UDP header.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
error_t udpProcessDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming UDP datagram processing.
Definition: udp.c:127
OsMutex udpCallbackMutex
Definition: udp.c:51
void * param
Definition: udp.h:107
Ipv4Addr srcIpAddr
Definition: ipcp.h:75
uint16_t srcPort
Definition: udp.h:76
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:723
UdpRxCallbackDesc udpCallbackTable[UDP_CALLBACK_TABLE_SIZE]
Definition: udp.c:53
__start_packed struct _Ipv4Header __end_packed
error_t udpSendDatagramEx(NetInterface *interface, uint16_t srcPort, const IpAddr *destIpAddr, uint16_t destPort, NetBuffer *buffer, size_t offset, uint8_t ttl)
Send a UDP datagram (raw interface)
Definition: udp.c:448
error_t
Error codes.
Definition: error.h:40
Ipv4Addr destIpAddr
Definition: ipcp.h:76
unsigned int uint_t
Definition: compiler_port.h:43
uint16_t port
Definition: udp.h:105
#define NetInterface
Definition: net.h:34
NetBuffer * udpAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold a UDP packet.
Definition: udp.c:656
Mutex object.
IP pseudo header.
Definition: ip.h:76
uint16_t port
Definition: dns_common.h:221
Entry describing a user callback.
Definition: udp.h:102
TCP (Transmission Control Protocol)
NetInterface * interface
Definition: udp.h:104
error_t udpInit(void)
UDP related initialization.
Definition: udp.c:61