Kea: ping_channel.h Source File

| Kea 3.0.2-git

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#include <ping_context.h>
17
18#include <boost/scoped_ptr.hpp>
19#include <boost/enable_shared_from_this.hpp>
20
21#include <iostream>
22#include <mutex>
23
24namespace isc {
25namespace ping_check {
26
29 typedef std::function<void(boost::system::error_code ec, size_t length)> SocketCallbackFunction;
30
34 class SocketCallback {
35public:
40 explicit inline SocketCallback(SocketCallbackFunction socket_callback)
41 : callback_(socket_callback) {
42 };
43
51 inline void operator()(boost::system::error_code ec, size_t length = 0) {
52 if (ec.value() == boost::asio::error::operation_aborted) {
53 return;
54 }
55
56 callback_(ec, length);
57 };
58
59private:
61 SocketCallbackFunction callback_;
62};
63
65 typedef ICMPSocket<SocketCallback> PingSocket;
66
68 typedef boost::shared_ptr<PingSocket> PingSocketPtr;
69
71 typedef std::function<PingContextPtr()> NextToSendCallback;
72
74 typedef std::function<void(PingContextPtr context)> UpdateToSendCallback;
75
77 typedef std::function<void(ICMPMsgPtr& echo, bool send_failed)> EchoSentCallback;
78
81 typedef std::function<void(ICMPMsgPtr& reply)> ReplyReceivedCallback;
82
84 typedef std::function<void()> ShutdownCallback;
85
107 class PingChannel : public boost::enable_shared_from_this<PingChannel> {
108public:
127 PingChannel(asiolink::IOServicePtr& io_service,
128 NextToSendCallback next_to_send_cb,
129 UpdateToSendCallback update_to_send_cb,
130 EchoSentCallback echo_sent_cb,
131 ReplyReceivedCallback reply_received_cb,
132 ShutdownCallback shutdown_cb = ShutdownCallback());
133
137 virtual ~PingChannel();
138
144 void open();
145
149 bool isOpen() const;
150
151 // @brief Schedules the next send.
152 //
153 // If the socket is not currently sending it posts a call to @c sendNext()
154 // to the channel's IOService.
155 virtual void startSend();
156
157 // @brief Schedules the next read.
158 //
159 // If the socket is not currently reading it posts a call to @c doRead()
160 // to the channel's IOService.
161 void startRead();
162
164 void close();
165
169 asiolink::IOServicePtr getIOService() {
170 return (io_service_);
171 }
172
173protected:
188 virtual void asyncReceive(void* data, size_t length, size_t offset,
189 asiolink::IOEndpoint* endpoint, SocketCallback& callback);
190
204 virtual void asyncSend(void* data, size_t length, asiolink::IOEndpoint* endpoint,
205 SocketCallback& callback);
206
207protected:
215 void doRead();
216
234 void socketReadCallback(boost::system::error_code ec, size_t length);
235
246 virtual void sendNext();
247
266 void socketWriteCallback(ICMPMsgPtr echo_sent, boost::system::error_code ec,
267 size_t length);
268
273 void stopChannel();
274
285 static uint32_t nextEchoInstanceNum();
286
293 bool canSend() {
294 return (socket_ && socket_->isOpen() && !stopping_ && !sending_);
295 }
296
303 bool canRead() {
304 return (socket_ && socket_->isOpen() && !stopping_ && !reading_);
305 }
306
312 size_t getInputBufSize() const;
313
320 unsigned char* getInputBufData();
321
323 asiolink::IOServicePtr io_service_;
324
326 NextToSendCallback next_to_send_cb_;
327
329 UpdateToSendCallback update_to_send_cb_;
330
332 EchoSentCallback echo_sent_cb_;
333
335 ReplyReceivedCallback reply_received_cb_;
336
338 ShutdownCallback shutdown_cb_;
339
341 PingSocketPtr socket_;
342
344 std::vector<uint8_t> input_buf_;
345
347 ICMPEndpoint reply_endpoint_;
348
350 bool reading_;
351
353 bool sending_;
354
356 bool stopping_;
357
359 const boost::scoped_ptr<std::mutex> mutex_;
360
369 const boost::scoped_ptr<std::mutex> send_mutex_;
370
373 bool single_threaded_;
374
376 util::WatchSocketPtr watch_socket_;
377
379 int registered_write_fd_;
380
382 int registered_read_fd_;
383};
384
386 typedef boost::shared_ptr<PingChannel> PingChannelPtr;
387
388} // end of namespace ping_check
389} // end of namespace isc
390
391#endif
asio_wrapper.h
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:382
isc::ping_check::PingChannel::getInputBufSize
size_t getInputBufSize() const
Returns input buffer size.
Definition ping_channel.cc:467
isc::ping_check::PingChannel::doRead
void doRead()
Initiates an asynchronous socket read.
Definition ping_channel.cc:194
isc::ping_check::PingChannel::~PingChannel
virtual ~PingChannel()
Destructor.
Definition ping_channel.cc:59
isc::ping_check::PingChannel::socket_
PingSocketPtr socket_
Socket through which to ping.
Definition ping_channel.h:341
isc::ping_check::PingChannel::stopChannel
void stopChannel()
Closes the socket channel and invokes the shutdown callback.
Definition ping_channel.cc:158
isc::ping_check::PingChannel::stopping_
bool stopping_
Indicates whether or not the channel has been told to stop.
Definition ping_channel.h:356
isc::ping_check::PingChannel::sendNext
virtual void sendNext()
Initiates sending the next ECHO REQUEST.
Definition ping_channel.cc:313
isc::ping_check::PingChannel::sending_
bool sending_
Indicates whether or not the socket has a write in progress.
Definition ping_channel.h:353
isc::ping_check::PingChannel::update_to_send_cb_
UpdateToSendCallback update_to_send_cb_
Callback to invoke to update selected context to SENDING state.
Definition ping_channel.h:329
isc::ping_check::PingChannel::isOpen
bool isOpen() const
Indicates whether or not the channel socket is open.
Definition ping_channel.cc:108
isc::ping_check::PingChannel::open
void open()
Opens the socket for communications.
Definition ping_channel.cc:64
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::send_mutex_
const boost::scoped_ptr< std::mutex > send_mutex_
The mutex used to protect internal state on send events.
Definition ping_channel.h:369
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:344
isc::ping_check::PingChannel::startRead
void startRead()
Definition ping_channel.cc:300
isc::ping_check::PingChannel::registered_write_fd_
int registered_write_fd_
WatchSocket fd registered with IfaceMgr.
Definition ping_channel.h:379
isc::ping_check::PingChannel::startSend
virtual void startSend()
Definition ping_channel.cc:287
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:377
isc::ping_check::PingChannel::reply_endpoint_
ICMPEndpoint reply_endpoint_
Retains the endpoint from which the most recent reply was received.
Definition ping_channel.h:347
isc::ping_check::PingChannel::echo_sent_cb_
EchoSentCallback echo_sent_cb_
Callback to invoke when an ECHO write has completed.
Definition ping_channel.h:332
isc::ping_check::PingChannel::getInputBufData
unsigned char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition ping_channel.cc:472
isc::ping_check::PingChannel::canRead
bool canRead()
Indicates whether or not a read can be initiated.
Definition ping_channel.h:303
isc::ping_check::PingChannel::io_service_
asiolink::IOServicePtr io_service_
IOService instance the drives socket IO.
Definition ping_channel.h:323
isc::ping_check::PingChannel::PingChannel
PingChannel(asiolink::IOServicePtr &io_service, NextToSendCallback next_to_send_cb, UpdateToSendCallback update_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::close
void close()
Closes the channel's socket.
Definition ping_channel.cc:114
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:335
isc::ping_check::PingChannel::shutdown_cb_
ShutdownCallback shutdown_cb_
Callback to invoke when the channel has shutdown.
Definition ping_channel.h:338
isc::ping_check::PingChannel::getIOService
asiolink::IOServicePtr getIOService()
Fetches the channel's IOService.
Definition ping_channel.h:169
isc::ping_check::PingChannel::watch_socket_
util::WatchSocketPtr watch_socket_
Pointer to WatchSocket instance supplying the "select-fd".
Definition ping_channel.h:376
isc::ping_check::PingChannel::reading_
bool reading_
Indicates whether or not the socket has a read in progress.
Definition ping_channel.h:350
isc::ping_check::PingChannel::next_to_send_cb_
NextToSendCallback next_to_send_cb_
Callback to invoke to fetch the next context with target address to ping.
Definition ping_channel.h:326
isc::ping_check::PingChannel::socketReadCallback
void socketReadCallback(boost::system::error_code ec, size_t length)
Socket read completion callback.
Definition ping_channel.cc:222
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:183
isc::ping_check::PingChannel::mutex_
const boost::scoped_ptr< std::mutex > mutex_
The mutex used to protect internal state.
Definition ping_channel.h:359
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:177
isc::ping_check::PingChannel::single_threaded_
bool single_threaded_
True if channel was opened in single-threaded mode, false otherwise.
Definition ping_channel.h:373
isc::ping_check::PingChannel::canSend
bool canSend()
Indicates whether or not a send can be initiated.
Definition ping_channel.h:293
isc::ping_check::SocketCallback
Functor associated with the socket object.
Definition ping_channel.h:34
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:51
isc::ping_check::SocketCallback::SocketCallback
SocketCallback(SocketCallbackFunction socket_callback)
Constructor.
Definition ping_channel.h:40
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:84
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:81
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:77
isc::ping_check::UpdateToSendCallback
std::function< void(PingContextPtr context)> UpdateToSendCallback
Function type for callback to update a context to SENDING state.
Definition ping_channel.h:74
isc::ping_check::PingContextPtr
boost::shared_ptr< PingContext > PingContextPtr
Defines a shared pointer to a PingContext.
Definition ping_context.h:275
isc::ping_check::PingChannelPtr
boost::shared_ptr< PingChannel > PingChannelPtr
Defines a smart pointer to PingChannel.
Definition ping_channel.h:386
isc::ping_check::NextToSendCallback
std::function< PingContextPtr()> NextToSendCallback
Function type for callback to fetch a context with next target to ping.
Definition ping_channel.h:71
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:29
isc::ping_check::PingSocketPtr
boost::shared_ptr< PingSocket > PingSocketPtr
Defines a pointer to PingSocket.
Definition ping_channel.h:68
isc::ping_check::PingSocket
ICMPSocket< SocketCallback > PingSocket
Socket type for performing ICMP socket IO.
Definition ping_channel.h:65
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
ping_context.h
watch_socket.h
Defines the class, WatchSocket.