[1] | 1 | /** @file
|
---|
[37196] | 2 | * IPRT - UDP/IP.
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[98103] | 6 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 7 | *
|
---|
[96407] | 8 | * This file is part of VirtualBox base platform packages, as
|
---|
| 9 | * available from https://www.virtualbox.org.
|
---|
[5999] | 10 | *
|
---|
[96407] | 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, in version 3 of the
|
---|
| 14 | * License.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful, but
|
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | * 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, see <https://www.gnu.org/licenses>.
|
---|
| 23 | *
|
---|
[5999] | 24 | * The contents of this file may alternatively be used under the terms
|
---|
| 25 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 28 | * CDDL are applicable instead of those of the GPL.
|
---|
| 29 | *
|
---|
| 30 | * You may elect to license modified versions of this file under the
|
---|
| 31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 32 | *
|
---|
| 33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 34 | */
|
---|
| 35 |
|
---|
[76557] | 36 | #ifndef IPRT_INCLUDED_udp_h
|
---|
| 37 | #define IPRT_INCLUDED_udp_h
|
---|
[76507] | 38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 39 | # pragma once
|
---|
| 40 | #endif
|
---|
[1] | 41 |
|
---|
| 42 | #include <iprt/cdefs.h>
|
---|
| 43 | #include <iprt/types.h>
|
---|
| 44 | #include <iprt/thread.h>
|
---|
[26588] | 45 | #include <iprt/net.h>
|
---|
[30270] | 46 | #include <iprt/sg.h>
|
---|
[32276] | 47 | #include <iprt/socket.h>
|
---|
[1] | 48 |
|
---|
| 49 | #ifdef IN_RING0
|
---|
| 50 | # error "There are no RTFile APIs available Ring-0 Host Context!"
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 |
|
---|
[20374] | 54 | RT_C_DECLS_BEGIN
|
---|
[1] | 55 |
|
---|
[37196] | 56 | /** @defgroup grp_rt_udp RTUdp - UDP/IP
|
---|
[1] | 57 | * @ingroup grp_rt
|
---|
| 58 | * @{
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
[37196] | 63 | * Handle incoming UDP datagrams.
|
---|
[1] | 64 | *
|
---|
| 65 | * @returns iprt status code.
|
---|
[37196] | 66 | * @returns VERR_UDP_SERVER_STOP to terminate the server loop forcing
|
---|
| 67 | * the RTUdpCreateServer() call to return.
|
---|
| 68 | * @param Sock The socket on which the datagram needs to be received.
|
---|
[1] | 69 | * @param pvUser User argument.
|
---|
| 70 | */
|
---|
[85121] | 71 | typedef DECLCALLBACKTYPE(int, FNRTUDPSERVE,(RTSOCKET Sock, void *pvUser));
|
---|
[37196] | 72 | /** Pointer to a RTUDPSERVE(). */
|
---|
| 73 | typedef FNRTUDPSERVE *PFNRTUDPSERVE;
|
---|
[1] | 74 |
|
---|
| 75 | /**
|
---|
[37196] | 76 | * Create single datagram at a time UDP Server in a separate thread.
|
---|
[1] | 77 | *
|
---|
[37196] | 78 | * The thread will loop accepting datagrams and call pfnServe for
|
---|
| 79 | * each of the incoming datagrams in turn. The pfnServe function can
|
---|
| 80 | * return VERR_UDP_SERVER_STOP too terminate this loop. RTUdpServerDestroy()
|
---|
[1] | 81 | * should be used to terminate the server.
|
---|
| 82 | *
|
---|
| 83 | * @returns iprt status code.
|
---|
[37196] | 84 | * @param pszAddress The address for creating a datagram socket.
|
---|
[1] | 85 | * If NULL or empty string the server is bound to all interfaces.
|
---|
[37196] | 86 | * @param uPort The port for creating a datagram socket.
|
---|
[1] | 87 | * @param enmType The thread type.
|
---|
| 88 | * @param pszThrdName The name of the worker thread.
|
---|
[37196] | 89 | * @param pfnServe The function which will handle incoming datagrams.
|
---|
[1] | 90 | * @param pvUser User argument passed to pfnServe.
|
---|
| 91 | * @param ppServer Where to store the serverhandle.
|
---|
| 92 | */
|
---|
[37196] | 93 | RTR3DECL(int) RTUdpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
|
---|
| 94 | PFNRTUDPSERVE pfnServe, void *pvUser, PPRTUDPSERVER ppServer);
|
---|
[1] | 95 |
|
---|
| 96 | /**
|
---|
[37196] | 97 | * Create single datagram at a time UDP Server.
|
---|
| 98 | * The caller must call RTUdpServerReceive() to actually start the server.
|
---|
[1] | 99 | *
|
---|
| 100 | * @returns iprt status code.
|
---|
[37196] | 101 | * @param pszAddress The address for creating a datagram socket.
|
---|
[1] | 102 | * If NULL the server is bound to all interfaces.
|
---|
[37196] | 103 | * @param uPort The port for creating a datagram socket.
|
---|
[1] | 104 | * @param ppServer Where to store the serverhandle.
|
---|
| 105 | */
|
---|
[37196] | 106 | RTR3DECL(int) RTUdpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTUDPSERVER ppServer);
|
---|
[1] | 107 |
|
---|
| 108 | /**
|
---|
[37196] | 109 | * Shuts down the server.
|
---|
[1] | 110 | *
|
---|
[37196] | 111 | * @returns IPRT status code.
|
---|
[1] | 112 | * @param pServer Handle to the server.
|
---|
| 113 | */
|
---|
[37196] | 114 | RTR3DECL(int) RTUdpServerShutdown(PRTUDPSERVER pServer);
|
---|
[1] | 115 |
|
---|
| 116 | /**
|
---|
[37196] | 117 | * Closes down and frees a UDP Server.
|
---|
[1] | 118 | *
|
---|
| 119 | * @returns iprt status code.
|
---|
[932] | 120 | * @param pServer Handle to the server.
|
---|
| 121 | */
|
---|
[37196] | 122 | RTR3DECL(int) RTUdpServerDestroy(PRTUDPSERVER pServer);
|
---|
[932] | 123 |
|
---|
| 124 | /**
|
---|
[37196] | 125 | * Listen for incoming datagrams.
|
---|
[27787] | 126 | *
|
---|
[37196] | 127 | * The function will loop waiting for datagrams and call pfnServe for
|
---|
| 128 | * each of the incoming datagrams in turn. The pfnServe function can
|
---|
| 129 | * return VERR_UDP_SERVER_STOP too terminate this loop. A stopped server
|
---|
| 130 | * can only be destroyed.
|
---|
[23625] | 131 | *
|
---|
[1] | 132 | * @returns iprt status code.
|
---|
[37196] | 133 | * @param pServer The server handle as returned from RTUdpServerCreateEx().
|
---|
| 134 | * @param pfnServe The function which will handle incoming datagrams.
|
---|
| 135 | * @param pvUser User argument passed to pfnServe.
|
---|
[1] | 136 | */
|
---|
[37196] | 137 | RTR3DECL(int) RTUdpServerListen(PRTUDPSERVER pServer, PFNRTUDPSERVE pfnServe, void *pvUser);
|
---|
[1] | 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Receive data from a socket.
|
---|
| 141 | *
|
---|
| 142 | * @returns iprt status code.
|
---|
| 143 | * @param Sock Socket descriptor.
|
---|
| 144 | * @param pvBuffer Where to put the data we read.
|
---|
| 145 | * @param cbBuffer Read buffer size.
|
---|
[37196] | 146 | * @param pcbRead Number of bytes read. Must be non-NULL.
|
---|
[57944] | 147 | * @param pSrcAddr The network address to read from.
|
---|
[1] | 148 | */
|
---|
[37196] | 149 | RTR3DECL(int) RTUdpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr);
|
---|
[1] | 150 |
|
---|
| 151 | /**
|
---|
[27497] | 152 | * Send data to a socket.
|
---|
[1] | 153 | *
|
---|
| 154 | * @returns iprt status code.
|
---|
[26683] | 155 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
| 156 | *
|
---|
[37196] | 157 | * @param pServer Handle to the server.
|
---|
[1] | 158 | * @param pvBuffer Buffer to write data to socket.
|
---|
| 159 | * @param cbBuffer How much to write.
|
---|
[37196] | 160 | * @param pDstAddr Destination address.
|
---|
[1] | 161 | */
|
---|
[37196] | 162 | RTR3DECL(int) RTUdpWrite(PRTUDPSERVER pServer, const void *pvBuffer,
|
---|
| 163 | size_t cbBuffer, PCRTNETADDR pDstAddr);
|
---|
[1] | 164 |
|
---|
[57955] | 165 | /**
|
---|
| 166 | * Create and connect a data socket.
|
---|
| 167 | *
|
---|
| 168 | * @returns iprt status code.
|
---|
[57957] | 169 | * @param pszAddress The address to connect to.
|
---|
| 170 | * @param uPort The port to connect to.
|
---|
[57970] | 171 | * @param pLocalAddr The local address to bind this socket to, can be
|
---|
| 172 | * NULL.
|
---|
[57957] | 173 | * @param pSock Where to store the handle to the established connection.
|
---|
[57955] | 174 | */
|
---|
[57970] | 175 | RTR3DECL(int) RTUdpCreateClientSocket(const char *pszAddress, uint32_t uPort, PRTNETADDR pLocalAddr, PRTSOCKET pSock);
|
---|
[57955] | 176 |
|
---|
[96864] | 177 | /**
|
---|
| 178 | * Create a data socket acting as a server.
|
---|
| 179 | *
|
---|
| 180 | * @returns iprt status code.
|
---|
| 181 | * @param pszAddress The address to connect to.
|
---|
| 182 | * @param uPort The port to connect to.
|
---|
| 183 | * @param pSock Where to store the handle to the established connection.
|
---|
| 184 | */
|
---|
| 185 | RTR3DECL(int) RTUdpCreateServerSocket(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
|
---|
| 186 |
|
---|
[1] | 187 | /** @} */
|
---|
[20374] | 188 | RT_C_DECLS_END
|
---|
[1] | 189 |
|
---|
[76585] | 190 | #endif /* !IPRT_INCLUDED_udp_h */
|
---|
[1] | 191 |
|
---|