Kea 3.1.1
ping_context.cc
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#include <config.h>
8
9#include <ping_context.h>
10#include <ping_check_log.h>
13#include <iostream>
14
15using namespace std;
16using namespace isc;
17using namespace isc::asiolink;
18using namespace isc::dhcp;
19using namespace isc::hooks;
20using namespace std::chrono;
21
22namespace isc {
23namespace ping_check {
24
26 uint32_t min_echos /* = 1 */,
27 uint32_t reply_timeout /* = 100 */,
28 ParkingLotHandlePtr& parking_lot /* = EMPTY_LOT() */)
29 : min_echos_(min_echos),
30 reply_timeout_(reply_timeout),
31 echos_sent_(0),
32 last_echo_sent_time_(EMPTY_TIME()),
33 send_wait_start_(EMPTY_TIME()),
34 next_expiry_(EMPTY_TIME()),
35 created_time_(PingContext::now()),
36 lease_(lease),
37 query_(query),
38 state_(NEW),
39 parking_lot_(parking_lot) {
40 if (!lease_) {
41 isc_throw(BadValue, "PingContext ctor - lease cannot be empty");
42 }
43
44 if (!query_) {
45 isc_throw(BadValue, "PingContext ctor - query cannot be empty");
46 }
47
49 isc_throw(BadValue, "PingContext ctor - target address cannot be 0.0.0.0");
50 }
51
52 if (min_echos_ == 0) {
53 isc_throw(BadValue, "PingContext ctor - min_echos must be greater than 0");
54 }
55
56 if (reply_timeout_ == 0) {
57 isc_throw(BadValue, "PingContext ctor - reply_timeout must be greater than 0");
58 }
59}
60
62PingContext::stringToState(const std::string& state_str) {
63 if (state_str == "NEW") {
64 return (NEW);
65 }
66
67 if (state_str == "WAITING_TO_SEND") {
68 return (WAITING_TO_SEND);
69 }
70
71 if (state_str == "SENDING") {
72 return (SENDING);
73 }
74
75 if (state_str == "WAITING_FOR_REPLY") {
76 return (WAITING_FOR_REPLY);
77 }
78
79 if (state_str == "TARGET_FREE") {
80 return (TARGET_FREE);
81 }
82
83 if (state_str == "TARGET_IN_USE") {
84 return (TARGET_IN_USE);
85 }
86
87 isc_throw(BadValue, "Invalid PingContext::State: '" << state_str << "'");
88}
89
92 return (time_point_cast<milliseconds>(std::chrono::system_clock::now()));
93}
94
95std::string
97 std::string label = "";
98 switch (state) {
99 case NEW:
100 label = "NEW";
101 break;
102 case WAITING_TO_SEND:
103 label = "WAITING_TO_SEND";
104 break;
105 case SENDING:
106 label = "SENDING";
107 break;
109 label = "WAITING_FOR_REPLY";
110 break;
111 case TARGET_FREE:
112 label = "TARGET_FREE";
113 break;
114 case TARGET_IN_USE:
115 label = "TARGET_IN_USE";
116 break;
117 }
118
119 return (label);
120}
121
123 return (lease_->addr_);
124}
125
126uint32_t
128 return (min_echos_);
129}
130
131void
133 min_echos_ = value;
134}
135
136uint32_t
138 return (reply_timeout_);
139}
140
141void
143 reply_timeout_ = value;
144}
145
146uint32_t
148 return (echos_sent_);
149}
150
151void
153 echos_sent_ = value;
154}
155
156const TimeStamp&
158 return (last_echo_sent_time_);
159}
160
161void
163 last_echo_sent_time_ = value;
164}
165
166const TimeStamp&
168 return (send_wait_start_);
169}
170
171bool
173 return (state_ == WAITING_TO_SEND);
174}
175
176void
178 send_wait_start_ = value;
179}
180
181const TimeStamp&
183 return (next_expiry_);
184}
185
186bool
188 return (state_ == WAITING_FOR_REPLY);
189}
190
191void
193 next_expiry_ = value;
194}
195
196const TimeStamp&
198 return (created_time_);
199}
200
203 return (state_);
204}
205
206void
208 state_ = value;
209}
210
213 return (query_);
214}
215
218 return (lease_);
219}
220
221void
222PingContext::beginWaitingToSend(const TimeStamp& begin_time /* = now() */) {
223 state_ = WAITING_TO_SEND;
224 send_wait_start_ = begin_time;
225}
226
227void
228PingContext::beginWaitingForReply(const TimeStamp& begin_time /* = now() */) {
229 ++echos_sent_;
230 last_echo_sent_time_ = begin_time;
231 next_expiry_ = begin_time + milliseconds(reply_timeout_);
232 state_ = WAITING_FOR_REPLY;
233}
234
235} // end of namespace ping_check
236} // end of namespace isc
237
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.
State
Defines PingContext life cycle states.
const TimeStamp & getSendWaitStart() const
Fetches the time the context went into WAITING_TO_SEND state.
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: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.
Defines the logger used by the top-level component of kea-lfc.