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