1 | /* $Id: VBoxNetAdpInstall.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NetAdpInstall - VBoxNetAdp installer command line tool.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2017 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 | #include <VBox/VBoxNetCfg-win.h>
|
---|
28 | #include <VBox/VBoxDrvCfg-win.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <devguid.h>
|
---|
31 |
|
---|
32 | #define VBOX_NETADP_APP_NAME L"NetAdpInstall"
|
---|
33 |
|
---|
34 | #define VBOX_NETADP_HWID L"sun_VBoxNetAdp"
|
---|
35 | #ifdef NDIS60
|
---|
36 | #define VBOX_NETADP_INF L"VBoxNetAdp6.inf"
|
---|
37 | #else /* !NDIS60 */
|
---|
38 | #define VBOX_NETADP_INF L"VBoxNetAdp.inf"
|
---|
39 | #endif /* !NDIS60 */
|
---|
40 |
|
---|
41 | static VOID winNetCfgLogger(LPCSTR szString)
|
---|
42 | {
|
---|
43 | printf("%s\n", szString);
|
---|
44 | }
|
---|
45 |
|
---|
46 | static int VBoxNetAdpInstall(void)
|
---|
47 | {
|
---|
48 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
49 |
|
---|
50 | HRESULT hr = CoInitialize(NULL);
|
---|
51 | if (SUCCEEDED(hr))
|
---|
52 | {
|
---|
53 | wprintf(L"adding host-only interface..\n");
|
---|
54 |
|
---|
55 | DWORD dwErr = ERROR_SUCCESS;
|
---|
56 | WCHAR MpInf[MAX_PATH];
|
---|
57 |
|
---|
58 | if (!GetFullPathNameW(VBOX_NETADP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf, NULL))
|
---|
59 | dwErr = GetLastError();
|
---|
60 |
|
---|
61 | if (dwErr == ERROR_SUCCESS)
|
---|
62 | {
|
---|
63 | INetCfg *pnc;
|
---|
64 | LPWSTR lpszLockedBy = NULL;
|
---|
65 | hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETADP_APP_NAME, 10000, &lpszLockedBy);
|
---|
66 | if (hr == S_OK)
|
---|
67 | {
|
---|
68 |
|
---|
69 | hr = VBoxNetCfgWinNetAdpInstall(pnc, MpInf);
|
---|
70 |
|
---|
71 | if (hr == S_OK)
|
---|
72 | {
|
---|
73 | wprintf(L"installed successfully\n");
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | wprintf(L"error installing VBoxNetAdp (0x%x)\n", hr);
|
---|
78 | }
|
---|
79 |
|
---|
80 | VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | wprintf(L"VBoxNetCfgWinQueryINetCfg failed: hr = 0x%x\n", hr);
|
---|
84 | /*
|
---|
85 | hr = VBoxDrvCfgInfInstall(MpInf);
|
---|
86 | if (FAILED(hr))
|
---|
87 | printf("VBoxDrvCfgInfInstall failed %#x\n", hr);
|
---|
88 |
|
---|
89 | GUID guid;
|
---|
90 | BSTR name, errMsg;
|
---|
91 |
|
---|
92 | hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface (MpInf, true, &guid, &name, &errMsg);
|
---|
93 | if (SUCCEEDED(hr))
|
---|
94 | {
|
---|
95 | ULONG ip, mask;
|
---|
96 | hr = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask);
|
---|
97 | if (SUCCEEDED(hr))
|
---|
98 | {
|
---|
99 | // ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip,
|
---|
100 | // i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter
|
---|
101 | ip = ip | (1 << 24);
|
---|
102 | hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask);
|
---|
103 | if (SUCCEEDED(hr))
|
---|
104 | {
|
---|
105 | printf("installation successful\n");
|
---|
106 | }
|
---|
107 | else
|
---|
108 | printf("VBoxNetCfgWinEnableStaticIpConfig failed: hr = 0x%x\n", hr);
|
---|
109 | }
|
---|
110 | else
|
---|
111 | printf("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed: hr = 0x%x\n", hr);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | printf("VBoxNetCfgWinCreateHostOnlyNetworkInterface failed: hr = 0x%x\n", hr);
|
---|
115 | */
|
---|
116 | }
|
---|
117 | else
|
---|
118 | {
|
---|
119 | wprintf(L"GetFullPathNameW failed: winEr = %d\n", dwErr);
|
---|
120 | hr = HRESULT_FROM_WIN32(dwErr);
|
---|
121 |
|
---|
122 | }
|
---|
123 | CoUninitialize();
|
---|
124 | }
|
---|
125 | else
|
---|
126 | wprintf(L"Error initializing COM (0x%x)\n", hr);
|
---|
127 |
|
---|
128 | VBoxNetCfgWinSetLogging(NULL);
|
---|
129 |
|
---|
130 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int VBoxNetAdpUninstall(void)
|
---|
134 | {
|
---|
135 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
136 |
|
---|
137 | printf("uninstalling all host-only interfaces..\n");
|
---|
138 |
|
---|
139 | HRESULT hr = CoInitialize(NULL);
|
---|
140 | if (SUCCEEDED(hr))
|
---|
141 | {
|
---|
142 | hr = VBoxNetCfgWinRemoveAllNetDevicesOfId(VBOX_NETADP_HWID);
|
---|
143 | if (SUCCEEDED(hr))
|
---|
144 | {
|
---|
145 | hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, L"Net", VBOX_NETADP_HWID, 0/* could be SUOI_FORCEDELETE */);
|
---|
146 | if (SUCCEEDED(hr))
|
---|
147 | {
|
---|
148 | printf("uninstallation successful\n");
|
---|
149 | }
|
---|
150 | else
|
---|
151 | printf("uninstalled successfully, but failed to remove infs\n");
|
---|
152 | }
|
---|
153 | else
|
---|
154 | printf("uninstall failed, hr = 0x%x\n", hr);
|
---|
155 | CoUninitialize();
|
---|
156 | }
|
---|
157 | else
|
---|
158 | printf("Error initializing COM (0x%x)\n", hr);
|
---|
159 |
|
---|
160 | VBoxNetCfgWinSetLogging(NULL);
|
---|
161 |
|
---|
162 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static int VBoxNetAdpUpdate(void)
|
---|
166 | {
|
---|
167 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
168 |
|
---|
169 | printf("uninstalling all host-only interfaces..\n");
|
---|
170 |
|
---|
171 | HRESULT hr = CoInitialize(NULL);
|
---|
172 | if (SUCCEEDED(hr))
|
---|
173 | {
|
---|
174 | BOOL fRebootRequired = FALSE;
|
---|
175 | /*
|
---|
176 | * Before we can update the driver for existing adapters we need to remove
|
---|
177 | * all old driver packages from the driver cache. Otherwise we may end up
|
---|
178 | * with both NDIS5 and NDIS6 versions of VBoxNetAdp in the cache which
|
---|
179 | * will cause all sorts of trouble.
|
---|
180 | */
|
---|
181 | VBoxDrvCfgInfUninstallAllF(L"Net", VBOX_NETADP_HWID, SUOI_FORCEDELETE);
|
---|
182 | hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(VBOX_NETADP_INF, &fRebootRequired, VBOX_NETADP_HWID);
|
---|
183 | if (SUCCEEDED(hr))
|
---|
184 | {
|
---|
185 | if (fRebootRequired)
|
---|
186 | printf("!!REBOOT REQUIRED!!\n");
|
---|
187 | printf("updated successfully\n");
|
---|
188 | }
|
---|
189 | else
|
---|
190 | printf("update failed, hr = 0x%x\n", hr);
|
---|
191 |
|
---|
192 | CoUninitialize();
|
---|
193 | }
|
---|
194 | else
|
---|
195 | printf("Error initializing COM (0x%x)\n", hr);
|
---|
196 |
|
---|
197 | VBoxNetCfgWinSetLogging(NULL);
|
---|
198 |
|
---|
199 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int VBoxNetAdpDisable(void)
|
---|
203 | {
|
---|
204 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
205 |
|
---|
206 | printf("disabling all host-only interfaces..\n");
|
---|
207 |
|
---|
208 | HRESULT hr = CoInitialize(NULL);
|
---|
209 | if (SUCCEEDED(hr))
|
---|
210 | {
|
---|
211 | hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE);
|
---|
212 | if (SUCCEEDED(hr))
|
---|
213 | {
|
---|
214 | printf("disabling successful\n");
|
---|
215 | }
|
---|
216 | else
|
---|
217 | printf("disable failed, hr = 0x%x\n", hr);
|
---|
218 |
|
---|
219 | CoUninitialize();
|
---|
220 | }
|
---|
221 | else
|
---|
222 | printf("Error initializing COM (0x%x)\n", hr);
|
---|
223 |
|
---|
224 | VBoxNetCfgWinSetLogging(NULL);
|
---|
225 |
|
---|
226 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static int VBoxNetAdpEnable(void)
|
---|
230 | {
|
---|
231 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
232 |
|
---|
233 | printf("enabling all host-only interfaces..\n");
|
---|
234 |
|
---|
235 | HRESULT hr = CoInitialize(NULL);
|
---|
236 | if (SUCCEEDED(hr))
|
---|
237 | {
|
---|
238 | hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_ENABLE);
|
---|
239 | if (SUCCEEDED(hr))
|
---|
240 | {
|
---|
241 | printf("enabling successful\n");
|
---|
242 | }
|
---|
243 | else
|
---|
244 | printf("enabling failed, hr = 0x%x\n", hr);
|
---|
245 |
|
---|
246 | CoUninitialize();
|
---|
247 | }
|
---|
248 | else
|
---|
249 | printf("Error initializing COM (0x%x)\n", hr);
|
---|
250 |
|
---|
251 | VBoxNetCfgWinSetLogging(NULL);
|
---|
252 |
|
---|
253 | return SUCCEEDED(hr) ? 0 : 1;
|
---|
254 | }
|
---|
255 |
|
---|
256 | static void printUsage(void)
|
---|
257 | {
|
---|
258 | printf("host-only network adapter configuration tool\n"
|
---|
259 | " Usage: VBoxNetAdpInstall [cmd]\n"
|
---|
260 | " cmd can be one of the following values:\n"
|
---|
261 | " i - install a new host-only interface (default command)\n"
|
---|
262 | " u - uninstall all host-only interfaces\n"
|
---|
263 | " a - update the host-only driver\n"
|
---|
264 | " d - disable all host-only interfaces\n"
|
---|
265 | " e - enable all host-only interfaces\n"
|
---|
266 | " h - print this message\n");
|
---|
267 | }
|
---|
268 |
|
---|
269 | int __cdecl main(int argc, char **argv)
|
---|
270 | {
|
---|
271 | if (argc < 2)
|
---|
272 | return VBoxNetAdpInstall();
|
---|
273 | if (argc > 2)
|
---|
274 | {
|
---|
275 | printUsage();
|
---|
276 | return 1;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (!strcmp(argv[1], "i"))
|
---|
280 | return VBoxNetAdpInstall();
|
---|
281 | if (!strcmp(argv[1], "u"))
|
---|
282 | return VBoxNetAdpUninstall();
|
---|
283 | if (!strcmp(argv[1], "a"))
|
---|
284 | return VBoxNetAdpUpdate();
|
---|
285 | if (!strcmp(argv[1], "d"))
|
---|
286 | return VBoxNetAdpDisable();
|
---|
287 | if (!strcmp(argv[1], "e"))
|
---|
288 | return VBoxNetAdpEnable();
|
---|
289 |
|
---|
290 | printUsage();
|
---|
291 | return !strcmp(argv[1], "h");
|
---|
292 | }
|
---|