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 | // Copyright (C) 2020-2021 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef RESOURCE_HANDLER_H
#define RESOURCE_HANDLER_H
#include <asiolink/io_address.h>
#include <dhcpsrv/lease.h>
#include <boost/noncopyable.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index_container.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/composite_key.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/hashed_index.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/member.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/mem_fun.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/shared_ptr.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mutex><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
namespace isc {
namespace dhcp {
/// @brief Thrown by lock users when a resource lock cannot be obtained.
class ResourceBusy : public Exception {
public:
ResourceBusy(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) {}
};
/// @brief Resource race avoidance RAII handler.
class ResourceHandler : public boost::noncopyable {
public:
/// @brief Constructor.
ResourceHandler();
/// @brief Destructor.
///
/// Releases owned resources.
virtual ~ResourceHandler();<--- Virtual destructor in base class
/// @brief Tries to acquires a resource.
///
/// Lookup the resource, if not found insert the resource
/// in the resource container and return true, if found return false
///
/// @param type Type of the resource, member of @c Lease::Type enum.
/// @param addr The address or prefix aka the resource.
/// @return true if the resource was acquired, false if the resource is
/// busy i.e. owned by a handler.
bool tryLock(Lease::Type type, const asiolink::IOAddress& addr);
/// @brief Checks if a resource is owned by this handler.
///
/// @param type Type of the resource, member of @c Lease::Type enum.
/// @param addr The address or prefix aka the resource.
/// @return true if this handler owns the resource, false otherwise.
bool isLocked(Lease::Type type, const asiolink::IOAddress& addr);
/// @brief Releases a resource.
///
/// Remove the resource from the resource container.
///
/// @param type Type of the resource, member of @c Lease::Type enum.
/// @param addr The address or prefix aka the resource.
/// @throw when we do not own the resource.
void unLock(Lease::Type type, const asiolink::IOAddress& addr);
private:
/// Type definitions.
//@{
/// @brief Structure representing a resource.
struct Resource {
/// @brief Constructor.
///
/// @param addr The address or prefix aka the resource..
Resource(Lease::Type type, const asiolink::IOAddress& addr)
: type_(type), addr_(addr) {
}
/// @brief The type.
Lease::Type type_;
/// @brief The resource.
asiolink::IOAddress addr_;
/// @brief The key extractor.
std::vector<uint8_t> toBytes() const {
return (addr_.toBytes());
}
};
/// @brief The type of shared pointers to resources.
typedef boost::shared_ptr<Resource> ResourcePtr;
/// @brief The type of the resource container.
typedef boost::multi_index_container<
// This container stores pointers to resource objects.
ResourcePtr,
// Start specification of indexes here.
boost::multi_index::indexed_by<
// First index is used to search by type and address.
boost::multi_index::hashed_unique<
boost::multi_index::composite_key<
Resource,
// Lease type.
boost::multi_index::member<
Resource, Lease::Type, &Resource::type_
>,
// Address bytes.
boost::multi_index::const_mem_fun<
Resource, std::vector<uint8_t>, &Resource::toBytes
>
>
>
>
> ResourceContainer;
//@}
/// Class members.
//@{
/// @brief The resource container.
static ResourceContainer resources_;
/// @brief Mutex to protect the resource container.
static std::mutex mutex_;
/// @brief Lookup a resource.
///
/// The mutex must be held by the caller.
///
/// @param type Type of the resource, member of @c Lease::Type enum.
/// @param addr The address or prefix aka the resource.
/// @return The busy resource or null.
static ResourcePtr
lookup(Lease::Type type, const asiolink::IOAddress& addr);
//@}
/// Instance members.
//@{
/// @brief Acquire a resource.
///
/// The mutex must be held by the caller.
///
/// @param type Type of the resource, member of @c Lease::Type enum.
/// @param addr The address or prefix aka the resource.
void lock(Lease::Type type, const asiolink::IOAddress& addr);
/// @brief Release a resource.
///
/// The mutex must be held by the caller.
///
/// Remove the resource from the resource container.
///
/// @param type Type of the resource, member of @c Lease::Type enum.
/// @param addr The address or prefix aka the resource.
void unLockInternal(Lease::Type type, const asiolink::IOAddress& addr);
/// @brief List of resources this handler owns.
ResourceContainer owned_;
//@}
};
/// @brief Resource race avoidance RAII handler for DHCPv4.
class ResourceHandler4 : public ResourceHandler {
public:
/// @brief Destructor.
///
/// Releases owned resources.
virtual ~ResourceHandler4() { }<--- Destructor in derived class
/// @brief Tries to acquires a resource.
///
/// Lookup the resource, if not found insert the resource
/// in the resource container and return true, if found return false
///
/// @param addr The address aka the resource.
/// @return true if the resource was acquired, false if the resource is
/// busy i.e. owned by a handler.
bool tryLock4(const asiolink::IOAddress& addr) {
return (tryLock(Lease::TYPE_V4, addr));
}
/// @brief Checks if a resource is owned by this handler.
///
/// @param addr The address aka the resource.
/// @return true if this handler owns the resource, false otherwise.
bool isLocked4(const asiolink::IOAddress& addr) {
return (isLocked(Lease::TYPE_V4, addr));
}
/// @brief Releases a resource.
///
/// Remove the resource from the resource container.
///
/// @param addr The address aka the resource.
/// @throw when we do not own the resource.
void unLock4(const asiolink::IOAddress& addr) {
unLock(Lease::TYPE_V4, addr);
}
};
} // namespace isc
} // namespace dhcp
#endif // RESOURCE_HANDLER_H
|