Kea 3.3.3
ping_context.cc
Go to the documentation of this file.
1// Copyright (C) 2023-2026 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#include <config.h>
8
12
13#include <iostream>
14
15#include <ping_check_log.h>
16
17using namespace std;
18using namespace isc;
19using namespace isc::asiolink;
20using namespace isc::dhcp;
21using namespace isc::hooks;
22using namespace std::chrono;
23
24namespace isc {
25namespace ping_check {
26
28 uint32_t min_echos /* = 1 */,
29 uint32_t reply_timeout /* = 100 */,
30 ParkingLotHandlePtr& parking_lot /* = EMPTY_LOT() */)
31 : min_echos_(min_echos),
32 reply_timeout_(reply_timeout),
33 echos_sent_(0),
34 last_echo_sent_time_(EMPTY_TIME()),
35 send_wait_start_(EMPTY_TIME()),
36 next_expiry_(EMPTY_TIME()),
37 created_time_(PingContext::now()),
38 lease_(lease),
39 query_(query),
40 state_(NEW),
41 parking_lot_(parking_lot) {
42 if (!lease_) {
43 isc_throw(BadValue, "PingContext ctor - lease cannot be empty");
44 }
45
46 if (!query_) {
47 isc_throw(BadValue, "PingContext ctor - query cannot be empty");
48 }
49
51 isc_throw(BadValue, "PingContext ctor - target address cannot be 0.0.0.0");
52 }
53
54 if (min_echos_ == 0) {
55 isc_throw(BadValue, "PingContext ctor - min_echos must be greater than 0");
56 }
57
58 if (reply_timeout_ == 0) {
59 isc_throw(BadValue, "PingContext ctor - reply_timeout must be greater than 0");
60 }
61}
62
64PingContext::stringToState(const std::string& state_str) {
65 if (state_str == "NEW") {
66 return (NEW);
67 }
68
69 if (state_str == "WAITING_TO_SEND") {
70 return (WAITING_TO_SEND);
71 }
72
73 if (state_str == "SENDING") {
74 return (SENDING);
75 }
76
77 if (state_str == "WAITING_FOR_REPLY") {
78 return (WAITING_FOR_REPLY);
79 }
80
81 if (state_str == "TARGET_FREE") {
82 return (TARGET_FREE);
83 }
84
85 if (state_str == "TARGET_IN_USE") {
86 return (TARGET_IN_USE);
87 }
88
89 isc_throw(BadValue, "Invalid PingContext::State: '" << state_str << "'");
90}
91
94 return (time_point_cast<milliseconds>(std::chrono::system_clock::now()));
95}
96
97std::string
99 std::string label = "";
100 switch (state) {
101 case NEW:
102 label = "NEW";
103 break;
104 case WAITING_TO_SEND:
105 label = "WAITING_TO_SEND";
106 break;
107 case SENDING:
108 label = "SENDING";
109 break;
111 label = "WAITING_FOR_REPLY";
112 break;
113 case TARGET_FREE:
114 label = "TARGET_FREE";
115 break;
116 case TARGET_IN_USE:
117 label = "TARGET_IN_USE";
118 break;
119 }
120
121 return (label);
122}
123
125 return (lease_->addr_);
126}
127
128uint32_t
130 return (min_echos_);
131}
132
133void
135 min_echos_ = value;
136}
137
138uint32_t
140 return (reply_timeout_);
141}
142
143void
145 reply_timeout_ = value;
146}
147
148uint32_t
150 return (echos_sent_);
151}
152
153void
155 echos_sent_ = value;
156}
157
158const TimeStamp&
160 return (last_echo_sent_time_);
161}
162
163void
165 last_echo_sent_time_ = value;
166}
167
168const TimeStamp&
170 return (send_wait_start_);
171}
172
173bool
175 return (state_ == WAITING_TO_SEND);
176}
177
178void
180 send_wait_start_ = value;
181}
182
183const TimeStamp&
185 return (next_expiry_);
186}
187
188bool
190 return (state_ == WAITING_FOR_REPLY);
191}
192
193void
195 next_expiry_ = value;
196}
197
198const TimeStamp&
200 return (created_time_);
201}
202
205 return (state_);
206}
207
208void
210 state_ = value;
211}
212
215 return (query_);
216}
217
220 return (lease_);
221}
222
223void
224PingContext::beginWaitingToSend(const TimeStamp& begin_time /* = now() */) {
225 state_ = WAITING_TO_SEND;
226 send_wait_start_ = begin_time;
227}
228
229void
230PingContext::beginWaitingForReply(const TimeStamp& begin_time /* = now() */) {
231 ++echos_sent_;
232 last_echo_sent_time_ = begin_time;
233 next_expiry_ = begin_time + milliseconds(reply_timeout_);
234 state_ = WAITING_FOR_REPLY;
235}
236
237} // end of namespace ping_check
238} // end of namespace isc
239
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
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.
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).
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.
const TimeStamp & getSendWaitStart() const
Fetches the time the context went into WAITING_TO_SEND state.
State
Defines PingContext life cycle states.
static std::string stateToString(const State &state)
Converts a State to a string.
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.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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:343
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.
Defines the logger used by the top-level component of kea-lfc.