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