Kea 2.5.8
isc::tcp::TcpConnection Class Referenceabstract

Accepts and handles a single TCP connection. More...

#include <tcp_connection.h>

+ Inheritance diagram for isc::tcp::TcpConnection:

Public Member Functions

 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.
 
virtual ~TcpConnection ()
 Destructor.
 
void asyncAccept ()
 Asynchronously accepts new connection.
 
virtual void close ()
 Closes the socket.
 
virtual TcpRequestPtr createRequest ()=0
 Creates a new, empty request.
 
void doHandshake ()
 Asynchronously performs TLS handshake.
 
void doRead (TcpRequestPtr request=TcpRequestPtr())
 Starts asynchronous read from the socket.
 
size_t getReadMax () const
 Fetches the maximum number of bytes read during single socket read.
 
const boost::asio::ip::tcp::endpoint getRemoteEndpoint () const
 Fetches the remote endpoint for the connection's socket.
 
TcpRequestPtr postData (TcpRequestPtr request, WireData &input_data)
 Appends newly received raw data to the given request.
 
virtual void requestReceived (TcpRequestPtr request)=0
 Processes a request once it has been completely received.
 
virtual bool responseSent (TcpResponsePtr response)=0
 Determines behavior after a response has been sent.
 
void setReadMax (const size_t read_max)
 Sets the maximum number of bytes read during single socket read.
 
virtual void shutdown ()
 Shutdown the socket.
 

Static Public Member Functions

static const boost::asio::ip::tcp::endpoint & NO_ENDPOINT ()
 Returns an empty end point.
 

Protected Member Functions

void acceptorCallback (const boost::system::error_code &ec)
 Local callback invoked when new connection is accepted.
 
void asyncSendResponse (TcpResponsePtr response)
 Sends TCP response asynchronously.
 
void doWrite (TcpResponsePtr response)
 Starts asynchronous write to the socket.
 
unsigned char * getInputBufData ()
 Returns pointer to the first byte of the input buffer.
 
size_t getInputBufSize () const
 Returns input buffer size.
 
std::string getRemoteEndpointAddressAsText () const
 returns remote address in textual form
 
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.
 
void setupIdleTimer ()
 Reset timer for detecting idle timeout in connections.
 
void shutdownCallback (const boost::system::error_code &ec)
 Callback invoked when TLS shutdown is performed.
 
virtual void shutdownConnection ()
 Shuts down current connection.
 
void socketReadCallback (TcpRequestPtr request, boost::system::error_code ec, size_t length)
 Callback invoked when new data is received over the socket.
 
virtual void socketWriteCallback (TcpResponsePtr request, boost::system::error_code ec, size_t length)
 Callback invoked when data is sent over the socket.
 
virtual void stopThisConnection ()
 Stops current connection.
 

Protected Attributes

TcpConnectionAcceptorPtr acceptor_
 Pointer to the TCP acceptor used to accept new connections.
 
TcpConnectionAcceptorCallback acceptor_callback_
 External TCP acceptor callback.
 
TcpConnectionFilterCallback connection_filter_
 External callback for filtering connections by IP address.
 
TcpConnectionPoolconnection_pool_
 Connection pool holding this connection.
 
long idle_timeout_
 Timeout after which the a TCP connection is shut down by the server.
 
asiolink::IntervalTimer idle_timer_
 Timer used to detect idle Timeout.
 
WireData input_buf_
 Buffer for a single socket read.
 
asiolink::IOServicePtr io_service_
 The IO service used to handle events.
 
size_t read_max_
 Maximum bytes to read in a single socket read.
 
boost::asio::ip::tcp::endpoint remote_endpoint_
 Remote endpoint.
 
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
 TCP socket used by this connection.
 
asiolink::TlsContextPtr tls_context_
 TLS context.
 
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
 TLS socket used by this connection.
 

Detailed Description

Accepts and handles a single TCP connection.

Definition at line 157 of file tcp_connection.h.

Constructor & Destructor Documentation

◆ TcpConnection()

isc::tcp::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.

Parameters
io_serviceIO service to be used by the connection.
acceptorPointer to the TCP acceptor object used to listen for new TCP connections.
tls_contextTLS context.
connection_poolConnection pool in which this connection is stored.
acceptor_callbackCallback invoked when new connection is accepted.
connection_filterCallback invoked prior to handshake which can be used to qualify and reject connections
idle_timeoutTimeout after which a TCP connection is closed by the server.
read_maxmaximum size of a single socket read. Defaults to 32K.

