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