1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (C) 2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <ping_context.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ping_check_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <exceptions/exceptions.h>
#include <util/chrono_time_utils.h>
#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::hooks;
using namespace std::chrono;

namespace isc {
namespace ping_check {

PingContext::PingContext(Lease4Ptr& lease, Pkt4Ptr& query,
                         uint32_t min_echos /* = 1 */,
                         uint32_t reply_timeout /* = 100 */,
                         ParkingLotHandlePtr& parking_lot /* = EMPTY_LOT() */)
    : min_echos_(min_echos),
      reply_timeout_(reply_timeout),
      echos_sent_(0),
      last_echo_sent_time_(EMPTY_TIME()),
      send_wait_start_(EMPTY_TIME()),
      next_expiry_(EMPTY_TIME()),
      created_time_(PingContext::now()),
      lease_(lease),
      query_(query),
      state_(NEW),
      parking_lot_(parking_lot) {
    if (!lease_) {
        isc_throw(BadValue, "PingContext ctor - lease cannot be empty");
    }

    if (!query_) {
        isc_throw(BadValue, "PingContext ctor - query cannot be empty");
    }

    if (getTarget() == IOAddress::IPV4_ZERO_ADDRESS()) {
        isc_throw(BadValue, "PingContext ctor - target address cannot be 0.0.0.0");
    }

    if (min_echos_ == 0) {
        isc_throw(BadValue, "PingContext ctor - min_echos must be greater than 0");
    }

    if (reply_timeout_ == 0) {
        isc_throw(BadValue, "PingContext ctor - reply_timeout must be greater than 0");
    }
}

PingContext::State
PingContext::stringToState(const std::string& state_str) {
    if (state_str == "NEW") {
        return (NEW);
    }

    if (state_str == "WAITING_TO_SEND") {
        return (WAITING_TO_SEND);
    }

    if (state_str == "SENDING") {
        return (SENDING);
    }

    if (state_str == "WAITING_FOR_REPLY") {
        return (WAITING_FOR_REPLY);
    }

    if (state_str == "TARGET_FREE") {
        return (TARGET_FREE);
    }

    if (state_str == "TARGET_IN_USE") {
        return (TARGET_IN_USE);
    }

    isc_throw(BadValue, "Invalid PingContext::State: '" << state_str << "'");
}

TimeStamp
PingContext::now() {
    return (time_point_cast<milliseconds>(std::chrono::system_clock::now()));
}

std::string
PingContext::stateToString(const PingContext::State& state) {
    std::string label = "";
    switch (state) {
    case NEW:
        label = "NEW";
        break;
    case WAITING_TO_SEND:
        label = "WAITING_TO_SEND";
        break;
    case SENDING:
        label = "SENDING";
        break;
    case WAITING_FOR_REPLY:
        label = "WAITING_FOR_REPLY";
        break;
    case TARGET_FREE:
        label = "TARGET_FREE";
        break;
    case TARGET_IN_USE:
        label = "TARGET_IN_USE";
        break;
    }

    return (label);
}

const IOAddress& PingContext::getTarget() const {
    return (lease_->addr_);
}

uint32_t
PingContext::getMinEchos() const {
    return (min_echos_);
}

void
PingContext::setMinEchos(uint32_t value) {
    min_echos_ = value;
}

uint32_t
PingContext::getReplyTimeout() const {
    return (reply_timeout_);
}

void
PingContext::setReplyTimeout(uint32_t value) {
    reply_timeout_ = value;
}

uint32_t
PingContext::getEchosSent() const {
    return (echos_sent_);
}

void
PingContext::setEchosSent(uint32_t value) {
    echos_sent_ = value;
}

const TimeStamp&
PingContext::getLastEchoSentTime() const {
    return (last_echo_sent_time_);
}

void
PingContext::setLastEchoSentTime(const TimeStamp& value) {
    last_echo_sent_time_ = value;
}

const TimeStamp&
PingContext::getSendWaitStart() const {
    return (send_wait_start_);
}

bool
PingContext::isWaitingToSend() const {
    return (state_ == WAITING_TO_SEND);
}

void
PingContext::setSendWaitStart(const TimeStamp& value) {
    send_wait_start_ = value;
}

const TimeStamp&
PingContext::getNextExpiry() const {
    return (next_expiry_);
}

bool
PingContext::isWaitingForReply() const {
    return (state_ == WAITING_FOR_REPLY);
}

void
PingContext::setNextExpiry(const TimeStamp& value) {
    next_expiry_ = value;
}

const TimeStamp&
PingContext::getCreatedTime() const {
    return (created_time_);
}

PingContext::State
PingContext::getState() const {
    return (state_);
}

void
PingContext::setState(const PingContext::State& value) {
    state_ = value;
}

Pkt4Ptr
PingContext::getQuery() const {
    return (query_);
}

Lease4Ptr
PingContext::getLease() const {
    return (lease_);
}

void
PingContext::beginWaitingToSend(const TimeStamp& begin_time /* = now() */) {
    state_ = WAITING_TO_SEND;
    send_wait_start_ = begin_time;
}

void
PingContext::beginWaitingForReply(const TimeStamp& begin_time /* = now() */) {
    ++echos_sent_;
    last_echo_sent_time_ = begin_time;
    next_expiry_ = begin_time + milliseconds(reply_timeout_);
    state_ = WAITING_FOR_REPLY;
}

} // end of namespace ping_check
} // end of namespace isc