Kea 3.3.3
lease_query_connection.h
Go to the documentation of this file.
1// Copyright (C) 2022-2026 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 LEASE_QUERY_CONNECTION_H
8#define LEASE_QUERY_CONNECTION_H
9
10#include <config.h>
11
13#include <asiolink/io_service.h>
14#include <dhcp/pkt6.h>
16#include <tcp/tcp_listener.h>
17#include <tcp/tcp_stream_msg.h>
18
19#include <boost/multi_index/indexed_by.hpp>
20#include <boost/multi_index/mem_fun.hpp>
21#include <boost/multi_index/member.hpp>
22#include <boost/multi_index/ordered_index.hpp>
23#include <boost/multi_index/sequenced_index.hpp>
24#include <boost/multi_index/tag.hpp>
25#include <boost/multi_index_container.hpp>
26
27namespace isc {
28namespace lease_query {
29
37template <class QueryType>
38class XidQueue {
39public:
41 typedef boost::shared_ptr<QueryType> QueryPtrType;
42
44 typedef boost::multi_index_container<
45 // It stores pointers to BulkLeaseQuery.
47 boost::multi_index::indexed_by<
48 boost::multi_index::sequenced<>,
49 boost::multi_index::ordered_unique<
50 boost::multi_index::const_mem_fun<QueryType,
51 Xid,
52 &QueryType::getXid>
53 >
54 >
56
60 void add(QueryPtrType query) {
61 std::lock_guard<std::mutex> lck(mutex_);
62 queries_.push_back(query);
63 }
64
69 QueryPtrType query;
70 {
71 std::lock_guard<std::mutex> lck(mutex_);
72 if (!queries_.empty()) {
73 query = queries_.front();
74 queries_.pop_front();
75 }
76 }
77 return (query);
78 }
79
83 bool empty() const {
84 std::lock_guard<std::mutex> lck(mutex_);
85 return (queries_.empty());
86 }
87
91 size_t size() const {
92 std::lock_guard<std::mutex> lck(mutex_);
93 return (queries_.size());
94 }
95
101 QueryPtrType find(const Xid& xid) const {
102 std::lock_guard<std::mutex> lck(mutex_);
103 return (findInternal(xid));
104 }
105
109 void remove(const Xid& xid) {
110 std::lock_guard<std::mutex> lck(mutex_);
111 auto& xid_index = queries_.template get<1>();
112 auto query = xid_index.find(xid);
113 if (query != xid_index.end()) {
114 xid_index.erase(query);
115 }
116 }
117
119 void clear() {
120 std::lock_guard<std::mutex> lck(mutex_);
121 queries_.clear();
122 }
123
124private:
132 QueryPtrType findInternal(const Xid& xid) const {
133 auto const& xid_index = queries_.template get<1>();
134 auto const query = xid_index.find(xid);
135 if (query != xid_index.end()) {
136 return (*query);
137 }
138 return (QueryPtrType());
139 }
140
142 XidQueueContainer queries_;
143
145 mutable std::mutex mutex_;
146};
147
150
152typedef boost::shared_ptr<LeaseQueryConnection> LeaseQueryConnectionPtr;
153
155typedef boost::weak_ptr<LeaseQueryConnection> LeaseQueryConnectionWPtr;
156
159public:
177 const tcp::TcpConnectionAcceptorPtr& acceptor,
178 const asiolink::TlsContextPtr& tls_context,
179 tcp::TcpConnectionPool& connection_pool,
180 const tcp::TcpConnectionAcceptorCallback& acceptor_callback,
181 const tcp::TcpConnectionFilterCallback& filter_callback,
182 const long idle_timeout,
183 const uint16_t family,
184 const size_t max_concurrent_queries,
185 const size_t read_max = 32768);
186
189 }
190
192 virtual void shutdown();
193
195 virtual void close();
196
200 virtual void stopThisConnection();
201
206 bool isStopping() const {
207 return (stopping_);
208 }
209
214 bool canSend() const {
215 return (can_send_);
216 }
217
222
228 virtual void sendNextResponse();
229
234 LeaseQueryConnectionPtr ptr = wptr.lock();
235 if (!ptr) {
236 return;
237 }
238 ptr->sendNextResponse();
239 }
240
247
253 virtual bool responseSent(tcp::TcpResponsePtr response);
254
264 virtual void requestReceived(tcp::TcpRequestPtr request);
265
272 BlqQueryPtr unpackQuery4(const uint8_t* buffer, size_t length) const;
273
280 BlqQueryPtr unpackQuery6(const uint8_t* buffer, size_t length) const;
281
287 void startQuery(BlqQueryPtr query_msg);
288
293 running_queries_.add(query);
294 }
295
297 size_t getNumRunningQueries() const {
298 return (running_queries_.size());
299 }
300
304 void removeRunningQuery(const Xid& xid) {
305 running_queries_.remove(xid);
306 }
307
312 // query list or waiting query queue, False otherwise.
313 bool findQuery(const Xid& xid) const {
314 return (running_queries_.find(xid) || pending_queries_.find(xid));
315 }
316
321 pending_queries_.add(query);
322 }
323
325 bool noPendingQuery() const {
326 return (pending_queries_.empty());
327 }
328
331
333 virtual void processNextQuery();
334
339 virtual bool pushToSend(BlqResponsePtr response);
340
347 BlqResponsePtr response) {
348 LeaseQueryConnectionPtr ptr = wptr.lock();
349 if (!ptr) {
350 return (false);
351 }
352 return (ptr->pushToSend(response));
353 }
354
358 virtual void post(const BlqPostCbArg& callback);
359
365 const BlqPostCbArg& callback) {
366 LeaseQueryConnectionPtr ptr = wptr.lock();
367 if (!ptr) {
368 return;
369 }
370 ptr->post(callback);
371 }
372
377 virtual void queryComplete(const Xid& xid);
378
383 static void doQueryComplete(LeaseQueryConnectionWPtr wptr, const Xid& xid) {
384 LeaseQueryConnectionPtr ptr = wptr.lock();
385 if (!ptr) {
386 return;
387 }
388 ptr->queryComplete(xid);
389 }
390
392 size_t getNumResponses() const;
393
396
399
400protected:
402 uint16_t family_;
403
406
409
412
415
422
425
427 mutable std::mutex responses_mutex_;
428
431
435};
436
437} // end of namespace isc::lease_query
438} // end of namespace isc
439
440#endif // LEASE_QUERY_CONNECTION_H
Derivation of TcpConnection used for Bulk LeaseQuery.
LeaseQueryConnection(const asiolink::IOServicePtr &io_service, const tcp::TcpConnectionAcceptorPtr &acceptor, const asiolink::TlsContextPtr &tls_context, tcp::TcpConnectionPool &connection_pool, const tcp::TcpConnectionAcceptorCallback &acceptor_callback, const tcp::TcpConnectionFilterCallback &filter_callback, const long idle_timeout, const uint16_t family, const size_t max_concurrent_queries, const size_t read_max=32768)
Constructor.
size_t max_concurrent_queries_
Maximum number of concurrent queries allowed.
size_t getNumResponses() const
Returns the number of responses in the response queue.
virtual bool responseSent(tcp::TcpResponsePtr response)
Processes a response once it has been sent.
bool findQuery(const Xid &xid) const
Find queries based on Xid in the query list and queue.
virtual tcp::TcpRequestPtr createRequest()
Creates a new empty request ready to receive data.
bool canSend() const
Can send (aka stopped) flag.
size_t getNumRunningQueries() const
Returns the number of queries in the in-progress list.
virtual void processNextQuery()
Process next waiting query.
bool noPendingQuery() const
Returns True if the queue of waiting queries is empty.
virtual void requestReceived(tcp::TcpRequestPtr request)
Processes a completely received request.
virtual void sendNextResponse()
Sends the next response in the response queue.
BlqQueryPtr unpackQuery6(const uint8_t *buffer, size_t length) const
Unpacks a DHCPv6 packet from a data buffer.
XidQueue< BlqMsg > pending_queries_
Queue of queries waiting to enter processing.
virtual void close()
Closes the socket.
BlqResponseList responses_
List of responses waiting to be sent.
static bool doPushToSend(LeaseQueryConnectionWPtr wptr, BlqResponsePtr response)
Class/static version of pushToSend.
uint16_t family_
Protocol family AF_INET or AF_INET6.
BlqQueryPtr popPendingQuery()
Pops a query from the queue of waiting queries.
XidQueue< BulkLeaseQuery > running_queries_
List of in-process queries.
virtual void stopThisConnection()
Stops current connection.
virtual void shutdown()
Shutdown the socket.
tcp::TcpResponsePtr makeTcpResponse(BlqResponsePtr blq_response) const
Constructs a ready to send TcpResponse from and BlqResponse.
std::mutex responses_mutex_
Mutex used to lock during responses access.
BlqResponsePtr response_to_send_
Tracks the response currently being sent.
void removeRunningQuery(const Xid &xid)
Removes a query from the in-progress query list.
static void doSendNextResponse(LeaseQueryConnectionWPtr wptr)
Class/static version of sendNextResponse.
void addPendingQuery(BlqQueryPtr query)
Queues a query to the end of the queue of waiting queries.
void startQuery(BlqQueryPtr query_msg)
Start query processing.
asiolink::IOServicePtr io_service_
IOService that drives the connection events.
virtual void queryComplete(const Xid &xid)
Finishes up when a query has been completed (e.g.
virtual void post(const BlqPostCbArg &callback)
Posts an event callback to the connection's IOService.
static void doPost(LeaseQueryConnectionWPtr wptr, const BlqPostCbArg &callback)
Class/static version of post.
BlqQueryPtr unpackQuery4(const uint8_t *buffer, size_t length) const
Unpacks a DHCPv4 packet from a data buffer.
asiolink::IOAddress getRequesterAddress() const
Returns the requester's ip address.
static void doQueryComplete(LeaseQueryConnectionWPtr wptr, const Xid &xid)
Class/static version of queryComplete.
void addRunningQuery(BulkLeaseQueryPtr query)
Adds a query to the end of the list of in-progress queries.
virtual bool pushToSend(BlqResponsePtr response)
Adds a response to the connection's outbound queue of responses.
Wrapper around a chronological list of queries, uniquely keyed by transaction id.
void remove(const Xid &xid)
Removes a query from the queue for a given transaction id.
bool empty() const
Empty predicate.
void clear()
Removes all queries from the queue.
void add(QueryPtrType query)
Adds a query to the end of the queue.
boost::shared_ptr< QueryType > QueryPtrType
Type of pointers to QueryType.
QueryPtrType pop()
Pops a query to the beginning of the queue.
boost::multi_index_container< QueryPtrType, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::ordered_unique< boost::multi_index::const_mem_fun< QueryType, Xid, &QueryType::getXid > > > > XidQueueContainer
Multi-index container for storing bulk lease queries.
QueryPtrType find(const Xid &xid) const
Fetches the query for a given transaction id.
size_t size() const
Fetches the number of entries in the queue.
Pool of active TCP connections.
Accepts and handles a single TCP connection.
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
int get(CalloutHandle &handle)
The gss-tsig-get command.
boost::shared_ptr< LeaseQueryConnection > LeaseQueryConnectionPtr
Defines a shared pointer to a LeaseQueryConnection.
boost::shared_ptr< BlqResponse > BlqResponsePtr
Defines a shared pointer to an BlqResponse.
Definition blq_msg.h:143
std::function< void()> BlqPostCbArg
Type of BLQ post callback argument..
uint32_t Xid
Defines a Bulk LeaseQuery transaction id.
Definition blq_msg.h:18
boost::weak_ptr< LeaseQueryConnection > LeaseQueryConnectionWPtr
Defines a weak pointer to a LeaseQueryConnection.
std::list< BlqResponsePtr > BlqResponseList
Contains a list of BlqResponse pointers.
Definition blq_msg.h:146
boost::shared_ptr< BlqQuery > BlqQueryPtr
Defines a shared pointer to an BlqQuery.
Definition blq_msg.h:116
boost::shared_ptr< BulkLeaseQuery > BulkLeaseQueryPtr
Defines a shared pointer to a BulkLeaseQuery object.
std::function< bool(const boost::asio::ip::tcp::endpoint &)> TcpConnectionFilterCallback
Type of the callback for filtering new connections by ip address.
boost::shared_ptr< TcpConnectionAcceptor > TcpConnectionAcceptorPtr
Type of shared pointer to TCP acceptors.
boost::shared_ptr< TcpRequest > TcpRequestPtr
Defines a smart pointer to a TcpRequest.
boost::shared_ptr< TcpResponse > TcpResponsePtr
std::function< void(const boost::system::error_code &)> TcpConnectionAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
Defines the logger used by the top-level component of kea-lfc.