Kea 3.3.1
dhcp4/client_handler.h
Go to the documentation of this file.
1// Copyright (C) 2020-2023 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 CLIENT_HANDLER_H
8#define CLIENT_HANDLER_H
9
10#include <dhcp/pkt4.h>
11#include <boost/noncopyable.hpp>
12#include <boost/multi_index_container.hpp>
13#include <boost/multi_index/composite_key.hpp>
14#include <boost/multi_index/hashed_index.hpp>
15#include <boost/multi_index/member.hpp>
16#include <boost/shared_ptr.hpp>
17#include <functional>
18#include <mutex>
19#include <thread>
20
21namespace isc {
22namespace dhcp {
23
25typedef std::function<void()> Continuation;
26
28typedef boost::shared_ptr<Continuation> ContinuationPtr;
29
34 return (boost::make_shared<Continuation>(cont));
35}
36
38class ClientHandler : public boost::noncopyable {
39public:
40
42
44 struct Client {
45
52 Client(Pkt4Ptr query, ClientIdPtr client_id, HWAddrPtr hwaddr);
53
55
57 std::vector<uint8_t> client_id_;
58
60 uint16_t htype_;
61
63 std::vector<uint8_t> hwaddr_;
64
66 uint8_t msg_type_;
67
69 uint32_t transid_;
70
72 std::thread::id thread_;
73
79
85 };
86
88 typedef boost::shared_ptr<Client> ClientPtr;
89
90private:
92 typedef boost::multi_index_container<
93
94 // This container stores pointers to client-by-id objects.
96
97 // Start specification of indexes here.
98 boost::multi_index::indexed_by<
99
100 // First index is used to search by client id.
101 boost::multi_index::hashed_unique<
102
103 // Client ID binary content as a member of the Client object.
104 boost::multi_index::member<
105 Client, std::vector<uint8_t>, &Client::client_id_
106 >
107 >
108 >
109 > ClientByIdContainer;
110
112 typedef boost::multi_index_container<
113
114 // This container stores pointers to client-by-hwaddr objects.
115 ClientPtr,
116
117 // Start specification of indexes here.
118 boost::multi_index::indexed_by<
119
120 // First index is used to search by HWAddr.
121 boost::multi_index::hashed_unique<
122
123 // This is a composite index for type and binary components.
124 boost::multi_index::composite_key<
125 Client,
126 boost::multi_index::member<
127 Client, uint16_t, &Client::htype_
128 >,
129 boost::multi_index::member<
130 Client, std::vector<uint8_t>, &Client::hwaddr_
131 >
132 >
133 >
134 >
135 > ClientByHWAddrContainer;
136
143 static ClientPtr lookup(const ClientIdPtr& client_id);
144
151 static ClientPtr lookup(const HWAddrPtr& hwaddr);
152
158 static void addById(const ClientPtr& client);
159
165 static void addByHWAddr(const ClientPtr& client);
166
172 static void del(const ClientIdPtr& client_id);
173
179 static void del(const HWAddrPtr& hwaddr);
180
184 static std::mutex mutex_;
185
187 static ClientByIdContainer clients_client_id_;
188
190 static ClientByHWAddrContainer clients_hwaddr_;
191
192public:
193
195
198
202 virtual ~ClientHandler();
203
215 bool tryLock(Pkt4Ptr query, ContinuationPtr cont = ContinuationPtr());
216
217private:
218
220
224 void lockById();
225
229 void lockByHWAddr();
230
234 void unLockById();
235
239 void unLockByHWAddr();
240
242 ClientPtr client_;
243
245 ClientIdPtr locked_client_id_;
246
248 HWAddrPtr locked_hwaddr_;
249};
250
251} // namespace isc
252} // namespace dhcp
253
254#endif // CLIENT_HANDLER_H
virtual ~ClientHandler()
Destructor.
boost::shared_ptr< Client > ClientPtr
The type of shared pointers to clients.
bool tryLock(Pkt4Ptr query, ContinuationPtr cont=ContinuationPtr())
Tries to acquires a client.
std::function< void()> Continuation
Define the type of packet processing continuation.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
ContinuationPtr makeContinuation(Continuation &&cont)
Continuation factory.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:154
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
boost::shared_ptr< Continuation > ContinuationPtr
Define the type of shared pointers to continuations.
Defines the logger used by the top-level component of kea-lfc.
Class (aka static) types, methods and members.
uint16_t htype_
Cached hardware type.
uint32_t transid_
Cached transaction ID.
std::vector< uint8_t > client_id_
Cached binary client ID.
Client(Pkt4Ptr query, ClientIdPtr client_id, HWAddrPtr hwaddr)
Constructor.
ContinuationPtr cont_
The continuation to process next query for the client.
std::vector< uint8_t > hwaddr_
Cached binary hardware address.
uint8_t msg_type_
Cached message type.
std::thread::id thread_
The ID of the thread processing the query.