VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/Dhcpd/Config.h@ 79824

最後變更 在這個檔案從79824是 79824,由 vboxsync 提交於 6 年 前

Dhcpd: Build fix for newer compilers. bugref:9288

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.7 KB
 
1/* $Id: Config.h 79824 2019-07-16 20:30:59Z vboxsync $ */
2/** @file
3 * DHCP server - server configuration
4 */
5
6/*
7 * Copyright (C) 2017-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#ifndef VBOX_INCLUDED_SRC_Dhcpd_Config_h
19#define VBOX_INCLUDED_SRC_Dhcpd_Config_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include "DhcpdInternal.h"
25#include <iprt/types.h>
26#include <iprt/net.h>
27#include <iprt/cpp/xml.h>
28#include <iprt/cpp/ministring.h>
29
30#include <VBox/intnet.h>
31
32#include "DhcpOptions.h"
33#include "ClientId.h"
34
35
36class Config;
37
38/**
39 * Base configuration
40 *
41 * @author bird (2019-07-15)
42 */
43class ConfigLevelBase
44{
45private:
46 /** DHCP options. */
47 optmap_t m_Options;
48protected:
49 /** Minimum lease time, zero means try next level up. */
50 uint32_t m_secMinLeaseTime;
51 /** Default lease time, zero means try next level up. */
52 uint32_t m_secDefaultLeaseTime;
53 /** Maximum lease time, zero means try next level up. */
54 uint32_t m_secMaxLeaseTime;
55
56public:
57 ConfigLevelBase()
58 : m_Options()
59 , m_secMinLeaseTime(0)
60 , m_secDefaultLeaseTime(0)
61 , m_secMaxLeaseTime(0)
62 { }
63
64 virtual ~ConfigLevelBase()
65 { }
66
67 virtual void initFromXml(xml::ElementNode const *pElm, bool fStrict, Config const *pConfig);
68 virtual const char *getType() const RT_NOEXCEPT = 0;
69 virtual const char *getName() const RT_NOEXCEPT = 0;
70
71 /**
72 * Tries to find DHCP option @a bOpt, returning an success indicator and
73 * iterator to the result.
74 */
75 bool findOption(uint8_t bOpt, optmap_t::const_iterator &a_rItRet) const RT_NOEXCEPT
76 {
77 a_rItRet = m_Options.find(bOpt);
78 return a_rItRet != m_Options.end();
79 }
80
81 /** @name Accessors
82 * @{ */
83 uint32_t getMinLeaseTime() const RT_NOEXCEPT { return m_secMinLeaseTime; }
84 uint32_t getDefaultLeaseTime() const RT_NOEXCEPT { return m_secDefaultLeaseTime; }
85 uint32_t getMaxLeaseTime() const RT_NOEXCEPT { return m_secMaxLeaseTime; }
86 /** @} */
87
88protected:
89 void i_parseOption(const xml::ElementNode *pElmOption);
90 virtual void i_parseChild(const xml::ElementNode *pElmChild, bool fStrict, Config const *pConfig);
91};
92
93
94/**
95 * Global config
96 */
97class GlobalConfig : public ConfigLevelBase
98{
99public:
100 GlobalConfig()
101 : ConfigLevelBase()
102 { }
103 void initFromXml(xml::ElementNode const *pElm, bool fStrict, Config const *pConfig) RT_OVERRIDE;
104 const char *getType() const RT_NOEXCEPT RT_OVERRIDE { return "global"; }
105 const char *getName() const RT_NOEXCEPT RT_OVERRIDE { return "GlobalConfig"; }
106};
107
108
109/**
110 * Group membership condition.
111 */
112class GroupCondition
113{
114protected:
115 /** The value. */
116 RTCString m_strValue;
117 /** Inclusive (true) or exclusive (false), latter takes precedency. */
118 bool m_fInclusive;
119
120public:
121 virtual ~GroupCondition()
122 {}
123
124 virtual int initCondition(const char *a_pszValue, bool a_fInclusive);
125 virtual bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
126 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT = 0;
127
128 /** @name accessors
129 * @{ */
130 RTCString const &getValue() const RT_NOEXCEPT { return m_strValue; }
131 bool getInclusive() const RT_NOEXCEPT { return m_fInclusive; }
132 /** @} */
133
134protected:
135 bool matchClassId(bool a_fPresent, std::vector<uint8_t> const &a_rBytes, bool fWildcard = false) const RT_NOEXCEPT;
136};
137
138/** MAC condition. */
139class GroupConditionMAC : public GroupCondition
140{
141private:
142 RTMAC m_MACAddress;
143public:
144 int initCondition(const char *a_pszValue, bool a_fInclusive) RT_OVERRIDE;
145 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
146 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT RT_OVERRIDE;
147};
148
149/** MAC wildcard condition. */
150class GroupConditionMACWildcard : public GroupCondition
151{
152public:
153 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
154 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT RT_OVERRIDE;
155};
156
157/** Vendor class ID condition. */
158class GroupConditionVendorClassID : public GroupCondition
159{
160public:
161 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
162 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT RT_OVERRIDE;
163};
164
165/** Vendor class ID wildcard condition. */
166class GroupConditionVendorClassIDWildcard : public GroupCondition
167{
168public:
169 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
170 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT RT_OVERRIDE;
171};
172
173/** User class ID condition. */
174class GroupConditionUserClassID : public GroupCondition
175{
176public:
177 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
178 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT RT_OVERRIDE;
179};
180
181/** User class ID wildcard condition. */
182class GroupConditionUserClassIDWildcard : public GroupCondition
183{
184public:
185 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass,
186 const OptUserClassId &a_ridUserClass) const RT_NOEXCEPT RT_OVERRIDE;
187};
188
189
190/**
191 * Group config
192 */
193class GroupConfig : public ConfigLevelBase
194{
195private:
196 typedef std::vector<GroupCondition *> GroupConditionVec;
197
198 /** The group name. */
199 RTCString m_strName;
200 /** Vector containing the inclusive membership conditions (must match one). */
201 GroupConditionVec m_Inclusive;
202 /** Vector containing the exclusive membership conditions (must match none). */
203 GroupConditionVec m_Exclusive;
204
205public:
206 GroupConfig()
207 : ConfigLevelBase()
208 {
209 }
210
211 void initFromXml(xml::ElementNode const *pElm, bool fStrict, Config const *pConfig) RT_OVERRIDE;
212 bool match(const ClientId &a_ridClient, const OptVendorClassId &a_ridVendorClass, const OptUserClassId &a_ridUserClass) const;
213
214 /** @name Accessors
215 * @{ */
216 const char *getType() const RT_NOEXCEPT RT_OVERRIDE { return "group"; }
217 const char *getName() const RT_NOEXCEPT RT_OVERRIDE { return m_strName.c_str(); }
218 RTCString const &getGroupName() const RT_NOEXCEPT { return m_strName; }
219 /** @} */
220
221protected:
222 void i_parseChild(const xml::ElementNode *pElmChild, bool fStrict, Config const *pConfig) RT_OVERRIDE;
223 /** Used to name unnamed groups. */
224 static uint32_t s_uGroupNo;
225};
226
227
228/**
229 * Host (MAC address) specific configuration.
230 */
231class HostConfig : public ConfigLevelBase
232{
233protected:
234 /** The MAC address. */
235 RTMAC m_MACAddress;
236 /** Name annotating the entry. */
237 RTCString m_strName;
238 /** Fixed address assignment when m_fHaveFixedAddress is true. */
239 RTNETADDRIPV4 m_FixedAddress;
240 /** Set if we have a fixed address asignment. */
241 bool m_fHaveFixedAddress;
242
243public:
244 HostConfig()
245 : ConfigLevelBase()
246 , m_fHaveFixedAddress(false)
247 {
248 RT_ZERO(m_MACAddress);
249 RT_ZERO(m_FixedAddress);
250 }
251
252 void initFromXml(xml::ElementNode const *pElm, bool fStrict, Config const *pConfig) RT_OVERRIDE;
253 const char *getType() const RT_NOEXCEPT RT_OVERRIDE { return "host"; }
254 const char *getName() const RT_NOEXCEPT RT_OVERRIDE { return m_strName.c_str(); }
255
256 /** @name Accessors
257 * @{ */
258 RTMAC const &getMACAddress() const RT_NOEXCEPT { return m_MACAddress; }
259 bool haveFixedAddress() const RT_NOEXCEPT { return m_fHaveFixedAddress; }
260 RTNETADDRIPV4 const & getFixedAddress() const RT_NOEXCEPT { return m_FixedAddress; }
261 /** @} */
262};
263
264
265/**
266 * DHCP server configuration.
267 */
268class Config
269{
270 /** Group configuration map. */
271 typedef std::map<RTCString, GroupConfig const * > GroupConfigMap;
272 /** Host configuration map. */
273 typedef std::map<RTMAC, HostConfig const * > HostConfigMap;
274
275
276 RTCString m_strHome; /**< path of ~/.VirtualBox or equivalent, */
277
278 RTCString m_strNetwork; /**< The name of the internal network the DHCP server is connected to. */
279 RTCString m_strLeasesFilename;/**< The lease DB filename. */
280
281 RTCString m_strTrunk; /**< The trunk name of the internal network. */
282 INTNETTRUNKTYPE m_enmTrunkType; /**< The trunk type of the internal network. */
283
284 RTMAC m_MacAddress; /**< The MAC address for the DHCP server. */
285
286 RTNETADDRIPV4 m_IPv4Address; /**< The IPv4 address of the DHCP server. */
287 RTNETADDRIPV4 m_IPv4Netmask; /**< The IPv4 netmask for the DHCP server. */
288
289 RTNETADDRIPV4 m_IPv4PoolFirst; /**< The first IPv4 address in the pool. */
290 RTNETADDRIPV4 m_IPv4PoolLast; /**< The last IPV4 address in the pool (inclusive like all other 'last' variables). */
291
292
293 /** The global configuration. */
294 GlobalConfig m_GlobalConfig;
295 /** The group configurations, indexed by group name. */
296 GroupConfigMap m_GroupConfigs;
297 /** The host configurations, indexed by MAC address. */
298 HostConfigMap m_HostConfigs;
299
300 /** Set if we've initialized the log already (via command line). */
301 static bool g_fInitializedLog;
302
303private:
304 Config();
305
306 int i_init() RT_NOEXCEPT;
307 int i_homeInit() RT_NOEXCEPT;
308 static Config *i_createInstanceAndCallInit() RT_NOEXCEPT;
309 int i_logInit() RT_NOEXCEPT;
310 static int i_logInitWithFilename(const char *pszFilename) RT_NOEXCEPT;
311 int i_complete() RT_NOEXCEPT;
312
313public:
314 /** @name Factory methods
315 * @{ */
316 static Config *hardcoded() RT_NOEXCEPT; /**< For testing. */
317 static Config *create(int argc, char **argv) RT_NOEXCEPT; /**< --config */
318 static Config *compat(int argc, char **argv);
319 /** @} */
320
321 /** @name Accessors
322 * @{ */
323 const RTCString &getHome() const RT_NOEXCEPT { return m_strHome; }
324
325 const RTCString &getNetwork() const RT_NOEXCEPT { return m_strNetwork; }
326 const RTCString &getLeasesFilename() const RT_NOEXCEPT { return m_strLeasesFilename; }
327
328 const RTCString &getTrunk() const RT_NOEXCEPT { return m_strTrunk; }
329 INTNETTRUNKTYPE getTrunkType() const RT_NOEXCEPT { return m_enmTrunkType; }
330
331 const RTMAC &getMacAddress() const RT_NOEXCEPT { return m_MacAddress; }
332
333 RTNETADDRIPV4 getIPv4Address() const RT_NOEXCEPT { return m_IPv4Address; }
334 RTNETADDRIPV4 getIPv4Netmask() const RT_NOEXCEPT { return m_IPv4Netmask; }
335 RTNETADDRIPV4 getIPv4PoolFirst() const RT_NOEXCEPT { return m_IPv4PoolFirst; }
336 RTNETADDRIPV4 getIPv4PoolLast() const RT_NOEXCEPT { return m_IPv4PoolLast; }
337 /** @} */
338
339 /** Gets the network (IP masked by network mask). */
340 RTNETADDRIPV4 getIPv4Network() const RT_NOEXCEPT
341 {
342 RTNETADDRIPV4 Network;
343 Network.u = m_IPv4Netmask.u & m_IPv4Address.u;
344 return Network;
345 }
346 /** Checks if the given IPv4 address is in the DHCP server network. */
347 bool isInIPv4Network(RTNETADDRIPV4 a_rAddress) const RT_NOEXCEPT
348 {
349 return (a_rAddress.u & getIPv4Netmask().u) == getIPv4Network().u;
350 }
351
352 /** Host configuration vector. */
353 typedef std::vector<HostConfig const *> HostConfigVec;
354 int getFixedAddressConfigs(HostConfigVec &a_rRetConfigs) const;
355
356 /** Configuration vector. */
357 typedef std::vector<ConfigLevelBase const *> ConfigVec;
358 ConfigVec &getConfigsForClient(ConfigVec &a_rRetConfigs, const ClientId &a_ridClient,
359 const OptVendorClassId &a_ridVendorClass,
360 const OptUserClassId &a_ridUserClass) const;
361 optmap_t &getOptionsForClient(optmap_t &a_rRetOpts, const OptParameterRequest &a_rReqOpts,
362 ConfigVec &a_rConfigs) const;
363
364private:
365 /** @name Configuration file reading and parsing
366 * @{ */
367 static Config *i_read(const char *pszFilename, bool fStrict) RT_NOEXCEPT;
368 void i_parseConfig(const xml::ElementNode *pElmRoot, bool fStrict);
369 void i_parseServer(const xml::ElementNode *pElmServer, bool fStrict);
370 /** @} */
371};
372
373#endif /* !VBOX_INCLUDED_SRC_Dhcpd_Config_h */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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