Kea: tcp_connection.h Source File

| Kea 3.2.0-git

tcp_connection.h
Go to the documentation of this file.
1// Copyright (C) 2022-2024 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 TCP_CONNECTION_H
8#define TCP_CONNECTION_H
9
10#include <asiolink/asio_wrapper.h>
11#include <asiolink/interval_timer.h>
12#include <asiolink/io_service.h>
13#include <tcp/tcp_connection_acceptor.h>
14#include <tcp/wire_data.h>
15
16#include <boost/enable_shared_from_this.hpp>
17#include <boost/system/error_code.hpp>
18#include <boost/shared_ptr.hpp>
19
20#include <array>
21#include <functional>
22#include <string>
23#include <iostream>
24
25#include <mutex>
26
27namespace isc {
28namespace tcp {
29
31 class TcpMessage {
32public:
34 virtual ~TcpMessage() = default;
35
39 const uint8_t* getWireData() const {
40 if (wire_data_.empty()) {
41 isc_throw(InvalidOperation, "TcpMessage::getWireData() - cannot access empty wire data");
42 }
43
44 return (wire_data_.data());
45 }
46
48 size_t getWireDataSize() const {
49 return (wire_data_.size());
50 }
51
52protected:
54 WireData wire_data_;
55};
56
58 class TcpRequest : public TcpMessage {
59public:
61 virtual ~TcpRequest(){};
62
68 virtual size_t postBuffer(const void* buf, const size_t nbytes) = 0;
69
73 virtual bool needData() const = 0;
74
80 virtual std::string logFormatRequest(const size_t limit = 0) const = 0;
81
83 virtual void unpack() = 0;
84
85private:
86
92 std::string logFormatRequestSafe(const size_t limit = 0) const;
93};
94
96 typedef boost::shared_ptr<TcpRequest> TcpRequestPtr;
97
99 class TcpResponse : public TcpMessage {
100public:
102 TcpResponse()
103 : send_in_progress_(false) {};
104
106 virtual ~TcpResponse() {};
107
113 bool wireDataAvail() const {
114 return (!wire_data_.empty());
115 }
116
118 virtual void pack() = 0;
119
123 virtual void consumeWireData(const size_t length);
124
125 bool sendInProgress() {
126 return (send_in_progress_);
127 }
128
129private:
131 bool send_in_progress_;
132};
133
134 typedef boost::shared_ptr<TcpResponse> TcpResponsePtr;
135
137 class TcpConnectionError : public Exception {
138public:
139 TcpConnectionError(const char* file, size_t line, const char* what) :
140 isc::Exception(file, line, what) { }
141};
142
147class TcpConnectionPool;
148
150 typedef std::function<bool(const boost::asio::ip::tcp::endpoint&)> TcpConnectionFilterCallback;
151
153 class TcpConnection : public boost::enable_shared_from_this<TcpConnection> {
154private:
155
158 typedef std::function<void(boost::system::error_code ec, size_t length)>
159 SocketCallbackFunction;
160
164 class SocketCallback {
165 public:
166
171 SocketCallback(SocketCallbackFunction socket_callback)
172 : callback_(socket_callback) {
173 }
174
184 void operator()(boost::system::error_code ec, size_t length = 0);
185
186 private:
188 SocketCallbackFunction callback_;
189 };
190
191public:
192
207 TcpConnection(const asiolink::IOServicePtr& io_service,
208 const TcpConnectionAcceptorPtr& acceptor,
209 const asiolink::TlsContextPtr& tls_context,
210 TcpConnectionPool& connection_pool,
211 const TcpConnectionAcceptorCallback& acceptor_callback,
212 const TcpConnectionFilterCallback& connection_filter,
213 const long idle_timeout,
214 const size_t read_max = 32768);
215
219 virtual ~TcpConnection();
220
225 void asyncAccept();
226
228 virtual void shutdown();
229
231 virtual void close();
232
237 void doHandshake();
238
251 void doRead(TcpRequestPtr request = TcpRequestPtr());
252
266 TcpRequestPtr postData(TcpRequestPtr request, WireData& input_data);
267
272 virtual void requestReceived(TcpRequestPtr request) = 0;
273
281 virtual TcpRequestPtr createRequest() = 0;
282
286 size_t getReadMax() const {
287 return (read_max_);
288 }
289
294 void setReadMax(const size_t read_max);
295
300 virtual bool responseSent(TcpResponsePtr response) = 0;
301
305 static const boost::asio::ip::tcp::endpoint& NO_ENDPOINT() {
306 static boost::asio::ip::tcp::endpoint endpoint;
307 return (endpoint);
308 }
309
314 const boost::asio::ip::tcp::endpoint getRemoteEndpoint() const {
315 return (remote_endpoint_);
316 }
317
318protected:
319
328 void doWrite(TcpResponsePtr response);
329
335 void asyncSendResponse(TcpResponsePtr response);
336
345 void acceptorCallback(const boost::system::error_code& ec);
346
353 void handshakeCallback(const boost::system::error_code& ec);
354
365 void socketReadCallback(TcpRequestPtr request,
366 boost::system::error_code ec,
367 size_t length);
368
375 virtual void socketWriteCallback(TcpResponsePtr request,
376 boost::system::error_code ec,
377 size_t length);
378
386 void shutdownCallback(const boost::system::error_code& ec);
387
389 void setupIdleTimer();
390
392 void idleTimeoutCallback();
393
397 virtual void shutdownConnection();
398
400 virtual void stopThisConnection();
401
403 std::string getRemoteEndpointAddressAsText() const;
404
408 unsigned char* getInputBufData() {
409 if (input_buf_.empty()) {
410 isc_throw(InvalidOperation, "TcpConnection::getInputBufData() - cannot access empty buffer");
411 }
412
413 return (input_buf_.data());
414 }
415
417 size_t getInputBufSize() const {
418 return (input_buf_.size());
419 }
420
422 asiolink::IOServicePtr io_service_;
423
425 asiolink::TlsContextPtr tls_context_;
426
429 long idle_timeout_;
430
432 asiolink::IntervalTimer idle_timer_;
433
435 std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
436
438 std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
439
441 TcpConnectionAcceptorPtr acceptor_;
442
444 TcpConnectionPool& connection_pool_;
445
447 TcpConnectionAcceptorCallback acceptor_callback_;
448
450 TcpConnectionFilterCallback connection_filter_;
451
453 size_t read_max_;
454
456 WireData input_buf_;
457
459 boost::asio::ip::tcp::endpoint remote_endpoint_;
460};
461
463 typedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
464
465} // end of namespace isc::tcp
466} // end of namespace isc
467
468#endif
asio_wrapper.h
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition exceptions/exceptions.h:143
isc::asiolink::IntervalTimer
The IntervalTimer class is a wrapper for the ASIO boost::asio::system_timer class.
Definition interval_timer.h:61
isc::tcp::TcpConnectionError::TcpConnectionError
TcpConnectionError(const char *file, size_t line, const char *what)
Definition tcp_connection.h:139
isc::tcp::TcpConnectionPool
Pool of active TCP connections.
Definition tcp_connection_pool.h:33
isc::tcp::TcpConnection::doWrite
void doWrite(TcpResponsePtr response)
Starts asynchronous write to the socket.
Definition tcp_connection.cc:231
isc::tcp::TcpConnection::createRequest
virtual TcpRequestPtr createRequest()=0
Creates a new, empty request.
isc::tcp::TcpConnection::shutdownCallback
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
Definition tcp_connection.cc:74
isc::tcp::TcpConnection::getInputBufData
unsigned char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition tcp_connection.h:408
isc::tcp::TcpConnection::acceptorCallback
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
Definition tcp_connection.cc:282
isc::tcp::TcpConnection::asyncAccept
void asyncAccept()
Asynchronously accepts new connection.
Definition tcp_connection.cc:141
isc::tcp::TcpConnection::getInputBufSize
size_t getInputBufSize() const
Returns input buffer size.
Definition tcp_connection.h:417
isc::tcp::TcpConnection::shutdown
virtual void shutdown()
Shutdown the socket.
Definition tcp_connection.cc:79
isc::tcp::TcpConnection::getReadMax
size_t getReadMax() const
Fetches the maximum number of bytes read during single socket read.
Definition tcp_connection.h:286
isc::tcp::TcpConnection::remote_endpoint_
boost::asio::ip::tcp::endpoint remote_endpoint_
Remote endpoint.
Definition tcp_connection.h:459
isc::tcp::TcpConnection::requestReceived
virtual void requestReceived(TcpRequestPtr request)=0
Processes a request once it has been completely received.
isc::tcp::TcpConnection::shutdownConnection
virtual void shutdownConnection()
Shuts down current connection.
Definition tcp_connection.cc:117
isc::tcp::TcpConnection::stopThisConnection
virtual void stopThisConnection()
Stops current connection.
Definition tcp_connection.cc:129
isc::tcp::TcpConnection::setReadMax
void setReadMax(const size_t read_max)
Sets the maximum number of bytes read during single socket read.
Definition tcp_connection.cc:510
isc::tcp::TcpConnection::acceptor_
TcpConnectionAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
Definition tcp_connection.h:441
isc::tcp::TcpConnection::close
virtual void close()
Closes the socket.
Definition tcp_connection.cc:100
isc::tcp::TcpConnection::tls_context_
asiolink::TlsContextPtr tls_context_
TLS context.
Definition tcp_connection.h:425
isc::tcp::TcpConnection::input_buf_
WireData input_buf_
Buffer for a single socket read.
Definition tcp_connection.h:456
isc::tcp::TcpConnection::socketReadCallback
void socketReadCallback(TcpRequestPtr request, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
Definition tcp_connection.cc:352
isc::tcp::TcpConnection::setupIdleTimer
void setupIdleTimer()
Reset timer for detecting idle timeout in connections.
Definition tcp_connection.cc:484
isc::tcp::TcpConnection::socketWriteCallback
virtual void socketWriteCallback(TcpResponsePtr request, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
Definition tcp_connection.cc:439
isc::tcp::TcpConnection::asyncSendResponse
void asyncSendResponse(TcpResponsePtr response)
Sends TCP response asynchronously.
Definition tcp_connection.cc:276
isc::tcp::TcpConnection::io_service_
asiolink::IOServicePtr io_service_
The IO service used to handle events.
Definition tcp_connection.h:422
isc::tcp::TcpConnection::getRemoteEndpointAddressAsText
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
Definition tcp_connection.cc:501
isc::tcp::TcpConnection::connection_filter_
TcpConnectionFilterCallback connection_filter_
External callback for filtering connections by IP address.
Definition tcp_connection.h:450
isc::tcp::TcpConnection::getRemoteEndpoint
const boost::asio::ip::tcp::endpoint getRemoteEndpoint() const
Fetches the remote endpoint for the connection's socket.
Definition tcp_connection.h:314
isc::tcp::TcpConnection::tcp_socket_
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition tcp_connection.h:435
isc::tcp::TcpConnection::doRead
void doRead(TcpRequestPtr request=TcpRequestPtr())
Starts asynchronous read from the socket.
Definition tcp_connection.cc:194
isc::tcp::TcpConnection::NO_ENDPOINT
static const boost::asio::ip::tcp::endpoint & NO_ENDPOINT()
Returns an empty end point.
Definition tcp_connection.h:305
isc::tcp::TcpConnection::~TcpConnection
virtual ~TcpConnection()
Destructor.
Definition tcp_connection.cc:69
isc::tcp::TcpConnection::idle_timer_
asiolink::IntervalTimer idle_timer_
Timer used to detect idle Timeout.
Definition tcp_connection.h:432
isc::tcp::TcpConnection::responseSent
virtual bool responseSent(TcpResponsePtr response)=0
Determines behavior after a response has been sent.
isc::tcp::TcpConnection::acceptor_callback_
TcpConnectionAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition tcp_connection.h:447
isc::tcp::TcpConnection::read_max_
size_t read_max_
Maximum bytes to read in a single socket read.
Definition tcp_connection.h:453
isc::tcp::TcpConnection::connection_pool_
TcpConnectionPool & connection_pool_
Connection pool holding this connection.
Definition tcp_connection.h:444
isc::tcp::TcpConnection::doHandshake
void doHandshake()
Asynchronously performs TLS handshake.
Definition tcp_connection.cc:169
isc::tcp::TcpConnection::postData
TcpRequestPtr postData(TcpRequestPtr request, WireData &input_data)
Appends newly received raw data to the given request.
Definition tcp_connection.cc:394
isc::tcp::TcpConnection::tls_socket_
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition tcp_connection.h:438
isc::tcp::TcpConnection::handshakeCallback
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
Definition tcp_connection.cc:336
isc::tcp::TcpConnection::idleTimeoutCallback
void idleTimeoutCallback()
Callback invoked when the client has been idle.
Definition tcp_connection.cc:490
isc::tcp::TcpConnection::TcpConnection
TcpConnection(const asiolink::IOServicePtr &io_service, const TcpConnectionAcceptorPtr &acceptor, const asiolink::TlsContextPtr &tls_context, TcpConnectionPool &connection_pool, const TcpConnectionAcceptorCallback &acceptor_callback, const TcpConnectionFilterCallback &connection_filter, const long idle_timeout, const size_t read_max=32768)
Constructor.
Definition tcp_connection.cc:41
isc::tcp::TcpConnection::idle_timeout_
long idle_timeout_
Timeout after which the a TCP connection is shut down by the server.
Definition tcp_connection.h:429
isc::tcp::TcpMessage
Base class for TCP messages.
Definition tcp_connection.h:31
isc::tcp::TcpMessage::wire_data_
WireData wire_data_
Buffer used for data in wire format data.
Definition tcp_connection.h:54
isc::tcp::TcpMessage::~TcpMessage
virtual ~TcpMessage()=default
Destructor.
isc::tcp::TcpMessage::getWireDataSize
size_t getWireDataSize() const
Returns current size of the wire data.
Definition tcp_connection.h:48
isc::tcp::TcpMessage::getWireData
const uint8_t * getWireData() const
Returns pointer to the first byte of the wire data.
Definition tcp_connection.h:39
isc::tcp::TcpRequest
Abstract class used to receive an inbound message.
Definition tcp_connection.h:58
isc::tcp::TcpRequest::needData
virtual bool needData() const =0
Returns true if the request is incomplete.
isc::tcp::TcpRequest::logFormatRequest
virtual std::string logFormatRequest(const size_t limit=0) const =0
Returns request contents formatted for log output.
isc::tcp::TcpRequest::unpack
virtual void unpack()=0
Unpacks wire data once the message has been completely received.
isc::tcp::TcpRequest::postBuffer
virtual size_t postBuffer(const void *buf, const size_t nbytes)=0
Adds data to an incomplete request.
isc::tcp::TcpRequest::~TcpRequest
virtual ~TcpRequest()
Destructor.
Definition tcp_connection.h:61
isc::tcp::TcpResponse::~TcpResponse
virtual ~TcpResponse()
Destructor.
Definition tcp_connection.h:106
isc::tcp::TcpResponse::consumeWireData
virtual void consumeWireData(const size_t length)
Erases n bytes from the beginning of the wire data.
Definition tcp_connection.cc:27
isc::tcp::TcpResponse::sendInProgress
bool sendInProgress()
Definition tcp_connection.h:125
isc::tcp::TcpResponse::pack
virtual void pack()=0
Prepares the wire data content for writing.
isc::tcp::TcpResponse::wireDataAvail
bool wireDataAvail() const
Checks if the output buffer contains some data to be sent.
Definition tcp_connection.h:113
isc::tcp::TcpResponse::TcpResponse
TcpResponse()
Constructor.
Definition tcp_connection.h:102
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
interval_timer.h
io_service.h
isc::asiolink::TlsContextPtr
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
Definition common_tls.h:37
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::tcp
Definition mt_tcp_listener_mgr.cc:23
isc::tcp::TcpConnectionFilterCallback
std::function< bool(const boost::asio::ip::tcp::endpoint &)> TcpConnectionFilterCallback
Type of the callback for filtering new connections by ip address.
Definition tcp_connection.h:150
isc::tcp::TcpConnectionAcceptorPtr
boost::shared_ptr< TcpConnectionAcceptor > TcpConnectionAcceptorPtr
Type of shared pointer to TCP acceptors.
Definition tcp_connection_acceptor.h:27
isc::tcp::TcpRequestPtr
boost::shared_ptr< TcpRequest > TcpRequestPtr
Defines a smart pointer to a TcpRequest.
Definition tcp_connection.h:96
isc::tcp::TcpConnectionPtr
boost::shared_ptr< TcpConnection > TcpConnectionPtr
Pointer to the TcpConnection.
Definition tcp_connection.h:463
isc::tcp::TcpResponsePtr
boost::shared_ptr< TcpResponse > TcpResponsePtr
Definition tcp_connection.h:134
isc::tcp::WireData
std::vector< uint8_t > WireData
Defines a data structure for storing raw bytes of data on the wire.
Definition wire_data.h:17
isc::tcp::TcpConnectionAcceptorCallback
std::function< void(const boost::system::error_code &)> TcpConnectionAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
Definition tcp_connection_acceptor.h:21
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
tcp_connection_acceptor.h
wire_data.h