Kea: ping_context.h Source File

| Kea 3.2.0-git

ping_context.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_CONTEXT_H
8#define PING_CONTEXT_H
9
10#include <dhcp/pkt4.h>
11#include <dhcpsrv/lease.h>
12#include <hooks/parking_lots.h>
13
14#include <chrono>
15
16namespace isc {
17namespace ping_check {
18
20 using TimeStamp = std::chrono::time_point<std::chrono::system_clock>;
21
31 class PingContext {
32public:
33
35 enum State {
36 NEW, // Newly created
37 WAITING_TO_SEND, // Waiting to send next ECHO REQUEST
38 SENDING, // Next ECHO REQUEST is being sent
39 WAITING_FOR_REPLY, // ECHO REQUEST sent, Waiting for reply or timeout
40 TARGET_FREE, // Target has been deemed free to offer.
41 TARGET_IN_USE // Target has been deemed in-use, do not offer
42 };
43
50 static State stringToState(const std::string& state_str);
51
56 static std::string stateToString(const State& state);
57
73 PingContext(isc::dhcp::Lease4Ptr& lease, isc::dhcp::Pkt4Ptr& query,
74 uint32_t min_echos = 1, uint32_t reply_timeout = 100,
75 isc::hooks::ParkingLotHandlePtr& parking_lot = EMPTY_LOT());
76
78 virtual ~PingContext() = default;
79
83 static TimeStamp now();
84
88 static const TimeStamp& EMPTY_TIME() {
89 static TimeStamp empty_time;
90 return (empty_time);
91 }
92
96 static const TimeStamp& MIN_TIME() {
97 static TimeStamp min_time = std::chrono::system_clock::time_point::min();
98 return (min_time);
99 }
100
104 static hooks::ParkingLotHandlePtr& EMPTY_LOT() {
105 static hooks::ParkingLotHandlePtr empty_lot(0);
106 return (empty_lot);
107 }
108
112 const isc::asiolink::IOAddress& getTarget() const;
113
117 uint32_t getMinEchos() const;
118
124 void setMinEchos(uint32_t value);
125
129 uint32_t getReplyTimeout() const;
130
136 void setReplyTimeout(uint32_t value);
137
141 uint32_t getEchosSent() const;
142
146 void setEchosSent(uint32_t value);
147
152 const TimeStamp& getLastEchoSentTime() const;
153
157 void setLastEchoSentTime(const TimeStamp& value);
158
164 const TimeStamp& getSendWaitStart() const;
165
169 void setSendWaitStart(const TimeStamp& value);
170
174 bool isWaitingToSend() const;
175
181 const TimeStamp& getNextExpiry() const;
182
185 void setNextExpiry(const TimeStamp& value);
186
190 bool isWaitingForReply() const;
191
195 const TimeStamp& getCreatedTime() const;
196
200 State getState() const;
201
205 void setState(const State& value);
206
210 isc::dhcp::Pkt4Ptr getQuery() const;
211
215 isc::dhcp::Lease4Ptr getLease() const;
216
221 void beginWaitingToSend(const TimeStamp& begin_time = PingContext::now());
222
227 void beginWaitingForReply(const TimeStamp& begin_time = PingContext::now());
228
233 isc::hooks::ParkingLotHandlePtr getParkingLot() {
234 return (parking_lot_);
235 };
236
237private:
240 uint32_t min_echos_ = 0;
241
243 uint32_t reply_timeout_ = 0;
244
246 uint32_t echos_sent_ = 0;
247
249 TimeStamp last_echo_sent_time_;
250
252 TimeStamp send_wait_start_;
253
255 TimeStamp next_expiry_;
256
258 TimeStamp created_time_;
259
261 isc::dhcp::Lease4Ptr lease_;
262
264 isc::dhcp::Pkt4Ptr query_;
265
267 State state_;
268
271 isc::hooks::ParkingLotHandlePtr parking_lot_;
272};
273
275 typedef boost::shared_ptr<PingContext> PingContextPtr;
276
277} // end of namespace ping_check
278} // end of namespace isc
279
280#endif
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::ping_check::PingContext::getNextExpiry
const TimeStamp & getNextExpiry() const
Fetches the time at which the WAITING_FOR_REPLY state expires(ed).
Definition ping_context.cc:182
isc::ping_check::PingContext::getLastEchoSentTime
const TimeStamp & getLastEchoSentTime() const
Fetches the timestamp of when the most recent ECHO REQUEST was sent.
Definition ping_context.cc:157
isc::ping_check::PingContext::getState
State getState() const
Fetches the current state.
Definition ping_context.cc:202
isc::ping_check::PingContext::setEchosSent
void setEchosSent(uint32_t value)
Sets the number of ECHO REQUESTs sent.
Definition ping_context.cc:152
isc::ping_check::PingContext::setMinEchos
void setMinEchos(uint32_t value)
Sets the minimum number of ECHO REQUESTs.
Definition ping_context.cc:132
isc::ping_check::PingContext::getLease
isc::dhcp::Lease4Ptr getLease() const
Returns the candidate lease whose address is the target to check.
Definition ping_context.cc:217
isc::ping_check::PingContext::setState
void setState(const State &value)
Sets the state.
Definition ping_context.cc:207
isc::ping_check::PingContext::isWaitingForReply
bool isWaitingForReply() const
Returns true if state is WAITING_FOR_REPLY.
Definition ping_context.cc:187
isc::ping_check::PingContext::MIN_TIME
static const TimeStamp & MIN_TIME()
Fetches the minimum timestamp.
Definition ping_context.h:96
isc::ping_check::PingContext::getReplyTimeout
uint32_t getReplyTimeout() const
Fetches the reply timeout (milliseconds).
Definition ping_context.cc:137
isc::ping_check::PingContext::getMinEchos
uint32_t getMinEchos() const
Fetches the minimum number of ECHO REQUESTs.
Definition ping_context.cc:127
isc::ping_check::PingContext::setSendWaitStart
void setSendWaitStart(const TimeStamp &value)
Sets the send wait start timestamp.
Definition ping_context.cc:177
isc::ping_check::PingContext::getQuery
isc::dhcp::Pkt4Ptr getQuery() const
Returns the query that instigated this check.
Definition ping_context.cc:212
isc::ping_check::PingContext::getCreatedTime
const TimeStamp & getCreatedTime() const
Fetches the time at which the context was created.
Definition ping_context.cc:197
isc::ping_check::PingContext::beginWaitingToSend
void beginWaitingToSend(const TimeStamp &begin_time=PingContext::now())
Enters WAITING_TO_SEND state.
Definition ping_context.cc:222
isc::ping_check::PingContext::isWaitingToSend
bool isWaitingToSend() const
Returns true if state is WAITING_TO_SEND.
Definition ping_context.cc:172
isc::ping_check::PingContext::EMPTY_TIME
static const TimeStamp & EMPTY_TIME()
Fetches an empty timestamp.
Definition ping_context.h:88
isc::ping_check::PingContext::PingContext
PingContext(isc::dhcp::Lease4Ptr &lease, isc::dhcp::Pkt4Ptr &query, uint32_t min_echos=1, uint32_t reply_timeout=100, isc::hooks::ParkingLotHandlePtr &parking_lot=EMPTY_LOT())
Constructor.
Definition ping_context.cc:25
isc::ping_check::PingContext::now
static TimeStamp now()
Fetches the current timestamp (UTC/milliseconds precision).
Definition ping_context.cc:91
isc::ping_check::PingContext::getParkingLot
isc::hooks::ParkingLotHandlePtr getParkingLot()
Fetches the parking lot used for this context.
Definition ping_context.h:233
isc::ping_check::PingContext::setNextExpiry
void setNextExpiry(const TimeStamp &value)
Sets the timestamp which specifies the time at which the WAITING_FOR_REPLY state expires.
Definition ping_context.cc:192
isc::ping_check::PingContext::getEchosSent
uint32_t getEchosSent() const
Fetches the number of ECHO REQUESTs sent.
Definition ping_context.cc:147
isc::ping_check::PingContext::State
State
Defines PingContext life cycle states.
Definition ping_context.h:35
isc::ping_check::PingContext::TARGET_IN_USE
@ TARGET_IN_USE
Definition ping_context.h:41
isc::ping_check::PingContext::NEW
@ NEW
Definition ping_context.h:36
isc::ping_check::PingContext::WAITING_FOR_REPLY
@ WAITING_FOR_REPLY
Definition ping_context.h:39
isc::ping_check::PingContext::SENDING
@ SENDING
Definition ping_context.h:38
isc::ping_check::PingContext::TARGET_FREE
@ TARGET_FREE
Definition ping_context.h:40
isc::ping_check::PingContext::WAITING_TO_SEND
@ WAITING_TO_SEND
Definition ping_context.h:37
isc::ping_check::PingContext::getSendWaitStart
const TimeStamp & getSendWaitStart() const
Fetches the time the context went into WAITING_TO_SEND state.
Definition ping_context.cc:167
isc::ping_check::PingContext::~PingContext
virtual ~PingContext()=default
Destructor.
isc::ping_check::PingContext::stateToString
static std::string stateToString(const State &state)
Converts a State to a string.
Definition ping_context.cc:96
isc::ping_check::PingContext::EMPTY_LOT
static hooks::ParkingLotHandlePtr & EMPTY_LOT()
Fetches an empty parking lot handle.
Definition ping_context.h:104
isc::ping_check::PingContext::setLastEchoSentTime
void setLastEchoSentTime(const TimeStamp &value)
Sets the timestamp the most recent ECHO REQUEST was sent.
Definition ping_context.cc:162
isc::ping_check::PingContext::beginWaitingForReply
void beginWaitingForReply(const TimeStamp &begin_time=PingContext::now())
Enters WAITING_TO_REPLY state.
Definition ping_context.cc:228
isc::ping_check::PingContext::getTarget
const isc::asiolink::IOAddress & getTarget() const
Fetches the IP address that is under test.
Definition ping_context.cc:122
isc::ping_check::PingContext::setReplyTimeout
void setReplyTimeout(uint32_t value)
Sets the reply timeout.
Definition ping_context.cc:142
isc::ping_check::PingContext::stringToState
static State stringToState(const std::string &state_str)
Converts a string to State.
Definition ping_context.cc:62
lease.h
isc::dhcp::Pkt4Ptr
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
isc::hooks::ParkingLotHandlePtr
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition parking_lots.h:381
isc::ping_check
Definition config_cache.cc:19
isc::ping_check::TimeStamp
std::chrono::time_point< std::chrono::system_clock > TimeStamp
Specifies the type for time stamps.
Definition ping_context.h:20
isc::ping_check::PingContextPtr
boost::shared_ptr< PingContext > PingContextPtr
Defines a shared pointer to a PingContext.
Definition ping_context.h:275
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
parking_lots.h
pkt4.h