Kea 3.1.1
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 auto& xid_index = queries_.template get<1>();
110 auto query = xid_index.find(xid);
111 if (query != xid_index.end()) {
112 xid_index.erase(query);
113 }
114 }
115
117 void clear() {
118 std::lock_guard<std::mutex> lck(mutex_);
119 queries_.clear();
120 }
121
122private:
130 QueryPtrType findInternal(const Xid& xid) const {
131 auto const& xid_index = queries_.template get<1>();
132 auto const query = xid_index.find(xid);
133 if (query != xid_index.end()) {
134 return (*query);
135 }
136 return (QueryPtrType());
137 }
138
140 XidQueueContainer queries_;
141
143 mutable std::mutex mutex_;
144};
145
148
150typedef boost::shared_ptr<LeaseQueryConnection> LeaseQueryConnectionPtr;
151
153typedef boost::weak_ptr<LeaseQueryConnection> LeaseQueryConnectionWPtr;
154
157public:
175 const tcp::TcpConnectionAcceptorPtr& acceptor,
176 const asiolink::TlsContextPtr& tls_context,
177 tcp::TcpConnectionPool& connection_pool,
178 const tcp::TcpConnectionAcceptorCallback& acceptor_callback,
179 const tcp::TcpConnectionFilterCallback& filter_callback,
180 const long idle_timeout,
181 const uint16_t family,
182 const size_t max_concurrent_queries,
183 const size_t read_max = 32768);
184
187 }
188
190 virtual void shutdown();
191
193 virtual void close();
194
198 virtual void stopThisConnection();
199
204 bool isStopping() const {
205 return (stopping_);
206 }
207
212 bool canSend() const {
213 return (can_send_);
214 }
215
220
226 virtual void sendNextResponse();
227
232 LeaseQueryConnectionPtr ptr = wptr.lock();
233 if (!ptr) {
234 return;
235 }
236 ptr->sendNextResponse();
237 }
238
245
251 virtual bool responseSent(tcp::TcpResponsePtr response);
252
262 virtual void requestReceived(tcp::TcpRequestPtr request);
263
270 BlqQueryPtr unpackQuery4(const uint8_t* buffer, size_t length) const;
271
278 BlqQueryPtr unpackQuery6(const uint8_t* buffer, size_t length) const;
279
285 void startQuery(BlqQueryPtr query_msg);
286
291 running_queries_.add(query);
292 }
293
295 size_t getNumRunningQueries() const {
296 return (running_queries_.size());
297 }
298
302 void removeRunningQuery(const Xid& xid) {
303 running_queries_.remove(xid);
304 }
305
310 // query list or waiting query queue, False otherwise.
311 bool findQuery(const Xid& xid) const {
312 return (running_queries_.find(xid) || pending_queries_.find(xid));
313 }
314
319 pending_queries_.add(query);
320 }
321
323 bool noPendingQuery() const {
324 return (pending_queries_.empty());
325 }
326
329
331 virtual void processNextQuery();
332
337 virtual bool pushToSend(BlqResponsePtr response);
338
345 BlqResponsePtr response) {
346 LeaseQueryConnectionPtr ptr = wptr.lock();
347 if (!ptr) {
348 return (false);
349 }
350 return (ptr->pushToSend(response));
351 }
352
356 virtual void post(const BlqPostCbArg& callback);
357
363 const BlqPostCbArg& callback) {
364 LeaseQueryConnectionPtr ptr = wptr.lock();
365 if (!ptr) {
366 return;
367 }
368 ptr->post(callback);
369 }
370
375 virtual void queryComplete(const Xid& xid);
376
381 static void doQueryComplete(LeaseQueryConnectionWPtr wptr, const Xid& xid) {
382 LeaseQueryConnectionPtr ptr = wptr.lock();
383 if (!ptr) {
384 return;
385 }
386 ptr->queryComplete(xid);
387 }
388
390 size_t getNumResponses() const;
391
394
397
398protected:
400 uint16_t family_;
401
404
407
410
413
420
423
425 mutable std::mutex responses_mutex_;
426
429
433};
434
435} // end of namespace isc::lease_query
436} // end of namespace isc
437
438#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.