Kea  2.1.7-git
dhcp4/client_handler.h
Go to the documentation of this file.
1 // Copyright (C) 2020 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 
21 namespace isc {
22 namespace dhcp {
23 
25 typedef std::function<void()> Continuation;
26 
28 typedef boost::shared_ptr<Continuation> ContinuationPtr;
29 
33 inline ContinuationPtr makeContinuation(Continuation&& cont) {
34  return (boost::make_shared<Continuation>(cont));
35 }
36 
38 class ClientHandler : public boost::noncopyable {
39 private:
40 
42 
44  struct Client {
45 
52  Client(Pkt4Ptr query, DuidPtr client_id, HWAddrPtr hwaddr);
53 
55  Pkt4Ptr query_;
56 
58  std::vector<uint8_t> duid_;
59 
61  uint16_t htype_;
62 
64  std::vector<uint8_t> hwaddr_;
65 
67  std::thread::id thread_;
68 
73  Pkt4Ptr next_query_;
74 
79  ContinuationPtr cont_;
80  };
81 
83  typedef boost::shared_ptr<Client> ClientPtr;
84 
86  typedef boost::multi_index_container<
87 
88  // This container stores pointers to client-by-id objects.
89  ClientPtr,
90 
91  // Start specification of indexes here.
92  boost::multi_index::indexed_by<
93 
94  // First index is used to search by Duid.
95  boost::multi_index::hashed_unique<
96 
97  // Client ID binary content as a member of the Client object.
98  boost::multi_index::member<
99  Client, std::vector<uint8_t>, &Client::duid_
100  >
101  >
102  >
103  > ClientByIdContainer;
104 
106  typedef boost::multi_index_container<
107 
108  // This container stores pointers to client-by-hwaddr objects.
109  ClientPtr,
110 
111  // Start specification of indexes here.
112  boost::multi_index::indexed_by<
113 
114  // First index is used to search by HWAddr.
115  boost::multi_index::hashed_unique<
116 
117  // This is a composite index for type and binary components.
118  boost::multi_index::composite_key<
119  Client,
120  boost::multi_index::member<
121  Client, uint16_t, &Client::htype_
122  >,
123  boost::multi_index::member<
124  Client, std::vector<uint8_t>, &Client::hwaddr_
125  >
126  >
127  >
128  >
129  > ClientByHWAddrContainer;
130 
137  static ClientPtr lookup(const DuidPtr& duid);
138 
145  static ClientPtr lookup(const HWAddrPtr& hwaddr);
146 
152  static void addById(const ClientPtr& client);
153 
159  static void addByHWAddr(const ClientPtr& client);
160 
166  static void del(const DuidPtr& duid);
167 
173  static void del(const HWAddrPtr& hwaddr);
174 
178  static std::mutex mutex_;
179 
181  static ClientByIdContainer clients_client_id_;
182 
184  static ClientByHWAddrContainer clients_hwaddr_;
185 
186 public:
187 
189 
191  ClientHandler();
192 
196  virtual ~ClientHandler();
197 
209  bool tryLock(Pkt4Ptr query, ContinuationPtr cont = ContinuationPtr());
210 
211 private:
212 
214 
218  void lockById();
219 
223  void lockByHWAddr();
224 
228  void unLockById();
229 
233  void unLockByHWAddr();
234 
236  ClientPtr client_;
237 
239  DuidPtr locked_client_id_;
240 
242  HWAddrPtr locked_hwaddr_;
243 };
244 
245 } // namespace isc
246 } // namespace dhcp
247 
248 #endif // CLIENT_HANDLER_H
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
ClientHandler()
Public interface.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
virtual ~ClientHandler()
Destructor.
Client race avoidance RAII handler.
Defines the logger used by the top-level component of kea-lfc.
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< Continuation > ContinuationPtr
Define the type of shared pointers to continuations.
ContinuationPtr makeContinuation(Continuation &&cont)
Continuation factory.