Kea: ping_channel.h Source File

| Kea 3.0.0

ping_channel.h
Go to the documentation of this file.
1// Copyright (C) 2023-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef PING_CHANNEL_H
8#define PING_CHANNEL_H
9
10#include <asiolink/asio_wrapper.h>
11#include <asiolink/io_address.h>
12#include <asiolink/io_service.h>
13#include <util/watch_socket.h>
14#include <icmp_msg.h>
15#include <icmp_socket.h>
16
17#include <boost/scoped_ptr.hpp>
18#include <boost/enable_shared_from_this.hpp>
19
20#include <iostream>
21#include <mutex>
22
23namespace isc {
24namespace ping_check {
25
28 typedef std::function<void(boost::system::error_code ec, size_t length)> SocketCallbackFunction;
29
33 class SocketCallback {
34public:
39 explicit inline SocketCallback(SocketCallbackFunction socket_callback)
40 : callback_(socket_callback) {
41 };
42
50 inline void operator()(boost::system::error_code ec, size_t length = 0) {
51 if (ec.value() == boost::asio::error::operation_aborted) {
52 return;
53 }
54
55 callback_(ec, length);
56 };
57
58private:
60 SocketCallbackFunction callback_;
61};
62
64 typedef ICMPSocket<SocketCallback> PingSocket;
65
67 typedef boost::shared_ptr<PingSocket> PingSocketPtr;
68
70 typedef std::function<bool(asiolink::IOAddress& target)> NextToSendCallback;
71
73 typedef std::function<void(ICMPMsgPtr& echo, bool send_failed)> EchoSentCallback;
74
77 typedef std::function<void(ICMPMsgPtr& reply)> ReplyReceivedCallback;
78
80 typedef std::function<void()> ShutdownCallback;
81
103 class PingChannel : public boost::enable_shared_from_this<PingChannel> {
104public:
121 PingChannel(asiolink::IOServicePtr& io_service,
122 NextToSendCallback next_to_send_cb,
123 EchoSentCallback echo_sent_cb,
124 ReplyReceivedCallback reply_received_cb,
125 ShutdownCallback shutdown_cb = ShutdownCallback());
126
130 virtual ~PingChannel();
131
137 void open();
138
142 bool isOpen() const;
143
144 // @brief Schedules the next send.
145 //
146 // If the socket is not currently sending it posts a call to @c sendNext()
147 // to the channel's IOService.
148 virtual void startSend();
149
150 // @brief Schedules the next read.
151 //
152 // If the socket is not currently reading it posts a call to @c doRead()
153 // to the channel's IOService.
154 void startRead();
155
157 void close();
158
162 asiolink::IOServicePtr getIOService() {
163 return (io_service_);
164 }
165
166protected:
181 virtual void asyncReceive(void* data, size_t length, size_t offset,
182 asiolink::IOEndpoint* endpoint, SocketCallback& callback);
183
197 virtual void asyncSend(void* data, size_t length, asiolink::IOEndpoint* endpoint,
198 SocketCallback& callback);
199
200protected:
208 void doRead();
209
227 void socketReadCallback(boost::system::error_code ec, size_t length);
228
239 virtual void sendNext();
240
259 void socketWriteCallback(ICMPMsgPtr echo_sent, boost::system::error_code ec,
260 size_t length);
261
266 void stopChannel();
267
278 static uint32_t nextEchoInstanceNum();
279
286 bool canSend() {
287 return (socket_ && socket_->isOpen() && !stopping_ && !sending_);
288 }
289
296 bool canRead() {
297 return (socket_ && socket_->isOpen() && !stopping_ && !reading_);
298 }
299
305 size_t getInputBufSize() const;
306
313 unsigned char* getInputBufData();
314
316 asiolink::IOServicePtr io_service_;
317
319 NextToSendCallback next_to_send_cb_;
320
322 EchoSentCallback echo_sent_cb_;
323
325 ReplyReceivedCallback reply_received_cb_;
326
328 ShutdownCallback shutdown_cb_;
329
331 PingSocketPtr socket_;
332
334 std::vector<uint8_t> input_buf_;
335
337 ICMPEndpoint reply_endpoint_;
338
340 bool reading_;
341
343 bool sending_;
344
346 bool stopping_;
347
349 const boost::scoped_ptr<std::mutex> mutex_;
350
353 bool single_threaded_;
354
356 util::WatchSocketPtr watch_socket_;
357
359 int registered_write_fd_;
360
362 int registered_read_fd_;
363};
364
366 typedef boost::shared_ptr<PingChannel> PingChannelPtr;
367
368} // end of namespace ping_check
369} // end of namespace isc
370
371#endif
asio_wrapper.h
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic)
Definition io_address.h:45
isc::asiolink::IOEndpoint
The IOEndpoint class is an abstract base class to represent a communication endpoint.
Definition io_endpoint.h:38
isc::ping_check::ICMPEndpoint
The ICMPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a ICM...
Definition icmp_endpoint.h:19
isc::ping_check::ICMPSocket
The ICMPSocket class is a concrete derived class of IOAsioSocket that represents a ICMP socket.
Definition icmp_socket.h:30
isc::ping_check::PingChannel::registered_read_fd_
int registered_read_fd_
ICMPSocket fd registered with IfaceMgr.
Definition ping_channel.h:362
isc::ping_check::PingChannel::getInputBufSize
size_t getInputBufSize() const
Returns input buffer size.
Definition ping_channel.cc:451
isc::ping_check::PingChannel::doRead
void doRead()
Initiates an asynchronous socket read.
Definition ping_channel.cc:192
isc::ping_check::PingChannel::~PingChannel
virtual ~PingChannel()
Destructor.
Definition ping_channel.cc:57
isc::ping_check::PingChannel::socket_
PingSocketPtr socket_
Socket through which to ping.
Definition ping_channel.h:331
isc::ping_check::PingChannel::stopChannel
void stopChannel()
Closes the socket channel and invokes the shutdown callback.
Definition ping_channel.cc:156
isc::ping_check::PingChannel::stopping_
bool stopping_
Indicates whether or not the channel has been told to stop.
Definition ping_channel.h:346
isc::ping_check::PingChannel::sendNext
virtual void sendNext()
Initiates sending the next ECHO REQUEST.
Definition ping_channel.cc:311
isc::ping_check::PingChannel::sending_
bool sending_
Indicates whether or not the socket has a write in progress.
Definition ping_channel.h:343
isc::ping_check::PingChannel::isOpen
bool isOpen() const
Indicates whether or not the channel socket is open.
Definition ping_channel.cc:106
isc::ping_check::PingChannel::open
void open()
Opens the socket for communications.
Definition ping_channel.cc:62
isc::ping_check::PingChannel::PingChannel
PingChannel(asiolink::IOServicePtr &io_service, NextToSendCallback next_to_send_cb, EchoSentCallback echo_sent_cb, ReplyReceivedCallback reply_received_cb, ShutdownCallback shutdown_cb=ShutdownCallback())
Constructor.
Definition ping_channel.cc:37
isc::ping_check::PingChannel::nextEchoInstanceNum
static uint32_t nextEchoInstanceNum()
returns the next unique ECHO instance number.
Definition ping_channel.cc:26
isc::ping_check::PingChannel::input_buf_
std::vector< uint8_t > input_buf_
Buffer to hold the contents for most recent socket read.
Definition ping_channel.h:334
isc::ping_check::PingChannel::startRead
void startRead()
Definition ping_channel.cc:298
isc::ping_check::PingChannel::registered_write_fd_
int registered_write_fd_
WatchSocket fd registered with IfaceMgr.
Definition ping_channel.h:359
isc::ping_check::PingChannel::startSend
virtual void startSend()
Definition ping_channel.cc:285
isc::ping_check::PingChannel::socketWriteCallback
void socketWriteCallback(ICMPMsgPtr echo_sent, boost::system::error_code ec, size_t length)
Socket write completion callback.
Definition ping_channel.cc:361
isc::ping_check::PingChannel::reply_endpoint_
ICMPEndpoint reply_endpoint_
Retains the endpoint from which the most recent reply was received.
Definition ping_channel.h:337
isc::ping_check::PingChannel::echo_sent_cb_
EchoSentCallback echo_sent_cb_
Callback to invoke when an ECHO write has completed.
Definition ping_channel.h:322
isc::ping_check::PingChannel::getInputBufData
unsigned char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition ping_channel.cc:456
isc::ping_check::PingChannel::canRead
bool canRead()
Indicates whether or not a read can be initiated.
Definition ping_channel.h:296
isc::ping_check::PingChannel::io_service_
asiolink::IOServicePtr io_service_
IOService instance the drives socket IO.
Definition ping_channel.h:316
isc::ping_check::PingChannel::close
void close()
Closes the channel's socket.
Definition ping_channel.cc:112
isc::ping_check::PingChannel::reply_received_cb_
ReplyReceivedCallback reply_received_cb_
Callback to invoke when an ICMP reply has been received.
Definition ping_channel.h:325
isc::ping_check::PingChannel::shutdown_cb_
ShutdownCallback shutdown_cb_
Callback to invoke when the channel has shutdown.
Definition ping_channel.h:328
isc::ping_check::PingChannel::getIOService
asiolink::IOServicePtr getIOService()
Fetches the channel's IOService.
Definition ping_channel.h:162
isc::ping_check::PingChannel::watch_socket_
util::WatchSocketPtr watch_socket_
Pointer to WatchSocket instance supplying the "select-fd".
Definition ping_channel.h:356
isc::ping_check::PingChannel::reading_
bool reading_
Indicates whether or not the socket has a read in progress.
Definition ping_channel.h:340
isc::ping_check::PingChannel::next_to_send_cb_
NextToSendCallback next_to_send_cb_
Callback to invoke to fetch the next address to ping.
Definition ping_channel.h:319
isc::ping_check::PingChannel::socketReadCallback
void socketReadCallback(boost::system::error_code ec, size_t length)
Socket read completion callback.
Definition ping_channel.cc:220
isc::ping_check::PingChannel::asyncSend
virtual void asyncSend(void *data, size_t length, asiolink::IOEndpoint *endpoint, SocketCallback &callback)
Send data on the socket asynchronously.
Definition ping_channel.cc:181
isc::ping_check::PingChannel::mutex_
const boost::scoped_ptr< std::mutex > mutex_
The mutex used to protect internal state.
Definition ping_channel.h:349
isc::ping_check::PingChannel::asyncReceive
virtual void asyncReceive(void *data, size_t length, size_t offset, asiolink::IOEndpoint *endpoint, SocketCallback &callback)
Receive data on the socket asynchronously.
Definition ping_channel.cc:175
isc::ping_check::PingChannel::single_threaded_
bool single_threaded_
True if channel was opened in single-threaded mode, false otherwise.
Definition ping_channel.h:353
isc::ping_check::PingChannel::canSend
bool canSend()
Indicates whether or not a send can be initiated.
Definition ping_channel.h:286
isc::ping_check::SocketCallback
Functor associated with the socket object.
Definition ping_channel.h:33
isc::ping_check::SocketCallback::operator()
void operator()(boost::system::error_code ec, size_t length=0)
Operator called when event associated with a socket occurs.
Definition ping_channel.h:50
isc::ping_check::SocketCallback::SocketCallback
SocketCallback(SocketCallbackFunction socket_callback)
Constructor.
Definition ping_channel.h:39
icmp_msg.h
icmp_socket.h
io_address.h
io_service.h
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:28
isc::data
Definition base_stamped_element.cc:12
isc::ping_check
Definition config_cache.cc:19
isc::ping_check::ShutdownCallback
std::function< void()> ShutdownCallback
Function type for callback to invoke when the channel has shutdown.
Definition ping_channel.h:80
isc::ping_check::ICMPMsgPtr
boost::shared_ptr< ICMPMsg > ICMPMsgPtr
Shared pointer type for ICMPMsg.
Definition icmp_msg.h:26
isc::ping_check::ReplyReceivedCallback
std::function< void(ICMPMsgPtr &reply)> ReplyReceivedCallback
Function type for callback to invoke when an ICMP reply has been received.
Definition ping_channel.h:77
isc::ping_check::EchoSentCallback
std::function< void(ICMPMsgPtr &echo, bool send_failed)> EchoSentCallback
Function type for callback to invoke upon ECHO send completion.
Definition ping_channel.h:73
isc::ping_check::NextToSendCallback
std::function< bool(asiolink::IOAddress &target)> NextToSendCallback
Function type for callback that fetches next IOAddress to ping.
Definition ping_channel.h:70
isc::ping_check::PingChannelPtr
boost::shared_ptr< PingChannel > PingChannelPtr
Defines a smart pointer to PingChannel.
Definition ping_channel.h:366
isc::ping_check::SocketCallbackFunction
std::function< void(boost::system::error_code ec, size_t length)> SocketCallbackFunction
Type of the function implementing a callback invoked by the SocketCallback functor.
Definition ping_channel.h:28
isc::ping_check::PingSocketPtr
boost::shared_ptr< PingSocket > PingSocketPtr
Defines a pointer to PingSocket.
Definition ping_channel.h:67
isc::ping_check::PingSocket
ICMPSocket< SocketCallback > PingSocket
Socket type for performing ICMP socket IO.
Definition ping_channel.h:64
isc::util::WatchSocketPtr
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Definition watch_socket.h:138
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
watch_socket.h
Defines the class, WatchSocket.