Kea 3.1.3
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#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
29typedef std::function<void(boost::system::error_code ec, size_t length)> SocketCallbackFunction;
30
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
66
68typedef boost::shared_ptr<PingSocket> PingSocketPtr;
69
71typedef std::function<PingContextPtr()> NextToSendCallback;
72
74typedef std::function<void(PingContextPtr context)> UpdateToSendCallback;
75
77typedef std::function<void(ICMPMsgPtr& echo, bool send_failed)> EchoSentCallback;
78
81typedef std::function<void(ICMPMsgPtr& reply)> ReplyReceivedCallback;
82
84typedef std::function<void()> ShutdownCallback;
85
107class PingChannel : public boost::enable_shared_from_this<PingChannel> {
108public:
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
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
324
327
330
333
336
339
342
344 std::vector<uint8_t> input_buf_;
345
348
351
354
357
359 const boost::scoped_ptr<std::mutex> mutex_;
360
369 const boost::scoped_ptr<std::mutex> send_mutex_;
370
374
377
380
383};
384
386typedef boost::shared_ptr<PingChannel> PingChannelPtr;
387
388} // end of namespace ping_check
389} // end of namespace isc
390
391#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.
UpdateToSendCallback update_to_send_cb_
Callback to invoke to update selected context to SENDING state.
bool isOpen() const
Indicates whether or not the channel socket is open.
void open()
Opens the socket for communications.
static uint32_t nextEchoInstanceNum()
returns the next unique ECHO instance number.
const boost::scoped_ptr< std::mutex > send_mutex_
The mutex used to protect internal state on send events.
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.
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.
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 context with target 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< void(PingContextPtr context)> UpdateToSendCallback
Function type for callback to update a context to SENDING state.
boost::shared_ptr< PingContext > PingContextPtr
Defines a shared pointer to a PingContext.
boost::shared_ptr< PingChannel > PingChannelPtr
Defines a smart pointer to PingChannel.
std::function< PingContextPtr()> NextToSendCallback
Function type for callback to fetch a context with next target to ping.
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.