[17488] | 1 | /* $Id: VBoxManageHostonly.cpp 42551 2012-08-02 16:44:39Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBoxManage - Implementation of hostonlyif command.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[42551] | 7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
[17488] | 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>
|
---|
[20928] | 25 | #include <VBox/com/errorprint.h>
|
---|
[17488] | 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>
|
---|
[18108] | 38 | #include <iprt/ctype.h>
|
---|
[17488] | 39 |
|
---|
| 40 | #include <VBox/log.h>
|
---|
| 41 |
|
---|
| 42 | #include "VBoxManage.h"
|
---|
| 43 |
|
---|
| 44 | #ifndef VBOX_ONLY_DOCS
|
---|
| 45 | using namespace com;
|
---|
| 46 |
|
---|
[35317] | 47 | #if defined(VBOX_WITH_NETFLT) && !defined(RT_OS_SOLARIS)
|
---|
[17488] | 48 | static int handleCreate(HandlerArg *a, int iStart, int *pcProcessed)
|
---|
| 49 | {
|
---|
[17679] | 50 | // if (a->argc - iStart < 1)
|
---|
| 51 | // return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
|
---|
[17488] | 52 |
|
---|
| 53 | int index = iStart;
|
---|
| 54 | HRESULT rc;
|
---|
[17679] | 55 | // Bstr name(a->argv[iStart]);
|
---|
| 56 | // index++;
|
---|
[17488] | 57 |
|
---|
| 58 | ComPtr<IHost> host;
|
---|
[41174] | 59 | CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), 1);
|
---|
[17488] | 60 |
|
---|
| 61 | ComPtr<IHostNetworkInterface> hif;
|
---|
| 62 | ComPtr<IProgress> progress;
|
---|
| 63 |
|
---|
[41174] | 64 | CHECK_ERROR_RET(host, CreateHostOnlyNetworkInterface (hif.asOutParam(), progress.asOutParam()), 1);
|
---|
[17488] | 65 |
|
---|
[24879] | 66 | rc = showProgress(progress);
|
---|
[17488] | 67 | *pcProcessed = index - iStart;
|
---|
[38525] | 68 | CHECK_PROGRESS_ERROR_RET(progress, ("Failed to create the host-only adapter"), 1);
|
---|
[17488] | 69 |
|
---|
[17679] | 70 | Bstr name;
|
---|
| 71 | CHECK_ERROR(hif, COMGETTER(Name) (name.asOutParam()));
|
---|
| 72 |
|
---|
[38735] | 73 | RTPrintf("Interface '%ls' was successfully created\n", name.raw());
|
---|
[17679] | 74 |
|
---|
[17488] | 75 | return 0;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static int handleRemove(HandlerArg *a, int iStart, int *pcProcessed)
|
---|
| 79 | {
|
---|
[41324] | 80 | *pcProcessed = 0;
|
---|
[17488] | 81 | if (a->argc - iStart < 1)
|
---|
[17549] | 82 | return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
|
---|
[17488] | 83 |
|
---|
| 84 | int index = iStart;
|
---|
| 85 | HRESULT rc;
|
---|
| 86 |
|
---|
| 87 | Bstr name(a->argv[iStart]);
|
---|
| 88 | index++;
|
---|
| 89 |
|
---|
| 90 | ComPtr<IHost> host;
|
---|
[35330] | 91 | CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), 1);
|
---|
[17488] | 92 |
|
---|
| 93 | ComPtr<IHostNetworkInterface> hif;
|
---|
[35330] | 94 | CHECK_ERROR_RET(host, FindHostNetworkInterfaceByName(name.raw(), hif.asOutParam()), 1);
|
---|
[17488] | 95 |
|
---|
[19239] | 96 | Bstr guid;
|
---|
[35330] | 97 | CHECK_ERROR_RET(hif, COMGETTER(Id)(guid.asOutParam()), 1);
|
---|
[17488] | 98 |
|
---|
| 99 | ComPtr<IProgress> progress;
|
---|
[35330] | 100 | CHECK_ERROR_RET(host, RemoveHostOnlyNetworkInterface(guid.raw(), progress.asOutParam()), 1);
|
---|
[17488] | 101 |
|
---|
[24879] | 102 | rc = showProgress(progress);
|
---|
[17488] | 103 | *pcProcessed = index - iStart;
|
---|
[38525] | 104 | CHECK_PROGRESS_ERROR_RET(progress, ("Failed to remove the host-only adapter"), 1);
|
---|
[17488] | 105 |
|
---|
| 106 | return 0;
|
---|
| 107 | }
|
---|
| 108 | #endif
|
---|
| 109 |
|
---|
[18108] | 110 | static const RTGETOPTDEF g_aHostOnlyIPOptions[]
|
---|
[17488] | 111 | = {
|
---|
[18108] | 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
|
---|
[17488] | 122 | };
|
---|
| 123 |
|
---|
| 124 | static int handleIpconfig(HandlerArg *a, int iStart, int *pcProcessed)
|
---|
| 125 | {
|
---|
| 126 | if (a->argc - iStart < 2)
|
---|
[17549] | 127 | return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
|
---|
[17488] | 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;
|
---|
[18017] | 137 | uint32_t uNetmasklengthv6 = (uint32_t)-1;
|
---|
[17488] | 138 | const char *pIpv6 = NULL;
|
---|
[18017] | 139 | const char *pIp = NULL;
|
---|
| 140 | const char *pNetmask = NULL;
|
---|
[17488] | 141 |
|
---|
| 142 | int c;
|
---|
| 143 | RTGETOPTUNION ValueUnion;
|
---|
| 144 | RTGETOPTSTATE GetState;
|
---|
| 145 | RTGetOptInit(&GetState,
|
---|
| 146 | a->argc,
|
---|
| 147 | a->argv,
|
---|
[18108] | 148 | g_aHostOnlyIPOptions,
|
---|
| 149 | RT_ELEMENTS(g_aHostOnlyIPOptions),
|
---|
[17488] | 150 | index,
|
---|
[26517] | 151 | RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
[17488] | 152 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
| 153 | {
|
---|
| 154 | switch (c)
|
---|
| 155 | {
|
---|
[18108] | 156 | case 'd': // --dhcp
|
---|
[17488] | 157 | if (bDhcp)
|
---|
[18108] | 158 | return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --dhcp once.");
|
---|
[17488] | 159 | else
|
---|
| 160 | bDhcp = true;
|
---|
| 161 | break;
|
---|
[18108] | 162 | case 'a': // --ip
|
---|
[17759] | 163 | if(pIp)
|
---|
[18108] | 164 | return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --ip once.");
|
---|
[17488] | 165 | else
|
---|
[17759] | 166 | pIp = ValueUnion.psz;
|
---|
[17488] | 167 | break;
|
---|
[18108] | 168 | case 'm': // --netmask
|
---|
[17759] | 169 | if(pNetmask)
|
---|
[18108] | 170 | return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --netmask once.");
|
---|
[17488] | 171 | else
|
---|
[17759] | 172 | pNetmask = ValueUnion.psz;
|
---|
[17488] | 173 | break;
|
---|
[18108] | 174 | case 'b': // --ipv6
|
---|
[17488] | 175 | if(pIpv6)
|
---|
[18108] | 176 | return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --ipv6 once.");
|
---|
[17488] | 177 | else
|
---|
| 178 | pIpv6 = ValueUnion.psz;
|
---|
| 179 | break;
|
---|
[18108] | 180 | case 'l': // --netmasklengthv6
|
---|
[17488] | 181 | if(bNetmasklengthv6)
|
---|
[18108] | 182 | return errorSyntax(USAGE_HOSTONLYIFS, "You can only specify --netmasklengthv6 once.");
|
---|
[17488] | 183 | else
|
---|
| 184 | {
|
---|
| 185 | bNetmasklengthv6 = true;
|
---|
| 186 | uNetmasklengthv6 = ValueUnion.u8;
|
---|
| 187 | }
|
---|
| 188 | break;
|
---|
[17759] | 189 | case VINF_GETOPT_NOT_OPTION:
|
---|
[18108] | 190 | return errorSyntax(USAGE_HOSTONLYIFS, "unhandled parameter: %s", ValueUnion.psz);
|
---|
[17759] | 191 | break;
|
---|
[17488] | 192 | default:
|
---|
| 193 | if (c > 0)
|
---|
[18108] | 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);
|
---|
[17488] | 202 | else if (ValueUnion.pDef)
|
---|
[17549] | 203 | return errorSyntax(USAGE_HOSTONLYIFS, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
[17488] | 204 | else
|
---|
[17549] | 205 | return errorSyntax(USAGE_HOSTONLYIFS, "%Rrs", c);
|
---|
[17488] | 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[18108] | 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 |
|
---|
[17488] | 215 | ComPtr<IHost> host;
|
---|
| 216 | CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
|
---|
| 217 |
|
---|
| 218 | ComPtr<IHostNetworkInterface> hif;
|
---|
[32718] | 219 | CHECK_ERROR(host, FindHostNetworkInterfaceByName(name.raw(),
|
---|
| 220 | hif.asOutParam()));
|
---|
[17488] | 221 |
|
---|
[18017] | 222 | if (FAILED(rc))
|
---|
[30322] | 223 | return errorArgument("Could not find interface '%s'", a->argv[iStart]);
|
---|
[17759] | 224 |
|
---|
[18017] | 225 | if (bDhcp)
|
---|
[17488] | 226 | {
|
---|
[42551] | 227 | CHECK_ERROR(hif, EnableDynamicIPConfig ());
|
---|
[17488] | 228 | }
|
---|
[18017] | 229 | else if (pIp)
|
---|
[17488] | 230 | {
|
---|
[18017] | 231 | if (!pNetmask)
|
---|
[17759] | 232 | pNetmask = "255.255.255.0"; /* ?? */
|
---|
[17488] | 233 |
|
---|
[42551] | 234 | CHECK_ERROR(hif, EnableStaticIPConfig(Bstr(pIp).raw(),
|
---|
[32718] | 235 | Bstr(pNetmask).raw()));
|
---|
[17488] | 236 | }
|
---|
[18017] | 237 | else if (pIpv6)
|
---|
[17488] | 238 | {
|
---|
[18017] | 239 | if (uNetmasklengthv6 == (uint32_t)-1)
|
---|
[17759] | 240 | uNetmasklengthv6 = 64; /* ?? */
|
---|
| 241 |
|
---|
| 242 | BOOL bIpV6Supported;
|
---|
[18017] | 243 | CHECK_ERROR(hif, COMGETTER(IPV6Supported)(&bIpV6Supported));
|
---|
| 244 | if (!bIpV6Supported)
|
---|
[17759] | 245 | {
|
---|
[32701] | 246 | RTMsgError("IPv6 setting is not supported for this adapter");
|
---|
[17759] | 247 | return 1;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 |
|
---|
[17488] | 251 | Bstr ipv6str(pIpv6);
|
---|
[42551] | 252 | CHECK_ERROR(hif, EnableStaticIPConfigV6(ipv6str.raw(),
|
---|
[32718] | 253 | (ULONG)uNetmasklengthv6));
|
---|
[17488] | 254 | }
|
---|
| 255 | else
|
---|
| 256 | {
|
---|
[17549] | 257 | return errorSyntax(USAGE_HOSTONLYIFS, "neither -dhcp nor -ip nor -ipv6 was spcfified");
|
---|
[17488] | 258 | }
|
---|
| 259 |
|
---|
| 260 | return 0;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | int handleHostonlyIf(HandlerArg *a)
|
---|
| 265 | {
|
---|
| 266 | int result = 0;
|
---|
| 267 | if (a->argc < 1)
|
---|
[17549] | 268 | return errorSyntax(USAGE_HOSTONLYIFS, "Not enough parameters");
|
---|
[17488] | 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 | }
|
---|
[35317] | 282 | #if defined(VBOX_WITH_NETFLT) && !defined(RT_OS_SOLARIS)
|
---|
[17488] | 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 | {
|
---|
[31539] | 304 | result = errorSyntax(USAGE_HOSTONLYIFS, "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
|
---|
[17488] | 305 | break;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | return result;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | #endif /* !VBOX_ONLY_DOCS */
|
---|