summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/IntNetSwitch/VBoxIntNetSwitch.cpp
blob: 44b123b6ab6eba61af56df5daa37ad6983efe85c (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
/* $Id: VBoxIntNetSwitch.cpp $ */
/** @file
 * Internal networking - Wrapper for the R0 network service.
 *
 * This is a bit hackish as we're mixing context here, however it is
 * very useful when making changes to the internal networking service.
 */

/*
 * Copyright (C) 2006-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 IN_INTNET_TESTCASE
#define IN_INTNET_R3
#include "IntNetSwitchInternal.h"

#include <VBox/err.h>
#include <VBox/vmm/vmm.h>
#include <iprt/asm.h>
#include <iprt/critsect.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include <iprt/semaphore.h>
#include <iprt/time.h>

#include <xpc/xpc.h>


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


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/**
 * Registered object.
 * This takes care of reference counting and tracking data for access checks.
 */
typedef struct SUPDRVOBJ
{
    /** Pointer to the next in the global list. */
    struct SUPDRVOBJ * volatile     pNext;
    /** Pointer to the object destructor.
     * This may be set to NULL if the image containing the destructor get unloaded. */
    PFNSUPDRVDESTRUCTOR             pfnDestructor;
    /** User argument 1. */
    void                           *pvUser1;
    /** User argument 2. */
    void                           *pvUser2;
    /** The total sum of all per-session usage. */
    uint32_t volatile               cUsage;
} SUPDRVOBJ, *PSUPDRVOBJ;


/**
 * The per-session object usage record.
 */
typedef struct SUPDRVUSAGE
{
    /** Pointer to the next in the list. */
    struct SUPDRVUSAGE * volatile   pNext;
    /** Pointer to the object we're recording usage for. */
    PSUPDRVOBJ                      pObj;
    /** The usage count. */
    uint32_t volatile               cUsage;
} SUPDRVUSAGE, *PSUPDRVUSAGE;


/**
 * Device extension.
 */
typedef struct SUPDRVDEVEXT
{
    /** Number of references to this service. */
    uint32_t volatile               cRefs;
    /** Critical section to serialize the initialization, usage counting and objects. */
    RTCRITSECT                      CritSect;
    /** List of registered objects. Protected by the spinlock. */
    PSUPDRVOBJ volatile             pObjs;
} SUPDRVDEVEXT;
typedef SUPDRVDEVEXT *PSUPDRVDEVEXT;


/**
 * Per session data.
 * This is mainly for memory tracking.
 */
typedef struct SUPDRVSESSION
{
    PSUPDRVDEVEXT                   pDevExt;
    /** List of generic usage records. (protected by SUPDRVDEVEXT::CritSect) */
    PSUPDRVUSAGE volatile           pUsage;
    /** The XPC connection handle for this session. */
    xpc_connection_t                hXpcCon;
    /** The intnet interface handle to wait on. */
    INTNETIFHANDLE                  hIfWait;
    /** Flag whether a receive wait was initiated. */
    bool volatile                   fRecvWait;
    /** Flag whether there is something to receive. */
    bool volatile                   fRecvAvail;
} SUPDRVSESSION;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
static SUPDRVDEVEXT g_DevExt;


INTNETR3DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType,
                                      PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2)
{
    RT_NOREF(enmType);

    PSUPDRVOBJ pObj = (PSUPDRVOBJ)RTMemAllocZ(sizeof(*pObj));
    if (!pObj)
        return NULL;
    pObj->cUsage = 1;
    pObj->pfnDestructor = pfnDestructor;
    pObj->pvUser1 = pvUser1;
    pObj->pvUser2 = pvUser2;

    /*
     * Insert the object and create the session usage record.
     */
    PSUPDRVUSAGE pUsage = (PSUPDRVUSAGE)RTMemAlloc(sizeof(*pUsage));
    if (!pUsage)
    {
        RTMemFree(pObj);
        return NULL;
    }

    PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
    RTCritSectEnter(&pDevExt->CritSect);

    /* The object. */
    pObj->pNext         = pDevExt->pObjs;
    pDevExt->pObjs      = pObj;

    /* The session record. */
    pUsage->cUsage      = 1;
    pUsage->pObj        = pObj;
    pUsage->pNext       = pSession->pUsage;
    pSession->pUsage    = pUsage;

    RTCritSectLeave(&pDevExt->CritSect);
    return pObj;
}


