VirtualBox

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

Last change on this file since 108380 was 107296, checked in by vboxsync, 5 months ago

Windows/Networking: Removed NDIS 5-based drivers, as we don't support Windows host OSes anymore which require this NDIS version -- only NDIS 6.0+ is supported now. bugref:10819

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1/* $Id: VBoxNetAdpInstall.cpp 107296 2024-12-12 11:07:22Z vboxsync $ */
2/** @file
3 * NetAdpInstall - VBoxNetAdp installer command line tool.
4 */
5
6/*
7 * Copyright (C) 2009-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
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#define VBOX_NETADP_INF L"VBoxNetAdp6.inf"
58
59
60static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
61{
62 RTMsgInfo("%s", pszString);
63}
64
65
66/** Wrapper around GetfullPathNameW that will try an alternative INF location.
67 *
68 * The default location is the current directory. If not found there, the
69 * alternative location is the executable directory. If not found there either,
70 * the first alternative is present to the caller.
71 */
72static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
73{
74 LPWSTR pwszFilePart;
75 DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
76 if (dwSize <= 0)
77 return dwSize;
78
79 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
80 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
81 {
82 WCHAR wsz[512];
83 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
84 if (cch > 0)
85 {
86 while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
87 cch--;
88 unsigned i = 0;
89 while (cch < sizeof(wsz) / sizeof(wsz[0]))
90 {
91 wsz[cch] = pwszFilePart[i++];
92 if (!wsz[cch])
93 {
94 dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
95 if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
96 return dwSize;
97 break;
98 }
99 cch++;
100 }
101 }
102 }
103
104 /* fallback */
105 return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
106}
107
108
109static int VBoxNetAdpInstall(void)
110{
111 RTMsgInfo("Adding host-only interface...");
112 VBoxNetCfgWinSetLogging(winNetCfgLogger);
113
114 HRESULT hr = CoInitialize(NULL);
115 if (SUCCEEDED(hr))
116 {
117 WCHAR wszInfFile[MAX_PATH];
118 DWORD cwcInfFile = MyGetfullPathNameW(VBOX_NETADP_INF, RT_ELEMENTS(wszInfFile), wszInfFile);
119 if (cwcInfFile > 0)
120 {
121 INetCfg *pnc;
122 LPWSTR lpszLockedBy = NULL;
123 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETADP_APP_NAME, 10000, &lpszLockedBy);
124 if (hr == S_OK)
125 {
126
127 hr = VBoxNetCfgWinNetAdpInstall(pnc, wszInfFile);
128
129 if (hr == S_OK)
130 RTMsgInfo("Installed successfully!");
131 else
132 RTMsgError("failed to install VBoxNetAdp: %Rhrc", hr);
133
134 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
135 }
136 else
137 RTMsgError("VBoxNetCfgWinQueryINetCfg failed: %Rhrc", hr);
138 /*
139 hr = VBoxDrvCfgInfInstall(MpInf);
140 if (FAILED(hr))
141 printf("VBoxDrvCfgInfInstall failed %#x\n", hr);
142
143 GUID guid;
144 BSTR name, errMsg;
145
146 hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface (MpInf, true, &guid, &name, &errMsg);
147 if (SUCCEEDED(hr))
148 {
149 ULONG ip, mask;
150 hr = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask);
151 if (SUCCEEDED(hr))
152 {
153 // ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip,
154 // i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter
155 ip = ip | (1 << 24);
156 hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask);
157 if (SUCCEEDED(hr))
158 printf("installation successful\n");
159 else
160 printf("VBoxNetCfgWinEnableStaticIpConfig failed: hr=%#lx\n", hr);
161 }
162 else
163 printf("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed: hr=%#lx\n", hr);
164 }
165 else
166 printf("VBoxNetCfgWinCreateHostOnlyNetworkInterface failed: hr=%#lx\n", hr);
167 */
168 }
169 else
170 {
171 DWORD dwErr = GetLastError();
172 RTMsgError("MyGetfullPathNameW failed: %Rwc", dwErr);
173 hr = HRESULT_FROM_WIN32(dwErr);
174 }
175 CoUninitialize();
176 }
177 else
178 RTMsgError("Failed initializing COM: %Rhrc", hr);
179
180 VBoxNetCfgWinSetLogging(NULL);
181
182 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
183}
184
185static int VBoxNetAdpUninstall(void)
186{
187 RTMsgInfo("Uninstalling all host-only interfaces...");
188 VBoxNetCfgWinSetLogging(winNetCfgLogger);
189
190 HRESULT hr = CoInitialize(NULL);
191 if (SUCCEEDED(hr))
192 {
193 hr = VBoxNetCfgWinRemoveAllNetDevicesOfId(VBOX_NETADP_HWID);
194 if (SUCCEEDED(hr))
195 {
196 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, L"Net", VBOX_NETADP_HWID, 0/* could be SUOI_FORCEDELETE */);
197 if (SUCCEEDED(hr))
198 RTMsgInfo("Uninstallation successful!");
199 else
200 RTMsgWarning("uninstalled successfully, but failed to remove infs (%Rhrc)\n", hr);
201 }
202 else
203 RTMsgError("uninstall failed: %Rhrc", hr);
204 CoUninitialize();
205 }
206 else
207 RTMsgError("Failed initializing COM: %Rhrc", hr);
208
209 VBoxNetCfgWinSetLogging(NULL);
210
211 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
212}
213
214static int VBoxNetAdpUpdate(void)
215{
216 RTMsgInfo("Uninstalling all host-only interfaces...");
217 VBoxNetCfgWinSetLogging(winNetCfgLogger);
218
219 HRESULT hr = CoInitialize(NULL);
220 if (SUCCEEDED(hr))
221 {
222 BOOL fRebootRequired = FALSE;
223 /*
224 * Before we can update the driver for existing adapters we need to remove
225 * all old driver packages from the driver cache. Otherwise we may end up
226 * with both NDIS5 and NDIS6 versions of VBoxNetAdp in the cache which
227 * will cause all sorts of trouble.
228 */
229 VBoxDrvCfgInfUninstallAllF(L"Net", VBOX_NETADP_HWID, SUOI_FORCEDELETE);
230 hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(VBOX_NETADP_INF, &fRebootRequired, VBOX_NETADP_HWID);
231 if (SUCCEEDED(hr))
232 {
233 if (fRebootRequired)
234 RTMsgWarning("!!REBOOT REQUIRED!!");
235 RTMsgInfo("Updated successfully!");
236 }
237 else
238 RTMsgError("update failed: %Rhrc", hr);
239
240 CoUninitialize();
241 }
242 else
243 RTMsgError("Failed initializing COM: %Rhrc", hr);
244
245 VBoxNetCfgWinSetLogging(NULL);
246
247 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
248}
249
250static int VBoxNetAdpDisable(void)
251{
252 RTMsgInfo("Disabling all host-only interfaces...");
253 VBoxNetCfgWinSetLogging(winNetCfgLogger);
254
255 HRESULT hr = CoInitialize(NULL);
256 if (SUCCEEDED(hr))
257 {
258 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE);
259 if (SUCCEEDED(hr))
260 RTMsgInfo("Disabling successful");
261 else
262 RTMsgError("disable failed: %Rhrc", hr);
263
264 CoUninitialize();
265 }
266 else
267 RTMsgError("Failed initializing COM: %Rhrc", hr);
268
269 VBoxNetCfgWinSetLogging(NULL);
270
271 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
272}
273
274static int VBoxNetAdpEnable(void)
275{
276 RTMsgInfo("Enabling all host-only interfaces...");
277 VBoxNetCfgWinSetLogging(winNetCfgLogger);
278
279 HRESULT hr = CoInitialize(NULL);
280 if (SUCCEEDED(hr))
281 {
282 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_ENABLE);
283 if (SUCCEEDED(hr))
284 RTMsgInfo("Enabling successful!");
285 else
286 RTMsgError("enabling failed: %hrc", hr);
287
288 CoUninitialize();
289 }
290 else
291 RTMsgError("Failed initializing COM: %Rhrc", hr);
292
293 VBoxNetCfgWinSetLogging(NULL);
294
295 return SUCCEEDED(hr) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
296}
297
298static void printUsage(void)
299{
300 RTPrintf("host-only network adapter configuration tool\n"
301 " Usage: %s [cmd]\n"
302 " cmd can be one of the following values:\n"
303 " i - install a new host-only interface (default command)\n"
304 " u - uninstall all host-only interfaces\n"
305 " a - update the host-only driver\n"
306 " d - disable all host-only interfaces\n"
307 " e - enable all host-only interfaces\n"
308 " h - print this message\n",
309 RTProcShortName());
310}
311
312int __cdecl main(int argc, char **argv)
313{
314 RTR3InitExe(argc, &argv, 0);
315
316 if (argc < 2)
317 return VBoxNetAdpInstall();
318 if (argc > 2)
319 {
320 printUsage();
321 return RTEXITCODE_SYNTAX;
322 }
323
324 if (!strcmp(argv[1], "i"))
325 return VBoxNetAdpInstall();
326 if (!strcmp(argv[1], "u"))
327 return VBoxNetAdpUninstall();
328 if (!strcmp(argv[1], "a"))
329 return VBoxNetAdpUpdate();
330 if (!strcmp(argv[1], "d"))
331 return VBoxNetAdpDisable();
332 if (!strcmp(argv[1], "e"))
333 return VBoxNetAdpEnable();
334
335 printUsage();
336 return !strcmp(argv[1], "h") ? RTEXITCODE_SUCCESS : RTEXITCODE_SYNTAX;
337}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette