VirtualBox

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

Last change on this file since 41309 was 41174, checked in by vboxsync, 13 years ago

Main/Network: Pass error messages from VBoxNetAdpCtl all the way up to frontends.

  • 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 41174 2012-05-04 17:02:07Z 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_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), 1);
60
61 ComPtr<IHostNetworkInterface> hif;
62 ComPtr<IProgress> progress;
63
64 CHECK_ERROR_RET(host, CreateHostOnlyNetworkInterface (hif.asOutParam(), progress.asOutParam()), 1);
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 {
82 *pcProcessed = 0;
83 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
84 }
85
86 int index = iStart;
87 HRESULT rc;
88
89 Bstr name(a->argv[iStart]);
90 index++;
91
92 ComPtr<IHost> host;
93 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), 1);
94
95 ComPtr<IHostNetworkInterface> hif;
96 CHECK_ERROR_RET(host, FindHostNetworkInterfaceByName(name.raw(), hif.asOutParam()), 1);
97
98 Bstr guid;
99 CHECK_ERROR_RET(hif, COMGETTER(Id)(guid.asOutParam()), 1);
100
101 ComPtr<IProgress> progress;
102 CHECK_ERROR_RET(host, RemoveHostOnlyNetworkInterface(guid.raw(), progress.asOutParam()), 1);
103
104 rc = showProgress(progress);
105 *pcProcessed = index - iStart;
106 CHECK_PROGRESS_ERROR_RET(progress, ("Failed to remove the host-only adapter"), 1);
107
108 return 0;
109}
110#endif
111
112static const RTGETOPTDEF g_aHostOnlyIPOptions[]
113 = {
114 { "--dhcp", 'd', RTGETOPT_REQ_NOTHING },
115 { "-dhcp", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
116 { "--ip", 'a', RTGETOPT_REQ_STRING },
117 { "-ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
118 { "--netmask", 'm', RTGETOPT_REQ_STRING },
119 { "-netmask", 'm', RTGETOPT_REQ_STRING }, // deprecated
120 { "--ipv6", 'b', RTGETOPT_REQ_STRING },
121 { "-ipv6", 'b', RTGETOPT_REQ_STRING }, // deprecated
122 { "--netmasklengthv6", 'l', RTGETOPT_REQ_UINT8 },
123 { "-netmasklengthv6", 'l', RTGETOPT_REQ_UINT8 } // deprecated
124 };
125
126static int handleIpconfig(HandlerArg *a, int iStart, int *pcProcessed)
127{
128 if (a->argc - iStart < 2)
129 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
130
131 int index = iStart;
132 HRESULT rc;
133
134 Bstr name(a->argv[iStart]);
135 index++;
136
137 bool bDhcp = false;
138 bool bNetmasklengthv6 = false;
139 uint32_t uNetmasklengthv6 = (uint32_t)-1;
140 const char *pIpv6 = NULL;
141 const char *pIp = NULL;
142 const char *pNetmask = NULL;
143
144 int c;
145 RTGETOPTUNION ValueUnion;
146 RTGETOPTSTATE GetState;
147 RTGetOptInit(&GetState,
148 a->argc,
149 a->argv,
150 g_aHostOnlyIPOptions,
151 RT_ELEMENTS(g_aHostOnlyIPOptions),
152 index,
153 RTGETOPTINIT_FLAGS_NO_STD_OPTS);
154 while ((c = RTGetOpt(&GetState, &ValueUnion)))
155 {
156 switch (c)
157 {
158 case 'd': // --dhcp
159 if (bDhcp)
160 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --dhcp once.");
161 else
162 bDhcp = true;
163 break;
164 case 'a': // --ip
165 if(pIp)
166 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --ip once.");
167 else
168 pIp = ValueUnion.psz;
169 break;
170 case 'm': // --netmask
171 if(pNetmask)
172 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --netmask once.");
173 else
174 pNetmask = ValueUnion.psz;
175 break;
176 case 'b': // --ipv6
177 if(pIpv6)
178 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --ipv6 once.");
179 else
180 pIpv6 = ValueUnion.psz;
181 break;
182 case 'l': // --netmasklengthv6
183 if(bNetmasklengthv6)
184 return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --netmasklengthv6 once.");
185 else
186 {
187 bNetmasklengthv6 = true;
188 uNetmasklengthv6 = ValueUnion.u8;
189 }
190 break;
191 case VINF_GETOPT_NOT_OPTION:
192 return errorSyntax(USAGE_HOSTONLYIFS, "unhandled parameter: %s", ValueUnion.psz);
193 break;
194 default:
195 if (c > 0)
196 {
197 if (RT_C_IS_GRAPH(c))
198 return errorSyntax(USAGE_HOSTONLYIFS, "unhandled option: -%c", c);
199 else
200 return errorSyntax(USAGE_HOSTONLYIFS, "unhandled option: %i", c);
201 }
202 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
203 return errorSyntax(USAGE_HOSTONLYIFS, "unknown option: %s", ValueUnion.psz);
204 else if (ValueUnion.pDef)
205 return errorSyntax(USAGE_HOSTONLYIFS, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
206 else
207 return errorSyntax(USAGE_HOSTONLYIFS, "%Rrs", c);
208 }
209 }
210
211 /* parameter sanity check */
212 if (bDhcp && (bNetmasklengthv6 || pIpv6 || pIp || pNetmask))
213 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use --dhcp with static ip configuration parameters: --ip, --netmask, --ipv6 and --netmasklengthv6.");
214 if((pIp || pNetmask) && (bNetmasklengthv6 || pIpv6))
215 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (--ip and --netmask) with ipv6 (--ipv6 and --netmasklengthv6) simultaneously.");
216
217 ComPtr<IHost> host;
218 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
219
220 ComPtr<IHostNetworkInterface> hif;
221 CHECK_ERROR(host, FindHostNetworkInterfaceByName(name.raw(),
222 hif.asOutParam()));
223
224 if (FAILED(rc))
225 return errorArgument("Could not find interface '%s'", a->argv[iStart]);
226
227 if (bDhcp)
228 {
229 CHECK_ERROR(hif, EnableDynamicIpConfig ());
230 }
231 else if (pIp)
232 {
233 if (!pNetmask)
234 pNetmask = "255.255.255.0"; /* ?? */
235
236 CHECK_ERROR(hif, EnableStaticIpConfig(Bstr(pIp).raw(),
237 Bstr(pNetmask).raw()));
238 }
239 else if (pIpv6)
240 {
241 if (uNetmasklengthv6 == (uint32_t)-1)
242 uNetmasklengthv6 = 64; /* ?? */
243
244 BOOL bIpV6Supported;
245 CHECK_ERROR(hif, COMGETTER(IPV6Supported)(&bIpV6Supported));
246 if (!bIpV6Supported)
247 {
248 RTMsgError("IPv6 setting is not supported for this adapter");
249 return 1;
250 }
251
252
253 Bstr ipv6str(pIpv6);
254 CHECK_ERROR(hif, EnableStaticIpConfigV6(ipv6str.raw(),
255 (ULONG)uNetmasklengthv6));
256 }
257 else
258 {
259 return errorSyntax(USAGE_HOSTONLYIFS, "neither -dhcp nor -ip nor -ipv6 was spcfified");
260 }
261
262 return 0;
263}
264
265
266int handleHostonlyIf(HandlerArg *a)
267{
268 int result = 0;
269 if (a->argc < 1)
270 return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
271
272 for (int i = 0; i < a->argc; i++)
273 {
274 if (strcmp(a->argv[i], "ipconfig") == 0)
275 {
276 int cProcessed;
277 result = handleIpconfig(a, i+1, &cProcessed);
278 break;
279// if(!rc)
280// i+= cProcessed;
281// else
282// break;
283 }
284#if defined(VBOX_WITH_NETFLT) && !defined(RT_OS_SOLARIS)
285 else if (strcmp(a->argv[i], "create") == 0)
286 {
287 int cProcessed;
288 result = handleCreate(a, i+1, &cProcessed);
289 if(!result)
290 i+= cProcessed;
291 else
292 break;
293 }
294 else if (strcmp(a->argv[i], "remove") == 0)
295 {
296 int cProcessed;
297 result = handleRemove(a, i+1, &cProcessed);
298 if(!result)
299 i+= cProcessed;
300 else
301 break;
302 }
303#endif
304 else
305 {
306 result = errorSyntax(USAGE_HOSTONLYIFS, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
307 break;
308 }
309 }
310
311 return result;
312}
313
314#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