VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioTestServiceTcp.cpp@ 92009

Last change on this file since 92009 was 91999, checked in by vboxsync, 3 years ago

Audio/Validation Kit: Fixed memory leaks found by ASAN, added + renamed some functions to streamline client destruction. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.4 KB
Line 
1/* $Id: AudioTestServiceTcp.cpp 91999 2021-10-22 11:43:28Z vboxsync $ */
2/** @file
3 * AudioTestServiceTcp - Audio test execution server, TCP/IP Transport Layer.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_AUDIO_TEST
23#include <iprt/log.h>
24
25#include <iprt/asm.h>
26#include <iprt/assert.h>
27#include <iprt/critsect.h>
28#include <iprt/err.h>
29#include <iprt/mem.h>
30#include <iprt/message.h>
31#include <iprt/poll.h>
32#include <iprt/string.h>
33#include <iprt/tcp.h>
34#include <iprt/thread.h>
35#include <iprt/time.h>
36
37#include <VBox/log.h>
38
39#include "AudioTestService.h"
40#include "AudioTestServiceInternal.h"
41
42
43/*********************************************************************************************************************************
44* Defined Constants And Macros *
45*********************************************************************************************************************************/
46
47
48/*********************************************************************************************************************************
49* Structures and Typedefs *
50*********************************************************************************************************************************/
51/**
52 * TCP specific client data.
53 */
54typedef struct ATSTRANSPORTCLIENT
55{
56 /** Socket of the current client. */
57 RTSOCKET hTcpClient;
58 /** Indicates whether \a hTcpClient comes from the server or from a client
59 * connect (relevant when closing it). */
60 bool fFromServer;
61 /** The size of the stashed data. */
62 size_t cbTcpStashed;
63 /** The size of the stashed data allocation. */
64 size_t cbTcpStashedAlloced;
65 /** The stashed data. */
66 uint8_t *pbTcpStashed;
67} ATSTRANSPORTCLIENT;
68
69/**
70 * Structure for keeping Audio Test Service (ATS) transport instance-specific data.
71 */
72typedef struct ATSTRANSPORTINST
73{
74 /** Critical section for serializing access. */
75 RTCRITSECT CritSect;
76 /** Connection mode to use. */
77 ATSCONNMODE enmConnMode;
78 /** The addresses to bind to. Empty string means any. */
79 char szBindAddr[256];
80 /** The TCP port to listen to. */
81 uint32_t uBindPort;
82 /** The addresses to connect to if running in reversed (VM NATed) mode. */
83 char szConnectAddr[256];
84 /** The TCP port to connect to if running in reversed (VM NATed) mode. */
85 uint32_t uConnectPort;
86 /** Pointer to the TCP server instance. */
87 PRTTCPSERVER pTcpServer;
88 /** Thread calling RTTcpServerListen2. */
89 RTTHREAD hThreadServer;
90 /** Thread calling RTTcpClientConnect. */
91 RTTHREAD hThreadConnect;
92 /** The main thread handle (for signalling). */
93 RTTHREAD hThreadMain;
94 /** Stop connecting attempts when set. */
95 bool fStopConnecting;
96 /** Connect cancel cookie. */
97 PRTTCPCLIENTCONNECTCANCEL volatile pConnectCancelCookie;
98} ATSTRANSPORTINST;
99/** Pointer to an Audio Test Service (ATS) TCP/IP transport instance. */
100typedef ATSTRANSPORTINST *PATSTRANSPORTINST;
101
102/**
103 * Structure holding an ATS connection context, which is
104 * required when connecting a client via server (listening) or client (connecting).
105 */
106typedef struct ATSCONNCTX
107{
108 /** Pointer to transport instance to use. */
109 PATSTRANSPORTINST pInst;
110 /** Pointer to transport client to connect. */
111 PATSTRANSPORTCLIENT pClient;
112 /** Connection timeout (in ms).
113 * Use RT_INDEFINITE_WAIT to wait indefinitely. */
114 uint32_t msTimeout;
115} ATSCONNCTX;
116/** Pointer to an Audio Test Service (ATS) TCP/IP connection context. */
117typedef ATSCONNCTX *PATSCONNCTX;
118
119
120/*********************************************************************************************************************************
121* Global Variables *
122*********************************************************************************************************************************/
123
124/**
125 * Disconnects the current client and frees all stashed data.
126 *
127 * @param pThis Transport instance.
128 * @param pClient Client to disconnect.
129 */
130static void atsTcpDisconnectClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
131{
132 RT_NOREF(pThis);
133
134 LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));
135
136 if (pClient->hTcpClient != NIL_RTSOCKET)
137 {
138 LogRelFlowFunc(("%RTsock\n", pClient->hTcpClient));
139
140 int rc;
141 if (pClient->fFromServer)
142 rc = RTTcpServerDisconnectClient2(pClient->hTcpClient);
143 else
144 rc = RTTcpClientClose(pClient->hTcpClient);
145 pClient->hTcpClient = NIL_RTSOCKET;
146 AssertRCSuccess(rc);
147 }
148
149 if (pClient->pbTcpStashed)
150 {
151 RTMemFree(pClient->pbTcpStashed);
152 pClient->pbTcpStashed = NULL;
153 }
154}
155
156/**
157 * Free's a client.
158 *
159 * @param pThis Transport instance.
160 * @param pClient Client to free.
161 * The pointer will be invalid after calling.
162 */
163static void atsTcpFreeClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
164{
165 if (!pClient)
166 return;
167
168 /* Make sure to disconnect first. */
169 atsTcpDisconnectClient(pThis, pClient);
170
171 RTMemFree(pClient);
172 pClient = NULL;
173}
174
175/**
176 * Sets the current client socket in a safe manner.
177 *
178 * @returns NIL_RTSOCKET if consumed, otherwise hTcpClient.
179 * @param pThis Transport instance.
180 * @param pClient Client to set the socket for.
181 * @param fFromServer Whether the socket is from a server (listening) or client (connecting) call.
182 * Important when closing / disconnecting.
183 * @param hTcpClient The client socket.
184 */
185static RTSOCKET atsTcpSetClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, bool fFromServer, RTSOCKET hTcpClient)
186{
187 RTCritSectEnter(&pThis->CritSect);
188 if ( pClient->hTcpClient == NIL_RTSOCKET
189 && !pThis->fStopConnecting)
190 {
191 LogRelFlowFunc(("New client %RTsock connected (fFromServer=%RTbool)\n", hTcpClient, fFromServer));
192
193 pClient->fFromServer = fFromServer;
194 pClient->hTcpClient = hTcpClient;
195 hTcpClient = NIL_RTSOCKET; /* Invalidate, as pClient has now ownership. */
196 }
197 RTCritSectLeave(&pThis->CritSect);
198 return hTcpClient;
199}
200
201/**
202 * Checks if it's a fatal RTTcpClientConnect return code.
203 *
204 * @returns true / false.
205 * @param rc The IPRT status code.
206 */
207static bool atsTcpIsFatalClientConnectStatus(int rc)
208{
209 return rc != VERR_NET_UNREACHABLE
210 && rc != VERR_NET_HOST_DOWN
211 && rc != VERR_NET_HOST_UNREACHABLE
212 && rc != VERR_NET_CONNECTION_REFUSED
213 && rc != VERR_TIMEOUT
214 && rc != VERR_NET_CONNECTION_TIMED_OUT;
215}
216
217/**
218 * Server mode connection thread.
219 *
220 * @returns iprt status code.
221 * @param hSelf Thread handle. Ignored.
222 * @param pvUser Pointer to ATSTRANSPORTINST the thread is bound to.
223 */
224static DECLCALLBACK(int) atsTcpServerConnectThread(RTTHREAD hSelf, void *pvUser)
225{
226 RT_NOREF(hSelf);
227
228 PATSCONNCTX pConnCtx = (PATSCONNCTX)pvUser;
229 PATSTRANSPORTINST pThis = pConnCtx->pInst;
230 PATSTRANSPORTCLIENT pClient = pConnCtx->pClient;
231
232 /** @todo Implement cancellation support for using pConnCtx->msTimeout. */
233
234 LogRelFlowFuncEnter();
235
236 RTSOCKET hTcpClient;
237 int rc = RTTcpServerListen2(pThis->pTcpServer, &hTcpClient);
238 if (RT_SUCCESS(rc))
239 {
240 hTcpClient = atsTcpSetClient(pThis, pClient, true /* fFromServer */, hTcpClient);
241 RTTcpServerDisconnectClient2(hTcpClient);
242 }
243
244 LogRelFlowFuncLeaveRC(rc);
245 return rc;
246}
247
248/**
249 * Client mode connection thread.
250 *
251 * @returns iprt status code.
252 * @param hSelf Thread handle. Use to sleep on. The main thread will
253 * signal it to speed up thread shutdown.
254 * @param pvUser Pointer to a connection context (PATSCONNCTX) the thread is bound to.
255 */
256static DECLCALLBACK(int) atsTcpClientConnectThread(RTTHREAD hSelf, void *pvUser)
257{
258 PATSCONNCTX pConnCtx = (PATSCONNCTX)pvUser;
259 PATSTRANSPORTINST pThis = pConnCtx->pInst;
260 PATSTRANSPORTCLIENT pClient = pConnCtx->pClient;
261
262 uint64_t msStartTs = RTTimeMilliTS();
263
264 LogRelFlowFuncEnter();
265
266 for (;;)
267 {
268 /* Stop? */
269 RTCritSectEnter(&pThis->CritSect);
270 bool fStop = pThis->fStopConnecting;
271 RTCritSectLeave(&pThis->CritSect);
272 if (fStop)
273 return VINF_SUCCESS;
274
275 /* Try connect. */ /** @todo make cancelable! */
276 RTSOCKET hTcpClient;
277 int rc = RTTcpClientConnectEx(pThis->szConnectAddr, pThis->uConnectPort, &hTcpClient,
278 RT_SOCKETCONNECT_DEFAULT_WAIT, &pThis->pConnectCancelCookie);
279 if (RT_SUCCESS(rc))
280 {
281 hTcpClient = atsTcpSetClient(pThis, pClient, false /* fFromServer */, hTcpClient);
282 RTTcpClientCloseEx(hTcpClient, true /* fGracefulShutdown*/);
283 break;
284 }
285
286 if (atsTcpIsFatalClientConnectStatus(rc))
287 return rc;
288
289 if ( pConnCtx->msTimeout != RT_INDEFINITE_WAIT
290 && RTTimeMilliTS() - msStartTs >= pConnCtx->msTimeout)
291 {
292 LogRelFlowFunc(("Timed out (%RU32ms)\n", pConnCtx->msTimeout));
293 return VERR_TIMEOUT;
294 }
295
296 /* Delay a wee bit before retrying. */
297 RTThreadUserWait(hSelf, 1536);
298 }
299
300 LogRelFlowFuncLeave();
301 return VINF_SUCCESS;
302}
303
304/**
305 * Wait on the threads to complete.
306 *
307 * @returns Thread status (if collected), otherwise VINF_SUCCESS.
308 * @param pThis Transport instance.
309 * @param cMillies The period to wait on each thread.
310 */
311static int atsTcpConnectWaitOnThreads(PATSTRANSPORTINST pThis, RTMSINTERVAL cMillies)
312{
313 int rcRet = VINF_SUCCESS;
314
315 LogRelFlowFuncEnter();
316
317 if (pThis->hThreadConnect != NIL_RTTHREAD)
318 {
319 int rcThread;
320 int rc2 = RTThreadWait(pThis->hThreadConnect, cMillies, &rcThread);
321 if (RT_SUCCESS(rc2))
322 {
323 pThis->hThreadConnect = NIL_RTTHREAD;
324 rcRet = rcThread;
325 }
326 }
327
328 if (pThis->hThreadServer != NIL_RTTHREAD)
329 {
330 int rcThread;
331 int rc2 = RTThreadWait(pThis->hThreadServer, cMillies, &rcThread);
332 if (RT_SUCCESS(rc2))
333 {
334 pThis->hThreadServer = NIL_RTTHREAD;
335 if (RT_SUCCESS(rc2))
336 rcRet = rcThread;
337 }
338 }
339
340 LogRelFlowFuncLeaveRC(rcRet);
341 return rcRet;
342}
343
344/**
345 * @interface_method_impl{ATSTRANSPORT,pfnWaitForConnect}
346 */
347static DECLCALLBACK(int) atsTcpWaitForConnect(PATSTRANSPORTINST pThis, RTMSINTERVAL msTimeout,
348 bool *pfFromServer, PPATSTRANSPORTCLIENT ppClientNew)
349{
350 PATSTRANSPORTCLIENT pClient = (PATSTRANSPORTCLIENT)RTMemAllocZ(sizeof(ATSTRANSPORTCLIENT));
351 AssertPtrReturn(pClient, VERR_NO_MEMORY);
352
353 int rc;
354
355 LogRelFlowFunc(("msTimeout=%RU32, enmConnMode=%#x\n", msTimeout, pThis->enmConnMode));
356
357 uint64_t msStartTs = RTTimeMilliTS();
358
359 if (pThis->enmConnMode == ATSCONNMODE_SERVER)
360 {
361 /** @todo Implement cancellation support for using \a msTimeout. */
362
363 pClient->fFromServer = true;
364 rc = RTTcpServerListen2(pThis->pTcpServer, &pClient->hTcpClient);
365 LogRelFlowFunc(("RTTcpServerListen2(%RTsock) -> %Rrc\n", pClient->hTcpClient, rc));
366 }
367 else if (pThis->enmConnMode == ATSCONNMODE_CLIENT)
368 {
369 pClient->fFromServer = false;
370 for (;;)
371 {
372 LogRelFlowFunc(("Calling RTTcpClientConnect(%s, %u,)...\n", pThis->szConnectAddr, pThis->uConnectPort));
373 rc = RTTcpClientConnect(pThis->szConnectAddr, pThis->uConnectPort, &pClient->hTcpClient);
374 LogRelFlowFunc(("RTTcpClientConnect(%RTsock) -> %Rrc\n", pClient->hTcpClient, rc));
375 if (RT_SUCCESS(rc) || atsTcpIsFatalClientConnectStatus(rc))
376 break;
377
378 if ( msTimeout != RT_INDEFINITE_WAIT
379 && RTTimeMilliTS() - msStartTs >= msTimeout)
380 {
381 rc = VERR_TIMEOUT;
382 break;
383 }
384
385 if (pThis->fStopConnecting)
386 {
387 rc = VINF_SUCCESS;
388 break;
389 }
390
391 /* Delay a wee bit before retrying. */
392 RTThreadSleep(1536);
393 }
394 }
395 else
396 {
397 Assert(pThis->enmConnMode == ATSCONNMODE_BOTH);
398
399 /*
400 * Create client threads.
401 */
402 RTCritSectEnter(&pThis->CritSect);
403
404 pThis->fStopConnecting = false;
405 RTCritSectLeave(&pThis->CritSect);
406
407 atsTcpConnectWaitOnThreads(pThis, 32 /* cMillies */);
408
409 ATSCONNCTX ConnCtx;
410 RT_ZERO(ConnCtx);
411 ConnCtx.pInst = pThis;
412 ConnCtx.pClient = pClient;
413 ConnCtx.msTimeout = msTimeout;
414
415 rc = VINF_SUCCESS;
416 if (pThis->hThreadConnect == NIL_RTTHREAD)
417 {
418 pThis->pConnectCancelCookie = NULL;
419 rc = RTThreadCreate(&pThis->hThreadConnect, atsTcpClientConnectThread, &ConnCtx, 0, RTTHREADTYPE_DEFAULT,
420 RTTHREADFLAGS_WAITABLE, "tcpconn");
421 }
422 if (pThis->hThreadServer == NIL_RTTHREAD && RT_SUCCESS(rc))
423 rc = RTThreadCreate(&pThis->hThreadServer, atsTcpServerConnectThread, &ConnCtx, 0, RTTHREADTYPE_DEFAULT,
424 RTTHREADFLAGS_WAITABLE, "tcpserv");
425
426 RTCritSectEnter(&pThis->CritSect);
427
428 /*
429 * Wait for connection to be established.
430 */
431 while ( RT_SUCCESS(rc)
432 && pClient->hTcpClient == NIL_RTSOCKET)
433 {
434 RTCritSectLeave(&pThis->CritSect);
435 rc = atsTcpConnectWaitOnThreads(pThis, 10 /* cMillies */);
436 RTCritSectEnter(&pThis->CritSect);
437 }
438
439 /*
440 * Cancel the threads.
441 */
442 pThis->fStopConnecting = true;
443
444 RTCritSectLeave(&pThis->CritSect);
445 RTTcpClientCancelConnect(&pThis->pConnectCancelCookie);
446 }
447
448 if (RT_SUCCESS(rc))
449 {
450 if (pfFromServer)
451 *pfFromServer = pClient->fFromServer;
452 *ppClientNew = pClient;
453 }
454 else
455 {
456 if (pClient)
457 {
458 atsTcpFreeClient(pThis, pClient);
459 pClient = NULL;
460 }
461 }
462
463 if (RT_FAILURE(rc))
464 LogRelFunc(("Failed with %Rrc\n", rc));
465
466 return rc;
467}
468
469/**
470 * @interface_method_impl{ATSTRANSPORT,pfnNotifyReboot}
471 */
472static DECLCALLBACK(void) atsTcpNotifyReboot(PATSTRANSPORTINST pThis)
473{
474 LogRelFlowFuncEnter();
475 if (pThis->pTcpServer)
476 {
477 int rc = RTTcpServerDestroy(pThis->pTcpServer);
478 if (RT_FAILURE(rc))
479 LogRelFunc(("RTTcpServerDestroy failed, rc=%Rrc", rc));
480 pThis->pTcpServer = NULL;
481 }
482 LogRelFlowFuncLeave();
483}
484
485/**
486 * @interface_method_impl{ATSTRANSPORT,pfnNotifyBye}
487 */
488static DECLCALLBACK(void) atsTcpNotifyBye(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
489{
490 LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));
491 atsTcpDisconnectClient(pThis, pClient);
492}
493
494/**
495 * @interface_method_impl{ATSTRANSPORT,pfnNotifyHowdy}
496 */
497static DECLCALLBACK(void) atsTcpNotifyHowdy(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
498{
499 LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));
500
501 /* nothing to do here */
502 RT_NOREF(pThis);
503}
504
505/**
506 * @interface_method_impl{ATSTRANSPORT,pfnBabble}
507 */
508static DECLCALLBACK(void) atsTcpBabble(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, PCATSPKTHDR pPktHdr, RTMSINTERVAL cMsSendTimeout)
509{
510 /*
511 * Try send the babble reply.
512 */
513 RT_NOREF(cMsSendTimeout); /** @todo implement the timeout here; non-blocking write + select-on-write. */
514 int rc;
515 size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, ATSPKT_ALIGNMENT);
516 do rc = RTTcpWrite(pClient->hTcpClient, pPktHdr, cbToSend);
517 while (rc == VERR_INTERRUPTED);
518
519 LogRelFlowFunc(("pClient=%RTsock, rc=%Rrc\n", pClient->hTcpClient, rc));
520
521 /*
522 * Disconnect the client.
523 */
524 atsTcpDisconnectClient(pThis, pClient);
525}
526
527/**
528 * @interface_method_impl{ATSTRANSPORT,pfnSendPkt}
529 */
530static DECLCALLBACK(int) atsTcpSendPkt(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, PCATSPKTHDR pPktHdr)
531{
532 AssertReturn(pPktHdr->cb >= sizeof(ATSPKTHDR), VERR_INVALID_PARAMETER);
533
534 /*
535 * Write it.
536 */
537 size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, ATSPKT_ALIGNMENT);
538
539 Log3Func(("%RU32 -> %zu\n", pPktHdr->cb, cbToSend));
540
541 LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));
542 LogRelFlowFunc(("Header:\n"
543 "%.*Rhxd\n", RT_MIN(sizeof(ATSPKTHDR), cbToSend), pPktHdr));
544
545 if (cbToSend > sizeof(ATSPKTHDR))
546 LogRelFlowFunc(("Payload:\n"
547 "%.*Rhxd\n",
548 RT_MIN(64, cbToSend - sizeof(ATSPKTHDR)), (uint8_t *)pPktHdr + sizeof(ATSPKTHDR)));
549
550 int rc = RTTcpWrite(pClient->hTcpClient, pPktHdr, cbToSend);
551 if ( RT_FAILURE(rc)
552 && rc != VERR_INTERRUPTED)
553 {
554 /* assume fatal connection error. */
555 LogRelFunc(("RTTcpWrite -> %Rrc -> atsTcpDisconnectClient(%RTsock)\n", rc, pClient->hTcpClient));
556 atsTcpDisconnectClient(pThis, pClient);
557 }
558
559 LogRelFlowFunc(("pClient=%RTsock, cbSent=%zu -> %Rrc\n",
560 pClient->hTcpClient, cbToSend, rc));
561
562 return rc;
563}
564
565/**
566 * @interface_method_impl{ATSTRANSPORT,pfnRecvPkt}
567 */
568static DECLCALLBACK(int) atsTcpRecvPkt(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, PPATSPKTHDR ppPktHdr)
569{
570 int rc = VINF_SUCCESS;
571 *ppPktHdr = NULL;
572
573 LogRelFlowFunc(("pClient=%RTsock (cbTcpStashed=%zu, cbTcpStashedAlloced=%zu)\n",
574 pClient->hTcpClient, pClient->cbTcpStashed, pClient->cbTcpStashedAlloced));
575
576 /*
577 * Read state.
578 */
579 size_t offData = 0;
580 size_t cbData = 0;
581 size_t cbDataAlloced;
582 uint8_t *pbData = NULL;
583
584 /*
585 * Any stashed data?
586 */
587 if (pClient->cbTcpStashedAlloced)
588 {
589 offData = pClient->cbTcpStashed;
590 cbDataAlloced = pClient->cbTcpStashedAlloced;
591 pbData = pClient->pbTcpStashed;
592
593 pClient->cbTcpStashed = 0;
594 pClient->cbTcpStashedAlloced = 0;
595 pClient->pbTcpStashed = NULL;
596 }
597 else
598 {
599 cbDataAlloced = RT_ALIGN_Z(64, ATSPKT_ALIGNMENT);
600 pbData = (uint8_t *)RTMemAlloc(cbDataAlloced);
601 AssertPtrReturn(pbData, VERR_NO_MEMORY);
602 }
603
604 /*
605 * Read and validate the length.
606 */
607 while (offData < sizeof(uint32_t))
608 {
609 size_t cbRead;
610 rc = RTTcpRead(pClient->hTcpClient, pbData + offData, sizeof(uint32_t) - offData, &cbRead);
611 if (RT_FAILURE(rc))
612 break;
613 if (cbRead == 0)
614 {
615 LogRelFunc(("RTTcpRead -> %Rrc / cbRead=0 -> VERR_NET_NOT_CONNECTED (#1)\n", rc));
616 rc = VERR_NET_NOT_CONNECTED;
617 break;
618 }
619 offData += cbRead;
620 }
621 if (RT_SUCCESS(rc))
622 {
623 ASMCompilerBarrier(); /* paranoia^3 */
624 cbData = *(uint32_t volatile *)pbData;
625 if (cbData >= sizeof(ATSPKTHDR) && cbData <= ATSPKT_MAX_SIZE)
626 {
627 /*
628 * Align the length and reallocate the return packet it necessary.
629 */
630 cbData = RT_ALIGN_Z(cbData, ATSPKT_ALIGNMENT);
631 if (cbData > cbDataAlloced)
632 {
633 void *pvNew = RTMemRealloc(pbData, cbData);
634 if (pvNew)
635 {
636 pbData = (uint8_t *)pvNew;
637 cbDataAlloced = cbData;
638 }
639 else
640 rc = VERR_NO_MEMORY;
641 }
642 if (RT_SUCCESS(rc))
643 {
644 /*
645 * Read the remainder of the data.
646 */
647 while (offData < cbData)
648 {
649 size_t cbRead;
650 rc = RTTcpRead(pClient->hTcpClient, pbData + offData, cbData - offData, &cbRead);
651 if (RT_FAILURE(rc))
652 break;
653 if (cbRead == 0)
654 {
655 LogRelFunc(("RTTcpRead -> %Rrc / cbRead=0 -> VERR_NET_NOT_CONNECTED (#2)\n", rc));
656 rc = VERR_NET_NOT_CONNECTED;
657 break;
658 }
659
660 offData += cbRead;
661 }
662
663 LogRelFlowFunc(("Header:\n"
664 "%.*Rhxd\n", sizeof(ATSPKTHDR), pbData));
665
666 if ( RT_SUCCESS(rc)
667 && cbData > sizeof(ATSPKTHDR))
668 LogRelFlowFunc(("Payload:\n"
669 "%.*Rhxd\n", RT_MIN(64, cbData - sizeof(ATSPKTHDR)), (uint8_t *)pbData + sizeof(ATSPKTHDR)));
670 }
671 }
672 else
673 {
674 LogRelFunc(("Received invalid packet size (%zu)\n", cbData));
675 rc = VERR_NET_PROTOCOL_ERROR;
676 }
677 }
678 if (RT_SUCCESS(rc))
679 *ppPktHdr = (PATSPKTHDR)pbData;
680 else
681 {
682 /*
683 * Deal with errors.
684 */
685 if (rc == VERR_INTERRUPTED)
686 {
687 /* stash it away for the next call. */
688 pClient->cbTcpStashed = cbData;
689 pClient->cbTcpStashedAlloced = cbDataAlloced;
690 pClient->pbTcpStashed = pbData;
691 }
692 else
693 {
694 RTMemFree(pbData);
695
696 /* assume fatal connection error. */
697 LogRelFunc(("RTTcpRead -> %Rrc -> atsTcpDisconnectClient(%RTsock)\n", rc, pClient->hTcpClient));
698 atsTcpDisconnectClient(pThis, pClient);
699 }
700 }
701
702 LogRelFlowFunc(("pClient=%RTsock, cbData=%zu -> %Rrc\n",
703 pClient->hTcpClient, cbData, rc));
704 return rc;
705}
706
707/**
708 * @interface_method_impl{ATSTRANSPORT,pfnPollSetAdd}
709 */
710static DECLCALLBACK(int) atsTcpPollSetAdd(PATSTRANSPORTINST pThis, RTPOLLSET hPollSet, PATSTRANSPORTCLIENT pClient, uint32_t idStart)
711{
712 RT_NOREF(pThis);
713 return RTPollSetAddSocket(hPollSet, pClient->hTcpClient, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, idStart);
714}
715
716/**
717 * @interface_method_impl{ATSTRANSPORT,pfnPollSetRemove}
718 */
719static DECLCALLBACK(int) atsTcpPollSetRemove(PATSTRANSPORTINST pThis, RTPOLLSET hPollSet, PATSTRANSPORTCLIENT pClient, uint32_t idStart)
720{
721 RT_NOREF(pThis, pClient);
722 return RTPollSetRemove(hPollSet, idStart);
723}
724
725/**
726 * @interface_method_impl{ATSTRANSPORT,pfnDisconnect}
727 */
728static DECLCALLBACK(void) atsTcpDisconnect(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
729{
730 atsTcpFreeClient(pThis, pClient);
731}
732
733/**
734 * @interface_method_impl{ATSTRANSPORT,pfnPollIn}
735 */
736static DECLCALLBACK(bool) atsTcpPollIn(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
737{
738 RT_NOREF(pThis);
739 int rc = RTTcpSelectOne(pClient->hTcpClient, 0/*cMillies*/);
740 return RT_SUCCESS(rc);
741}
742
743/**
744 * @interface_method_impl{ATSTRANSPORT,pfnStop}
745 */
746static DECLCALLBACK(void) atsTcpStop(PATSTRANSPORTINST pThis)
747{
748 LogRelFlowFuncEnter();
749
750 /* Signal thread */
751 if (RTCritSectIsInitialized(&pThis->CritSect))
752 {
753 RTCritSectEnter(&pThis->CritSect);
754 pThis->fStopConnecting = true;
755 RTCritSectLeave(&pThis->CritSect);
756 }
757
758 if (pThis->hThreadConnect != NIL_RTTHREAD)
759 {
760 RTThreadUserSignal(pThis->hThreadConnect);
761 RTTcpClientCancelConnect(&pThis->pConnectCancelCookie);
762 }
763
764 /* Shut down the server (will wake up thread). */
765 if (pThis->pTcpServer)
766 {
767 LogRelFlowFunc(("Destroying server...\n"));
768 int rc = RTTcpServerDestroy(pThis->pTcpServer);
769 if (RT_FAILURE(rc))
770 LogRelFunc(("RTTcpServerDestroy failed with %Rrc", rc));
771 pThis->pTcpServer = NULL;
772 }
773
774 /* Wait for the thread (they should've had some time to quit by now). */
775 atsTcpConnectWaitOnThreads(pThis, 15000);
776
777 LogRelFlowFuncLeave();
778}
779
780/**
781 * @interface_method_impl{ATSTRANSPORT,pfnCreate}
782 */
783static DECLCALLBACK(int) atsTcpCreate(PATSTRANSPORTINST *ppThis)
784{
785 PATSTRANSPORTINST pThis = (PATSTRANSPORTINST)RTMemAllocZ(sizeof(ATSTRANSPORTINST));
786 AssertPtrReturn(pThis, VERR_NO_MEMORY);
787
788 int rc = RTCritSectInit(&pThis->CritSect);
789 if (RT_SUCCESS(rc))
790 {
791 *ppThis = pThis;
792 }
793
794 return rc;
795}
796
797/**
798 * @interface_method_impl{ATSTRANSPORT,pfnDestroy}
799 */
800static DECLCALLBACK(int) atsTcpDestroy(PATSTRANSPORTINST pThis)
801{
802 /* Stop things first. */
803 atsTcpStop(pThis);
804
805 /* Finally, clean up the critical section. */
806 if (RTCritSectIsInitialized(&pThis->CritSect))
807 RTCritSectDelete(&pThis->CritSect);
808
809 RTMemFree(pThis);
810 pThis = NULL;
811
812 return VINF_SUCCESS;
813}
814
815/**
816 * @interface_method_impl{ATSTRANSPORT,pfnStart}
817 */
818static DECLCALLBACK(int) atsTcpStart(PATSTRANSPORTINST pThis)
819{
820 int rc = VINF_SUCCESS;
821
822 if (pThis->enmConnMode != ATSCONNMODE_CLIENT)
823 {
824 rc = RTTcpServerCreateEx(pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, &pThis->pTcpServer);
825 if (RT_FAILURE(rc))
826 {
827 if (rc == VERR_NET_DOWN)
828 {
829 LogRelFunc(("RTTcpServerCreateEx(%s, %u,) failed: %Rrc, retrying for 20 seconds...\n",
830 pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, rc));
831 uint64_t StartMs = RTTimeMilliTS();
832 do
833 {
834 RTThreadSleep(1000);
835 rc = RTTcpServerCreateEx(pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, &pThis->pTcpServer);
836 } while ( rc == VERR_NET_DOWN
837 && RTTimeMilliTS() - StartMs < 20000);
838 if (RT_SUCCESS(rc))
839 LogRelFunc(("RTTcpServerCreateEx succceeded\n"));
840 }
841
842 if (RT_FAILURE(rc))
843 {
844 LogRelFunc(("RTTcpServerCreateEx(%s, %u,) failed: %Rrc\n",
845 pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, rc));
846 }
847 }
848 }
849
850 return rc;
851}
852
853/**
854 * @interface_method_impl{ATSTRANSPORT,pfnOption}
855 */
856static DECLCALLBACK(int) atsTcpOption(PATSTRANSPORTINST pThis, int ch, PCRTGETOPTUNION pVal)
857{
858 int rc;
859
860 switch (ch)
861 {
862 case ATSTCPOPT_CONN_MODE:
863 pThis->enmConnMode = (ATSCONNMODE)pVal->u32;
864 return VINF_SUCCESS;
865
866 case ATSTCPOPT_BIND_ADDRESS:
867 rc = RTStrCopy(pThis->szBindAddr, sizeof(pThis->szBindAddr), pVal->psz);
868 if (RT_FAILURE(rc))
869 return RTMsgErrorRc(VERR_INVALID_PARAMETER, "TCP bind address is too long (%Rrc)", rc);
870 if (!pThis->szBindAddr[0])
871 return RTMsgErrorRc(VERR_INVALID_PARAMETER, "No TCP bind address specified: %s", pThis->szBindAddr);
872 return VINF_SUCCESS;
873
874 case ATSTCPOPT_BIND_PORT:
875 pThis->uBindPort = pVal->u16;
876 return VINF_SUCCESS;
877
878 case ATSTCPOPT_CONNECT_ADDRESS:
879 rc = RTStrCopy(pThis->szConnectAddr, sizeof(pThis->szConnectAddr), pVal->psz);
880 if (RT_FAILURE(rc))
881 return RTMsgErrorRc(VERR_INVALID_PARAMETER, "TCP connect address is too long (%Rrc)", rc);
882 if (!pThis->szConnectAddr[0])
883 return RTMsgErrorRc(VERR_INVALID_PARAMETER, "No TCP connect address specified");
884 return VINF_SUCCESS;
885
886 case ATSTCPOPT_CONNECT_PORT:
887 pThis->uConnectPort = pVal->u16;
888 return VINF_SUCCESS;
889
890 default:
891 break;
892 }
893 return VERR_TRY_AGAIN;
894}
895
896/**
897 * @interface_method_impl{ATSTRANSPORT,pfnUsage}
898 */
899DECLCALLBACK(void) atsTcpUsage(PRTSTREAM pStream)
900{
901 RTStrmPrintf(pStream,
902 " --tcp-conn-mode <0=both|1=client|2=server>\n"
903 " Selects the connection mode.\n"
904 " Default: 0 (both)\n"
905 " --tcp-bind-addr[ess] <address>\n"
906 " The address(es) to listen to TCP connection on. Empty string\n"
907 " means any address, this is the default.\n"
908 " --tcp-bind-port <port>\n"
909 " The port to listen to TCP connections on.\n"
910 " Default: %u\n"
911 " --tcp-connect-addr[ess] <address>\n"
912 " The address of the server to try connect to in client mode.\n"
913 " Default: " ATS_TCP_DEF_CONNECT_GUEST_STR "\n"
914 " --tcp-connect-port <port>\n"
915 " The port on the server to connect to in client mode.\n"
916 " Default: %u\n"
917 , ATS_TCP_DEF_BIND_PORT_GUEST, ATS_TCP_DEF_CONNECT_PORT_GUEST);
918}
919
920/** Command line options for the TCP/IP transport layer. */
921static const RTGETOPTDEF g_TcpOpts[] =
922{
923 { "--tcp-conn-mode", ATSTCPOPT_CONN_MODE, RTGETOPT_REQ_STRING },
924 { "--tcp-bind-addr", ATSTCPOPT_BIND_ADDRESS, RTGETOPT_REQ_STRING },
925 { "--tcp-bind-address", ATSTCPOPT_BIND_ADDRESS, RTGETOPT_REQ_STRING },
926 { "--tcp-bind-port", ATSTCPOPT_BIND_PORT, RTGETOPT_REQ_UINT16 },
927 { "--tcp-connect-addr", ATSTCPOPT_CONNECT_ADDRESS, RTGETOPT_REQ_STRING },
928 { "--tcp-connect-address", ATSTCPOPT_CONNECT_ADDRESS, RTGETOPT_REQ_STRING },
929 { "--tcp-connect-port", ATSTCPOPT_CONNECT_PORT, RTGETOPT_REQ_UINT16 }
930};
931
932/** TCP/IP transport layer. */
933const ATSTRANSPORT g_TcpTransport =
934{
935 /* .szName = */ "tcp",
936 /* .pszDesc = */ "TCP/IP",
937 /* .cOpts = */ &g_TcpOpts[0],
938 /* .paOpts = */ RT_ELEMENTS(g_TcpOpts),
939 /* .pfnUsage = */ atsTcpUsage,
940 /* .pfnCreate = */ atsTcpCreate,
941 /* .pfnDestroy = */ atsTcpDestroy,
942 /* .pfnOption = */ atsTcpOption,
943 /* .pfnStart = */ atsTcpStart,
944 /* .pfnStop = */ atsTcpStop,
945 /* .pfnWaitForConnect = */ atsTcpWaitForConnect,
946 /* .pfnDisconnect = */ atsTcpDisconnect,
947 /* .pfnPollIn = */ atsTcpPollIn,
948 /* .pfnPollSetAdd = */ atsTcpPollSetAdd,
949 /* .pfnPollSetRemove = */ atsTcpPollSetRemove,
950 /* .pfnRecvPkt = */ atsTcpRecvPkt,
951 /* .pfnSendPkt = */ atsTcpSendPkt,
952 /* .pfnBabble = */ atsTcpBabble,
953 /* .pfnNotifyHowdy = */ atsTcpNotifyHowdy,
954 /* .pfnNotifyBye = */ atsTcpNotifyBye,
955 /* .pfnNotifyReboot = */ atsTcpNotifyReboot,
956 /* .u32EndMarker = */ UINT32_C(0x12345678)
957};
958
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette