summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/Audio/AudioTestServiceTcp.cpp
blob: 8177a008fc12750e844d5ce32013eb95ac891460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/* $Id: AudioTestServiceTcp.cpp $ */
/** @file
 * AudioTestServiceTcp - Audio test execution server, TCP/IP Transport Layer.
 */

/*
 * Copyright (C) 2021-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_AUDIO_TEST
#include <iprt/log.h>

#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/critsect.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/poll.h>
#include <iprt/string.h>
#include <iprt/tcp.h>
#include <iprt/thread.h>
#include <iprt/time.h>

#include <VBox/log.h>

#include "AudioTestService.h"
#include "AudioTestServiceInternal.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * TCP specific client data.
 */
typedef struct ATSTRANSPORTCLIENT
{
    /** Socket of the current client. */
    RTSOCKET                           hTcpClient;
    /** Indicates whether \a hTcpClient comes from the server or from a client
     * connect (relevant when closing it). */
    bool                               fFromServer;
    /** The size of the stashed data. */
    size_t                             cbTcpStashed;
    /** The size of the stashed data allocation. */
    size_t                             cbTcpStashedAlloced;
    /** The stashed data. */
    uint8_t                           *pbTcpStashed;
} ATSTRANSPORTCLIENT;

/**
 * Structure for keeping Audio Test Service (ATS) transport instance-specific data.
 */
typedef struct ATSTRANSPORTINST
{
    /** Critical section for serializing access. */
    RTCRITSECT                         CritSect;
    /** Connection mode to use. */
    ATSCONNMODE                        enmConnMode;
    /** The addresses to bind to.  Empty string means any. */
    char                               szBindAddr[256];
    /** The TCP port to listen to. */
    uint32_t                           uBindPort;
    /** The addresses to connect to if running in reversed (VM NATed) mode. */
    char                               szConnectAddr[256];
    /** The TCP port to connect to if running in reversed (VM NATed) mode. */
    uint32_t                           uConnectPort;
    /** Pointer to the TCP server instance. */
    PRTTCPSERVER                       pTcpServer;
    /** Thread calling RTTcpServerListen2. */
    RTTHREAD                           hThreadServer;
    /** Thread calling RTTcpClientConnect. */
    RTTHREAD                           hThreadConnect;
    /** The main thread handle (for signalling). */
    RTTHREAD                           hThreadMain;
    /** Stop connecting attempts when set. */
    bool                               fStopConnecting;
    /** Connect cancel cookie. */
    PRTTCPCLIENTCONNECTCANCEL volatile pConnectCancelCookie;
} ATSTRANSPORTINST;
/** Pointer to an Audio Test Service (ATS) TCP/IP transport instance. */
typedef ATSTRANSPORTINST *PATSTRANSPORTINST;

/**
 * Structure holding an ATS connection context, which is
 * required when connecting a client via server (listening) or client (connecting).
 */
typedef struct ATSCONNCTX
{
    /** Pointer to transport instance to use. */
    PATSTRANSPORTINST                  pInst;
    /** Pointer to transport client to connect. */
    PATSTRANSPORTCLIENT                pClient;
    /** Connection timeout (in ms).
     *  Use RT_INDEFINITE_WAIT to wait indefinitely. */
    uint32_t                           msTimeout;
} ATSCONNCTX;
/** Pointer to an Audio Test Service (ATS) TCP/IP connection context. */
typedef ATSCONNCTX *PATSCONNCTX;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/

/**
 * Disconnects the current client and frees all stashed data.
 *
 * @param   pThis           Transport instance.
 * @param   pClient         Client to disconnect.
 */
static void atsTcpDisconnectClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
{
    RT_NOREF(pThis);

    LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));

    if (pClient->hTcpClient != NIL_RTSOCKET)
    {
        LogRelFlowFunc(("%RTsock\n", pClient->hTcpClient));

        int rc;
        if (pClient->fFromServer)
            rc = RTTcpServerDisconnectClient2(pClient->hTcpClient);
        else
            rc = RTTcpClientClose(pClient->hTcpClient);
        pClient->hTcpClient = NIL_RTSOCKET;
        AssertRCSuccess(rc);
    }

    if (pClient->pbTcpStashed)
    {
        RTMemFree(pClient->pbTcpStashed);
        pClient->pbTcpStashed = NULL;
    }
}

