VirtualBox

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

Last change on this file since 45190 was 45156, checked in by vboxsync, 12 years ago

Frontends/VBoxManage: listing/manipulating nat networks if VBOX_WITH_NAT_SERVICE is defined.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/* $Id: VBoxManageNATNetwork.cpp 45156 2013-03-25 05:50:09Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of NAT Network command command.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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* Header Files *
20*******************************************************************************/
21#ifndef VBOX_ONLY_DOCS
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/EventQueue.h>
27
28#include <VBox/com/VirtualBox.h>
29#endif /* !VBOX_ONLY_DOCS */
30
31#include <iprt/cidr.h>
32#include <iprt/param.h>
33#include <iprt/path.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/net.h>
37#include <iprt/getopt.h>
38#include <iprt/ctype.h>
39
40#include <VBox/log.h>
41
42#include "VBoxManage.h"
43
44#ifndef VBOX_ONLY_DOCS
45using namespace com;
46
47typedef enum enMainOpCodes
48{
49 OP_ADD = 1000,
50 OP_REMOVE,
51 OP_MODIFY,
52 OP_START,
53 OP_STOP
54} OPCODE;
55
56static const RTGETOPTDEF g_aNATNetworkIPOptions[]
57 = {
58 { "--netname", 't', RTGETOPT_REQ_STRING },
59 { "--network", 'n', RTGETOPT_REQ_STRING },
60 { "--dhcp", 'h', RTGETOPT_REQ_BOOL },
61 { "--ipv6", '6', RTGETOPT_REQ_BOOL},
62 { "--enable", 'e', RTGETOPT_REQ_NOTHING },
63 { "--disable", 'd', RTGETOPT_REQ_NOTHING },
64
65 };
66
67static int handleOp(HandlerArg *a, OPCODE enmCode, int iStart, int *pcProcessed)
68{
69 if (a->argc - iStart < 2)
70 return errorSyntax(USAGE_NATNETWORK, "Not enough parameters");
71
72 int index = iStart;
73 HRESULT rc;
74
75 const char *pNetName = NULL;
76 const char *pNetworkCidr = NULL;
77 int enable = -1;
78 int dhcp = -1;
79 int ipv6 = -1;
80
81 int c;
82 RTGETOPTUNION ValueUnion;
83 RTGETOPTSTATE GetState;
84 RTGetOptInit(&GetState,
85 a->argc,
86 a->argv,
87 g_aNATNetworkIPOptions,
88 enmCode != OP_REMOVE ? RT_ELEMENTS(g_aNATNetworkIPOptions) : 4, /* we use only --netname and --ifname for remove*/
89 index,
90 RTGETOPTINIT_FLAGS_NO_STD_OPTS);
91 while ((c = RTGetOpt(&GetState, &ValueUnion)))
92 {
93 switch (c)
94 {
95 case 't': // --netname
96 if(pNetName)
97 return errorSyntax(USAGE_NATNETWORK, "You can only specify --netname once.");
98 else
99 {
100 pNetName = ValueUnion.psz;
101 }
102 break;
103 case 'n': // --network
104 if(pNetworkCidr)
105 return errorSyntax(USAGE_NATNETWORK, "You can only specify --network once.");
106 else
107 {
108 pNetworkCidr = ValueUnion.psz;
109 }
110 break;
111 case 'e': // --enable
112 if(enable >= 0)
113 return errorSyntax(USAGE_NATNETWORK, "You can specify either --enable or --disable once.");
114 else
115 {
116 enable = 1;
117 }
118 break;
119 case 'd': // --disable
120 if(enable >= 0)
121 return errorSyntax(USAGE_NATNETWORK, "You can specify either --enable or --disable once.");
122 else
123 {
124 enable = 0;
125 }
126 break;
127 case VINF_GETOPT_NOT_OPTION:
128 return errorSyntax(USAGE_NATNETWORK, "unhandled parameter: %s", ValueUnion.psz);
129 break;
130 case 'h':
131 if (dhcp != -1)
132 return errorSyntax(USAGE_NATNETWORK, "You can specify --dhcp once.");
133 dhcp = ValueUnion.f;
134 break;
135 case '6':
136 if (ipv6 != -1)
137 return errorSyntax(USAGE_NATNETWORK, "You can specify --ipv6 once.");
138 ipv6 = ValueUnion.f;
139 break;
140 default:
141 if (c > 0)
142 {
143 if (RT_C_IS_GRAPH(c))
144 return errorSyntax(USAGE_NATNETWORK, "unhandled option: -%c", c);
145 else
146 return errorSyntax(USAGE_NATNETWORK, "unhandled option: %i", c);
147 }
148 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
149 return errorSyntax(USAGE_NATNETWORK, "unknown option: %s", ValueUnion.psz);
150 else if (ValueUnion.pDef)
151 return errorSyntax(USAGE_NATNETWORK, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
152 else
153 return errorSyntax(USAGE_NATNETWORK, "%Rrs", c);
154 }
155 }
156
157 if (!pNetName)
158 return errorSyntax(USAGE_NATNETWORK,
159 "You need to specify --netname option");
160 /* verification */
161 switch (enmCode)
162 {
163 case OP_ADD:
164 if (!pNetworkCidr)
165 return errorSyntax(USAGE_NATNETWORK,
166 "You need to specify --network option");
167 break;
168 case OP_MODIFY:
169 case OP_REMOVE:
170 case OP_START:
171 case OP_STOP:
172 break;
173 default:
174 AssertMsgFailedReturn(("Unknown operation (:%d)", enmCode), VERR_NOT_IMPLEMENTED);
175 }
176
177 Bstr NetName;
178 NetName = Bstr(pNetName);
179
180
181 ComPtr<INATNetwork> net;
182 rc = a->virtualBox->FindNATNetworkByName(NetName.mutableRaw(), net.asOutParam());
183 if(enmCode == OP_ADD)
184 {
185 if (SUCCEEDED(rc))
186 return errorArgument("NATNetwork server already exists");
187
188 CHECK_ERROR(a->virtualBox, CreateNATNetwork(NetName.raw(), net.asOutParam()));
189 if (FAILED(rc))
190 return errorArgument("Failed to create the NAT network service");
191 }
192 else if (FAILED(rc))
193 {
194 return errorArgument("NATNetwork server does not exist");
195 }
196
197 switch (enmCode)
198 {
199 case OP_ADD:
200 case OP_MODIFY:
201 {
202 if (pNetworkCidr)
203 {
204 CHECK_ERROR(net, COMSETTER(Network)(Bstr(pNetworkCidr).raw()));
205 if(FAILED(rc))
206 return errorArgument("Failed to set configuration");
207 }
208 if (dhcp >= 0)
209 {
210 CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp));
211 if(FAILED(rc))
212 return errorArgument("Failed to set configuration");
213 }
214
215 if (ipv6 >= 0)
216 {
217 CHECK_ERROR(net, COMSETTER(IPv6Enabled) ((BOOL)ipv6));
218 if(FAILED(rc))
219 return errorArgument("Failed to set configuration");
220 }
221
222 if(enable >= 0)
223 {
224 CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable));
225 if(FAILED(rc))
226 return errorArgument("Failed to set configuration");
227
228 }
229 break;
230 }
231 case OP_REMOVE:
232 {
233 CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net));
234 if(FAILED(rc))
235 return errorArgument("Failed to remove nat network");
236 break;
237 }
238 case OP_START:
239 {
240 CHECK_ERROR(net, Start(Bstr("whatever").raw()));
241 if(FAILED(rc))
242 return errorArgument("Failed to start network");
243 break;
244 }
245 case OP_STOP:
246 {
247 CHECK_ERROR(net, Stop());
248 if(FAILED(rc))
249 return errorArgument("Failed to start network");
250 break;
251 }
252 default:;
253 }
254 return 0;
255}
256
257
258int handleNATNetwork(HandlerArg *a)
259{
260 if (a->argc < 1)
261 return errorSyntax(USAGE_NATNETWORK, "Not enough parameters");
262
263 int result;
264 int cProcessed;
265 if (strcmp(a->argv[0], "modify") == 0)
266 result = handleOp(a, OP_MODIFY, 1, &cProcessed);
267 else if (strcmp(a->argv[0], "add") == 0)
268 result = handleOp(a, OP_ADD, 1, &cProcessed);
269 else if (strcmp(a->argv[0], "remove") == 0)
270 result = handleOp(a, OP_REMOVE, 1, &cProcessed);
271 else if (strcmp(a->argv[0], "start") == 0)
272 result = handleOp(a, OP_START, 1, &cProcessed);
273 else if (strcmp(a->argv[0], "stop") == 0)
274 result = handleOp(a, OP_STOP, 1, &cProcessed);
275 else
276 result = errorSyntax(USAGE_NATNETWORK, "Invalid parameter '%s'", Utf8Str(a->argv[0]).c_str());
277
278 return result;
279}
280
281#endif /* !VBOX_ONLY_DOCS */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette