1 | /* $Id: VBoxNetSend.h 50410 2014-02-11 09:37:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * A place to share code and definitions between VBoxNetAdp and VBoxNetFlt host drivers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /** @todo move this to src/VBox/HostDrivers/darwin as a .cpp file. */
|
---|
28 | #ifndef ___VBox_VBoxNetSend_h
|
---|
29 | #define ___VBox_VBoxNetSend_h
|
---|
30 |
|
---|
31 | #if defined(RT_OS_DARWIN)
|
---|
32 |
|
---|
33 | # include <iprt/err.h>
|
---|
34 | # include <iprt/assert.h>
|
---|
35 | # include <iprt/string.h>
|
---|
36 |
|
---|
37 | # include <sys/socket.h>
|
---|
38 | # include <net/kpi_interface.h>
|
---|
39 | RT_C_DECLS_BEGIN /* Buggy 10.4 headers, fixed in 10.5. */
|
---|
40 | # include <sys/kpi_mbuf.h>
|
---|
41 | RT_C_DECLS_END
|
---|
42 | # include <net/if.h>
|
---|
43 |
|
---|
44 | RT_C_DECLS_BEGIN
|
---|
45 |
|
---|
46 | # if defined(IN_RING0)
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs and submits a dummy packet to ifnet_input().
|
---|
50 | *
|
---|
51 | * This is a workaround for "stuck dock icon" issue. When the first packet goes
|
---|
52 | * through the interface DLIL grabs a reference to the thread that submits the
|
---|
53 | * packet and holds it until the interface is destroyed. By submitting this
|
---|
54 | * dummy we make DLIL grab the thread of a non-GUI process.
|
---|
55 | *
|
---|
56 | * Most of this function was copied from vboxNetFltDarwinMBufFromSG().
|
---|
57 | *
|
---|
58 | * @returns VBox status code.
|
---|
59 | * @param pIfNet The interface that will hold the reference to the calling
|
---|
60 | * thread. We submit dummy as if it was coming from this interface.
|
---|
61 | */
|
---|
62 | DECLINLINE(int) VBoxNetSendDummy(ifnet_t pIfNet)
|
---|
63 | {
|
---|
64 | int rc = VINF_SUCCESS;
|
---|
65 |
|
---|
66 | size_t cbTotal = 50; /* No Ethernet header */
|
---|
67 | mbuf_how_t How = MBUF_WAITOK;
|
---|
68 | mbuf_t pPkt = NULL;
|
---|
69 | errno_t err = mbuf_allocpacket(How, cbTotal, NULL, &pPkt);
|
---|
70 | if (!err)
|
---|
71 | {
|
---|
72 | /* Skip zero sized memory buffers (paranoia). */
|
---|
73 | mbuf_t pCur = pPkt;
|
---|
74 | while (pCur && !mbuf_maxlen(pCur))
|
---|
75 | pCur = mbuf_next(pCur);
|
---|
76 | Assert(pCur);
|
---|
77 |
|
---|
78 | /* Set the required packet header attributes. */
|
---|
79 | mbuf_pkthdr_setlen(pPkt, cbTotal);
|
---|
80 | mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
|
---|
81 |
|
---|
82 | mbuf_setlen(pCur, cbTotal);
|
---|
83 | memset(mbuf_data(pCur), 0, cbTotal);
|
---|
84 |
|
---|
85 | mbuf_pkthdr_setrcvif(pPkt, pIfNet); /* will crash without this. */
|
---|
86 |
|
---|
87 | errno_t err = ifnet_input(pIfNet, pPkt, NULL);
|
---|
88 | if (err)
|
---|
89 | {
|
---|
90 | rc = RTErrConvertFromErrno(err);
|
---|
91 | mbuf_freem(pPkt);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | else
|
---|
95 | rc = RTErrConvertFromErrno(err);
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | # endif /* IN_RING0 */
|
---|
101 |
|
---|
102 | RT_C_DECLS_END
|
---|
103 |
|
---|
104 | #endif /* RT_OS_DARWIN */
|
---|
105 |
|
---|
106 | #endif /* !___VBox_VBoxNetSend_h */
|
---|
107 |
|
---|