1 | /* $Id: Db.h 79524 2019-07-04 10:14:02Z 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.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_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 | class Binding
|
---|
40 | {
|
---|
41 | friend class Db;
|
---|
42 |
|
---|
43 | public:
|
---|
44 | enum State { FREE, RELEASED, EXPIRED, OFFERED, ACKED };
|
---|
45 |
|
---|
46 | private:
|
---|
47 | const RTNETADDRIPV4 m_addr;
|
---|
48 | State m_state;
|
---|
49 | ClientId m_id;
|
---|
50 | Timestamp m_issued;
|
---|
51 | uint32_t m_secLease;
|
---|
52 |
|
---|
53 | public:
|
---|
54 | Binding();
|
---|
55 | Binding(const Binding &);
|
---|
56 |
|
---|
57 | explicit Binding(RTNETADDRIPV4 addrParam)
|
---|
58 | : m_addr(addrParam), m_state(FREE),
|
---|
59 | m_issued(), m_secLease() {}
|
---|
60 |
|
---|
61 | Binding(RTNETADDRIPV4 addrParam, const ClientId &idParam)
|
---|
62 | : m_addr(addrParam), m_state(FREE), m_id(idParam),
|
---|
63 | m_issued(), m_secLease() {}
|
---|
64 |
|
---|
65 |
|
---|
66 | RTNETADDRIPV4 addr() const { return m_addr; }
|
---|
67 |
|
---|
68 | State state() const { return m_state; }
|
---|
69 | const char *stateName() const;
|
---|
70 |
|
---|
71 | const ClientId &id() const { return m_id; }
|
---|
72 |
|
---|
73 | uint32_t leaseTime() const { return m_secLease; }
|
---|
74 | Timestamp issued() const { return m_issued; }
|
---|
75 |
|
---|
76 | Binding &setState(State stateParam)
|
---|
77 | {
|
---|
78 | m_state = stateParam;
|
---|
79 | return *this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | Binding &setState(const char *pszStateName);
|
---|
83 |
|
---|
84 | Binding &setLeaseTime(uint32_t secLease)
|
---|
85 | {
|
---|
86 | m_issued = Timestamp::now();
|
---|
87 | m_secLease = secLease;
|
---|
88 | return *this;
|
---|
89 | }
|
---|
90 |
|
---|
91 | Binding &giveTo(const ClientId &idParam)
|
---|
92 | {
|
---|
93 | m_id = idParam;
|
---|
94 | m_state = FREE;
|
---|
95 | return *this;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void free()
|
---|
99 | {
|
---|
100 | m_id = ClientId();
|
---|
101 | m_state = FREE;
|
---|
102 | }
|
---|
103 |
|
---|
104 | bool expire(Timestamp deadline);
|
---|
105 | bool expire() { return expire(Timestamp::now()); }
|
---|
106 |
|
---|
107 | static Binding *fromXML(const xml::ElementNode *ndLease);
|
---|
108 | int toXML(xml::ElementNode *ndParent) const;
|
---|
109 |
|
---|
110 | public:
|
---|
111 | static void registerFormat(); /* %R[binding] */
|
---|
112 |
|
---|
113 | private:
|
---|
114 | static bool g_fFormatRegistered;
|
---|
115 | static DECLCALLBACK(size_t) rtStrFormat(
|
---|
116 | PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
117 | const char *pszType, void const *pvValue,
|
---|
118 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
119 | void *pvUser);
|
---|
120 | };
|
---|
121 |
|
---|
122 |
|
---|
123 | class Db
|
---|
124 | {
|
---|
125 | private:
|
---|
126 | typedef std::list<Binding *> bindings_t;
|
---|
127 |
|
---|
128 | const Config *m_pConfig;
|
---|
129 | bindings_t m_bindings;
|
---|
130 | IPv4Pool m_pool;
|
---|
131 |
|
---|
132 | public:
|
---|
133 | Db();
|
---|
134 | ~Db();
|
---|
135 |
|
---|
136 | int init(const Config *pConfig);
|
---|
137 |
|
---|
138 | bool addressBelongs(RTNETADDRIPV4 addr) const { return m_pool.contains(addr); }
|
---|
139 |
|
---|
140 | Binding *allocateBinding(const DhcpClientMessage &req);
|
---|
141 | bool releaseBinding(const DhcpClientMessage &req);
|
---|
142 |
|
---|
143 | void cancelOffer(const DhcpClientMessage &req);
|
---|
144 |
|
---|
145 | void expire();
|
---|
146 |
|
---|
147 | public:
|
---|
148 | int loadLeases(const RTCString &strFileName);
|
---|
149 | void loadLease(const xml::ElementNode *ndLease);
|
---|
150 |
|
---|
151 | int writeLeases(const RTCString &strFileName) const;
|
---|
152 |
|
---|
153 | private:
|
---|
154 | Binding *createBinding(const ClientId &id = ClientId());
|
---|
155 | Binding *createBinding(RTNETADDRIPV4 addr, const ClientId &id = ClientId());
|
---|
156 |
|
---|
157 | Binding *allocateAddress(const ClientId &id, RTNETADDRIPV4 addr);
|
---|
158 |
|
---|
159 | /* add binding e.g. from the leases file */
|
---|
160 | int addBinding(Binding *b);
|
---|
161 | };
|
---|
162 |
|
---|
163 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_Db_h */
|
---|