VirtualBox

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

Last change on this file since 29438 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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