VirtualBox

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

Last change on this file since 46224 was 42551, checked in by vboxsync, 12 years ago

Main: big API naming cleanup, use all caps acronyms everywhere, including SDK docs
Frontends/VBoxManage: implement guestcontrol execute for new API, disabled by default

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