20namespace ph = std::placeholders;
27 static uint32_t echo_instance_num = 0x00010000;
29 echo_instance_num = 0x00010001;
34 return (echo_instance_num);
53 "PingChannel ctor - io_service cannot be empty");
73 [](boost::system::error_code ec,
size_t ) {
80 socket_->open(&ping_to_endpoint, socket_cb);
98 }
catch (
const std::exception& ex) {
130 std::string error_string;
132 if (!error_string.empty()) {
146 }
catch (
const std::exception& ex) {
177 socket_->asyncReceive(
data, length, offset, endpoint, callback);
183 socket_->asyncSend(
data, length, endpoint, callback);
210 }
catch (
const std::exception& ex) {
229 if (ec.value() == boost::asio::error::operation_aborted) {
233 }
else if ((ec.value() == boost::asio::error::try_again) ||
234 (ec.value() == boost::asio::error::would_block)) {
259 .arg(reply->getSource())
261 .arg(reply->getSequence());
263 }
catch (
const std::exception& ex) {
293 io_service_->post(std::bind(f, shared_from_this()));
306 io_service_->post(std::bind(f, shared_from_this()));
330 next_echo->setDestination(target);
333 next_echo->setId(
static_cast<uint16_t
>(instance_num >> 16));
334 next_echo->setSequence(
static_cast<uint16_t
>(instance_num & 0x0000FFFF));
337 ICMPPtr echo_icmp = next_echo->pack();
349 asyncSend(echo_icmp.get(),
sizeof(
struct icmp), &target_endpoint,
cb);
350 }
catch (
const std::exception& ex) {
374 }
catch (
const std::exception& ex) {
388 bool send_failed =
false;
390 auto error_value = ec.value();
391 if (error_value == boost::asio::error::operation_aborted) {
395 }
else if ((error_value == boost::asio::error::try_again) ||
396 (error_value == boost::asio::error::would_block)) {
401 }
else if ((error_value == boost::asio::error::network_unreachable) ||
402 (error_value == boost::asio::error::host_unreachable) ||
403 (error_value == boost::asio::error::network_down)) {
407 }
else if (error_value == boost::asio::error::no_buffer_space) {
410 }
else if (error_value == boost::asio::error::access_denied) {
432 .arg(echo->getDestination())
436 }
else if (length > 0) {
439 .arg(echo->getDestination())
441 .arg(echo->getSequence());
459 "PingChannel::getInputBufData() - cannot access empty buffer");
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
A generic exception that is thrown when an unexpected error condition occurs.
The IOAddress class represents an IP addresses (version agnostic)
static const IOAddress & IPV4_ZERO_ADDRESS()
Returns an address set to all zeros.
The IOEndpoint class is an abstract base class to represent a communication endpoint.
void deleteExternalSocket(int socketfd)
Deletes external socket.
std::function< void(int fd)> SocketCallback
Defines callback used when data is received over external sockets.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
void addExternalSocket(int socketfd, SocketCallback callback)
Adds external socket and a callback.
The ICMPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a ICM...
Embodies an ICMP message.
static ICMPMsgPtr unpack(const uint8_t *wire_data, size_t length)
Unpacks an ICMP message from the given wire_data.
int registered_read_fd_
ICMPSocket fd registered with IfaceMgr.
size_t getInputBufSize() const
Returns input buffer size.
void doRead()
Initiates an asynchronous socket read.
virtual ~PingChannel()
Destructor.
PingSocketPtr socket_
Socket through which to ping.
void stopChannel()
Closes the socket channel and invokes the shutdown callback.
bool stopping_
Indicates whether or not the channel has been told to stop.
virtual void sendNext()
Initiates sending the next ECHO REQUEST.
bool sending_
Indicates whether or not the socket has a write in progress.
bool isOpen() const
Indicates whether or not the channel socket is open.
void open()
Opens the socket for communications.
PingChannel(asiolink::IOServicePtr &io_service, NextToSendCallback next_to_send_cb, EchoSentCallback echo_sent_cb, ReplyReceivedCallback reply_received_cb, ShutdownCallback shutdown_cb=ShutdownCallback())
Constructor.
static uint32_t nextEchoInstanceNum()
returns the next unique ECHO instance number.
std::vector< uint8_t > input_buf_
Buffer to hold the contents for most recent socket read.
int registered_write_fd_
WatchSocket fd registered with IfaceMgr.
void socketWriteCallback(ICMPMsgPtr echo_sent, boost::system::error_code ec, size_t length)
Socket write completion callback.
ICMPEndpoint reply_endpoint_
Retains the endpoint from which the most recent reply was received.
EchoSentCallback echo_sent_cb_
Callback to invoke when an ECHO write has completed.
unsigned char * getInputBufData()
Returns pointer to the first byte of the input buffer.
bool canRead()
Indicates whether or not a read can be initiated.
asiolink::IOServicePtr io_service_
IOService instance the drives socket IO.
void close()
Closes the channel's socket.
ReplyReceivedCallback reply_received_cb_
Callback to invoke when an ICMP reply has been received.
ShutdownCallback shutdown_cb_
Callback to invoke when the channel has shutdown.
util::WatchSocketPtr watch_socket_
Pointer to WatchSocket instance supplying the "select-fd".
bool reading_
Indicates whether or not the socket has a read in progress.
NextToSendCallback next_to_send_cb_
Callback to invoke to fetch the next address to ping.
void socketReadCallback(boost::system::error_code ec, size_t length)
Socket read completion callback.
virtual void asyncSend(void *data, size_t length, asiolink::IOEndpoint *endpoint, SocketCallback &callback)
Send data on the socket asynchronously.
const boost::scoped_ptr< std::mutex > mutex_
The mutex used to protect internal state.
virtual void asyncReceive(void *data, size_t length, size_t offset, asiolink::IOEndpoint *endpoint, SocketCallback &callback)
Receive data on the socket asynchronously.
bool single_threaded_
True if channel was opened in single-threaded mode, false otherwise.
bool canSend()
Indicates whether or not a send can be initiated.
Functor associated with the socket object.
Provides an IO "ready" semaphore for use with select() or poll() WatchSocket exposes a single open fi...
#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_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
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
Trace detailed operations.
std::function< void()> ShutdownCallback
Function type for callback to invoke when the channel has shutdown.
boost::shared_ptr< ICMPMsg > ICMPMsgPtr
Shared pointer type for ICMPMsg.
isc::log::Logger ping_check_logger("ping-check-hooks")
std::function< void(ICMPMsgPtr &reply)> ReplyReceivedCallback
Function type for callback to invoke when an ICMP reply has been received.
std::function< void(ICMPMsgPtr &echo, bool send_failed)> EchoSentCallback
Function type for callback to invoke upon ECHO send completion.
std::function< bool(asiolink::IOAddress &target)> NextToSendCallback
Function type for callback that fetches next IOAddress to ping.
boost::shared_ptr< struct icmp > ICMPPtr
Shared pointer type for struct icmp.
boost::shared_ptr< PingChannel > PingChannelPtr
Defines a smart pointer to PingChannel.
ICMPSocket< SocketCallback > PingSocket
Socket type for performing ICMP socket IO.
Defines the logger used by the top-level component of kea-lfc.
const isc::log::MessageID PING_CHECK_CHANNEL_STOP
const isc::log::MessageID PING_CHECK_CHANNEL_WATCH_SOCKET_CLEAR_ERROR
const isc::log::MessageID PING_CHECK_CHANNEL_SOCKET_CLOSED
const isc::log::MessageID PING_CHECK_CHANNEL_MALFORMED_PACKET_RECEIVED
const isc::log::MessageID PING_CHECK_CHANNEL_SOCKET_READ_FAILED
const isc::log::MessageID PING_CHECK_CHANNEL_SOCKET_WRITE_FAILED
const isc::log::MessageID PING_CHECK_CHANNEL_ECHO_REPLY_RECEIVED
const isc::log::MessageID PING_CHECK_UNEXPECTED_WRITE_ERROR
const isc::log::MessageID PING_CHECK_CHANNEL_SOCKET_OPENED
const isc::log::MessageID PING_CHECK_UNEXPECTED_READ_ERROR
const isc::log::MessageID PING_CHECK_CHANNEL_WATCH_SOCKET_CLOSE_ERROR
const isc::log::MessageID PING_CHECK_CHANNEL_SOCKET_CLOSE_ERROR
const isc::log::MessageID PING_CHECK_CHANNEL_NETWORK_WRITE_ERROR
const isc::log::MessageID PING_CHECK_CHANNEL_ECHO_REQUEST_SENT
RAII lock object to protect the code in the same scope with a mutex.