Kea 3.1.1
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
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
28typedef std::function<void(boost::system::error_code ec, size_t length)> SocketCallbackFunction;
29
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
65
67typedef boost::shared_ptr<PingSocket> PingSocketPtr;
68
70typedef std::function<bool(asiolink::IOAddress& target)> NextToSendCallback;
71
73typedef std::function<void(ICMPMsgPtr& echo, bool send_failed)> EchoSentCallback;
74
77typedef std::function<void(ICMPMsgPtr& reply)> ReplyReceivedCallback;
78
80typedef std::function<void()> ShutdownCallback;
81
103class PingChannel : public boost::enable_shared_from_this<PingChannel> {
104public:
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
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
317
320
323
326
329
332
334 std::vector<uint8_t> input_buf_;
335
338
341
344
347
349 const boost::scoped_ptr<std::mutex> mutex_;
350
354
357
360
363};
364
366typedef boost::shared_ptr<PingChannel> PingChannelPtr;
367
368} // end of namespace ping_check
369} // end of namespace isc
370
371#endif
The ICMPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a ICM...
The ICMPSocket class is a concrete derived class of IOAsioSocket that represents a ICMP socket.
Definition icmp_socket.h:30
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.
asiolink::IOServicePtr getIOService()
Fetches the channel's IOService.
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.
void operator()(boost::system::error_code ec, size_t length=0)
Operator called when event associated with a socket occurs.
SocketCallback(SocketCallbackFunction socket_callback)
Constructor.
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.
Definition icmp_msg.h:26
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< PingChannel > PingChannelPtr
Defines a smart pointer to PingChannel.
std::function< void(boost::system::error_code ec, size_t length)> SocketCallbackFunction
Type of the function implementing a callback invoked by the SocketCallback functor.
boost::shared_ptr< PingSocket > PingSocketPtr
Defines a pointer to PingSocket.
ICMPSocket< SocketCallback > PingSocket
Socket type for performing ICMP socket IO.
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Defines the logger used by the top-level component of kea-lfc.
Defines the class, WatchSocket.