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