[45156] | 1 | /* $Id: VBoxManageNATNetwork.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBoxManage - Implementation of NAT Network command command.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[45156] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[45156] | 26 | */
|
---|
| 27 |
|
---|
[57358] | 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[45156] | 32 | #include <VBox/com/com.h>
|
---|
| 33 | #include <VBox/com/array.h>
|
---|
| 34 | #include <VBox/com/ErrorInfo.h>
|
---|
| 35 | #include <VBox/com/errorprint.h>
|
---|
| 36 | #include <VBox/com/VirtualBox.h>
|
---|
| 37 |
|
---|
[47704] | 38 | #ifndef RT_OS_WINDOWS
|
---|
| 39 | # include <netinet/in.h>
|
---|
[48363] | 40 | #else
|
---|
| 41 | /* from <ws2ipdef.h> */
|
---|
| 42 | # define INET6_ADDRSTRLEN 65
|
---|
[47704] | 43 | #endif
|
---|
[45559] | 44 |
|
---|
| 45 | #define IPv6
|
---|
| 46 |
|
---|
| 47 | #include <iprt/cdefs.h>
|
---|
[45156] | 48 | #include <iprt/cidr.h>
|
---|
| 49 | #include <iprt/param.h>
|
---|
| 50 | #include <iprt/path.h>
|
---|
| 51 | #include <iprt/stream.h>
|
---|
| 52 | #include <iprt/string.h>
|
---|
| 53 | #include <iprt/net.h>
|
---|
| 54 | #include <iprt/getopt.h>
|
---|
| 55 | #include <iprt/ctype.h>
|
---|
| 56 |
|
---|
| 57 | #include <VBox/log.h>
|
---|
| 58 |
|
---|
[93287] | 59 | #include <algorithm>
|
---|
[45559] | 60 | #include <vector>
|
---|
[83796] | 61 | #include <iprt/sanitized/string>
|
---|
[45559] | 62 |
|
---|
[45156] | 63 | #include "VBoxManage.h"
|
---|
[45559] | 64 | #include "VBoxPortForwardString.h"
|
---|
[45156] | 65 |
|
---|
[45559] | 66 |
|
---|
[92372] | 67 | DECLARE_TRANSLATION_CONTEXT(Nat);
|
---|
| 68 |
|
---|
[45156] | 69 | using namespace com;
|
---|
| 70 |
|
---|
[56118] | 71 | typedef enum
|
---|
[45156] | 72 | {
|
---|
| 73 | OP_ADD = 1000,
|
---|
| 74 | OP_REMOVE,
|
---|
| 75 | OP_MODIFY,
|
---|
| 76 | OP_START,
|
---|
| 77 | OP_STOP
|
---|
| 78 | } OPCODE;
|
---|
| 79 |
|
---|
[45559] | 80 | typedef struct PFNAME2DELETE
|
---|
| 81 | {
|
---|
[49114] | 82 | char szName[PF_NAMELEN];
|
---|
[45559] | 83 | bool fIPv6;
|
---|
| 84 | } PFNAME2DELETE, *PPFNAME2DELETE;
|
---|
| 85 |
|
---|
| 86 | typedef std::vector<PFNAME2DELETE> VPF2DELETE;
|
---|
| 87 | typedef VPF2DELETE::const_iterator VPF2DELETEITERATOR;
|
---|
| 88 |
|
---|
| 89 | typedef std::vector<PORTFORWARDRULE> VPF2ADD;
|
---|
| 90 | typedef VPF2ADD::const_iterator VPF2ADDITERATOR;
|
---|
| 91 |
|
---|
[48096] | 92 | typedef std::vector<std::string> LOOPBACK2DELETEADD;
|
---|
| 93 | typedef LOOPBACK2DELETEADD::iterator LOOPBACK2DELETEADDITERATOR;
|
---|
[45559] | 94 |
|
---|
[93286] | 95 | static HRESULT printNATNetwork(const ComPtr<INATNetwork> &pNATNet,
|
---|
| 96 | bool fLong = true)
|
---|
[60501] | 97 | {
|
---|
[95140] | 98 | HRESULT hrc;
|
---|
[60501] | 99 |
|
---|
| 100 | do
|
---|
| 101 | {
|
---|
| 102 | Bstr strVal;
|
---|
[88755] | 103 | BOOL fVal;
|
---|
| 104 |
|
---|
[60501] | 105 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(NetworkName)(strVal.asOutParam()));
|
---|
[92372] | 106 | RTPrintf(Nat::tr("Name: %ls\n"), strVal.raw());
|
---|
[88755] | 107 |
|
---|
[93286] | 108 | if (fLong)
|
---|
| 109 | {
|
---|
| 110 | /*
|
---|
| 111 | * What does it even mean for a natnet to be disabled?
|
---|
| 112 | * (rhetorical question). Anyway, don't print it unless
|
---|
| 113 | * asked for a complete dump.
|
---|
| 114 | */
|
---|
| 115 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(Enabled)(&fVal));
|
---|
| 116 | RTPrintf(Nat::tr("Enabled: %s\n"), fVal ? Nat::tr("Yes") : Nat::tr("No"));
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[60501] | 119 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(Network)(strVal.asOutParam()));
|
---|
[92372] | 120 | RTPrintf(Nat::tr("Network: %ls\n"), strVal.raw());
|
---|
[88755] | 121 |
|
---|
[60501] | 122 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(Gateway)(strVal.asOutParam()));
|
---|
[92372] | 123 | RTPrintf(Nat::tr("Gateway: %ls\n"), strVal.raw());
|
---|
[88755] | 124 |
|
---|
| 125 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(NeedDhcpServer)(&fVal));
|
---|
[94322] | 126 | RTPrintf(Nat::tr("DHCP Server: %s\n"), fVal ? Nat::tr("Yes") : Nat::tr("No"));
|
---|
[88755] | 127 |
|
---|
[60501] | 128 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(IPv6Enabled)(&fVal));
|
---|
[92594] | 129 | RTPrintf("IPv6: %s\n", fVal ? Nat::tr("Yes") : Nat::tr("No"));
|
---|
[88755] | 130 |
|
---|
| 131 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(IPv6Prefix)(strVal.asOutParam()));
|
---|
[92372] | 132 | RTPrintf(Nat::tr("IPv6 Prefix: %ls\n"), strVal.raw());
|
---|
[88755] | 133 |
|
---|
| 134 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fVal));
|
---|
[92372] | 135 | RTPrintf(Nat::tr("IPv6 Default: %s\n"), fVal ? Nat::tr("Yes") : Nat::tr("No"));
|
---|
[88755] | 136 |
|
---|
| 137 |
|
---|
[93286] | 138 | if (fLong)
|
---|
| 139 | {
|
---|
| 140 | com::SafeArray<BSTR> strs;
|
---|
| 141 |
|
---|
| 142 | #define PRINT_STRING_ARRAY(title) do { \
|
---|
| 143 | if (strs.size() > 0) \
|
---|
| 144 | { \
|
---|
| 145 | RTPrintf(title); \
|
---|
| 146 | for (size_t j = 0; j < strs.size(); ++j) \
|
---|
| 147 | RTPrintf(" %s\n", Utf8Str(strs[j]).c_str()); \
|
---|
| 148 | } \
|
---|
| 149 | } while (0)
|
---|
| 150 |
|
---|
| 151 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(strs)));
|
---|
| 152 | PRINT_STRING_ARRAY(Nat::tr("Port-forwarding (ipv4)\n"));
|
---|
| 153 | strs.setNull();
|
---|
| 154 |
|
---|
| 155 | CHECK_ERROR(pNATNet, COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(strs)));
|
---|
| 156 | PRINT_STRING_ARRAY(Nat::tr("Port-forwarding (ipv6)\n"));
|
---|
| 157 | strs.setNull();
|
---|
| 158 |
|
---|
| 159 | CHECK_ERROR(pNATNet, COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs)));
|
---|
| 160 | PRINT_STRING_ARRAY(Nat::tr("loopback mappings (ipv4)\n"));
|
---|
| 161 | strs.setNull();
|
---|
| 162 |
|
---|
| 163 | #undef PRINT_STRING_ARRAY
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[60501] | 166 | RTPrintf("\n");
|
---|
| 167 | } while (0);
|
---|
| 168 |
|
---|
[95140] | 169 | return hrc;
|
---|
[60501] | 170 | }
|
---|
| 171 |
|
---|
[60508] | 172 | static RTEXITCODE handleNATList(HandlerArg *a)
|
---|
[60501] | 173 | {
|
---|
[95140] | 174 | HRESULT hrc;
|
---|
[60501] | 175 |
|
---|
[92372] | 176 | RTPrintf(Nat::tr("NAT Networks:\n\n"));
|
---|
[60501] | 177 |
|
---|
| 178 | const char *pszFilter = NULL;
|
---|
| 179 | if (a->argc > 1)
|
---|
| 180 | pszFilter = a->argv[1];
|
---|
| 181 |
|
---|
| 182 | size_t cFound = 0;
|
---|
| 183 |
|
---|
| 184 | com::SafeIfaceArray<INATNetwork> arrNetNets;
|
---|
| 185 | CHECK_ERROR(a->virtualBox, COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(arrNetNets)));
|
---|
| 186 | for (size_t i = 0; i < arrNetNets.size(); ++i)
|
---|
| 187 | {
|
---|
| 188 | ComPtr<INATNetwork> pNATNet = arrNetNets[i];
|
---|
| 189 |
|
---|
| 190 | if (pszFilter)
|
---|
| 191 | {
|
---|
| 192 | Bstr strVal;
|
---|
| 193 | CHECK_ERROR_BREAK(pNATNet, COMGETTER(NetworkName)(strVal.asOutParam()));
|
---|
| 194 |
|
---|
[93701] | 195 | Utf8Str strValUTF8(strVal);
|
---|
[60501] | 196 | if (!RTStrSimplePatternMatch(pszFilter, strValUTF8.c_str()))
|
---|
| 197 | continue;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[95140] | 200 | hrc = printNATNetwork(pNATNet);
|
---|
| 201 | if (FAILED(hrc))
|
---|
[60501] | 202 | break;
|
---|
| 203 |
|
---|
| 204 | cFound++;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[95140] | 207 | if (SUCCEEDED(hrc))
|
---|
[92372] | 208 | RTPrintf(Nat::tr("%zu %s found\n"), cFound, cFound == 1 ? Nat::tr("network") : Nat::tr("networks", "", cFound));
|
---|
[60501] | 209 |
|
---|
[95140] | 210 | return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
[60501] | 211 | }
|
---|
| 212 |
|
---|
[56118] | 213 | static RTEXITCODE handleOp(HandlerArg *a, OPCODE enmCode)
|
---|
[45156] | 214 | {
|
---|
[56118] | 215 | if (a->argc - 1 <= 1)
|
---|
[94211] | 216 | return errorSyntax(Nat::tr("Not enough parameters"));
|
---|
[45156] | 217 |
|
---|
| 218 | const char *pNetName = NULL;
|
---|
[88756] | 219 | const char *pPrefixIPv4 = NULL;
|
---|
| 220 | const char *pPrefixIPv6 = NULL;
|
---|
[45156] | 221 | int enable = -1;
|
---|
| 222 | int dhcp = -1;
|
---|
| 223 | int ipv6 = -1;
|
---|
[93278] | 224 | int ipv6_default = -1;
|
---|
[45156] | 225 |
|
---|
[45559] | 226 | VPF2DELETE vPfName2Delete;
|
---|
| 227 | VPF2ADD vPf2Add;
|
---|
| 228 |
|
---|
[48096] | 229 | LOOPBACK2DELETEADD vLoopback2Delete;
|
---|
| 230 | LOOPBACK2DELETEADD vLoopback2Add;
|
---|
| 231 |
|
---|
| 232 | LONG loopback6Offset = 0; /* ignore me */
|
---|
| 233 |
|
---|
[93278] | 234 | enum
|
---|
| 235 | {
|
---|
| 236 | kNATNetworkIota = 1000,
|
---|
| 237 | kNATNetwork_IPv6Default,
|
---|
| 238 | kNATNetwork_IPv6Prefix,
|
---|
| 239 | };
|
---|
| 240 |
|
---|
[56118] | 241 | static const RTGETOPTDEF g_aNATNetworkIPOptions[] =
|
---|
| 242 | {
|
---|
[88756] | 243 | { "--netname", 't', RTGETOPT_REQ_STRING },
|
---|
| 244 | { "--network", 'n', RTGETOPT_REQ_STRING }, /* old name */
|
---|
| 245 | { "--ipv4-prefix", 'n', RTGETOPT_REQ_STRING }, /* new name */
|
---|
| 246 | { "--dhcp", 'h', RTGETOPT_REQ_BOOL },
|
---|
| 247 | { "--ipv6", '6', RTGETOPT_REQ_BOOL }, /* old name */
|
---|
[93278] | 248 | { "--ipv6-default", kNATNetwork_IPv6Default, RTGETOPT_REQ_BOOL },
|
---|
[88756] | 249 | { "--ipv6-enable", '6', RTGETOPT_REQ_BOOL }, /* new name */
|
---|
[93278] | 250 | { "--ipv6-prefix", kNATNetwork_IPv6Prefix, RTGETOPT_REQ_STRING },
|
---|
[88756] | 251 | { "--enable", 'e', RTGETOPT_REQ_NOTHING },
|
---|
| 252 | { "--disable", 'd', RTGETOPT_REQ_NOTHING },
|
---|
| 253 | { "--port-forward-4", 'p', RTGETOPT_REQ_STRING },
|
---|
| 254 | { "--port-forward-6", 'P', RTGETOPT_REQ_STRING },
|
---|
| 255 | { "--loopback-4", 'l', RTGETOPT_REQ_STRING },
|
---|
| 256 | { "--loopback-6", 'L', RTGETOPT_REQ_STRING },
|
---|
[56118] | 257 | };
|
---|
| 258 |
|
---|
[45156] | 259 | int c;
|
---|
| 260 | RTGETOPTUNION ValueUnion;
|
---|
| 261 | RTGETOPTSTATE GetState;
|
---|
[56118] | 262 | RTGetOptInit(&GetState, a->argc, a->argv, g_aNATNetworkIPOptions,
|
---|
[45156] | 263 | enmCode != OP_REMOVE ? RT_ELEMENTS(g_aNATNetworkIPOptions) : 4, /* we use only --netname and --ifname for remove*/
|
---|
[56118] | 264 | 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
| 265 | while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
[45156] | 266 | {
|
---|
| 267 | switch (c)
|
---|
| 268 | {
|
---|
| 269 | case 't': // --netname
|
---|
[45560] | 270 | if (pNetName)
|
---|
[94211] | 271 | return errorSyntax(Nat::tr("You can only specify --netname only once."));
|
---|
[56118] | 272 | pNetName = ValueUnion.psz;
|
---|
[48104] | 273 | break;
|
---|
[45559] | 274 |
|
---|
[45156] | 275 | case 'n': // --network
|
---|
[88756] | 276 | if (pPrefixIPv4)
|
---|
[94211] | 277 | return errorSyntax(Nat::tr("You can only specify --network only once."));
|
---|
[88756] | 278 | pPrefixIPv4 = ValueUnion.psz;
|
---|
[48104] | 279 | break;
|
---|
[45559] | 280 |
|
---|
[45156] | 281 | case 'e': // --enable
|
---|
[48104] | 282 | if (enable >= 0)
|
---|
[94211] | 283 | return errorSyntax(Nat::tr("You can specify either --enable or --disable once."));
|
---|
[56118] | 284 | enable = 1;
|
---|
[48104] | 285 | break;
|
---|
[45559] | 286 |
|
---|
[45156] | 287 | case 'd': // --disable
|
---|
[45560] | 288 | if (enable >= 0)
|
---|
[94211] | 289 | return errorSyntax(Nat::tr("You can specify either --enable or --disable once."));
|
---|
[56118] | 290 | enable = 0;
|
---|
[48104] | 291 | break;
|
---|
| 292 |
|
---|
[45156] | 293 | case 'h':
|
---|
| 294 | if (dhcp != -1)
|
---|
[94211] | 295 | return errorSyntax(Nat::tr("You can specify --dhcp only once."));
|
---|
[45156] | 296 | dhcp = ValueUnion.f;
|
---|
| 297 | break;
|
---|
[45559] | 298 |
|
---|
[45156] | 299 | case '6':
|
---|
| 300 | if (ipv6 != -1)
|
---|
[94211] | 301 | return errorSyntax(Nat::tr("You can specify --ipv6 only once."));
|
---|
[45156] | 302 | ipv6 = ValueUnion.f;
|
---|
| 303 | break;
|
---|
[45559] | 304 |
|
---|
[93278] | 305 | case kNATNetwork_IPv6Prefix:
|
---|
[88756] | 306 | if (pPrefixIPv6)
|
---|
[94211] | 307 | return errorSyntax(Nat::tr("You can specify --ipv6-prefix only once."));
|
---|
[88756] | 308 | pPrefixIPv6 = ValueUnion.psz;
|
---|
| 309 | break;
|
---|
| 310 |
|
---|
[93278] | 311 | case kNATNetwork_IPv6Default: // XXX: uwe
|
---|
| 312 | if (ipv6_default != -1)
|
---|
[94211] | 313 | return errorSyntax(Nat::tr("You can specify --ipv6-default only once."));
|
---|
[93278] | 314 | ipv6_default = ValueUnion.f;
|
---|
| 315 | break;
|
---|
| 316 |
|
---|
[48950] | 317 | case 'L': /* ipv6 loopback */
|
---|
| 318 | case 'l': /* ipv4 loopback */
|
---|
| 319 | if (RTStrCmp(ValueUnion.psz, "delete") == 0)
|
---|
| 320 | {
|
---|
| 321 | /* deletion */
|
---|
| 322 | if (enmCode != OP_MODIFY)
|
---|
[94211] | 323 | errorSyntax(Nat::tr("loopback couldn't be deleted on modified\n"));
|
---|
[48950] | 324 | if (c == 'L')
|
---|
[48104] | 325 | loopback6Offset = -1;
|
---|
[48950] | 326 | else
|
---|
| 327 | {
|
---|
| 328 | int vrc;
|
---|
| 329 | RTGETOPTUNION Addr2Delete;
|
---|
| 330 | vrc = RTGetOptFetchValue(&GetState,
|
---|
| 331 | &Addr2Delete,
|
---|
| 332 | RTGETOPT_REQ_STRING);
|
---|
| 333 | if (RT_FAILURE(vrc))
|
---|
[94211] | 334 | return errorSyntax(Nat::tr("Not enough parаmeters\n"));
|
---|
[48096] | 335 |
|
---|
[48950] | 336 | vLoopback2Delete.push_back(std::string(Addr2Delete.psz));
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
[56118] | 339 | else
|
---|
[48950] | 340 | {
|
---|
[56118] | 341 | /* addition */
|
---|
[48950] | 342 | if (c == 'L')
|
---|
[56118] | 343 | loopback6Offset = ValueUnion.u32;
|
---|
[48950] | 344 | else
|
---|
[56118] | 345 | vLoopback2Add.push_back(std::string(ValueUnion.psz));
|
---|
[48950] | 346 | }
|
---|
| 347 | break;
|
---|
[48096] | 348 |
|
---|
[45559] | 349 | case 'P': /* ipv6 portforwarding*/
|
---|
| 350 | case 'p': /* ipv4 portforwarding */
|
---|
| 351 | {
|
---|
| 352 | if (RTStrCmp(ValueUnion.psz, "delete") != 0)
|
---|
| 353 | {
|
---|
[56118] | 354 | /* addition */
|
---|
| 355 | /* netPfStrToPf will clean up the Pfr */
|
---|
[45559] | 356 | PORTFORWARDRULE Pfr;
|
---|
| 357 | int irc = netPfStrToPf(ValueUnion.psz, (c == 'P'), &Pfr);
|
---|
| 358 | if (RT_FAILURE(irc))
|
---|
[94211] | 359 | return errorSyntax(Nat::tr("Invalid port-forward rule %s\n"), ValueUnion.psz);
|
---|
[48104] | 360 |
|
---|
[45559] | 361 | vPf2Add.push_back(Pfr);
|
---|
| 362 | }
|
---|
| 363 | else
|
---|
| 364 | {
|
---|
[56118] | 365 | /* deletion */
|
---|
[45559] | 366 | if (enmCode != OP_MODIFY)
|
---|
[94211] | 367 | return errorSyntax(Nat::tr("Port-forward could be deleted on modify\n"));
|
---|
[45559] | 368 |
|
---|
[56118] | 369 | RTGETOPTUNION NamePf2DeleteUnion;
|
---|
| 370 | int vrc = RTGetOptFetchValue(&GetState, &NamePf2DeleteUnion, RTGETOPT_REQ_STRING);
|
---|
[45559] | 371 | if (RT_FAILURE(vrc))
|
---|
[94211] | 372 | return errorSyntax(Nat::tr("Not enough parаmeters\n"));
|
---|
[48104] | 373 |
|
---|
[45559] | 374 | if (strlen(NamePf2DeleteUnion.psz) > PF_NAMELEN)
|
---|
[94211] | 375 | return errorSyntax(Nat::tr("Port-forward rule name is too long\n"));
|
---|
[45559] | 376 |
|
---|
[56118] | 377 | PFNAME2DELETE Name2Delete;
|
---|
[45559] | 378 | RT_ZERO(Name2Delete);
|
---|
[49114] | 379 | RTStrCopy(Name2Delete.szName, PF_NAMELEN, NamePf2DeleteUnion.psz);
|
---|
[45559] | 380 | Name2Delete.fIPv6 = (c == 'P');
|
---|
| 381 | vPfName2Delete.push_back(Name2Delete);
|
---|
| 382 | }
|
---|
| 383 | break;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
[45156] | 386 | default:
|
---|
[94211] | 387 | return errorGetOpt(c, &ValueUnion);
|
---|
[45156] | 388 | }
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | if (!pNetName)
|
---|
[94211] | 392 | return errorSyntax(Nat::tr("You need to specify the --netname option"));
|
---|
[45156] | 393 | /* verification */
|
---|
| 394 | switch (enmCode)
|
---|
| 395 | {
|
---|
| 396 | case OP_ADD:
|
---|
[88756] | 397 | if (!pPrefixIPv4)
|
---|
[94211] | 398 | return errorSyntax(Nat::tr("You need to specify the --network option"));
|
---|
[45156] | 399 | break;
|
---|
| 400 | case OP_MODIFY:
|
---|
| 401 | case OP_REMOVE:
|
---|
| 402 | case OP_START:
|
---|
| 403 | case OP_STOP:
|
---|
| 404 | break;
|
---|
| 405 | default:
|
---|
[92372] | 406 | AssertMsgFailedReturn((Nat::tr("Unknown operation (:%d)"), enmCode), RTEXITCODE_FAILURE);
|
---|
[45156] | 407 | }
|
---|
| 408 |
|
---|
[95140] | 409 | HRESULT hrc;
|
---|
[45156] | 410 | Bstr NetName;
|
---|
| 411 | NetName = Bstr(pNetName);
|
---|
| 412 |
|
---|
| 413 | ComPtr<INATNetwork> net;
|
---|
[95140] | 414 | hrc = a->virtualBox->FindNATNetworkByName(NetName.mutableRaw(), net.asOutParam());
|
---|
[48104] | 415 | if (enmCode == OP_ADD)
|
---|
[45156] | 416 | {
|
---|
[95140] | 417 | if (SUCCEEDED(hrc))
|
---|
[92372] | 418 | return errorArgument(Nat::tr("NATNetwork server already exists"));
|
---|
[45156] | 419 |
|
---|
| 420 | CHECK_ERROR(a->virtualBox, CreateNATNetwork(NetName.raw(), net.asOutParam()));
|
---|
[95140] | 421 | if (FAILED(hrc))
|
---|
[92372] | 422 | return errorArgument(Nat::tr("Failed to create the NAT network service"));
|
---|
[45156] | 423 | }
|
---|
[95140] | 424 | else if (FAILED(hrc))
|
---|
[92372] | 425 | return errorArgument(Nat::tr("NATNetwork server does not exist"));
|
---|
[45156] | 426 |
|
---|
| 427 | switch (enmCode)
|
---|
| 428 | {
|
---|
[48104] | 429 | case OP_ADD:
|
---|
| 430 | case OP_MODIFY:
|
---|
[45156] | 431 | {
|
---|
[88756] | 432 | if (pPrefixIPv4)
|
---|
[45559] | 433 | {
|
---|
[88756] | 434 | CHECK_ERROR(net, COMSETTER(Network)(Bstr(pPrefixIPv4).raw()));
|
---|
[95140] | 435 | if (FAILED(hrc))
|
---|
[92372] | 436 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
[48104] | 437 | }
|
---|
| 438 | if (dhcp >= 0)
|
---|
| 439 | {
|
---|
| 440 | CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp));
|
---|
[95140] | 441 | if (FAILED(hrc))
|
---|
[92372] | 442 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
[48104] | 443 | }
|
---|
[45559] | 444 |
|
---|
[88756] | 445 | /*
|
---|
| 446 | * If we are asked to disable IPv6, do it early so that
|
---|
| 447 | * the same command can also set IPv6 prefix to empty if
|
---|
| 448 | * it so wishes.
|
---|
| 449 | */
|
---|
| 450 | if (ipv6 == 0)
|
---|
[48104] | 451 | {
|
---|
[88756] | 452 | CHECK_ERROR(net, COMSETTER(IPv6Enabled)(FALSE));
|
---|
[95140] | 453 | if (FAILED(hrc))
|
---|
[92372] | 454 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
[45559] | 455 | }
|
---|
| 456 |
|
---|
[88756] | 457 | if (pPrefixIPv6)
|
---|
| 458 | {
|
---|
| 459 | CHECK_ERROR(net, COMSETTER(IPv6Prefix)(Bstr(pPrefixIPv6).raw()));
|
---|
[95140] | 460 | if (FAILED(hrc))
|
---|
[92372] | 461 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
[88756] | 462 | }
|
---|
| 463 |
|
---|
| 464 | /*
|
---|
| 465 | * If we are asked to enable IPv6, do it late, so that the
|
---|
| 466 | * same command can also set IPv6 prefix.
|
---|
| 467 | */
|
---|
| 468 | if (ipv6 > 0)
|
---|
| 469 | {
|
---|
| 470 | CHECK_ERROR(net, COMSETTER(IPv6Enabled)(TRUE));
|
---|
[95140] | 471 | if (FAILED(hrc))
|
---|
[92372] | 472 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
[88756] | 473 | }
|
---|
| 474 |
|
---|
[93278] | 475 | if (ipv6_default != -1)
|
---|
| 476 | {
|
---|
| 477 | BOOL fIPv6Default = RT_BOOL(ipv6_default);
|
---|
| 478 | CHECK_ERROR(net, COMSETTER(AdvertiseDefaultIPv6RouteEnabled)(fIPv6Default));
|
---|
[95140] | 479 | if (FAILED(hrc))
|
---|
[93278] | 480 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[48104] | 483 | if (!vPfName2Delete.empty())
|
---|
[45559] | 484 | {
|
---|
[48104] | 485 | VPF2DELETEITERATOR it;
|
---|
| 486 | for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it)
|
---|
| 487 | {
|
---|
| 488 | CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6,
|
---|
[49114] | 489 | Bstr((*it).szName).raw()));
|
---|
[95140] | 490 | if (FAILED(hrc))
|
---|
[92372] | 491 | return errorArgument(Nat::tr("Failed to delete pf"));
|
---|
[48104] | 492 | }
|
---|
| 493 | }
|
---|
[45559] | 494 |
|
---|
[48104] | 495 | if (!vPf2Add.empty())
|
---|
| 496 | {
|
---|
| 497 | VPF2ADDITERATOR it;
|
---|
| 498 | for (it = vPf2Add.begin(); it != vPf2Add.end(); ++it)
|
---|
| 499 | {
|
---|
| 500 | NATProtocol_T proto = NATProtocol_TCP;
|
---|
| 501 | if ((*it).iPfrProto == IPPROTO_TCP)
|
---|
| 502 | proto = NATProtocol_TCP;
|
---|
| 503 | else if ((*it).iPfrProto == IPPROTO_UDP)
|
---|
| 504 | proto = NATProtocol_UDP;
|
---|
| 505 | else
|
---|
| 506 | continue; /* XXX: warning here. */
|
---|
[45559] | 507 |
|
---|
[48104] | 508 | CHECK_ERROR(net, AddPortForwardRule((BOOL)(*it).fPfrIPv6,
|
---|
[49114] | 509 | Bstr((*it).szPfrName).raw(),
|
---|
[48104] | 510 | proto,
|
---|
[49114] | 511 | Bstr((*it).szPfrHostAddr).raw(),
|
---|
[48104] | 512 | (*it).u16PfrHostPort,
|
---|
[49114] | 513 | Bstr((*it).szPfrGuestAddr).raw(),
|
---|
[48104] | 514 | (*it).u16PfrGuestPort));
|
---|
[95140] | 515 | if (FAILED(hrc))
|
---|
[92372] | 516 | return errorArgument(Nat::tr("Failed to add pf"));
|
---|
[48104] | 517 | }
|
---|
[45559] | 518 | }
|
---|
[48104] | 519 |
|
---|
| 520 | if (loopback6Offset)
|
---|
[48096] | 521 | {
|
---|
[48104] | 522 | if (loopback6Offset == -1)
|
---|
| 523 | loopback6Offset = 0; /* deletion */
|
---|
[48096] | 524 |
|
---|
[56118] | 525 | CHECK_ERROR_RET(net, COMSETTER(LoopbackIp6)(loopback6Offset), RTEXITCODE_FAILURE);
|
---|
[48104] | 526 | }
|
---|
[48096] | 527 |
|
---|
[48104] | 528 | /* addLocalMapping (hostid, offset) */
|
---|
| 529 | if (!vLoopback2Add.empty())
|
---|
| 530 | {
|
---|
[48405] | 531 | /* we're expecting stings 127.0.0.1=5 */
|
---|
[48104] | 532 | LOOPBACK2DELETEADDITERATOR it;
|
---|
| 533 | for (it = vLoopback2Add.begin();
|
---|
| 534 | it != vLoopback2Add.end();
|
---|
| 535 | ++it)
|
---|
| 536 | {
|
---|
| 537 | std::string address, strOffset;
|
---|
[53312] | 538 | size_t pos = it->find('=');
|
---|
[48104] | 539 | LONG lOffset = 0;
|
---|
| 540 | Bstr bstrAddress;
|
---|
[48096] | 541 |
|
---|
[92372] | 542 | AssertReturn(pos != std::string::npos, errorArgument(Nat::tr("invalid loopback string")));
|
---|
[48104] | 543 |
|
---|
| 544 | address = it->substr(0, pos);
|
---|
| 545 | strOffset = it->substr(pos + 1);
|
---|
| 546 |
|
---|
| 547 | lOffset = RTStrToUInt32(strOffset.c_str());
|
---|
[92372] | 548 | AssertReturn(lOffset > 0, errorArgument(Nat::tr("invalid loopback string")));
|
---|
[48104] | 549 |
|
---|
| 550 | bstrAddress = Bstr(address.c_str());
|
---|
| 551 |
|
---|
[56118] | 552 | CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), lOffset), RTEXITCODE_FAILURE);
|
---|
[48104] | 553 | }
|
---|
[48096] | 554 | }
|
---|
| 555 |
|
---|
[48104] | 556 | if (!vLoopback2Delete.empty())
|
---|
[48096] | 557 | {
|
---|
[48104] | 558 | /* we're expecting stings 127.0.0.1 */
|
---|
| 559 | LOOPBACK2DELETEADDITERATOR it;
|
---|
| 560 | for (it = vLoopback2Add.begin();
|
---|
| 561 | it != vLoopback2Add.end();
|
---|
| 562 | ++it)
|
---|
| 563 | {
|
---|
| 564 | Bstr bstrAddress;
|
---|
| 565 | bstrAddress = Bstr(it->c_str());
|
---|
[48096] | 566 |
|
---|
[56118] | 567 | CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), 0), RTEXITCODE_FAILURE);
|
---|
[48104] | 568 | }
|
---|
[48096] | 569 | }
|
---|
| 570 |
|
---|
[48104] | 571 | if (enable >= 0)
|
---|
| 572 | {
|
---|
| 573 | CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable));
|
---|
[95140] | 574 | if (FAILED(hrc))
|
---|
[92372] | 575 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
[48104] | 576 | }
|
---|
| 577 | break;
|
---|
| 578 | }
|
---|
| 579 | case OP_REMOVE:
|
---|
[45156] | 580 | {
|
---|
[48104] | 581 | CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net));
|
---|
[95140] | 582 | if (FAILED(hrc))
|
---|
[92372] | 583 | return errorArgument(Nat::tr("Failed to remove nat network"));
|
---|
[48104] | 584 | break;
|
---|
[45156] | 585 | }
|
---|
[48104] | 586 | case OP_START:
|
---|
| 587 | {
|
---|
[81883] | 588 | CHECK_ERROR(net, Start());
|
---|
[95140] | 589 | if (FAILED(hrc))
|
---|
[92372] | 590 | return errorArgument(Nat::tr("Failed to start network"));
|
---|
[48104] | 591 | break;
|
---|
| 592 | }
|
---|
| 593 | case OP_STOP:
|
---|
| 594 | {
|
---|
| 595 | CHECK_ERROR(net, Stop());
|
---|
[95140] | 596 | if (FAILED(hrc))
|
---|
[92372] | 597 | return errorArgument(Nat::tr("Failed to stop network"));
|
---|
[48104] | 598 | break;
|
---|
| 599 | }
|
---|
| 600 | default:;
|
---|
[45156] | 601 | }
|
---|
[56118] | 602 | return RTEXITCODE_SUCCESS;
|
---|
[45156] | 603 | }
|
---|
| 604 |
|
---|
| 605 |
|
---|
[93279] | 606 | /*
|
---|
| 607 | * VBoxManage natnetwork ...
|
---|
| 608 | */
|
---|
[56118] | 609 | RTEXITCODE handleNATNetwork(HandlerArg *a)
|
---|
[45156] | 610 | {
|
---|
| 611 | if (a->argc < 1)
|
---|
[94211] | 612 | return errorSyntax(Nat::tr("Not enough parameters"));
|
---|
[45156] | 613 |
|
---|
[56118] | 614 | RTEXITCODE rcExit;
|
---|
[45156] | 615 | if (strcmp(a->argv[0], "modify") == 0)
|
---|
[94211] | 616 | {
|
---|
| 617 | setCurrentSubcommand(HELP_SCOPE_NATNETWORK_MODIFY);
|
---|
[56118] | 618 | rcExit = handleOp(a, OP_MODIFY);
|
---|
[94211] | 619 | }
|
---|
[45156] | 620 | else if (strcmp(a->argv[0], "add") == 0)
|
---|
[94211] | 621 | {
|
---|
| 622 | setCurrentSubcommand(HELP_SCOPE_NATNETWORK_ADD);
|
---|
[56118] | 623 | rcExit = handleOp(a, OP_ADD);
|
---|
[94211] | 624 | }
|
---|
[45156] | 625 | else if (strcmp(a->argv[0], "remove") == 0)
|
---|
[94211] | 626 | {
|
---|
| 627 | setCurrentSubcommand(HELP_SCOPE_NATNETWORK_REMOVE);
|
---|
[56118] | 628 | rcExit = handleOp(a, OP_REMOVE);
|
---|
[94211] | 629 | }
|
---|
[45156] | 630 | else if (strcmp(a->argv[0], "start") == 0)
|
---|
[94211] | 631 | {
|
---|
| 632 | setCurrentSubcommand(HELP_SCOPE_NATNETWORK_START);
|
---|
[56118] | 633 | rcExit = handleOp(a, OP_START);
|
---|
[94211] | 634 | }
|
---|
[45156] | 635 | else if (strcmp(a->argv[0], "stop") == 0)
|
---|
[94211] | 636 | {
|
---|
| 637 | setCurrentSubcommand(HELP_SCOPE_NATNETWORK_STOP);
|
---|
[56118] | 638 | rcExit = handleOp(a, OP_STOP);
|
---|
[94211] | 639 | }
|
---|
[60501] | 640 | else if (strcmp(a->argv[0], "list") == 0)
|
---|
[94211] | 641 | {
|
---|
| 642 | setCurrentSubcommand(HELP_SCOPE_NATNETWORK_LIST);
|
---|
[60508] | 643 | rcExit = handleNATList(a);
|
---|
[94211] | 644 | }
|
---|
[45156] | 645 | else
|
---|
[94211] | 646 | rcExit = errorSyntax(Nat::tr("Invalid parameter '%s'"), a->argv[0]);
|
---|
[56118] | 647 | return rcExit;
|
---|
[45156] | 648 | }
|
---|
| 649 |
|
---|
[93279] | 650 |
|
---|
| 651 | /*
|
---|
| 652 | * VBoxManage list natnetworks ...
|
---|
| 653 | */
|
---|
| 654 | RTEXITCODE listNATNetworks(bool fLong, bool fSorted,
|
---|
| 655 | const ComPtr<IVirtualBox> &pVirtualBox)
|
---|
| 656 | {
|
---|
[95140] | 657 | HRESULT hrc;
|
---|
[93279] | 658 |
|
---|
[93287] | 659 | com::SafeIfaceArray<INATNetwork> aNets;
|
---|
| 660 | CHECK_ERROR_RET(pVirtualBox,
|
---|
| 661 | COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(aNets)),
|
---|
| 662 | RTEXITCODE_FAILURE);
|
---|
[93279] | 663 |
|
---|
[93287] | 664 | const size_t cNets = aNets.size();
|
---|
| 665 | if (cNets == 0)
|
---|
| 666 | return RTEXITCODE_SUCCESS;
|
---|
| 667 |
|
---|
| 668 | /*
|
---|
| 669 | * Sort the list if necessary. The sort is indirect via an
|
---|
| 670 | * intermediate array of indexes.
|
---|
| 671 | */
|
---|
| 672 | std::vector<size_t> vIndexes(cNets);
|
---|
| 673 | for (size_t i = 0; i < cNets; ++i)
|
---|
| 674 | vIndexes[i] = i;
|
---|
| 675 |
|
---|
| 676 | if (fSorted)
|
---|
[93279] | 677 | {
|
---|
[93287] | 678 | std::vector<com::Bstr> vBstrNames(cNets);
|
---|
| 679 | for (size_t i = 0; i < cNets; ++i)
|
---|
| 680 | {
|
---|
| 681 | CHECK_ERROR_RET(aNets[i],
|
---|
| 682 | COMGETTER(NetworkName)(vBstrNames[i].asOutParam()),
|
---|
| 683 | RTEXITCODE_FAILURE);
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | struct SortBy {
|
---|
| 687 | const std::vector<com::Bstr> &ks;
|
---|
| 688 | SortBy(const std::vector<com::Bstr> &aKeys) : ks(aKeys) {}
|
---|
| 689 | bool operator() (size_t l, size_t r) { return ks[l] < ks[r]; }
|
---|
| 690 | };
|
---|
| 691 |
|
---|
| 692 | std::sort(vIndexes.begin(), vIndexes.end(),
|
---|
| 693 | SortBy(vBstrNames));
|
---|
[93279] | 694 | }
|
---|
| 695 |
|
---|
[93287] | 696 | for (size_t i = 0; i < cNets; ++i)
|
---|
| 697 | {
|
---|
| 698 | printNATNetwork(aNets[vIndexes[i]], fLong);
|
---|
| 699 | }
|
---|
| 700 |
|
---|
[93279] | 701 | return RTEXITCODE_SUCCESS;
|
---|
| 702 | }
|
---|