VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetAdpInstall.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 21 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: VBoxNetAdpInstall.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * NetAdpInstall - VBoxNetAdp installer command line tool.
4 */
5
6/*
7 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <VBox/VBoxNetCfg-win.h>
42#include <VBox/VBoxDrvCfg-win.h>
43#include <devguid.h>
44
45#include <iprt/initterm.h>
46#include <iprt/message.h>
47#include <iprt/process.h>
48#include <iprt/stream.h>
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54#define VBOX_NETADP_APP_NAME L"NetAdpInstall"
55
56#define VBOX_NETADP_HWID L"sun_VBoxNetAdp"
57#ifdef NDIS60
58# define VBOX_NETADP_INF L"VBoxNetAdp6.inf"
59#else
60# define VBOX_NETADP_INF L"VBoxNetAdp.inf"
61#endif
62
63
64static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
65{
66 RTMsgInfo("%s", pszString);
67}
68
69
70/** Wrapper around GetfullPathNameW that will try an alternative INF location.
71 *
72 * The default location is the current directory. If not found there, the
73 * alternative location is the executable directory. If not found there either,
74 * the first alternative is present to the caller.
75 */
76static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
77{
78 LPWSTR pwszFilePart;
79 DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
80 if (dwSize <= 0)
81 return dwSize;
82
83 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
84 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
85 {
86 WCHAR wsz[512];
87 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
88 if (cch > 0)
89 {
90 while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
91 cch--;
92 unsigned i = 0;
93 while (cch < sizeof(wsz) / sizeof(wsz[0]))
94 {
95 wsz[cch] = pwszFilePart[i++];
96 if (!wsz[cch])
97 {
98 dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
99 if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
100 return dwSize;
101 break;
102 }
103 cch++;
104 }
105 }
106 }
107
108 /* fallback */
109 return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
110}
111
112
113static int VBoxNetAdpInstall(void)
114{
115 RTMsgInfo("Adding host-only interface...");
116 VBoxNetCfgWinSetLogging(winNetCfgLogger);
117
118 HRESULT hr = CoInitialize(NULL);
119 if (SUCCEEDED(hr))
120 {
121 WCHAR wszInfFile[MAX_PATH];
122 DWORD cwcInfFile = MyGetfullPathNameW(VBOX_NETADP_INF, RT_ELEMENTS(wszInfFile), wszInfFile);
123 if (cwcInfFile > 0)
124 {
125 INetCfg *pnc;
126 LPWSTR lpszLockedBy = NULL;
127 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETADP_APP_NAME, 10000, &lpszLockedBy);
128 if (hr == S_OK)
129 {
130
131 hr = VBoxNetCfgWinNetAdpInstall(pnc, wszInfFile);
132
133 if (hr == S_OK)
134 RTMsgInfo("Installed successfully!");
135 else
136 RTMsgError("failed to install VBoxNetAdp: %Rhrc", hr);
137
138 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
139 }
140 else
141 RTMsgError("VBoxNetCfgWinQueryINetCfg failed: %Rhrc", hr);
142 /*
143 hr = VBoxDrvCfgInfInstall(MpInf);
144 if (FAILED(hr))
145 printf("VBoxDrvCfgInfInstall failed %#x\n", hr);
146
147 GUID guid;
148 BSTR name, errMsg;
149
150 hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface (MpInf, true, &guid, &name, &errMsg);
151 if (SUCCEEDED(hr))
152 {
153 ULONG ip, mask;
154 hr = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask);
155 if (SUCCEEDED(hr))
156 {
157 // ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip,
158 // i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter
159 ip = ip | (1 << 24);
160 hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask);
161 if (SUCCEEDED(hr))
162 printf("installation successful\n");
163 else
164 printf("VBoxNetCfgWinEnableStaticIpConfig failed: hr=%#lx\n", hr);
165 }
166 else
167 printf("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed: hr=%#lx\n", hr);
168 }
169 else
170 printf("VBoxNetCfgWinCreateHostOnlyNetworkInterface failed: hr=%#lx\n", hr);
171 */
172 }
173 else
174 {
175 DWORD dwErr = GetLastError();
176 RTMsgError("MyGetfullPathNameW failed: %Rwc", dwErr);
177 hr = HRESULT_FROM_WIN32(dwErr);
178 }
179 CoUninitialize();
180 }
181 else
182 RTMsgError("Failed initializing COM: %Rhrc", hr);
183
184 VBoxNetCfgWinSetLogging(NULL);
185
186 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
187}
188
189static int VBoxNetAdpUninstall(void)
190{
191 RTMsgInfo("Uninstalling all host-only interfaces...");
192 VBoxNetCfgWinSetLogging(winNetCfgLogger);
193
194 HRESULT hr = CoInitialize(NULL);
195 if (SUCCEEDED(hr))
196 {
197 hr = VBoxNetCfgWinRemoveAllNetDevicesOfId(VBOX_NETADP_HWID);
198 if (SUCCEEDED(hr))
199 {
200 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, L"Net", VBOX_NETADP_HWID, 0/* could be SUOI_FORCEDELETE */);
201 if (SUCCEEDED(hr))
202 RTMsgInfo("Uninstallation successful!");
203 else
204 RTMsgWarning("uninstalled successfully, but failed to remove infs (%Rhrc)\n", hr);
205 }
206 else
207 RTMsgError("uninstall failed: %Rhrc", hr);
208 CoUninitialize();
209 }
210 else
211 RTMsgError("Failed initializing COM: %Rhrc", hr);
212
213 VBoxNetCfgWinSetLogging(NULL);
214
215 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
216}
217
218static int VBoxNetAdpUpdate(void)
219{
220 RTMsgInfo("Uninstalling all host-only interfaces...");
221 VBoxNetCfgWinSetLogging(winNetCfgLogger);
222
223 HRESULT hr = CoInitialize(NULL);
224 if (SUCCEEDED(hr))
225 {
226 BOOL fRebootRequired = FALSE;
227 /*
228 * Before we can update the driver for existing adapters we need to remove
229 * all old driver packages from the driver cache. Otherwise we may end up
230 * with both NDIS5 and NDIS6 versions of VBoxNetAdp in the cache which
231 * will cause all sorts of trouble.
232 */
233 VBoxDrvCfgInfUninstallAllF(L"Net", VBOX_NETADP_HWID, SUOI_FORCEDELETE);
234 hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(VBOX_NETADP_INF, &fRebootRequired, VBOX_NETADP_HWID);
235 if (SUCCEEDED(hr))
236 {
237 if (fRebootRequired)
238 RTMsgWarning("!!REBOOT REQUIRED!!");
239 RTMsgInfo("Updated successfully!");
240 }
241 else
242 RTMsgError("update failed: %Rhrc", hr);
243
244 CoUninitialize();
245 }
246 else
247 RTMsgError("Failed initializing COM: %Rhrc", hr);
248
249 VBoxNetCfgWinSetLogging(NULL);
250
251 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
252}
253
254static int VBoxNetAdpDisable(void)
255{
256 RTMsgInfo("Disabling all host-only interfaces...");
257 VBoxNetCfgWinSetLogging(winNetCfgLogger);
258
259 HRESULT hr = CoInitialize(NULL);
260 if (SUCCEEDED(hr))
261 {
262 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE);
263 if (SUCCEEDED(hr))
264 RTMsgInfo("Disabling successful");
265 else
266 RTMsgError("disable failed: %Rhrc", hr);
267
268 CoUninitialize();
269 }
270 else
271 RTMsgError("Failed initializing COM: %Rhrc", hr);
272
273 VBoxNetCfgWinSetLogging(NULL);
274
275 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
276}
277
278static int VBoxNetAdpEnable(void)
279{
280 RTMsgInfo("Enabling all host-only interfaces...");
281 VBoxNetCfgWinSetLogging(winNetCfgLogger);
282
283 HRESULT hr = CoInitialize(NULL);
284 if (SUCCEEDED(hr))
285 {
286 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_ENABLE);
287 if (SUCCEEDED(hr))
288 RTMsgInfo("Enabling successful!");
289 else
290 RTMsgError("enabling failed: %hrc", hr);
291
292 CoUninitialize();
293 }
294 else
295 RTMsgError("Failed initializing COM: %Rhrc", hr);
296
297 VBoxNetCfgWinSetLogging(NULL);
298
299 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
300}
301
302static void printUsage(void)
303{
304 RTPrintf("host-only network adapter configuration tool\n"
305 " Usage: %s [cmd]\n"
306 " cmd can be one of the following values:\n"
307 " i - install a new host-only interface (default command)\n"
308 " u - uninstall all host-only interfaces\n"
309 " a - update the host-only driver\n"
310 " d - disable all host-only interfaces\n"
311 " e - enable all host-only interfaces\n"
312 " h - print this message\n",
313 RTProcShortName());
314}
315
316int __cdecl main(int argc, char **argv)
317{
318 RTR3InitExe(argc, &argv, 0);
319
320 if (argc < 2)
321 return VBoxNetAdpInstall();
322 if (argc > 2)
323 {
324 printUsage();
325 return RTEXITCODE_SYNTAX;
326 }
327
328 if (!strcmp(argv[1], "i"))
329 return VBoxNetAdpInstall();
330 if (!strcmp(argv[1], "u"))
331 return VBoxNetAdpUninstall();
332 if (!strcmp(argv[1], "a"))
333 return VBoxNetAdpUpdate();
334 if (!strcmp(argv[1], "d"))
335 return VBoxNetAdpDisable();
336 if (!strcmp(argv[1], "e"))
337 return VBoxNetAdpEnable();
338
339 printUsage();
340 return !strcmp(argv[1], "h") ? RTEXITCODE_SUCCESS : RTEXITCODE_SYNTAX;
341}
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