Kea 3.1.7
lease_query_connection.h
Go to the documentation of this file.
1// Copyright (C) 2022-2025 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>
12#include <asiolink/io_service.h>
13#include <dhcp/pkt6.h>
14#include <tcp/tcp_listener.h>
15#include <tcp/tcp_stream_msg.h>
16#include <bulk_lease_query.h>
17
18#include <boost/multi_index/indexed_by.hpp>
19#include <boost/multi_index/member.hpp>
20#include <boost/multi_index_container.hpp>
21#include <boost/multi_index/mem_fun.hpp>
22#include <boost/multi_index/sequenced_index.hpp>
23#include <boost/multi_index/ordered_index.hpp>
24#include <boost/multi_index/tag.hpp>
25
26namespace isc {
27namespace lease_query {
28
36template <class QueryType>
37class XidQueue {
38public:
40 typedef boost::shared_ptr<QueryType> QueryPtrType;
41
43 typedef boost::multi_index_container<
44 // It stores pointers to BulkLeaseQuery.
46 boost::multi_index::indexed_by<
47 boost::multi_index::sequenced<>,
48 boost::multi_index::ordered_unique<
49 boost::multi_index::const_mem_fun<QueryType,
50 Xid,
51 &QueryType::getXid>
52 >
53 >
55
59 void add(QueryPtrType query) {
60 std::lock_guard<std::mutex> lck(mutex_);
61 queries_.push_back(query);
62 }
63
68 QueryPtrType query;
69 {
70 std::lock_guard<std::mutex> lck(mutex_);
71 if (!queries_.empty()) {
72 query = queries_.front();
73 queries_.pop_front();
74 }
75 }
76 return (query);
77 }
78
82 bool empty() const {
83 std::lock_guard<std::mutex> lck(mutex_);
84 return (queries_.empty());
85 }
86
90 size_t size() const {
91 std::lock_guard<std::mutex> lck(mutex_);
92 return (queries_.size());
93 }
94
100 QueryPtrType find(const Xid& xid) const {
101 std::lock_guard<std::mutex> lck(mutex_);
102 return (findInternal(xid));
103 }
104
108 void remove(const Xid& xid) {
109 std::lock_guard<std::mutex> lck(mutex_);
110 auto& xid_index = queries_.template get<1>();
111 auto query = xid_index.find(xid);
112 if (query != xid_index.end()) {
113 xid_index.erase(query);
114 }
115 }
116
118 void clear() {
119 std::lock_guard<std::mutex> lck(mutex_);
120 queries_.clear();
121 }
122
123private:
131 QueryPtrType findInternal(const Xid& xid) const {
132 auto const& xid_index = queries_.template get<1>();
133 auto const query = xid_index.find(xid);
134 if (query != xid_index.end()) {
135 return (*query);
136 }
137 return (QueryPtrType());
138 }
139
141 XidQueueContainer queries_;
142
144 mutable std::mutex mutex_;
145};
146
149
151typedef boost::shared_ptr<LeaseQueryConnection> LeaseQueryConnectionPtr;
152
154typedef boost::weak_ptr<LeaseQueryConnection> LeaseQueryConnectionWPtr;
155
158public:
176 const tcp::TcpConnectionAcceptorPtr& acceptor,
177 const asiolink::TlsContextPtr& tls_context,
178 tcp::TcpConnectionPool& connection_pool,
179 const tcp::TcpConnectionAcceptorCallback& acceptor_callback,
180 const tcp::TcpConnectionFilterCallback& filter_callback,
181 const long idle_timeout,
182 const uint16_t family,
183 const size_t max_concurrent_queries,
184 const size_t read_max = 32768);
185
188 }
189
191 virtual void shutdown();
192
194 virtual void close();
195
199 virtual void stopThisConnection();
200
205 bool isStopping() const {
206 return (stopping_);
207 }
208
213 bool canSend() const {
214 return (can_send_);
215 }
216
221
227 virtual void sendNextResponse();
228
233 LeaseQueryConnectionPtr ptr = wptr.lock();
234 if (!ptr) {
235 return;
236 }
237 ptr->sendNextResponse();
238 }
239
246
252 virtual bool responseSent(tcp::TcpResponsePtr response);
253
263 virtual void requestReceived(tcp::TcpRequestPtr request);
264
271 BlqQueryPtr unpackQuery4(const uint8_t* buffer, size_t length) const;
272
279 BlqQueryPtr unpackQuery6(const uint8_t* buffer, size_t length) const;
280
286 void startQuery(BlqQueryPtr query_msg);
287
292 running_queries_.add(query);
293 }
294
296 size_t getNumRunningQueries() const {
297 return (running_queries_.size());
298 }
299
303 void removeRunningQuery(const Xid& xid) {
304 running_queries_.remove(xid);
305 }
306
311 // query list or waiting query queue, False otherwise.
312 bool findQuery(const Xid& xid) const {
313 return (running_queries_.find(xid) || pending_queries_.find(xid));
314 }
315
320 pending_queries_.add(query);
321 }
322
324 bool noPendingQuery() const {
325 return (pending_queries_.empty());
326 }
327
330
332 virtual void processNextQuery();
333
338 virtual bool pushToSend(BlqResponsePtr response);
339
346 BlqResponsePtr response) {
347 LeaseQueryConnectionPtr ptr = wptr.lock();
348 if (!ptr) {
349 return (false);
350 }
351 return (ptr->pushToSend(response));
352 }
353
357 virtual void post(const BlqPostCbArg& callback);
358
364 const BlqPostCbArg& callback) {
365 LeaseQueryConnectionPtr ptr = wptr.lock();
366 if (!ptr) {
367 return;
368 }
369 ptr->post(callback);
370 }
371
376 virtual void queryComplete(const Xid& xid);
377
382 static void doQueryComplete(LeaseQueryConnectionWPtr wptr, const Xid& xid) {
383 LeaseQueryConnectionPtr ptr = wptr.lock();
384 if (!ptr) {
385 return;
386 }
387 ptr->queryComplete(xid);
388 }
389
391 size_t getNumResponses() const;
392
395
398
399protected:
401 uint16_t family_;
402
405
408
411
414
421
424
426 mutable std::mutex responses_mutex_;
427
430
434};
435
436} // end of namespace isc::lease_query
437} // end of namespace isc
438
439#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.