[45156] | 1 | /* $Id: VBoxManageNATNetwork.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBoxManage - Implementation of NAT Network command command.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[69500] | 7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
[45156] | 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 |
|
---|
[57358] | 18 |
|
---|
| 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[45156] | 22 | #ifndef VBOX_ONLY_DOCS
|
---|
[45559] | 23 |
|
---|
[45156] | 24 | #include <VBox/com/com.h>
|
---|
| 25 | #include <VBox/com/array.h>
|
---|
| 26 | #include <VBox/com/ErrorInfo.h>
|
---|
| 27 | #include <VBox/com/errorprint.h>
|
---|
| 28 | #include <VBox/com/VirtualBox.h>
|
---|
| 29 | #endif /* !VBOX_ONLY_DOCS */
|
---|
| 30 |
|
---|
[47704] | 31 | #ifndef RT_OS_WINDOWS
|
---|
| 32 | # include <netinet/in.h>
|
---|
[48363] | 33 | #else
|
---|
| 34 | /* from <ws2ipdef.h> */
|
---|
| 35 | # define INET6_ADDRSTRLEN 65
|
---|
[47704] | 36 | #endif
|
---|
[45559] | 37 |
|
---|
| 38 | #define IPv6
|
---|
| 39 |
|
---|
| 40 | #include <iprt/cdefs.h>
|
---|
[45156] | 41 | #include <iprt/cidr.h>
|
---|
| 42 | #include <iprt/param.h>
|
---|
| 43 | #include <iprt/path.h>
|
---|
| 44 | #include <iprt/stream.h>
|
---|
| 45 | #include <iprt/string.h>
|
---|
| 46 | #include <iprt/net.h>
|
---|
| 47 | #include <iprt/getopt.h>
|
---|
| 48 | #include <iprt/ctype.h>
|
---|
| 49 |
|
---|
| 50 | #include <VBox/log.h>
|
---|
| 51 |
|
---|
[45559] | 52 | #include <vector>
|
---|
[48096] | 53 | #include <string>
|
---|
[45559] | 54 |
|
---|
[45156] | 55 | #include "VBoxManage.h"
|
---|
[45559] | 56 | #include "VBoxPortForwardString.h"
|
---|
[45156] | 57 |
|
---|
| 58 | #ifndef VBOX_ONLY_DOCS
|
---|
[45559] | 59 |
|
---|
[45156] | 60 | using namespace com;
|
---|
| 61 |
|
---|
[56118] | 62 | typedef enum
|
---|
[45156] | 63 | {
|
---|
| 64 | OP_ADD = 1000,
|
---|
| 65 | OP_REMOVE,
|
---|
| 66 | OP_MODIFY,
|
---|
| 67 | OP_START,
|
---|
| 68 | OP_STOP
|
---|
| 69 | } OPCODE;
|
---|
| 70 |
|
---|
[45559] | 71 | typedef struct PFNAME2DELETE
|
---|
| 72 | {
|
---|
[49114] | 73 | char szName[PF_NAMELEN];
|
---|
[45559] | 74 | bool fIPv6;
|
---|
| 75 | } PFNAME2DELETE, *PPFNAME2DELETE;
|
---|
| 76 |
|
---|
| 77 | typedef std::vector<PFNAME2DELETE> VPF2DELETE;
|
---|
| 78 | typedef VPF2DELETE::const_iterator VPF2DELETEITERATOR;
|
---|
| 79 |
|
---|
| 80 | typedef std::vector<PORTFORWARDRULE> VPF2ADD;
|
---|
| 81 | typedef VPF2ADD::const_iterator VPF2ADDITERATOR;
|
---|
| 82 |
|
---|
[48096] | 83 | typedef std::vector<std::string> LOOPBACK2DELETEADD;
|
---|
| 84 | typedef LOOPBACK2DELETEADD::iterator LOOPBACK2DELETEADDITERATOR;
|
---|
[45559] | 85 |
|
---|
[60501] | 86 | static HRESULT printNATNetwork(const ComPtr<INATNetwork> &pNATNet)
|
---|
| 87 | {
|
---|
| 88 | HRESULT rc;
|
---|
| 89 |
|
---|
| 90 | do
|
---|
| 91 | {
|
---|
| 92 | Bstr strVal;
|
---|
| 93 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(NetworkName)(strVal.asOutParam()));
|
---|
| 94 | RTPrintf("Name: %ls\n", strVal.raw());
|
---|
| 95 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(Network)(strVal.asOutParam()));
|
---|
| 96 | RTPrintf("Network: %ls\n", strVal.raw());
|
---|
| 97 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(Gateway)(strVal.asOutParam()));
|
---|
| 98 | RTPrintf("Gateway: %ls\n", strVal.raw());
|
---|
| 99 | BOOL fVal;
|
---|
| 100 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(IPv6Enabled)(&fVal));
|
---|
| 101 | RTPrintf("IPv6: %s\n", fVal ? "Yes" : "No");
|
---|
| 102 | if (fVal)
|
---|
| 103 | {
|
---|
| 104 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(IPv6Prefix)(strVal.asOutParam()));
|
---|
| 105 | RTPrintf("IPv6 Prefix: %s\n", strVal.raw());
|
---|
| 106 | }
|
---|
| 107 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(Enabled)(&fVal));
|
---|
| 108 | RTPrintf("Enabled: %s\n", fVal ? "Yes" : "No");
|
---|
| 109 | /** @todo Add more information here. */
|
---|
| 110 | RTPrintf("\n");
|
---|
| 111 |
|
---|
| 112 | } while (0);
|
---|
| 113 |
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[60508] | 117 | static RTEXITCODE handleNATList(HandlerArg *a)
|
---|
[60501] | 118 | {
|
---|
| 119 | HRESULT rc;
|
---|
| 120 |
|
---|
| 121 | RTPrintf("NAT Networks:\n\n");
|
---|
| 122 |
|
---|
| 123 | const char *pszFilter = NULL;
|
---|
| 124 | if (a->argc > 1)
|
---|
| 125 | pszFilter = a->argv[1];
|
---|
| 126 |
|
---|
| 127 | size_t cFound = 0;
|
---|
| 128 |
|
---|
| 129 | com::SafeIfaceArray<INATNetwork> arrNetNets;
|
---|
| 130 | CHECK_ERROR(a->virtualBox, COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(arrNetNets)));
|
---|
| 131 | for (size_t i = 0; i < arrNetNets.size(); ++i)
|
---|
| 132 | {
|
---|
| 133 | ComPtr<INATNetwork> pNATNet = arrNetNets[i];
|
---|
| 134 |
|
---|
| 135 | if (pszFilter)
|
---|
| 136 | {
|
---|
| 137 | Bstr strVal;
|
---|
| 138 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(NetworkName)(strVal.asOutParam()));
|
---|
| 139 |
|
---|
| 140 | Utf8Str strValUTF8 = Utf8Str(strVal);
|
---|
| 141 | if (!RTStrSimplePatternMatch(pszFilter, strValUTF8.c_str()))
|
---|
| 142 | continue;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (i > 0)
|
---|
| 146 | RTPrintf("\n");
|
---|
| 147 | rc = printNATNetwork(pNATNet);
|
---|
| 148 | if (FAILED(rc))
|
---|
| 149 | break;
|
---|
| 150 |
|
---|
| 151 | cFound++;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | if (SUCCEEDED(rc))
|
---|
| 155 | RTPrintf("%zu %s found\n", cFound, cFound == 1 ? "network" : "networks");
|
---|
| 156 |
|
---|
| 157 | return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[56118] | 160 | static RTEXITCODE handleOp(HandlerArg *a, OPCODE enmCode)
|
---|
[45156] | 161 | {
|
---|
[56118] | 162 | if (a->argc - 1 <= 1)
|
---|
[45156] | 163 | return errorSyntax(USAGE_NATNETWORK, "Not enough parameters");
|
---|
| 164 |
|
---|
| 165 | const char *pNetName = NULL;
|
---|
| 166 | const char *pNetworkCidr = NULL;
|
---|
| 167 | int enable = -1;
|
---|
| 168 | int dhcp = -1;
|
---|
| 169 | int ipv6 = -1;
|
---|
| 170 |
|
---|
[45559] | 171 | VPF2DELETE vPfName2Delete;
|
---|
| 172 | VPF2ADD vPf2Add;
|
---|
| 173 |
|
---|
[48096] | 174 | LOOPBACK2DELETEADD vLoopback2Delete;
|
---|
| 175 | LOOPBACK2DELETEADD vLoopback2Add;
|
---|
| 176 |
|
---|
| 177 | LONG loopback6Offset = 0; /* ignore me */
|
---|
| 178 |
|
---|
[56118] | 179 | static const RTGETOPTDEF g_aNATNetworkIPOptions[] =
|
---|
| 180 | {
|
---|
| 181 | { "--netname", 't', RTGETOPT_REQ_STRING },
|
---|
| 182 | { "--network", 'n', RTGETOPT_REQ_STRING },
|
---|
| 183 | { "--dhcp", 'h', RTGETOPT_REQ_BOOL },
|
---|
| 184 | { "--ipv6", '6', RTGETOPT_REQ_BOOL },
|
---|
| 185 | { "--enable", 'e', RTGETOPT_REQ_NOTHING },
|
---|
| 186 | { "--disable", 'd', RTGETOPT_REQ_NOTHING },
|
---|
| 187 | { "--port-forward-4", 'p', RTGETOPT_REQ_STRING },
|
---|
| 188 | { "--port-forward-6", 'P', RTGETOPT_REQ_STRING },
|
---|
| 189 | { "--loopback-4", 'l', RTGETOPT_REQ_STRING },
|
---|
| 190 | { "--loopback-6", 'L', RTGETOPT_REQ_STRING },
|
---|
| 191 | };
|
---|
| 192 |
|
---|
[45156] | 193 | int c;
|
---|
| 194 | RTGETOPTUNION ValueUnion;
|
---|
| 195 | RTGETOPTSTATE GetState;
|
---|
[56118] | 196 | RTGetOptInit(&GetState, a->argc, a->argv, g_aNATNetworkIPOptions,
|
---|
[45156] | 197 | enmCode != OP_REMOVE ? RT_ELEMENTS(g_aNATNetworkIPOptions) : 4, /* we use only --netname and --ifname for remove*/
|
---|
[56118] | 198 | 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
| 199 | while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
[45156] | 200 | {
|
---|
| 201 | switch (c)
|
---|
| 202 | {
|
---|
| 203 | case 't': // --netname
|
---|
[45560] | 204 | if (pNetName)
|
---|
[48104] | 205 | return errorSyntax(USAGE_NATNETWORK, "You can only specify --netname only once.");
|
---|
[56118] | 206 | pNetName = ValueUnion.psz;
|
---|
[48104] | 207 | break;
|
---|
[45559] | 208 |
|
---|
[45156] | 209 | case 'n': // --network
|
---|
[45560] | 210 | if (pNetworkCidr)
|
---|
[48104] | 211 | return errorSyntax(USAGE_NATNETWORK, "You can only specify --network only once.");
|
---|
[56118] | 212 | pNetworkCidr = ValueUnion.psz;
|
---|
[48104] | 213 | break;
|
---|
[45559] | 214 |
|
---|
[45156] | 215 | case 'e': // --enable
|
---|
[48104] | 216 | if (enable >= 0)
|
---|
[45156] | 217 | return errorSyntax(USAGE_NATNETWORK, "You can specify either --enable or --disable once.");
|
---|
[56118] | 218 | enable = 1;
|
---|
[48104] | 219 | break;
|
---|
[45559] | 220 |
|
---|
[45156] | 221 | case 'd': // --disable
|
---|
[45560] | 222 | if (enable >= 0)
|
---|
[45156] | 223 | return errorSyntax(USAGE_NATNETWORK, "You can specify either --enable or --disable once.");
|
---|
[56118] | 224 | enable = 0;
|
---|
[48104] | 225 | break;
|
---|
| 226 |
|
---|
[45156] | 227 | case 'h':
|
---|
| 228 | if (dhcp != -1)
|
---|
[48104] | 229 | return errorSyntax(USAGE_NATNETWORK, "You can specify --dhcp only once.");
|
---|
[45156] | 230 | dhcp = ValueUnion.f;
|
---|
| 231 | break;
|
---|
[45559] | 232 |
|
---|
[45156] | 233 | case '6':
|
---|
| 234 | if (ipv6 != -1)
|
---|
[48104] | 235 | return errorSyntax(USAGE_NATNETWORK, "You can specify --ipv6 only once.");
|
---|
[45156] | 236 | ipv6 = ValueUnion.f;
|
---|
| 237 | break;
|
---|
[45559] | 238 |
|
---|
[48950] | 239 | case 'L': /* ipv6 loopback */
|
---|
| 240 | case 'l': /* ipv4 loopback */
|
---|
| 241 | if (RTStrCmp(ValueUnion.psz, "delete") == 0)
|
---|
| 242 | {
|
---|
| 243 | /* deletion */
|
---|
| 244 | if (enmCode != OP_MODIFY)
|
---|
| 245 | errorSyntax(USAGE_NATNETWORK,
|
---|
| 246 | "loopback couldn't be deleted on modified\n");
|
---|
| 247 | if (c == 'L')
|
---|
[48104] | 248 | loopback6Offset = -1;
|
---|
[48950] | 249 | else
|
---|
| 250 | {
|
---|
| 251 | int vrc;
|
---|
| 252 | RTGETOPTUNION Addr2Delete;
|
---|
| 253 | vrc = RTGetOptFetchValue(&GetState,
|
---|
| 254 | &Addr2Delete,
|
---|
| 255 | RTGETOPT_REQ_STRING);
|
---|
| 256 | if (RT_FAILURE(vrc))
|
---|
| 257 | return errorSyntax(USAGE_NATNETWORK,
|
---|
| 258 | "Not enough parmaters\n");
|
---|
[48096] | 259 |
|
---|
[48950] | 260 | vLoopback2Delete.push_back(std::string(Addr2Delete.psz));
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
[56118] | 263 | else
|
---|
[48950] | 264 | {
|
---|
[56118] | 265 | /* addition */
|
---|
[48950] | 266 | if (c == 'L')
|
---|
[56118] | 267 | loopback6Offset = ValueUnion.u32;
|
---|
[48950] | 268 | else
|
---|
[56118] | 269 | vLoopback2Add.push_back(std::string(ValueUnion.psz));
|
---|
[48950] | 270 | }
|
---|
| 271 | break;
|
---|
[48096] | 272 |
|
---|
[45559] | 273 | case 'P': /* ipv6 portforwarding*/
|
---|
| 274 | case 'p': /* ipv4 portforwarding */
|
---|
| 275 | {
|
---|
| 276 | if (RTStrCmp(ValueUnion.psz, "delete") != 0)
|
---|
| 277 | {
|
---|
[56118] | 278 | /* addition */
|
---|
| 279 | /* netPfStrToPf will clean up the Pfr */
|
---|
[45559] | 280 | PORTFORWARDRULE Pfr;
|
---|
| 281 | int irc = netPfStrToPf(ValueUnion.psz, (c == 'P'), &Pfr);
|
---|
| 282 | if (RT_FAILURE(irc))
|
---|
[56118] | 283 | return errorSyntax(USAGE_NATNETWORK, "Invalid port-forward rule %s\n", ValueUnion.psz);
|
---|
[48104] | 284 |
|
---|
[45559] | 285 | vPf2Add.push_back(Pfr);
|
---|
| 286 | }
|
---|
| 287 | else
|
---|
| 288 | {
|
---|
[56118] | 289 | /* deletion */
|
---|
[45559] | 290 | if (enmCode != OP_MODIFY)
|
---|
| 291 | return errorSyntax(USAGE_NATNETWORK,
|
---|
| 292 | "Port-forward could be deleted on modify \n");
|
---|
| 293 |
|
---|
[56118] | 294 | RTGETOPTUNION NamePf2DeleteUnion;
|
---|
| 295 | int vrc = RTGetOptFetchValue(&GetState, &NamePf2DeleteUnion, RTGETOPT_REQ_STRING);
|
---|
[45559] | 296 | if (RT_FAILURE(vrc))
|
---|
[56118] | 297 | return errorSyntax(USAGE_NATNETWORK, "Not enough parmaters\n");
|
---|
[48104] | 298 |
|
---|
[45559] | 299 | if (strlen(NamePf2DeleteUnion.psz) > PF_NAMELEN)
|
---|
[56118] | 300 | return errorSyntax(USAGE_NATNETWORK, "Port-forward rule name is too long\n");
|
---|
[45559] | 301 |
|
---|
[56118] | 302 | PFNAME2DELETE Name2Delete;
|
---|
[45559] | 303 | RT_ZERO(Name2Delete);
|
---|
[49114] | 304 | RTStrCopy(Name2Delete.szName, PF_NAMELEN, NamePf2DeleteUnion.psz);
|
---|
[45559] | 305 | Name2Delete.fIPv6 = (c == 'P');
|
---|
| 306 | vPfName2Delete.push_back(Name2Delete);
|
---|
| 307 | }
|
---|
| 308 | break;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[45156] | 311 | default:
|
---|
[56118] | 312 | return errorGetOpt(USAGE_NATNETWORK, c, &ValueUnion);
|
---|
[45156] | 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | if (!pNetName)
|
---|
[56118] | 317 | return errorSyntax(USAGE_NATNETWORK, "You need to specify the --netname option");
|
---|
[45156] | 318 | /* verification */
|
---|
| 319 | switch (enmCode)
|
---|
| 320 | {
|
---|
| 321 | case OP_ADD:
|
---|
| 322 | if (!pNetworkCidr)
|
---|
[56118] | 323 | return errorSyntax(USAGE_NATNETWORK, "You need to specify the --network option");
|
---|
[45156] | 324 | break;
|
---|
| 325 | case OP_MODIFY:
|
---|
| 326 | case OP_REMOVE:
|
---|
| 327 | case OP_START:
|
---|
| 328 | case OP_STOP:
|
---|
| 329 | break;
|
---|
| 330 | default:
|
---|
[56118] | 331 | AssertMsgFailedReturn(("Unknown operation (:%d)", enmCode), RTEXITCODE_FAILURE);
|
---|
[45156] | 332 | }
|
---|
| 333 |
|
---|
[56118] | 334 | HRESULT rc;
|
---|
[45156] | 335 | Bstr NetName;
|
---|
| 336 | NetName = Bstr(pNetName);
|
---|
| 337 |
|
---|
| 338 | ComPtr<INATNetwork> net;
|
---|
| 339 | rc = a->virtualBox->FindNATNetworkByName(NetName.mutableRaw(), net.asOutParam());
|
---|
[48104] | 340 | if (enmCode == OP_ADD)
|
---|
[45156] | 341 | {
|
---|
| 342 | if (SUCCEEDED(rc))
|
---|
| 343 | return errorArgument("NATNetwork server already exists");
|
---|
| 344 |
|
---|
| 345 | CHECK_ERROR(a->virtualBox, CreateNATNetwork(NetName.raw(), net.asOutParam()));
|
---|
| 346 | if (FAILED(rc))
|
---|
| 347 | return errorArgument("Failed to create the NAT network service");
|
---|
| 348 | }
|
---|
| 349 | else if (FAILED(rc))
|
---|
| 350 | return errorArgument("NATNetwork server does not exist");
|
---|
| 351 |
|
---|
| 352 | switch (enmCode)
|
---|
| 353 | {
|
---|
[48104] | 354 | case OP_ADD:
|
---|
| 355 | case OP_MODIFY:
|
---|
[45156] | 356 | {
|
---|
[48104] | 357 | if (pNetworkCidr)
|
---|
[45559] | 358 | {
|
---|
[48104] | 359 | CHECK_ERROR(net, COMSETTER(Network)(Bstr(pNetworkCidr).raw()));
|
---|
| 360 | if (FAILED(rc))
|
---|
| 361 | return errorArgument("Failed to set configuration");
|
---|
| 362 | }
|
---|
| 363 | if (dhcp >= 0)
|
---|
| 364 | {
|
---|
| 365 | CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp));
|
---|
| 366 | if (FAILED(rc))
|
---|
| 367 | return errorArgument("Failed to set configuration");
|
---|
| 368 | }
|
---|
[45559] | 369 |
|
---|
[48104] | 370 | if (ipv6 >= 0)
|
---|
| 371 | {
|
---|
| 372 | CHECK_ERROR(net, COMSETTER(IPv6Enabled) ((BOOL)ipv6));
|
---|
| 373 | if (FAILED(rc))
|
---|
| 374 | return errorArgument("Failed to set configuration");
|
---|
[45559] | 375 | }
|
---|
| 376 |
|
---|
[48104] | 377 | if (!vPfName2Delete.empty())
|
---|
[45559] | 378 | {
|
---|
[48104] | 379 | VPF2DELETEITERATOR it;
|
---|
| 380 | for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it)
|
---|
| 381 | {
|
---|
| 382 | CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6,
|
---|
[49114] | 383 | Bstr((*it).szName).raw()));
|
---|
[48104] | 384 | if (FAILED(rc))
|
---|
| 385 | return errorArgument("Failed to delete pf");
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
[45559] | 388 |
|
---|
[48104] | 389 | if (!vPf2Add.empty())
|
---|
| 390 | {
|
---|
| 391 | VPF2ADDITERATOR it;
|
---|
| 392 | for (it = vPf2Add.begin(); it != vPf2Add.end(); ++it)
|
---|
| 393 | {
|
---|
| 394 | NATProtocol_T proto = NATProtocol_TCP;
|
---|
| 395 | if ((*it).iPfrProto == IPPROTO_TCP)
|
---|
| 396 | proto = NATProtocol_TCP;
|
---|
| 397 | else if ((*it).iPfrProto == IPPROTO_UDP)
|
---|
| 398 | proto = NATProtocol_UDP;
|
---|
| 399 | else
|
---|
| 400 | continue; /* XXX: warning here. */
|
---|
[45559] | 401 |
|
---|
[48104] | 402 | CHECK_ERROR(net, AddPortForwardRule((BOOL)(*it).fPfrIPv6,
|
---|
[49114] | 403 | Bstr((*it).szPfrName).raw(),
|
---|
[48104] | 404 | proto,
|
---|
[49114] | 405 | Bstr((*it).szPfrHostAddr).raw(),
|
---|
[48104] | 406 | (*it).u16PfrHostPort,
|
---|
[49114] | 407 | Bstr((*it).szPfrGuestAddr).raw(),
|
---|
[48104] | 408 | (*it).u16PfrGuestPort));
|
---|
| 409 | if (FAILED(rc))
|
---|
| 410 | return errorArgument("Failed to add pf");
|
---|
| 411 | }
|
---|
[45559] | 412 | }
|
---|
[48104] | 413 |
|
---|
| 414 | if (loopback6Offset)
|
---|
[48096] | 415 | {
|
---|
[48104] | 416 | if (loopback6Offset == -1)
|
---|
| 417 | loopback6Offset = 0; /* deletion */
|
---|
[48096] | 418 |
|
---|
[56118] | 419 | CHECK_ERROR_RET(net, COMSETTER(LoopbackIp6)(loopback6Offset), RTEXITCODE_FAILURE);
|
---|
[48104] | 420 | }
|
---|
[48096] | 421 |
|
---|
[48104] | 422 | /* addLocalMapping (hostid, offset) */
|
---|
| 423 | if (!vLoopback2Add.empty())
|
---|
| 424 | {
|
---|
[48405] | 425 | /* we're expecting stings 127.0.0.1=5 */
|
---|
[48104] | 426 | LOOPBACK2DELETEADDITERATOR it;
|
---|
| 427 | for (it = vLoopback2Add.begin();
|
---|
| 428 | it != vLoopback2Add.end();
|
---|
| 429 | ++it)
|
---|
| 430 | {
|
---|
| 431 | std::string address, strOffset;
|
---|
[53312] | 432 | size_t pos = it->find('=');
|
---|
[48104] | 433 | LONG lOffset = 0;
|
---|
| 434 | Bstr bstrAddress;
|
---|
[48096] | 435 |
|
---|
[53493] | 436 | AssertReturn(pos != std::string::npos, errorArgument("invalid loopback string"));
|
---|
[48104] | 437 |
|
---|
| 438 | address = it->substr(0, pos);
|
---|
| 439 | strOffset = it->substr(pos + 1);
|
---|
| 440 |
|
---|
| 441 | lOffset = RTStrToUInt32(strOffset.c_str());
|
---|
| 442 | AssertReturn(lOffset > 0, errorArgument("invalid loopback string"));
|
---|
| 443 |
|
---|
| 444 | bstrAddress = Bstr(address.c_str());
|
---|
| 445 |
|
---|
[56118] | 446 | CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), lOffset), RTEXITCODE_FAILURE);
|
---|
[48104] | 447 | }
|
---|
[48096] | 448 | }
|
---|
| 449 |
|
---|
[48104] | 450 | if (!vLoopback2Delete.empty())
|
---|
[48096] | 451 | {
|
---|
[48104] | 452 | /* we're expecting stings 127.0.0.1 */
|
---|
| 453 | LOOPBACK2DELETEADDITERATOR it;
|
---|
| 454 | for (it = vLoopback2Add.begin();
|
---|
| 455 | it != vLoopback2Add.end();
|
---|
| 456 | ++it)
|
---|
| 457 | {
|
---|
| 458 | Bstr bstrAddress;
|
---|
| 459 | bstrAddress = Bstr(it->c_str());
|
---|
[48096] | 460 |
|
---|
[56118] | 461 | CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), 0), RTEXITCODE_FAILURE);
|
---|
[48104] | 462 | }
|
---|
[48096] | 463 | }
|
---|
| 464 |
|
---|
[48104] | 465 | if (enable >= 0)
|
---|
| 466 | {
|
---|
| 467 | CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable));
|
---|
| 468 | if (FAILED(rc))
|
---|
| 469 | return errorArgument("Failed to set configuration");
|
---|
| 470 | }
|
---|
| 471 | break;
|
---|
| 472 | }
|
---|
| 473 | case OP_REMOVE:
|
---|
[45156] | 474 | {
|
---|
[48104] | 475 | CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net));
|
---|
| 476 | if (FAILED(rc))
|
---|
| 477 | return errorArgument("Failed to remove nat network");
|
---|
| 478 | break;
|
---|
[45156] | 479 | }
|
---|
[48104] | 480 | case OP_START:
|
---|
| 481 | {
|
---|
| 482 | CHECK_ERROR(net, Start(Bstr("whatever").raw()));
|
---|
| 483 | if (FAILED(rc))
|
---|
| 484 | return errorArgument("Failed to start network");
|
---|
| 485 | break;
|
---|
| 486 | }
|
---|
| 487 | case OP_STOP:
|
---|
| 488 | {
|
---|
| 489 | CHECK_ERROR(net, Stop());
|
---|
| 490 | if (FAILED(rc))
|
---|
| 491 | return errorArgument("Failed to start network");
|
---|
| 492 | break;
|
---|
| 493 | }
|
---|
| 494 | default:;
|
---|
[45156] | 495 | }
|
---|
[56118] | 496 | return RTEXITCODE_SUCCESS;
|
---|
[45156] | 497 | }
|
---|
| 498 |
|
---|
| 499 |
|
---|
[56118] | 500 | RTEXITCODE handleNATNetwork(HandlerArg *a)
|
---|
[45156] | 501 | {
|
---|
| 502 | if (a->argc < 1)
|
---|
| 503 | return errorSyntax(USAGE_NATNETWORK, "Not enough parameters");
|
---|
| 504 |
|
---|
[56118] | 505 | RTEXITCODE rcExit;
|
---|
[45156] | 506 | if (strcmp(a->argv[0], "modify") == 0)
|
---|
[56118] | 507 | rcExit = handleOp(a, OP_MODIFY);
|
---|
[45156] | 508 | else if (strcmp(a->argv[0], "add") == 0)
|
---|
[56118] | 509 | rcExit = handleOp(a, OP_ADD);
|
---|
[45156] | 510 | else if (strcmp(a->argv[0], "remove") == 0)
|
---|
[56118] | 511 | rcExit = handleOp(a, OP_REMOVE);
|
---|
[45156] | 512 | else if (strcmp(a->argv[0], "start") == 0)
|
---|
[56118] | 513 | rcExit = handleOp(a, OP_START);
|
---|
[45156] | 514 | else if (strcmp(a->argv[0], "stop") == 0)
|
---|
[56118] | 515 | rcExit = handleOp(a, OP_STOP);
|
---|
[60501] | 516 | else if (strcmp(a->argv[0], "list") == 0)
|
---|
[60508] | 517 | rcExit = handleNATList(a);
|
---|
[45156] | 518 | else
|
---|
[56118] | 519 | rcExit = errorSyntax(USAGE_NATNETWORK, "Invalid parameter '%s'", Utf8Str(a->argv[0]).c_str());
|
---|
| 520 | return rcExit;
|
---|
[45156] | 521 | }
|
---|
| 522 |
|
---|
| 523 | #endif /* !VBOX_ONLY_DOCS */
|
---|