/**
 * Free's a client.
 *
 * @param   pThis           Transport instance.
 * @param   pClient         Client to free.
 *                          The pointer will be invalid after calling.
 */
static void atsTcpFreeClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
{
    if (!pClient)
        return;

    /* Make sure to disconnect first. */
    atsTcpDisconnectClient(pThis, pClient);

    RTMemFree(pClient);
    pClient = NULL;
}

/**
 * Sets the current client socket in a safe manner.
 *
 * @returns NIL_RTSOCKET if consumed, otherwise hTcpClient.
 * @param   pThis           Transport instance.
 * @param   pClient         Client to set the socket for.
 * @param   fFromServer     Whether the socket is from a server (listening) or client (connecting) call.
 *                          Important when closing / disconnecting.
 * @param   hTcpClient      The client socket.
 */
static RTSOCKET atsTcpSetClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, bool fFromServer, RTSOCKET hTcpClient)
{
    RTCritSectEnter(&pThis->CritSect);
    if (   pClient->hTcpClient  == NIL_RTSOCKET
        && !pThis->fStopConnecting)
    {
        LogRelFlowFunc(("New client %RTsock connected (fFromServer=%RTbool)\n", hTcpClient, fFromServer));

        pClient->fFromServer = fFromServer;
        pClient->hTcpClient = hTcpClient;
        hTcpClient = NIL_RTSOCKET; /* Invalidate, as pClient has now ownership. */
    }
    RTCritSectLeave(&pThis->CritSect);
    return hTcpClient;
}

/**
 * Checks if it's a fatal RTTcpClientConnect return code.
 *
 * @returns true / false.
 * @param   rc              The IPRT status code.
 */
static bool atsTcpIsFatalClientConnectStatus(int rc)
{
    return rc != VERR_NET_UNREACHABLE
        && rc != VERR_NET_HOST_DOWN
        && rc != VERR_NET_HOST_UNREACHABLE
        && rc != VERR_NET_CONNECTION_REFUSED
        && rc != VERR_TIMEOUT
        && rc != VERR_NET_CONNECTION_TIMED_OUT;
}

/**
 * Server mode connection thread.
 *
 * @returns iprt status code.
 * @param   hSelf           Thread handle. Ignored.
 * @param   pvUser          Pointer to ATSTRANSPORTINST the thread is bound to.
 */
