VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/Dhcpd/Db.h@ 77807

Last change on this file since 77807 was 76576, checked in by vboxsync, 6 years ago

NetworkServices: scm header guard alignment.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette