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
12#include <asiolink/io_service.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
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:
55};
56
58class 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
96typedef boost::shared_ptr<TcpRequest> TcpRequestPtr;
97
99class TcpResponse : public TcpMessage {
100public:
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
126 return (send_in_progress_);
127 }
128
129private:
131 bool send_in_progress_;
132};
133
134typedef boost::shared_ptr<TcpResponse> TcpResponsePtr;
135
138public:
139 TcpConnectionError(const char* file, size_t line, const char* what) :
140 isc::Exception(file, line, what) { }
141};
142
147class TcpConnectionPool;
148
150typedef std::function<bool(const boost::asio::ip::tcp::endpoint&)> TcpConnectionFilterCallback;
151
153class 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
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
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
423
426
430
433
435 std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
436
438 std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
439
442
445
448
451
453 size_t read_max_;
454
457
459 boost::asio::ip::tcp::endpoint remote_endpoint_;
460};
461
463typedef boost::shared_ptr<TcpConnection> TcpConnectionPtr;
464
465} // end of namespace isc::tcp
466} // end of namespace isc
467
468#endif
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a function is called in a prohibited way.
TcpConnectionError(const char *file, size_t line, const char *what)
Pool of active TCP connections.
void doWrite(TcpResponsePtr response)
Starts asynchronous write to the socket.
virtual TcpRequestPtr createRequest()=0
Creates a new, empty request.
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
unsigned char * getInputBufData()
Returns pointer to the first byte of the input buffer.
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
void asyncAccept()
Asynchronously accepts new connection.
size_t getInputBufSize() const
Returns input buffer size.
virtual void shutdown()
Shutdown the socket.
size_t getReadMax() const
Fetches the maximum number of bytes read during single socket read.
boost::asio::ip::tcp::endpoint remote_endpoint_
Remote endpoint.
virtual void requestReceived(TcpRequestPtr request)=0
Processes a request once it has been completely received.
virtual void shutdownConnection()
Shuts down current connection.
virtual void stopThisConnection()
Stops current connection.
void setReadMax(const size_t read_max)
Sets the maximum number of bytes read during single socket read.
TcpConnectionAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
virtual void close()
Closes the socket.
asiolink::TlsContextPtr tls_context_
TLS context.
WireData input_buf_
Buffer for a single socket read.
void socketReadCallback(TcpRequestPtr request, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
void setupIdleTimer()
Reset timer for detecting idle timeout in connections.
virtual void socketWriteCallback(TcpResponsePtr request, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
void asyncSendResponse(TcpResponsePtr response)
Sends TCP response asynchronously.
asiolink::IOServicePtr io_service_
The IO service used to handle events.
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
TcpConnectionFilterCallback connection_filter_
External callback for filtering connections by IP address.
const boost::asio::ip::tcp::endpoint getRemoteEndpoint() const
Fetches the remote endpoint for the connection's socket.
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
void doRead(TcpRequestPtr request=TcpRequestPtr())
Starts asynchronous read from the socket.
static const boost::asio::ip::tcp::endpoint & NO_ENDPOINT()
Returns an empty end point.
virtual ~TcpConnection()
Destructor.
asiolink::IntervalTimer idle_timer_
Timer used to detect idle Timeout.
virtual bool responseSent(TcpResponsePtr response)=0
Determines behavior after a response has been sent.
TcpConnectionAcceptorCallback acceptor_callback_
External TCP acceptor callback.
size_t read_max_
Maximum bytes to read in a single socket read.
TcpConnectionPool & connection_pool_
Connection pool holding this connection.
void doHandshake()
Asynchronously performs TLS handshake.
TcpRequestPtr postData(TcpRequestPtr request, WireData &input_data)
Appends newly received raw data to the given request.
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
void idleTimeoutCallback()
Callback invoked when the client has been idle.
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.
long idle_timeout_
Timeout after which the a TCP connection is shut down by the server.
Base class for TCP messages.
WireData wire_data_
Buffer used for data in wire format data.
virtual ~TcpMessage()=default
Destructor.
size_t getWireDataSize() const
Returns current size of the wire data.
const uint8_t * getWireData() const
Returns pointer to the first byte of the wire data.
Abstract class used to receive an inbound message.
virtual bool needData() const =0
Returns true if the request is incomplete.
virtual std::string logFormatRequest(const size_t limit=0) const =0
Returns request contents formatted for log output.
virtual void unpack()=0
Unpacks wire data once the message has been completely received.
virtual size_t postBuffer(const void *buf, const size_t nbytes)=0
Adds data to an incomplete request.
virtual ~TcpRequest()
Destructor.
virtual ~TcpResponse()
Destructor.
virtual void consumeWireData(const size_t length)
Erases n bytes from the beginning of the wire data.
virtual void pack()=0
Prepares the wire data content for writing.
bool wireDataAvail() const
Checks if the output buffer contains some data to be sent.
TcpResponse()
Constructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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< TcpConnection > TcpConnectionPtr
Pointer to the TcpConnection.
boost::shared_ptr< TcpResponse > TcpResponsePtr
std::vector< uint8_t > WireData
Defines a data structure for storing raw bytes of data on the wire.
Definition wire_data.h:17
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.