14#include <boost/make_shared.hpp>
21namespace ph = std::placeholders;
28 send_in_progress_ =
true;
34SocketCallback::operator()(boost::system::error_code ec,
size_t length) {
35 if (ec.value() == boost::asio::error::operation_aborted) {
38 callback_(ec, length);
47 const long idle_timeout,
48 const size_t read_max )
49 : io_service_(io_service),
50 tls_context_(tls_context),
51 idle_timeout_(idle_timeout),
52 idle_timer_(io_service),
56 connection_pool_(connection_pool),
57 acceptor_callback_(acceptor_callback),
58 connection_filter_(connection_filter),
60 input_buf_(read_max) {
150 boost::dynamic_pointer_cast<TlsConnectionAcceptor>(
acceptor_);
162 }
catch (
const std::exception& ex) {
164 "connections: " << ex.
what());
187 }
catch (
const std::exception& ex) {
233 if (response->wireDataAvail()) {
247 response->getWireDataSize(),
256 response->getWireDataSize(),
353 boost::system::error_code ec,
size_t length) {
357 if (ec.value() == boost::asio::error::operation_aborted) {
362 }
else if ((ec.value() != boost::asio::error::try_again) &&
363 (ec.value() != boost::asio::error::would_block)) {
386 next_request =
postData(request, input_data);
395 size_t bytes_left = 0;
396 size_t length = input_data.size();
399 size_t bytes_used = request->postBuffer(
static_cast<void*
>(input_data.data()), length);
401 bytes_left = length - bytes_used;
402 input_data.erase(input_data.begin(), input_data.begin() + length);
405 if (request->needData()) {
421 }
catch (
const std::exception& ex) {
432 request =
postData(request, input_data);
440 boost::system::error_code ec,
size_t length) {
444 if (ec.value() == boost::asio::error::operation_aborted) {
449 }
else if ((ec.value() != boost::asio::error::try_again) &&
450 (ec.value() != boost::asio::error::would_block)) {
471 if (length > response->getWireDataSize()) {
472 length = response->getWireDataSize();
477 response->consumeWireData(length);
506 return (
"(unknown address)");
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
void cancel()
Cancel the timer.
void setup(const Callback &cbfunc, const long interval, const Mode &mode=REPEATING)
Register timer callback function and interval.
The TCPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a TCP ...
The TCPSocket class is a concrete derived class of IOAsioSocket that represents a TCP socket.
The TLSSocket class is a concrete derived class of IOAsioSocket that represents a TLS socket.
Generic error reported within TcpConnection class.
Pool of active TCP connections.
void stop(const TcpConnectionPtr &connection)
Removes a connection from the pool and stops it.
static std::atomic< uint64_t > rejected_counter_
Class/static rejected (by the accept filter) connection counter.
void shutdown(const TcpConnectionPtr &connection)
Removes a connection from the pool and shutdown it.
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.
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.
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
TcpConnectionFilterCallback connection_filter_
External callback for filtering connections by IP address.
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.
WireData wire_data_
Buffer used for data in wire format data.
virtual void consumeWireData(const size_t length)
Erases n bytes from the beginning of the wire data.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
const int DBGLVL_TRACE_BASIC
Trace basic operations.
const int DBGLVL_TRACE_DETAIL_DATA
Trace data associated with detailed operations.
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
boost::shared_ptr< TlsConnectionAcceptor > TlsConnectionAcceptorPtr
Type of shared pointer to TLS acceptors.
const isc::log::MessageID TCP_CONNECTION_STOP_FAILED
const isc::log::MessageID TCP_IDLE_CONNECTION_TIMEOUT_OCCURRED
boost::shared_ptr< TcpConnectionAcceptor > TcpConnectionAcceptorPtr
Type of shared pointer to TCP acceptors.
const isc::log::MessageID TLS_REQUEST_RECEIVE_START
const isc::log::MessageID TLS_CONNECTION_HANDSHAKE_FAILED
const isc::log::MessageID TCP_CONNECTION_STOP
const isc::log::MessageID TCP_DATA_SENT
boost::shared_ptr< TcpRequest > TcpRequestPtr
Defines a smart pointer to a TcpRequest.
std::function< bool(const boost::asio::ip::tcp::endpoint &) TcpConnectionFilterCallback)
Type of the callback for filtering new connections by ip address.
const isc::log::MessageID TCP_CONNECTION_REJECTED_BY_FILTER
const isc::log::MessageID TLS_CONNECTION_HANDSHAKE_START
std::function< void(const boost::system::error_code &) TcpConnectionAcceptorCallback)
Type of the callback for the TCP acceptor used in this library.
boost::shared_ptr< TcpResponse > TcpResponsePtr
const isc::log::MessageID TLS_SERVER_RESPONSE_SEND
const isc::log::MessageID TCP_DATA_RECEIVED
const isc::log::MessageID TCP_REQUEST_RECEIVE_START
std::vector< uint8_t > WireData
Defines a data structure for storing raw bytes of data on the wire.
isc::log::Logger tcp_logger("tcp")
Defines the logger used within libkea-tcp library.
const isc::log::MessageID TCP_SERVER_RESPONSE_SEND
const isc::log::MessageID TCP_REQUEST_RECEIVED_FAILED
const isc::log::MessageID TCP_CONNECTION_SHUTDOWN
const isc::log::MessageID TCP_CONNECTION_SHUTDOWN_FAILED
const isc::log::MessageID TCP_CLIENT_REQUEST_RECEIVED
Defines the logger used by the top-level component of kea-lfc.