summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/Dhcpd/Db.h
blob: 5208cf3b1b56993714bbb990f2f45e58ce4c8d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* $Id: Db.h $ */
/** @file
 * DHCP server - address database
 */

/*
 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#ifndef VBOX_INCLUDED_SRC_Dhcpd_Db_h
#define VBOX_INCLUDED_SRC_Dhcpd_Db_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include "DhcpdInternal.h"
#include <iprt/net.h>

#include <iprt/cpp/ministring.h>
#include <iprt/cpp/xml.h>

#include <list>

#include "Timestamp.h"
#include "ClientId.h"
#include "IPv4Pool.h"
#include "Config.h"
#include "DhcpMessage.h"


/**
 * An address binding in the lease database.
 *
 * This is how an allocated IPv4 address is mananged.
 */
class Binding
{
    friend class Db;

public:
    enum State { FREE, RELEASED, EXPIRED, OFFERED, ACKED };

private:
    const RTNETADDRIPV4 m_addr;
    State               m_state;
    ClientId            m_id;
    Timestamp           m_issued;
    uint32_t            m_secLease;
    /** Set if this is a fixed assignment. */
    bool                m_fFixed;

public:
    Binding();
    Binding(const Binding &);

    explicit Binding(RTNETADDRIPV4 a_Addr)
        : m_addr(a_Addr), m_state(FREE), m_issued(), m_secLease(0), m_fFixed(false)
    {}

    Binding(RTNETADDRIPV4 a_Addr, const ClientId &a_id)
        : m_addr(a_Addr), m_state(FREE), m_id(a_id), m_issued(), m_secLease(0), m_fFixed(false)
    {}

    Binding(RTNETADDRIPV4 a_Addr, const RTMAC &a_MACAddress, bool a_fFixed)
        : m_addr(a_Addr)
        , m_state(ACKED)
        , m_id(ClientId(a_MACAddress, OptClientId()))
        , m_issued(Timestamp::now())
        , m_secLease(UINT32_MAX - 1)
        , m_fFixed(a_fFixed)
    {}


    /** @name Attribute accessors
     * @{ */
    RTNETADDRIPV4   addr() const RT_NOEXCEPT        { return m_addr; }

    const ClientId &id() const RT_NOEXCEPT          { return m_id; }
    void            idUpdate(const ClientId &a_ridClient);

    uint32_t        leaseTime() const RT_NOEXCEPT   { return m_secLease; }
    Timestamp       issued() const RT_NOEXCEPT      { return m_issued; }

    State           state() const RT_NOEXCEPT       { return m_state; }
    const char     *stateName() const RT_NOEXCEPT;
    Binding        &setState(const char *pszStateName) RT_NOEXCEPT;
    Binding        &setState(State stateParam) RT_NOEXCEPT
    {
        m_state = stateParam;
        return *this;
    }

    bool            isFixed() const RT_NOEXCEPT     { return m_fFixed; }
    /** @} */


    Binding &setLeaseTime(uint32_t secLease) RT_NOEXCEPT
    {
        m_issued = Timestamp::now();
        m_secLease = secLease;
        return *this;
    }

    /** Reassigns the binding to the given client.   */
    Binding &giveTo(const ClientId &a_id) RT_NOEXCEPT
    {
        m_id = a_id;
        m_state = FREE;
        return *this;
    }

    void free()
    {
        m_id = ClientId();
        m_state = FREE;
    }

    bool expire(Timestamp tsDeadline) RT_NOEXCEPT;
    bool expire() RT_NOEXCEPT
    {
        return expire(Timestamp::now());
    }

    /** @name Serialization
     * @{ */
    static Binding *fromXML(const xml::ElementNode *pElmLease);
    void            toXML(xml::ElementNode *pElmParent) const;
    /** @} */

    /** @name String formatting of %R[binding].
     * @{ */
    static void registerFormat() RT_NOEXCEPT;
private:
    static DECLCALLBACK(size_t) rtStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char *pszType,
                                            void const *pvValue, int cchWidth, int cchPrecision, unsigned fFlags, void *pvUser);
    static bool g_fFormatRegistered;
    /** @} */

    Binding &operator=(const Binding &); /**< Shuts up warning C4626 (incorrect warning?). */
};


/**
 * The lease database.
 *
 * There is currently just one instance of this class in a running DHCP server
 * residing in Dhcpd::m_db.  It covers one single range of IPv4 addresses, which
 * currently unbound addressed are managed by m_pool.  The allocated addresses
 * are kept in the m_bindings list.  Once an address has been allocated, it will
 * stay in the m_bindings list even after released or expired.
 */
class Db
{
private:
    typedef std::list<Binding *> bindings_t;

    /** Configuration (set at init).
     * @note Currently not used.  */
    const Config   *m_pConfig;
    /** The lease database.
     * @note Since fixed assignments are added during initialization, they will
     *       always be first.  The allocateBinding() code depends on this.  */
    bindings_t      m_bindings;
    /** Address allocation pool. */
    IPv4Pool        m_pool;

public:
    Db();
    ~Db();

    int      init(const Config *pConfig);

    /** Check if @a addr belonges to this lease database. */
    bool     addressBelongs(RTNETADDRIPV4 addr) const RT_NOEXCEPT { return m_pool.contains(addr); }

    Binding *allocateBinding(const DhcpClientMessage &req, Config::ConfigVec const &rConfigVec);
    bool     releaseBinding(const DhcpClientMessage &req) RT_NOEXCEPT;

    void     cancelOffer(const DhcpClientMessage &req) RT_NOEXCEPT;

    void     expire() RT_NOEXCEPT;

    /** @name Database serialization methods
     * @{ */
    int      loadLeases(const RTCString &strFilename) RT_NOEXCEPT;
private:
    int      i_loadLease(const xml::ElementNode *pElmLease) RT_NOEXCEPT;
public:
    int      writeLeases(const RTCString &strFilename) const RT_NOEXCEPT;
    /** @} */

private:
    int      i_enterFixedAddressAssignment(RTNETADDRIPV4 const &a_rAddress, RTMAC const &a_rMACAddress) RT_NOEXCEPT;
    Binding *i_createBinding(const ClientId &id = ClientId());
    Binding *i_createBinding(RTNETADDRIPV4 addr, const ClientId &id = ClientId());

    Binding *i_allocateAddress(const ClientId &id, RTNETADDRIPV4 addr);

    /* add binding e.g. from the leases file */
    int      i_addBinding(Binding *pNewBinding) RT_NOEXCEPT;
};

#endif /* !VBOX_INCLUDED_SRC_Dhcpd_Db_h */