![]() |
Kea 3.1.1
|
Provides thread-safe ICMP ECHO REQUEST/ECHO REPLY service. More...
#include <ping_channel.h>
Public Member Functions | |
PingChannel (asiolink::IOServicePtr &io_service, NextToSendCallback next_to_send_cb, EchoSentCallback echo_sent_cb, ReplyReceivedCallback reply_received_cb, ShutdownCallback shutdown_cb=ShutdownCallback()) | |
Constructor. | |
virtual | ~PingChannel () |
Destructor. | |
void | close () |
Closes the channel's socket. | |
asiolink::IOServicePtr | getIOService () |
Fetches the channel's IOService. | |
bool | isOpen () const |
Indicates whether or not the channel socket is open. | |
void | open () |
Opens the socket for communications. | |
void | startRead () |
virtual void | startSend () |
Protected Member Functions | |
virtual void | asyncReceive (void *data, size_t length, size_t offset, asiolink::IOEndpoint *endpoint, SocketCallback &callback) |
Receive data on the socket asynchronously. | |
virtual void | asyncSend (void *data, size_t length, asiolink::IOEndpoint *endpoint, SocketCallback &callback) |
Send data on the socket asynchronously. | |
bool | canRead () |
Indicates whether or not a read can be initiated. | |
bool | canSend () |
Indicates whether or not a send can be initiated. | |
void | doRead () |
Initiates an asynchronous socket read. | |
unsigned char * | getInputBufData () |
Returns pointer to the first byte of the input buffer. | |
size_t | getInputBufSize () const |
Returns input buffer size. | |
virtual void | sendNext () |
Initiates sending the next ECHO REQUEST. | |
void | socketReadCallback (boost::system::error_code ec, size_t length) |
Socket read completion callback. | |
void | socketWriteCallback (ICMPMsgPtr echo_sent, boost::system::error_code ec, size_t length) |
Socket write completion callback. | |
void | stopChannel () |
Closes the socket channel and invokes the shutdown callback. | |
Static Protected Member Functions | |
static uint32_t | nextEchoInstanceNum () |
returns the next unique ECHO instance number. | |
Protected Attributes | |
EchoSentCallback | echo_sent_cb_ |
Callback to invoke when an ECHO write has completed. | |
std::vector< uint8_t > | input_buf_ |
Buffer to hold the contents for most recent socket read. | |
asiolink::IOServicePtr | io_service_ |
IOService instance the drives socket IO. | |
const boost::scoped_ptr< std::mutex > | mutex_ |
The mutex used to protect internal state. | |
NextToSendCallback | next_to_send_cb_ |
Callback to invoke to fetch the next address to ping. | |
bool | reading_ |
Indicates whether or not the socket has a read in progress. | |
int | registered_read_fd_ |
ICMPSocket fd registered with IfaceMgr. | |
int | registered_write_fd_ |
WatchSocket fd registered with IfaceMgr. | |
ICMPEndpoint | reply_endpoint_ |
Retains the endpoint from which the most recent reply was received. | |
ReplyReceivedCallback | reply_received_cb_ |
Callback to invoke when an ICMP reply has been received. | |
bool | sending_ |
Indicates whether or not the socket has a write in progress. | |
ShutdownCallback | shutdown_cb_ |
Callback to invoke when the channel has shutdown. | |
bool | single_threaded_ |
True if channel was opened in single-threaded mode, false otherwise. | |
PingSocketPtr | socket_ |
Socket through which to ping. | |
bool | stopping_ |
Indicates whether or not the channel has been told to stop. | |
util::WatchSocketPtr | watch_socket_ |
Pointer to WatchSocket instance supplying the "select-fd". | |
Provides thread-safe ICMP ECHO REQUEST/ECHO REPLY service.
PingChannel uses a PingSocket to send out ECHO REQUESTs and receive ICMP replies. It is thread-safe and can be driven either with a single-threaded IOService or a multi-threaded IOServiceThreadPool. It uses series of callbacks to perpetually send requests to target addresses and feed back replies received:
Callback handlers are supplied via the PingChannel constructor. Higher order functions are provided, that once instantiated, can be used by calling layers to control the channel (e.g. open the channel, initiate reading, initiate writing, and close the channel).
Definition at line 103 of file ping_channel.h.
isc::ping_check::PingChannel::PingChannel | ( | asiolink::IOServicePtr & | io_service, |
NextToSendCallback | next_to_send_cb, | ||
EchoSentCallback | echo_sent_cb, | ||
ReplyReceivedCallback | reply_received_cb, | ||
ShutdownCallback | shutdown_cb = ShutdownCallback() ) |
Constructor.
Instantiates the channel with its socket closed.
io_service | pointer to the IOService instance that will manage the channel's IO. Must not be empty |
next_to_send_cb | callback to invoke to fetch the next IOAddress to ping |
echo_sent_cb | callback to invoke when an ECHO send has completed |
reply_received_cb | callback to invoke when an ICMP reply has been received. This callback is passed all inbound ICMP messages (e.g. ECHO REPLY, UNREACHABLE, etc...) |
shutdown_cb | callback to invoke when the channel has shutdown due to an error |
BadValue | if io_service is empty. |
Definition at line 37 of file ping_channel.cc.
References echo_sent_cb_, input_buf_, io_service_, isc_throw, mutex_, next_to_send_cb_, reading_, registered_read_fd_, registered_write_fd_, reply_received_cb_, sending_, shutdown_cb_, single_threaded_, socket_, stopping_, and watch_socket_.
|
virtual |
Destructor.
Closes the socket if its open.
Definition at line 57 of file ping_channel.cc.
References close().
|
protectedvirtual |
Receive data on the socket asynchronously.
Calls the underlying socket's asyncReceive() method to read a packet of data from a remote endpoint. Arrival of the data is signalled via a call to the callback function.
This virtual function is provided as means to inject errors during read operations to facilitate testing.
data | buffer to receive incoming message |
length | length of the data buffer |
offset | offset into buffer where data is to be put |
endpoint | source of the communication |
callback | callback object |
Definition at line 175 of file ping_channel.cc.
References socket_.
Referenced by doRead().
|
protectedvirtual |
Send data on the socket asynchronously.
Calls the underlying socket's asyncSend() method to send a packet of data from a remote endpoint. Arrival of the data is signalled via a call to the callback function.
This virtual function is provided as means to inject errors during write operations to facilitate testing.
data | buffer containing the data to send |
length | length of the data buffer |
endpoint | destination of the communication |
callback | callback object |
Definition at line 181 of file ping_channel.cc.
References single_threaded_, socket_, and watch_socket_.
Referenced by sendNext().
|
inlineprotected |
Indicates whether or not a read can be initiated.
Must be called in a thread-safe context
Definition at line 296 of file ping_channel.h.
References reading_, socket_, and stopping_.
Referenced by doRead(), and startRead().
|
inlineprotected |
Indicates whether or not a send can be initiated.
Must be called in a thread-safe context
Definition at line 286 of file ping_channel.h.
References sending_, socket_, and stopping_.
Referenced by sendNext(), and startSend().
void isc::ping_check::PingChannel::close | ( | ) |
Closes the channel's socket.
Definition at line 112 of file ping_channel.cc.
References isc::log::DBGLVL_TRACE_BASIC, isc::dhcp::IfaceMgr::deleteExternalSocket(), isc::dhcp::IfaceMgr::instance(), LOG_DEBUG, LOG_ERROR, mutex_, PING_CHECK_CHANNEL_SOCKET_CLOSE_ERROR, PING_CHECK_CHANNEL_SOCKET_CLOSED, PING_CHECK_CHANNEL_WATCH_SOCKET_CLOSE_ERROR, isc::ping_check::ping_check_logger, registered_read_fd_, registered_write_fd_, single_threaded_, socket_, and watch_socket_.
Referenced by ~PingChannel(), and stopChannel().
|
protected |
Initiates an asynchronous socket read.
If the channel is able to read (is open, not stopping and not currently reading) it invokes isc::ping_check::ICMPSocket<SocketCallback>::asyncReceive() otherwise it simply returns. If the call to asyncReceive() fails it calls stopChannel()
otherwise, when it completes it will invoke socketReadCallback()
.
Definition at line 192 of file ping_channel.cc.
References asyncReceive(), canRead(), getInputBufData(), getInputBufSize(), LOG_ERROR, mutex_, isc::ping_check::ping_check_logger, PING_CHECK_UNEXPECTED_READ_ERROR, reading_, reply_endpoint_, socketReadCallback(), and stopChannel().
Referenced by socketReadCallback().
|
protected |
Returns pointer to the first byte of the input buffer.
Must be called in a thread-safe context
InvalidOperation | if called when the buffer is empty. |
Definition at line 456 of file ping_channel.cc.
References input_buf_, and isc_throw.
Referenced by doRead(), and socketReadCallback().
|
protected |
Returns input buffer size.
Must be called in a thread-safe context
Definition at line 451 of file ping_channel.cc.
References input_buf_.
Referenced by doRead(), and socketReadCallback().
|
inline |
Fetches the channel's IOService.
Definition at line 162 of file ping_channel.h.
References io_service_.
bool isc::ping_check::PingChannel::isOpen | ( | ) | const |
Indicates whether or not the channel socket is open.
Definition at line 106 of file ping_channel.cc.
|
staticprotected |
returns the next unique ECHO instance number.
This method generates and returns the next ECHO instance number by incrementing the current value. It is a strictly monotonously increasing value beginning at 0x00010001. At roll over it resets to 0x00010001.
Must be called in a thread-safe context
Definition at line 26 of file ping_channel.cc.
References UINT32_MAX.
Referenced by sendNext().
void isc::ping_check::PingChannel::open | ( | ) |
Opens the socket for communications.
(Re)Creates the PingSocket instance and opens it.
Unexpected | if the open fails. |
Definition at line 62 of file ping_channel.cc.
References isc::dhcp::IfaceMgr::addExternalSocket(), isc::log::DBGLVL_TRACE_BASIC, isc::dhcp::IfaceMgr::instance(), io_service_, isc::asiolink::IOAddress::IPV4_ZERO_ADDRESS(), isc_throw, LOG_DEBUG, mutex_, PING_CHECK_CHANNEL_SOCKET_OPENED, isc::ping_check::ping_check_logger, reading_, registered_read_fd_, registered_write_fd_, sending_, single_threaded_, socket_, stopping_, and watch_socket_.
|
protectedvirtual |
Initiates sending the next ECHO REQUEST.
If the channel is able to send (i.e is open, not stopping and not currently writing):
PingSocket::asyncSend()
socketWriteCallback()
. Definition at line 311 of file ping_channel.cc.
References asyncSend(), canSend(), isc::ping_check::ICMPMsg::ECHO_REQUEST, LOG_ERROR, mutex_, next_to_send_cb_, nextEchoInstanceNum(), isc::ping_check::ping_check_logger, PING_CHECK_UNEXPECTED_WRITE_ERROR, sending_, socketWriteCallback(), and stopChannel().
Referenced by socketWriteCallback().
|
protected |
Socket read completion callback.
Invoked when PingSocket::asyncRead() completes. Upon read success and data received:
On error conditions:
ec | error code indicating either success or the error encountered |
length | number of bytes read |
Definition at line 220 of file ping_channel.cc.
References isc::log::DBGLVL_TRACE_BASIC, isc::log::DBGLVL_TRACE_DETAIL, doRead(), isc::ping_check::ICMPMsg::ECHO_REPLY, getInputBufData(), getInputBufSize(), LOG_DEBUG, LOG_ERROR, mutex_, PING_CHECK_CHANNEL_ECHO_REPLY_RECEIVED, PING_CHECK_CHANNEL_MALFORMED_PACKET_RECEIVED, PING_CHECK_CHANNEL_SOCKET_READ_FAILED, isc::ping_check::ping_check_logger, reading_, reply_received_cb_, stopChannel(), stopping_, and isc::ping_check::ICMPMsg::unpack().
Referenced by doRead().
|
protected |
Socket write completion callback.
Invoked when PingSocket::asyncWrite() completes. Upon write success:
On error conditions:
echo_sent | ECHO REQUEST that was written (or attempted to be written) |
ec | error code indicating either success or the error encountered |
length | number of bytes written |
Definition at line 361 of file ping_channel.cc.
References isc::log::DBGLVL_TRACE_DETAIL, echo_sent_cb_, LOG_DEBUG, LOG_ERROR, mutex_, PING_CHECK_CHANNEL_ECHO_REQUEST_SENT, PING_CHECK_CHANNEL_NETWORK_WRITE_ERROR, PING_CHECK_CHANNEL_SOCKET_WRITE_FAILED, PING_CHECK_CHANNEL_WATCH_SOCKET_CLEAR_ERROR, isc::ping_check::ping_check_logger, sending_, sendNext(), single_threaded_, stopChannel(), stopping_, and watch_socket_.
Referenced by sendNext().
void isc::ping_check::PingChannel::startRead | ( | ) |
Definition at line 298 of file ping_channel.cc.
References canRead(), io_service_, and mutex_.
|
virtual |
Definition at line 285 of file ping_channel.cc.
References canSend(), io_service_, and mutex_.
|
protected |
Closes the socket channel and invokes the shutdown callback.
This function is invoked to notify the calling layer that the socket has encountered an unrecoverable error and is stopping operations.
Definition at line 156 of file ping_channel.cc.
References close(), isc::log::DBGLVL_TRACE_BASIC, LOG_DEBUG, mutex_, PING_CHECK_CHANNEL_STOP, isc::ping_check::ping_check_logger, shutdown_cb_, and stopping_.
Referenced by doRead(), sendNext(), socketReadCallback(), and socketWriteCallback().
|
protected |
Callback to invoke when an ECHO write has completed.
Definition at line 322 of file ping_channel.h.
Referenced by PingChannel(), and socketWriteCallback().
|
protected |
Buffer to hold the contents for most recent socket read.
Definition at line 334 of file ping_channel.h.
Referenced by PingChannel(), getInputBufData(), and getInputBufSize().
|
protected |
IOService instance the drives socket IO.
Definition at line 316 of file ping_channel.h.
Referenced by PingChannel(), getIOService(), open(), startRead(), and startSend().
|
protected |
The mutex used to protect internal state.
Definition at line 349 of file ping_channel.h.
Referenced by PingChannel(), close(), doRead(), isOpen(), open(), sendNext(), socketReadCallback(), socketWriteCallback(), startRead(), startSend(), and stopChannel().
|
protected |
Callback to invoke to fetch the next address to ping.
Definition at line 319 of file ping_channel.h.
Referenced by PingChannel(), and sendNext().
|
protected |
Indicates whether or not the socket has a read in progress.
Definition at line 340 of file ping_channel.h.
Referenced by PingChannel(), canRead(), doRead(), open(), and socketReadCallback().
|
protected |
ICMPSocket fd registered with IfaceMgr.
Definition at line 362 of file ping_channel.h.
Referenced by PingChannel(), close(), and open().
|
protected |
WatchSocket fd registered with IfaceMgr.
Definition at line 359 of file ping_channel.h.
Referenced by PingChannel(), close(), and open().
|
protected |
Retains the endpoint from which the most recent reply was received.
Definition at line 337 of file ping_channel.h.
Referenced by doRead().
|
protected |
Callback to invoke when an ICMP reply has been received.
Definition at line 325 of file ping_channel.h.
Referenced by PingChannel(), and socketReadCallback().
|
protected |
Indicates whether or not the socket has a write in progress.
Definition at line 343 of file ping_channel.h.
Referenced by PingChannel(), canSend(), open(), sendNext(), and socketWriteCallback().
|
protected |
Callback to invoke when the channel has shutdown.
Definition at line 328 of file ping_channel.h.
Referenced by PingChannel(), and stopChannel().
|
protected |
True if channel was opened in single-threaded mode, false otherwise.
Definition at line 353 of file ping_channel.h.
Referenced by PingChannel(), asyncSend(), close(), open(), and socketWriteCallback().
|
protected |
Socket through which to ping.
Definition at line 331 of file ping_channel.h.
Referenced by PingChannel(), asyncReceive(), asyncSend(), canRead(), canSend(), close(), isOpen(), and open().
|
protected |
Indicates whether or not the channel has been told to stop.
Definition at line 346 of file ping_channel.h.
Referenced by PingChannel(), canRead(), canSend(), open(), socketReadCallback(), socketWriteCallback(), and stopChannel().
|
protected |
Pointer to WatchSocket instance supplying the "select-fd".
Definition at line 356 of file ping_channel.h.
Referenced by PingChannel(), asyncSend(), close(), open(), and socketWriteCallback().