VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageDHCPServer.cpp@ 18063

Last change on this file since 18063 was 18023, checked in by vboxsync, 16 years ago

Dhcp->DHCP

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/* $Id: VBoxManageDHCPServer.cpp 18023 2009-03-17 13:48:59Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of hostonlyif command.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#ifndef VBOX_ONLY_DOCS
26#include <VBox/com/com.h>
27#include <VBox/com/array.h>
28#include <VBox/com/ErrorInfo.h>
29#include <VBox/com/errorprint2.h>
30#include <VBox/com/EventQueue.h>
31
32#include <VBox/com/VirtualBox.h>
33
34#include <vector>
35#include <list>
36#endif /* !VBOX_ONLY_DOCS */
37
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
46#include <VBox/log.h>
47
48#include "VBoxManage.h"
49
50#ifndef VBOX_ONLY_DOCS
51using namespace com;
52
53typedef enum enMainOpCodes
54{
55 OP_ADD = 1000,
56 OP_REMOVE,
57 OP_MODIFY,
58} OPCODE;
59
60enum enOptionCodes
61{
62 NETNAME = 1000,
63 IFNAME,
64 IP,
65 NETMASK,
66 LOWERIP,
67 UPPERIP,
68 ENABLE,
69 DISABLE
70};
71
72static const RTGETOPTDEF g_aListOptions[]
73 = {
74 { "-netname", NETNAME, RTGETOPT_REQ_STRING },
75 { "-ifname", IFNAME, RTGETOPT_REQ_STRING },
76 { "-ip", IP, RTGETOPT_REQ_STRING },
77 { "-netmask", NETMASK, RTGETOPT_REQ_STRING },
78 { "-lowerip", LOWERIP, RTGETOPT_REQ_STRING },
79 { "-upperip", UPPERIP, RTGETOPT_REQ_STRING },
80 { "-enable", ENABLE, RTGETOPT_REQ_NOTHING },
81 { "-disable", DISABLE, RTGETOPT_REQ_NOTHING }
82 };
83
84static int handleOp(HandlerArg *a, OPCODE enmCode, int iStart, int *pcProcessed)
85{
86 if (a->argc - iStart < 2)
87 return errorSyntax(USAGE_DHCPSERVER, "Not enough parameters");
88
89 int index = iStart;
90 HRESULT rc;
91
92 const char *pNetName = NULL;
93 const char *pIfName = NULL;
94 const char * pIp = NULL;
95 const char * pNetmask = NULL;
96 const char * pLowerIp = NULL;
97 const char * pUpperIp = NULL;
98 int enable = -1;
99
100 int c;
101 RTGETOPTUNION ValueUnion;
102 RTGETOPTSTATE GetState;
103 RTGetOptInit(&GetState,
104 a->argc,
105 a->argv,
106 g_aListOptions,
107 enmCode != OP_REMOVE ? RT_ELEMENTS(g_aListOptions): 2, /* we use only -netname and -ifname for remove*/
108 index,
109 0 /* fFlags */);
110 while ((c = RTGetOpt(&GetState, &ValueUnion)))
111 {
112 switch (c)
113 {
114 case NETNAME:
115 if(pNetName)
116 return errorSyntax(USAGE_DHCPSERVER, "You can only specify -netname once.");
117 else if (pIfName)
118 return errorSyntax(USAGE_DHCPSERVER, "You can either use a -netname or -ifname for identifying the dhcp server.");
119 else
120 {
121 pNetName = ValueUnion.psz;
122 }
123 break;
124 case IFNAME:
125 if(pIfName)
126 return errorSyntax(USAGE_DHCPSERVER, "You can only specify -ifname once.");
127 else if (pNetName)
128 return errorSyntax(USAGE_DHCPSERVER, "You can either use a -netname or -ipname for identifying the dhcp server.");
129 else
130 {
131 pIfName = ValueUnion.psz;
132 }
133 break;
134 case IP:
135 if(pIp)
136 return errorSyntax(USAGE_DHCPSERVER, "You can only specify -ip once.");
137 else
138 {
139 pIp = ValueUnion.psz;
140 }
141 break;
142 case NETMASK:
143 if(pNetmask)
144 return errorSyntax(USAGE_DHCPSERVER, "You can only specify -netmask once.");
145 else
146 {
147 pNetmask = ValueUnion.psz;
148 }
149 break;
150 case LOWERIP:
151 if(pLowerIp)
152 return errorSyntax(USAGE_DHCPSERVER, "You can only specify -lowerip once.");
153 else
154 {
155 pLowerIp = ValueUnion.psz;
156 }
157 break;
158 case UPPERIP:
159 if(pUpperIp)
160 return errorSyntax(USAGE_DHCPSERVER, "You can only specify -upperip once.");
161 else
162 {
163 pUpperIp = ValueUnion.psz;
164 }
165 break;
166 case ENABLE:
167 if(enable >= 0)
168 return errorSyntax(USAGE_DHCPSERVER, "You can specify either -enable or -disable once.");
169 else
170 {
171 enable = 1;
172 }
173 break;
174 case DISABLE:
175 if(enable >= 0)
176 return errorSyntax(USAGE_DHCPSERVER, "You can specify either -enable or -disable once.");
177 else
178 {
179 enable = 0;
180 }
181 break;
182 case VINF_GETOPT_NOT_OPTION:
183 case VERR_GETOPT_UNKNOWN_OPTION:
184 return errorSyntax(USAGE_DHCPSERVER, "Unknown option \"%s\".", ValueUnion.psz);
185 break;
186 default:
187 if (c > 0)
188 return errorSyntax(USAGE_DHCPSERVER, "missing case: %c\n", c);
189 else if (ValueUnion.pDef)
190 return errorSyntax(USAGE_DHCPSERVER, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
191 else
192 return errorSyntax(USAGE_DHCPSERVER, "%Rrs", c);
193 }
194 }
195
196 if(! pNetName && !pIfName)
197 return errorSyntax(USAGE_DHCPSERVER, "You need to specify either -netname or -ifname to identify the dhcp server");
198
199 if(enmCode != OP_REMOVE)
200 {
201 if(enable < 0 || pIp || pNetmask || pLowerIp || pUpperIp)
202 {
203 if(!pIp)
204 return errorSyntax(USAGE_DHCPSERVER, "You need to specify -ip option");
205
206 if(!pNetmask)
207 return errorSyntax(USAGE_DHCPSERVER, "You need to specify -netmask option");
208
209 if(!pLowerIp)
210 return errorSyntax(USAGE_DHCPSERVER, "You need to specify -lowerip option");
211
212 if(!pUpperIp)
213 return errorSyntax(USAGE_DHCPSERVER, "You need to specify -upperip option");
214 }
215 }
216
217 Bstr NetName;
218 if(!pNetName)
219 {
220 ComPtr<IHost> host;
221 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
222
223 ComPtr<IHostNetworkInterface> hif;
224 CHECK_ERROR(host, FindHostNetworkInterfaceByName(Bstr(pIfName).mutableRaw(), hif.asOutParam()));
225 if(FAILED(rc))
226 return errorArgument("could not find interface '%s'", pIfName);
227
228 CHECK_ERROR(hif, COMGETTER(NetworkName) (NetName.asOutParam()));
229 if(FAILED(rc))
230 return errorArgument("could not get network name for the interface '%s'", pIfName);
231 }
232 else
233 {
234 NetName = Bstr(pNetName);
235 }
236
237 ComPtr<IDHCPServer> svr;
238 rc = a->virtualBox->FindDHCPServerByNetworkName(NetName.mutableRaw(), svr.asOutParam());
239 if(enmCode == OP_ADD)
240 {
241 if(SUCCEEDED(rc))
242 return errorArgument("dhcp server already exists");
243
244 CHECK_ERROR(a->virtualBox, CreateDHCPServer(NetName.mutableRaw(), svr.asOutParam()));
245 if(FAILED(rc))
246 return errorArgument("failed to create server");
247 }
248 else if(FAILED(rc))
249 {
250 return errorArgument("dhcp server does not exist");
251 }
252
253 if(enmCode != OP_REMOVE)
254 {
255 if (pIp || pNetmask || pLowerIp || pUpperIp)
256 {
257 CHECK_ERROR(svr, SetConfiguration (Bstr(pIp).mutableRaw(), Bstr(pNetmask).mutableRaw(), Bstr(pLowerIp).mutableRaw(), Bstr(pUpperIp).mutableRaw()));
258 if(FAILED(rc))
259 return errorArgument("failed to set configuration");
260 }
261
262 if(enable >= 0)
263 {
264 CHECK_ERROR(svr, COMSETTER(Enabled) ((BOOL)enable));
265 }
266 }
267 else
268 {
269 CHECK_ERROR(a->virtualBox, RemoveDHCPServer(svr));
270 if(FAILED(rc))
271 return errorArgument("failed to remove server");
272 }
273
274 return 0;
275}
276
277
278int handleDHCPServer(HandlerArg *a)
279{
280 int result = 0;
281 if (a->argc < 1)
282 return errorSyntax(USAGE_DHCPSERVER, "Not enough parameters");
283
284 for (int i = 0; i < a->argc; i++)
285 {
286 if (strcmp(a->argv[i], "modify") == 0)
287 {
288 int cProcessed;
289 result = handleOp(a, OP_MODIFY, i+1, &cProcessed);
290 break;
291 }
292 else if (strcmp(a->argv[i], "add") == 0)
293 {
294 int cProcessed;
295 result = handleOp(a, OP_ADD, i+1, &cProcessed);
296 break;
297 }
298 else if (strcmp(a->argv[i], "remove") == 0)
299 {
300 int cProcessed;
301 result = handleOp(a, OP_REMOVE, i+1, &cProcessed);
302 break;
303 }
304 else
305 {
306 result = errorSyntax(USAGE_DHCPSERVER, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
307 break;
308 }
309 }
310
311 return result;
312}
313
314#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