VirtualBox

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

Last change on this file since 56339 was 56118, checked in by vboxsync, 10 years ago

VBoxManage: A quick command handler return-code cleanup that turned out to be rather tedious.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: VBoxManageHostonly.cpp 56118 2015-05-27 19:49:50Z 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 RTEXITCODE handleCreate(HandlerArg *a)
47{
48 /*
49 * Parse input.
50 */
51 RTGETOPTUNION ValueUnion;
52 RTGETOPTSTATE GetState;
53 RTGetOptInit(&GetState, a->argc, a->argv, NULL, 0, 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
54 int ch = RTGetOpt(&GetState, &ValueUnion);
55 if (ch != 0)
56 return errorGetOpt(USAGE_HOSTONLYIFS, ch, &ValueUnion);
57
58 /*
59 * Do the work.
60 */
61 ComPtr<IHost> host;
62 CHECK_ERROR2I_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
63
64 ComPtr<IHostNetworkInterface> hif;
65 ComPtr<IProgress> progress;
66
67 CHECK_ERROR2I_RET(host, CreateHostOnlyNetworkInterface(hif.asOutParam(), progress.asOutParam()), RTEXITCODE_FAILURE);
68
69 HRESULT hrc = showProgress(progress);
70 CHECK_PROGRESS_ERROR_RET(progress, ("Failed to create the host-only adapter"), RTEXITCODE_FAILURE);
71
72 Bstr bstrName;
73 CHECK_ERROR2I(hif, COMGETTER(Name)(bstrName.asOutParam()));
74
75 RTPrintf("Interface '%ls' was successfully created\n", bstrName.raw());
76
77 return RTEXITCODE_SUCCESS;
78}
79
80static RTEXITCODE handleRemove(HandlerArg *a)
81{
82 /*
83 * Parse input.
84 */
85 const char *pszName = NULL;
86 int ch;
87 RTGETOPTUNION ValueUnion;
88 RTGETOPTSTATE GetState;
89 RTGetOptInit(&GetState, a->argc, a->argv, NULL, 0, 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
90 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
91 switch (ch)
92 {
93 case VINF_GETOPT_NOT_OPTION:
94 if (pszName)
95 return errorSyntax(USAGE_HOSTONLYIFS, "Only one interface name can be specified");
96 pszName = ValueUnion.psz;
97 break;
98
99 default:
100 return errorGetOpt(USAGE_HOSTONLYIFS, ch, &ValueUnion);
101 }
102 if (!pszName)
103 return errorSyntax(USAGE_HOSTONLYIFS, "No interface name was specified");
104
105 /*
106 * Do the work.
107 */
108 ComPtr<IHost> host;
109 CHECK_ERROR2I_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
110
111 ComPtr<IHostNetworkInterface> hif;
112 CHECK_ERROR2I_RET(host, FindHostNetworkInterfaceByName(Bstr(pszName).raw(), hif.asOutParam()), RTEXITCODE_FAILURE);
113
114 Bstr guid;
115 CHECK_ERROR2I_RET(hif, COMGETTER(Id)(guid.asOutParam()), RTEXITCODE_FAILURE);
116
117 ComPtr<IProgress> progress;
118 CHECK_ERROR2I_RET(host, RemoveHostOnlyNetworkInterface(guid.raw(), progress.asOutParam()), RTEXITCODE_FAILURE);
119
120 HRESULT hrc = showProgress(progress);
121 CHECK_PROGRESS_ERROR_RET(progress, ("Failed to remove the host-only adapter"), RTEXITCODE_FAILURE);
122
123 return RTEXITCODE_SUCCESS;
124}
125#endif
126
127static const RTGETOPTDEF g_aHostOnlyIPOptions[]
128 = {
129 { "--dhcp", 'd', RTGETOPT_REQ_NOTHING },
130 { "-dhcp", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
131 { "--ip", 'a', RTGETOPT_REQ_STRING },
132 { "-ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
133 { "--netmask", 'm', RTGETOPT_REQ_STRING },
134 { "-netmask", 'm', RTGETOPT_REQ_STRING }, // deprecated
135 { "--ipv6", 'b', RTGETOPT_REQ_STRING },
136 { "-ipv6", 'b', RTGETOPT_REQ_STRING }, // deprecated
137 { "--netmasklengthv6", 'l', RTGETOPT_REQ_UINT8 },
138 { "-netmasklengthv6", 'l', RTGETOPT_REQ_UINT8 } // deprecated
139 };
140
141static RTEXITCODE handleIpConfig(HandlerArg *a)
142{
143 bool fDhcp = false;
144 bool fNetmasklengthv6 = false;
145 uint32_t uNetmasklengthv6 = UINT32_MAX;
146 const char *pszIpv6 = NULL;
147 const char *pszIp = NULL;
148 const char *pszNetmask = NULL;
149 const char *pszName = NULL;
150
151 int c;
152 RTGETOPTUNION ValueUnion;
153 RTGETOPTSTATE GetState;
154 RTGetOptInit(&GetState, a->argc, a->argv, g_aHostOnlyIPOptions, RT_ELEMENTS(g_aHostOnlyIPOptions),
155 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
156 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
157 {
158 switch (c)
159 {
160 case 'd': // --dhcp
161 fDhcp = true;
162 break;
163 case 'a': // --ip
164 if (pszIp)
165 RTMsgWarning("The --ip option is specified more than once");
166 pszIp = ValueUnion.psz;
167 break;
168 case 'm': // --netmask
169 if (pszNetmask)
170 RTMsgWarning("The --netmask option is specified more than once");
171 pszNetmask = ValueUnion.psz;
172 break;
173 case 'b': // --ipv6
174 if (pszIpv6)
175 RTMsgWarning("The --ipv6 option is specified more than once");
176 pszIpv6 = ValueUnion.psz;
177 break;
178 case 'l': // --netmasklengthv6
179 if (fNetmasklengthv6)
180 RTMsgWarning("The --netmasklengthv6 option is specified more than once");
181 fNetmasklengthv6 = true;
182 uNetmasklengthv6 = ValueUnion.u8;
183 break;
184 case VINF_GETOPT_NOT_OPTION:
185 if (pszName)
186 return errorSyntax(USAGE_HOSTONLYIFS, "Only one interface name can be specified");
187 pszName = ValueUnion.psz;
188 break;
189 default:
190 return errorGetOpt(USAGE_HOSTONLYIFS, c, &ValueUnion);
191 }
192 }
193
194 /* parameter sanity check */
195 if (fDhcp && (fNetmasklengthv6 || pszIpv6 || pszIp || pszNetmask))
196 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use --dhcp with static ip configuration parameters: --ip, --netmask, --ipv6 and --netmasklengthv6.");
197 if ((pszIp || pszNetmask) && (fNetmasklengthv6 || pszIpv6))
198 return errorSyntax(USAGE_HOSTONLYIFS, "You can not use ipv4 configuration (--ip and --netmask) with ipv6 (--ipv6 and --netmasklengthv6) simultaneously.");
199
200 ComPtr<IHost> host;
201 CHECK_ERROR2I_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
202
203 ComPtr<IHostNetworkInterface> hif;
204 CHECK_ERROR2I_RET(host, FindHostNetworkInterfaceByName(Bstr(pszName).raw(), hif.asOutParam()), RTEXITCODE_FAILURE);
205 if (hif.isNull())
206 return errorArgument("Could not find interface '%s'", pszName);
207
208 if (fDhcp)
209 CHECK_ERROR2I_RET(hif, EnableDynamicIPConfig(), RTEXITCODE_FAILURE);
210 else if (pszIp)
211 {
212 if (!pszNetmask)
213 pszNetmask = "255.255.255.0"; /* ?? */
214 CHECK_ERROR2I_RET(hif, EnableStaticIPConfig(Bstr(pszIp).raw(), Bstr(pszNetmask).raw()), RTEXITCODE_FAILURE);
215 }
216 else if (pszIpv6)
217 {
218 BOOL fIpV6Supported;
219 CHECK_ERROR2I_RET(hif, COMGETTER(IPV6Supported)(&fIpV6Supported), RTEXITCODE_FAILURE);
220 if (!fIpV6Supported)
221 {
222 RTMsgError("IPv6 setting is not supported for this adapter");
223 return RTEXITCODE_FAILURE;
224 }
225
226 if (uNetmasklengthv6 == UINT32_MAX)
227 uNetmasklengthv6 = 64; /* ?? */
228 CHECK_ERROR2I_RET(hif, EnableStaticIPConfigV6(Bstr(pszIpv6).raw(), (ULONG)uNetmasklengthv6), RTEXITCODE_FAILURE);
229 }
230 else
231 return errorSyntax(USAGE_HOSTONLYIFS, "Neither -dhcp nor -ip nor -ipv6 was specfified");
232
233 return RTEXITCODE_SUCCESS;
234}
235
236
237RTEXITCODE handleHostonlyIf(HandlerArg *a)
238{
239 if (a->argc < 1)
240 return errorSyntax(USAGE_HOSTONLYIFS, "No sub-command specified");
241
242 RTEXITCODE rcExit;
243 if (!strcmp(a->argv[0], "ipconfig"))
244 rcExit = handleIpConfig(a);
245#if defined(VBOX_WITH_NETFLT) && !defined(RT_OS_SOLARIS)
246 else if (!strcmp(a->argv[0], "create"))
247 rcExit = handleCreate(a);
248 else if (!strcmp(a->argv[0], "remove"))
249 rcExit = handleRemove(a);
250#endif
251 else
252 rcExit = errorSyntax(USAGE_HOSTONLYIFS, "Unknown sub-command '%s'", a->argv[0]);
253 return rcExit;
254}
255
256#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