Kea 2.7.6
|
Accepts and handles a single TCP connection. More...
#include <tcp_connection.h>
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. | |
TcpConnectionPool & | connection_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. | |
Accepts and handles a single TCP connection.
Definition at line 157 of file tcp_connection.h.
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.
io_service | IO service to be used by the connection. |
acceptor | Pointer to the TCP acceptor object used to listen for new TCP connections. |
tls_context | TLS context. |
connection_pool | Connection pool in which this connection is stored. |
acceptor_callback | Callback invoked when new connection is accepted. |
connection_filter | Callback invoked prior to handshake which can be used to qualify and reject connections |
idle_timeout | Timeout after which a TCP connection is closed by the server. |
read_max | maximum size of a single socket read. Defaults to 32K. |
Definition at line 41 of file tcp_connection.cc.
References tcp_socket_, and tls_socket_.
|
virtual |
Destructor.
Closes current connection.
Definition at line 69 of file tcp_connection.cc.
References close().
|
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.
ec | Error code. |
Definition at line 282 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().
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 141 of file tcp_connection.cc.
References acceptor_, acceptorCallback(), isc_throw, tcp_socket_, and tls_socket_.
|
protected |
Sends TCP response asynchronously.
Internally it calls TcpConnection::doWrite to send the data.
response | Pointer to the TCP response to be sent. |
Definition at line 276 of file tcp_connection.cc.
References doWrite().
|
virtual |
Closes the socket.
Definition at line 100 of file tcp_connection.cc.
References isc::asiolink::IntervalTimer::cancel(), idle_timer_, isc_throw, tcp_socket_, and tls_socket_.
Referenced by ~TcpConnection().
|
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.
Referenced by doRead(), and postData().
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 169 of file tcp_connection.cc.
References doRead(), handshakeCallback(), isc_throw, setupIdleTimer(), and tls_socket_.
Referenced by acceptorCallback().
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.
request | Pointer 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 194 of file tcp_connection.cc.
References createRequest(), getInputBufData(), getInputBufSize(), setupIdleTimer(), socketReadCallback(), stopThisConnection(), tcp_socket_, and tls_socket_.
Referenced by doHandshake(), handshakeCallback(), and socketReadCallback().
|
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.
response | Pointer to the response to write operation should be performed. |
Definition at line 231 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().
|
inlineprotected |
Returns pointer to the first byte of the input buffer.
InvalidOperation | if called when the buffer is empty. |
Definition at line 412 of file tcp_connection.h.
References input_buf_, and isc_throw.
Referenced by doRead().
|
inlineprotected |
Returns input buffer size.
Definition at line 421 of file tcp_connection.h.
References input_buf_.
Referenced by doRead().
|
inline |
Fetches the maximum number of bytes read during single socket read.
Definition at line 290 of file tcp_connection.h.
References read_max_.
|
inline |
Fetches the remote endpoint for the connection's socket.
Definition at line 318 of file tcp_connection.h.
References remote_endpoint_.
|
protected |
returns remote address in textual form
Definition at line 501 of file tcp_connection.cc.
References NO_ENDPOINT(), and remote_endpoint_.
Referenced by acceptorCallback(), doWrite(), handshakeCallback(), idleTimeoutCallback(), postData(), shutdownConnection(), socketReadCallback(), socketWriteCallback(), and stopThisConnection().
|
protected |
Local callback invoked when TLS handshake is performed.
If the handshake is performed successfully the TcpConnection::doRead is called.
ec | Error code. |
Definition at line 336 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().
|
protected |
Callback invoked when the client has been idle.
Definition at line 490 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().
|
inlinestatic |
Returns an empty end point.
Definition at line 309 of file tcp_connection.h.
Referenced by acceptorCallback(), getRemoteEndpointAddressAsText(), and isc::tcp::TcpConnectionPool::usedByRemoteIpInternal().
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.
request | request to which data should be posted. |
input_data | raw data to post. |
Definition at line 394 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().
|
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().
|
pure virtual |
Determines behavior after a response has been sent.
response | Pointer to the response sent. |
Referenced by doWrite().
void isc::tcp::TcpConnection::setReadMax | ( | const size_t | read_max | ) |
Sets the maximum number of bytes read during single socket read.
read_max | maximum number of bytes to read. |
BadValue | if the parameter is not greater than zero. |
Definition at line 510 of file tcp_connection.cc.
References input_buf_, isc_throw, and read_max_.
|
protected |
Reset timer for detecting idle timeout in connections.
Definition at line 484 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().
|
virtual |
Shutdown the socket.
Definition at line 79 of file tcp_connection.cc.
References isc::asiolink::IntervalTimer::cancel(), idle_timer_, isc_throw, shutdownCallback(), tcp_socket_, and tls_socket_.
|
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.
ec | Error code (ignored). |
Definition at line 74 of file tcp_connection.cc.
References tls_socket_.
Referenced by shutdown().
|
protectedvirtual |
Shuts down current connection.
Copied from the next method stopThisConnection
Definition at line 117 of file tcp_connection.cc.
References connection_pool_, isc::log::DBGLVL_TRACE_BASIC, getRemoteEndpointAddressAsText(), LOG_DEBUG, LOG_ERROR, isc::tcp::TcpConnectionPool::shutdown(), isc::tcp::TCP_CONNECTION_SHUTDOWN, isc::tcp::TCP_CONNECTION_SHUTDOWN_FAILED, and isc::tcp::tcp_logger.
|
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.
request | Pointer to the request for which the callback is invoked. |
ec | Error code. |
length | Length of the received data. |
Definition at line 352 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().
|
protectedvirtual |
Callback invoked when data is sent over the socket.
request | Pointer to the request for which the callback is invoked. |
ec | Error code. |
length | Length of the data sent. |
Definition at line 439 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().
|
protectedvirtual |
Stops current connection.
Definition at line 129 of file tcp_connection.cc.
References connection_pool_, isc::log::DBGLVL_TRACE_BASIC, getRemoteEndpointAddressAsText(), LOG_DEBUG, LOG_ERROR, isc::tcp::TcpConnectionPool::stop(), isc::tcp::TCP_CONNECTION_STOP, isc::tcp::TCP_CONNECTION_STOP_FAILED, and isc::tcp::tcp_logger.
Referenced by acceptorCallback(), doRead(), doWrite(), handshakeCallback(), idleTimeoutCallback(), socketReadCallback(), and socketWriteCallback().
|
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().
|
protected |
External TCP acceptor callback.
Definition at line 451 of file tcp_connection.h.
Referenced by acceptorCallback().
|
protected |
External callback for filtering connections by IP address.
Definition at line 454 of file tcp_connection.h.
Referenced by acceptorCallback().
|
protected |
Connection pool holding this connection.
Definition at line 448 of file tcp_connection.h.
Referenced by shutdownConnection(), and stopThisConnection().
|
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().
|
protected |
Timer used to detect idle Timeout.
Definition at line 436 of file tcp_connection.h.
Referenced by close(), postData(), setupIdleTimer(), and shutdown().
|
protected |
Buffer for a single socket read.
Definition at line 460 of file tcp_connection.h.
Referenced by getInputBufData(), getInputBufSize(), setReadMax(), and socketReadCallback().
|
protected |
The IO service used to handle events.
Definition at line 426 of file tcp_connection.h.
|
protected |
Maximum bytes to read in a single socket read.
Definition at line 457 of file tcp_connection.h.
Referenced by getReadMax(), and setReadMax().
|
protected |
Remote endpoint.
Definition at line 463 of file tcp_connection.h.
Referenced by acceptorCallback(), getRemoteEndpoint(), and getRemoteEndpointAddressAsText().
|
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().
|
protected |
|
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().