VirtualBox

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

Last change on this file since 71353 was 70836, checked in by vboxsync, 7 years ago

NetworkServices/Dhcpd: export fix

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