Kea 2.7.6
resource_handler.h
Go to the documentation of this file.
1// Copyright (C) 2020-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef RESOURCE_HANDLER_H
8#define RESOURCE_HANDLER_H
9
10#include <asiolink/io_address.h>
11#include <dhcpsrv/lease.h>
12
13#include <boost/noncopyable.hpp>
14#include <boost/multi_index_container.hpp>
15#include <boost/multi_index/composite_key.hpp>
16#include <boost/multi_index/hashed_index.hpp>
17#include <boost/multi_index/member.hpp>
18#include <boost/multi_index/mem_fun.hpp>
19#include <boost/shared_ptr.hpp>
20
21#include <mutex>
22
23namespace isc {
24namespace dhcp {
25
27class ResourceBusy : public Exception {
28public:
29 ResourceBusy(const char* file, size_t line, const char* what) :
30 isc::Exception(file, line, what) {}
31};
32
34class ResourceHandler : public boost::noncopyable {
35public:
36
39
43 virtual ~ResourceHandler();
44
54 bool tryLock(Lease::Type type, const asiolink::IOAddress& addr);
55
61 bool isLocked(Lease::Type type, const asiolink::IOAddress& addr);
62
70 void unLock(Lease::Type type, const asiolink::IOAddress& addr);
71
72private:
73
75
76
78 struct Resource {
79
83 Resource(Lease::Type type, const asiolink::IOAddress& addr)
84 : type_(type), addr_(addr) {
85 }
86
88 Lease::Type type_;
89
92
94 std::vector<uint8_t> toBytes() const {
95 return (addr_.toBytes());
96 }
97 };
98
100 typedef boost::shared_ptr<Resource> ResourcePtr;
101
103 typedef boost::multi_index_container<
104
105 // This container stores pointers to resource objects.
106 ResourcePtr,
107
108 // Start specification of indexes here.
109 boost::multi_index::indexed_by<
110
111 // First index is used to search by type and address.
112 boost::multi_index::hashed_unique<
113 boost::multi_index::composite_key<
114 Resource,
115 // Lease type.
116 boost::multi_index::member<
117 Resource, Lease::Type, &Resource::type_
118 >,
119 // Address bytes.
120 boost::multi_index::const_mem_fun<
121 Resource, std::vector<uint8_t>, &Resource::toBytes
122 >
123 >
124 >
125 >
126 > ResourceContainer;
127
129
131
132
134 static ResourceContainer resources_;
135
137 static std::mutex mutex_;
138
146 static ResourcePtr
147 lookup(Lease::Type type, const asiolink::IOAddress& addr);
148
150
152
153
160 void lock(Lease::Type type, const asiolink::IOAddress& addr);
161
170 void unLockInternal(Lease::Type type, const asiolink::IOAddress& addr);
171
173 ResourceContainer owned_;
174
176};
177
180public:
181
185 virtual ~ResourceHandler4() { }
186
195 bool tryLock4(const asiolink::IOAddress& addr) {
196 return (tryLock(Lease::TYPE_V4, addr));
197 }
198
203 bool isLocked4(const asiolink::IOAddress& addr) {
204 return (isLocked(Lease::TYPE_V4, addr));
205 }
206
213 void unLock4(const asiolink::IOAddress& addr) {
214 unLock(Lease::TYPE_V4, addr);
215 }
216};
217
218} // namespace isc
219} // namespace dhcp
220
221#endif // RESOURCE_HANDLER_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Thrown by lock users when a resource lock cannot be obtained.
ResourceBusy(const char *file, size_t line, const char *what)
Resource race avoidance RAII handler for DHCPv4.
void unLock4(const asiolink::IOAddress &addr)
Releases a resource.
bool isLocked4(const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.
virtual ~ResourceHandler4()
Destructor.
bool tryLock4(const asiolink::IOAddress &addr)
Tries to acquires a resource.
Resource race avoidance RAII handler.
virtual ~ResourceHandler()
Destructor.
bool tryLock(Lease::Type type, const asiolink::IOAddress &addr)
Tries to acquires a resource.
void unLock(Lease::Type type, const asiolink::IOAddress &addr)
Releases a resource.
bool isLocked(Lease::Type type, const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition lease.h:46
@ TYPE_V4
IPv4 lease.
Definition lease.h:50