static DECLCALLBACK(int) atsTcpServerConnectThread(RTTHREAD hSelf, void *pvUser)
{
    RT_NOREF(hSelf);

    PATSCONNCTX         pConnCtx = (PATSCONNCTX)pvUser;
    PATSTRANSPORTINST   pThis    = pConnCtx->pInst;
    PATSTRANSPORTCLIENT pClient  = pConnCtx->pClient;

    /** @todo Implement cancellation support for using pConnCtx->msTimeout. */

    LogRelFlowFuncEnter();

    RTSOCKET hTcpClient;
    int rc = RTTcpServerListen2(pThis->pTcpServer, &hTcpClient);
    if (RT_SUCCESS(rc))
    {
        hTcpClient = atsTcpSetClient(pThis, pClient, true /* fFromServer */, hTcpClient);
        RTTcpServerDisconnectClient2(hTcpClient);
    }

    LogRelFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Client mode connection thread.
 *
 * @returns iprt status code.
 * @param   hSelf           Thread handle. Use to sleep on. The main thread will
 *                          signal it to speed up thread shutdown.
 * @param   pvUser          Pointer to a connection context (PATSCONNCTX) the thread is bound to.
 */
static DECLCALLBACK(int) atsTcpClientConnectThread(RTTHREAD hSelf, void *pvUser)
{
    PATSCONNCTX         pConnCtx = (PATSCONNCTX)pvUser;
    PATSTRANSPORTINST   pThis    = pConnCtx->pInst;
    PATSTRANSPORTCLIENT pClient  = pConnCtx->pClient;

    uint64_t msStartTs = RTTimeMilliTS();

    LogRelFlowFuncEnter();

    for (;;)
    {
        /* Stop? */
        RTCritSectEnter(&pThis->CritSect);
        bool fStop = pThis->fStopConnecting;
        RTCritSectLeave(&pThis->CritSect);
        if (fStop)
            return VINF_SUCCESS;

        /* Try connect. */ /** @todo make cancelable! */
        RTSOCKET hTcpClient;
        int rc = RTTcpClientConnectEx(pThis->szConnectAddr, pThis->uConnectPort, &hTcpClient,
                                      RT_SOCKETCONNECT_DEFAULT_WAIT, &pThis->pConnectCancelCookie);
        if (RT_SUCCESS(rc))
        {
            hTcpClient = atsTcpSetClient(pThis, pClient, false /* fFromServer */, hTcpClient);
            RTTcpClientCloseEx(hTcpClient, true /* fGracefulShutdown*/);
            break;
        }

        if (atsTcpIsFatalClientConnectStatus(rc))
            return rc;

        if (   pConnCtx->msTimeout         != RT_INDEFINITE_WAIT
            && RTTimeMilliTS() - msStartTs >= pConnCtx->msTimeout)
        {
            LogRelFlowFunc(("Timed out (%RU32ms)\n", pConnCtx->msTimeout));
            return VERR_TIMEOUT;
        }

        /* Delay a wee bit before retrying. */
        RTThreadUserWait(hSelf, 1536);
    }

    LogRelFlowFuncLeave();
    return VINF_SUCCESS;
}

/**
 * Wait on the threads to complete.
 *
 * @returns Thread status (if collected), otherwise VINF_SUCCESS.
 * @param   pThis           Transport instance.
 * @param   cMillies        The period to wait on each thread.
 */
static int atsTcpConnectWaitOnThreads(PATSTRANSPORTINST pThis, RTMSINTERVAL cMillies)
{
    int rcRet = VINF_SUCCESS;

    LogRelFlowFuncEnter();

    if (pThis->hThreadConnect != NIL_RTTHREAD)
    {
        int rcThread;
        int rc2 = RTThreadWait(pThis->hThreadConnect, cMillies, &rcThread);
        if (RT_SUCCESS(rc2))
        {
            pThis->hThreadConnect = NIL_RTTHREAD;
            rcRet = rcThread;
        }
    }

    if (pThis->hThreadServer != NIL_RTTHREAD)
    {
        int rcThread;
        int rc2 = RTThreadWait(pThis->hThreadServer, cMillies, &rcThread);
        if (RT_SUCCESS(rc2))
        {
            pThis->hThreadServer = NIL_RTTHREAD;
            if (RT_SUCCESS(rc2))
                rcRet = rcThread;
        }
    }

    LogRelFlowFuncLeaveRC(rcRet);
    return rcRet;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnWaitForConnect}
 */
static DECLCALLBACK(int) atsTcpWaitForConnect(PATSTRANSPORTINST pThis,  RTMSINTERVAL msTimeout,
                                              bool *pfFromServer, PPATSTRANSPORTCLIENT ppClientNew)
{
    PATSTRANSPORTCLIENT pClient = (PATSTRANSPORTCLIENT)RTMemAllocZ(sizeof(ATSTRANSPORTCLIENT));
    AssertPtrReturn(pClient, VERR_NO_MEMORY);

    int rc;

    LogRelFlowFunc(("msTimeout=%RU32, enmConnMode=%#x\n", msTimeout, pThis->enmConnMode));

    uint64_t msStartTs = RTTimeMilliTS();

    if (pThis->enmConnMode == ATSCONNMODE_SERVER)
    {
        /** @todo Implement cancellation support for using \a msTimeout. */

        pClient->fFromServer = true;
        rc = RTTcpServerListen2(pThis->pTcpServer, &pClient->hTcpClient);
        LogRelFlowFunc(("RTTcpServerListen2(%RTsock) -> %Rrc\n", pClient->hTcpClient, rc));
    }
    else if (pThis->enmConnMode == ATSCONNMODE_CLIENT)
    {
        pClient->fFromServer = false;
        for (;;)
        {
            LogRelFlowFunc(("Calling RTTcpClientConnect(%s, %u,)...\n", pThis->szConnectAddr, pThis->uConnectPort));
            rc = RTTcpClientConnect(pThis->szConnectAddr, pThis->uConnectPort, &pClient->hTcpClient);
            LogRelFlowFunc(("RTTcpClientConnect(%RTsock) -> %Rrc\n", pClient->hTcpClient, rc));
            if (RT_SUCCESS(rc) || atsTcpIsFatalClientConnectStatus(rc))
                break;

            if (   msTimeout                   != RT_INDEFINITE_WAIT
                && RTTimeMilliTS() - msStartTs >= msTimeout)
            {
                rc = VERR_TIMEOUT;
                break;
            }

            if (pThis->fStopConnecting)
            {
                rc = VINF_SUCCESS;
                break;
            }

            /* Delay a wee bit before retrying. */
            RTThreadSleep(1536);
        }
    }
    else
    {
        Assert(pThis->enmConnMode == ATSCONNMODE_BOTH);

        /*
         * Create client threads.
         */
        RTCritSectEnter(&pThis->CritSect);

        pThis->fStopConnecting = false;
        RTCritSectLeave(&pThis->CritSect);

        atsTcpConnectWaitOnThreads(pThis, 32 /* cMillies */);

        ATSCONNCTX ConnCtx;
        RT_ZERO(ConnCtx);
        ConnCtx.pInst     = pThis;
        ConnCtx.pClient   = pClient;
        ConnCtx.msTimeout = msTimeout;

        rc = VINF_SUCCESS;
        if (pThis->hThreadConnect == NIL_RTTHREAD)
        {
            pThis->pConnectCancelCookie = NULL;
            rc = RTThreadCreate(&pThis->hThreadConnect, atsTcpClientConnectThread, &ConnCtx, 0, RTTHREADTYPE_DEFAULT,
                                RTTHREADFLAGS_WAITABLE, "tcpconn");
        }
        if (pThis->hThreadServer == NIL_RTTHREAD && RT_SUCCESS(rc))
            rc = RTThreadCreate(&pThis->hThreadServer, atsTcpServerConnectThread, &ConnCtx, 0, RTTHREADTYPE_DEFAULT,
                                RTTHREADFLAGS_WAITABLE, "tcpserv");

        RTCritSectEnter(&pThis->CritSect);

        /*
         * Wait for connection to be established.
         */
        while (   RT_SUCCESS(rc)
               && pClient->hTcpClient == NIL_RTSOCKET)
        {
            RTCritSectLeave(&pThis->CritSect);
            rc = atsTcpConnectWaitOnThreads(pThis, 10 /* cMillies */);
            RTCritSectEnter(&pThis->CritSect);
        }

        /*
         * Cancel the threads.
         */
        pThis->fStopConnecting = true;

        RTCritSectLeave(&pThis->CritSect);
        RTTcpClientCancelConnect(&pThis->pConnectCancelCookie);
    }

    if (RT_SUCCESS(rc))
    {
        if (pfFromServer)
            *pfFromServer = pClient->fFromServer;
        *ppClientNew = pClient;
    }
    else
    {
        if (pClient)
        {
            atsTcpFreeClient(pThis, pClient);
            pClient = NULL;
        }
    }

    if (RT_FAILURE(rc))
        LogRelFunc(("Failed with %Rrc\n", rc));

    return rc;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnNotifyReboot}
 */
static DECLCALLBACK(void) atsTcpNotifyReboot(PATSTRANSPORTINST pThis)
{
    LogRelFlowFuncEnter();
    if (pThis->pTcpServer)
    {
        int rc = RTTcpServerDestroy(pThis->pTcpServer);
        if (RT_FAILURE(rc))
            LogRelFunc(("RTTcpServerDestroy failed, rc=%Rrc", rc));
        pThis->pTcpServer = NULL;
    }
    LogRelFlowFuncLeave();
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnNotifyBye}
 */
static DECLCALLBACK(void) atsTcpNotifyBye(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
{
    LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));
    atsTcpDisconnectClient(pThis, pClient);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnNotifyHowdy}
 */
