1 | /* $Id: Db.h 79563 2019-07-06 01:22:56Z 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 |
|
---|
58 | public:
|
---|
59 | Binding();
|
---|
60 | Binding(const Binding &);
|
---|
61 |
|
---|
62 | explicit Binding(RTNETADDRIPV4 a_Addr)
|
---|
63 | : m_addr(a_Addr), m_state(FREE), m_issued(), m_secLease()
|
---|
64 | {}
|
---|
65 |
|
---|
66 | Binding(RTNETADDRIPV4 a_Addr, const ClientId &a_id)
|
---|
67 | : m_addr(a_Addr), m_state(FREE), m_id(a_id), m_issued(), m_secLease()
|
---|
68 | {}
|
---|
69 |
|
---|
70 |
|
---|
71 | /** @name Attribute accessors
|
---|
72 | * @{ */
|
---|
73 | RTNETADDRIPV4 addr() const RT_NOEXCEPT { return m_addr; }
|
---|
74 |
|
---|
75 | const ClientId &id() const RT_NOEXCEPT { return m_id; }
|
---|
76 |
|
---|
77 | uint32_t leaseTime() const RT_NOEXCEPT { return m_secLease; }
|
---|
78 | Timestamp issued() const RT_NOEXCEPT { return m_issued; }
|
---|
79 |
|
---|
80 | State state() const RT_NOEXCEPT { return m_state; }
|
---|
81 | const char *stateName() const RT_NOEXCEPT;
|
---|
82 | Binding &setState(const char *pszStateName) RT_NOEXCEPT;
|
---|
83 | Binding &setState(State stateParam) RT_NOEXCEPT
|
---|
84 | {
|
---|
85 | m_state = stateParam;
|
---|
86 | return *this;
|
---|
87 | }
|
---|
88 | /** @} */
|
---|
89 |
|
---|
90 |
|
---|
91 | Binding &setLeaseTime(uint32_t secLease) RT_NOEXCEPT
|
---|
92 | {
|
---|
93 | m_issued = Timestamp::now();
|
---|
94 | m_secLease = secLease;
|
---|
95 | return *this;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Reassigns the binding to the given client. */
|
---|
99 | Binding &giveTo(const ClientId &a_id) RT_NOEXCEPT
|
---|
100 | {
|
---|
101 | m_id = a_id;
|
---|
102 | m_state = FREE;
|
---|
103 | return *this;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void free()
|
---|
107 | {
|
---|
108 | m_id = ClientId();
|
---|
109 | m_state = FREE;
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool expire(Timestamp tsDeadline) RT_NOEXCEPT;
|
---|
113 | bool expire() RT_NOEXCEPT
|
---|
114 | {
|
---|
115 | return expire(Timestamp::now());
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** @name Serialization
|
---|
119 | * @{ */
|
---|
120 | static Binding *fromXML(const xml::ElementNode *pElmLease);
|
---|
121 | void toXML(xml::ElementNode *pElmParent) const;
|
---|
122 | /** @} */
|
---|
123 |
|
---|
124 | /** @name String formatting of %R[binding].
|
---|
125 | * @{ */
|
---|
126 | static void registerFormat() RT_NOEXCEPT;
|
---|
127 | private:
|
---|
128 | static DECLCALLBACK(size_t) rtStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char *pszType,
|
---|
129 | void const *pvValue, int cchWidth, int cchPrecision, unsigned fFlags, void *pvUser);
|
---|
130 | static bool g_fFormatRegistered;
|
---|
131 | /** @} */
|
---|
132 |
|
---|
133 | Binding &operator=(const Binding &); /**< Shuts up warning C4626 (incorrect warning?). */
|
---|
134 | };
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * The lease database.
|
---|
139 | *
|
---|
140 | * There is currently just one instance of this class in a running DHCP server
|
---|
141 | * residing in Dhcpd::m_db. It covers one single range of IPv4 addresses, which
|
---|
142 | * currently unbound addressed are managed by m_pool. The allocated addresses
|
---|
143 | * are kept in the m_bindings list. Once an address has been allocated, it will
|
---|
144 | * stay in the m_bindings list even after released or expired.
|
---|
145 | */
|
---|
146 | class Db
|
---|
147 | {
|
---|
148 | private:
|
---|
149 | typedef std::list<Binding *> bindings_t;
|
---|
150 |
|
---|
151 | /** Configuration (set at init).
|
---|
152 | * @note Currently not used. */
|
---|
153 | const Config *m_pConfig;
|
---|
154 | /** The lease database. */
|
---|
155 | bindings_t m_bindings;
|
---|
156 | /** Address allocation pool. */
|
---|
157 | IPv4Pool m_pool;
|
---|
158 |
|
---|
159 | public:
|
---|
160 | Db();
|
---|
161 | ~Db();
|
---|
162 |
|
---|
163 | int init(const Config *pConfig);
|
---|
164 |
|
---|
165 | /** Check if @a addr belonges to this lease database. */
|
---|
166 | bool addressBelongs(RTNETADDRIPV4 addr) const RT_NOEXCEPT { return m_pool.contains(addr); }
|
---|
167 |
|
---|
168 | Binding *allocateBinding(const DhcpClientMessage &req);
|
---|
169 | bool releaseBinding(const DhcpClientMessage &req) RT_NOEXCEPT;
|
---|
170 |
|
---|
171 | void cancelOffer(const DhcpClientMessage &req) RT_NOEXCEPT;
|
---|
172 |
|
---|
173 | void expire() RT_NOEXCEPT;
|
---|
174 |
|
---|
175 | /** @name Database serialization methods
|
---|
176 | * @{ */
|
---|
177 | int loadLeases(const RTCString &strFilename) RT_NOEXCEPT;
|
---|
178 | private:
|
---|
179 | int i_loadLease(const xml::ElementNode *pElmLease) RT_NOEXCEPT;
|
---|
180 | public:
|
---|
181 | int writeLeases(const RTCString &strFilename) const RT_NOEXCEPT;
|
---|
182 | /** @} */
|
---|
183 |
|
---|
184 | private:
|
---|
185 | Binding *i_createBinding(const ClientId &id = ClientId());
|
---|
186 | Binding *i_createBinding(RTNETADDRIPV4 addr, const ClientId &id = ClientId());
|
---|
187 |
|
---|
188 | Binding *i_allocateAddress(const ClientId &id, RTNETADDRIPV4 addr);
|
---|
189 |
|
---|
190 | /* add binding e.g. from the leases file */
|
---|
191 | int i_addBinding(Binding *pNewBinding) RT_NOEXCEPT;
|
---|
192 | };
|
---|
193 |
|
---|
194 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Db_h */
|
---|