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