VirtualBox

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

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

Main: s/DHCPOptionEncoding_Legacy/DHCPOptionEncoding_Normal/g as 'Legacy' does not seem a good fit for the more userfriendly value encoding. bugref:9288

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.5 KB
Line 
1/* $Id: VBoxManageDHCPServer.cpp 79778 2019-07-15 00:36:08Z 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 {
267 CHECK_ERROR2I_STMT(ptrDHCPServer, GetConfig(m_enmScope, Bstr(m_pszName).raw(), m_uSlot, TRUE /*mayAdd*/,
268 m_ptrConfig.asOutParam()), m_ptrConfig.setNull());
269 }
270 return m_ptrConfig;
271 }
272
273 ComPtr<IDHCPIndividualConfig> &getIndividual(ComPtr<IDHCPServer> const &ptrDHCPServer)
274 {
275 getConfig(ptrDHCPServer);
276 if (m_ptrIndividualConfig.isNull() && m_ptrConfig.isNotNull())
277 {
278 HRESULT hrc = m_ptrConfig.queryInterfaceTo(m_ptrIndividualConfig.asOutParam());
279 if (FAILED(hrc))
280 {
281 com::GlueHandleComError(m_ptrConfig, "queryInterface", hrc, __FILE__, __LINE__);
282 m_ptrIndividualConfig.setNull();
283 }
284 }
285 return m_ptrIndividualConfig;
286 }
287
288 ComPtr<IDHCPGroupConfig> &getGroup(ComPtr<IDHCPServer> const &ptrDHCPServer)
289 {
290 getConfig(ptrDHCPServer);
291 if (m_ptrGroupConfig.isNull() && m_ptrConfig.isNotNull())
292 {
293 HRESULT hrc = m_ptrConfig.queryInterfaceTo(m_ptrGroupConfig.asOutParam());
294 if (FAILED(hrc))
295 {
296 com::GlueHandleComError(m_ptrConfig, "queryInterface", hrc, __FILE__, __LINE__);
297 m_ptrGroupConfig.setNull();
298 }
299 }
300 return m_ptrGroupConfig;
301 }
302
303 DHCPConfigScope_T getScope() const { return m_enmScope; }
304
305private:
306 void resetPointers()
307 {
308 m_ptrConfig.setNull();
309 m_ptrGlobalConfig.setNull();
310 m_ptrIndividualConfig.setNull();
311 m_ptrGroupConfig.setNull();
312 }
313};
314
315enum
316{
317 DHCP_ADDMOD = 1000,
318 DHCP_ADDMOD_ZAP_OPTIONS,
319 DHCP_ADDMOD_INCL_MAC,
320 DHCP_ADDMOD_EXCL_MAC,
321 DHCP_ADDMOD_DEL_MAC,
322 DHCP_ADDMOD_INCL_MAC_WILD,
323 DHCP_ADDMOD_EXCL_MAC_WILD,
324 DHCP_ADDMOD_DEL_MAC_WILD,
325 DHCP_ADDMOD_INCL_VENDOR,
326 DHCP_ADDMOD_EXCL_VENDOR,
327 DHCP_ADDMOD_DEL_VENDOR,
328 DHCP_ADDMOD_INCL_VENDOR_WILD,
329 DHCP_ADDMOD_EXCL_VENDOR_WILD,
330 DHCP_ADDMOD_DEL_VENDOR_WILD,
331 DHCP_ADDMOD_INCL_USER,
332 DHCP_ADDMOD_EXCL_USER,
333 DHCP_ADDMOD_DEL_USER,
334 DHCP_ADDMOD_INCL_USER_WILD,
335 DHCP_ADDMOD_EXCL_USER_WILD,
336 DHCP_ADDMOD_DEL_USER_WILD,
337 DHCP_ADDMOD_ZAP_CONDITIONS
338};
339
340/**
341 * Handles the 'add' and 'modify' subcommands.
342 */
343static DECLCALLBACK(RTEXITCODE) dhcpdHandleAddAndModify(PDHCPDCMDCTX pCtx, int argc, char **argv)
344{
345 static const RTGETOPTDEF s_aOptions[] =
346 {
347 DHCPD_CMD_COMMON_OPTION_DEFS(),
348 { "--server-ip", 'a', RTGETOPT_REQ_STRING },
349 { "--ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
350 { "-ip", 'a', RTGETOPT_REQ_STRING }, // deprecated
351 { "--netmask", 'm', RTGETOPT_REQ_STRING },
352 { "-netmask", 'm', RTGETOPT_REQ_STRING }, // deprecated
353 { "--lower-ip", 'l', RTGETOPT_REQ_STRING },
354 { "--lowerip", 'l', RTGETOPT_REQ_STRING },
355 { "-lowerip", 'l', RTGETOPT_REQ_STRING }, // deprecated
356 { "--upper-ip", 'u', RTGETOPT_REQ_STRING },
357 { "--upperip", 'u', RTGETOPT_REQ_STRING },
358 { "-upperip", 'u', RTGETOPT_REQ_STRING }, // deprecated
359 { "--enable", 'e', RTGETOPT_REQ_NOTHING },
360 { "-enable", 'e', RTGETOPT_REQ_NOTHING }, // deprecated
361 { "--disable", 'd', RTGETOPT_REQ_NOTHING },
362 { "-disable", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
363 { "--global", 'g', RTGETOPT_REQ_NOTHING },
364 { "--group", 'G', RTGETOPT_REQ_STRING },
365 { "--mac-address", 'E', RTGETOPT_REQ_MACADDR },
366 { "--vm", 'M', RTGETOPT_REQ_STRING },
367 { "--nic", 'n', RTGETOPT_REQ_UINT8 },
368 { "--set-opt", 's', RTGETOPT_REQ_UINT8 },
369 { "--set-opt-hex", 'x', RTGETOPT_REQ_UINT8 },
370 { "--del-opt", 'D', RTGETOPT_REQ_UINT8 },
371 { "--zap-options", DHCP_ADDMOD_ZAP_OPTIONS, RTGETOPT_REQ_NOTHING },
372 { "--min-lease-time", 'q' , RTGETOPT_REQ_UINT32 },
373 { "--default-lease-time", 'L' , RTGETOPT_REQ_UINT32 },
374 { "--max-lease-time", 'Q' , RTGETOPT_REQ_UINT32 },
375 { "--remove-config", 'R', RTGETOPT_REQ_NOTHING },
376 { "--fixed-address", 'f', RTGETOPT_REQ_STRING },
377 /* group conditions: */
378 { "--incl-mac", DHCP_ADDMOD_INCL_MAC, RTGETOPT_REQ_STRING },
379 { "--excl-mac", DHCP_ADDMOD_EXCL_MAC, RTGETOPT_REQ_STRING },
380 { "--del-mac", DHCP_ADDMOD_DEL_MAC, RTGETOPT_REQ_STRING },
381 { "--incl-mac-wild", DHCP_ADDMOD_INCL_MAC_WILD, RTGETOPT_REQ_STRING },
382 { "--excl-mac-wild", DHCP_ADDMOD_EXCL_MAC_WILD, RTGETOPT_REQ_STRING },
383 { "--del-mac-wild", DHCP_ADDMOD_DEL_MAC_WILD, RTGETOPT_REQ_STRING },
384 { "--incl-vendor", DHCP_ADDMOD_INCL_VENDOR, RTGETOPT_REQ_STRING },
385 { "--excl-vendor", DHCP_ADDMOD_EXCL_VENDOR, RTGETOPT_REQ_STRING },
386 { "--del-vendor", DHCP_ADDMOD_DEL_VENDOR, RTGETOPT_REQ_STRING },
387 { "--incl-vendor-wild", DHCP_ADDMOD_INCL_VENDOR_WILD, RTGETOPT_REQ_STRING },
388 { "--excl-vendor-wild", DHCP_ADDMOD_EXCL_VENDOR_WILD, RTGETOPT_REQ_STRING },
389 { "--del-vendor-wild", DHCP_ADDMOD_DEL_VENDOR_WILD, RTGETOPT_REQ_STRING },
390 { "--incl-user", DHCP_ADDMOD_INCL_USER, RTGETOPT_REQ_STRING },
391 { "--excl-user", DHCP_ADDMOD_EXCL_USER, RTGETOPT_REQ_STRING },
392 { "--del-user", DHCP_ADDMOD_DEL_USER, RTGETOPT_REQ_STRING },
393 { "--incl-user-wild", DHCP_ADDMOD_INCL_USER_WILD, RTGETOPT_REQ_STRING },
394 { "--excl-user-wild", DHCP_ADDMOD_EXCL_USER_WILD, RTGETOPT_REQ_STRING },
395 { "--del-user-wild", DHCP_ADDMOD_DEL_USER_WILD, RTGETOPT_REQ_STRING },
396 { "--zap-conditions", DHCP_ADDMOD_ZAP_CONDITIONS, RTGETOPT_REQ_NOTHING },
397 /* obsolete, to be removed: */
398 { "--id", 'i', RTGETOPT_REQ_UINT8 }, // obsolete, backwards compatibility only.
399 { "--value", 'p', RTGETOPT_REQ_STRING }, // obsolete, backwards compatibility only.
400 { "--remove", 'r', RTGETOPT_REQ_NOTHING }, // obsolete, backwards compatibility only.
401 { "--options", 'o', RTGETOPT_REQ_NOTHING }, // obsolete legacy, ignored
402
403 };
404
405 /*
406 * Parse the arguments in two passes:
407 *
408 * 1. Validate the command line and establish the IDHCPServer settings.
409 * 2. Execute the various IDHCPConfig settings changes.
410 *
411 * This is considered simpler than duplicating the command line instructions
412 * into elaborate structures and executing these.
413 */
414 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
415 ComPtr<IDHCPServer> ptrDHCPServer;
416 for (size_t iPass = 0; iPass < 2; iPass++)
417 {
418 const char *pszServerIp = NULL;
419 const char *pszNetmask = NULL;
420 const char *pszLowerIp = NULL;
421 const char *pszUpperIp = NULL;
422 int fEnabled = -1;
423
424 DHCPCmdScope Scope;
425 char szMACAddress[32];
426
427 bool fNeedValueOrRemove = false; /* Only used with --id; remove in 6.1+ */
428 uint8_t u8OptId = 0; /* Only used too keep --id for following --value/--remove. remove in 6.1+ */
429
430 RTGETOPTSTATE GetState;
431 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
432 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
433
434 RTGETOPTUNION ValueUnion;
435 while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
436 {
437 switch (vrc)
438 {
439 DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
440 case 'a': // --server-ip
441 pszServerIp = ValueUnion.psz;
442 break;
443 case 'm': // --netmask
444 pszNetmask = ValueUnion.psz;
445 break;
446 case 'l': // --lower-ip
447 pszLowerIp = ValueUnion.psz;
448 break;
449 case 'u': // --upper-ip
450 pszUpperIp = ValueUnion.psz;
451 break;
452 case 'e': // --enable
453 fEnabled = 1;
454 break;
455 case 'd': // --disable
456 fEnabled = 0;
457 break;
458
459 /*
460 * Configuration selection:
461 */
462 case 'g': // --global Sets the option scope to 'global'.
463 if (fNeedValueOrRemove)
464 return errorSyntax("Incomplete option sequence preseeding '--global'");
465 Scope.setGlobal();
466 break;
467
468 case 'G': // --group
469 if (fNeedValueOrRemove)
470 return errorSyntax("Incomplete option sequence preseeding '--group'");
471 if (!*ValueUnion.psz)
472 return errorSyntax("Group name cannot be empty");
473 Scope.setGroup(ValueUnion.psz);
474 break;
475
476 case 'E': // --mac-address
477 if (fNeedValueOrRemove)
478 return errorSyntax("Incomplete option sequence preseeding '--mac-address'");
479 RTStrPrintf(szMACAddress, sizeof(szMACAddress), "%RTmac", &ValueUnion.MacAddr);
480 Scope.setMACAddress(szMACAddress);
481 break;
482
483 case 'M': // --vm Sets the option scope to ValueUnion.psz + 0.
484 if (fNeedValueOrRemove)
485 return errorSyntax("Incomplete option sequence preseeding '--vm'");
486 Scope.setMachineNIC(ValueUnion.psz);
487 break;
488
489 case 'n': // --nic Sets the option scope to pszVmName + (ValueUnion.u8 - 1).
490 if (Scope.getScope() != DHCPConfigScope_MachineNIC)
491 return errorSyntax("--nic option requires a --vm preceeding selecting the VM it should apply to");
492 if (fNeedValueOrRemove)
493 return errorSyntax("Incomplete option sequence preseeding '--nic=%u", ValueUnion.u8);
494 if (ValueUnion.u8 < 1)
495 return errorSyntax("invalid NIC number: %u", ValueUnion.u8);
496 Scope.setMachineSlot(ValueUnion.u8 - 1);
497 break;
498
499 /*
500 * Modify configuration:
501 */
502 case 's': // --set-opt num stringvalue
503 {
504 uint8_t const idAddOpt = ValueUnion.u8;
505 vrc = RTGetOptFetchValue(&GetState, &ValueUnion, RTGETOPT_REQ_STRING);
506 if (RT_FAILURE(vrc))
507 return errorFetchValue(1, "--set-opt", vrc, &ValueUnion);
508 if (iPass == 1)
509 {
510 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
511 if (ptrConfig.isNull())
512 return RTEXITCODE_FAILURE;
513 CHECK_ERROR2I_STMT(ptrConfig, SetOption((DhcpOpt_T)idAddOpt, DHCPOptionEncoding_Normal,
514 Bstr(ValueUnion.psz).raw()), rcExit = RTEXITCODE_FAILURE);
515 }
516 break;
517 }
518
519 case 'x': // --set-opt-hex num hex-string
520 {
521 uint8_t const idAddOpt = ValueUnion.u8;
522 vrc = RTGetOptFetchValue(&GetState, &ValueUnion, RTGETOPT_REQ_STRING);
523 if (RT_FAILURE(vrc))
524 return errorFetchValue(1, "--set-opt-hex", vrc, &ValueUnion);
525 uint8_t abBuf[256];
526 size_t cbRet;
527 vrc = RTStrConvertHexBytesEx(ValueUnion.psz, abBuf, sizeof(abBuf), RTSTRCONVERTHEXBYTES_F_SEP_COLON,
528 NULL, &cbRet);
529 if (RT_FAILURE(vrc))
530 return errorArgument("Malformed hex string given to --set-opt-hex %u: %s\n", idAddOpt, ValueUnion.psz);
531 if (iPass == 1)
532 {
533 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
534 if (ptrConfig.isNull())
535 return RTEXITCODE_FAILURE;
536 CHECK_ERROR2I_STMT(ptrConfig, SetOption((DhcpOpt_T)idAddOpt, DHCPOptionEncoding_Hex,
537 Bstr(ValueUnion.psz).raw()), rcExit = RTEXITCODE_FAILURE);
538 }
539 break;
540 }
541
542 case 'D': // --del-opt num
543 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
544 return errorSyntax("--del-opt does not apply to the 'add' subcommand");
545 if (iPass == 1)
546 {
547 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
548 if (ptrConfig.isNull())
549 return RTEXITCODE_FAILURE;
550 CHECK_ERROR2I_STMT(ptrConfig, RemoveOption((DhcpOpt_T)ValueUnion.u8), rcExit = RTEXITCODE_FAILURE);
551 }
552 break;
553
554 case DHCP_ADDMOD_ZAP_OPTIONS:
555 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
556 return errorSyntax("--zap-options does not apply to the 'add' subcommand");
557 if (iPass == 1)
558 {
559 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
560 if (ptrConfig.isNull())
561 return RTEXITCODE_FAILURE;
562 CHECK_ERROR2I_STMT(ptrConfig, RemoveAllOptions(), rcExit = RTEXITCODE_FAILURE);
563 }
564 break;
565
566 case 'q': // --min-lease-time
567 if (iPass == 1)
568 {
569 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
570 if (ptrConfig.isNull())
571 return RTEXITCODE_FAILURE;
572 CHECK_ERROR2I_STMT(ptrConfig, COMSETTER(MinLeaseTime)(ValueUnion.u32), rcExit = RTEXITCODE_FAILURE);
573 }
574 break;
575
576 case 'L': // --default-lease-time
577 if (iPass == 1)
578 {
579 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
580 if (ptrConfig.isNull())
581 return RTEXITCODE_FAILURE;
582 CHECK_ERROR2I_STMT(ptrConfig, COMSETTER(DefaultLeaseTime)(ValueUnion.u32), rcExit = RTEXITCODE_FAILURE);
583 }
584 break;
585
586 case 'Q': // --max-lease-time
587 if (iPass == 1)
588 {
589 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
590 if (ptrConfig.isNull())
591 return RTEXITCODE_FAILURE;
592 CHECK_ERROR2I_STMT(ptrConfig, COMSETTER(MaxLeaseTime)(ValueUnion.u32), rcExit = RTEXITCODE_FAILURE);
593 }
594 break;
595
596 case 'R': // --remove-config
597 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
598 return errorSyntax("--remove-config does not apply to the 'add' subcommand");
599 if (Scope.getScope() == DHCPConfigScope_Global)
600 return errorSyntax("--remove-config cannot be applied to the global config");
601 if (iPass == 1)
602 {
603 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
604 if (ptrConfig.isNull())
605 return RTEXITCODE_FAILURE;
606 CHECK_ERROR2I_STMT(ptrConfig, Remove(), rcExit = RTEXITCODE_FAILURE);
607 }
608 Scope.setGlobal();
609 break;
610
611 case 'f': // --fixed-address
612 if (Scope.getScope() != DHCPConfigScope_MachineNIC && Scope.getScope() != DHCPConfigScope_MAC)
613 return errorSyntax("--fixed-address can only be applied to a VM NIC or an MAC address");
614 if (iPass == 1)
615 {
616 ComPtr<IDHCPIndividualConfig> &ptrIndividualConfig = Scope.getIndividual(ptrDHCPServer);
617 if (ptrIndividualConfig.isNull())
618 return RTEXITCODE_FAILURE;
619 CHECK_ERROR2I_STMT(ptrIndividualConfig, COMSETTER(FixedAddress)(Bstr(ValueUnion.psz).raw()),
620 rcExit = RTEXITCODE_FAILURE);
621 }
622 break;
623
624 /*
625 * Group conditions:
626 */
627 case DHCP_ADDMOD_INCL_MAC:
628 case DHCP_ADDMOD_EXCL_MAC:
629 case DHCP_ADDMOD_DEL_MAC:
630 case DHCP_ADDMOD_INCL_MAC_WILD:
631 case DHCP_ADDMOD_EXCL_MAC_WILD:
632 case DHCP_ADDMOD_DEL_MAC_WILD:
633 case DHCP_ADDMOD_INCL_VENDOR:
634 case DHCP_ADDMOD_EXCL_VENDOR:
635 case DHCP_ADDMOD_DEL_VENDOR:
636 case DHCP_ADDMOD_INCL_VENDOR_WILD:
637 case DHCP_ADDMOD_EXCL_VENDOR_WILD:
638 case DHCP_ADDMOD_DEL_VENDOR_WILD:
639 case DHCP_ADDMOD_INCL_USER:
640 case DHCP_ADDMOD_EXCL_USER:
641 case DHCP_ADDMOD_DEL_USER:
642 case DHCP_ADDMOD_INCL_USER_WILD:
643 case DHCP_ADDMOD_EXCL_USER_WILD:
644 case DHCP_ADDMOD_DEL_USER_WILD:
645 {
646 if (Scope.getScope() != DHCPConfigScope_Group)
647 return errorSyntax("A group must be selected to perform condition alterations.");
648 if (!*ValueUnion.psz)
649 return errorSyntax("Condition value cannot be empty"); /* or can it? */
650 if (iPass != 1)
651 break;
652
653 DHCPGroupConditionType_T enmType;
654 switch (vrc)
655 {
656 case DHCP_ADDMOD_INCL_MAC: case DHCP_ADDMOD_EXCL_MAC: case DHCP_ADDMOD_DEL_MAC:
657 enmType = DHCPGroupConditionType_MAC;
658 break;
659 case DHCP_ADDMOD_INCL_MAC_WILD: case DHCP_ADDMOD_EXCL_MAC_WILD: case DHCP_ADDMOD_DEL_MAC_WILD:
660 enmType = DHCPGroupConditionType_MACWildcard;
661 break;
662 case DHCP_ADDMOD_INCL_VENDOR: case DHCP_ADDMOD_EXCL_VENDOR: case DHCP_ADDMOD_DEL_VENDOR:
663 enmType = DHCPGroupConditionType_vendorClassID;
664 break;
665 case DHCP_ADDMOD_INCL_VENDOR_WILD: case DHCP_ADDMOD_EXCL_VENDOR_WILD: case DHCP_ADDMOD_DEL_VENDOR_WILD:
666 enmType = DHCPGroupConditionType_vendorClassIDWildcard;
667 break;
668 case DHCP_ADDMOD_INCL_USER: case DHCP_ADDMOD_EXCL_USER: case DHCP_ADDMOD_DEL_USER:
669 enmType = DHCPGroupConditionType_userClassID;
670 break;
671 case DHCP_ADDMOD_INCL_USER_WILD: case DHCP_ADDMOD_EXCL_USER_WILD: case DHCP_ADDMOD_DEL_USER_WILD:
672 enmType = DHCPGroupConditionType_userClassIDWildcard;
673 break;
674 default:
675 AssertFailedReturn(RTEXITCODE_FAILURE);
676 }
677
678 int fInclusive;
679 switch (vrc)
680 {
681 case DHCP_ADDMOD_DEL_MAC:
682 case DHCP_ADDMOD_DEL_MAC_WILD:
683 case DHCP_ADDMOD_DEL_USER:
684 case DHCP_ADDMOD_DEL_USER_WILD:
685 case DHCP_ADDMOD_DEL_VENDOR:
686 case DHCP_ADDMOD_DEL_VENDOR_WILD:
687 fInclusive = -1;
688 break;
689 case DHCP_ADDMOD_EXCL_MAC:
690 case DHCP_ADDMOD_EXCL_MAC_WILD:
691 case DHCP_ADDMOD_EXCL_USER:
692 case DHCP_ADDMOD_EXCL_USER_WILD:
693 case DHCP_ADDMOD_EXCL_VENDOR:
694 case DHCP_ADDMOD_EXCL_VENDOR_WILD:
695 fInclusive = 0;
696 break;
697 case DHCP_ADDMOD_INCL_MAC:
698 case DHCP_ADDMOD_INCL_MAC_WILD:
699 case DHCP_ADDMOD_INCL_USER:
700 case DHCP_ADDMOD_INCL_USER_WILD:
701 case DHCP_ADDMOD_INCL_VENDOR:
702 case DHCP_ADDMOD_INCL_VENDOR_WILD:
703 fInclusive = 1;
704 break;
705 default:
706 AssertFailedReturn(RTEXITCODE_FAILURE);
707 }
708
709 ComPtr<IDHCPGroupConfig> &ptrGroupConfig = Scope.getGroup(ptrDHCPServer);
710 if (ptrGroupConfig.isNull())
711 return RTEXITCODE_FAILURE;
712 if (fInclusive >= 0)
713 {
714 ComPtr<IDHCPGroupCondition> ptrCondition;
715 CHECK_ERROR2I_STMT(ptrGroupConfig, AddCondition((BOOL)fInclusive, enmType, Bstr(ValueUnion.psz).raw(),
716 ptrCondition.asOutParam()), rcExit = RTEXITCODE_FAILURE);
717 }
718 else
719 {
720 com::SafeIfaceArray<IDHCPGroupCondition> Conditions;
721 CHECK_ERROR2I_STMT(ptrGroupConfig, COMGETTER(Conditions)(ComSafeArrayAsOutParam(Conditions)),
722 rcExit = RTEXITCODE_FAILURE; break);
723 bool fFound = false;
724 for (size_t iCond = 0; iCond < Conditions.size(); iCond++)
725 {
726 DHCPGroupConditionType_T enmCurType = DHCPGroupConditionType_MAC;
727 CHECK_ERROR2I_STMT(Conditions[iCond], COMGETTER(Type)(&enmCurType),
728 rcExit = RTEXITCODE_FAILURE; continue);
729 if (enmCurType == enmType)
730 {
731 Bstr bstrValue;
732 CHECK_ERROR2I_STMT(Conditions[iCond], COMGETTER(Value)(bstrValue.asOutParam()),
733 rcExit = RTEXITCODE_FAILURE; continue);
734 if (RTUtf16CmpUtf8(bstrValue.raw(), ValueUnion.psz) == 0)
735 {
736 CHECK_ERROR2I_STMT(Conditions[iCond], Remove(), rcExit = RTEXITCODE_FAILURE);
737 fFound = true;
738 }
739 }
740 }
741 if (!fFound)
742 rcExit = RTMsgErrorExitFailure("Could not find any condition of type %d with value '%s' to delete",
743 enmType, ValueUnion.psz);
744 }
745 break;
746 }
747
748 case DHCP_ADDMOD_ZAP_CONDITIONS:
749 if (Scope.getScope() != DHCPConfigScope_Group)
750 return errorSyntax("--zap-conditions can only be with a group selected");
751 if (iPass == 1)
752 {
753 ComPtr<IDHCPGroupConfig> &ptrGroupConfig = Scope.getGroup(ptrDHCPServer);
754 if (ptrGroupConfig.isNull())
755 return RTEXITCODE_FAILURE;
756 CHECK_ERROR2I_STMT(ptrGroupConfig, RemoveAllConditions(), rcExit = RTEXITCODE_FAILURE);
757 }
758 break;
759
760 /*
761 * For backwards compatibility. Remove in 6.1 or later.
762 */
763
764 case 'o': // --options - obsolete, ignored.
765 break;
766
767 case 'i': // --id
768 if (fNeedValueOrRemove)
769 return errorSyntax("Incomplete option sequence preseeding '--id=%u", ValueUnion.u8);
770 u8OptId = ValueUnion.u8;
771 fNeedValueOrRemove = true;
772 break;
773
774 case 'p': // --value
775 if (!fNeedValueOrRemove)
776 return errorSyntax("--value without --id=dhcp-opt-no");
777 if (iPass == 1)
778 {
779 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
780 if (ptrConfig.isNull())
781 return RTEXITCODE_FAILURE;
782 CHECK_ERROR2I_STMT(ptrConfig, SetOption((DhcpOpt_T)u8OptId, DHCPOptionEncoding_Normal,
783 Bstr(ValueUnion.psz).raw()), rcExit = RTEXITCODE_FAILURE);
784 }
785 fNeedValueOrRemove = false;
786 break;
787
788 case 'r': // --remove
789 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
790 return errorSyntax("--remove does not apply to the 'add' subcommand");
791 if (!fNeedValueOrRemove)
792 return errorSyntax("--remove without --id=dhcp-opt-no");
793
794 if (iPass == 1)
795 {
796 ComPtr<IDHCPConfig> &ptrConfig = Scope.getConfig(ptrDHCPServer);
797 if (ptrConfig.isNull())
798 return RTEXITCODE_FAILURE;
799 CHECK_ERROR2I_STMT(ptrConfig, RemoveOption((DhcpOpt_T)u8OptId), rcExit = RTEXITCODE_FAILURE);
800 }
801 fNeedValueOrRemove = false;
802 break;
803
804 default:
805 return errorGetOpt(vrc, &ValueUnion);
806 }
807 }
808
809 if (iPass != 0)
810 break;
811
812 /*
813 * Ensure we've got mandatory options and supply defaults
814 * where needed (modify case)
815 */
816 if (!pCtx->pszNetwork && !pCtx->pszInterface)
817 return errorSyntax("You need to specify either --network or --interface to identify the DHCP server");
818
819 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
820 {
821 if (!pszServerIp)
822 rcExit = errorSyntax("Missing required option: --ip");
823 if (!pszNetmask)
824 rcExit = errorSyntax("Missing required option: --netmask");
825 if (!pszLowerIp)
826 rcExit = errorSyntax("Missing required option: --lowerip");
827 if (!pszUpperIp)
828 rcExit = errorSyntax("Missing required option: --upperip");
829 if (rcExit != RTEXITCODE_SUCCESS)
830 return rcExit;
831 }
832
833 /*
834 * Find or create the server.
835 */
836 HRESULT rc;
837 Bstr NetName;
838 if (!pCtx->pszNetwork)
839 {
840 ComPtr<IHost> host;
841 CHECK_ERROR(pCtx->pArg->virtualBox, COMGETTER(Host)(host.asOutParam()));
842
843 ComPtr<IHostNetworkInterface> hif;
844 CHECK_ERROR(host, FindHostNetworkInterfaceByName(Bstr(pCtx->pszInterface).mutableRaw(), hif.asOutParam()));
845 if (FAILED(rc))
846 return errorArgument("Could not find interface '%s'", pCtx->pszInterface);
847
848 CHECK_ERROR(hif, COMGETTER(NetworkName) (NetName.asOutParam()));
849 if (FAILED(rc))
850 return errorArgument("Could not get network name for the interface '%s'", pCtx->pszInterface);
851 }
852 else
853 {
854 NetName = Bstr(pCtx->pszNetwork);
855 }
856
857 rc = pCtx->pArg->virtualBox->FindDHCPServerByNetworkName(NetName.mutableRaw(), ptrDHCPServer.asOutParam());
858 if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
859 {
860 if (SUCCEEDED(rc))
861 return errorArgument("DHCP server already exists");
862
863 CHECK_ERROR(pCtx->pArg->virtualBox, CreateDHCPServer(NetName.mutableRaw(), ptrDHCPServer.asOutParam()));
864 if (FAILED(rc))
865 return errorArgument("Failed to create the DHCP server");
866 }
867 else if (FAILED(rc))
868 return errorArgument("DHCP server does not exist");
869
870 /*
871 * Apply IDHCPServer settings:
872 */
873 HRESULT hrc;
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