1 | /* $Id: RpcChannelHook.cpp 71160 2018-02-28 17:07:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Global COM Class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2018 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 | #include <iprt\assert.h>
|
---|
20 | #include <iprt\process.h>
|
---|
21 | #include <iprt\string.h>
|
---|
22 | #include <iprt/asm.h>
|
---|
23 | #include "Logging.h"
|
---|
24 | #include <VBox/com/VirtualBox.h>
|
---|
25 |
|
---|
26 | #include <string>
|
---|
27 |
|
---|
28 | #include "VirtualBox.h"
|
---|
29 | #include "RpcChannelHook.h"
|
---|
30 |
|
---|
31 | #ifdef RT_OS_WINDOWS
|
---|
32 |
|
---|
33 | volatile bool CRpcChannelHook::s_bChannelRegistered = false;
|
---|
34 | volatile bool CRpcChannelHook::s_bVBpoxSDSCalledOnce = false;
|
---|
35 | CRpcChannelHook CRpcChannelHook::s_RpcChannelHook;
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * RpcChannelHook IUnknown interface implementation.
|
---|
39 | */
|
---|
40 |
|
---|
41 |
|
---|
42 | STDMETHODIMP CRpcChannelHook::QueryInterface(REFIID riid, void **ppv)
|
---|
43 | {
|
---|
44 | if (riid == IID_IUnknown)
|
---|
45 | {
|
---|
46 | *ppv = (IUnknown*)this;
|
---|
47 | }
|
---|
48 | else if (riid == IID_IChannelHook)
|
---|
49 | {
|
---|
50 | *ppv = (IChannelHook*)this;
|
---|
51 | }
|
---|
52 | else
|
---|
53 | {
|
---|
54 | *ppv = 0;
|
---|
55 | return E_NOINTERFACE;
|
---|
56 | }
|
---|
57 | this->AddRef();
|
---|
58 | return S_OK;
|
---|
59 | }
|
---|
60 |
|
---|
61 | STDMETHODIMP_(ULONG) CRpcChannelHook::AddRef(void)
|
---|
62 | {
|
---|
63 | return 2;
|
---|
64 | }
|
---|
65 |
|
---|
66 | STDMETHODIMP_(ULONG) CRpcChannelHook::Release(void)
|
---|
67 | {
|
---|
68 | return 1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * C wrapper functions.
|
---|
73 | */
|
---|
74 |
|
---|
75 | // Warning: functions below are not thread safe
|
---|
76 | extern "C"
|
---|
77 | {
|
---|
78 | void SetupClientRpcChannelHook(void)
|
---|
79 | {
|
---|
80 | // register single hook only
|
---|
81 | if (!CRpcChannelHook::IsChannelHookRegistered())
|
---|
82 | {
|
---|
83 | HRESULT hr = CoRegisterChannelHook(RPC_CHANNEL_EXTENSION_GUID,
|
---|
84 | &CRpcChannelHook::s_RpcChannelHook);
|
---|
85 | NOREF(hr);
|
---|
86 | Assert(SUCCEEDED(hr));
|
---|
87 | CRpcChannelHook::RegisterChannelHook();
|
---|
88 | LogFunc(("Registered RPC client channel hook \n"));
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Internal functions.
|
---|
96 | */
|
---|
97 |
|
---|
98 | bool CRpcChannelHook::IsChannelHookRegistered()
|
---|
99 | {
|
---|
100 | return ASMAtomicReadBool(&s_bChannelRegistered);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | void CRpcChannelHook::RegisterChannelHook()
|
---|
105 | {
|
---|
106 | ASMAtomicWriteBool(&s_bChannelRegistered, true);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* RpcChannelHook IChannelHook interface implementation.*/
|
---|
110 |
|
---|
111 |
|
---|
112 | STDMETHODIMP_(void) CRpcChannelHook::ClientGetSize(REFGUID uExtent, REFIID riid, ULONG *pDataSize)
|
---|
113 | {
|
---|
114 | NOREF(riid);
|
---|
115 | Assert(uExtent == m_ChannelHookID);
|
---|
116 | Assert(pDataSize);
|
---|
117 | if (uExtent == m_ChannelHookID)
|
---|
118 | {
|
---|
119 | if (pDataSize)
|
---|
120 | {
|
---|
121 | *pDataSize = 0;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * This is callback of RPC channel hook called on COM client when COM method call
|
---|
128 | * finished on server and response returned.
|
---|
129 | * We use it to catch a moment when a new VirtualBox object sucessfully instantiated.
|
---|
130 | * This callback is called in client process - VirtualBox.exe, VBoxManage or custom client
|
---|
131 | * If it happend we register new API client in VBoxSDS.
|
---|
132 | * Parameters:
|
---|
133 | * uExtent - unique ID of our RPC channel
|
---|
134 | * rIID - IID of called server interface (IVirtualBox in our case)
|
---|
135 | * pDataBuffer - NULL, such as we have nothing to transfer from server side
|
---|
136 | * hrFault - result of called COM server method
|
---|
137 | */
|
---|
138 | STDMETHODIMP_(void) CRpcChannelHook::ClientNotify(REFGUID uExtent, REFIID riid,
|
---|
139 | ULONG cbDataSize, void *pDataBuffer, DWORD lDataRep, HRESULT hrFault)
|
---|
140 | {
|
---|
141 | NOREF(cbDataSize);
|
---|
142 | NOREF(pDataBuffer);
|
---|
143 | NOREF(lDataRep);
|
---|
144 |
|
---|
145 | // Check that it created VirtualBox and this is first method called on server
|
---|
146 | // (CreateInstance)
|
---|
147 | if (uExtent == m_ChannelHookID &&
|
---|
148 | riid == IID_IVirtualBox &&
|
---|
149 | !ASMAtomicReadBool(&s_bVBpoxSDSCalledOnce) &&
|
---|
150 | SUCCEEDED(hrFault) )
|
---|
151 | {
|
---|
152 | LogFunc(("Finished call of VirtualBox method\n"));
|
---|
153 |
|
---|
154 | ASMAtomicWriteBool(&s_bVBpoxSDSCalledOnce, true);
|
---|
155 | ComPtr<IVirtualBoxClientList> ptrClientList;
|
---|
156 | /*
|
---|
157 | * Connect to VBoxSDS.
|
---|
158 | * Note: VBoxSDS can handle duplicate calls
|
---|
159 | */
|
---|
160 | HRESULT hrc = CoCreateInstance(CLSID_VirtualBoxClientList, NULL, CLSCTX_LOCAL_SERVER, IID_IVirtualBoxClientList,
|
---|
161 | (void **)ptrClientList.asOutParam());
|
---|
162 | if (SUCCEEDED(hrc))
|
---|
163 | {
|
---|
164 | RTPROCESS pid = RTProcSelf();
|
---|
165 | hrc = ptrClientList->RegisterClient(pid);
|
---|
166 | LogFunc(("Called VBoxSDS RegisterClient() : hr=%Rhrf\n", hrc));
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | LogFunc(("Error to connect to VBoxSDS: hr=%Rhrf\n", hrc));
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | STDMETHODIMP_(void) CRpcChannelHook::ClientFillBuffer(REFGUID uExtent, REFIID riid,
|
---|
176 | ULONG *pDataSize, void *pDataBuffer)
|
---|
177 | {
|
---|
178 | NOREF(uExtent);
|
---|
179 | NOREF(riid);
|
---|
180 | NOREF(pDataSize);
|
---|
181 | NOREF(pDataBuffer);
|
---|
182 | }
|
---|
183 |
|
---|
184 | STDMETHODIMP_(void) CRpcChannelHook::ServerGetSize(REFGUID uExtent, REFIID riid,
|
---|
185 | HRESULT hrFault, ULONG *pDataSize)
|
---|
186 | {
|
---|
187 | // Nothing to send to client side from server side
|
---|
188 | NOREF(uExtent);
|
---|
189 | NOREF(riid);
|
---|
190 | NOREF(hrFault);
|
---|
191 | *pDataSize = 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | STDMETHODIMP_(void) CRpcChannelHook::ServerNotify(REFGUID uExtent, REFIID riid,
|
---|
195 | ULONG cbDataSize, void *pDataBuffer, DWORD lDataRep)
|
---|
196 | {
|
---|
197 | // Nothing to do on server side
|
---|
198 | NOREF(uExtent);
|
---|
199 | NOREF(riid);
|
---|
200 | NOREF(cbDataSize);
|
---|
201 | NOREF(pDataBuffer);
|
---|
202 | NOREF(lDataRep);
|
---|
203 | }
|
---|
204 |
|
---|
205 | STDMETHODIMP_(void) CRpcChannelHook::ServerFillBuffer(REFGUID uExtent, REFIID riid,
|
---|
206 | ULONG *pDataSize, void *pDataBuffer, HRESULT hrFault)
|
---|
207 | {
|
---|
208 | // Nothing to send to client side from server side
|
---|
209 | NOREF(uExtent);
|
---|
210 | NOREF(riid);
|
---|
211 | NOREF(pDataSize);
|
---|
212 | NOREF(pDataBuffer);
|
---|
213 | NOREF(hrFault);
|
---|
214 | }
|
---|
215 |
|
---|
216 | #endif
|
---|