VirtualBox

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

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

VBoxManage: hostonlyif help

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: VBoxManageHostonly.cpp 17549 2009-03-09 06:27:25Z 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.215389.xyz. 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 (name, 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 return 0;
91}
92
93static int handleRemove(HandlerArg *a, int iStart, int *pcProcessed)
94{
95 if (a->argc - iStart < 1)
96 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
97
98 int index = iStart;
99 HRESULT rc;
100
101 Bstr name(a->argv[iStart]);
102 index++;
103
104 ComPtr<IHost> host;
105 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
106
107 ComPtr<IHostNetworkInterface> hif;
108 CHECK_ERROR(host, FindHostNetworkInterfaceByName(name, hif.asOutParam()));
109
110 GUID guid;
111 CHECK_ERROR(hif, COMGETTER(Id)(&guid));
112
113 ComPtr<IProgress> progress;
114 CHECK_ERROR(host, RemoveHostOnlyNetworkInterface (guid, hif.asOutParam(),progress.asOutParam()));
115
116 showProgress(progress);
117
118 HRESULT hr;
119 CHECK_ERROR(progress, COMGETTER(ResultCode) (&hr));
120
121 *pcProcessed = index - iStart;
122
123 if(FAILED(hr))
124 {
125 com::ProgressErrorInfo info(progress);
126 if (info.isBasicAvailable())
127 RTPrintf("Error: failed to remove the host-only adapter. Error message: %lS\n", info.getText().raw());
128 else
129 RTPrintf("Error: failed to remove the host-only adapter. No error message available, HRESULT code: 0x%x\n", hr);
130
131 return 1;
132 }
133
134 return 0;
135}
136#endif
137
138enum enOptionCodes
139{
140 DHCP = 1000,
141 IP,
142 NETMASK,
143 IPV6,
144 NETMASKLENGTHV6
145};
146
147static const RTGETOPTDEF g_aListOptions[]
148 = {
149 { "-dhcp", DHCP, RTGETOPT_REQ_NOTHING },
150 { "-ip", IP, RTGETOPT_REQ_IPV4ADDR },
151 { "-netmask", NETMASK, RTGETOPT_REQ_IPV4ADDR },
152 { "-ipv6", IPV6, RTGETOPT_REQ_STRING },
153 { "-netmasklengthv6", NETMASKLENGTHV6, RTGETOPT_REQ_UINT8 }
154 };
155
156static int handleIpconfig(HandlerArg *a, int iStart, int *pcProcessed)
157{
158 if (a->argc - iStart < 2)
159 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
160
161 int index = iStart;
162 HRESULT rc;
163
164 Bstr name(a->argv[iStart]);
165 index++;
166
167 bool bDhcp = false;
168 bool bNetmasklengthv6 = false;
169 uint8_t uNetmasklengthv6 = 0;
170 const char *pIpv6 = NULL;
171 bool bIp = false;
172 RTNETADDRIPV4 ip;
173 bool bNetmask = false;
174 RTNETADDRIPV4 netmask;
175
176 int c;
177 RTGETOPTUNION ValueUnion;
178 RTGETOPTSTATE GetState;
179 RTGetOptInit(&GetState,
180 a->argc,
181 a->argv,
182 g_aListOptions,
183 RT_ELEMENTS(g_aListOptions),
184 index,
185 0 /* fFlags */);
186 while ((c = RTGetOpt(&GetState, &ValueUnion)))
187 {
188 switch (c)
189 {
190 case DHCP: // -dhcp
191 if (bDhcp)
192 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -dhcp once.");
193 else if(bNetmasklengthv6 || pIpv6 || bIp || bNetmask)
194 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
195 else
196 bDhcp = true;
197 break;
198 case IP:
199 if(bIp)
200 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -ip once.");
201 else if (bDhcp)
202 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
203 else if(bNetmasklengthv6 || pIpv6)
204 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
205 else
206 {
207 bIp = true;
208 ip = ValueUnion.IPv4Addr;
209 }
210 break;
211 case NETMASK:
212 if(bNetmask)
213 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -netmask once.");
214 else if (bDhcp)
215 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
216 else if(bNetmasklengthv6 || pIpv6)
217 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
218 else
219 {
220 bNetmask = true;
221 netmask = ValueUnion.IPv4Addr;
222 }
223 break;
224 case IPV6:
225 if(pIpv6)
226 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -ipv6 once.");
227 else if (bDhcp)
228 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
229 else if(bIp || bNetmask)
230 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
231 else
232 pIpv6 = ValueUnion.psz;
233 break;
234 case NETMASKLENGTHV6:
235 if(bNetmasklengthv6)
236 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify -netmasklengthv6 once.");
237 else if (bDhcp)
238 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use -dhcp with static ip configuration parameters: -ip, -netmask, -ipv6 and -netmasklengthv6.");
239 else if(bIp || bNetmask)
240 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (-ip and -netmask) with ipv6 (-ipv6 and -netmasklengthv6) simultaneously.");
241 else
242 {
243 bNetmasklengthv6 = true;
244 uNetmasklengthv6 = ValueUnion.u8;
245 }
246 break;
247 default:
248 if (c > 0)
249 return errorSyntax(USAGE_HOSTONLYIFS, "missing case: %c\n", c);
250 else if (ValueUnion.pDef)
251 return errorSyntax(USAGE_HOSTONLYIFS, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
252 else
253 return errorSyntax(USAGE_HOSTONLYIFS, "%Rrs", c);
254 }
255 }
256
257 ComPtr<IHost> host;
258 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
259
260 ComPtr<IHostNetworkInterface> hif;
261 CHECK_ERROR(host, FindHostNetworkInterfaceByName(name, hif.asOutParam()));
262
263 if(bDhcp)
264 {
265 CHECK_ERROR(hif, EnableDynamicIpConfig ());
266 }
267 else if(bIp)
268 {
269 if(!bNetmask)
270 {
271 netmask.u = 0;
272 }
273
274 CHECK_ERROR(hif, EnableStaticIpConfig (ip.u, netmask.u));
275 }
276 else if(pIpv6)
277 {
278 Bstr ipv6str(pIpv6);
279 CHECK_ERROR(hif, EnableStaticIpConfigV6 (ipv6str, (ULONG)uNetmasklengthv6));
280 }
281 else
282 {
283 return errorSyntax(USAGE_HOSTONLYIFS, "neither -dhcp nor -ip nor -ipv6 was spcfified");
284 }
285
286 return 0;
287}
288
289
290int handleHostonlyIf(HandlerArg *a)
291{
292 int result = 0;
293 if (a->argc < 1)
294 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
295
296 for (int i = 0; i < a->argc; i++)
297 {
298 if (strcmp(a->argv[i], "ipconfig") == 0)
299 {
300 int cProcessed;
301 result = handleIpconfig(a, i+1, &cProcessed);
302 break;
303// if(!rc)
304// i+= cProcessed;
305// else
306// break;
307 }
308#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
309 else if (strcmp(a->argv[i], "create") == 0)
310 {
311 int cProcessed;
312 result = handleCreate(a, i+1, &cProcessed);
313 if(!result)
314 i+= cProcessed;
315 else
316 break;
317 }
318 else if (strcmp(a->argv[i], "remove") == 0)
319 {
320 int cProcessed;
321 result = handleRemove(a, i+1, &cProcessed);
322 if(!result)
323 i+= cProcessed;
324 else
325 break;
326 }
327#endif
328 else
329 {
330 result = errorSyntax(USAGE_HOSTONLYIFS, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
331 break;
332 }
333 }
334
335 return result;
336}
337
338#endif /* !VBOX_ONLY_DOCS */
Note: See TracBrowser for help on using the repository browser.

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