VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageNATNetwork.cpp

Last change on this file was 106061, checked in by vboxsync, 8 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.7 KB
Line 
1/* $Id: VBoxManageNATNetwork.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of NAT Network command command.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
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
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
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
38#ifndef RT_OS_WINDOWS
39# include <netinet/in.h>
40#else
41/* from <ws2ipdef.h> */
42# define INET6_ADDRSTRLEN 65
43#endif
44
45#define IPv6
46
47#include <iprt/cdefs.h>
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
59#include <algorithm>
60#include <vector>
61#include <iprt/sanitized/string>
62
63#include "VBoxManage.h"
64#include "VBoxPortForwardString.h"
65
66
67DECLARE_TRANSLATION_CONTEXT(Nat);
68
69using namespace com;
70
71typedef enum
72{
73 OP_ADD = 1000,
74 OP_REMOVE,
75 OP_MODIFY,
76 OP_START,
77 OP_STOP
78} OPCODE;
79
80typedef struct PFNAME2DELETE
81{
82 char szName[PF_NAMELEN];
83 bool fIPv6;
84} PFNAME2DELETE, *PPFNAME2DELETE;
85
86typedef std::vector<PFNAME2DELETE> VPF2DELETE;
87typedef VPF2DELETE::const_iterator VPF2DELETEITERATOR;
88
89typedef std::vector<PORTFORWARDRULE> VPF2ADD;
90typedef VPF2ADD::const_iterator VPF2ADDITERATOR;
91
92typedef std::vector<std::string> LOOPBACK2DELETEADD;
93typedef LOOPBACK2DELETEADD::iterator LOOPBACK2DELETEADDITERATOR;
94
95static HRESULT printNATNetwork(const ComPtr<INATNetwork> &pNATNet,
96 bool fLong = true)
97{
98 HRESULT hrc;
99
100 do
101 {
102 Bstr strVal;
103 BOOL fVal;
104
105 CHECK_ERROR_BREAK(pNATNet, COMGETTER(NetworkName)(strVal.asOutParam()));
106 RTPrintf(Nat::tr("Name: %ls\n"), strVal.raw());
107
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
119 CHECK_ERROR_BREAK(pNATNet, COMGETTER(Network)(strVal.asOutParam()));
120 RTPrintf(Nat::tr("Network: %ls\n"), strVal.raw());
121
122 CHECK_ERROR_BREAK(pNATNet, COMGETTER(Gateway)(strVal.asOutParam()));
123 RTPrintf(Nat::tr("Gateway: %ls\n"), strVal.raw());
124
125 CHECK_ERROR_BREAK(pNATNet, COMGETTER(NeedDhcpServer)(&fVal));
126 RTPrintf(Nat::tr("DHCP Server: %s\n"), fVal ? Nat::tr("Yes") : Nat::tr("No"));
127
128 CHECK_ERROR_BREAK(pNATNet, COMGETTER(IPv6Enabled)(&fVal));
129 RTPrintf("IPv6: %s\n", fVal ? Nat::tr("Yes") : Nat::tr("No"));
130
131 CHECK_ERROR_BREAK(pNATNet, COMGETTER(IPv6Prefix)(strVal.asOutParam()));
132 RTPrintf(Nat::tr("IPv6 Prefix: %ls\n"), strVal.raw());
133
134 CHECK_ERROR_BREAK(pNATNet, COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fVal));
135 RTPrintf(Nat::tr("IPv6 Default: %s\n"), fVal ? Nat::tr("Yes") : Nat::tr("No"));
136
137
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
166 RTPrintf("\n");
167 } while (0);
168
169 return hrc;
170}
171
172static RTEXITCODE handleNATList(HandlerArg *a)
173{
174 HRESULT hrc;
175
176 RTPrintf(Nat::tr("NAT Networks:\n\n"));
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
195 Utf8Str strValUTF8(strVal);
196 if (!RTStrSimplePatternMatch(pszFilter, strValUTF8.c_str()))
197 continue;
198 }
199
200 hrc = printNATNetwork(pNATNet);
201 if (FAILED(hrc))
202 break;
203
204 cFound++;
205 }
206
207 if (SUCCEEDED(hrc))
208 RTPrintf(Nat::tr("%zu %s found\n"), cFound, cFound == 1 ? Nat::tr("network") : Nat::tr("networks", "", cFound));
209
210 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
211}
212
213static RTEXITCODE handleOp(HandlerArg *a, OPCODE enmCode)
214{
215 if (a->argc - 1 <= 1)
216 return errorSyntax(Nat::tr("Not enough parameters"));
217
218 const char *pNetName = NULL;
219 const char *pPrefixIPv4 = NULL;
220 const char *pPrefixIPv6 = NULL;
221 int enable = -1;
222 int dhcp = -1;
223 int ipv6 = -1;
224 int ipv6_default = -1;
225
226 VPF2DELETE vPfName2Delete;
227 VPF2ADD vPf2Add;
228
229 LOOPBACK2DELETEADD vLoopback2Delete;
230 LOOPBACK2DELETEADD vLoopback2Add;
231
232 LONG loopback6Offset = 0; /* ignore me */
233
234 enum
235 {
236 kNATNetworkIota = 1000,
237 kNATNetwork_IPv6Default,
238 kNATNetwork_IPv6Prefix,
239 };
240
241 static const RTGETOPTDEF g_aNATNetworkIPOptions[] =
242 {
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 */
248 { "--ipv6-default", kNATNetwork_IPv6Default, RTGETOPT_REQ_BOOL },
249 { "--ipv6-enable", '6', RTGETOPT_REQ_BOOL }, /* new name */
250 { "--ipv6-prefix", kNATNetwork_IPv6Prefix, RTGETOPT_REQ_STRING },
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 },
257 };
258
259 int c;
260 RTGETOPTUNION ValueUnion;
261 RTGETOPTSTATE GetState;
262 RTGetOptInit(&GetState, a->argc, a->argv, g_aNATNetworkIPOptions,
263 enmCode != OP_REMOVE ? RT_ELEMENTS(g_aNATNetworkIPOptions) : 4, /* we use only --netname and --ifname for remove*/
264 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
265 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
266 {
267 switch (c)
268 {
269 case 't': // --netname
270 if (pNetName)
271 return errorSyntax(Nat::tr("You can only specify --netname only once."));
272 pNetName = ValueUnion.psz;
273 break;
274
275 case 'n': // --network
276 if (pPrefixIPv4)
277 return errorSyntax(Nat::tr("You can only specify --network only once."));
278 pPrefixIPv4 = ValueUnion.psz;
279 break;
280
281 case 'e': // --enable
282 if (enable >= 0)
283 return errorSyntax(Nat::tr("You can specify either --enable or --disable once."));
284 enable = 1;
285 break;
286
287 case 'd': // --disable
288 if (enable >= 0)
289 return errorSyntax(Nat::tr("You can specify either --enable or --disable once."));
290 enable = 0;
291 break;
292
293 case 'h':
294 if (dhcp != -1)
295 return errorSyntax(Nat::tr("You can specify --dhcp only once."));
296 dhcp = ValueUnion.f;
297 break;
298
299 case '6':
300 if (ipv6 != -1)
301 return errorSyntax(Nat::tr("You can specify --ipv6 only once."));
302 ipv6 = ValueUnion.f;
303 break;
304
305 case kNATNetwork_IPv6Prefix:
306 if (pPrefixIPv6)
307 return errorSyntax(Nat::tr("You can specify --ipv6-prefix only once."));
308 pPrefixIPv6 = ValueUnion.psz;
309 break;
310
311 case kNATNetwork_IPv6Default: // XXX: uwe
312 if (ipv6_default != -1)
313 return errorSyntax(Nat::tr("You can specify --ipv6-default only once."));
314 ipv6_default = ValueUnion.f;
315 break;
316
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)
323 errorSyntax(Nat::tr("loopback couldn't be deleted on modified\n"));
324 if (c == 'L')
325 loopback6Offset = -1;
326 else
327 {
328 int vrc;
329 RTGETOPTUNION Addr2Delete;
330 vrc = RTGetOptFetchValue(&GetState,
331 &Addr2Delete,
332 RTGETOPT_REQ_STRING);
333 if (RT_FAILURE(vrc))
334 return errorSyntax(Nat::tr("Not enough parаmeters\n"));
335
336 vLoopback2Delete.push_back(std::string(Addr2Delete.psz));
337 }
338 }
339 else
340 {
341 /* addition */
342 if (c == 'L')
343 loopback6Offset = ValueUnion.u32;
344 else
345 vLoopback2Add.push_back(std::string(ValueUnion.psz));
346 }
347 break;
348
349 case 'P': /* ipv6 portforwarding*/
350 case 'p': /* ipv4 portforwarding */
351 {
352 if (RTStrCmp(ValueUnion.psz, "delete") != 0)
353 {
354 /* addition */
355 /* netPfStrToPf will clean up the Pfr */
356 PORTFORWARDRULE Pfr;
357 int irc = netPfStrToPf(ValueUnion.psz, (c == 'P'), &Pfr);
358 if (RT_FAILURE(irc))
359 return errorSyntax(Nat::tr("Invalid port-forward rule %s\n"), ValueUnion.psz);
360
361 vPf2Add.push_back(Pfr);
362 }
363 else
364 {
365 /* deletion */
366 if (enmCode != OP_MODIFY)
367 return errorSyntax(Nat::tr("Port-forward could be deleted on modify\n"));
368
369 RTGETOPTUNION NamePf2DeleteUnion;
370 int vrc = RTGetOptFetchValue(&GetState, &NamePf2DeleteUnion, RTGETOPT_REQ_STRING);
371 if (RT_FAILURE(vrc))
372 return errorSyntax(Nat::tr("Not enough parаmeters\n"));
373
374 if (strlen(NamePf2DeleteUnion.psz) > PF_NAMELEN)
375 return errorSyntax(Nat::tr("Port-forward rule name is too long\n"));
376
377 PFNAME2DELETE Name2Delete;
378 RT_ZERO(Name2Delete);
379 RTStrCopy(Name2Delete.szName, PF_NAMELEN, NamePf2DeleteUnion.psz);
380 Name2Delete.fIPv6 = (c == 'P');
381 vPfName2Delete.push_back(Name2Delete);
382 }
383 break;
384 }
385
386 default:
387 return errorGetOpt(c, &ValueUnion);
388 }
389 }
390
391 if (!pNetName)
392 return errorSyntax(Nat::tr("You need to specify the --netname option"));
393 /* verification */
394 switch (enmCode)
395 {
396 case OP_ADD:
397 if (!pPrefixIPv4)
398 return errorSyntax(Nat::tr("You need to specify the --network option"));
399 break;
400 case OP_MODIFY:
401 case OP_REMOVE:
402 case OP_START:
403 case OP_STOP:
404 break;
405 default:
406 AssertMsgFailedReturn((Nat::tr("Unknown operation (:%d)"), enmCode), RTEXITCODE_FAILURE);
407 }
408
409 HRESULT hrc;
410 Bstr NetName;
411 NetName = Bstr(pNetName);
412
413 ComPtr<INATNetwork> net;
414 hrc = a->virtualBox->FindNATNetworkByName(NetName.mutableRaw(), net.asOutParam());
415 if (enmCode == OP_ADD)
416 {
417 if (SUCCEEDED(hrc))
418 return errorArgument(Nat::tr("NATNetwork server already exists"));
419
420 CHECK_ERROR(a->virtualBox, CreateNATNetwork(NetName.raw(), net.asOutParam()));
421 if (FAILED(hrc))
422 return errorArgument(Nat::tr("Failed to create the NAT network service"));
423 }
424 else if (FAILED(hrc))
425 return errorArgument(Nat::tr("NATNetwork server does not exist"));
426
427 switch (enmCode)
428 {
429 case OP_ADD:
430 case OP_MODIFY:
431 {
432 if (pPrefixIPv4)
433 {
434 CHECK_ERROR(net, COMSETTER(Network)(Bstr(pPrefixIPv4).raw()));
435 if (FAILED(hrc))
436 return errorArgument(Nat::tr("Failed to set configuration"));
437 }
438 if (dhcp >= 0)
439 {
440 CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp));
441 if (FAILED(hrc))
442 return errorArgument(Nat::tr("Failed to set configuration"));
443 }
444
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)
451 {
452 CHECK_ERROR(net, COMSETTER(IPv6Enabled)(FALSE));
453 if (FAILED(hrc))
454 return errorArgument(Nat::tr("Failed to set configuration"));
455 }
456
457 if (pPrefixIPv6)
458 {
459 CHECK_ERROR(net, COMSETTER(IPv6Prefix)(Bstr(pPrefixIPv6).raw()));
460 if (FAILED(hrc))
461 return errorArgument(Nat::tr("Failed to set configuration"));
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));
471 if (FAILED(hrc))
472 return errorArgument(Nat::tr("Failed to set configuration"));
473 }
474
475 if (ipv6_default != -1)
476 {
477 BOOL fIPv6Default = RT_BOOL(ipv6_default);
478 CHECK_ERROR(net, COMSETTER(AdvertiseDefaultIPv6RouteEnabled)(fIPv6Default));
479 if (FAILED(hrc))
480 return errorArgument(Nat::tr("Failed to set configuration"));
481 }
482
483 if (!vPfName2Delete.empty())
484 {
485 VPF2DELETEITERATOR it;
486 for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it)
487 {
488 CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6,
489 Bstr((*it).szName).raw()));
490 if (FAILED(hrc))
491 return errorArgument(Nat::tr("Failed to delete pf"));
492 }
493 }
494
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. */
507
508 CHECK_ERROR(net, AddPortForwardRule((BOOL)(*it).fPfrIPv6,
509 Bstr((*it).szPfrName).raw(),
510 proto,
511 Bstr((*it).szPfrHostAddr).raw(),
512 (*it).u16PfrHostPort,
513 Bstr((*it).szPfrGuestAddr).raw(),
514 (*it).u16PfrGuestPort));
515 if (FAILED(hrc))
516 return errorArgument(Nat::tr("Failed to add pf"));
517 }
518 }
519
520 if (loopback6Offset)
521 {
522 if (loopback6Offset == -1)
523 loopback6Offset = 0; /* deletion */
524
525 CHECK_ERROR_RET(net, COMSETTER(LoopbackIp6)(loopback6Offset), RTEXITCODE_FAILURE);
526 }
527
528 /* addLocalMapping (hostid, offset) */
529 if (!vLoopback2Add.empty())
530 {
531 /* we're expecting stings 127.0.0.1=5 */
532 LOOPBACK2DELETEADDITERATOR it;
533 for (it = vLoopback2Add.begin();
534 it != vLoopback2Add.end();
535 ++it)
536 {
537 std::string address, strOffset;
538 size_t pos = it->find('=');
539 LONG lOffset = 0;
540 Bstr bstrAddress;
541
542 AssertReturn(pos != std::string::npos, errorArgument(Nat::tr("invalid loopback string")));
543
544 address = it->substr(0, pos);
545 strOffset = it->substr(pos + 1);
546
547 lOffset = RTStrToUInt32(strOffset.c_str());
548 AssertReturn(lOffset > 0, errorArgument(Nat::tr("invalid loopback string")));
549
550 bstrAddress = Bstr(address.c_str());
551
552 CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), lOffset), RTEXITCODE_FAILURE);
553 }
554 }
555
556 if (!vLoopback2Delete.empty())
557 {
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());
566
567 CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), 0), RTEXITCODE_FAILURE);
568 }
569 }
570
571 if (enable >= 0)
572 {
573 CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable));
574 if (FAILED(hrc))
575 return errorArgument(Nat::tr("Failed to set configuration"));
576 }
577 break;
578 }
579 case OP_REMOVE:
580 {
581 CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net));
582 if (FAILED(hrc))
583 return errorArgument(Nat::tr("Failed to remove nat network"));
584 break;
585 }
586 case OP_START:
587 {
588 CHECK_ERROR(net, Start());
589 if (FAILED(hrc))
590 return errorArgument(Nat::tr("Failed to start network"));
591 break;
592 }
593 case OP_STOP:
594 {
595 CHECK_ERROR(net, Stop());
596 if (FAILED(hrc))
597 return errorArgument(Nat::tr("Failed to stop network"));
598 break;
599 }
600 default:;
601 }
602 return RTEXITCODE_SUCCESS;
603}
604
605
606/*
607 * VBoxManage natnetwork ...
608 */
609RTEXITCODE handleNATNetwork(HandlerArg *a)
610{
611 if (a->argc < 1)
612 return errorSyntax(Nat::tr("Not enough parameters"));
613
614 RTEXITCODE rcExit;
615 if (strcmp(a->argv[0], "modify") == 0)
616 {
617 setCurrentSubcommand(HELP_SCOPE_NATNETWORK_MODIFY);
618 rcExit = handleOp(a, OP_MODIFY);
619 }
620 else if (strcmp(a->argv[0], "add") == 0)
621 {
622 setCurrentSubcommand(HELP_SCOPE_NATNETWORK_ADD);
623 rcExit = handleOp(a, OP_ADD);
624 }
625 else if (strcmp(a->argv[0], "remove") == 0)
626 {
627 setCurrentSubcommand(HELP_SCOPE_NATNETWORK_REMOVE);
628 rcExit = handleOp(a, OP_REMOVE);
629 }
630 else if (strcmp(a->argv[0], "start") == 0)
631 {
632 setCurrentSubcommand(HELP_SCOPE_NATNETWORK_START);
633 rcExit = handleOp(a, OP_START);
634 }
635 else if (strcmp(a->argv[0], "stop") == 0)
636 {
637 setCurrentSubcommand(HELP_SCOPE_NATNETWORK_STOP);
638 rcExit = handleOp(a, OP_STOP);
639 }
640 else if (strcmp(a->argv[0], "list") == 0)
641 {
642 setCurrentSubcommand(HELP_SCOPE_NATNETWORK_LIST);
643 rcExit = handleNATList(a);
644 }
645 else
646 rcExit = errorSyntax(Nat::tr("Invalid parameter '%s'"), a->argv[0]);
647 return rcExit;
648}
649
650
651/*
652 * VBoxManage list natnetworks ...
653 */
654RTEXITCODE listNATNetworks(bool fLong, bool fSorted,
655 const ComPtr<IVirtualBox> &pVirtualBox)
656{
657 HRESULT hrc;
658
659 com::SafeIfaceArray<INATNetwork> aNets;
660 CHECK_ERROR_RET(pVirtualBox,
661 COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(aNets)),
662 RTEXITCODE_FAILURE);
663
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)
677 {
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));
694 }
695
696 for (size_t i = 0; i < cNets; ++i)
697 {
698 printNATNetwork(aNets[vIndexes[i]], fLong);
699 }
700
701 return RTEXITCODE_SUCCESS;
702}
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