Kea 3.1.1
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
20using TimeStamp = std::chrono::time_point<std::chrono::system_clock>;
21
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
74 uint32_t min_echos = 1, uint32_t reply_timeout = 100,
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
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
211
216
221 void beginWaitingToSend(const TimeStamp& begin_time = PingContext::now());
222
227 void beginWaitingForReply(const TimeStamp& begin_time = PingContext::now());
228
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
262
264 isc::dhcp::Pkt4Ptr query_;
265
267 State state_;
268
272};
273
275typedef boost::shared_ptr<PingContext> PingContextPtr;
276
277} // end of namespace ping_check
278} // end of namespace isc
279
280#endif
const TimeStamp & getNextExpiry() const
Fetches the time at which the WAITING_FOR_REPLY state expires(ed)
const TimeStamp & getLastEchoSentTime() const
Fetches the timestamp of when the most recent ECHO REQUEST was sent.
State getState() const
Fetches the current state.
void setEchosSent(uint32_t value)
Sets the number of ECHO REQUESTs sent.
void setMinEchos(uint32_t value)
Sets the minimum number of ECHO REQUESTs.
isc::dhcp::Lease4Ptr getLease() const
Returns the candidate lease whose address is the target to check.
void setState(const State &value)
Sets the state.
bool isWaitingForReply() const
Returns true if state is WAITING_FOR_REPLY.
static const TimeStamp & MIN_TIME()
Fetches the minimum timestamp.
uint32_t getReplyTimeout() const
Fetches the reply timeout (milliseconds)
uint32_t getMinEchos() const
Fetches the minimum number of ECHO REQUESTs.
void setSendWaitStart(const TimeStamp &value)
Sets the send wait start timestamp.
isc::dhcp::Pkt4Ptr getQuery() const
Returns the query that instigated this check.
const TimeStamp & getCreatedTime() const
Fetches the time at which the context was created.
void beginWaitingToSend(const TimeStamp &begin_time=PingContext::now())
Enters WAITING_TO_SEND state.
bool isWaitingToSend() const
Returns true if state is WAITING_TO_SEND.
static const TimeStamp & EMPTY_TIME()
Fetches an empty timestamp.
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.
static TimeStamp now()
Fetches the current timestamp (UTC/milliseconds precision)
isc::hooks::ParkingLotHandlePtr getParkingLot()
Fetches the parking lot used for this context.
void setNextExpiry(const TimeStamp &value)
Sets the timestamp which specifies the time at which the WAITING_FOR_REPLY state expires.
uint32_t getEchosSent() const
Fetches the number of ECHO REQUESTs sent.
State
Defines PingContext life cycle states.
const TimeStamp & getSendWaitStart() const
Fetches the time the context went into WAITING_TO_SEND state.
virtual ~PingContext()=default
Destructor.
static std::string stateToString(const State &state)
Converts a State to a string.
static hooks::ParkingLotHandlePtr & EMPTY_LOT()
Fetches an empty parking lot handle.
void setLastEchoSentTime(const TimeStamp &value)
Sets the timestamp the most recent ECHO REQUEST was sent.
void beginWaitingForReply(const TimeStamp &begin_time=PingContext::now())
Enters WAITING_TO_REPLY state.
const isc::asiolink::IOAddress & getTarget() const
Fetches the IP address that is under test.
void setReplyTimeout(uint32_t value)
Sets the reply timeout.
static State stringToState(const std::string &state_str)
Converts a string to State.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
std::chrono::time_point< std::chrono::system_clock > TimeStamp
Specifies the type for time stamps.
boost::shared_ptr< PingContext > PingContextPtr
Defines a shared pointer to a PingContext.
Defines the logger used by the top-level component of kea-lfc.