INTNETR3DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking)
{
    PSUPDRVDEVEXT   pDevExt     = pSession->pDevExt;
    PSUPDRVOBJ      pObj        = (PSUPDRVOBJ)pvObj;
    int             rc          = VINF_SUCCESS;
    PSUPDRVUSAGE    pUsage;

    RT_NOREF(fNoBlocking);

    RTCritSectEnter(&pDevExt->CritSect);

    /*
     * Reference the object.
     */
    ASMAtomicIncU32(&pObj->cUsage);

    /*
     * Look for the session record.
     */
    for (pUsage = pSession->pUsage; pUsage; pUsage = pUsage->pNext)
    {
        if (pUsage->pObj == pObj)
            break;
    }

    if (pUsage)
        pUsage->cUsage++;
    else
    {
        /* create a new session record. */
        pUsage = (PSUPDRVUSAGE)RTMemAlloc(sizeof(*pUsage));
        if (RT_LIKELY(pUsage))
        {
            pUsage->cUsage   = 1;
            pUsage->pObj     = pObj;
            pUsage->pNext    = pSession->pUsage;
            pSession->pUsage = pUsage;
        }
        else
        {
            ASMAtomicDecU32(&pObj->cUsage);
            rc = VERR_TRY_AGAIN;
        }
    }

    RTCritSectLeave(&pDevExt->CritSect);
    return rc;
}


INTNETR3DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession)
{
    return SUPR0ObjAddRefEx(pvObj, pSession, false);
}


INTNETR3DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession)
{
    PSUPDRVDEVEXT       pDevExt     = pSession->pDevExt;
    PSUPDRVOBJ          pObj        = (PSUPDRVOBJ)pvObj;
    int                 rc          = VERR_INVALID_PARAMETER;
    PSUPDRVUSAGE        pUsage;
    PSUPDRVUSAGE        pUsagePrev;

    /*
     * Acquire the spinlock and look for the usage record.
     */
    RTCritSectEnter(&pDevExt->CritSect);

    for (pUsagePrev = NULL, pUsage = pSession->pUsage;
         pUsage;
         pUsagePrev = pUsage, pUsage = pUsage->pNext)
    {
        if (pUsage->pObj == pObj)
        {
            rc = VINF_SUCCESS;
            AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
            if (pUsage->cUsage > 1)
            {
                pObj->cUsage--;
                pUsage->cUsage--;
            }
            else
            {
                /*
                 * Free the session record.
                 */
                if (pUsagePrev)
                    pUsagePrev->pNext = pUsage->pNext;
                else
                    pSession->pUsage = pUsage->pNext;
                RTMemFree(pUsage);

                /* What about the object? */
                if (pObj->cUsage > 1)
                    pObj->cUsage--;
                else
                {
                    /*
                     * Object is to be destroyed, unlink it.
                     */
                    rc = VINF_OBJECT_DESTROYED;
                    if (pDevExt->pObjs == pObj)
                        pDevExt->pObjs = pObj->pNext;
                    else
                    {
                        PSUPDRVOBJ pObjPrev;
                        for (pObjPrev = pDevExt->pObjs; pObjPrev; pObjPrev = pObjPrev->pNext)
                            if (pObjPrev->pNext == pObj)
                            {
                                pObjPrev->pNext = pObj->pNext;
                                break;
                            }
                        Assert(pObjPrev);
                    }
                }
            }
            break;
        }
    }

    RTCritSectLeave(&pDevExt->CritSect);

    /*
     * Call the destructor and free the object if required.
     */
    if (rc == VINF_OBJECT_DESTROYED)
    {
        if (pObj->pfnDestructor)
            pObj->pfnDestructor(pObj, pObj->pvUser1, pObj->pvUser2);
        RTMemFree(pObj);
    }

    return rc;
}