static DECLCALLBACK(void) atsTcpNotifyHowdy(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
{
    LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));

    /* nothing to do here */
    RT_NOREF(pThis);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnBabble}
 */
static DECLCALLBACK(void) atsTcpBabble(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, PCATSPKTHDR pPktHdr, RTMSINTERVAL cMsSendTimeout)
{
    /*
     * Try send the babble reply.
     */
    RT_NOREF(cMsSendTimeout); /** @todo implement the timeout here; non-blocking write + select-on-write. */
    int     rc;
    size_t  cbToSend = RT_ALIGN_Z(pPktHdr->cb, ATSPKT_ALIGNMENT);
    do  rc = RTTcpWrite(pClient->hTcpClient, pPktHdr, cbToSend);
    while (rc == VERR_INTERRUPTED);

    LogRelFlowFunc(("pClient=%RTsock, rc=%Rrc\n", pClient->hTcpClient, rc));

    /*
     * Disconnect the client.
     */
    atsTcpDisconnectClient(pThis, pClient);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnSendPkt}
 */
static DECLCALLBACK(int) atsTcpSendPkt(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, PCATSPKTHDR pPktHdr)
{
    AssertReturn(pPktHdr->cb >= sizeof(ATSPKTHDR), VERR_INVALID_PARAMETER);

    /*
     * Write it.
     */
    size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, ATSPKT_ALIGNMENT);

    Log3Func(("%RU32 -> %zu\n", pPktHdr->cb, cbToSend));

    LogRel4(("pClient=%RTsock\n", pClient->hTcpClient));
    LogRel4(("Header:\n"
             "%.*Rhxd\n", RT_MIN(sizeof(ATSPKTHDR), cbToSend), pPktHdr));

    if (cbToSend > sizeof(ATSPKTHDR))
        LogRel4(("Payload:\n"
                 "%.*Rhxd\n",
                 RT_MIN(64, cbToSend - sizeof(ATSPKTHDR)), (uint8_t *)pPktHdr + sizeof(ATSPKTHDR)));

    int rc = RTTcpWrite(pClient->hTcpClient, pPktHdr, cbToSend);
    if (    RT_FAILURE(rc)
        &&  rc != VERR_INTERRUPTED)
    {
        /* assume fatal connection error. */
        LogRelFunc(("RTTcpWrite -> %Rrc -> atsTcpDisconnectClient(%RTsock)\n", rc, pClient->hTcpClient));
        atsTcpDisconnectClient(pThis, pClient);
    }

    LogRel3(("atsTcpSendPkt: pClient=%RTsock, achOpcode=%.8s, cbSent=%zu -> %Rrc\n", pClient->hTcpClient, (const char *)pPktHdr->achOpcode, cbToSend, rc));
    return rc;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnRecvPkt}
 */
