VirtualBox

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

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

Main/Hostonly: not specify name on Create

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: VBoxManageHostonly.cpp 17679 2009-03-11 11:39: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_IPV4ADDR },
156 { "-netmask", NETMASK, RTGETOPT_REQ_IPV4ADDR },
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 uint8_t uNetmasklengthv6 = 0;
175 const char *pIpv6 = NULL;
176 bool bIp = false;
177 RTNETADDRIPV4 ip;
178 bool bNetmask = false;
179 RTNETADDRIPV4 netmask;
180
181 int c;
182 RTGETOPTUNION ValueUnion;
183 RTGETOPTSTATE GetState;
184 RTGetOptInit(&GetState,
185 a->argc,
186 a->argv,
187 g_aListOptions,
188 RT_ELEMENTS(g_aListOptions),
189 index,
190 0 /* fFlags */);
191 while ((c = RTGetOpt(&GetState, &ValueUnion)))
192 {
193 switch (c)
194 {
195 case DHCP: // -dhcp
196 if (bDhcp)
197 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -dhcp once.");
198 else if(bNetmasklengthv6 || pIpv6 || bIp || bNetmask)
199 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
200 else
201 bDhcp = true;
202 break;
203 case IP:
204 if(bIp)
205 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -ip once.");
206 else if (bDhcp)
207 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
208 else if(bNetmasklengthv6 || pIpv6)
209 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
210 else
211 {
212 bIp = true;
213 ip = ValueUnion.IPv4Addr;
214 }
215 break;
216 case NETMASK:
217 if(bNetmask)
218 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -netmask once.");
219 else if (bDhcp)
220 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
221 else if(bNetmasklengthv6 || pIpv6)
222 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
223 else
224 {
225 bNetmask = true;
226 netmask = ValueUnion.IPv4Addr;
227 }
228 break;
229 case IPV6:
230 if(pIpv6)
231 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -ipv6 once.");
232 else if (bDhcp)
233 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
234 else if(bIp || bNetmask)
235 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
236 else
237 pIpv6 = ValueUnion.psz;
238 break;
239 case NETMASKLENGTHV6:
240 if(bNetmasklengthv6)
241 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -netmasklengthv6 once.");
242 else if (bDhcp)
243 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
244 else if(bIp || bNetmask)
245 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
246 else
247 {
248 bNetmasklengthv6 = true;
249 uNetmasklengthv6 = ValueUnion.u8;
250 }
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(bDhcp)
269 {
270 CHECK_ERROR(hif, EnableDynamicIpConfig ());
271 }
272 else if(bIp)
273 {
274 if(!bNetmask)
275 {
276 netmask.u = 0;
277 }
278
279 CHECK_ERROR(hif, EnableStaticIpConfig (ip.u, netmask.u));
280 }
281 else if(pIpv6)
282 {
283 Bstr ipv6str(pIpv6);
284 CHECK_ERROR(hif, EnableStaticIpConfigV6 (ipv6str, (ULONG)uNetmasklengthv6));
285 }
286 else
287 {
288 return errorSyntax(USAGE_HOSTONLYIFS, "neither -dhcp nor -ip nor -ipv6 was spcfified");
289 }
290
291 return 0;
292}
293
294
295int handleHostonlyIf(HandlerArg *a)
296{
297 int result = 0;
298 if (a->argc < 1)
299 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
300
301 for (int i = 0; i < a->argc; i++)
302 {
303 if (strcmp(a->argv[i], "ipconfig") == 0)
304 {
305 int cProcessed;
306 result = handleIpconfig(a, i+1, &cProcessed);
307 break;
308// if(!rc)
309// i+= cProcessed;
310// else
311// break;
312 }
313#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
314 else if (strcmp(a->argv[i], "create") == 0)
315 {
316 int cProcessed;
317 result = handleCreate(a, i+1, &cProcessed);
318 if(!result)
319 i+= cProcessed;
320 else
321 break;
322 }
323 else if (strcmp(a->argv[i], "remove") == 0)
324 {
325 int cProcessed;
326 result = handleRemove(a, i+1, &cProcessed);
327 if(!result)
328 i+= cProcessed;
329 else
330 break;
331 }
332#endif
333 else
334 {
335 result = errorSyntax(USAGE_HOSTONLYIFS, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
336 break;
337 }
338 }
339
340 return result;
341}
342
343#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