| Kea 3.0.2-git

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>
11#include <exceptions/exceptions.h>
12#include <util/chrono_time_utils.h>
13#include <iostream>
14
15 using namespace std;
16 using namespace isc;
17 using namespace isc::asiolink;
18 using namespace isc::dhcp;
19 using namespace isc::hooks;
20 using namespace std::chrono;
21
22 namespace isc {
23 namespace ping_check {
24
25 PingContext::PingContext(Lease4Ptr& lease, Pkt4Ptr& query,
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
48 if (getTarget() == IOAddress::IPV4_ZERO_ADDRESS()) {
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
61 PingContext::State
62 PingContext::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
90 TimeStamp
91 PingContext::now() {
92 return (time_point_cast<milliseconds>(std::chrono::system_clock::now()));
93}
94
95std::string
96 PingContext::stateToString(const PingContext::State& state) {
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;
108 case WAITING_FOR_REPLY:
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
122 const IOAddress& PingContext::getTarget() const {
123 return (lease_->addr_);
124}
125
126uint32_t
127 PingContext::getMinEchos() const {
128 return (min_echos_);
129}
130
131 void
132 PingContext::setMinEchos(uint32_t value) {
133 min_echos_ = value;
134}
135
136uint32_t
137 PingContext::getReplyTimeout() const {
138 return (reply_timeout_);
139}
140
141 void
142 PingContext::setReplyTimeout(uint32_t value) {
143 reply_timeout_ = value;
144}
145
146uint32_t
147 PingContext::getEchosSent() const {
148 return (echos_sent_);
149}
150
151 void
152 PingContext::setEchosSent(uint32_t value) {
153 echos_sent_ = value;
154}
155
156 const TimeStamp&
157 PingContext::getLastEchoSentTime() const {
158 return (last_echo_sent_time_);
159}
160
161 void
162 PingContext::setLastEchoSentTime(const TimeStamp& value) {
163 last_echo_sent_time_ = value;
164}
165
166 const TimeStamp&
167 PingContext::getSendWaitStart() const {
168 return (send_wait_start_);
169}
170
171 bool
172 PingContext::isWaitingToSend() const {
173 return (state_ == WAITING_TO_SEND);
174}
175
176 void
177 PingContext::setSendWaitStart(const TimeStamp& value) {
178 send_wait_start_ = value;
179}
180
181 const TimeStamp&
182 PingContext::getNextExpiry() const {
183 return (next_expiry_);
184}
185
186 bool
187 PingContext::isWaitingForReply() const {
188 return (state_ == WAITING_FOR_REPLY);
189}
190
191 void
192 PingContext::setNextExpiry(const TimeStamp& value) {
193 next_expiry_ = value;
194}
195
196 const TimeStamp&
197 PingContext::getCreatedTime() const {
198 return (created_time_);
199}
200
201 PingContext::State
202 PingContext::getState() const {
203 return (state_);
204}
205
206 void
207 PingContext::setState(const PingContext::State& value) {
208 state_ = value;
209}
210
211 Pkt4Ptr
212 PingContext::getQuery() const {
213 return (query_);
214}
215
216 Lease4Ptr
217 PingContext::getLease() const {
218 return (lease_);
219}
220
221 void
222 PingContext::beginWaitingToSend(const TimeStamp& begin_time /* = now() */) {
223 state_ = WAITING_TO_SEND;
224 send_wait_start_ = begin_time;
225}
226
227 void
228 PingContext::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
chrono_time_utils.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:45
isc::asiolink::IOAddress::IPV4_ZERO_ADDRESS
static const IOAddress & IPV4_ZERO_ADDRESS()
Returns an address set to all zeros.
Definition io_address.h:277
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::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::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::stateToString
static std::string stateToString(const State &state)
Converts a State to a string.
Definition ping_context.cc:96
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
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::asiolink
Definition addr_utilities.cc:170
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::Pkt4Ptr
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:555
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
isc::hooks
Definition callout_handle.cc:21
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
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
ping_check_log.h
ping_context.h