1 | /* $Id: Db.h 79819 2019-07-16 19:57:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - address database
|
---|
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.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 | #ifndef VBOX_INCLUDED_SRC_Dhcpd_Db_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Dhcpd_Db_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include "DhcpdInternal.h"
|
---|
25 | #include <iprt/net.h>
|
---|
26 |
|
---|
27 | #include <iprt/cpp/ministring.h>
|
---|
28 | #include <iprt/cpp/xml.h>
|
---|
29 |
|
---|
30 | #include <list>
|
---|
31 |
|
---|
32 | #include "Timestamp.h"
|
---|
33 | #include "ClientId.h"
|
---|
34 | #include "IPv4Pool.h"
|
---|
35 | #include "Config.h"
|
---|
36 | #include "DhcpMessage.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * An address binding in the lease database.
|
---|
41 | *
|
---|
42 | * This is how an allocated IPv4 address is mananged.
|
---|
43 | */
|
---|
44 | class Binding
|
---|
45 | {
|
---|
46 | friend class Db;
|
---|
47 |
|
---|
48 | public:
|
---|
49 | enum State { FREE, RELEASED, EXPIRED, OFFERED, ACKED };
|
---|
50 |
|
---|
51 | private:
|
---|
52 | const RTNETADDRIPV4 m_addr;
|
---|
53 | State m_state;
|
---|
54 | ClientId m_id;
|
---|
55 | Timestamp m_issued;
|
---|
56 | uint32_t m_secLease;
|
---|
57 | /** Set if this is a fixed assignment. */
|
---|
58 | bool m_fFixed;
|
---|
59 |
|
---|
60 | public:
|
---|
61 | Binding();
|
---|
62 | Binding(const Binding &);
|
---|
63 |
|
---|
64 | explicit Binding(RTNETADDRIPV4 a_Addr)
|
---|
65 | : m_addr(a_Addr), m_state(FREE), m_issued(), m_secLease(0), m_fFixed(false)
|
---|
66 | {}
|
---|
67 |
|
---|
68 | Binding(RTNETADDRIPV4 a_Addr, const ClientId &a_id)
|
---|
69 | : m_addr(a_Addr), m_state(FREE), m_id(a_id), m_issued(), m_secLease(0), m_fFixed(false)
|
---|
70 | {}
|
---|
71 |
|
---|
72 | Binding(RTNETADDRIPV4 a_Addr, const RTMAC &a_MACAddress, bool a_fFixed)
|
---|
73 | : m_addr(a_Addr)
|
---|
74 | , m_state(ACKED)
|
---|
75 | , m_id(ClientId(a_MACAddress, OptClientId()))
|
---|
76 | , m_issued(Timestamp::now())
|
---|
77 | , m_secLease(UINT32_MAX - 1)
|
---|
78 | , m_fFixed(a_fFixed)
|
---|
79 | {}
|
---|
80 |
|
---|
81 |
|
---|
82 | /** @name Attribute accessors
|
---|
83 | * @{ */
|
---|
84 | RTNETADDRIPV4 addr() const RT_NOEXCEPT { return m_addr; }
|
---|
85 |
|
---|
86 | const ClientId &id() const RT_NOEXCEPT { return m_id; }
|
---|
87 | void idUpdate(const ClientId &a_ridClient);
|
---|
88 |
|
---|
89 | uint32_t leaseTime() const RT_NOEXCEPT { return m_secLease; }
|
---|
90 | Timestamp issued() const RT_NOEXCEPT { return m_issued; }
|
---|
91 |
|
---|
92 | State state() const RT_NOEXCEPT { return m_state; }
|
---|
93 | const char *stateName() const RT_NOEXCEPT;
|
---|
94 | Binding &setState(const char *pszStateName) RT_NOEXCEPT;
|
---|
95 | Binding &setState(State stateParam) RT_NOEXCEPT
|
---|
96 | {
|
---|
97 | m_state = stateParam;
|
---|
98 | return *this;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool isFixed() const RT_NOEXCEPT { return m_fFixed; }
|
---|
102 | /** @} */
|
---|
103 |
|
---|
104 |
|
---|
105 | Binding &setLeaseTime(uint32_t secLease) RT_NOEXCEPT
|
---|
106 | {
|
---|
107 | m_issued = Timestamp::now();
|
---|
108 | m_secLease = secLease;
|
---|
109 | return *this;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Reassigns the binding to the given client. */
|
---|
113 | Binding &giveTo(const ClientId &a_id) RT_NOEXCEPT
|
---|
114 | {
|
---|
115 | m_id = a_id;
|
---|
116 | m_state = FREE;
|
---|
117 | return *this;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void free()
|
---|
121 | {
|
---|
122 | m_id = ClientId();
|
---|
123 | m_state = FREE;
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool expire(Timestamp tsDeadline) RT_NOEXCEPT;
|
---|
127 | bool expire() RT_NOEXCEPT
|
---|
128 | {
|
---|
129 | return expire(Timestamp::now());
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** @name Serialization
|
---|
133 | * @{ */
|
---|
134 | static Binding *fromXML(const xml::ElementNode *pElmLease);
|
---|
135 | void toXML(xml::ElementNode *pElmParent) const;
|
---|
136 | /** @} */
|
---|
137 |
|
---|
138 | /** @name String formatting of %R[binding].
|
---|
139 | * @{ */
|
---|
140 | static void registerFormat() RT_NOEXCEPT;
|
---|
141 | private:
|
---|
142 | static DECLCALLBACK(size_t) rtStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char *pszType,
|
---|
143 | void const *pvValue, int cchWidth, int cchPrecision, unsigned fFlags, void *pvUser);
|
---|
144 | static bool g_fFormatRegistered;
|
---|
145 | /** @} */
|
---|
146 |
|
---|
147 | Binding &operator=(const Binding &); /**< Shuts up warning C4626 (incorrect warning?). */
|
---|
148 | };
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * The lease database.
|
---|
153 | *
|
---|
154 | * There is currently just one instance of this class in a running DHCP server
|
---|
155 | * residing in Dhcpd::m_db. It covers one single range of IPv4 addresses, which
|
---|
156 | * currently unbound addressed are managed by m_pool. The allocated addresses
|
---|
157 | * are kept in the m_bindings list. Once an address has been allocated, it will
|
---|
158 | * stay in the m_bindings list even after released or expired.
|
---|
159 | */
|
---|
160 | class Db
|
---|
161 | {
|
---|
162 | private:
|
---|
163 | typedef std::list<Binding *> bindings_t;
|
---|
164 |
|
---|
165 | /** Configuration (set at init).
|
---|
166 | * @note Currently not used. */
|
---|
167 | const Config *m_pConfig;
|
---|
168 | /** The lease database.
|
---|
169 | * @note Since fixed assignments are added during initialization, they will
|
---|
170 | * always be first. The allocateBinding() code depends on this. */
|
---|
171 | bindings_t m_bindings;
|
---|
172 | /** Address allocation pool. */
|
---|
173 | IPv4Pool m_pool;
|
---|
174 |
|
---|
175 | public:
|
---|
176 | Db();
|
---|
177 | ~Db();
|
---|
178 |
|
---|
179 | int init(const Config *pConfig);
|
---|
180 |
|
---|
181 | /** Check if @a addr belonges to this lease database. */
|
---|
182 | bool addressBelongs(RTNETADDRIPV4 addr) const RT_NOEXCEPT { return m_pool.contains(addr); }
|
---|
183 |
|
---|
184 | Binding *allocateBinding(const DhcpClientMessage &req, Config::ConfigVec const &rConfigVec);
|
---|
185 | bool releaseBinding(const DhcpClientMessage &req) RT_NOEXCEPT;
|
---|
186 |
|
---|
187 | void cancelOffer(const DhcpClientMessage &req) RT_NOEXCEPT;
|
---|
188 |
|
---|
189 | void expire() RT_NOEXCEPT;
|
---|
190 |
|
---|
191 | /** @name Database serialization methods
|
---|
192 | * @{ */
|
---|
193 | int loadLeases(const RTCString &strFilename) RT_NOEXCEPT;
|
---|
194 | private:
|
---|
195 | int i_loadLease(const xml::ElementNode *pElmLease) RT_NOEXCEPT;
|
---|
196 | public:
|
---|
197 | int writeLeases(const RTCString &strFilename) const RT_NOEXCEPT;
|
---|
198 | /** @} */
|
---|
199 |
|
---|
200 | private:
|
---|
201 | int i_enterFixedAddressAssignment(RTNETADDRIPV4 const &a_rAddress, RTMAC const &a_rMACAddress) RT_NOEXCEPT;
|
---|
202 | Binding *i_createBinding(const ClientId &id = ClientId());
|
---|
203 | Binding *i_createBinding(RTNETADDRIPV4 addr, const ClientId &id = ClientId());
|
---|
204 |
|
---|
205 | Binding *i_allocateAddress(const ClientId &id, RTNETADDRIPV4 addr);
|
---|
206 |
|
---|
207 | /* add binding e.g. from the leases file */
|
---|
208 | int i_addBinding(Binding *pNewBinding) RT_NOEXCEPT;
|
---|
209 | };
|
---|
210 |
|
---|
211 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Db_h */
|
---|