VirtualBox

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

Last change on this file since 38537 was 38525, checked in by vboxsync, 13 years ago

FE/CLI: use CHECK_PROGRESS_ERROR

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: VBoxManageHostonly.cpp 38525 2011-08-25 11:40:58Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of hostonlyif command.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#ifndef VBOX_ONLY_DOCS
22#include <VBox/com/com.h>
23#include <VBox/com/array.h>
24#include <VBox/com/ErrorInfo.h>
25#include <VBox/com/errorprint.h>
26#include <VBox/com/EventQueue.h>
27
28#include <VBox/com/VirtualBox.h>
29#endif /* !VBOX_ONLY_DOCS */
30
31#include <iprt/cidr.h>
32#include <iprt/param.h>
33#include <iprt/path.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/net.h>
37#include <iprt/getopt.h>
38#include <iprt/ctype.h>
39
40#include <VBox/log.h>
41
42#include "VBoxManage.h"
43
44#ifndef VBOX_ONLY_DOCS
45using namespace com;
46
47#if defined(VBOX_WITH_NETFLT) && !defined(RT_OS_SOLARIS)
48static int handleCreate(HandlerArg *a, int iStart, int *pcProcessed)
49{
50// if (a->argc - iStart < 1)
51// return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
52
53 int index = iStart;
54 HRESULT rc;
55// Bstr name(a->argv[iStart]);
56// index++;
57
58 ComPtr<IHost> host;
59 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
60
61 ComPtr<IHostNetworkInterface> hif;
62 ComPtr<IProgress> progress;
63
64 CHECK_ERROR(host, CreateHostOnlyNetworkInterface (hif.asOutParam(), progress.asOutParam()));
65
66 rc = showProgress(progress);
67 *pcProcessed = index - iStart;
68 CHECK_PROGRESS_ERROR_RET(progress, ("Failed to create the host-only adapter"), 1);
69
70 Bstr name;
71 CHECK_ERROR(hif, COMGETTER(Name) (name.asOutParam()));
72
73 RTPrintf("Interface '%lS' was successfully created\n", name.raw());
74
75 return 0;
76}
77
78static int handleRemove(HandlerArg *a, int iStart, int *pcProcessed)
79{
80 if (a->argc - iStart < 1)
81 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
82
83 int index = iStart;
84 HRESULT rc;
85
86 Bstr name(a->argv[iStart]);
87 index++;
88
89 ComPtr<IHost> host;
90 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), 1);
91
92 ComPtr<IHostNetworkInterface> hif;
93 CHECK_ERROR_RET(host, FindHostNetworkInterfaceByName(name.raw(), hif.asOutParam()), 1);
94
95 Bstr guid;
96 CHECK_ERROR_RET(hif, COMGETTER(Id)(guid.asOutParam()), 1);
97
98 ComPtr<IProgress> progress;
99 CHECK_ERROR_RET(host, RemoveHostOnlyNetworkInterface(guid.raw(), progress.asOutParam()), 1);
100
101 rc = showProgress(progress);
102 *pcProcessed = index - iStart;
103 CHECK_PROGRESS_ERROR_RET(progress, ("Failed to remove the host-only adapter"), 1);
104
105 return 0;
106}
107#endif
108
109static const RTGETOPTDEF g_aHostOnlyIPOptions[]
110 = {
111 { "--dhcp", 'd', RTGETOPT_REQ_NOTHING },
112 { "-dhcp", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
113 { "--ip", 'a', RTGETOPT_REQ_STRING },
114 { "-ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
115 { "--netmask", 'm', RTGETOPT_REQ_STRING },
116 { "-netmask", 'm', RTGETOPT_REQ_STRING }, // deprecated
117 { "--ipv6", 'b', RTGETOPT_REQ_STRING },
118 { "-ipv6", 'b', RTGETOPT_REQ_STRING }, // deprecated
119 { "--netmasklengthv6", 'l', RTGETOPT_REQ_UINT8 },
120 { "-netmasklengthv6", 'l', RTGETOPT_REQ_UINT8 } // deprecated
121 };
122
123static int handleIpconfig(HandlerArg *a, int iStart, int *pcProcessed)
124{
125 if (a->argc - iStart < 2)
126 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
127
128 int index = iStart;
129 HRESULT rc;
130
131 Bstr name(a->argv[iStart]);
132 index++;
133
134 bool bDhcp = false;
135 bool bNetmasklengthv6 = false;
136 uint32_t uNetmasklengthv6 = (uint32_t)-1;
137 const char *pIpv6 = NULL;
138 const char *pIp = NULL;
139 const char *pNetmask = NULL;
140
141 int c;
142 RTGETOPTUNION ValueUnion;
143 RTGETOPTSTATE GetState;
144 RTGetOptInit(&GetState,
145 a->argc,
146 a->argv,
147 g_aHostOnlyIPOptions,
148 RT_ELEMENTS(g_aHostOnlyIPOptions),
149 index,
150 RTGETOPTINIT_FLAGS_NO_STD_OPTS);
151 while ((c = RTGetOpt(&GetState, &ValueUnion)))
152 {
153 switch (c)
154 {
155 case 'd': // --dhcp
156 if (bDhcp)
157 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --dhcp once.");
158 else
159 bDhcp = true;
160 break;
161 case 'a': // --ip
162 if(pIp)
163 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --ip once.");
164 else
165 pIp = ValueUnion.psz;
166 break;
167 case 'm': // --netmask
168 if(pNetmask)
169 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --netmask once.");
170 else
171 pNetmask = ValueUnion.psz;
172 break;
173 case 'b': // --ipv6
174 if(pIpv6)
175 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --ipv6 once.");
176 else
177 pIpv6 = ValueUnion.psz;
178 break;
179 case 'l': // --netmasklengthv6
180 if(bNetmasklengthv6)
181 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --netmasklengthv6 once.");
182 else
183 {
184 bNetmasklengthv6 = true;
185 uNetmasklengthv6 = ValueUnion.u8;
186 }
187 break;
188 case VINF_GETOPT_NOT_OPTION:
189 return errorSyntax(USAGE_HOSTONLYIFS, "unhandled parameter: %s", ValueUnion.psz);
190 break;
191 default:
192 if (c > 0)
193 {
194 if (RT_C_IS_GRAPH(c))
195 return errorSyntax(USAGE_HOSTONLYIFS, "unhandled option: -%c", c);
196 else
197 return errorSyntax(USAGE_HOSTONLYIFS, "unhandled option: %i", c);
198 }
199 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
200 return errorSyntax(USAGE_HOSTONLYIFS, "unknown option: %s", ValueUnion.psz);
201 else if (ValueUnion.pDef)
202 return errorSyntax(USAGE_HOSTONLYIFS, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
203 else
204 return errorSyntax(USAGE_HOSTONLYIFS, "%Rrs", c);
205 }
206 }
207
208 /* parameter sanity check */
209 if (bDhcp && (bNetmasklengthv6 || pIpv6 || pIp || pNetmask))
210 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use --dhcp with static ip configuration parameters: --ip, --netmask, --ipv6 and --netmasklengthv6.");
211 if((pIp || pNetmask) && (bNetmasklengthv6 || pIpv6))
212 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (--ip and --netmask) with ipv6 (--ipv6 and --netmasklengthv6) simultaneously.");
213
214 ComPtr<IHost> host;
215 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
216
217 ComPtr<IHostNetworkInterface> hif;
218 CHECK_ERROR(host, FindHostNetworkInterfaceByName(name.raw(),
219 hif.asOutParam()));
220
221 if (FAILED(rc))
222 return errorArgument("Could not find interface '%s'", a->argv[iStart]);
223
224 if (bDhcp)
225 {
226 CHECK_ERROR(hif, EnableDynamicIpConfig ());
227 }
228 else if (pIp)
229 {
230 if (!pNetmask)
231 pNetmask = "255.255.255.0"; /* ?? */
232
233 CHECK_ERROR(hif, EnableStaticIpConfig(Bstr(pIp).raw(),
234 Bstr(pNetmask).raw()));
235 }
236 else if (pIpv6)
237 {
238 if (uNetmasklengthv6 == (uint32_t)-1)
239 uNetmasklengthv6 = 64; /* ?? */
240
241 BOOL bIpV6Supported;
242 CHECK_ERROR(hif, COMGETTER(IPV6Supported)(&bIpV6Supported));
243 if (!bIpV6Supported)
244 {
245 RTMsgError("IPv6 setting is not supported for this adapter");
246 return 1;
247 }
248
249
250 Bstr ipv6str(pIpv6);
251 CHECK_ERROR(hif, EnableStaticIpConfigV6(ipv6str.raw(),
252 (ULONG)uNetmasklengthv6));
253 }
254 else
255 {
256 return errorSyntax(USAGE_HOSTONLYIFS, "neither -dhcp nor -ip nor -ipv6 was spcfified");
257 }
258
259 return 0;
260}
261
262
263int handleHostonlyIf(HandlerArg *a)
264{
265 int result = 0;
266 if (a->argc < 1)
267 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
268
269 for (int i = 0; i < a->argc; i++)
270 {
271 if (strcmp(a->argv[i], "ipconfig") == 0)
272 {
273 int cProcessed;
274 result = handleIpconfig(a, i+1, &cProcessed);
275 break;
276// if(!rc)
277// i+= cProcessed;
278// else
279// break;
280 }
281#if defined(VBOX_WITH_NETFLT) && !defined(RT_OS_SOLARIS)
282 else if (strcmp(a->argv[i], "create") == 0)
283 {
284 int cProcessed;
285 result = handleCreate(a, i+1, &cProcessed);
286 if(!result)
287 i+= cProcessed;
288 else
289 break;
290 }
291 else if (strcmp(a->argv[i], "remove") == 0)
292 {
293 int cProcessed;
294 result = handleRemove(a, i+1, &cProcessed);
295 if(!result)
296 i+= cProcessed;
297 else
298 break;
299 }
300#endif
301 else
302 {
303 result = errorSyntax(USAGE_HOSTONLYIFS, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
304 break;
305 }
306 }
307
308 return result;
309}
310
311#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