1 | /* $Id: Db.h 79530 2019-07-04 18:25:04Z 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 | * Address binding in the lease database.
|
---|
41 | */
|
---|
42 | class Binding
|
---|
43 | {
|
---|
44 | friend class Db;
|
---|
45 |
|
---|
46 | public:
|
---|
47 | enum State { FREE, RELEASED, EXPIRED, OFFERED, ACKED };
|
---|
48 |
|
---|
49 | private:
|
---|
50 | const RTNETADDRIPV4 m_addr;
|
---|
51 | State m_state;
|
---|
52 | ClientId m_id;
|
---|
53 | Timestamp m_issued;
|
---|
54 | uint32_t m_secLease;
|
---|
55 |
|
---|
56 | public:
|
---|
57 | Binding();
|
---|
58 | Binding(const Binding &);
|
---|
59 |
|
---|
60 | explicit Binding(RTNETADDRIPV4 a_Addr)
|
---|
61 | : m_addr(a_Addr), m_state(FREE), m_issued(), m_secLease()
|
---|
62 | {}
|
---|
63 |
|
---|
64 | Binding(RTNETADDRIPV4 a_Addr, const ClientId &a_id)
|
---|
65 | : m_addr(a_Addr), m_state(FREE), m_id(a_id), m_issued(), m_secLease()
|
---|
66 | {}
|
---|
67 |
|
---|
68 |
|
---|
69 | /** @name Attribute accessors
|
---|
70 | * @{ */
|
---|
71 | RTNETADDRIPV4 addr() const { return m_addr; }
|
---|
72 |
|
---|
73 | const ClientId &id() const { return m_id; }
|
---|
74 |
|
---|
75 | uint32_t leaseTime() const { return m_secLease; }
|
---|
76 | Timestamp issued() const { return m_issued; }
|
---|
77 |
|
---|
78 | State state() const { return m_state; }
|
---|
79 | const char *stateName() const;
|
---|
80 | Binding &setState(const char *pszStateName);
|
---|
81 | Binding &setState(State stateParam)
|
---|
82 | {
|
---|
83 | m_state = stateParam;
|
---|
84 | return *this;
|
---|
85 | }
|
---|
86 | /** @} */
|
---|
87 |
|
---|
88 |
|
---|
89 | Binding &setLeaseTime(uint32_t secLease)
|
---|
90 | {
|
---|
91 | m_issued = Timestamp::now();
|
---|
92 | m_secLease = secLease;
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Reassigns the binding to the given client. */
|
---|
97 | Binding &giveTo(const ClientId &a_id)
|
---|
98 | {
|
---|
99 | m_id = a_id;
|
---|
100 | m_state = FREE;
|
---|
101 | return *this;
|
---|
102 | }
|
---|
103 |
|
---|
104 | void free()
|
---|
105 | {
|
---|
106 | m_id = ClientId();
|
---|
107 | m_state = FREE;
|
---|
108 | }
|
---|
109 |
|
---|
110 | bool expire(Timestamp tsDeadline);
|
---|
111 | bool expire() { return expire(Timestamp::now()); }
|
---|
112 |
|
---|
113 | /** @name Serialization
|
---|
114 | * @{ */
|
---|
115 | static Binding *fromXML(const xml::ElementNode *pElmLease);
|
---|
116 | void toXML(xml::ElementNode *pElmParent) const;
|
---|
117 | /** @} */
|
---|
118 |
|
---|
119 | /** @name String formatting of %R[binding].
|
---|
120 | * @{ */
|
---|
121 | static void registerFormat();
|
---|
122 | private:
|
---|
123 | static DECLCALLBACK(size_t) rtStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char *pszType,
|
---|
124 | void const *pvValue, int cchWidth, int cchPrecision, unsigned fFlags, void *pvUser);
|
---|
125 | static bool g_fFormatRegistered;
|
---|
126 | /** @} */
|
---|
127 | };
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * The lease database.
|
---|
132 | */
|
---|
133 | class Db
|
---|
134 | {
|
---|
135 | private:
|
---|
136 | typedef std::list<Binding *> bindings_t;
|
---|
137 |
|
---|
138 | /** Configuration (set at init). */
|
---|
139 | const Config *m_pConfig;
|
---|
140 | /** The lease database. */
|
---|
141 | bindings_t m_bindings;
|
---|
142 | /** Address allocation pool. */
|
---|
143 | IPv4Pool m_pool;
|
---|
144 |
|
---|
145 | public:
|
---|
146 | Db();
|
---|
147 | ~Db();
|
---|
148 |
|
---|
149 | int init(const Config *pConfig);
|
---|
150 |
|
---|
151 | /** Check if @a addr belonges to this lease database. */
|
---|
152 | bool addressBelongs(RTNETADDRIPV4 addr) const { return m_pool.contains(addr); }
|
---|
153 |
|
---|
154 | Binding *allocateBinding(const DhcpClientMessage &req);
|
---|
155 | bool releaseBinding(const DhcpClientMessage &req);
|
---|
156 |
|
---|
157 | void cancelOffer(const DhcpClientMessage &req);
|
---|
158 |
|
---|
159 | void expire();
|
---|
160 |
|
---|
161 | /** @name Database serialization methods
|
---|
162 | * @{ */
|
---|
163 | int loadLeases(const RTCString &strFilename);
|
---|
164 | private:
|
---|
165 | int i_loadLease(const xml::ElementNode *pElmLease);
|
---|
166 | public:
|
---|
167 | int writeLeases(const RTCString &strFilename) const;
|
---|
168 | /** @} */
|
---|
169 |
|
---|
170 | private:
|
---|
171 | Binding *i_createBinding(const ClientId &id = ClientId());
|
---|
172 | Binding *i_createBinding(RTNETADDRIPV4 addr, const ClientId &id = ClientId());
|
---|
173 |
|
---|
174 | Binding *i_allocateAddress(const ClientId &id, RTNETADDRIPV4 addr);
|
---|
175 |
|
---|
176 | /* add binding e.g. from the leases file */
|
---|
177 | int i_addBinding(Binding *pNewBinding);
|
---|
178 | };
|
---|
179 |
|
---|
180 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Db_h */
|
---|