static DECLCALLBACK(int) atsTcpRecvPkt(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient, PPATSPKTHDR ppPktHdr)
{
    int rc = VINF_SUCCESS;
    *ppPktHdr = NULL;

    LogRel4(("pClient=%RTsock (cbTcpStashed=%zu, cbTcpStashedAlloced=%zu)\n",
             pClient->hTcpClient, pClient->cbTcpStashed, pClient->cbTcpStashedAlloced));

    /*
     * Read state.
     */
    size_t      offData       = 0;
    size_t      cbData        = 0;
    size_t      cbDataAlloced;
    uint8_t    *pbData        = NULL;

    /*
     * Any stashed data?
     */
    if (pClient->cbTcpStashedAlloced)
    {
        offData               = pClient->cbTcpStashed;
        cbDataAlloced         = pClient->cbTcpStashedAlloced;
        pbData                = pClient->pbTcpStashed;

        pClient->cbTcpStashed        = 0;
        pClient->cbTcpStashedAlloced = 0;
        pClient->pbTcpStashed        = NULL;
    }
    else
    {
        cbDataAlloced = RT_ALIGN_Z(64,  ATSPKT_ALIGNMENT);
        pbData = (uint8_t *)RTMemAlloc(cbDataAlloced);
        AssertPtrReturn(pbData, VERR_NO_MEMORY);
    }

    /*
     * Read and validate the length.
     */
    while (offData < sizeof(uint32_t))
    {
        size_t cbRead;
        rc = RTTcpRead(pClient->hTcpClient, pbData + offData, sizeof(uint32_t) - offData, &cbRead);
        if (RT_FAILURE(rc))
            break;
        if (cbRead == 0)
        {
            LogRelFunc(("RTTcpRead -> %Rrc / cbRead=0 -> VERR_NET_NOT_CONNECTED (#1)\n", rc));
            rc = VERR_NET_NOT_CONNECTED;
            break;
        }
        offData += cbRead;
    }
    if (RT_SUCCESS(rc))
    {
        ASMCompilerBarrier(); /* paranoia^3 */
        cbData = *(uint32_t volatile *)pbData;
        if (cbData >= sizeof(ATSPKTHDR) && cbData <= ATSPKT_MAX_SIZE)
        {
            /*
             * Align the length and reallocate the return packet it necessary.
             */
            cbData = RT_ALIGN_Z(cbData, ATSPKT_ALIGNMENT);
            if (cbData > cbDataAlloced)
            {
                void *pvNew = RTMemRealloc(pbData, cbData);
                if (pvNew)
                {
                    pbData = (uint8_t *)pvNew;
                    cbDataAlloced = cbData;
                }
                else
                    rc = VERR_NO_MEMORY;
            }
            if (RT_SUCCESS(rc))
            {
                /*
                 * Read the remainder of the data.
                 */
                while (offData < cbData)
                {
                    size_t cbRead;
                    rc = RTTcpRead(pClient->hTcpClient, pbData + offData, cbData - offData, &cbRead);
                    if (RT_FAILURE(rc))
                        break;
                    if (cbRead == 0)
                    {
                        LogRelFunc(("RTTcpRead -> %Rrc / cbRead=0 -> VERR_NET_NOT_CONNECTED (#2)\n", rc));
                        rc = VERR_NET_NOT_CONNECTED;
                        break;
                    }

                    offData += cbRead;
                }

                LogRel4(("Header:\n"
                         "%.*Rhxd\n", sizeof(ATSPKTHDR), pbData));

                if (   RT_SUCCESS(rc)
                    && cbData > sizeof(ATSPKTHDR))
                    LogRel4(("Payload:\n"
                             "%.*Rhxd\n", RT_MIN(64, cbData - sizeof(ATSPKTHDR)), (uint8_t *)pbData + sizeof(ATSPKTHDR)));
            }
        }
        else
        {
            LogRelFunc(("Received invalid packet size (%zu)\n", cbData));
            rc = VERR_NET_PROTOCOL_ERROR;
        }
    }
    if (RT_SUCCESS(rc))
        *ppPktHdr = (PATSPKTHDR)pbData;
    else
    {
        /*
         * Deal with errors.
         */
        if (rc == VERR_INTERRUPTED)
        {
            /* stash it away for the next call. */
            pClient->cbTcpStashed        = cbData;
            pClient->cbTcpStashedAlloced = cbDataAlloced;
            pClient->pbTcpStashed        = pbData;
        }
        else
        {
            RTMemFree(pbData);

            /* assume fatal connection error. */
            LogRelFunc(("RTTcpRead -> %Rrc -> atsTcpDisconnectClient(%RTsock)\n", rc, pClient->hTcpClient));
            atsTcpDisconnectClient(pThis, pClient);
        }
    }

    PATSPKTHDR pPktHdr = (PATSPKTHDR)pbData;
    LogRel3(("atsTcpRecvPkt: pClient=%RTsock, achOpcode=%.8s, cbRead=%zu -> %Rrc\n",
             pClient->hTcpClient, pPktHdr ? (const char *)pPktHdr->achOpcode : "NONE    ", cbData, rc));
    return rc;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnPollSetAdd}
 */