INTNETR3DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName)
{
    RT_NOREF(pvObj, pSession, pszObjName);
    return VINF_SUCCESS;
}


INTNETR3DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
{
    RT_NOREF(pSession);

    /*
     * This is used to allocate and map the send/receive buffers into the callers process space, meaning
     * we have to mmap it with the shareable attribute.
     */
    void *pv = mmap(NULL, cb, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
    if (pv == MAP_FAILED)
        return VERR_NO_MEMORY;

    *ppvR0 = (RTR0PTR)pv;
    if (ppvR3)
        *ppvR3 = pv;
    return VINF_SUCCESS;
}


INTNETR3DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
{
    RT_NOREF(pSession);

    PINTNETBUF pBuf = (PINTNETBUF)uPtr; /// @todo Hack hack hack!
    munmap((void *)uPtr, pBuf->cbBuf);
    return VINF_SUCCESS;
}


/**
 * Destroys the given internal network XPC connection session freeing all allocated resources.
 *
 * @returns Reference count of the device extension..
 * @param   pSession        The ession to destroy.
 */
static uint32_t intnetR3SessionDestroy(PSUPDRVSESSION pSession)
{
    PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
    uint32_t cRefs = ASMAtomicDecU32(&pDevExt->cRefs);
    xpc_transaction_end();
    xpc_connection_set_context(pSession->hXpcCon, NULL);
    xpc_connection_cancel(pSession->hXpcCon);
    pSession->hXpcCon = NULL;

    ASMAtomicXchgBool(&pSession->fRecvAvail, true);

    if (pSession->pUsage)
    {
        PSUPDRVUSAGE  pUsage;
        RTCritSectEnter(&pDevExt->CritSect);

        while ((pUsage = pSession->pUsage) != NULL)
        {
            PSUPDRVOBJ  pObj = pUsage->pObj;
            pSession->pUsage = pUsage->pNext;

            AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
            if (pUsage->cUsage < pObj->cUsage)
            {
                pObj->cUsage -= pUsage->cUsage;
            }
            else
            {
                /* Destroy the object and free the record. */
                if (pDevExt->pObjs == pObj)
                    pDevExt->pObjs = pObj->pNext;
                else
                {
                    PSUPDRVOBJ pObjPrev;
                    for (pObjPrev = pDevExt->pObjs; pObjPrev; pObjPrev = pObjPrev->pNext)
                        if (pObjPrev->pNext == pObj)
                        {
                            pObjPrev->pNext = pObj->pNext;
                            break;
                        }
                    Assert(pObjPrev);
                }

                RTCritSectLeave(&pDevExt->CritSect);

                if (pObj->pfnDestructor)
                    pObj->pfnDestructor(pObj, pObj->pvUser1, pObj->pvUser2);
                RTMemFree(pObj);

                RTCritSectEnter(&pDevExt->CritSect);
            }

            /* free it and continue. */
            RTMemFree(pUsage);
        }

        RTCritSectLeave(&pDevExt->CritSect);
        AssertMsg(!pSession->pUsage, ("Some buster reregistered an object during destruction!\n"));
    }

    RTMemFree(pSession);
    return cRefs;
}


/**
 * Data available in th receive buffer callback.
 */
static DECLCALLBACK(void) intnetR3RecvAvail(INTNETIFHANDLE hIf, void *pvUser)
{
    RT_NOREF(hIf);
    PSUPDRVSESSION pSession = (PSUPDRVSESSION)pvUser;

    if (ASMAtomicXchgBool(&pSession->fRecvWait, false))
    {
        /* Send an empty message. */
        xpc_object_t hObjPoke = xpc_dictionary_create(NULL, NULL, 0);
        xpc_connection_send_message(pSession->hXpcCon, hObjPoke);
        xpc_release(hObjPoke);
    }
    else
        ASMAtomicXchgBool(&pSession->fRecvAvail, true);
}


static void intnetR3RequestProcess(xpc_connection_t hCon, xpc_object_t hObj, PSUPDRVSESSION pSession)
{
    int rc = VINF_SUCCESS;
    uint64_t iReq = xpc_dictionary_get_uint64(hObj, "req-id");
    size_t cbReq = 0;
    const void *pvReq = xpc_dictionary_get_data(hObj, "req", &cbReq);
    union
    {
        INTNETOPENREQ                 OpenReq;
        INTNETIFCLOSEREQ              IfCloseReq;
        INTNETIFGETBUFFERPTRSREQ      IfGetBufferPtrsReq;
        INTNETIFSETPROMISCUOUSMODEREQ IfSetPromiscuousModeReq;
        INTNETIFSETMACADDRESSREQ      IfSetMacAddressReq;
        INTNETIFSETACTIVEREQ          IfSetActiveReq;
        INTNETIFSENDREQ               IfSendReq;
        INTNETIFWAITREQ               IfWaitReq;
        INTNETIFABORTWAITREQ          IfAbortWaitReq;
    } ReqReply;

    memcpy(&ReqReply, pvReq, RT_MIN(sizeof(ReqReply), cbReq));
    size_t cbReply = 0;

    if (pvReq)
    {
        switch (iReq)
        {
            case VMMR0_DO_INTNET_OPEN:
            {
                if (cbReq == sizeof(INTNETOPENREQ))
                {
                    rc = IntNetR3Open(pSession, &ReqReply.OpenReq.szNetwork[0], ReqReply.OpenReq.enmTrunkType, ReqReply.OpenReq.szTrunk,
                                      ReqReply.OpenReq.fFlags, ReqReply.OpenReq.cbSend, ReqReply.OpenReq.cbRecv,
                                      intnetR3RecvAvail, pSession, &ReqReply.OpenReq.hIf);
                    cbReply = sizeof(INTNETOPENREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_CLOSE:
            {
                if (cbReq == sizeof(INTNETIFCLOSEREQ))
                {
                    rc = IntNetR0IfCloseReq(pSession, &ReqReply.IfCloseReq);
                    cbReply = sizeof(INTNETIFCLOSEREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS:
            {
                if (cbReq == sizeof(INTNETIFGETBUFFERPTRSREQ))
                {
                    rc = IntNetR0IfGetBufferPtrsReq(pSession, &ReqReply.IfGetBufferPtrsReq);
                    /* This is special as we need to return a shared memory segment. */
                    xpc_object_t hObjReply = xpc_dictionary_create_reply(hObj);
                    xpc_object_t hObjShMem = xpc_shmem_create(ReqReply.IfGetBufferPtrsReq.pRing3Buf, ReqReply.IfGetBufferPtrsReq.pRing3Buf->cbBuf);
                    if (hObjShMem)
                    {
                        xpc_dictionary_set_value(hObjReply, "buf-ptr", hObjShMem);
                        xpc_release(hObjShMem);
                    }
                    else
                        rc = VERR_NO_MEMORY;

                    xpc_dictionary_set_uint64(hObjReply, "rc", INTNET_R3_SVC_SET_RC(rc));
                    xpc_connection_send_message(hCon, hObjReply);
                    xpc_release(hObjReply);
                    return;
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
            {
                if (cbReq == sizeof(INTNETIFSETPROMISCUOUSMODEREQ))
                {
                    rc = IntNetR0IfSetPromiscuousModeReq(pSession, &ReqReply.IfSetPromiscuousModeReq);
                    cbReply = sizeof(INTNETIFSETPROMISCUOUSMODEREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
            {
                if (cbReq == sizeof(INTNETIFSETMACADDRESSREQ))
                {
                    rc = IntNetR0IfSetMacAddressReq(pSession, &ReqReply.IfSetMacAddressReq);
                    cbReply = sizeof(INTNETIFSETMACADDRESSREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_SET_ACTIVE:
            {
                if (cbReq == sizeof(INTNETIFSETACTIVEREQ))
                {
                    rc = IntNetR0IfSetActiveReq(pSession, &ReqReply.IfSetActiveReq);
                    cbReply = sizeof(INTNETIFSETACTIVEREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_SEND:
            {
                if (cbReq == sizeof(INTNETIFSENDREQ))
                {
                    rc = IntNetR0IfSendReq(pSession, &ReqReply.IfSendReq);
                    cbReply = sizeof(INTNETIFSENDREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_WAIT:
            {
                if (cbReq == sizeof(INTNETIFWAITREQ))
                {
                    ASMAtomicXchgBool(&pSession->fRecvWait, true);
                    if (ASMAtomicXchgBool(&pSession->fRecvAvail, false))
                    {
                        ASMAtomicXchgBool(&pSession->fRecvWait, false);

                        /* Send an empty message. */
                        xpc_object_t hObjPoke = xpc_dictionary_create(NULL, NULL, 0);
                        xpc_connection_send_message(pSession->hXpcCon, hObjPoke);
                        xpc_release(hObjPoke);
                    }
                    return;
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            case VMMR0_DO_INTNET_IF_ABORT_WAIT:
            {
                if (cbReq == sizeof(INTNETIFABORTWAITREQ))
                {
                    ASMAtomicXchgBool(&pSession->fRecvWait, false);
                    if (ASMAtomicXchgBool(&pSession->fRecvAvail, false))
                    {
                        /* Send an empty message. */
                        xpc_object_t hObjPoke = xpc_dictionary_create(NULL, NULL, 0);
                        xpc_connection_send_message(pSession->hXpcCon, hObjPoke);
                        xpc_release(hObjPoke);
                    }
                    cbReply = sizeof(INTNETIFABORTWAITREQ);
                }
                else
                    rc = VERR_INVALID_PARAMETER;
                break;
            }
            default:
                rc = VERR_INVALID_PARAMETER;
        }
    }

    xpc_object_t hObjReply = xpc_dictionary_create_reply(hObj);
    xpc_dictionary_set_uint64(hObjReply, "rc", INTNET_R3_SVC_SET_RC(rc));
    xpc_dictionary_set_data(hObjReply, "reply", &ReqReply, cbReply);
    xpc_connection_send_message(hCon, hObjReply);
    xpc_release(hObjReply);
}


DECLCALLBACK(void) xpcConnHandler(xpc_connection_t hXpcCon)
{
    xpc_connection_set_event_handler(hXpcCon, ^(xpc_object_t hObj) {
        PSUPDRVSESSION pSession = (PSUPDRVSESSION)xpc_connection_get_context(hXpcCon);

        if (xpc_get_type(hObj) == XPC_TYPE_ERROR)
        {
            if (hObj == XPC_ERROR_CONNECTION_INVALID)
                intnetR3SessionDestroy(pSession);
            else if (hObj == XPC_ERROR_TERMINATION_IMMINENT)
            {
                PSUPDRVDEVEXT pDevExt = pSession->pDevExt;

                uint32_t cRefs = intnetR3SessionDestroy(pSession);
                if (!cRefs)
                {
                    /* Last one cleans up the global data. */
                    RTCritSectDelete(&pDevExt->CritSect);
                }
            }
        }
        else
            intnetR3RequestProcess(hXpcCon, hObj, pSession);
    });

    PSUPDRVSESSION pSession = (PSUPDRVSESSION)RTMemAllocZ(sizeof(*pSession));
    if (pSession)
    {
        pSession->pDevExt = &g_DevExt;
        pSession->hXpcCon = hXpcCon;

        xpc_connection_set_context(hXpcCon, pSession);
        xpc_connection_resume(hXpcCon);
        xpc_transaction_begin();
        ASMAtomicIncU32(&g_DevExt.cRefs);
    }
}


int main(int argc, char **argv)
{
    int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
    if (RT_SUCCESS(rc))
    {
        IntNetR0Init();

        g_DevExt.pObjs = NULL;
        rc = RTCritSectInit(&g_DevExt.CritSect);
        if (RT_SUCCESS(rc))
            xpc_main(xpcConnHandler); /* Never returns. */

        exit(EXIT_FAILURE);
    }

    return RTMsgInitFailure(rc);
}