VirtualBox

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

Last change on this file since 48265 was 46658, checked in by vboxsync, 11 years ago

include VBox/com/EventQueue only if necessary

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