static DECLCALLBACK(int) atsTcpPollSetAdd(PATSTRANSPORTINST pThis, RTPOLLSET hPollSet, PATSTRANSPORTCLIENT pClient, uint32_t idStart)
{
    RT_NOREF(pThis);
    return RTPollSetAddSocket(hPollSet, pClient->hTcpClient, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, idStart);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnPollSetRemove}
 */
static DECLCALLBACK(int) atsTcpPollSetRemove(PATSTRANSPORTINST pThis, RTPOLLSET hPollSet, PATSTRANSPORTCLIENT pClient, uint32_t idStart)
{
    RT_NOREF(pThis, pClient);
    return RTPollSetRemove(hPollSet, idStart);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnDisconnect}
 */
static DECLCALLBACK(void) atsTcpDisconnect(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
{
    atsTcpFreeClient(pThis, pClient);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnPollIn}
 */
static DECLCALLBACK(bool) atsTcpPollIn(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
{
    RT_NOREF(pThis);
    int rc = RTTcpSelectOne(pClient->hTcpClient, 0/*cMillies*/);
    return RT_SUCCESS(rc);
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnStop}
 */
static DECLCALLBACK(void) atsTcpStop(PATSTRANSPORTINST pThis)
{
    LogRelFlowFuncEnter();

    /* Signal thread */
    if (RTCritSectIsInitialized(&pThis->CritSect))
    {
        RTCritSectEnter(&pThis->CritSect);
        pThis->fStopConnecting = true;
        RTCritSectLeave(&pThis->CritSect);
    }

    if (pThis->hThreadConnect != NIL_RTTHREAD)
    {
        RTThreadUserSignal(pThis->hThreadConnect);
        RTTcpClientCancelConnect(&pThis->pConnectCancelCookie);
    }

    /* Shut down the server (will wake up thread). */
    if (pThis->pTcpServer)
    {
        LogRelFlowFunc(("Destroying server...\n"));
        int rc = RTTcpServerDestroy(pThis->pTcpServer);
        if (RT_FAILURE(rc))
            LogRelFunc(("RTTcpServerDestroy failed with %Rrc", rc));
        pThis->pTcpServer        = NULL;
    }

    /* Wait for the thread (they should've had some time to quit by now). */
    atsTcpConnectWaitOnThreads(pThis, 15000);

    LogRelFlowFuncLeave();
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnCreate}
 */
static DECLCALLBACK(int) atsTcpCreate(PATSTRANSPORTINST *ppThis)
{
    PATSTRANSPORTINST pThis = (PATSTRANSPORTINST)RTMemAllocZ(sizeof(ATSTRANSPORTINST));
    AssertPtrReturn(pThis, VERR_NO_MEMORY);

    int rc = RTCritSectInit(&pThis->CritSect);
    if (RT_SUCCESS(rc))
    {
        *ppThis = pThis;
    }

    return rc;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnDestroy}
 */
static DECLCALLBACK(int) atsTcpDestroy(PATSTRANSPORTINST pThis)
{
    /* Stop things first. */
    atsTcpStop(pThis);

    /* Finally, clean up the critical section. */
    if (RTCritSectIsInitialized(&pThis->CritSect))
        RTCritSectDelete(&pThis->CritSect);

    RTMemFree(pThis);
    pThis = NULL;

    return VINF_SUCCESS;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnStart}
 */
static DECLCALLBACK(int) atsTcpStart(PATSTRANSPORTINST pThis)
{
    int rc = VINF_SUCCESS;

    if (pThis->enmConnMode != ATSCONNMODE_CLIENT)
    {
        rc = RTTcpServerCreateEx(pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, &pThis->pTcpServer);
        if (RT_FAILURE(rc))
        {
            if (rc == VERR_NET_DOWN)
            {
                LogRelFunc(("RTTcpServerCreateEx(%s, %u,) failed: %Rrc, retrying for 20 seconds...\n",
                            pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, rc));
                uint64_t StartMs = RTTimeMilliTS();
                do
                {
                    RTThreadSleep(1000);
                    rc = RTTcpServerCreateEx(pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, &pThis->pTcpServer);
                } while (   rc == VERR_NET_DOWN
                         && RTTimeMilliTS() - StartMs < 20000);
                if (RT_SUCCESS(rc))
                    LogRelFunc(("RTTcpServerCreateEx succceeded\n"));
            }

            if (RT_FAILURE(rc))
            {
                LogRelFunc(("RTTcpServerCreateEx(%s, %u,) failed: %Rrc\n",
                            pThis->szBindAddr[0] ? pThis->szBindAddr : NULL, pThis->uBindPort, rc));
            }
        }
    }

    return rc;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnOption}
 */
static DECLCALLBACK(int) atsTcpOption(PATSTRANSPORTINST pThis, int ch, PCRTGETOPTUNION pVal)
{
    int rc;

    switch (ch)
    {
        case ATSTCPOPT_CONN_MODE:
            pThis->enmConnMode = (ATSCONNMODE)pVal->u32;
            return VINF_SUCCESS;

        case ATSTCPOPT_BIND_ADDRESS:
            rc = RTStrCopy(pThis->szBindAddr, sizeof(pThis->szBindAddr), pVal->psz);
            if (RT_FAILURE(rc))
                return RTMsgErrorRc(VERR_INVALID_PARAMETER, "TCP bind address is too long (%Rrc)", rc);
            if (!pThis->szBindAddr[0])
                return RTMsgErrorRc(VERR_INVALID_PARAMETER, "No TCP bind address specified: %s", pThis->szBindAddr);
            return VINF_SUCCESS;

        case ATSTCPOPT_BIND_PORT:
            pThis->uBindPort = pVal->u16;
            return VINF_SUCCESS;

        case ATSTCPOPT_CONNECT_ADDRESS:
            rc = RTStrCopy(pThis->szConnectAddr, sizeof(pThis->szConnectAddr), pVal->psz);
            if (RT_FAILURE(rc))
                return RTMsgErrorRc(VERR_INVALID_PARAMETER, "TCP connect address is too long (%Rrc)", rc);
            if (!pThis->szConnectAddr[0])
                return RTMsgErrorRc(VERR_INVALID_PARAMETER, "No TCP connect address specified");
            return VINF_SUCCESS;

        case ATSTCPOPT_CONNECT_PORT:
            pThis->uConnectPort = pVal->u16;
            return VINF_SUCCESS;

        default:
            break;
    }
    return VERR_TRY_AGAIN;
}

/**
 * @interface_method_impl{ATSTRANSPORT,pfnUsage}
 */
DECLCALLBACK(void) atsTcpUsage(PRTSTREAM pStream)
{
    RTStrmPrintf(pStream,
                 "  --tcp-conn-mode <0=both|1=client|2=server>\n"
                 "    Selects the connection mode\n"
                 "    Default: 0 (both)\n"
                 "  --tcp-bind-addr[ess] <address>\n"
                 "    The address(es) to listen to TCP connection on. Empty string\n"
                 "    means any address, this is the default\n"
                 "  --tcp-bind-port <port>\n"
                 "    The port to listen to TCP connections on\n"
                 "    Default: %u\n"
                 "  --tcp-connect-addr[ess] <address>\n"
                 "    The address of the server to try connect to in client mode\n"
                 "    Default: " ATS_TCP_DEF_CONNECT_GUEST_STR "\n"
                 "  --tcp-connect-port <port>\n"
                 "    The port on the server to connect to in client mode\n"
                 "    Default: %u\n"
                 , ATS_TCP_DEF_BIND_PORT_GUEST, ATS_TCP_DEF_CONNECT_PORT_GUEST);
}

/** Command line options for the TCP/IP transport layer. */
static const RTGETOPTDEF  g_TcpOpts[] =
{
    { "--tcp-conn-mode",        ATSTCPOPT_CONN_MODE,        RTGETOPT_REQ_STRING },
    { "--tcp-bind-addr",        ATSTCPOPT_BIND_ADDRESS,     RTGETOPT_REQ_STRING },
    { "--tcp-bind-address",     ATSTCPOPT_BIND_ADDRESS,     RTGETOPT_REQ_STRING },
    { "--tcp-bind-port",        ATSTCPOPT_BIND_PORT,        RTGETOPT_REQ_UINT16 },
    { "--tcp-connect-addr",     ATSTCPOPT_CONNECT_ADDRESS,  RTGETOPT_REQ_STRING },
    { "--tcp-connect-address",  ATSTCPOPT_CONNECT_ADDRESS,  RTGETOPT_REQ_STRING },
    { "--tcp-connect-port",     ATSTCPOPT_CONNECT_PORT,     RTGETOPT_REQ_UINT16 }
};

/** TCP/IP transport layer. */
const ATSTRANSPORT g_TcpTransport =
{
    /* .szName            = */ "tcp",
    /* .pszDesc           = */ "TCP/IP",
    /* .cOpts             = */ &g_TcpOpts[0],
    /* .paOpts            = */ RT_ELEMENTS(g_TcpOpts),
    /* .pfnUsage          = */ atsTcpUsage,
    /* .pfnCreate         = */ atsTcpCreate,
    /* .pfnDestroy        = */ atsTcpDestroy,
    /* .pfnOption         = */ atsTcpOption,
    /* .pfnStart          = */ atsTcpStart,
    /* .pfnStop           = */ atsTcpStop,
    /* .pfnWaitForConnect = */ atsTcpWaitForConnect,
    /* .pfnDisconnect     = */ atsTcpDisconnect,
    /* .pfnPollIn         = */ atsTcpPollIn,
    /* .pfnPollSetAdd     = */ atsTcpPollSetAdd,
    /* .pfnPollSetRemove  = */ atsTcpPollSetRemove,
    /* .pfnRecvPkt        = */ atsTcpRecvPkt,
    /* .pfnSendPkt        = */ atsTcpSendPkt,
    /* .pfnBabble         = */ atsTcpBabble,
    /* .pfnNotifyHowdy    = */ atsTcpNotifyHowdy,
    /* .pfnNotifyBye      = */ atsTcpNotifyBye,
    /* .pfnNotifyReboot   = */ atsTcpNotifyReboot,
    /* .u32EndMarker      = */ UINT32_C(0x12345678)
};