VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageHostonly.cpp@ 17766

Last change on this file since 17766 was 17759, checked in by vboxsync, 16 years ago

Main/NetIf: IPv4 properties changed from ULONG to wstring, VBoxManage fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: VBoxManageHostonly.cpp 17759 2009-03-12 15:57:18Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of hostonlyif command.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#ifndef VBOX_ONLY_DOCS
26#include <VBox/com/com.h>
27#include <VBox/com/array.h>
28#include <VBox/com/ErrorInfo.h>
29#include <VBox/com/errorprint2.h>
30#include <VBox/com/EventQueue.h>
31
32#include <VBox/com/VirtualBox.h>
33
34#include <vector>
35#include <list>
36#endif /* !VBOX_ONLY_DOCS */
37
38#include <iprt/cidr.h>
39#include <iprt/param.h>
40#include <iprt/path.h>
41#include <iprt/stream.h>
42#include <iprt/string.h>
43#include <iprt/net.h>
44#include <iprt/getopt.h>
45
46#include <VBox/log.h>
47
48#include "VBoxManage.h"
49
50#ifndef VBOX_ONLY_DOCS
51using namespace com;
52
53#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
54static int handleCreate(HandlerArg *a, int iStart, int *pcProcessed)
55{
56// if (a->argc - iStart < 1)
57// return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
58
59 int index = iStart;
60 HRESULT rc;
61// Bstr name(a->argv[iStart]);
62// index++;
63
64 ComPtr<IHost> host;
65 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
66
67 ComPtr<IHostNetworkInterface> hif;
68 ComPtr<IProgress> progress;
69
70 CHECK_ERROR(host, CreateHostOnlyNetworkInterface (hif.asOutParam(), progress.asOutParam()));
71
72 showProgress(progress);
73
74 HRESULT hr;
75 CHECK_ERROR(progress, COMGETTER(ResultCode) (&hr));
76
77 *pcProcessed = index - iStart;
78
79 if(FAILED(hr))
80 {
81 com::ProgressErrorInfo info(progress);
82 if (info.isBasicAvailable())
83 RTPrintf("Error: failed to remove the host-only adapter. Error message: %lS\n", info.getText().raw());
84 else
85 RTPrintf("Error: failed to remove the host-only adapter. No error message available, HRESULT code: 0x%x\n", hr);
86
87 return 1;
88 }
89
90 Bstr name;
91 CHECK_ERROR(hif, COMGETTER(Name) (name.asOutParam()));
92
93 RTPrintf("Interface '%lS' was successfully created\n", name.raw());
94
95 return 0;
96}
97
98static int handleRemove(HandlerArg *a, int iStart, int *pcProcessed)
99{
100 if (a->argc - iStart < 1)
101 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
102
103 int index = iStart;
104 HRESULT rc;
105
106 Bstr name(a->argv[iStart]);
107 index++;
108
109 ComPtr<IHost> host;
110 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
111
112 ComPtr<IHostNetworkInterface> hif;
113 CHECK_ERROR(host, FindHostNetworkInterfaceByName(name, hif.asOutParam()));
114
115 GUID guid;
116 CHECK_ERROR(hif, COMGETTER(Id)(&guid));
117
118 ComPtr<IProgress> progress;
119 CHECK_ERROR(host, RemoveHostOnlyNetworkInterface (guid, hif.asOutParam(),progress.asOutParam()));
120
121 showProgress(progress);
122
123 HRESULT hr;
124 CHECK_ERROR(progress, COMGETTER(ResultCode) (&hr));
125
126 *pcProcessed = index - iStart;
127
128 if(FAILED(hr))
129 {
130 com::ProgressErrorInfo info(progress);
131 if (info.isBasicAvailable())
132 RTPrintf("Error: failed to remove the host-only adapter. Error message: %lS\n", info.getText().raw());
133 else
134 RTPrintf("Error: failed to remove the host-only adapter. No error message available, HRESULT code: 0x%x\n", hr);
135
136 return 1;
137 }
138
139 return 0;
140}
141#endif
142
143enum enOptionCodes
144{
145 DHCP = 1000,
146 IP,
147 NETMASK,
148 IPV6,
149 NETMASKLENGTHV6
150};
151
152static const RTGETOPTDEF g_aListOptions[]
153 = {
154 { "-dhcp", DHCP, RTGETOPT_REQ_NOTHING },
155 { "-ip", IP, RTGETOPT_REQ_STRING },
156 { "-netmask", NETMASK, RTGETOPT_REQ_STRING },
157 { "-ipv6", IPV6, RTGETOPT_REQ_STRING },
158 { "-netmasklengthv6", NETMASKLENGTHV6, RTGETOPT_REQ_UINT8 }
159 };
160
161static int handleIpconfig(HandlerArg *a, int iStart, int *pcProcessed)
162{
163 if (a->argc - iStart < 2)
164 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
165
166 int index = iStart;
167 HRESULT rc;
168
169 Bstr name(a->argv[iStart]);
170 index++;
171
172 bool bDhcp = false;
173 bool bNetmasklengthv6 = false;
174 uint32_t uNetmasklengthv6 = -1;
175 const char *pIpv6 = NULL;
176 const char * pIp = NULL;
177 const char * pNetmask = NULL;
178
179 int c;
180 RTGETOPTUNION ValueUnion;
181 RTGETOPTSTATE GetState;
182 RTGetOptInit(&GetState,
183 a->argc,
184 a->argv,
185 g_aListOptions,
186 RT_ELEMENTS(g_aListOptions),
187 index,
188 0 /* fFlags */);
189 while ((c = RTGetOpt(&GetState, &ValueUnion)))
190 {
191 switch (c)
192 {
193 case DHCP: // -dhcp
194 if (bDhcp)
195 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -dhcp once.");
196 else if(bNetmasklengthv6 || pIpv6 || pIp || pNetmask)
197 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
198 else
199 bDhcp = true;
200 break;
201 case IP:
202 if(pIp)
203 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -ip once.");
204 else if (bDhcp)
205 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
206 else if(bNetmasklengthv6 || pIpv6)
207 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
208 else
209 {
210 pIp = ValueUnion.psz;
211 }
212 break;
213 case NETMASK:
214 if(pNetmask)
215 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -netmask once.");
216 else if (bDhcp)
217 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
218 else if(bNetmasklengthv6 || pIpv6)
219 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
220 else
221 {
222 pNetmask = ValueUnion.psz;
223 }
224 break;
225 case IPV6:
226 if(pIpv6)
227 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -ipv6 once.");
228 else if (bDhcp)
229 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
230 else if(pIp || pNetmask)
231 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
232 else
233 pIpv6 = ValueUnion.psz;
234 break;
235 case NETMASKLENGTHV6:
236 if(bNetmasklengthv6)
237 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -netmasklengthv6 once.");
238 else if (bDhcp)
239 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
240 else if(pIp || pNetmask)
241 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
242 else
243 {
244 bNetmasklengthv6 = true;
245 uNetmasklengthv6 = ValueUnion.u8;
246 }
247 break;
248 case VINF_GETOPT_NOT_OPTION:
249 case VERR_GETOPT_UNKNOWN_OPTION:
250 return errorSyntax(USAGE_HOSTONLYIFS, "Unknown option \"%s\".", ValueUnion.psz);
251 break;
252 default:
253 if (c > 0)
254 return errorSyntax(USAGE_HOSTONLYIFS, "missing case: %c\n", c);
255 else if (ValueUnion.pDef)
256 return errorSyntax(USAGE_HOSTONLYIFS, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
257 else
258 return errorSyntax(USAGE_HOSTONLYIFS, "%Rrs", c);
259 }
260 }
261
262 ComPtr<IHost> host;
263 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
264
265 ComPtr<IHostNetworkInterface> hif;
266 CHECK_ERROR(host, FindHostNetworkInterfaceByName(name, hif.asOutParam()));
267
268 if(FAILED(rc))
269 return errorArgument("could not find interface '%s'", a->argv[iStart]);
270
271 if(bDhcp)
272 {
273 CHECK_ERROR(hif, EnableDynamicIpConfig ());
274 }
275 else if(pIp)
276 {
277 if(!pNetmask)
278 pNetmask = "255.255.255.0"; /* ?? */
279
280 CHECK_ERROR(hif, EnableStaticIpConfig (Bstr(pIp), Bstr(pNetmask)));
281 }
282 else if(pIpv6)
283 {
284 if(uNetmasklengthv6 == -1)
285 uNetmasklengthv6 = 64; /* ?? */
286
287 BOOL bIpV6Supported;
288 CHECK_ERROR(hif, COMGETTER(IPV6Supported) (&bIpV6Supported));
289 if(!bIpV6Supported)
290 {
291 RTPrintf("IPv6 setting is not supported for this adapter\n");
292 return 1;
293 }
294
295
296 Bstr ipv6str(pIpv6);
297 CHECK_ERROR(hif, EnableStaticIpConfigV6 (ipv6str, (ULONG)uNetmasklengthv6));
298 }
299 else
300 {
301 return errorSyntax(USAGE_HOSTONLYIFS, "neither -dhcp nor -ip nor -ipv6 was spcfified");
302 }
303
304 return 0;
305}
306
307
308int handleHostonlyIf(HandlerArg *a)
309{
310 int result = 0;
311 if (a->argc < 1)
312 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
313
314 for (int i = 0; i < a->argc; i++)
315 {
316 if (strcmp(a->argv[i], "ipconfig") == 0)
317 {
318 int cProcessed;
319 result = handleIpconfig(a, i+1, &cProcessed);
320 break;
321// if(!rc)
322// i+= cProcessed;
323// else
324// break;
325 }
326#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
327 else if (strcmp(a->argv[i], "create") == 0)
328 {
329 int cProcessed;
330 result = handleCreate(a, i+1, &cProcessed);
331 if(!result)
332 i+= cProcessed;
333 else
334 break;
335 }
336 else if (strcmp(a->argv[i], "remove") == 0)
337 {
338 int cProcessed;
339 result = handleRemove(a, i+1, &cProcessed);
340 if(!result)
341 i+= cProcessed;
342 else
343 break;
344 }
345#endif
346 else
347 {
348 result = errorSyntax(USAGE_HOSTONLYIFS, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
349 break;
350 }
351 }
352
353 return result;
354}
355
356#endif /* !VBOX_ONLY_DOCS */
Note: See TracBrowser for help on using the repository browser.

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