VirtualBox

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

Last change on this file since 79771 was 79771, checked in by vboxsync, 6 years ago

Main/DHCP*,VBoxManage: Some adjustments to the DHCP*Config interfaces and reworked the VBoxMAnage dhcpserver add/modify commands to support groups, fixed address assignments, setting lease times, removing group and individaul configurations, ++. todo: update man page. bugref:9288

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.6 KB
Line 
1/* $Id: VBoxManageDHCPServer.cpp 79771 2019-07-14 21:10:58Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of dhcpserver command.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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#include <iprt/cidr.h>
29#include <iprt/param.h>
30#include <iprt/path.h>
31#include <iprt/stream.h>
32#include <iprt/string.h>
33#include <iprt/net.h>
34#include <iprt/getopt.h>
35#include <iprt/ctype.h>
36
37#include <VBox/log.h>
38
39#include "VBoxManage.h"
40
41#include <vector>
42#include <map>
43
44using namespace com;
45
46
47/*********************************************************************************************************************************
48* Defined Constants And Macros *
49*********************************************************************************************************************************/
50#define DHCPD_CMD_COMMON_OPT_NETWORK 999 /**< The --network / --netname option number. */
51#define DHCPD_CMD_COMMON_OPT_INTERFACE 998 /**< The --interface / --ifname option number. */
52/** Common option definitions. */
53#define DHCPD_CMD_COMMON_OPTION_DEFS() \
54 { "--network", DHCPD_CMD_COMMON_OPT_NETWORK, RTGETOPT_REQ_STRING }, \
55 { "--netname", DHCPD_CMD_COMMON_OPT_NETWORK, RTGETOPT_REQ_STRING }, /* legacy */ \
56 { "--interface", DHCPD_CMD_COMMON_OPT_INTERFACE, RTGETOPT_REQ_STRING }, \
57 { "--ifname", DHCPD_CMD_COMMON_OPT_INTERFACE, RTGETOPT_REQ_STRING } /* legacy */
58
59/** Handles common options in the typical option parsing switch. */
60#define DHCPD_CMD_COMMON_OPTION_CASES(a_pCtx, a_ch, a_pValueUnion) \
61 case DHCPD_CMD_COMMON_OPT_NETWORK: \
62 if ((a_pCtx)->pszInterface != NULL) \
63 return errorSyntax("Either --network or --interface, not both"); \
64 (a_pCtx)->pszNetwork = ValueUnion.psz; \
65 break; \
66 case DHCPD_CMD_COMMON_OPT_INTERFACE: \
67 if ((a_pCtx)->pszNetwork != NULL) \
68 return errorSyntax("Either --interface or --network, not both"); \
69 (a_pCtx)->pszInterface = ValueUnion.psz; \
70 break
71
72
73/*********************************************************************************************************************************
74* Structures and Typedefs *
75*********************************************************************************************************************************/
76/** Pointer to a dhcpserver command context. */
77typedef struct DHCPDCMDCTX *PDHCPDCMDCTX;
78
79/**
80 * Definition of a dhcpserver command, with handler and various flags.
81 */
82typedef struct DHCPDCMDDEF
83{
84 /** The command name. */
85 const char *pszName;
86
87 /**
88 * Actual command handler callback.
89 *
90 * @param pCtx Pointer to command context to use.
91 */
92 DECLR3CALLBACKMEMBER(RTEXITCODE, pfnHandler, (PDHCPDCMDCTX pCtx, int argc, char **argv));
93
94 /** The sub-command scope flags. */
95 uint64_t fSubcommandScope;
96} DHCPDCMDDEF;
97/** Pointer to a const dhcpserver command definition. */
98typedef DHCPDCMDDEF const *PCDHCPDCMDDEF;
99
100/**
101 * dhcpserver command context (mainly for carrying common options and such).
102 */
103typedef struct DHCPDCMDCTX
104{
105 /** The handler arguments from the main() function. */
106 HandlerArg *pArg;
107 /** Pointer to the command definition. */
108 PCDHCPDCMDDEF pCmdDef;
109 /** The network name. */
110 const char *pszNetwork;
111 /** The (trunk) interface name. */
112 const char *pszInterface;
113} DHCPDCMDCTX;
114
115typedef std::pair<DhcpOpt_T, Utf8Str> DhcpOptSpec;
116typedef std::vector<DhcpOptSpec> DhcpOpts;
117typedef DhcpOpts::iterator DhcpOptIterator;
118
119typedef std::vector<DhcpOpt_T> DhcpOptIds;
120typedef DhcpOptIds::iterator DhcpOptIdIterator;
121
122struct VmNameSlotKey
123{
124 const Utf8Str VmName;
125 uint8_t u8Slot;
126
127 VmNameSlotKey(const Utf8Str &aVmName, uint8_t aSlot)
128 : VmName(aVmName)
129 , u8Slot(aSlot)
130 {}
131
132 bool operator<(const VmNameSlotKey& that) const
133 {
134 if (VmName == that.VmName)
135 return u8Slot < that.u8Slot;
136 return VmName < that.VmName;
137 }
138};
139
140typedef std::map<VmNameSlotKey, DhcpOpts> VmSlot2OptionsM;
141typedef VmSlot2OptionsM::iterator VmSlot2OptionsIterator;
142typedef VmSlot2OptionsM::value_type VmSlot2OptionsPair;
143
144typedef std::map<VmNameSlotKey, DhcpOptIds> VmSlot2OptionIdsM;
145typedef VmSlot2OptionIdsM::iterator VmSlot2OptionIdsIterator;
146
147
148
149/**
150 * Helper that find the DHCP server instance.
151 *
152 * @returns The DHCP server instance. NULL if failed (complaining done).
153 * @param pCtx The DHCP server command context.
154 */
155static ComPtr<IDHCPServer> dhcpdFindServer(PDHCPDCMDCTX pCtx)
156{
157 ComPtr<IDHCPServer> ptrRet;
158 if (pCtx->pszNetwork || pCtx->pszInterface)
159 {
160 Assert(pCtx->pszNetwork == NULL || pCtx->pszInterface == NULL);
161
162 /*
163 * We need a network name to find the DHCP server. So, if interface is
164 * given we have to look it up.
165 */
166 HRESULT hrc;
167 Bstr bstrNetName(pCtx->pszNetwork);
168 if (!pCtx->pszNetwork)
169 {
170 ComPtr<IHost> ptrIHost;
171 CHECK_ERROR2_RET(hrc, pCtx->pArg->virtualBox, COMGETTER(Host)(ptrIHost.asOutParam()), ptrRet);
172
173 Bstr bstrInterface(pCtx->pszInterface);
174 ComPtr<IHostNetworkInterface> ptrIHostIf;
175 CHECK_ERROR2(hrc, ptrIHost, FindHostNetworkInterfaceByName(bstrInterface.raw(), ptrIHostIf.asOutParam()));
176 if (FAILED(hrc))
177 {
178 errorArgument("Failed to locate host-only interface '%s'", pCtx->pszInterface);
179 return ptrRet;
180 }
181
182 CHECK_ERROR2_RET(hrc, ptrIHostIf, COMGETTER(NetworkName)(bstrNetName.asOutParam()), ptrRet);
183 }
184
185 /*
186 * Now, try locate the server
187 */
188 hrc = pCtx->pArg->virtualBox->FindDHCPServerByNetworkName(bstrNetName.raw(), ptrRet.asOutParam());
189 if (SUCCEEDED(hrc))
190 return ptrRet;
191 if (pCtx->pszNetwork)
192 errorArgument("Failed to find DHCP server for network '%s'", pCtx->pszNetwork);
193 else
194 errorArgument("Failed to find DHCP server for host-only interface '%s' (network '%ls')",
195 pCtx->pszInterface, bstrNetName.raw());
196 }
197 else
198 errorSyntax("You need to specify either --network or --interface to identify the DHCP server");
199 return ptrRet;
200}
201
202
203/**
204 * Helper class for dhcpdHandleAddAndModify
205 */
206class DHCPCmdScope
207{
208 DHCPConfigScope_T m_enmScope;
209 const char *m_pszName;
210 uint8_t m_uSlot;
211 ComPtr<IDHCPConfig> m_ptrConfig;
212 ComPtr<IDHCPGlobalConfig> m_ptrGlobalConfig;
213 ComPtr<IDHCPGroupConfig> m_ptrGroupConfig;
214 ComPtr<IDHCPIndividualConfig> m_ptrIndividualConfig;
215
216public:
217 DHCPCmdScope()
218 : m_enmScope(DHCPConfigScope_Global)
219 , m_pszName(NULL)
220 , m_uSlot(0)
221 {
222 }
223
224 void setGlobal()
225 {
226 m_enmScope = DHCPConfigScope_Global;
227 m_pszName = NULL;
228 m_uSlot = 0;
229 resetPointers();
230 }
231
232 void setGroup(const char *pszGroup)
233 {
234 m_enmScope = DHCPConfigScope_Group;
235 m_pszName = pszGroup;
236 m_uSlot = 0;
237 resetPointers();
238 }
239
240 void setMachineNIC(const char *pszMachine)
241 {
242 m_enmScope = DHCPConfigScope_MachineNIC;
243 m_pszName = pszMachine;
244 m_uSlot = 0;
245 resetPointers();
246 }
247
248 void setMachineSlot(uint8_t uSlot)
249 {
250 Assert(m_enmScope == DHCPConfigScope_MachineNIC);
251 m_uSlot = uSlot;
252 resetPointers();
253 }
254
255 void setMACAddress(const char *pszMACAddress)
256 {
257 m_enmScope = DHCPConfigScope_MAC;
258 m_pszName = pszMACAddress;
259 m_uSlot = 0;
260 resetPointers();
261 }
262
263 ComPtr<IDHCPConfig> &getConfig(ComPtr<IDHCPServer> const &ptrDHCPServer)
264 {
265 if (m_ptrConfig.isNull())
266 CHECK_ERROR2I_STMT(ptrDHCPServer, GetConfig(m_enmScope, Bstr(m_pszName).raw(), m_uSlot, TRUE /*mayAdd*/,
267 m_ptrConfig.asOutParam()), m_ptrConfig.setNull());
268 return m_ptrConfig;
269 }
270
271 ComPtr<IDHCPIndividualConfig> &getIndividual(ComPtr<IDHCPServer> const &ptrDHCPServer)
272 {
273 getConfig(ptrDHCPServer);
274 if (m_ptrIndividualConfig.isNull() && m_ptrConfig.isNotNull())
275 {
276 HRESULT hrc = m_ptrConfig.queryInterfaceTo(m_ptrIndividualConfig.asOutParam());
277 if (FAILED(hrc))
278 {
279 com::GlueHandleComError(m_ptrConfig, "queryInterface", hrc, __FILE__, __LINE__);
280 m_ptrIndividualConfig.setNull();
281 }
282 }
283 return m_ptrIndividualConfig;
284 }
285
286 ComPtr<IDHCPGroupConfig> &getGroup(ComPtr<IDHCPServer> const &ptrDHCPServer)
287 {
288 getConfig(ptrDHCPServer);
289 if (m_ptrGroupConfig.isNull() && m_ptrConfig.isNotNull())
290 {
291 HRESULT hrc = m_ptrConfig.queryInterfaceTo(m_ptrGroupConfig.asOutParam());
292 if (FAILED(hrc))
293 {
294 com::GlueHandleComError(m_ptrConfig, "queryInterface", hrc, __FILE__, __LINE__);
295 m_ptrGroupConfig.setNull();
296 }
297 }
298 return m_ptrGroupConfig;
299 }
300
301 DHCPConfigScope_T getScope() const { return m_enmScope; }
302
303private:
304 void resetPointers()
305 {
306 m_ptrConfig.setNull();
307 m_ptrGlobalConfig.setNull();
308 m_ptrIndividualConfig.setNull();
309 m_ptrGroupConfig.setNull();
310 }
311};
312
313enum
314{
315 DHCP_ADDMOD = 1000,
316 DHCP_ADDMOD_ZAP_OPTIONS,
317 DHCP_ADDMOD_INCL_MAC,
318 DHCP_ADDMOD_EXCL_MAC,
319 DHCP_ADDMOD_DEL_MAC,
320 DHCP_ADDMOD_INCL_MAC_WILD,
321 DHCP_ADDMOD_EXCL_MAC_WILD,
322 DHCP_ADDMOD_DEL_MAC_WILD,
323 DHCP_ADDMOD_INCL_VENDOR,
324 DHCP_ADDMOD_EXCL_VENDOR,
325 DHCP_ADDMOD_DEL_VENDOR,
326 DHCP_ADDMOD_INCL_VENDOR_WILD,
327 DHCP_ADDMOD_EXCL_VENDOR_WILD,
328 DHCP_ADDMOD_DEL_VENDOR_WILD,
329 DHCP_ADDMOD_INCL_USER,
330 DHCP_ADDMOD_EXCL_USER,
331 DHCP_ADDMOD_DEL_USER,
332 DHCP_ADDMOD_INCL_USER_WILD,
333 DHCP_ADDMOD_EXCL_USER_WILD,
334 DHCP_ADDMOD_DEL_USER_WILD,
335 DHCP_ADDMOD_ZAP_CONDITIONS
336};
337
338/**
339 * Handles the 'add' and 'modify' subcommands.
340 */
341static DECLCALLBACK(RTEXITCODE) dhcpdHandleAddAndModify(PDHCPDCMDCTX pCtx, int argc, char **argv)
342{
343 static const RTGETOPTDEF s_aOptions[] =
344 {
345 DHCPD_CMD_COMMON_OPTION_DEFS(),
346 { "--server-ip", 'a', RTGETOPT_REQ_STRING },
347 { "--ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
348 { "-ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
349 { "--netmask", 'm', RTGETOPT_REQ_STRING },
350 { "-netmask", 'm', RTGETOPT_REQ_STRING }, // deprecated
351 { "--lower-ip", 'l', RTGETOPT_REQ_STRING },
352 { "--lowerip", 'l', RTGETOPT_REQ_STRING },
353 { "-lowerip", 'l', RTGETOPT_REQ_STRING }, // deprecated
354 { "--upper-ip", 'u', RTGETOPT_REQ_STRING },
355 { "--upperip", 'u', RTGETOPT_REQ_STRING },
356 { "-upperip", 'u', RTGETOPT_REQ_STRING }, // deprecated
357 { "--enable", 'e', RTGETOPT_REQ_NOTHING },
358 { "-enable", 'e', RTGETOPT_REQ_NOTHING }, // deprecated
359 { "--disable", 'd', RTGETOPT_REQ_NOTHING },
360 { "-disable", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
361 { "--global", 'g', RTGETOPT_REQ_NOTHING },
362 { "--group", 'G', RTGETOPT_REQ_STRING },
363 { "--mac-address", 'E', RTGETOPT_REQ_MACADDR },
364 { "--vm", 'M', RTGETOPT_REQ_STRING },
365 { "--nic", 'n', RTGETOPT_REQ_UINT8 },
366 { "--set-opt", 's', RTGETOPT_REQ_UINT8 },
367 { "--set-opt-hex", 'x', RTGETOPT_REQ_UINT8 },
368 { "--del-opt", 'D', RTGETOPT_REQ_UINT8 },
369 { "--zap-options", DHCP_ADDMOD_ZAP_OPTIONS, RTGETOPT_REQ_NOTHING },
370 { "--min-lease-time", 'q' , RTGETOPT_REQ_UINT32 },
371 { "--default-lease-time", 'L' , RTGETOPT_REQ_UINT32 },
372 { "--max-lease-time", 'Q' , RTGETOPT_REQ_UINT32 },
373 { "--remove-config", 'R', RTGETOPT_REQ_NOTHING },
374 { "--fixed-address", 'f', RTGETOPT_REQ_STRING },
375 /* group conditions: */
376 { "--incl-mac", DHCP_ADDMOD_INCL_MAC, RTGETOPT_REQ_STRING },
377 { "--excl-mac", DHCP_ADDMOD_EXCL_MAC, RTGETOPT_REQ_STRING },
378 { "--del-mac", DHCP_ADDMOD_DEL_MAC, RTGETOPT_REQ_STRING },
379 { "--incl-mac-wild", DHCP_ADDMOD_INCL_MAC_WILD, RTGETOPT_REQ_STRING },
380 { "--excl-mac-wild", DHCP_ADDMOD_EXCL_MAC_WILD, RTGETOPT_REQ_STRING },
381 { "--del-mac-wild", DHCP_ADDMOD_DEL_MAC_WILD, RTGETOPT_REQ_STRING },
382 { "--incl-vendor", DHCP_ADDMOD_INCL_VENDOR, RTGETOPT_REQ_STRING },
383 { "--excl-vendor", DHCP_ADDMOD_EXCL_VENDOR, RTGETOPT_REQ_STRING },
384 { "--del-vendor", DHCP_ADDMOD_DEL_VENDOR, RTGETOPT_REQ_STRING },
385 { "--incl-vendor-wild", DHCP_ADDMOD_INCL_VENDOR_WILD, RTGETOPT_REQ_STRING },
386 { "--excl-vendor-wild", DHCP_ADDMOD_EXCL_VENDOR_WILD, RTGETOPT_REQ_STRING },
387 { "--del-vendor-wild", DHCP_ADDMOD_DEL_VENDOR_WILD, RTGETOPT_REQ_STRING },
388 { "--incl-user", DHCP_ADDMOD_INCL_USER, RTGETOPT_REQ_STRING },
389 { "--excl-user", DHCP_ADDMOD_EXCL_USER, RTGETOPT_REQ_STRING },
390 { "--del-user", DHCP_ADDMOD_DEL_USER, RTGETOPT_REQ_STRING },
391 { "--incl-user-wild", DHCP_ADDMOD_INCL_USER_WILD, RTGETOPT_REQ_STRING },
392 { "--excl-user-wild", DHCP_ADDMOD_EXCL_USER_WILD, RTGETOPT_REQ_STRING },
393 { "--del-user-wild", DHCP_ADDMOD_DEL_USER_WILD, RTGETOPT_REQ_STRING },
394 { "--zap-conditions", DHCP_ADDMOD_ZAP_CONDITIONS, RTGETOPT_REQ_NOTHING },
395 /* obsolete, to be removed: */
396 { "--id", 'i', RTGETOPT_REQ_UINT8 }, // obsolete, backwards compatibility only.
397 { "--value", 'p', RTGETOPT_REQ_STRING }, // obsolete, backwards compatibility only.
398 { "--remove", 'r', RTGETOPT_REQ_NOTHING }, // obsolete, backwards compatibility only.
399 { "--options", 'o', RTGETOPT_REQ_NOTHING }, // obsolete legacy, ignored
400
401 };
402
403 /*
404 * Parse the arguments in two passes:
405 *
406 * 1. Validate the command line and establish the IDHCPServer settings.
407 * 2. Execute the various IDHCPConfig settings changes.
408 *
409 * This is considered simpler than duplicating the command line instructions
410 * into elaborate structures and executing these.
411 */
412 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
413 ComPtr<IDHCPServer> ptrDHCPServer;
414 for (size_t iPass = 0; iPass < 2; iPass++)
415 {
416 const char *pszServerIp = NULL;
417 const char *pszNetmask = NULL;
418 const char *pszLowerIp = NULL;
419 const char *pszUpperIp = NULL;
420 int fEnabled = -1;
421
422 DHCPCmdScope Scope;
423 char szMACAddress[32];
424
425 bool fNeedValueOrRemove = false; /* Only used with --id; remove in 6.1+ */
426 uint8_t u8OptId = 0; /* Only used too keep --id for following --value/--remove. remove in 6.1+ */
427
428 RTGETOPTSTATE GetState;
429 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
430 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
431
432 RTGETOPTUNION ValueUnion;
433 while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
434 {
435 switch (vrc)
436 {
437 DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
438 case 'a': // --server-ip
439 pszServerIp = ValueUnion.psz;
440 break;
441 case 'm': // --netmask
442 pszNetmask = ValueUnion.psz;
443 break;
444 case 'l': // --lower-ip
445 pszLowerIp = ValueUnion.psz;
446 break;
447 case 'u': // --upper-ip
448 pszUpperIp = ValueUnion.psz;
449 break;
450 case 'e': // --enable
451 fEnabled = 1;
452 break;
453 case 'd': // --disable
454 fEnabled = 0;
455 break;
456
457 /*
458 * Configuration selection:
459 */
460 case 'g': // --global Sets the option scope to 'global'.
461 if (fNeedValueOrRemove)
462 return errorSyntax("Incomplete option sequence preseeding '--global'");
463 Scope.setGlobal();
464 break;
465
466 case 'G': // --group
467 if (fNeedValueOrRemove)
468 return errorSyntax("Incomplete option sequence preseeding '--group'");
469 if (!*ValueUnion.psz)
470 return errorSyntax("Group name cannot be empty");
471 Scope.setGroup(ValueUnion.psz);
472 break;
473
474 case 'E': // --mac-address
475 if (fNeedValueOrRemove)
476 return errorSyntax("Incomplete option sequence preseeding '--mac-address'");
477 RTStrPrintf(szMACAddress, sizeof(szMACAddress), "%RTmac", &ValueUnion.MacAddr);
478 Scope.setMACAddress(szMACAddress);
479 break;
480
481 case 'M': // --vm Sets the option scope to ValueUnion.psz + 0.
482 if (fNeedValueOrRemove)
483 return errorSyntax("Incomplete option sequence preseeding '--vm'");
484 Scope.setMachineNIC(ValueUnion.psz);
485 break;
486
487 case 'n': // --nic Sets the option scope to pszVmName + (ValueUnion.u8 - 1).
488 if (Scope.getScope() != DHCPConfigScope_MachineNIC)
489 return errorSyntax("--nic option requires a --vm preceeding selecting the VM it should apply to");
490 if (fNeedValueOrRemove)
491 return errorSyntax("Incomplete option sequence preseeding '--nic=%u", ValueUnion.u8);
492 if (ValueUnion.u8 < 1)
493 return errorSyntax("invalid NIC number: %u", ValueUnion.u8);
494 Scope.setMachineSlot(ValueUnion.u8 - 1);
495 break;
496
497 /*
498 * Modify configuration:
499 */
500 case 's': // --set-opt num stringvalue
501 {
502 uint8_t const idAddOpt = ValueUnion.u8;
503 vrc = RTGetOptFetchValue(&GetState, &ValueUnion, RTGETOPT_REQ_STRING);
504 if (RT_FAILURE(vrc))
505 return errorFetchValue(1, "--set-opt", vrc, &ValueUnion);
506 if (iPass == 1)
507 {
508 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
509 if (ptrConfig.isNull())
510 return RTEXITCODE_FAILURE;
511 CHECK_ERROR2I_STMT(ptrConfig, SetOption((DhcpOpt_T)idAddOpt, DHCPOptionEncoding_Legacy,
512 Bstr(ValueUnion.psz).raw()), rcExit = RTEXITCODE_FAILURE);
513 }
514 break;
515 }
516
517 case 'x': // --set-opt-hex num hex-string
518 {
519 uint8_t const idAddOpt = ValueUnion.u8;
520 vrc = RTGetOptFetchValue(&GetState, &ValueUnion, RTGETOPT_REQ_STRING);
521 if (RT_FAILURE(vrc))
522 return errorFetchValue(1, "--set-opt-hex", vrc, &ValueUnion);
523 uint8_t abBuf[256];
524 size_t cbRet;
525 vrc = RTStrConvertHexBytesEx(ValueUnion.psz, abBuf, sizeof(abBuf), RTSTRCONVERTHEXBYTES_F_SEP_COLON,
526 NULL, &cbRet);
527 if (RT_FAILURE(vrc))
528 return errorArgument("Malformed hex string given to --set-opt-hex %u: %s\n", idAddOpt, ValueUnion.psz);
529 if (iPass == 1)
530 {
531 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
532 if (ptrConfig.isNull())
533 return RTEXITCODE_FAILURE;
534 CHECK_ERROR2I_STMT(ptrConfig, SetOption((DhcpOpt_T)idAddOpt, DHCPOptionEncoding_Hex,
535 Bstr(ValueUnion.psz).raw()), rcExit = RTEXITCODE_FAILURE);
536 }
537 break;
538 }
539
540 case 'D': // --del-opt num
541 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
542 return errorSyntax("--del-opt does not apply to the 'add' subcommand");
543 if (iPass == 1)
544 {
545 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
546 if (ptrConfig.isNull())
547 return RTEXITCODE_FAILURE;
548 CHECK_ERROR2I_STMT(ptrConfig, RemoveOption((DhcpOpt_T)ValueUnion.u8), rcExit = RTEXITCODE_FAILURE);
549 }
550 break;
551
552 case DHCP_ADDMOD_ZAP_OPTIONS:
553 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
554 return errorSyntax("--zap-options does not apply to the 'add' subcommand");
555 if (iPass == 1)
556 {
557 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
558 if (ptrConfig.isNull())
559 return RTEXITCODE_FAILURE;
560 CHECK_ERROR2I_STMT(ptrConfig, RemoveAllOptions(), rcExit = RTEXITCODE_FAILURE);
561 }
562 break;
563
564 case 'q': // --min-lease-time
565 if (iPass == 1)
566 {
567 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
568 if (ptrConfig.isNull())
569 return RTEXITCODE_FAILURE;
570 CHECK_ERROR2I_STMT(ptrConfig, COMSETTER(MinLeaseTime)(ValueUnion.u32), rcExit = RTEXITCODE_FAILURE);
571 }
572 break;
573
574 case 'L': // --default-lease-time
575 if (iPass == 1)
576 {
577 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
578 if (ptrConfig.isNull())
579 return RTEXITCODE_FAILURE;
580 CHECK_ERROR2I_STMT(ptrConfig, COMSETTER(DefaultLeaseTime)(ValueUnion.u32), rcExit = RTEXITCODE_FAILURE);
581 }
582 break;
583
584 case 'Q': // --max-lease-time
585 if (iPass == 1)
586 {
587 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
588 if (ptrConfig.isNull())
589 return RTEXITCODE_FAILURE;
590 CHECK_ERROR2I_STMT(ptrConfig, COMSETTER(MaxLeaseTime)(ValueUnion.u32), rcExit = RTEXITCODE_FAILURE);
591 }
592 break;
593
594 case 'R': // --remove-config
595 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
596 return errorSyntax("--remove-config does not apply to the 'add' subcommand");
597 if (Scope.getScope() == DHCPConfigScope_Global)
598 return errorSyntax("--remove-config cannot be applied to the global config");
599 if (iPass == 1)
600 {
601 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
602 if (ptrConfig.isNull())
603 return RTEXITCODE_FAILURE;
604 CHECK_ERROR2I_STMT(ptrConfig, Remove(), rcExit = RTEXITCODE_FAILURE);
605 }
606 Scope.setGlobal();
607 break;
608
609 case 'f': // --fixed-address
610 if (Scope.getScope() != DHCPConfigScope_MachineNIC && Scope.getScope() != DHCPConfigScope_MAC)
611 return errorSyntax("--fixed-address can only be applied to a VM NIC or an MAC address");
612 if (iPass == 1)
613 {
614 ComPtr<IDHCPIndividualConfig> &ptrIndividualConfig = Scope.getIndividual(ptrDHCPServer);
615 if (ptrIndividualConfig.isNull())
616 return RTEXITCODE_FAILURE;
617 CHECK_ERROR2I_STMT(ptrIndividualConfig, COMSETTER(FixedAddress)(Bstr(ValueUnion.psz).raw()),
618 rcExit = RTEXITCODE_FAILURE);
619 }
620 break;
621
622 /*
623 * Group conditions:
624 */
625 case DHCP_ADDMOD_INCL_MAC:
626 case DHCP_ADDMOD_EXCL_MAC:
627 case DHCP_ADDMOD_DEL_MAC:
628 case DHCP_ADDMOD_INCL_MAC_WILD:
629 case DHCP_ADDMOD_EXCL_MAC_WILD:
630 case DHCP_ADDMOD_DEL_MAC_WILD:
631 case DHCP_ADDMOD_INCL_VENDOR:
632 case DHCP_ADDMOD_EXCL_VENDOR:
633 case DHCP_ADDMOD_DEL_VENDOR:
634 case DHCP_ADDMOD_INCL_VENDOR_WILD:
635 case DHCP_ADDMOD_EXCL_VENDOR_WILD:
636 case DHCP_ADDMOD_DEL_VENDOR_WILD:
637 case DHCP_ADDMOD_INCL_USER:
638 case DHCP_ADDMOD_EXCL_USER:
639 case DHCP_ADDMOD_DEL_USER:
640 case DHCP_ADDMOD_INCL_USER_WILD:
641 case DHCP_ADDMOD_EXCL_USER_WILD:
642 case DHCP_ADDMOD_DEL_USER_WILD:
643 {
644 if (Scope.getScope() != DHCPConfigScope_Group)
645 return errorSyntax("A group must be selected to perform condition alterations.");
646 if (!*ValueUnion.psz)
647 return errorSyntax("Condition value cannot be empty"); /* or can it? */
648 if (iPass != 1)
649 break;
650
651 DHCPGroupConditionType enmType;
652 switch (vrc)
653 {
654 case DHCP_ADDMOD_INCL_MAC: case DHCP_ADDMOD_EXCL_MAC: case DHCP_ADDMOD_DEL_MAC:
655 enmType = DHCPGroupConditionType_MAC;
656 break;
657 case DHCP_ADDMOD_INCL_MAC_WILD: case DHCP_ADDMOD_EXCL_MAC_WILD: case DHCP_ADDMOD_DEL_MAC_WILD:
658 enmType = DHCPGroupConditionType_MACWildcard;
659 break;
660 case DHCP_ADDMOD_INCL_VENDOR: case DHCP_ADDMOD_EXCL_VENDOR: case DHCP_ADDMOD_DEL_VENDOR:
661 enmType = DHCPGroupConditionType_vendorClassID;
662 break;
663 case DHCP_ADDMOD_INCL_VENDOR_WILD: case DHCP_ADDMOD_EXCL_VENDOR_WILD: case DHCP_ADDMOD_DEL_VENDOR_WILD:
664 enmType = DHCPGroupConditionType_vendorClassIDWildcard;
665 break;
666 case DHCP_ADDMOD_INCL_USER: case DHCP_ADDMOD_EXCL_USER: case DHCP_ADDMOD_DEL_USER:
667 enmType = DHCPGroupConditionType_userClassID;
668 break;
669 case DHCP_ADDMOD_INCL_USER_WILD: case DHCP_ADDMOD_EXCL_USER_WILD: case DHCP_ADDMOD_DEL_USER_WILD:
670 enmType = DHCPGroupConditionType_userClassIDWildcard;
671 break;
672 default:
673 AssertFailedReturn(RTEXITCODE_FAILURE);
674 }
675
676 int fInclusive;
677 switch (vrc)
678 {
679 case DHCP_ADDMOD_DEL_MAC:
680 case DHCP_ADDMOD_DEL_MAC_WILD:
681 case DHCP_ADDMOD_DEL_USER:
682 case DHCP_ADDMOD_DEL_USER_WILD:
683 case DHCP_ADDMOD_DEL_VENDOR:
684 case DHCP_ADDMOD_DEL_VENDOR_WILD:
685 fInclusive = -1;
686 break;
687 case DHCP_ADDMOD_EXCL_MAC:
688 case DHCP_ADDMOD_EXCL_MAC_WILD:
689 case DHCP_ADDMOD_EXCL_USER:
690 case DHCP_ADDMOD_EXCL_USER_WILD:
691 case DHCP_ADDMOD_EXCL_VENDOR:
692 case DHCP_ADDMOD_EXCL_VENDOR_WILD:
693 fInclusive = 0;
694 break;
695 case DHCP_ADDMOD_INCL_MAC:
696 case DHCP_ADDMOD_INCL_MAC_WILD:
697 case DHCP_ADDMOD_INCL_USER:
698 case DHCP_ADDMOD_INCL_USER_WILD:
699 case DHCP_ADDMOD_INCL_VENDOR:
700 case DHCP_ADDMOD_INCL_VENDOR_WILD:
701 fInclusive = 1;
702 break;
703 default:
704 AssertFailedReturn(RTEXITCODE_FAILURE);
705 }
706
707 ComPtr<IDHCPGroupConfig> &ptrGroupConfig = Scope.getGroup(ptrDHCPServer);
708 if (ptrGroupConfig.isNull())
709 return RTEXITCODE_FAILURE;
710 if (fInclusive >= 0)
711 {
712 ComPtr<IDHCPGroupCondition> ptrCondition;
713 CHECK_ERROR2I_STMT(ptrGroupConfig, AddCondition((BOOL)fInclusive, enmType, Bstr(ValueUnion.psz).raw(),
714 ptrCondition.asOutParam()), rcExit = RTEXITCODE_FAILURE);
715 }
716 else
717 {
718 com::SafeIfaceArray<IDHCPGroupCondition> Conditions;
719 CHECK_ERROR2I_STMT(ptrGroupConfig, COMGETTER(Conditions)(ComSafeArrayAsOutParam(Conditions)),
720 rcExit = RTEXITCODE_FAILURE; break);
721 bool fFound = false;
722 for (size_t iCond = 0; iCond < Conditions.size(); iCond++)
723 {
724 DHCPGroupConditionType_T enmCurType = DHCPGroupConditionType_MAC;
725 CHECK_ERROR2I_STMT(Conditions[iCond], COMGETTER(Type)(&enmCurType),
726 rcExit = RTEXITCODE_FAILURE; continue);
727 if (enmCurType == enmType)
728 {
729 Bstr bstrValue;
730 CHECK_ERROR2I_STMT(Conditions[iCond], COMGETTER(Value)(bstrValue.asOutParam()),
731 rcExit = RTEXITCODE_FAILURE; continue);
732 if (RTUtf16CmpUtf8(bstrValue.raw(), ValueUnion.psz) == 0)
733 {
734 CHECK_ERROR2I_STMT(Conditions[iCond], Remove(), rcExit = RTEXITCODE_FAILURE);
735 fFound = true;
736 }
737 }
738 }
739 if (!fFound)
740 rcExit = RTMsgErrorExitFailure("Could not find any condition of type %d with value '%s' to delete",
741 enmType, ValueUnion.psz);
742 }
743 break;
744 }
745
746 case DHCP_ADDMOD_ZAP_CONDITIONS:
747 if (Scope.getScope() != DHCPConfigScope_Group)
748 return errorSyntax("--zap-conditions can only be with a group selected");
749 if (iPass == 1)
750 {
751 ComPtr<IDHCPGroupConfig> &ptrGroupConfig = Scope.getGroup(ptrDHCPServer);
752 if (ptrGroupConfig.isNull())
753 return RTEXITCODE_FAILURE;
754 CHECK_ERROR2I_STMT(ptrGroupConfig, RemoveAllConditions(), rcExit = RTEXITCODE_FAILURE);
755 }
756 break;
757
758 /*
759 * For backwards compatibility. Remove in 6.1 or later.
760 */
761
762 case 'o': // --options - obsolete, ignored.
763 break;
764
765 case 'i': // --id
766 if (fNeedValueOrRemove)
767 return errorSyntax("Incomplete option sequence preseeding '--id=%u", ValueUnion.u8);
768 u8OptId = ValueUnion.u8;
769 fNeedValueOrRemove = true;
770 break;
771
772 case 'p': // --value
773 if (!fNeedValueOrRemove)
774 return errorSyntax("--value without --id=dhcp-opt-no");
775 if (iPass == 1)
776 {
777 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
778 if (ptrConfig.isNull())
779 return RTEXITCODE_FAILURE;
780 CHECK_ERROR2I_STMT(ptrConfig, SetOption((DhcpOpt_T)u8OptId, DHCPOptionEncoding_Legacy,
781 Bstr(ValueUnion.psz).raw()), rcExit = RTEXITCODE_FAILURE);
782 }
783 fNeedValueOrRemove = false;
784 break;
785
786 case 'r': // --remove
787 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
788 return errorSyntax("--remove does not apply to the 'add' subcommand");
789 if (!fNeedValueOrRemove)
790 return errorSyntax("--remove without --id=dhcp-opt-no");
791
792 if (iPass == 1)
793 {
794 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
795 if (ptrConfig.isNull())
796 return RTEXITCODE_FAILURE;
797 CHECK_ERROR2I_STMT(ptrConfig, RemoveOption((DhcpOpt_T)u8OptId), rcExit = RTEXITCODE_FAILURE);
798 }
799 fNeedValueOrRemove = false;
800 break;
801
802 default:
803 return errorGetOpt(vrc, &ValueUnion);
804 }
805 }
806
807 if (iPass != 0)
808 break;
809
810 /*
811 * Ensure we've got mandatory options and supply defaults
812 * where needed (modify case)
813 */
814 if (!pCtx->pszNetwork && !pCtx->pszInterface)
815 return errorSyntax("You need to specify either --network or --interface to identify the DHCP server");
816
817 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
818 {
819 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
820 if (!pszServerIp)
821 rcExit = errorSyntax("Missing required option: --ip");
822 if (!pszNetmask)
823 rcExit = errorSyntax("Missing required option: --netmask");
824 if (!pszLowerIp)
825 rcExit = errorSyntax("Missing required option: --lowerip");
826 if (!pszUpperIp)
827 rcExit = errorSyntax("Missing required option: --upperip");
828 if (rcExit != RTEXITCODE_SUCCESS)
829 return rcExit;
830 }
831
832 /*
833 * Find or create the server.
834 */
835 HRESULT rc;
836 Bstr NetName;
837 if (!pCtx->pszNetwork)
838 {
839 ComPtr<IHost> host;
840 CHECK_ERROR(pCtx->pArg->virtualBox, COMGETTER(Host)(host.asOutParam()));
841
842 ComPtr<IHostNetworkInterface> hif;
843 CHECK_ERROR(host, FindHostNetworkInterfaceByName(Bstr(pCtx->pszInterface).mutableRaw(), hif.asOutParam()));
844 if (FAILED(rc))
845 return errorArgument("Could not find interface '%s'", pCtx->pszInterface);
846
847 CHECK_ERROR(hif, COMGETTER(NetworkName) (NetName.asOutParam()));
848 if (FAILED(rc))
849 return errorArgument("Could not get network name for the interface '%s'", pCtx->pszInterface);
850 }
851 else
852 {
853 NetName = Bstr(pCtx->pszNetwork);
854 }
855
856 rc = pCtx->pArg->virtualBox->FindDHCPServerByNetworkName(NetName.mutableRaw(), ptrDHCPServer.asOutParam());
857 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
858 {
859 if (SUCCEEDED(rc))
860 return errorArgument("DHCP server already exists");
861
862 CHECK_ERROR(pCtx->pArg->virtualBox, CreateDHCPServer(NetName.mutableRaw(), ptrDHCPServer.asOutParam()));
863 if (FAILED(rc))
864 return errorArgument("Failed to create the DHCP server");
865 }
866 else if (FAILED(rc))
867 return errorArgument("DHCP server does not exist");
868
869 /*
870 * Apply IDHCPServer settings:
871 */
872 HRESULT hrc;
873 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
874 if (pszServerIp || pszNetmask || pszLowerIp || pszUpperIp)
875 {
876 Bstr bstrServerIp(pszServerIp);
877 Bstr bstrNetmask(pszNetmask);
878 Bstr bstrLowerIp(pszLowerIp);
879 Bstr bstrUpperIp(pszUpperIp);
880
881 if (!pszServerIp)
882 {
883 CHECK_ERROR2_RET(hrc, ptrDHCPServer, COMGETTER(IPAddress)(bstrServerIp.asOutParam()), RTEXITCODE_FAILURE);
884 }
885 if (!pszNetmask)
886 {
887 CHECK_ERROR2_RET(hrc, ptrDHCPServer, COMGETTER(NetworkMask)(bstrNetmask.asOutParam()), RTEXITCODE_FAILURE);
888 }
889 if (!pszLowerIp)
890 {
891 CHECK_ERROR2_RET(hrc, ptrDHCPServer, COMGETTER(LowerIP)(bstrLowerIp.asOutParam()), RTEXITCODE_FAILURE);
892 }
893 if (!pszUpperIp)
894 {
895 CHECK_ERROR2_RET(hrc, ptrDHCPServer, COMGETTER(UpperIP)(bstrUpperIp.asOutParam()), RTEXITCODE_FAILURE);
896 }
897
898 CHECK_ERROR2_STMT(hrc, ptrDHCPServer, SetConfiguration(bstrServerIp.raw(), bstrNetmask.raw(),
899 bstrLowerIp.raw(), bstrUpperIp.raw()),
900 rcExit = errorArgument("Failed to set configuration (%ls, %ls, %ls, %ls)", bstrServerIp.raw(),
901 bstrNetmask.raw(), bstrLowerIp.raw(), bstrUpperIp.raw()));
902 }
903
904 if (fEnabled >= 0)
905 {
906 CHECK_ERROR2_STMT(hrc, ptrDHCPServer, COMSETTER(Enabled)((BOOL)fEnabled), rcExit = RTEXITCODE_FAILURE);
907 }
908 }
909
910 return rcExit;
911}
912
913
914/**
915 * Handles the 'remove' subcommand.
916 */
917static DECLCALLBACK(RTEXITCODE) dhcpdHandleRemove(PDHCPDCMDCTX pCtx, int argc, char **argv)
918{
919 /*
920 * Parse the command line.
921 */
922 static const RTGETOPTDEF s_aOptions[] =
923 {
924 DHCPD_CMD_COMMON_OPTION_DEFS(),
925 };
926
927 RTGETOPTSTATE GetState;
928 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
929 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
930
931 RTGETOPTUNION ValueUnion;
932 while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
933 {
934 switch (vrc)
935 {
936 DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
937 default:
938 return errorGetOpt(vrc, &ValueUnion);
939 }
940 }
941
942 /*
943 * Locate the server and perform the requested operation.
944 */
945 ComPtr<IDHCPServer> ptrDHCPServer = dhcpdFindServer(pCtx);
946 if (ptrDHCPServer.isNotNull())
947 {
948 HRESULT hrc;
949 CHECK_ERROR2(hrc, pCtx->pArg->virtualBox, RemoveDHCPServer(ptrDHCPServer));
950 if (SUCCEEDED(hrc))
951 return RTEXITCODE_SUCCESS;
952 errorArgument("Failed to remove server");
953 }
954 return RTEXITCODE_FAILURE;
955}
956
957
958/**
959 * Handles the 'restart' subcommand.
960 */
961static DECLCALLBACK(RTEXITCODE) dhcpdHandleRestart(PDHCPDCMDCTX pCtx, int argc, char **argv)
962{
963 /*
964 * Parse the command line.
965 */
966 static const RTGETOPTDEF s_aOptions[] =
967 {
968 DHCPD_CMD_COMMON_OPTION_DEFS(),
969 };
970
971 RTGETOPTSTATE GetState;
972 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
973 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
974
975 RTGETOPTUNION ValueUnion;
976 while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
977 {
978 switch (vrc)
979 {
980 DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
981 default:
982 return errorGetOpt(vrc, &ValueUnion);
983 }
984 }
985
986 /*
987 * Locate the server and perform the requested operation.
988 */
989 ComPtr<IDHCPServer> ptrDHCPServer = dhcpdFindServer(pCtx);
990 if (ptrDHCPServer.isNotNull())
991 {
992 HRESULT hrc;
993 CHECK_ERROR2(hrc, ptrDHCPServer, Restart());
994 if (SUCCEEDED(hrc))
995 return RTEXITCODE_SUCCESS;
996 errorArgument("Failed to restart server");
997 }
998 return RTEXITCODE_FAILURE;
999}
1000
1001
1002/**
1003 * Handles the 'findlease' subcommand.
1004 */
1005static DECLCALLBACK(RTEXITCODE) dhcpdHandleFindLease(PDHCPDCMDCTX pCtx, int argc, char **argv)
1006{
1007 /*
1008 * Parse the command line.
1009 */
1010 static const RTGETOPTDEF s_aOptions[] =
1011 {
1012 DHCPD_CMD_COMMON_OPTION_DEFS(),
1013 { "--mac-address", 'm', RTGETOPT_REQ_MACADDR },
1014
1015 };
1016
1017 bool fHaveMacAddress = false;
1018 RTMAC MacAddress = { { 0, 0, 0, 0, 0, 0 } };
1019
1020 RTGETOPTSTATE GetState;
1021 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
1022 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
1023
1024 RTGETOPTUNION ValueUnion;
1025 while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
1026 {
1027 switch (vrc)
1028 {
1029 DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
1030
1031 case 'm': // --mac-address
1032 fHaveMacAddress = true;
1033 MacAddress = ValueUnion.MacAddr;
1034 break;
1035
1036 default:
1037 return errorGetOpt(vrc, &ValueUnion);
1038 }
1039 }
1040
1041 if (!fHaveMacAddress)
1042 return errorSyntax("You need to specify a MAC address too look for");
1043
1044 /*
1045 * Locate the server and perform the requested operation.
1046 */
1047 ComPtr<IDHCPServer> ptrDHCPServer = dhcpdFindServer(pCtx);
1048 if (ptrDHCPServer.isNull())
1049 return RTEXITCODE_FAILURE;
1050
1051 char szMac[32];
1052 RTStrPrintf(szMac, sizeof(szMac), "%RTmac", &MacAddress);
1053 Bstr bstrAddress;
1054 Bstr bstrState;
1055 LONG64 secIssued = 0;
1056 LONG64 secExpire = 0;
1057 HRESULT hrc;
1058 CHECK_ERROR2(hrc, ptrDHCPServer, FindLeaseByMAC(Bstr(szMac).raw(), 0 /*type*/,
1059 bstrAddress.asOutParam(), bstrState.asOutParam(), &secIssued, &secExpire));
1060 if (SUCCEEDED(hrc))
1061 {
1062 RTTIMESPEC TimeSpec;
1063 int64_t cSecLeftToLive = secExpire - RTTimeSpecGetSeconds(RTTimeNow(&TimeSpec));
1064 RTTIME Time;
1065 char szIssued[RTTIME_STR_LEN];
1066 RTTimeToStringEx(RTTimeExplode(&Time, RTTimeSpecSetSeconds(&TimeSpec, secIssued)), szIssued, sizeof(szIssued), 0);
1067 char szExpire[RTTIME_STR_LEN];
1068 RTTimeToStringEx(RTTimeExplode(&Time, RTTimeSpecSetSeconds(&TimeSpec, secExpire)), szExpire, sizeof(szExpire), 0);
1069
1070 RTPrintf("IP Address: %ls\n"
1071 "MAC Address: %RTmac\n"
1072 "State: %ls\n"
1073 "Issued: %s (%RU64)\n"
1074 "Expire: %s (%RU64)\n"
1075 "TTL: %RU64 sec, currently %RU64 sec left\n",
1076 bstrAddress.raw(),
1077 &MacAddress,
1078 bstrState.raw(),
1079 szIssued, secIssued,
1080 szExpire, secExpire,
1081 secExpire >= secIssued ? secExpire - secIssued : 0, cSecLeftToLive > 0 ? cSecLeftToLive : 0);
1082 return RTEXITCODE_SUCCESS;
1083 }
1084 return RTEXITCODE_FAILURE;
1085}
1086
1087
1088/**
1089 * Handles the 'dhcpserver' command.
1090 */
1091RTEXITCODE handleDHCPServer(HandlerArg *pArg)
1092{
1093 /*
1094 * Command definitions.
1095 */
1096 static const DHCPDCMDDEF s_aCmdDefs[] =
1097 {
1098 { "add", dhcpdHandleAddAndModify, HELP_SCOPE_DHCPSERVER_ADD },
1099 { "modify", dhcpdHandleAddAndModify, HELP_SCOPE_DHCPSERVER_MODIFY },
1100 { "remove", dhcpdHandleRemove, HELP_SCOPE_DHCPSERVER_REMOVE },
1101 { "restart", dhcpdHandleRestart, HELP_SCOPE_DHCPSERVER_RESTART },
1102 { "findlease", dhcpdHandleFindLease, HELP_SCOPE_DHCPSERVER_FINDLEASE },
1103 };
1104
1105 /*
1106 * VBoxManage dhcpserver [common-options] subcommand ...
1107 */
1108 DHCPDCMDCTX CmdCtx;
1109 CmdCtx.pArg = pArg;
1110 CmdCtx.pCmdDef = NULL;
1111 CmdCtx.pszInterface = NULL;
1112 CmdCtx.pszNetwork = NULL;
1113
1114 static const RTGETOPTDEF s_CommonOptions[] = { DHCPD_CMD_COMMON_OPTION_DEFS() };
1115 RTGETOPTSTATE GetState;
1116 int vrc = RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_CommonOptions, RT_ELEMENTS(s_CommonOptions), 0,
1117 0 /* No sorting! */);
1118 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
1119
1120 RTGETOPTUNION ValueUnion;
1121 while ((vrc = RTGetOpt(&GetState, &ValueUnion)) != 0)
1122 {
1123 switch (vrc)
1124 {
1125 DHCPD_CMD_COMMON_OPTION_CASES(&CmdCtx, vrc, &ValueUnion);
1126
1127 case VINF_GETOPT_NOT_OPTION:
1128 {
1129 const char *pszCmd = ValueUnion.psz;
1130 uint32_t iCmd;
1131 for (iCmd = 0; iCmd < RT_ELEMENTS(s_aCmdDefs); iCmd++)
1132 if (strcmp(s_aCmdDefs[iCmd].pszName, pszCmd) == 0)
1133 {
1134 CmdCtx.pCmdDef = &s_aCmdDefs[iCmd];
1135 setCurrentSubcommand(s_aCmdDefs[iCmd].fSubcommandScope);
1136 return s_aCmdDefs[iCmd].pfnHandler(&CmdCtx, pArg->argc - GetState.iNext + 1,
1137 &pArg->argv[GetState.iNext - 1]);
1138 }
1139 return errorUnknownSubcommand(pszCmd);
1140 }
1141
1142 default:
1143 return errorGetOpt(vrc, &ValueUnion);
1144 }
1145 }
1146 return errorNoSubcommand();
1147}
1148
Note: See TracBrowser for help on using the repository browser.

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