FreeRDP
Loading...
Searching...
No Matches
udplistener_internal.h
1
17
18#ifndef FREERDP_LIB_CORE_UDPLISTENER_INTERNAL_H
19#define FREERDP_LIB_CORE_UDPLISTENER_INTERNAL_H
20
21#include <winpr/wtypes.h>
22#include <winpr/synch.h>
23#include <winpr/winsock.h>
24
25#if !defined(_WIN32)
26#include <poll.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#endif
30
31#include <freerdp/api.h>
32
33#include "udplistener.h"
34
35/* Listener implementation is split across these files:
36 * - udpsocket.c: platform-specific socket operations to avoid #if in other files.
37 * - udpflow.c: per-client datagram path (see rdpUdpFlow in include/freerdp/server/udp.h),
38 * including the receive queue and sending logic.
39 * - udplistener.c: socket and flow table management, datagram dispatch, and the
40 * process-wide listener thread.
41 * - udplistener_relay.c: relays datagrams to other processes (e.g. after fork()) using
42 * socket pairs and a dedicated relay thread. */
43
44/* Relay endpoints may close at any time; must prevent SIGPIPE */
45#if !defined(MSG_NOSIGNAL)
46#define MSG_NOSIGNAL 0
47#endif
48#if !defined(MSG_DONTWAIT)
49#define MSG_DONTWAIT 0
50#endif
51#if defined(SOCK_CLOEXEC)
52#define UDPLISTENER_SOCK_CLOEXEC SOCK_CLOEXEC
53#else
54#define UDPLISTENER_SOCK_CLOEXEC 0
55#endif
56#if !defined(SHUT_RDWR)
57#define SHUT_RDWR SD_BOTH
58#endif
59
60/* Shared receive buffer for all sessions (similar to RDPUDP_RECEIVE_BUFFER). */
61#define UDPLISTENER_RECEIVE_BUFFER (4 * 1024 * 1024)
62
63/* Max sockets per listener. The listener thread polls these plus one internal event. */
64#define UDPLISTENER_MAX_SOCKETS 32
65
66/* Max datagrams read per socket per pass to prevent starvation. Relay thread uses the
67 * same budget per relay. */
68#define UDPLISTENER_READ_BUDGET 256
69
70/* Queue size for datagrams awaiting the tunnel. Sufficient for ACKs and input if the
71 * tunnel keeps up. Excess datagrams are dropped to prevent a flooding client from
72 * impacting other flows. */
73#define UDPFLOW_SLOTS 256
74
75/* Initial ring capacity, allocated on the first datagram. Doubles when full up to
76 * UDPFLOW_SLOTS to minimize memory for inactive flows. */
77#define UDPFLOW_FIRST_SLOTS 8
78
79/* Max relays per listener. Each relay consumes a file descriptor; limited to prevent
80 * descriptor exhaustion (typical limit is 1024). */
81#define UDPLISTENER_MAX_RELAYS 256
82
83/* The local address the client sent datagrams to */
84typedef struct
85{
86 BOOL valid;
87 int family;
88 struct in_addr ipv4;
89 struct in6_addr ipv6;
90 UINT32 interfaceIndex;
92
94{
95 CRITICAL_SECTION lock;
96 BYTE cookieHash[32];
97
98 /* Event is set when datagrams are pending; protected by lock */
99 HANDLE event;
100 BYTE*
101 ring; /* Allocated upon the first datagram to avoid memory overhead for inactive clients. */
102 size_t slots;
103 UINT16 lengths[UDPFLOW_SLOTS];
104 size_t head;
105 size_t count;
106
107 /* Client address, reached via one of the listener's sockets */
108 SOCKET sockfd;
109 struct sockaddr_storage peer;
110 size_t peerLength;
111 udpLocalAddress local; /* Local address used for sending replies */
112
113 /* Socket to a relay, owned by the flow */
114 SOCKET relay;
115 HANDLE relayEvent;
116 BOOL relayClosed; /* Indicates the remote end closed, meaning the socket will remain readable
117 indefinitely. */
118
119 FreeRDP_UdpFlowReceiveFn receive;
120 void* receiveCustom;
121 FreeRDP_UdpFlowSendFn send;
122 void* sendCustom;
123
124 BOOL (*check)(void* custom);
125 UINT32 (*timeout)(void* custom);
126 void* workerCustom;
127
128 freerdp_udp_listener* listener; /* Set under the listener's lock */
129};
130
131typedef struct
132{
133 rdpUdpFlow* flow;
134 SOCKET sockfd; /* The socket on which the SYN must arrive; INVALID_SOCKET allows any socket. */
135 BOOL bound; /* TRUE if a SYN packet has arrived from the address in 'addr' */
136 struct sockaddr_storage addr;
137 size_t addrlen;
139
140typedef struct
141{
142 struct sockaddr_storage addr; /* The address the socket is bound to */
143 size_t addrlen;
144 SOCKET sockfd;
145 HANDLE event;
146 BOOL localAddress; /* Indicates whether each datagram reports the address the client sent to */
148
149typedef struct s_udp_listener_relay
150{
151 struct s_udp_listener_relay* next;
152 freerdp_udp_listener* listener;
153 rdpUdpFlow* flow;
154 SOCKET sockfd;
155 LONG ending; /* Signals the relay thread to terminate this relay; access via Interlocked
156 functions. */
158
159#if defined(_WIN32)
160typedef WSAPOLLFD udpPollFd;
161#else
162typedef struct pollfd udpPollFd;
163#endif
164
166{
167 CRITICAL_SECTION lock;
168
169 udpListenerSocket sockets[UDPLISTENER_MAX_SOCKETS];
170 size_t socketCount;
171 /* Sockets no flow uses, out of the wait set; the listener thread may still wait on their
172 * events, so it closes them when it next builds its wait set. */
173 udpListenerSocket retired[UDPLISTENER_MAX_SOCKETS];
174 size_t retiredCount;
175
176 /* All entries are owned by entriesByHash; added to entriesByAddress after SYN arrives. */
177 wHashTable* entriesByHash;
178 wHashTable* entriesByAddress;
179
180 /* Single thread serving all relays */
181 udpListenerRelay* relays;
182 size_t relayCount;
183 HANDLE relayThread;
184 LONG relayStop;
185 LONG
186 relayFailed; /* Relay thread stopped due to an error; access using Interlocked functions. */
187 SOCKET relayWake[2]; /* Write to [0] to wake the thread waiting on [1]. */
188
189 HANDLE thread;
190 HANDLE wake;
191 LONG stop;
192
193 BOOL shared; /* Process-wide listener that closes when the last flow is closed. */
194 BOOL freed; /* freerdp_udp_listener_free() was called; the last flow removed releases it. */
195
196 BYTE buffer[0x10000];
197};
198
199/* Implementation in udpsocket.c */
200
202WINPR_ATTR_NODISCARD
203FREERDP_LOCAL BOOL udpsocket_address_equal(const struct sockaddr_storage* a,
204 const struct sockaddr* b, size_t blen);
205
208FREERDP_LOCAL void udpsocket_unmap_address(const struct sockaddr* addr, size_t addrlen,
209 struct sockaddr_storage* out, size_t* outlen);
210
212WINPR_ATTR_NODISCARD
213FREERDP_LOCAL SOCKET udpsocket_new(int family, int type, int protocol);
214
216FREERDP_LOCAL void udpsocket_set_no_inherit(SOCKET sockfd);
217
220WINPR_ATTR_NODISCARD
221FREERDP_LOCAL SOCKET udpsocket_bind(const struct sockaddr* addr, size_t addrlen);
222
224FREERDP_LOCAL void udpsocket_ignore_icmp_errors(SOCKET sockfd);
225
228WINPR_ATTR_NODISCARD
229FREERDP_LOCAL BOOL udpsocket_is_message_socket(SOCKET sockfd);
230
232FREERDP_LOCAL void udpsocket_enable_local_address(udpListenerSocket* sock);
233
237WINPR_ATTR_NODISCARD
238FREERDP_LOCAL int udpsocket_receive(const udpListenerSocket* sock, BYTE* data, size_t size,
239 struct sockaddr_storage* addr, int* addrlen,
240 udpLocalAddress* local);
241
244WINPR_ATTR_NODISCARD
245FREERDP_LOCAL BOOL udpsocket_send_from(SOCKET sockfd, const struct sockaddr_storage* to,
246 size_t tolen, const udpLocalAddress* local, const BYTE* data,
247 size_t length);
248
252WINPR_ATTR_NODISCARD
253FREERDP_LOCAL BOOL udpsocket_source_address_unavailable(int* error);
254
256WINPR_ATTR_NODISCARD
257FREERDP_LOCAL BOOL udpsocket_peer_closed(void);
258
261WINPR_ATTR_NODISCARD
262FREERDP_LOCAL BOOL udpsocket_message_pair(SOCKET pair[2]);
263
264FREERDP_LOCAL void udpsocket_poll_set(udpPollFd* fd, SOCKET sockfd);
265
267WINPR_ATTR_NODISCARD
268FREERDP_LOCAL int udpsocket_poll(udpPollFd* fds, size_t count);
269
270/* Implementation in udplistener.c */
271
274WINPR_ATTR_NODISCARD
275FREERDP_LOCAL BOOL udplistener_add_flow(freerdp_udp_listener* listener, rdpUdpFlow* flow,
276 SOCKET sockfd);
277
278/* Functions implemented in udplistener_relay.c */
279
281FREERDP_LOCAL void udplistener_stop_relays(freerdp_udp_listener* listener);
282
284FREERDP_LOCAL void udplistener_relays_close_after_fork(freerdp_udp_listener* listener);
285
286#endif /* End of FREERDP_LIB_CORE_UDPLISTENER_INTERNAL_H */
Internal library implementation of include/freerdp/server/udp.h and the server UDP API,...