Definition at line 50 of file tcp_connection.cc.

References tcp_socket_, and tls_socket_.

◆ ~TcpConnection()

isc::tcp::TcpConnection::~TcpConnection ( )
virtual

Destructor.

Closes current connection.

Definition at line 78 of file tcp_connection.cc.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ acceptorCallback()

void isc::tcp::TcpConnection::acceptorCallback ( const boost::system::error_code &  ec)
protected

Local callback invoked when new connection is accepted.

It invokes external (supplied via constructor) acceptor callback. If the acceptor is not opened it returns immediately. If the connection is accepted successfully the TcpConnection::doRead or TcpConnection::doHandshake is called.

Parameters
ecError code.

Definition at line 291 of file tcp_connection.cc.

References acceptor_, acceptor_callback_, connection_filter_, isc::log::DBGLVL_TRACE_DETAIL, doHandshake(), getRemoteEndpointAddressAsText(), idle_timeout_, LOG_DEBUG, NO_ENDPOINT(), isc::tcp::TcpConnectionPool::rejected_counter_, remote_endpoint_, stopThisConnection(), isc::tcp::TCP_CONNECTION_REJECTED_BY_FILTER, isc::tcp::tcp_logger, isc::tcp::TCP_REQUEST_RECEIVE_START, tcp_socket_, isc::tcp::TLS_CONNECTION_HANDSHAKE_START, tls_context_, and tls_socket_.

Referenced by asyncAccept().

+ Here is the call graph for this function:

◆ asyncAccept()

void isc::tcp::TcpConnection::asyncAccept ( )

Asynchronously accepts new connection.

When the connection is established successfully, the timeout timer is setup and the asynchronous handshake with client is performed.

Definition at line 150 of file tcp_connection.cc.

References acceptor_, acceptorCallback(), isc_throw, tcp_socket_, tls_socket_, and isc::Exception::what().

+ Here is the call graph for this function:

◆ asyncSendResponse()

void isc::tcp::TcpConnection::asyncSendResponse ( TcpResponsePtr  response)
protected

Sends TCP response asynchronously.

Internally it calls TcpConnection::doWrite to send the data.

Parameters
responsePointer to the TCP response to be sent.

Definition at line 285 of file tcp_connection.cc.

References doWrite().

+ Here is the call graph for this function:

◆ close()

void isc::tcp::TcpConnection::close ( )
virtual

Closes the socket.

Definition at line 109 of file tcp_connection.cc.

References isc::asiolink::IntervalTimer::cancel(), idle_timer_, isc_throw, tcp_socket_, and tls_socket_.

Referenced by ~TcpConnection().

+ Here is the call graph for this function:

◆ createRequest()

virtual TcpRequestPtr isc::tcp::TcpConnection::createRequest ( )
pure virtual

Creates a new, empty request.

This function is called by postData(), following the completion of the current request, to create a new request for accepting the next data read.

Returns
Pointer to the new request.

Referenced by doRead(), and postData().

◆ doHandshake()

void isc::tcp::TcpConnection::doHandshake ( )

Asynchronously performs TLS handshake.

When the handshake is performed successfully or skipped because TLS was not enabled, the asynchronous read from the socket is started.

Definition at line 178 of file tcp_connection.cc.

References doRead(), handshakeCallback(), isc_throw, setupIdleTimer(), tls_socket_, and isc::Exception::what().

Referenced by acceptorCallback().

+ Here is the call graph for this function:

◆ doRead()

void isc::tcp::TcpConnection::doRead ( TcpRequestPtr  request = TcpRequestPtr())

Starts asynchronous read from the socket.

The data received over the socket are supplied to the TCP parser until the parser signals that the entire request has been received or until the parser signals an error. In the former case the server creates an TCP response using supplied response creator object.

In case of error the connection is stopped.

Parameters
requestPointer to the request for which the read operation should be performed. It defaults to null pointer which indicates that this function should create new request.

Definition at line 203 of file tcp_connection.cc.

References createRequest(), getInputBufData(), getInputBufSize(), setupIdleTimer(), socketReadCallback(), stopThisConnection(), tcp_socket_, and tls_socket_.

Referenced by doHandshake(), handshakeCallback(), and socketReadCallback().

+ Here is the call graph for this function:

◆ doWrite()

void isc::tcp::TcpConnection::doWrite ( TcpResponsePtr  response)
protected

