[1] | 1 | /* $Id: udp.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[37196] | 3 | * IPRT - UDP/IP.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[3672] | 41 | #ifdef RT_OS_WINDOWS
|
---|
[62592] | 42 | # include <iprt/win/winsock2.h>
|
---|
[27770] | 43 | #else
|
---|
| 44 | # include <sys/types.h>
|
---|
| 45 | # include <sys/socket.h>
|
---|
[27759] | 46 | # include <errno.h>
|
---|
| 47 | # include <netinet/in.h>
|
---|
[37196] | 48 | # include <netinet/udp.h>
|
---|
[27759] | 49 | # include <arpa/inet.h>
|
---|
| 50 | # include <netdb.h>
|
---|
[27770] | 51 | #endif
|
---|
[26683] | 52 | #include <limits.h>
|
---|
[1] | 53 |
|
---|
[23625] | 54 | #include "internal/iprt.h"
|
---|
[37196] | 55 | #include <iprt/udp.h>
|
---|
[23625] | 56 |
|
---|
| 57 | #include <iprt/asm.h>
|
---|
[1] | 58 | #include <iprt/assert.h>
|
---|
| 59 | #include <iprt/err.h>
|
---|
[23625] | 60 | #include <iprt/mempool.h>
|
---|
[27497] | 61 | #include <iprt/mem.h>
|
---|
[1] | 62 | #include <iprt/string.h>
|
---|
[27503] | 63 | #include <iprt/socket.h>
|
---|
[23625] | 64 | #include <iprt/thread.h>
|
---|
[24204] | 65 | #include <iprt/time.h>
|
---|
[1] | 66 |
|
---|
[23625] | 67 | #include "internal/magics.h"
|
---|
[27503] | 68 | #include "internal/socket.h"
|
---|
[1] | 69 |
|
---|
[23625] | 70 |
|
---|
[57358] | 71 | /*********************************************************************************************************************************
|
---|
| 72 | * Defined Constants And Macros *
|
---|
| 73 | *********************************************************************************************************************************/
|
---|
[1] | 74 | /* fixup backlevel OSes. */
|
---|
[3672] | 75 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
[23625] | 76 | # define socklen_t int
|
---|
[1] | 77 | #endif
|
---|
| 78 |
|
---|
| 79 |
|
---|
[57358] | 80 | /*********************************************************************************************************************************
|
---|
| 81 | * Structures and Typedefs *
|
---|
| 82 | *********************************************************************************************************************************/
|
---|
[1] | 83 | /**
|
---|
[37196] | 84 | * UDP Server state.
|
---|
[1] | 85 | */
|
---|
[37196] | 86 | typedef enum RTUDPSERVERSTATE
|
---|
[1] | 87 | {
|
---|
| 88 | /** Invalid. */
|
---|
[37196] | 89 | RTUDPSERVERSTATE_INVALID = 0,
|
---|
[1] | 90 | /** Created. */
|
---|
[37196] | 91 | RTUDPSERVERSTATE_CREATED,
|
---|
| 92 | /** Thread for incoming datagrams is starting up. */
|
---|
| 93 | RTUDPSERVERSTATE_STARTING,
|
---|
| 94 | /** Waiting for incoming datagrams. */
|
---|
| 95 | RTUDPSERVERSTATE_WAITING,
|
---|
| 96 | /** Handling an incoming datagram. */
|
---|
| 97 | RTUDPSERVERSTATE_RECEIVING,
|
---|
| 98 | /** Thread terminating. */
|
---|
| 99 | RTUDPSERVERSTATE_STOPPING,
|
---|
| 100 | /** Thread terminated. */
|
---|
| 101 | RTUDPSERVERSTATE_STOPPED,
|
---|
| 102 | /** Final cleanup before being unusable. */
|
---|
| 103 | RTUDPSERVERSTATE_DESTROYING
|
---|
| 104 | } RTUDPSERVERSTATE;
|
---|
[1] | 105 |
|
---|
| 106 | /*
|
---|
[37196] | 107 | * Internal representation of the UDP Server handle.
|
---|
[1] | 108 | */
|
---|
[37196] | 109 | typedef struct RTUDPSERVER
|
---|
[1] | 110 | {
|
---|
[37196] | 111 | /** The magic value (RTUDPSERVER_MAGIC). */
|
---|
[23625] | 112 | uint32_t volatile u32Magic;
|
---|
[1] | 113 | /** The server state. */
|
---|
[37196] | 114 | RTUDPSERVERSTATE volatile enmState;
|
---|
[1] | 115 | /** The server thread. */
|
---|
| 116 | RTTHREAD Thread;
|
---|
| 117 | /** The server socket. */
|
---|
[37196] | 118 | RTSOCKET volatile hSocket;
|
---|
| 119 | /** The datagram receiver function. */
|
---|
| 120 | PFNRTUDPSERVE pfnServe;
|
---|
[1] | 121 | /** Argument to pfnServer. */
|
---|
| 122 | void *pvUser;
|
---|
[37196] | 123 | } RTUDPSERVER;
|
---|
[1] | 124 |
|
---|
| 125 |
|
---|
[57358] | 126 | /*********************************************************************************************************************************
|
---|
| 127 | * Internal Functions *
|
---|
| 128 | *********************************************************************************************************************************/
|
---|
[37196] | 129 | static DECLCALLBACK(int) rtUdpServerThread(RTTHREAD ThreadSelf, void *pvServer);
|
---|
| 130 | static int rtUdpServerListen(PRTUDPSERVER pServer);
|
---|
| 131 | static int rtUdpServerListenCleanup(PRTUDPSERVER pServer);
|
---|
| 132 | static int rtUdpServerDestroySocket(RTSOCKET volatile *pSock, const char *pszMsg);
|
---|
| 133 | static int rtUdpClose(RTSOCKET Sock, const char *pszMsg);
|
---|
[1] | 134 |
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Atomicly updates a socket variable.
|
---|
[27500] | 138 | * @returns The old handle value.
|
---|
| 139 | * @param phSock The socket handle variable to update.
|
---|
[57944] | 140 | * @param hNew The new socket handle value.
|
---|
[1] | 141 | */
|
---|
[37196] | 142 | DECLINLINE(RTSOCKET) rtUdpAtomicXchgSock(RTSOCKET volatile *phSock, const RTSOCKET hNew)
|
---|
[1] | 143 | {
|
---|
[27500] | 144 | RTSOCKET hRet;
|
---|
| 145 | ASMAtomicXchgHandle(phSock, hNew, &hRet);
|
---|
| 146 | return hRet;
|
---|
[1] | 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
[37196] | 151 | * Tries to change the UDP server state.
|
---|
[1] | 152 | */
|
---|
[37196] | 153 | DECLINLINE(bool) rtUdpServerTrySetState(PRTUDPSERVER pServer, RTUDPSERVERSTATE enmStateNew, RTUDPSERVERSTATE enmStateOld)
|
---|
[1] | 154 | {
|
---|
| 155 | bool fRc;
|
---|
| 156 | ASMAtomicCmpXchgSize(&pServer->enmState, enmStateNew, enmStateOld, fRc);
|
---|
| 157 | return fRc;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[23625] | 160 | /**
|
---|
[37196] | 161 | * Changes the UDP server state.
|
---|
[23625] | 162 | */
|
---|
[37196] | 163 | DECLINLINE(void) rtUdpServerSetState(PRTUDPSERVER pServer, RTUDPSERVERSTATE enmStateNew, RTUDPSERVERSTATE enmStateOld)
|
---|
[23625] | 164 | {
|
---|
| 165 | bool fRc;
|
---|
| 166 | ASMAtomicCmpXchgSize(&pServer->enmState, enmStateNew, enmStateOld, fRc);
|
---|
| 167 | Assert(fRc); NOREF(fRc);
|
---|
| 168 | }
|
---|
[1] | 169 |
|
---|
[27497] | 170 |
|
---|
[1] | 171 | /**
|
---|
[37196] | 172 | * Closes a socket.
|
---|
[23625] | 173 | *
|
---|
| 174 | * @returns IPRT status code.
|
---|
| 175 | */
|
---|
[37196] | 176 | static int rtUdpServerDestroySocket(RTSOCKET volatile *pSock, const char *pszMsg)
|
---|
[23625] | 177 | {
|
---|
[37196] | 178 | RTSOCKET hSocket = rtUdpAtomicXchgSock(pSock, NIL_RTSOCKET);
|
---|
[27497] | 179 | if (hSocket != NIL_RTSOCKET)
|
---|
[23625] | 180 | {
|
---|
[37196] | 181 | return rtUdpClose(hSocket, pszMsg);
|
---|
[23625] | 182 | }
|
---|
[37196] | 183 | return VINF_UDP_SERVER_NO_CLIENT;
|
---|
[23625] | 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
[37196] | 187 | RTR3DECL(int) RTUdpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
|
---|
| 188 | PFNRTUDPSERVE pfnServe, void *pvUser, PPRTUDPSERVER ppServer)
|
---|
[1] | 189 | {
|
---|
| 190 | /*
|
---|
[23625] | 191 | * Validate input.
|
---|
[1] | 192 | */
|
---|
[23625] | 193 | AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
|
---|
| 194 | AssertPtrReturn(pfnServe, VERR_INVALID_POINTER);
|
---|
| 195 | AssertPtrReturn(pszThrdName, VERR_INVALID_POINTER);
|
---|
| 196 | AssertPtrReturn(ppServer, VERR_INVALID_POINTER);
|
---|
[1] | 197 |
|
---|
| 198 | /*
|
---|
| 199 | * Create the server.
|
---|
| 200 | */
|
---|
[37196] | 201 | PRTUDPSERVER pServer;
|
---|
| 202 | int rc = RTUdpServerCreateEx(pszAddress, uPort, &pServer);
|
---|
[1] | 203 | if (RT_SUCCESS(rc))
|
---|
| 204 | {
|
---|
| 205 | /*
|
---|
| 206 | * Create the listener thread.
|
---|
| 207 | */
|
---|
[23625] | 208 | RTMemPoolRetain(pServer);
|
---|
[37196] | 209 | pServer->enmState = RTUDPSERVERSTATE_STARTING;
|
---|
[1] | 210 | pServer->pvUser = pvUser;
|
---|
| 211 | pServer->pfnServe = pfnServe;
|
---|
[37196] | 212 | rc = RTThreadCreate(&pServer->Thread, rtUdpServerThread, pServer, 0, enmType, /*RTTHREADFLAGS_WAITABLE*/0, pszThrdName);
|
---|
[1] | 213 | if (RT_SUCCESS(rc))
|
---|
| 214 | {
|
---|
| 215 | /* done */
|
---|
| 216 | if (ppServer)
|
---|
| 217 | *ppServer = pServer;
|
---|
[23625] | 218 | else
|
---|
| 219 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
[1] | 220 | return rc;
|
---|
| 221 | }
|
---|
[23625] | 222 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
[1] | 223 |
|
---|
| 224 | /*
|
---|
| 225 | * Destroy the server.
|
---|
| 226 | */
|
---|
[37196] | 227 | rtUdpServerSetState(pServer, RTUDPSERVERSTATE_CREATED, RTUDPSERVERSTATE_STARTING);
|
---|
| 228 | RTUdpServerDestroy(pServer);
|
---|
[1] | 229 | }
|
---|
| 230 |
|
---|
| 231 | return rc;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
[37196] | 236 | * Server thread, loops waiting for datagrams until it's terminated.
|
---|
[1] | 237 | *
|
---|
| 238 | * @returns iprt status code. (ignored).
|
---|
| 239 | * @param ThreadSelf Thread handle.
|
---|
| 240 | * @param pvServer Server handle.
|
---|
| 241 | */
|
---|
[37196] | 242 | static DECLCALLBACK(int) rtUdpServerThread(RTTHREAD ThreadSelf, void *pvServer)
|
---|
[1] | 243 | {
|
---|
[37196] | 244 | PRTUDPSERVER pServer = (PRTUDPSERVER)pvServer;
|
---|
[23625] | 245 | int rc;
|
---|
[37196] | 246 | if (rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_WAITING, RTUDPSERVERSTATE_STARTING))
|
---|
| 247 | rc = rtUdpServerListen(pServer);
|
---|
[23625] | 248 | else
|
---|
[37196] | 249 | rc = rtUdpServerListenCleanup(pServer);
|
---|
[23625] | 250 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
[1] | 251 | NOREF(ThreadSelf);
|
---|
| 252 | return VINF_SUCCESS;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
[37196] | 256 | RTR3DECL(int) RTUdpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTUDPSERVER ppServer)
|
---|
[1] | 257 | {
|
---|
| 258 |
|
---|
| 259 | /*
|
---|
[23625] | 260 | * Validate input.
|
---|
[1] | 261 | */
|
---|
[23625] | 262 | AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
|
---|
| 263 | AssertPtrReturn(ppServer, VERR_INVALID_PARAMETER);
|
---|
[1] | 264 |
|
---|
| 265 | /*
|
---|
[39801] | 266 | * Resolve the address.
|
---|
[1] | 267 | */
|
---|
[39801] | 268 | RTNETADDR LocalAddr;
|
---|
| 269 | int rc = RTSocketParseInetAddress(pszAddress, uPort, &LocalAddr);
|
---|
| 270 | if (RT_FAILURE(rc))
|
---|
| 271 | return rc;
|
---|
[1] | 272 |
|
---|
| 273 | /*
|
---|
| 274 | * Setting up socket.
|
---|
| 275 | */
|
---|
[37196] | 276 | RTSOCKET Sock;
|
---|
[96475] | 277 | rc = rtSocketCreate(&Sock, AF_INET, SOCK_DGRAM, IPPROTO_UDP, false /*fInheritable*/);
|
---|
[27497] | 278 | if (RT_SUCCESS(rc))
|
---|
[1] | 279 | {
|
---|
| 280 | /*
|
---|
| 281 | * Set socket options.
|
---|
| 282 | */
|
---|
| 283 | int fFlag = 1;
|
---|
[37196] | 284 | if (!rtSocketSetOpt(Sock, SOL_SOCKET, SO_REUSEADDR, &fFlag, sizeof(fFlag)))
|
---|
[1] | 285 | {
|
---|
| 286 | /*
|
---|
[37196] | 287 | * Bind a name to the socket.
|
---|
[1] | 288 | */
|
---|
[39801] | 289 | rc = rtSocketBind(Sock, &LocalAddr);
|
---|
[27497] | 290 | if (RT_SUCCESS(rc))
|
---|
[1] | 291 | {
|
---|
| 292 | /*
|
---|
[27497] | 293 | * Create the server handle.
|
---|
[1] | 294 | */
|
---|
[37196] | 295 | PRTUDPSERVER pServer = (PRTUDPSERVER)RTMemPoolAlloc(RTMEMPOOL_DEFAULT, sizeof(*pServer));
|
---|
[27497] | 296 | if (pServer)
|
---|
[1] | 297 | {
|
---|
[37196] | 298 | pServer->u32Magic = RTUDPSERVER_MAGIC;
|
---|
| 299 | pServer->enmState = RTUDPSERVERSTATE_CREATED;
|
---|
| 300 | pServer->Thread = NIL_RTTHREAD;
|
---|
| 301 | pServer->hSocket = Sock;
|
---|
| 302 | pServer->pfnServe = NULL;
|
---|
| 303 | pServer->pvUser = NULL;
|
---|
[27497] | 304 | *ppServer = pServer;
|
---|
| 305 | return VINF_SUCCESS;
|
---|
| 306 | }
|
---|
[23613] | 307 |
|
---|
[27497] | 308 | /* bail out */
|
---|
| 309 | rc = VERR_NO_MEMORY;
|
---|
[1] | 310 | }
|
---|
| 311 | }
|
---|
| 312 | else
|
---|
[27497] | 313 | AssertMsgFailed(("rtSocketSetOpt: %Rrc\n", rc));
|
---|
[37196] | 314 | rtUdpClose(Sock, "RTServerCreateEx");
|
---|
[1] | 315 | }
|
---|
| 316 |
|
---|
| 317 | return rc;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
[37196] | 321 | RTR3DECL(int) RTUdpServerListen(PRTUDPSERVER pServer, PFNRTUDPSERVE pfnServe, void *pvUser)
|
---|
[1] | 322 | {
|
---|
| 323 | /*
|
---|
[23625] | 324 | * Validate input and retain the instance.
|
---|
[1] | 325 | */
|
---|
[23625] | 326 | AssertPtrReturn(pfnServe, VERR_INVALID_POINTER);
|
---|
| 327 | AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
|
---|
[37196] | 328 | AssertReturn(pServer->u32Magic == RTUDPSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
[23625] | 329 | AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
|
---|
| 330 |
|
---|
| 331 | int rc = VERR_INVALID_STATE;
|
---|
[37196] | 332 | if (rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_WAITING, RTUDPSERVERSTATE_CREATED))
|
---|
[1] | 333 | {
|
---|
| 334 | Assert(!pServer->pfnServe);
|
---|
| 335 | Assert(!pServer->pvUser);
|
---|
| 336 | Assert(pServer->Thread == NIL_RTTHREAD);
|
---|
| 337 |
|
---|
| 338 | pServer->pfnServe = pfnServe;
|
---|
| 339 | pServer->pvUser = pvUser;
|
---|
| 340 | pServer->Thread = RTThreadSelf();
|
---|
| 341 | Assert(pServer->Thread != NIL_RTTHREAD);
|
---|
[37196] | 342 | rc = rtUdpServerListen(pServer);
|
---|
[1] | 343 | }
|
---|
[23625] | 344 | else
|
---|
| 345 | {
|
---|
| 346 | AssertMsgFailed(("enmState=%d\n", pServer->enmState));
|
---|
| 347 | rc = VERR_INVALID_STATE;
|
---|
| 348 | }
|
---|
| 349 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
| 350 | return rc;
|
---|
[1] | 351 | }
|
---|
| 352 |
|
---|
| 353 |
|
---|
[932] | 354 | /**
|
---|
[37196] | 355 | * Internal worker common for RTUdpServerListen and the thread created by
|
---|
| 356 | * RTUdpServerCreate().
|
---|
[23625] | 357 | *
|
---|
| 358 | * The caller makes sure it has its own memory reference and releases it upon
|
---|
| 359 | * return.
|
---|
[932] | 360 | */
|
---|
[37196] | 361 | static int rtUdpServerListen(PRTUDPSERVER pServer)
|
---|
[1] | 362 | {
|
---|
| 363 | /*
|
---|
[37196] | 364 | * Wait for incoming datagrams loop.
|
---|
[1] | 365 | */
|
---|
| 366 | for (;;)
|
---|
| 367 | {
|
---|
| 368 | /*
|
---|
[27787] | 369 | * Change state, getting an extra reference to the socket so we can
|
---|
| 370 | * allow others to close it while we're stuck in rtSocketAccept.
|
---|
[1] | 371 | */
|
---|
[37196] | 372 | RTUDPSERVERSTATE enmState = pServer->enmState;
|
---|
| 373 | RTSOCKET hSocket;
|
---|
| 374 | ASMAtomicReadHandle(&pServer->hSocket, &hSocket);
|
---|
| 375 | if (hSocket != NIL_RTSOCKET)
|
---|
| 376 | RTSocketRetain(hSocket);
|
---|
| 377 | if ( enmState != RTUDPSERVERSTATE_WAITING
|
---|
| 378 | && enmState != RTUDPSERVERSTATE_RECEIVING)
|
---|
[27787] | 379 | {
|
---|
[37196] | 380 | RTSocketRelease(hSocket);
|
---|
| 381 | return rtUdpServerListenCleanup(pServer);
|
---|
[27787] | 382 | }
|
---|
[37196] | 383 | if (!rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_WAITING, enmState))
|
---|
[27787] | 384 | {
|
---|
[37196] | 385 | RTSocketRelease(hSocket);
|
---|
[1] | 386 | continue;
|
---|
[27787] | 387 | }
|
---|
[1] | 388 |
|
---|
| 389 | /*
|
---|
[37196] | 390 | * Wait for incoming datagrams or errors.
|
---|
[1] | 391 | */
|
---|
[37196] | 392 | uint32_t fEvents;
|
---|
| 393 | int rc = RTSocketSelectOneEx(hSocket, RTSOCKET_EVT_READ | RTSOCKET_EVT_ERROR, &fEvents, 1000);
|
---|
| 394 | RTSocketRelease(hSocket);
|
---|
| 395 | if (rc == VERR_TIMEOUT)
|
---|
| 396 | continue;
|
---|
[27497] | 397 | if (RT_FAILURE(rc))
|
---|
[1] | 398 | {
|
---|
| 399 | /* These are typical for what can happen during destruction. */
|
---|
[27497] | 400 | if ( rc == VERR_INVALID_HANDLE
|
---|
| 401 | || rc == VERR_INVALID_PARAMETER
|
---|
| 402 | || rc == VERR_NET_NOT_SOCKET)
|
---|
[37196] | 403 | return rtUdpServerListenCleanup(pServer);
|
---|
[1] | 404 | continue;
|
---|
| 405 | }
|
---|
[37196] | 406 | if (fEvents & RTSOCKET_EVT_ERROR)
|
---|
| 407 | return rtUdpServerListenCleanup(pServer);
|
---|
[1] | 408 |
|
---|
| 409 | /*
|
---|
| 410 | * Run a pfnServe callback.
|
---|
| 411 | */
|
---|
[37196] | 412 | if (!rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_RECEIVING, RTUDPSERVERSTATE_WAITING))
|
---|
| 413 | return rtUdpServerListenCleanup(pServer);
|
---|
| 414 | rc = pServer->pfnServe(hSocket, pServer->pvUser);
|
---|
[1] | 415 |
|
---|
| 416 | /*
|
---|
| 417 | * Stop the server?
|
---|
| 418 | */
|
---|
[37196] | 419 | if (rc == VERR_UDP_SERVER_STOP)
|
---|
[1] | 420 | {
|
---|
[37196] | 421 | if (rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_STOPPING, RTUDPSERVERSTATE_RECEIVING))
|
---|
[1] | 422 | {
|
---|
| 423 | /*
|
---|
| 424 | * Reset the server socket and change the state to stopped. After that state change
|
---|
| 425 | * we cannot safely access the handle so we'll have to return here.
|
---|
| 426 | */
|
---|
[37196] | 427 | hSocket = rtUdpAtomicXchgSock(&pServer->hSocket, NIL_RTSOCKET);
|
---|
| 428 | rtUdpServerSetState(pServer, RTUDPSERVERSTATE_STOPPED, RTUDPSERVERSTATE_STOPPING);
|
---|
| 429 | rtUdpClose(hSocket, "Listener: server stopped");
|
---|
[1] | 430 | }
|
---|
[23625] | 431 | else
|
---|
[37196] | 432 | rtUdpServerListenCleanup(pServer); /* ignore rc */
|
---|
[23625] | 433 | return rc;
|
---|
[1] | 434 | }
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 |
|
---|
| 439 | /**
|
---|
| 440 | * Clean up after listener.
|
---|
| 441 | */
|
---|
[37196] | 442 | static int rtUdpServerListenCleanup(PRTUDPSERVER pServer)
|
---|
[1] | 443 | {
|
---|
| 444 | /*
|
---|
[37196] | 445 | * Close the server socket.
|
---|
[1] | 446 | */
|
---|
[37196] | 447 | rtUdpServerDestroySocket(&pServer->hSocket, "ListenCleanup");
|
---|
[23625] | 448 |
|
---|
| 449 | /*
|
---|
| 450 | * Figure the return code and make sure the state is OK.
|
---|
| 451 | */
|
---|
[37196] | 452 | RTUDPSERVERSTATE enmState = pServer->enmState;
|
---|
[23625] | 453 | switch (enmState)
|
---|
[1] | 454 | {
|
---|
[37196] | 455 | case RTUDPSERVERSTATE_STOPPING:
|
---|
| 456 | case RTUDPSERVERSTATE_STOPPED:
|
---|
| 457 | return VERR_UDP_SERVER_SHUTDOWN;
|
---|
[1] | 458 |
|
---|
[37196] | 459 | case RTUDPSERVERSTATE_WAITING:
|
---|
| 460 | rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_STOPPED, enmState);
|
---|
| 461 | return VERR_UDP_SERVER_DESTROYED;
|
---|
[1] | 462 |
|
---|
[37196] | 463 | case RTUDPSERVERSTATE_DESTROYING:
|
---|
| 464 | return VERR_UDP_SERVER_DESTROYED;
|
---|
[23625] | 465 |
|
---|
[37196] | 466 | case RTUDPSERVERSTATE_STARTING:
|
---|
| 467 | case RTUDPSERVERSTATE_RECEIVING:
|
---|
[23625] | 468 | default:
|
---|
| 469 | AssertMsgFailedReturn(("pServer=%p enmState=%d\n", pServer, enmState), VERR_INTERNAL_ERROR_4);
|
---|
[1] | 470 | }
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 |
|
---|
[37196] | 474 | RTR3DECL(int) RTUdpServerShutdown(PRTUDPSERVER pServer)
|
---|
[932] | 475 | {
|
---|
| 476 | /*
|
---|
[23625] | 477 | * Validate input and retain the instance.
|
---|
[932] | 478 | */
|
---|
[23625] | 479 | AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
|
---|
[37196] | 480 | AssertReturn(pServer->u32Magic == RTUDPSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
[23625] | 481 | AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
|
---|
| 482 |
|
---|
| 483 | /*
|
---|
| 484 | * Try change the state to stopping, then replace and destroy the server socket.
|
---|
| 485 | */
|
---|
| 486 | for (;;)
|
---|
[932] | 487 | {
|
---|
[37196] | 488 | RTUDPSERVERSTATE enmState = pServer->enmState;
|
---|
| 489 | if ( enmState != RTUDPSERVERSTATE_WAITING
|
---|
| 490 | && enmState != RTUDPSERVERSTATE_RECEIVING)
|
---|
[23625] | 491 | {
|
---|
| 492 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
| 493 | switch (enmState)
|
---|
| 494 | {
|
---|
[37196] | 495 | case RTUDPSERVERSTATE_CREATED:
|
---|
| 496 | case RTUDPSERVERSTATE_STARTING:
|
---|
[23625] | 497 | default:
|
---|
| 498 | AssertMsgFailed(("%d\n", enmState));
|
---|
| 499 | return VERR_INVALID_STATE;
|
---|
| 500 |
|
---|
[37196] | 501 | case RTUDPSERVERSTATE_STOPPING:
|
---|
| 502 | case RTUDPSERVERSTATE_STOPPED:
|
---|
[23625] | 503 | return VINF_SUCCESS;
|
---|
| 504 |
|
---|
[37196] | 505 | case RTUDPSERVERSTATE_DESTROYING:
|
---|
| 506 | return VERR_UDP_SERVER_DESTROYED;
|
---|
[23625] | 507 | }
|
---|
| 508 | }
|
---|
[37196] | 509 | if (rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_STOPPING, enmState))
|
---|
[23625] | 510 | {
|
---|
[37196] | 511 | rtUdpServerDestroySocket(&pServer->hSocket, "RTUdpServerShutdown");
|
---|
| 512 | rtUdpServerSetState(pServer, RTUDPSERVERSTATE_STOPPED, RTUDPSERVERSTATE_STOPPING);
|
---|
[23625] | 513 |
|
---|
| 514 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
| 515 | return VINF_SUCCESS;
|
---|
| 516 | }
|
---|
[932] | 517 | }
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 |
|
---|
[37196] | 521 | RTR3DECL(int) RTUdpServerDestroy(PRTUDPSERVER pServer)
|
---|
[1] | 522 | {
|
---|
| 523 | /*
|
---|
[23625] | 524 | * Validate input and retain the instance.
|
---|
[1] | 525 | */
|
---|
[23625] | 526 | AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
|
---|
[37196] | 527 | AssertReturn(pServer->u32Magic == RTUDPSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
[23625] | 528 | AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE); /* paranoia */
|
---|
[1] | 529 |
|
---|
| 530 | /*
|
---|
[23625] | 531 | * Move the state along so the listener can figure out what's going on.
|
---|
[1] | 532 | */
|
---|
[23625] | 533 | for (;;)
|
---|
[1] | 534 | {
|
---|
[23625] | 535 | bool fDestroyable;
|
---|
[37196] | 536 | RTUDPSERVERSTATE enmState = pServer->enmState;
|
---|
[1] | 537 | switch (enmState)
|
---|
| 538 | {
|
---|
[37196] | 539 | case RTUDPSERVERSTATE_STARTING:
|
---|
| 540 | case RTUDPSERVERSTATE_WAITING:
|
---|
| 541 | case RTUDPSERVERSTATE_RECEIVING:
|
---|
| 542 | case RTUDPSERVERSTATE_CREATED:
|
---|
| 543 | case RTUDPSERVERSTATE_STOPPED:
|
---|
| 544 | fDestroyable = rtUdpServerTrySetState(pServer, RTUDPSERVERSTATE_DESTROYING, enmState);
|
---|
[1] | 545 | break;
|
---|
| 546 |
|
---|
[23625] | 547 | /* destroyable states */
|
---|
[37196] | 548 | case RTUDPSERVERSTATE_STOPPING:
|
---|
[23625] | 549 | fDestroyable = true;
|
---|
[1] | 550 | break;
|
---|
| 551 |
|
---|
| 552 | /*
|
---|
[23625] | 553 | * Everything else means user or internal misbehavior.
|
---|
[1] | 554 | */
|
---|
| 555 | default:
|
---|
| 556 | AssertMsgFailed(("pServer=%p enmState=%d\n", pServer, enmState));
|
---|
[23625] | 557 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
[1] | 558 | return VERR_INTERNAL_ERROR;
|
---|
| 559 | }
|
---|
[23625] | 560 | if (fDestroyable)
|
---|
| 561 | break;
|
---|
[1] | 562 | }
|
---|
| 563 |
|
---|
[23625] | 564 | /*
|
---|
| 565 | * Destroy it.
|
---|
| 566 | */
|
---|
[37196] | 567 | ASMAtomicWriteU32(&pServer->u32Magic, ~RTUDPSERVER_MAGIC);
|
---|
| 568 | rtUdpServerDestroySocket(&pServer->hSocket, "Destroyer: server");
|
---|
[1] | 569 |
|
---|
[23625] | 570 | /*
|
---|
| 571 | * Release it.
|
---|
| 572 | */
|
---|
| 573 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
| 574 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
| 575 | return VINF_SUCCESS;
|
---|
[1] | 576 | }
|
---|
| 577 |
|
---|
| 578 |
|
---|
| 579 | /**
|
---|
| 580 | * Internal close function which does all the proper bitching.
|
---|
| 581 | */
|
---|
[37196] | 582 | static int rtUdpClose(RTSOCKET Sock, const char *pszMsg)
|
---|
[1] | 583 | {
|
---|
[39083] | 584 | NOREF(pszMsg); /** @todo drop this parameter? */
|
---|
| 585 |
|
---|
[1] | 586 | /* ignore nil handles. */
|
---|
| 587 | if (Sock == NIL_RTSOCKET)
|
---|
| 588 | return VINF_SUCCESS;
|
---|
| 589 |
|
---|
| 590 | /*
|
---|
[27787] | 591 | * Close the socket handle (drops our reference to it).
|
---|
[1] | 592 | */
|
---|
[27787] | 593 | return RTSocketClose(Sock);
|
---|
[27497] | 594 | }
|
---|
| 595 |
|
---|
| 596 |
|
---|
[37196] | 597 | RTR3DECL(int) RTUdpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr)
|
---|
[27497] | 598 | {
|
---|
[37196] | 599 | if (!RT_VALID_PTR(pcbRead))
|
---|
| 600 | return VERR_INVALID_POINTER;
|
---|
| 601 | return RTSocketReadFrom(Sock, pvBuffer, cbBuffer, pcbRead, pSrcAddr);
|
---|
[27497] | 602 | }
|
---|
| 603 |
|
---|
| 604 |
|
---|
[37196] | 605 | RTR3DECL(int) RTUdpWrite(PRTUDPSERVER pServer, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pDstAddr)
|
---|
[27497] | 606 | {
|
---|
[37196] | 607 | /*
|
---|
| 608 | * Validate input and retain the instance.
|
---|
| 609 | */
|
---|
| 610 | AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
|
---|
| 611 | AssertReturn(pServer->u32Magic == RTUDPSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 612 | AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
|
---|
[27497] | 613 |
|
---|
[37196] | 614 | RTSOCKET hSocket;
|
---|
| 615 | ASMAtomicReadHandle(&pServer->hSocket, &hSocket);
|
---|
| 616 | if (hSocket == NIL_RTSOCKET)
|
---|
[27497] | 617 | {
|
---|
[37196] | 618 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
| 619 | return VERR_INVALID_HANDLE;
|
---|
[27497] | 620 | }
|
---|
[37196] | 621 | RTSocketRetain(hSocket);
|
---|
[1] | 622 |
|
---|
[37196] | 623 | int rc = VINF_SUCCESS;
|
---|
| 624 | RTUDPSERVERSTATE enmState = pServer->enmState;
|
---|
| 625 | if ( enmState != RTUDPSERVERSTATE_CREATED
|
---|
| 626 | && enmState != RTUDPSERVERSTATE_STARTING
|
---|
| 627 | && enmState != RTUDPSERVERSTATE_WAITING
|
---|
| 628 | && enmState != RTUDPSERVERSTATE_RECEIVING
|
---|
| 629 | && enmState != RTUDPSERVERSTATE_STOPPING)
|
---|
| 630 | rc = VERR_INVALID_STATE;
|
---|
[27497] | 631 |
|
---|
[37196] | 632 | if (RT_SUCCESS(rc))
|
---|
| 633 | rc = RTSocketWriteTo(hSocket, pvBuffer, cbBuffer, pDstAddr);
|
---|
[27959] | 634 |
|
---|
[37196] | 635 | RTSocketRelease(hSocket);
|
---|
| 636 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
|
---|
[27959] | 637 |
|
---|
[30468] | 638 | return rc;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
[57955] | 641 |
|
---|
[57970] | 642 | RTR3DECL(int) RTUdpCreateClientSocket(const char *pszAddress, uint32_t uPort, PRTNETADDR pLocalAddr, PRTSOCKET pSock)
|
---|
[57955] | 643 | {
|
---|
| 644 | /*
|
---|
| 645 | * Validate input.
|
---|
| 646 | */
|
---|
| 647 | AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
|
---|
| 648 | AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
|
---|
[57970] | 649 | AssertPtrReturn(pSock, VERR_INVALID_POINTER);
|
---|
[57955] | 650 |
|
---|
| 651 | /*
|
---|
| 652 | * Resolve the address.
|
---|
| 653 | */
|
---|
| 654 | RTNETADDR Addr;
|
---|
| 655 | int rc = RTSocketParseInetAddress(pszAddress, uPort, &Addr);
|
---|
| 656 | if (RT_FAILURE(rc))
|
---|
| 657 | return rc;
|
---|
| 658 |
|
---|
| 659 | /*
|
---|
| 660 | * Create the socket and connect.
|
---|
| 661 | */
|
---|
| 662 | RTSOCKET Sock;
|
---|
[96475] | 663 | rc = rtSocketCreate(&Sock, AF_INET, SOCK_DGRAM, 0, false /*fInheritable*/);
|
---|
[57955] | 664 | if (RT_SUCCESS(rc))
|
---|
| 665 | {
|
---|
[57970] | 666 | if (pLocalAddr)
|
---|
| 667 | rc = rtSocketBind(Sock, pLocalAddr);
|
---|
[57955] | 668 | if (RT_SUCCESS(rc))
|
---|
| 669 | {
|
---|
[57970] | 670 | rc = rtSocketConnect(Sock, &Addr, RT_SOCKETCONNECT_DEFAULT_WAIT);
|
---|
[57957] | 671 | if (RT_SUCCESS(rc))
|
---|
| 672 | {
|
---|
| 673 | *pSock = Sock;
|
---|
| 674 | return VINF_SUCCESS;
|
---|
| 675 | }
|
---|
[57955] | 676 | }
|
---|
| 677 | RTSocketClose(Sock);
|
---|
| 678 | }
|
---|
| 679 | return rc;
|
---|
| 680 | }
|
---|
[57957] | 681 |
|
---|
[96864] | 682 |
|
---|
| 683 | RTR3DECL(int) RTUdpCreateServerSocket(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock)
|
---|
| 684 | {
|
---|
| 685 | /*
|
---|
| 686 | * Validate input.
|
---|
| 687 | */
|
---|
| 688 | AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
|
---|
| 689 | AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
|
---|
| 690 | AssertPtrReturn(pSock, VERR_INVALID_POINTER);
|
---|
| 691 |
|
---|
| 692 | /*
|
---|
| 693 | * Resolve the address.
|
---|
| 694 | */
|
---|
| 695 | RTNETADDR LocalAddr;
|
---|
| 696 | int rc = RTSocketParseInetAddress(pszAddress, uPort, &LocalAddr);
|
---|
| 697 | if (RT_FAILURE(rc))
|
---|
| 698 | return rc;
|
---|
| 699 |
|
---|
| 700 | /*
|
---|
| 701 | * Setting up socket.
|
---|
| 702 | */
|
---|
| 703 | RTSOCKET Sock;
|
---|
| 704 | rc = rtSocketCreate(&Sock, AF_INET, SOCK_DGRAM, IPPROTO_UDP, false /*fInheritable*/);
|
---|
| 705 | if (RT_SUCCESS(rc))
|
---|
| 706 | {
|
---|
| 707 | /*
|
---|
| 708 | * Set socket options.
|
---|
| 709 | */
|
---|
| 710 | int fFlag = 1;
|
---|
| 711 | if (!rtSocketSetOpt(Sock, SOL_SOCKET, SO_REUSEADDR, &fFlag, sizeof(fFlag)))
|
---|
| 712 | {
|
---|
| 713 | /*
|
---|
| 714 | * Bind a name to the socket.
|
---|
| 715 | */
|
---|
| 716 | rc = rtSocketBind(Sock, &LocalAddr);
|
---|
| 717 | if (RT_SUCCESS(rc))
|
---|
| 718 | {
|
---|
| 719 | *pSock = Sock;
|
---|
| 720 | return VINF_SUCCESS;
|
---|
| 721 | }
|
---|
| 722 | }
|
---|
| 723 | RTSocketClose(Sock);
|
---|
| 724 | }
|
---|
| 725 | return rc;
|
---|
| 726 | }
|
---|