1 | /* $Id: VBoxManageNATNetwork.cpp 93701 2022-02-11 18:58:31Z 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK, 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(USAGE_NATNETWORK,
|
---|
318 | Nat::tr("loopback couldn't be deleted on modified\n"));
|
---|
319 | if (c == 'L')
|
---|
320 | loopback6Offset = -1;
|
---|
321 | else
|
---|
322 | {
|
---|
323 | int vrc;
|
---|
324 | RTGETOPTUNION Addr2Delete;
|
---|
325 | vrc = RTGetOptFetchValue(&GetState,
|
---|
326 | &Addr2Delete,
|
---|
327 | RTGETOPT_REQ_STRING);
|
---|
328 | if (RT_FAILURE(vrc))
|
---|
329 | return errorSyntax(USAGE_NATNETWORK,
|
---|
330 | Nat::tr("Not enough parаmeters\n"));
|
---|
331 |
|
---|
332 | vLoopback2Delete.push_back(std::string(Addr2Delete.psz));
|
---|
333 | }
|
---|
334 | }
|
---|
335 | else
|
---|
336 | {
|
---|
337 | /* addition */
|
---|
338 | if (c == 'L')
|
---|
339 | loopback6Offset = ValueUnion.u32;
|
---|
340 | else
|
---|
341 | vLoopback2Add.push_back(std::string(ValueUnion.psz));
|
---|
342 | }
|
---|
343 | break;
|
---|
344 |
|
---|
345 | case 'P': /* ipv6 portforwarding*/
|
---|
346 | case 'p': /* ipv4 portforwarding */
|
---|
347 | {
|
---|
348 | if (RTStrCmp(ValueUnion.psz, "delete") != 0)
|
---|
349 | {
|
---|
350 | /* addition */
|
---|
351 | /* netPfStrToPf will clean up the Pfr */
|
---|
352 | PORTFORWARDRULE Pfr;
|
---|
353 | int irc = netPfStrToPf(ValueUnion.psz, (c == 'P'), &Pfr);
|
---|
354 | if (RT_FAILURE(irc))
|
---|
355 | return errorSyntax(USAGE_NATNETWORK, Nat::tr("Invalid port-forward rule %s\n"), ValueUnion.psz);
|
---|
356 |
|
---|
357 | vPf2Add.push_back(Pfr);
|
---|
358 | }
|
---|
359 | else
|
---|
360 | {
|
---|
361 | /* deletion */
|
---|
362 | if (enmCode != OP_MODIFY)
|
---|
363 | return errorSyntax(USAGE_NATNETWORK,
|
---|
364 | Nat::tr("Port-forward could be deleted on modify\n"));
|
---|
365 |
|
---|
366 | RTGETOPTUNION NamePf2DeleteUnion;
|
---|
367 | int vrc = RTGetOptFetchValue(&GetState, &NamePf2DeleteUnion, RTGETOPT_REQ_STRING);
|
---|
368 | if (RT_FAILURE(vrc))
|
---|
369 | return errorSyntax(USAGE_NATNETWORK, Nat::tr("Not enough parаmeters\n"));
|
---|
370 |
|
---|
371 | if (strlen(NamePf2DeleteUnion.psz) > PF_NAMELEN)
|
---|
372 | return errorSyntax(USAGE_NATNETWORK, Nat::tr("Port-forward rule name is too long\n"));
|
---|
373 |
|
---|
374 | PFNAME2DELETE Name2Delete;
|
---|
375 | RT_ZERO(Name2Delete);
|
---|
376 | RTStrCopy(Name2Delete.szName, PF_NAMELEN, NamePf2DeleteUnion.psz);
|
---|
377 | Name2Delete.fIPv6 = (c == 'P');
|
---|
378 | vPfName2Delete.push_back(Name2Delete);
|
---|
379 | }
|
---|
380 | break;
|
---|
381 | }
|
---|
382 |
|
---|
383 | default:
|
---|
384 | return errorGetOpt(USAGE_NATNETWORK, c, &ValueUnion);
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | if (!pNetName)
|
---|
389 | return errorSyntax(USAGE_NATNETWORK, Nat::tr("You need to specify the --netname option"));
|
---|
390 | /* verification */
|
---|
391 | switch (enmCode)
|
---|
392 | {
|
---|
393 | case OP_ADD:
|
---|
394 | if (!pPrefixIPv4)
|
---|
395 | return errorSyntax(USAGE_NATNETWORK, Nat::tr("You need to specify the --network option"));
|
---|
396 | break;
|
---|
397 | case OP_MODIFY:
|
---|
398 | case OP_REMOVE:
|
---|
399 | case OP_START:
|
---|
400 | case OP_STOP:
|
---|
401 | break;
|
---|
402 | default:
|
---|
403 | AssertMsgFailedReturn((Nat::tr("Unknown operation (:%d)"), enmCode), RTEXITCODE_FAILURE);
|
---|
404 | }
|
---|
405 |
|
---|
406 | HRESULT rc;
|
---|
407 | Bstr NetName;
|
---|
408 | NetName = Bstr(pNetName);
|
---|
409 |
|
---|
410 | ComPtr<INATNetwork> net;
|
---|
411 | rc = a->virtualBox->FindNATNetworkByName(NetName.mutableRaw(), net.asOutParam());
|
---|
412 | if (enmCode == OP_ADD)
|
---|
413 | {
|
---|
414 | if (SUCCEEDED(rc))
|
---|
415 | return errorArgument(Nat::tr("NATNetwork server already exists"));
|
---|
416 |
|
---|
417 | CHECK_ERROR(a->virtualBox, CreateNATNetwork(NetName.raw(), net.asOutParam()));
|
---|
418 | if (FAILED(rc))
|
---|
419 | return errorArgument(Nat::tr("Failed to create the NAT network service"));
|
---|
420 | }
|
---|
421 | else if (FAILED(rc))
|
---|
422 | return errorArgument(Nat::tr("NATNetwork server does not exist"));
|
---|
423 |
|
---|
424 | switch (enmCode)
|
---|
425 | {
|
---|
426 | case OP_ADD:
|
---|
427 | case OP_MODIFY:
|
---|
428 | {
|
---|
429 | if (pPrefixIPv4)
|
---|
430 | {
|
---|
431 | CHECK_ERROR(net, COMSETTER(Network)(Bstr(pPrefixIPv4).raw()));
|
---|
432 | if (FAILED(rc))
|
---|
433 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
434 | }
|
---|
435 | if (dhcp >= 0)
|
---|
436 | {
|
---|
437 | CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp));
|
---|
438 | if (FAILED(rc))
|
---|
439 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*
|
---|
443 | * If we are asked to disable IPv6, do it early so that
|
---|
444 | * the same command can also set IPv6 prefix to empty if
|
---|
445 | * it so wishes.
|
---|
446 | */
|
---|
447 | if (ipv6 == 0)
|
---|
448 | {
|
---|
449 | CHECK_ERROR(net, COMSETTER(IPv6Enabled)(FALSE));
|
---|
450 | if (FAILED(rc))
|
---|
451 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
452 | }
|
---|
453 |
|
---|
454 | if (pPrefixIPv6)
|
---|
455 | {
|
---|
456 | CHECK_ERROR(net, COMSETTER(IPv6Prefix)(Bstr(pPrefixIPv6).raw()));
|
---|
457 | if (FAILED(rc))
|
---|
458 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
459 | }
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * If we are asked to enable IPv6, do it late, so that the
|
---|
463 | * same command can also set IPv6 prefix.
|
---|
464 | */
|
---|
465 | if (ipv6 > 0)
|
---|
466 | {
|
---|
467 | CHECK_ERROR(net, COMSETTER(IPv6Enabled)(TRUE));
|
---|
468 | if (FAILED(rc))
|
---|
469 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
470 | }
|
---|
471 |
|
---|
472 | if (ipv6_default != -1)
|
---|
473 | {
|
---|
474 | BOOL fIPv6Default = RT_BOOL(ipv6_default);
|
---|
475 | CHECK_ERROR(net, COMSETTER(AdvertiseDefaultIPv6RouteEnabled)(fIPv6Default));
|
---|
476 | if (FAILED(rc))
|
---|
477 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
478 | }
|
---|
479 |
|
---|
480 | if (!vPfName2Delete.empty())
|
---|
481 | {
|
---|
482 | VPF2DELETEITERATOR it;
|
---|
483 | for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it)
|
---|
484 | {
|
---|
485 | CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6,
|
---|
486 | Bstr((*it).szName).raw()));
|
---|
487 | if (FAILED(rc))
|
---|
488 | return errorArgument(Nat::tr("Failed to delete pf"));
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | if (!vPf2Add.empty())
|
---|
493 | {
|
---|
494 | VPF2ADDITERATOR it;
|
---|
495 | for (it = vPf2Add.begin(); it != vPf2Add.end(); ++it)
|
---|
496 | {
|
---|
497 | NATProtocol_T proto = NATProtocol_TCP;
|
---|
498 | if ((*it).iPfrProto == IPPROTO_TCP)
|
---|
499 | proto = NATProtocol_TCP;
|
---|
500 | else if ((*it).iPfrProto == IPPROTO_UDP)
|
---|
501 | proto = NATProtocol_UDP;
|
---|
502 | else
|
---|
503 | continue; /* XXX: warning here. */
|
---|
504 |
|
---|
505 | CHECK_ERROR(net, AddPortForwardRule((BOOL)(*it).fPfrIPv6,
|
---|
506 | Bstr((*it).szPfrName).raw(),
|
---|
507 | proto,
|
---|
508 | Bstr((*it).szPfrHostAddr).raw(),
|
---|
509 | (*it).u16PfrHostPort,
|
---|
510 | Bstr((*it).szPfrGuestAddr).raw(),
|
---|
511 | (*it).u16PfrGuestPort));
|
---|
512 | if (FAILED(rc))
|
---|
513 | return errorArgument(Nat::tr("Failed to add pf"));
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | if (loopback6Offset)
|
---|
518 | {
|
---|
519 | if (loopback6Offset == -1)
|
---|
520 | loopback6Offset = 0; /* deletion */
|
---|
521 |
|
---|
522 | CHECK_ERROR_RET(net, COMSETTER(LoopbackIp6)(loopback6Offset), RTEXITCODE_FAILURE);
|
---|
523 | }
|
---|
524 |
|
---|
525 | /* addLocalMapping (hostid, offset) */
|
---|
526 | if (!vLoopback2Add.empty())
|
---|
527 | {
|
---|
528 | /* we're expecting stings 127.0.0.1=5 */
|
---|
529 | LOOPBACK2DELETEADDITERATOR it;
|
---|
530 | for (it = vLoopback2Add.begin();
|
---|
531 | it != vLoopback2Add.end();
|
---|
532 | ++it)
|
---|
533 | {
|
---|
534 | std::string address, strOffset;
|
---|
535 | size_t pos = it->find('=');
|
---|
536 | LONG lOffset = 0;
|
---|
537 | Bstr bstrAddress;
|
---|
538 |
|
---|
539 | AssertReturn(pos != std::string::npos, errorArgument(Nat::tr("invalid loopback string")));
|
---|
540 |
|
---|
541 | address = it->substr(0, pos);
|
---|
542 | strOffset = it->substr(pos + 1);
|
---|
543 |
|
---|
544 | lOffset = RTStrToUInt32(strOffset.c_str());
|
---|
545 | AssertReturn(lOffset > 0, errorArgument(Nat::tr("invalid loopback string")));
|
---|
546 |
|
---|
547 | bstrAddress = Bstr(address.c_str());
|
---|
548 |
|
---|
549 | CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), lOffset), RTEXITCODE_FAILURE);
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | if (!vLoopback2Delete.empty())
|
---|
554 | {
|
---|
555 | /* we're expecting stings 127.0.0.1 */
|
---|
556 | LOOPBACK2DELETEADDITERATOR it;
|
---|
557 | for (it = vLoopback2Add.begin();
|
---|
558 | it != vLoopback2Add.end();
|
---|
559 | ++it)
|
---|
560 | {
|
---|
561 | Bstr bstrAddress;
|
---|
562 | bstrAddress = Bstr(it->c_str());
|
---|
563 |
|
---|
564 | CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), 0), RTEXITCODE_FAILURE);
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | if (enable >= 0)
|
---|
569 | {
|
---|
570 | CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable));
|
---|
571 | if (FAILED(rc))
|
---|
572 | return errorArgument(Nat::tr("Failed to set configuration"));
|
---|
573 | }
|
---|
574 | break;
|
---|
575 | }
|
---|
576 | case OP_REMOVE:
|
---|
577 | {
|
---|
578 | CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net));
|
---|
579 | if (FAILED(rc))
|
---|
580 | return errorArgument(Nat::tr("Failed to remove nat network"));
|
---|
581 | break;
|
---|
582 | }
|
---|
583 | case OP_START:
|
---|
584 | {
|
---|
585 | CHECK_ERROR(net, Start());
|
---|
586 | if (FAILED(rc))
|
---|
587 | return errorArgument(Nat::tr("Failed to start network"));
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | case OP_STOP:
|
---|
591 | {
|
---|
592 | CHECK_ERROR(net, Stop());
|
---|
593 | if (FAILED(rc))
|
---|
594 | return errorArgument(Nat::tr("Failed to stop network"));
|
---|
595 | break;
|
---|
596 | }
|
---|
597 | default:;
|
---|
598 | }
|
---|
599 | return RTEXITCODE_SUCCESS;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | /*
|
---|
604 | * VBoxManage natnetwork ...
|
---|
605 | */
|
---|
606 | RTEXITCODE handleNATNetwork(HandlerArg *a)
|
---|
607 | {
|
---|
608 | if (a->argc < 1)
|
---|
609 | return errorSyntax(USAGE_NATNETWORK, Nat::tr("Not enough parameters"));
|
---|
610 |
|
---|
611 | RTEXITCODE rcExit;
|
---|
612 | if (strcmp(a->argv[0], "modify") == 0)
|
---|
613 | rcExit = handleOp(a, OP_MODIFY);
|
---|
614 | else if (strcmp(a->argv[0], "add") == 0)
|
---|
615 | rcExit = handleOp(a, OP_ADD);
|
---|
616 | else if (strcmp(a->argv[0], "remove") == 0)
|
---|
617 | rcExit = handleOp(a, OP_REMOVE);
|
---|
618 | else if (strcmp(a->argv[0], "start") == 0)
|
---|
619 | rcExit = handleOp(a, OP_START);
|
---|
620 | else if (strcmp(a->argv[0], "stop") == 0)
|
---|
621 | rcExit = handleOp(a, OP_STOP);
|
---|
622 | else if (strcmp(a->argv[0], "list") == 0)
|
---|
623 | rcExit = handleNATList(a);
|
---|
624 | else
|
---|
625 | rcExit = errorSyntax(USAGE_NATNETWORK, Nat::tr("Invalid parameter '%s'"), a->argv[0]);
|
---|
626 | return rcExit;
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | /*
|
---|
631 | * VBoxManage list natnetworks ...
|
---|
632 | */
|
---|
633 | RTEXITCODE listNATNetworks(bool fLong, bool fSorted,
|
---|
634 | const ComPtr<IVirtualBox> &pVirtualBox)
|
---|
635 | {
|
---|
636 | HRESULT rc;
|
---|
637 |
|
---|
638 | com::SafeIfaceArray<INATNetwork> aNets;
|
---|
639 | CHECK_ERROR_RET(pVirtualBox,
|
---|
640 | COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(aNets)),
|
---|
641 | RTEXITCODE_FAILURE);
|
---|
642 |
|
---|
643 | const size_t cNets = aNets.size();
|
---|
644 | if (cNets == 0)
|
---|
645 | return RTEXITCODE_SUCCESS;
|
---|
646 |
|
---|
647 | /*
|
---|
648 | * Sort the list if necessary. The sort is indirect via an
|
---|
649 | * intermediate array of indexes.
|
---|
650 | */
|
---|
651 | std::vector<size_t> vIndexes(cNets);
|
---|
652 | for (size_t i = 0; i < cNets; ++i)
|
---|
653 | vIndexes[i] = i;
|
---|
654 |
|
---|
655 | if (fSorted)
|
---|
656 | {
|
---|
657 | std::vector<com::Bstr> vBstrNames(cNets);
|
---|
658 | for (size_t i = 0; i < cNets; ++i)
|
---|
659 | {
|
---|
660 | CHECK_ERROR_RET(aNets[i],
|
---|
661 | COMGETTER(NetworkName)(vBstrNames[i].asOutParam()),
|
---|
662 | RTEXITCODE_FAILURE);
|
---|
663 | }
|
---|
664 |
|
---|
665 | struct SortBy {
|
---|
666 | const std::vector<com::Bstr> &ks;
|
---|
667 | SortBy(const std::vector<com::Bstr> &aKeys) : ks(aKeys) {}
|
---|
668 | bool operator() (size_t l, size_t r) { return ks[l] < ks[r]; }
|
---|
669 | };
|
---|
670 |
|
---|
671 | std::sort(vIndexes.begin(), vIndexes.end(),
|
---|
672 | SortBy(vBstrNames));
|
---|
673 | }
|
---|
674 |
|
---|
675 | for (size_t i = 0; i < cNets; ++i)
|
---|
676 | {
|
---|
677 | printNATNetwork(aNets[vIndexes[i]], fLong);
|
---|
678 | }
|
---|
679 |
|
---|
680 | return RTEXITCODE_SUCCESS;
|
---|
681 | }
|
---|
682 |
|
---|
683 | #endif /* !VBOX_ONLY_DOCS */
|
---|