Starts asynchronous write to the socket.

The output_buf_ must contain the data to be sent.

In case of error the connection is stopped.

Parameters
responsePointer to the response to write operation should be performed.

Definition at line 240 of file tcp_connection.cc.

References isc::log::DBGLVL_TRACE_BASIC, getRemoteEndpointAddressAsText(), LOG_DEBUG, responseSent(), setupIdleTimer(), socketWriteCallback(), stopThisConnection(), isc::tcp::tcp_logger, isc::tcp::TCP_SERVER_RESPONSE_SEND, tcp_socket_, isc::tcp::TLS_SERVER_RESPONSE_SEND, and tls_socket_.

Referenced by asyncSendResponse(), and socketWriteCallback().

+ Here is the call graph for this function:

◆ getInputBufData()

unsigned char * isc::tcp::TcpConnection::getInputBufData ( )
inlineprotected

Returns pointer to the first byte of the input buffer.

Exceptions
InvalidOperationif called when the buffer is empty.

Definition at line 412 of file tcp_connection.h.

References input_buf_, and isc_throw.

Referenced by doRead().

◆ getInputBufSize()

size_t isc::tcp::TcpConnection::getInputBufSize ( ) const
inlineprotected

Returns input buffer size.

Definition at line 421 of file tcp_connection.h.

References input_buf_.

Referenced by doRead().

◆ getReadMax()

size_t isc::tcp::TcpConnection::getReadMax ( ) const
inline

Fetches the maximum number of bytes read during single socket read.

Returns
Maximum number of bytes to read.

Definition at line 290 of file tcp_connection.h.

References read_max_.

◆ getRemoteEndpoint()

const boost::asio::ip::tcp::endpoint isc::tcp::TcpConnection::getRemoteEndpoint ( ) const
inline

Fetches the remote endpoint for the connection's socket.

Returns
A reference to the endpoint if the socket is open, otherwise NO_ENDPOINT.

Definition at line 318 of file tcp_connection.h.

References remote_endpoint_.

◆ getRemoteEndpointAddressAsText()

std::string isc::tcp::TcpConnection::getRemoteEndpointAddressAsText ( ) const
protected

returns remote address in textual form

Definition at line 510 of file tcp_connection.cc.

References NO_ENDPOINT(), and remote_endpoint_.

Referenced by acceptorCallback(), doWrite(), handshakeCallback(), idleTimeoutCallback(), postData(), shutdownConnection(), socketReadCallback(), socketWriteCallback(), and stopThisConnection().

+ Here is the call graph for this function:

◆ handshakeCallback()

void isc::tcp::TcpConnection::handshakeCallback ( const boost::system::error_code &  ec)
protected

Local callback invoked when TLS handshake is performed.

If the handshake is performed successfully the TcpConnection::doRead is called.

Parameters
ecError code.

Definition at line 345 of file tcp_connection.cc.

References isc::log::DBGLVL_TRACE_DETAIL, doRead(), getRemoteEndpointAddressAsText(), idle_timeout_, LOG_DEBUG, LOG_INFO, stopThisConnection(), isc::tcp::tcp_logger, isc::tcp::TLS_CONNECTION_HANDSHAKE_FAILED, and isc::tcp::TLS_REQUEST_RECEIVE_START.

Referenced by doHandshake().

+ Here is the call graph for this function:

◆ idleTimeoutCallback()

void isc::tcp::TcpConnection::idleTimeoutCallback ( )
protected

Callback invoked when the client has been idle.

Definition at line 499 of file tcp_connection.cc.

References isc::log::DBGLVL_TRACE_DETAIL, getRemoteEndpointAddressAsText(), LOG_DEBUG, stopThisConnection(), isc::tcp::TCP_IDLE_CONNECTION_TIMEOUT_OCCURRED, and isc::tcp::tcp_logger.

Referenced by setupIdleTimer().

+ Here is the call graph for this function:

◆ NO_ENDPOINT()

static const boost::asio::ip::tcp::endpoint & isc::tcp::TcpConnection::NO_ENDPOINT ( )
inlinestatic

Returns an empty end point.

Returns
an uninitialized endpoint.

Definition at line 309 of file tcp_connection.h.

Referenced by acceptorCallback(), getRemoteEndpointAddressAsText(), and isc::tcp::TcpConnectionPool::usedByRemoteIpInternal().

◆ postData()

TcpRequestPtr isc::tcp::TcpConnection::postData ( TcpRequestPtr  request,
WireData input_data 
)

