[55260] | 1 | /* $Id: DrvTCP.cpp 103425 2024-02-19 11:12:14Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * TCP socket driver implementing the IStream interface.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[69419] | 7 | * Contributed by Alexey Eromenko (derived from DrvNamedPipe).
|
---|
[55260] | 8 | *
|
---|
[98103] | 9 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[55260] | 10 | *
|
---|
[96407] | 11 | * This file is part of VirtualBox base platform packages, as
|
---|
| 12 | * available from https://www.virtualbox.org.
|
---|
| 13 | *
|
---|
| 14 | * This program is free software; you can redistribute it and/or
|
---|
| 15 | * modify it under the terms of the GNU General Public License
|
---|
| 16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 17 | * License.
|
---|
| 18 | *
|
---|
| 19 | * This program is distributed in the hope that it will be useful, but
|
---|
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 22 | * General Public License for more details.
|
---|
| 23 | *
|
---|
| 24 | * You should have received a copy of the GNU General Public License
|
---|
| 25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 26 | *
|
---|
| 27 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[55260] | 28 | */
|
---|
| 29 |
|
---|
| 30 |
|
---|
[57358] | 31 | /*********************************************************************************************************************************
|
---|
| 32 | * Header Files *
|
---|
| 33 | *********************************************************************************************************************************/
|
---|
[55260] | 34 | #define LOG_GROUP LOG_GROUP_DRV_TCP
|
---|
| 35 | #include <VBox/vmm/pdmdrv.h>
|
---|
| 36 | #include <iprt/assert.h>
|
---|
| 37 | #include <iprt/file.h>
|
---|
| 38 | #include <iprt/stream.h>
|
---|
| 39 | #include <iprt/alloc.h>
|
---|
[68699] | 40 | #include <iprt/pipe.h>
|
---|
| 41 | #include <iprt/poll.h>
|
---|
[55260] | 42 | #include <iprt/string.h>
|
---|
| 43 | #include <iprt/semaphore.h>
|
---|
[68699] | 44 | #include <iprt/socket.h>
|
---|
| 45 | #include <iprt/tcp.h>
|
---|
[55260] | 46 | #include <iprt/uuid.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 |
|
---|
| 49 | #include "VBoxDD.h"
|
---|
| 50 |
|
---|
[69419] | 51 |
|
---|
[57358] | 52 | /*********************************************************************************************************************************
|
---|
| 53 | * Defined Constants And Macros *
|
---|
| 54 | *********************************************************************************************************************************/
|
---|
[55260] | 55 |
|
---|
[68699] | 56 | #define DRVTCP_POLLSET_ID_SOCKET 0
|
---|
| 57 | #define DRVTCP_POLLSET_ID_WAKEUP 1
|
---|
[57358] | 58 |
|
---|
[68699] | 59 | #define DRVTCP_WAKEUP_REASON_EXTERNAL 0
|
---|
| 60 | #define DRVTCP_WAKEUP_REASON_NEW_CONNECTION 1
|
---|
| 61 |
|
---|
[69419] | 62 |
|
---|
[57358] | 63 | /*********************************************************************************************************************************
|
---|
| 64 | * Structures and Typedefs *
|
---|
| 65 | *********************************************************************************************************************************/
|
---|
[55260] | 66 | /**
|
---|
| 67 | * TCP driver instance data.
|
---|
| 68 | *
|
---|
| 69 | * @implements PDMISTREAM
|
---|
| 70 | */
|
---|
| 71 | typedef struct DRVTCP
|
---|
| 72 | {
|
---|
| 73 | /** The stream interface. */
|
---|
| 74 | PDMISTREAM IStream;
|
---|
| 75 | /** Pointer to the driver instance. */
|
---|
| 76 | PPDMDRVINS pDrvIns;
|
---|
| 77 | /** Pointer to the TCP server address:port or port only. (Freed by MM) */
|
---|
| 78 | char *pszLocation;
|
---|
| 79 | /** Flag whether VirtualBox represents the server or client side. */
|
---|
| 80 | bool fIsServer;
|
---|
| 81 |
|
---|
[68699] | 82 | /** Handle of the TCP server for incoming connections. */
|
---|
| 83 | PRTTCPSERVER hTcpServ;
|
---|
[55260] | 84 | /** Socket handle of the TCP socket connection. */
|
---|
[68699] | 85 | RTSOCKET hTcpSock;
|
---|
[55260] | 86 |
|
---|
[68699] | 87 | /** Poll set used to wait for I/O events. */
|
---|
| 88 | RTPOLLSET hPollSet;
|
---|
| 89 | /** Reading end of the wakeup pipe. */
|
---|
| 90 | RTPIPE hPipeWakeR;
|
---|
| 91 | /** Writing end of the wakeup pipe. */
|
---|
| 92 | RTPIPE hPipeWakeW;
|
---|
[68717] | 93 | /** Flag whether the send buffer is full nad it is required to wait for more
|
---|
| 94 | * space until there is room again. */
|
---|
| 95 | bool fXmitBufFull;
|
---|
[68699] | 96 |
|
---|
[83584] | 97 | /** Number of connections active. */
|
---|
| 98 | volatile uint32_t cConnections;
|
---|
[55260] | 99 | /** Thread for listening for new connections. */
|
---|
| 100 | RTTHREAD ListenThread;
|
---|
| 101 | /** Flag to signal listening thread to shut down. */
|
---|
| 102 | bool volatile fShutdown;
|
---|
[84688] | 103 | /** Flag to signal whether the thread was woken up from external. */
|
---|
| 104 | bool volatile fWokenUp;
|
---|
[55260] | 105 | } DRVTCP, *PDRVTCP;
|
---|
| 106 |
|
---|
| 107 |
|
---|
[57358] | 108 | /*********************************************************************************************************************************
|
---|
| 109 | * Internal Functions *
|
---|
| 110 | *********************************************************************************************************************************/
|
---|
[55260] | 111 |
|
---|
| 112 |
|
---|
[68699] | 113 | /**
|
---|
[83584] | 114 | * Kicks any possibly polling thread to get informed about changes - extended version
|
---|
| 115 | * sending additional data along with the wakeup reason.
|
---|
| 116 | *
|
---|
| 117 | * @returns VBOx status code.
|
---|
| 118 | * @param pThis The TCP driver instance.
|
---|
| 119 | * @param bReason The reason code to handle.
|
---|
| 120 | * @param pvData The additional to send along with the wakeup reason.
|
---|
| 121 | * @param cbData Number of bytes to send along.
|
---|
| 122 | */
|
---|
| 123 | static int drvTcpPollerKickEx(PDRVTCP pThis, uint8_t bReason, const void *pvData, size_t cbData)
|
---|
| 124 | {
|
---|
| 125 | size_t cbWritten = 0;
|
---|
| 126 | int rc = RTPipeWriteBlocking(pThis->hPipeWakeW, &bReason, 1, &cbWritten);
|
---|
| 127 | if (RT_SUCCESS(rc))
|
---|
| 128 | rc = RTPipeWriteBlocking(pThis->hPipeWakeW, pvData, cbData, &cbWritten);
|
---|
| 129 | return rc;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
[68699] | 134 | * Kicks any possibly polling thread to get informed about changes.
|
---|
| 135 | *
|
---|
| 136 | * @returns VBOx status code.
|
---|
| 137 | * @param pThis The TCP driver instance.
|
---|
| 138 | * @param bReason The reason code to handle.
|
---|
| 139 | */
|
---|
| 140 | static int drvTcpPollerKick(PDRVTCP pThis, uint8_t bReason)
|
---|
| 141 | {
|
---|
| 142 | size_t cbWritten = 0;
|
---|
[83584] | 143 | return RTPipeWriteBlocking(pThis->hPipeWakeW, &bReason, 1, &cbWritten);
|
---|
[68699] | 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
[84157] | 147 | /**
|
---|
[86114] | 148 | * Closes the connection.
|
---|
| 149 | *
|
---|
| 150 | * @param pThis The TCP driver instance.
|
---|
| 151 | */
|
---|
| 152 | static void drvTcpConnectionClose(PDRVTCP pThis)
|
---|
| 153 | {
|
---|
| 154 | Assert(pThis->hTcpSock != NIL_RTSOCKET);
|
---|
| 155 |
|
---|
| 156 | int rc = RTPollSetRemove(pThis->hPollSet, DRVTCP_POLLSET_ID_SOCKET);
|
---|
| 157 | AssertRC(rc);
|
---|
| 158 |
|
---|
| 159 | if (pThis->fIsServer)
|
---|
| 160 | RTTcpServerDisconnectClient2(pThis->hTcpSock);
|
---|
| 161 | else
|
---|
| 162 | RTSocketClose(pThis->hTcpSock);
|
---|
| 163 | pThis->hTcpSock = NIL_RTSOCKET;
|
---|
| 164 | ASMAtomicDecU32(&pThis->cConnections);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
[84157] | 169 | * Checks the wakeup pipe for events.
|
---|
| 170 | *
|
---|
| 171 | * @returns VBox status code.
|
---|
| 172 | * @param pThis The TCP driver instance.
|
---|
| 173 | * @param fEvts Event mask to set if a new connection arrived.
|
---|
| 174 | */
|
---|
| 175 | static int drvTcpWakeupPipeCheckForRequest(PDRVTCP pThis, uint32_t fEvts)
|
---|
| 176 | {
|
---|
[84688] | 177 | int rc = VINF_SUCCESS;
|
---|
| 178 |
|
---|
| 179 | while ( RT_SUCCESS(rc)
|
---|
| 180 | || rc == VERR_INTERRUPTED)
|
---|
[84157] | 181 | {
|
---|
[84688] | 182 | uint8_t bReason;
|
---|
| 183 | size_t cbRead = 0;
|
---|
| 184 | int rc2 = RTPipeRead(pThis->hPipeWakeR, &bReason, 1, &cbRead);
|
---|
| 185 | if (rc2 == VINF_TRY_AGAIN) /* Nothing there so we are done here. */
|
---|
| 186 | break;
|
---|
| 187 | else if (RT_SUCCESS(rc2))
|
---|
[84157] | 188 | {
|
---|
[84688] | 189 | if (bReason == DRVTCP_WAKEUP_REASON_EXTERNAL)
|
---|
| 190 | {
|
---|
| 191 | ASMAtomicXchgBool(&pThis->fWokenUp, false);
|
---|
| 192 | rc = VERR_INTERRUPTED;
|
---|
| 193 | }
|
---|
| 194 | else if (bReason == DRVTCP_WAKEUP_REASON_NEW_CONNECTION)
|
---|
| 195 | {
|
---|
| 196 | Assert(pThis->hTcpSock == NIL_RTSOCKET);
|
---|
[84157] | 197 |
|
---|
[84688] | 198 | /* Read the socket handle. */
|
---|
| 199 | RTSOCKET hTcpSockNew = NIL_RTSOCKET;
|
---|
| 200 | rc = RTPipeReadBlocking(pThis->hPipeWakeR, &hTcpSockNew, sizeof(hTcpSockNew), NULL);
|
---|
| 201 | AssertRC(rc);
|
---|
[84157] | 202 |
|
---|
[84688] | 203 | /* Always include error event. */
|
---|
| 204 | fEvts |= RTPOLL_EVT_ERROR;
|
---|
| 205 | rc = RTPollSetAddSocket(pThis->hPollSet, hTcpSockNew,
|
---|
| 206 | fEvts, DRVTCP_POLLSET_ID_SOCKET);
|
---|
| 207 | if (RT_SUCCESS(rc))
|
---|
| 208 | pThis->hTcpSock = hTcpSockNew;
|
---|
| 209 | }
|
---|
| 210 | else
|
---|
| 211 | AssertMsgFailed(("Unknown wakeup reason in pipe %u\n", bReason));
|
---|
[84157] | 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | return rc;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
[68699] | 219 | /** @interface_method_impl{PDMISTREAM,pfnPoll} */
|
---|
| 220 | static DECLCALLBACK(int) drvTcpPoll(PPDMISTREAM pInterface, uint32_t fEvts, uint32_t *pfEvts, RTMSINTERVAL cMillies)
|
---|
| 221 | {
|
---|
| 222 | int rc = VINF_SUCCESS;
|
---|
| 223 | PDRVTCP pThis = RT_FROM_MEMBER(pInterface, DRVTCP, IStream);
|
---|
| 224 |
|
---|
| 225 | if (pThis->hTcpSock != NIL_RTSOCKET)
|
---|
| 226 | {
|
---|
[83584] | 227 | Assert(ASMAtomicReadU32(&pThis->cConnections) > 0);
|
---|
| 228 |
|
---|
| 229 | /* Always include error event. */
|
---|
| 230 | fEvts |= RTPOLL_EVT_ERROR;
|
---|
| 231 | rc = RTPollSetEventsChange(pThis->hPollSet, DRVTCP_POLLSET_ID_SOCKET, fEvts);
|
---|
| 232 | AssertRC(rc);
|
---|
[68699] | 233 | }
|
---|
[84157] | 234 | else
|
---|
| 235 | {
|
---|
| 236 | /*
|
---|
| 237 | * Check whether new connection arrived first so we don't miss it in case
|
---|
| 238 | * the guest is constantly writing data and we always end up here.
|
---|
| 239 | */
|
---|
| 240 | rc = drvTcpWakeupPipeCheckForRequest(pThis, fEvts);
|
---|
| 241 | if ( pThis->hTcpSock == NIL_RTSOCKET
|
---|
| 242 | && (fEvts & RTPOLL_EVT_WRITE))
|
---|
| 243 | {
|
---|
| 244 | /*
|
---|
| 245 | * Just pretend we can always write to not fill up any buffers and block the guest
|
---|
| 246 | * from sending data.
|
---|
| 247 | */
|
---|
| 248 | *pfEvts |= RTPOLL_EVT_WRITE;
|
---|
| 249 | return rc;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
[68699] | 252 |
|
---|
| 253 | if (RT_SUCCESS(rc))
|
---|
| 254 | {
|
---|
| 255 | while (RT_SUCCESS(rc))
|
---|
| 256 | {
|
---|
| 257 | uint32_t fEvtsRecv = 0;
|
---|
| 258 | uint32_t idHnd = 0;
|
---|
[83584] | 259 | uint64_t tsStartMs = RTTimeMilliTS();
|
---|
| 260 | RTMSINTERVAL cThisWaitMs = cMillies;
|
---|
[68699] | 261 |
|
---|
[73618] | 262 | /*
|
---|
| 263 | * Just check for data available to be read if the send buffer wasn't full till now and
|
---|
| 264 | * the caller wants to check whether writing is possible with the event set.
|
---|
| 265 | *
|
---|
| 266 | * On Windows the write event is only posted after a send operation returned
|
---|
| 267 | * WSAEWOULDBLOCK. So without this we would block in the poll call below waiting
|
---|
| 268 | * for an event which would never happen if the buffer has space left.
|
---|
| 269 | */
|
---|
| 270 | if ( (fEvts & RTPOLL_EVT_WRITE)
|
---|
| 271 | && !pThis->fXmitBufFull
|
---|
[83584] | 272 | && pThis->hTcpSock != NIL_RTSOCKET)
|
---|
| 273 | cThisWaitMs = 0;
|
---|
[73618] | 274 |
|
---|
[83584] | 275 | rc = RTPoll(pThis->hPollSet, cThisWaitMs, &fEvtsRecv, &idHnd);
|
---|
| 276 |
|
---|
| 277 | /* Adjust remaining time to wait. */
|
---|
| 278 | uint64_t tsPollSpanMs = RTTimeMilliTS() - tsStartMs;
|
---|
[103425] | 279 | Assert(tsPollSpanMs == (RTMSINTERVAL)tsPollSpanMs);
|
---|
| 280 |
|
---|
| 281 | cMillies -= RT_MIN(cMillies, (RTMSINTERVAL)tsPollSpanMs);
|
---|
[68699] | 282 | if (RT_SUCCESS(rc))
|
---|
| 283 | {
|
---|
| 284 | if (idHnd == DRVTCP_POLLSET_ID_WAKEUP)
|
---|
| 285 | {
|
---|
| 286 | /* We got woken up, drain the pipe and return. */
|
---|
[84157] | 287 | rc = drvTcpWakeupPipeCheckForRequest(pThis, fEvts);
|
---|
[68699] | 288 | }
|
---|
| 289 | else
|
---|
| 290 | {
|
---|
| 291 | Assert(idHnd == DRVTCP_POLLSET_ID_SOCKET);
|
---|
| 292 |
|
---|
| 293 | /* On error we close the socket here. */
|
---|
| 294 | if (fEvtsRecv & RTPOLL_EVT_ERROR)
|
---|
[86114] | 295 | drvTcpConnectionClose(pThis); /* Continue with polling afterwards. */
|
---|
[68699] | 296 | else
|
---|
| 297 | {
|
---|
[68717] | 298 | if (fEvtsRecv & RTPOLL_EVT_WRITE)
|
---|
| 299 | pThis->fXmitBufFull = false;
|
---|
[73618] | 300 | else if (!pThis->fXmitBufFull)
|
---|
| 301 | fEvtsRecv |= RTPOLL_EVT_WRITE;
|
---|
[68699] | 302 | *pfEvts = fEvtsRecv;
|
---|
| 303 | break;
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
[73618] | 307 | else if ( rc == VERR_TIMEOUT
|
---|
| 308 | && !pThis->fXmitBufFull)
|
---|
| 309 | {
|
---|
| 310 | *pfEvts = RTPOLL_EVT_WRITE;
|
---|
| 311 | rc = VINF_SUCCESS;
|
---|
| 312 | break;
|
---|
| 313 | }
|
---|
[68699] | 314 | }
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | return rc;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | /** @interface_method_impl{PDMISTREAM,pfnPollInterrupt} */
|
---|
| 322 | static DECLCALLBACK(int) drvTcpPollInterrupt(PPDMISTREAM pInterface)
|
---|
| 323 | {
|
---|
[84688] | 324 | int rc = VINF_SUCCESS;
|
---|
[68699] | 325 | PDRVTCP pThis = RT_FROM_MEMBER(pInterface, DRVTCP, IStream);
|
---|
[84688] | 326 |
|
---|
| 327 | if (!ASMAtomicXchgBool(&pThis->fWokenUp, true))
|
---|
| 328 | rc = drvTcpPollerKick(pThis, DRVTCP_WAKEUP_REASON_EXTERNAL);
|
---|
| 329 |
|
---|
| 330 | return rc;
|
---|
[68699] | 331 | }
|
---|
| 332 |
|
---|
| 333 |
|
---|
[62956] | 334 | /** @interface_method_impl{PDMISTREAM,pfnRead} */
|
---|
[68699] | 335 | static DECLCALLBACK(int) drvTcpRead(PPDMISTREAM pInterface, void *pvBuf, size_t *pcbRead)
|
---|
[55260] | 336 | {
|
---|
| 337 | int rc = VINF_SUCCESS;
|
---|
[68699] | 338 | PDRVTCP pThis = RT_FROM_MEMBER(pInterface, DRVTCP, IStream);
|
---|
[55260] | 339 | LogFlow(("%s: pvBuf=%p *pcbRead=%#x (%s)\n", __FUNCTION__, pvBuf, *pcbRead, pThis->pszLocation));
|
---|
| 340 |
|
---|
| 341 | Assert(pvBuf);
|
---|
| 342 |
|
---|
[68699] | 343 | if (pThis->hTcpSock != NIL_RTSOCKET)
|
---|
[55260] | 344 | {
|
---|
[68699] | 345 | size_t cbRead;
|
---|
| 346 | size_t cbBuf = *pcbRead;
|
---|
| 347 | rc = RTSocketReadNB(pThis->hTcpSock, pvBuf, cbBuf, &cbRead);
|
---|
| 348 | if (RT_SUCCESS(rc))
|
---|
[55260] | 349 | {
|
---|
[68699] | 350 | if (!cbRead && rc != VINF_TRY_AGAIN)
|
---|
| 351 | {
|
---|
[86114] | 352 | drvTcpConnectionClose(pThis);
|
---|
[68699] | 353 | rc = VINF_SUCCESS;
|
---|
| 354 | }
|
---|
| 355 | *pcbRead = cbRead;
|
---|
[55260] | 356 | }
|
---|
| 357 | }
|
---|
| 358 | else
|
---|
| 359 | {
|
---|
| 360 | RTThreadSleep(100);
|
---|
| 361 | *pcbRead = 0;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | LogFlow(("%s: *pcbRead=%zu returns %Rrc\n", __FUNCTION__, *pcbRead, rc));
|
---|
| 365 | return rc;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 |
|
---|
[62956] | 369 | /** @interface_method_impl{PDMISTREAM,pfnWrite} */
|
---|
[68699] | 370 | static DECLCALLBACK(int) drvTcpWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *pcbWrite)
|
---|
[55260] | 371 | {
|
---|
| 372 | int rc = VINF_SUCCESS;
|
---|
[68699] | 373 | PDRVTCP pThis = RT_FROM_MEMBER(pInterface, DRVTCP, IStream);
|
---|
[55260] | 374 | LogFlow(("%s: pvBuf=%p *pcbWrite=%#x (%s)\n", __FUNCTION__, pvBuf, *pcbWrite, pThis->pszLocation));
|
---|
| 375 |
|
---|
| 376 | Assert(pvBuf);
|
---|
[68699] | 377 | if (pThis->hTcpSock != NIL_RTSOCKET)
|
---|
[55260] | 378 | {
|
---|
[68699] | 379 | size_t cbBuf = *pcbWrite;
|
---|
| 380 | rc = RTSocketWriteNB(pThis->hTcpSock, pvBuf, cbBuf, pcbWrite);
|
---|
[68717] | 381 | if (rc == VINF_TRY_AGAIN)
|
---|
| 382 | {
|
---|
| 383 | Assert(*pcbWrite == 0);
|
---|
| 384 | pThis->fXmitBufFull = true;
|
---|
| 385 | rc = VERR_TIMEOUT;
|
---|
| 386 | }
|
---|
[55260] | 387 | }
|
---|
[84157] | 388 | /* else Just pretend we wrote everything to not block. */
|
---|
[55260] | 389 |
|
---|
[68717] | 390 | LogFlow(("%s: returns %Rrc *pcbWrite=%zu\n", __FUNCTION__, rc, *pcbWrite));
|
---|
[55260] | 391 | return rc;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 |
|
---|
| 395 | /**
|
---|
| 396 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
| 397 | */
|
---|
| 398 | static DECLCALLBACK(void *) drvTCPQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
| 399 | {
|
---|
| 400 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
| 401 | PDRVTCP pThis = PDMINS_2_DATA(pDrvIns, PDRVTCP);
|
---|
| 402 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
| 403 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMISTREAM, &pThis->IStream);
|
---|
| 404 | return NULL;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 |
|
---|
| 408 | /* -=-=-=-=- listen thread -=-=-=-=- */
|
---|
| 409 |
|
---|
| 410 | /**
|
---|
| 411 | * Receive thread loop.
|
---|
| 412 | *
|
---|
[62908] | 413 | * @returns VINF_SUCCESS
|
---|
| 414 | * @param hThreadSelf Thread handle to this thread.
|
---|
[55260] | 415 | * @param pvUser User argument.
|
---|
| 416 | */
|
---|
[62908] | 417 | static DECLCALLBACK(int) drvTCPListenLoop(RTTHREAD hThreadSelf, void *pvUser)
|
---|
[55260] | 418 | {
|
---|
[62908] | 419 | RT_NOREF(hThreadSelf);
|
---|
[55260] | 420 | PDRVTCP pThis = (PDRVTCP)pvUser;
|
---|
| 421 |
|
---|
| 422 | while (RT_LIKELY(!pThis->fShutdown))
|
---|
| 423 | {
|
---|
[68699] | 424 | RTSOCKET hTcpSockNew = NIL_RTSOCKET;
|
---|
| 425 | int rc = RTTcpServerListen2(pThis->hTcpServ, &hTcpSockNew);
|
---|
| 426 | if (RT_SUCCESS(rc))
|
---|
[55260] | 427 | {
|
---|
[83584] | 428 | if (ASMAtomicReadU32(&pThis->cConnections) > 0)
|
---|
[68699] | 429 | {
|
---|
| 430 | LogRel(("DrvTCP%d: only single connection supported\n", pThis->pDrvIns->iInstance));
|
---|
| 431 | RTTcpServerDisconnectClient2(hTcpSockNew);
|
---|
| 432 | }
|
---|
| 433 | else
|
---|
| 434 | {
|
---|
[83584] | 435 | ASMAtomicIncU32(&pThis->cConnections);
|
---|
| 436 |
|
---|
[68699] | 437 | /* Inform the poller about the new socket. */
|
---|
[83584] | 438 | drvTcpPollerKickEx(pThis, DRVTCP_WAKEUP_REASON_NEW_CONNECTION, &hTcpSockNew, sizeof(hTcpSockNew));
|
---|
[68699] | 439 | }
|
---|
[55260] | 440 | }
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | return VINF_SUCCESS;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
| 447 |
|
---|
| 448 | /**
|
---|
| 449 | * Common worker for drvTCPPowerOff and drvTCPDestructor.
|
---|
| 450 | *
|
---|
| 451 | * @param pThis The instance data.
|
---|
| 452 | */
|
---|
| 453 | static void drvTCPShutdownListener(PDRVTCP pThis)
|
---|
| 454 | {
|
---|
| 455 | /*
|
---|
| 456 | * Signal shutdown of the listener thread.
|
---|
| 457 | */
|
---|
| 458 | pThis->fShutdown = true;
|
---|
| 459 | if ( pThis->fIsServer
|
---|
[68699] | 460 | && pThis->hTcpServ != NULL)
|
---|
[55260] | 461 | {
|
---|
[68699] | 462 | int rc = RTTcpServerShutdown(pThis->hTcpServ);
|
---|
| 463 | AssertRC(rc);
|
---|
| 464 | pThis->hTcpServ = NULL;
|
---|
[55260] | 465 | }
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 |
|
---|
| 469 | /**
|
---|
| 470 | * Power off a TCP socket stream driver instance.
|
---|
| 471 | *
|
---|
| 472 | * This does most of the destruction work, to avoid ordering dependencies.
|
---|
| 473 | *
|
---|
| 474 | * @param pDrvIns The driver instance data.
|
---|
| 475 | */
|
---|
| 476 | static DECLCALLBACK(void) drvTCPPowerOff(PPDMDRVINS pDrvIns)
|
---|
| 477 | {
|
---|
| 478 | PDRVTCP pThis = PDMINS_2_DATA(pDrvIns, PDRVTCP);
|
---|
| 479 | LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
|
---|
| 480 |
|
---|
| 481 | drvTCPShutdownListener(pThis);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 |
|
---|
| 485 | /**
|
---|
| 486 | * Destruct a TCP socket stream driver instance.
|
---|
| 487 | *
|
---|
| 488 | * Most VM resources are freed by the VM. This callback is provided so that
|
---|
| 489 | * any non-VM resources can be freed correctly.
|
---|
| 490 | *
|
---|
| 491 | * @param pDrvIns The driver instance data.
|
---|
| 492 | */
|
---|
| 493 | static DECLCALLBACK(void) drvTCPDestruct(PPDMDRVINS pDrvIns)
|
---|
| 494 | {
|
---|
| 495 | PDRVTCP pThis = PDMINS_2_DATA(pDrvIns, PDRVTCP);
|
---|
| 496 | LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
|
---|
| 497 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
| 498 |
|
---|
| 499 | drvTCPShutdownListener(pThis);
|
---|
| 500 |
|
---|
| 501 | /*
|
---|
| 502 | * While the thread exits, clean up as much as we can.
|
---|
| 503 | */
|
---|
[68699] | 504 | if (pThis->hTcpSock != NIL_RTSOCKET)
|
---|
| 505 | {
|
---|
| 506 | int rc = RTPollSetRemove(pThis->hPollSet, DRVTCP_POLLSET_ID_SOCKET);
|
---|
| 507 | AssertRC(rc);
|
---|
[55260] | 508 |
|
---|
[68699] | 509 | rc = RTSocketShutdown(pThis->hTcpSock, true /* fRead */, true /* fWrite */);
|
---|
| 510 | AssertRC(rc);
|
---|
| 511 |
|
---|
| 512 | rc = RTSocketClose(pThis->hTcpSock);
|
---|
| 513 | AssertRC(rc); RT_NOREF(rc);
|
---|
| 514 |
|
---|
| 515 | pThis->hTcpSock = NIL_RTSOCKET;
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | if (pThis->hPipeWakeR != NIL_RTPIPE)
|
---|
[55260] | 519 | {
|
---|
[68699] | 520 | int rc = RTPipeClose(pThis->hPipeWakeR);
|
---|
| 521 | AssertRC(rc);
|
---|
[55260] | 522 |
|
---|
[68699] | 523 | pThis->hPipeWakeR = NIL_RTPIPE;
|
---|
[55260] | 524 | }
|
---|
| 525 |
|
---|
[68699] | 526 | if (pThis->hPipeWakeW != NIL_RTPIPE)
|
---|
| 527 | {
|
---|
| 528 | int rc = RTPipeClose(pThis->hPipeWakeW);
|
---|
| 529 | AssertRC(rc);
|
---|
[55260] | 530 |
|
---|
[68699] | 531 | pThis->hPipeWakeW = NIL_RTPIPE;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | if (pThis->hPollSet != NIL_RTPOLLSET)
|
---|
| 535 | {
|
---|
| 536 | int rc = RTPollSetDestroy(pThis->hPollSet);
|
---|
| 537 | AssertRC(rc);
|
---|
| 538 |
|
---|
| 539 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[91897] | 542 | PDMDrvHlpMMHeapFree(pDrvIns, pThis->pszLocation);
|
---|
[55260] | 543 | pThis->pszLocation = NULL;
|
---|
| 544 |
|
---|
| 545 | /*
|
---|
| 546 | * Wait for the thread.
|
---|
| 547 | */
|
---|
| 548 | if (pThis->ListenThread != NIL_RTTHREAD)
|
---|
| 549 | {
|
---|
| 550 | int rc = RTThreadWait(pThis->ListenThread, 30000, NULL);
|
---|
| 551 | if (RT_SUCCESS(rc))
|
---|
| 552 | pThis->ListenThread = NIL_RTTHREAD;
|
---|
| 553 | else
|
---|
| 554 | LogRel(("DrvTCP%d: listen thread did not terminate (%Rrc)\n", pDrvIns->iInstance, rc));
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
| 560 | * Construct a TCP socket stream driver instance.
|
---|
| 561 | *
|
---|
| 562 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
| 563 | */
|
---|
| 564 | static DECLCALLBACK(int) drvTCPConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
| 565 | {
|
---|
[62908] | 566 | RT_NOREF(fFlags);
|
---|
| 567 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
[91868] | 568 | PDRVTCP pThis = PDMINS_2_DATA(pDrvIns, PDRVTCP);
|
---|
| 569 | PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
|
---|
[55260] | 570 |
|
---|
| 571 | /*
|
---|
| 572 | * Init the static parts.
|
---|
| 573 | */
|
---|
| 574 | pThis->pDrvIns = pDrvIns;
|
---|
| 575 | pThis->pszLocation = NULL;
|
---|
| 576 | pThis->fIsServer = false;
|
---|
[83584] | 577 | pThis->fXmitBufFull = false;
|
---|
| 578 | pThis->cConnections = 0;
|
---|
[55260] | 579 |
|
---|
[68699] | 580 | pThis->hTcpServ = NULL;
|
---|
| 581 | pThis->hTcpSock = NIL_RTSOCKET;
|
---|
[55260] | 582 |
|
---|
[68699] | 583 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
| 584 | pThis->hPipeWakeR = NIL_RTPIPE;
|
---|
| 585 | pThis->hPipeWakeW = NIL_RTPIPE;
|
---|
| 586 |
|
---|
[55260] | 587 | pThis->ListenThread = NIL_RTTHREAD;
|
---|
| 588 | pThis->fShutdown = false;
|
---|
[84688] | 589 | pThis->fWokenUp = false;
|
---|
[55260] | 590 | /* IBase */
|
---|
| 591 | pDrvIns->IBase.pfnQueryInterface = drvTCPQueryInterface;
|
---|
| 592 | /* IStream */
|
---|
[68699] | 593 | pThis->IStream.pfnPoll = drvTcpPoll;
|
---|
| 594 | pThis->IStream.pfnPollInterrupt = drvTcpPollInterrupt;
|
---|
| 595 | pThis->IStream.pfnRead = drvTcpRead;
|
---|
| 596 | pThis->IStream.pfnWrite = drvTcpWrite;
|
---|
[55260] | 597 |
|
---|
| 598 | /*
|
---|
| 599 | * Validate and read the configuration.
|
---|
| 600 | */
|
---|
| 601 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "Location|IsServer", "");
|
---|
| 602 |
|
---|
[91868] | 603 | int rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "Location", &pThis->pszLocation);
|
---|
[55260] | 604 | if (RT_FAILURE(rc))
|
---|
| 605 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 606 | N_("Configuration error: querying \"Location\" resulted in %Rrc"), rc);
|
---|
[91868] | 607 | rc = pHlp->pfnCFGMQueryBool(pCfg, "IsServer", &pThis->fIsServer);
|
---|
[55260] | 608 | if (RT_FAILURE(rc))
|
---|
| 609 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 610 | N_("Configuration error: querying \"IsServer\" resulted in %Rrc"), rc);
|
---|
| 611 |
|
---|
[68699] | 612 | rc = RTPipeCreate(&pThis->hPipeWakeR, &pThis->hPipeWakeW, 0 /* fFlags */);
|
---|
| 613 | if (RT_FAILURE(rc))
|
---|
| 614 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 615 | N_("DrvTCP#%d: Failed to create wake pipe"), pDrvIns->iInstance);
|
---|
| 616 |
|
---|
| 617 | rc = RTPollSetCreate(&pThis->hPollSet);
|
---|
| 618 | if (RT_FAILURE(rc))
|
---|
| 619 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 620 | N_("DrvTCP#%d: Failed to create poll set"), pDrvIns->iInstance);
|
---|
| 621 |
|
---|
| 622 | rc = RTPollSetAddPipe(pThis->hPollSet, pThis->hPipeWakeR,
|
---|
| 623 | RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
| 624 | DRVTCP_POLLSET_ID_WAKEUP);
|
---|
| 625 | if (RT_FAILURE(rc))
|
---|
| 626 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 627 | N_("DrvTCP#%d failed to add wakeup pipe for %s to poll set"),
|
---|
| 628 | pDrvIns->iInstance, pThis->pszLocation);
|
---|
| 629 |
|
---|
[55260] | 630 | /*
|
---|
| 631 | * Create/Open the socket.
|
---|
| 632 | */
|
---|
| 633 | if (pThis->fIsServer)
|
---|
| 634 | {
|
---|
[68699] | 635 | uint32_t uPort = 0;
|
---|
| 636 | rc = RTStrToUInt32Ex(pThis->pszLocation, NULL, 10, &uPort);
|
---|
| 637 | if (RT_FAILURE(rc))
|
---|
| 638 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 639 | N_("DrvTCP#%d: The port part of the location is not a numerical value"),
|
---|
| 640 | pDrvIns->iInstance);
|
---|
[55260] | 641 |
|
---|
[69419] | 642 | /** @todo Allow binding to distinct interfaces. */
|
---|
[68699] | 643 | rc = RTTcpServerCreateEx(NULL, uPort, &pThis->hTcpServ);
|
---|
| 644 | if (RT_FAILURE(rc))
|
---|
| 645 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 646 | N_("DrvTCP#%d failed to create server socket"), pDrvIns->iInstance);
|
---|
| 647 |
|
---|
[55260] | 648 | rc = RTThreadCreate(&pThis->ListenThread, drvTCPListenLoop, (void *)pThis, 0,
|
---|
| 649 | RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "DrvTCPStream");
|
---|
| 650 | if (RT_FAILURE(rc))
|
---|
| 651 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 652 | N_("DrvTCP#%d failed to create listening thread"), pDrvIns->iInstance);
|
---|
| 653 | }
|
---|
| 654 | else
|
---|
| 655 | {
|
---|
[68699] | 656 | char *pszPort = strchr(pThis->pszLocation, ':');
|
---|
| 657 | if (!pszPort)
|
---|
| 658 | return PDMDrvHlpVMSetError(pDrvIns, VERR_NOT_FOUND, RT_SRC_POS,
|
---|
| 659 | N_("DrvTCP#%d: The location misses the port to connect to"),
|
---|
| 660 | pDrvIns->iInstance);
|
---|
[55260] | 661 |
|
---|
[68699] | 662 | *pszPort = '\0'; /* Overwrite temporarily to avoid copying the hostname into a temporary buffer. */
|
---|
| 663 | uint32_t uPort = 0;
|
---|
| 664 | rc = RTStrToUInt32Ex(pszPort + 1, NULL, 10, &uPort);
|
---|
| 665 | if (RT_FAILURE(rc))
|
---|
| 666 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 667 | N_("DrvTCP#%d: The port part of the location is not a numerical value"),
|
---|
| 668 | pDrvIns->iInstance);
|
---|
| 669 |
|
---|
| 670 | rc = RTTcpClientConnect(pThis->pszLocation, uPort, &pThis->hTcpSock);
|
---|
| 671 | *pszPort = ':'; /* Restore delimiter before checking the status. */
|
---|
| 672 | if (RT_FAILURE(rc))
|
---|
| 673 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
[55260] | 674 | N_("DrvTCP#%d failed to connect to socket %s"),
|
---|
| 675 | pDrvIns->iInstance, pThis->pszLocation);
|
---|
[68699] | 676 |
|
---|
| 677 | rc = RTPollSetAddSocket(pThis->hPollSet, pThis->hTcpSock,
|
---|
| 678 | RTPOLL_EVT_READ | RTPOLL_EVT_WRITE | RTPOLL_EVT_ERROR,
|
---|
| 679 | DRVTCP_POLLSET_ID_SOCKET);
|
---|
| 680 | if (RT_FAILURE(rc))
|
---|
| 681 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
| 682 | N_("DrvTCP#%d failed to add socket for %s to poll set"),
|
---|
| 683 | pDrvIns->iInstance, pThis->pszLocation);
|
---|
| 684 |
|
---|
[83584] | 685 | ASMAtomicIncU32(&pThis->cConnections);
|
---|
[55260] | 686 | }
|
---|
| 687 |
|
---|
| 688 | LogRel(("DrvTCP: %s, %s\n", pThis->pszLocation, pThis->fIsServer ? "server" : "client"));
|
---|
| 689 | return VINF_SUCCESS;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 |
|
---|
| 693 | /**
|
---|
| 694 | * TCP stream driver registration record.
|
---|
| 695 | */
|
---|
| 696 | const PDMDRVREG g_DrvTCP =
|
---|
| 697 | {
|
---|
| 698 | /* u32Version */
|
---|
| 699 | PDM_DRVREG_VERSION,
|
---|
| 700 | /* szName */
|
---|
| 701 | "TCP",
|
---|
| 702 | /* szRCMod */
|
---|
| 703 | "",
|
---|
| 704 | /* szR0Mod */
|
---|
| 705 | "",
|
---|
| 706 | /* pszDescription */
|
---|
| 707 | "TCP serial stream driver.",
|
---|
| 708 | /* fFlags */
|
---|
| 709 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
| 710 | /* fClass. */
|
---|
| 711 | PDM_DRVREG_CLASS_STREAM,
|
---|
| 712 | /* cMaxInstances */
|
---|
| 713 | ~0U,
|
---|
| 714 | /* cbInstance */
|
---|
| 715 | sizeof(DRVTCP),
|
---|
| 716 | /* pfnConstruct */
|
---|
| 717 | drvTCPConstruct,
|
---|
| 718 | /* pfnDestruct */
|
---|
| 719 | drvTCPDestruct,
|
---|
| 720 | /* pfnRelocate */
|
---|
| 721 | NULL,
|
---|
| 722 | /* pfnIOCtl */
|
---|
| 723 | NULL,
|
---|
| 724 | /* pfnPowerOn */
|
---|
| 725 | NULL,
|
---|
| 726 | /* pfnReset */
|
---|
| 727 | NULL,
|
---|
| 728 | /* pfnSuspend */
|
---|
| 729 | NULL,
|
---|
| 730 | /* pfnResume */
|
---|
| 731 | NULL,
|
---|
| 732 | /* pfnAttach */
|
---|
| 733 | NULL,
|
---|
| 734 | /* pfnDetach */
|
---|
| 735 | NULL,
|
---|
| 736 | /* pfnPowerOff */
|
---|
| 737 | drvTCPPowerOff,
|
---|
| 738 | /* pfnSoftReset */
|
---|
| 739 | NULL,
|
---|
| 740 | /* u32EndVersion */
|
---|
| 741 | PDM_DRVREG_VERSION
|
---|
| 742 | };
|
---|
| 743 |
|
---|