Appends newly received raw data to the given request.

The input data is passed into the current request's postBuffer method. If the request is still incomplete, we return it and wait for more data to post. Otherwise, the request is complete and it is passed into TcpConnection::requestReceived() to be processed. Upon return from that, a new request is created and returned to be used for the next read cycle.

Parameters
requestrequest to which data should be posted.
input_dataraw data to post.
Returns
Pointer to the request to use for the next read.

Definition at line 403 of file tcp_connection.cc.

References isc::asiolink::IntervalTimer::cancel(), createRequest(), isc::log::DBGLVL_TRACE_BASIC, getRemoteEndpointAddressAsText(), idle_timer_, LOG_DEBUG, LOG_ERROR, postData(), requestReceived(), isc::tcp::TCP_CLIENT_REQUEST_RECEIVED, isc::tcp::tcp_logger, and isc::tcp::TCP_REQUEST_RECEIVED_FAILED.

Referenced by postData(), and socketReadCallback().

+ Here is the call graph for this function:

◆ requestReceived()

virtual void isc::tcp::TcpConnection::requestReceived ( TcpRequestPtr  request)
pure virtual

Processes a request once it has been completely received.

This function is called by postData() if the post results in a completion (i.e. request's needData() returns false).

Referenced by postData().

◆ responseSent()

virtual bool isc::tcp::TcpConnection::responseSent ( TcpResponsePtr  response)
pure virtual

Determines behavior after a response has been sent.

Parameters
responsePointer to the response sent.
Returns
True if the idle timer should be started.

Referenced by doWrite().

◆ setReadMax()

void isc::tcp::TcpConnection::setReadMax ( const size_t  read_max)

Sets the maximum number of bytes read during single socket read.

Parameters
read_maxmaximum number of bytes to read.
Exceptions
BadValueif the parameter is not greater than zero.

Definition at line 519 of file tcp_connection.cc.

References input_buf_, isc_throw, and read_max_.

◆ setupIdleTimer()

void isc::tcp::TcpConnection::setupIdleTimer ( )
protected

Reset timer for detecting idle timeout in connections.

Definition at line 493 of file tcp_connection.cc.

References idle_timeout_, idle_timer_, idleTimeoutCallback(), isc::asiolink::IntervalTimer::ONE_SHOT, and isc::asiolink::IntervalTimer::setup().

Referenced by doHandshake(), doRead(), doWrite(), and socketReadCallback().

+ Here is the call graph for this function:

◆ shutdown()

void isc::tcp::TcpConnection::shutdown ( )
virtual

Shutdown the socket.

Definition at line 88 of file tcp_connection.cc.

References isc::asiolink::IntervalTimer::cancel(), idle_timer_, isc_throw, shutdownCallback(), tcp_socket_, and tls_socket_.

+ Here is the call graph for this function:

◆ shutdownCallback()

void isc::tcp::TcpConnection::shutdownCallback ( const boost::system::error_code &  ec)
protected

Callback invoked when TLS shutdown is performed.

The TLS socket is unconditionally closed but the callback is called only when the peer has answered so the connection should be explicitly closed in all cases, i.e. do not rely on this handler.

Parameters
ecError code (ignored).

Definition at line 83 of file tcp_connection.cc.

References tls_socket_.

Referenced by shutdown().

◆ shutdownConnection()

void isc::tcp::TcpConnection::shutdownConnection ( )
protectedvirtual

◆ socketReadCallback()

void isc::tcp::TcpConnection::socketReadCallback ( TcpRequestPtr  request,
boost::system::error_code  ec,
size_t  length 
)
protected

Callback invoked when new data is received over the socket.

This callback supplies the data to the TCP parser and continues parsing. When the parser signals end of the TCP request the callback prepares a response and starts asynchronous send over the socket.

Parameters
requestPointer to the request for which the callback is invoked.
ecError code.
lengthLength of the received data.

Definition at line 361 of file tcp_connection.cc.

References isc::log::DBGLVL_TRACE_DETAIL_DATA, doRead(), getRemoteEndpointAddressAsText(), input_buf_, LOG_DEBUG, postData(), setupIdleTimer(), stopThisConnection(), isc::tcp::TCP_DATA_RECEIVED, and isc::tcp::tcp_logger.

Referenced by doRead().

+ Here is the call graph for this function:

◆ socketWriteCallback()

void isc::tcp::TcpConnection::socketWriteCallback ( TcpResponsePtr  request,
boost::system::error_code  ec,
size_t  length 
)
protectedvirtual

Callback invoked when data is sent over the socket.

Parameters
requestPointer to the request for which the callback is invoked.
ecError code.
lengthLength of the data sent.

Definition at line 448 of file tcp_connection.cc.

References isc::log::DBGLVL_TRACE_DETAIL_DATA, doWrite(), getRemoteEndpointAddressAsText(), LOG_DEBUG, stopThisConnection(), isc::tcp::TCP_DATA_SENT, and isc::tcp::tcp_logger.

Referenced by doWrite().

+ Here is the call graph for this function:

◆ stopThisConnection()

void isc::tcp::TcpConnection::stopThisConnection ( )
protectedvirtual

Member Data Documentation

◆ acceptor_

TcpConnectionAcceptorPtr isc::tcp::TcpConnection::acceptor_
protected

Pointer to the TCP acceptor used to accept new connections.

Definition at line 445 of file tcp_connection.h.

Referenced by acceptorCallback(), and asyncAccept().

◆ acceptor_callback_

TcpConnectionAcceptorCallback isc::tcp::TcpConnection::acceptor_callback_
protected

External TCP acceptor callback.

Definition at line 451 of file tcp_connection.h.

Referenced by acceptorCallback().

◆ connection_filter_

TcpConnectionFilterCallback isc::tcp::TcpConnection::connection_filter_
protected

External callback for filtering connections by IP address.

Definition at line 454 of file tcp_connection.h.

Referenced by acceptorCallback().

◆ connection_pool_

TcpConnectionPool& isc::tcp::TcpConnection::connection_pool_
protected

Connection pool holding this connection.

Definition at line 448 of file tcp_connection.h.

Referenced by shutdownConnection(), and stopThisConnection().

◆ idle_timeout_

long isc::tcp::TcpConnection::idle_timeout_
protected

Timeout after which the a TCP connection is shut down by the server.

Definition at line 433 of file tcp_connection.h.

Referenced by acceptorCallback(), handshakeCallback(), and setupIdleTimer().

◆ idle_timer_

asiolink::IntervalTimer isc::tcp::TcpConnection::idle_timer_
protected

Timer used to detect idle Timeout.

Definition at line 436 of file tcp_connection.h.

Referenced by close(), postData(), setupIdleTimer(), and shutdown().

◆ input_buf_

WireData isc::tcp::TcpConnection::input_buf_
protected

Buffer for a single socket read.

Definition at line 460 of file tcp_connection.h.

Referenced by getInputBufData(), getInputBufSize(), setReadMax(), and socketReadCallback().

◆ io_service_

asiolink::IOServicePtr isc::tcp::TcpConnection::io_service_
protected

The IO service used to handle events.

Definition at line 426 of file tcp_connection.h.

◆ read_max_

size_t isc::tcp::TcpConnection::read_max_
protected

Maximum bytes to read in a single socket read.

Definition at line 457 of file tcp_connection.h.

Referenced by getReadMax(), and setReadMax().

◆ remote_endpoint_

boost::asio::ip::tcp::endpoint isc::tcp::TcpConnection::remote_endpoint_
protected

Remote endpoint.

Definition at line 463 of file tcp_connection.h.

Referenced by acceptorCallback(), getRemoteEndpoint(), and getRemoteEndpointAddressAsText().

◆ tcp_socket_

std::unique_ptr<asiolink::TCPSocket<SocketCallback> > isc::tcp::TcpConnection::tcp_socket_
protected

TCP socket used by this connection.

Definition at line 439 of file tcp_connection.h.

Referenced by TcpConnection(), acceptorCallback(), asyncAccept(), close(), doRead(), doWrite(), and shutdown().

◆ tls_context_

asiolink::TlsContextPtr isc::tcp::TcpConnection::tls_context_
protected

TLS context.

Definition at line 429 of file tcp_connection.h.

Referenced by acceptorCallback().

◆ tls_socket_

std::unique_ptr<asiolink::TLSSocket<SocketCallback> > isc::tcp::TcpConnection::tls_socket_
protected

TLS socket used by this connection.

Definition at line 442 of file tcp_connection.h.

Referenced by TcpConnection(), acceptorCallback(), asyncAccept(), close(), doHandshake(), doRead(), doWrite(), shutdown(), and shutdownCallback().


The documentation for this class was generated from the following files: