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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright (C) 2013-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <asiolink/asio_wrapper.h>
#include <dhcp_ddns/dhcp_ddns_log.h>
#include <dhcp_ddns/ncr_io.h>
#include <util/multi_threading_mgr.h>

#include <boost/algorithm/string/predicate.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <mutex><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace dhcp_ddns {

using namespace isc::util;
using namespace std;

NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str) {
    if (boost::iequals(protocol_str, "UDP")) {
        return (NCR_UDP);
    }

    if (boost::iequals(protocol_str, "TCP")) {
        return (NCR_TCP);
    }

    isc_throw(BadValue,
              "Invalid NameChangeRequest protocol: " << protocol_str);
}

std::string ncrProtocolToString(NameChangeProtocol protocol) {
    switch (protocol) {
    case NCR_UDP:
        return ("UDP");
    case NCR_TCP:
        return ("TCP");
    default:
        break;
    }

    std::ostringstream stream;
    stream  << "UNKNOWN(" << protocol << ")";
    return (stream.str());
}

//************************** NameChangeListener ***************************

NameChangeListener::NameChangeListener(RequestReceiveHandlerPtr recv_handler)
    : listening_(false), io_pending_(false), recv_handler_(recv_handler) {
};

void
NameChangeListener::startListening(const isc::asiolink::IOServicePtr& io_service) {
    if (amListening()) {
        // This amounts to a programmatic error.
        isc_throw(NcrListenerError, "NameChangeListener is already listening");
    }

    // Call implementation dependent open.
    try {
        open(io_service);
    } catch (const isc::Exception& ex) {
        stopListening();
        isc_throw(NcrListenerOpenError, "Open failed: " << ex.what());
    }

    // Set our status to listening.
    setListening(true);

    // Start the first asynchronous receive.
    try {
        receiveNext();
    } catch (const isc::Exception& ex) {
        stopListening();
        isc_throw(NcrListenerReceiveError, "doReceive failed: " << ex.what());
    }
}

void
NameChangeListener::receiveNext() {
    io_pending_ = true;
    doReceive();
}

void
NameChangeListener::stopListening() {
    try {
        // Call implementation dependent close.
        close();
    } catch (const isc::Exception &ex) {
        // Swallow exceptions. If we have some sort of error we'll log
        // it but we won't propagate the throw.
        LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_LISTEN_CLOSE_ERROR)
                  .arg(ex.what());
    }

    // Set it false, no matter what.  This allows us to at least try to
    // re-open via startListening().
    setListening(false);
}

void
NameChangeListener::invokeRecvHandler(const Result result,
                                      NameChangeRequestPtr& ncr) {
    // Call the registered application layer handler.
    // Surround the invocation with a try-catch. The invoked handler is
    // not supposed to throw, but in the event it does we will at least
    // report it.
    try {
        io_pending_ = false;
        (*recv_handler_)(result, ncr);
    } catch (const std::exception& ex) {
        LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_UNCAUGHT_NCR_RECV_HANDLER_ERROR)
                  .arg(ex.what());
    }

    // Start the next IO layer asynchronous receive.
    // In the event the handler above intervened and decided to stop listening
    // we need to check that first.
    if (amListening()) {
        try {
            receiveNext();
        } catch (const isc::Exception& ex) {<--- Shadowed declaration
            // It is possible though unlikely, for doReceive to fail without
            // scheduling the read. While, unlikely, it does mean the callback
            // will not get called with a failure. A throw here would surface
            // at the IOService::run (or run variant) invocation.  So we will
            // close the window by invoking the application handler with
            // a failed result, and let the application layer sort it out.
            LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_RECV_NEXT_ERROR)
                      .arg(ex.what());

            // Call the registered application layer handler.
            // Surround the invocation with a try-catch. The invoked handler is
            // not supposed to throw, but in the event it does we will at least
            // report it.
            NameChangeRequestPtr empty;
            try {
                io_pending_ = false;
                (*recv_handler_)(ERROR, empty);
            } catch (const std::exception& ex) {<--- Shadow variable
                LOG_ERROR(dhcp_ddns_logger,
                          DHCP_DDNS_UNCAUGHT_NCR_RECV_HANDLER_ERROR)
                          .arg(ex.what());
            }
        }
    }
}

//************************* NameChangeSender ******************************

NameChangeSender::NameChangeSender(RequestSendHandlerPtr send_handler,
                                   size_t send_queue_max)
    : sending_(false), send_handler_(send_handler),
      send_queue_max_(send_queue_max), mutex_(new mutex()) {

    // Queue size must be big enough to hold at least 1 entry.
    setQueueMaxSize(send_queue_max);
}

void
NameChangeSender::startSending(const isc::asiolink::IOServicePtr& io_service) {
    if (amSending()) {
        // This amounts to a programmatic error.
        isc_throw(NcrSenderError, "NameChangeSender is already sending");
    }

    // Call implementation dependent open.
    try {
        if (MultiThreadingMgr::instance().getMode()) {
            lock_guard<mutex> lock(*mutex_);
            startSendingInternal(io_service);
        } else {
            startSendingInternal(io_service);
        }
    } catch (const isc::Exception& ex) {
        stopSending();
        isc_throw(NcrSenderOpenError, "Open failed: " << ex.what());
    }
}

void
NameChangeSender::startSendingInternal(const isc::asiolink::IOServicePtr& io_service) {
    // Clear send marker.
    ncr_to_send_.reset();

    // Remember io service we're given.
    io_service_ = io_service;
    open(io_service);

    // Set our status to sending.
    setSending(true);

    // If there's any queued already.. we'll start sending.
    sendNext();
}

void
NameChangeSender::stopSending() {
    // Set it send indicator to false, no matter what. This allows us to at
    // least try to re-open via startSending(). Also, setting it false now,
    // allows us to break sendNext() chain in invokeSendHandler.
    setSending(false);

    // If there is an outstanding IO to complete, attempt to process it.
    if (ioReady() && io_service_) {
        try {
            runReadyIO();
        } catch (const std::exception& ex) {
            // Swallow exceptions. If we have some sort of error we'll log
            // it but we won't propagate the throw.
            LOG_ERROR(dhcp_ddns_logger,
                  DHCP_DDNS_NCR_FLUSH_IO_ERROR).arg(ex.what());
        }
    }

    try {
        // Call implementation dependent close.
        close();
    } catch (const isc::Exception &ex) {
        // Swallow exceptions. If we have some sort of error we'll log
        // it but we won't propagate the throw.
        LOG_ERROR(dhcp_ddns_logger,
                  DHCP_DDNS_NCR_SEND_CLOSE_ERROR).arg(ex.what());
    }

    if (io_service_) {
        try {
            io_service_->stopAndPoll(false);
        } catch (const std::exception& ex) {
            // Swallow exceptions. If we have some sort of error we'll log
            // it but we won't propagate the throw.
            LOG_ERROR(dhcp_ddns_logger,
                      DHCP_DDNS_NCR_FLUSH_IO_ERROR).arg(ex.what());
        }
    }

    io_service_.reset();
}

void
NameChangeSender::sendRequest(NameChangeRequestPtr& ncr) {
    if (!amSending()) {
        isc_throw(NcrSenderError, "sender is not ready to send");
    }

    if (!ncr) {
        isc_throw(NcrSenderError, "request to send is empty");
    }

    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        sendRequestInternal(ncr);
    } else {
        sendRequestInternal(ncr);
    }
}

void
NameChangeSender::sendRequestInternal(NameChangeRequestPtr& ncr) {
    if (send_queue_.size() >= send_queue_max_) {
        isc_throw(NcrSenderQueueFull,
                  "send queue has reached maximum capacity: "
                  << send_queue_max_);
    }

    // Put it on the queue.
    send_queue_.push_back(ncr);

    // Call sendNext to schedule the next one to go.
    sendNext();
}

void
NameChangeSender::sendNext() {
    if (ncr_to_send_) {
        // @todo Not sure if there is any risk of getting stuck here but
        // an interval timer to defend would be good.
        // In reality, the derivation should ensure they timeout themselves
        return;
    }

    // If queue isn't empty, then get one from the front. Note we leave
    // it on the front of the queue until we successfully send it.
    if (!send_queue_.empty()) {
        ncr_to_send_ = send_queue_.front();

       // @todo start defense timer
       // If a send were to hang and we timed it out, then timeout
       // handler need to cycle thru open/close ?

       // Call implementation dependent send.
       doSend(ncr_to_send_);
    }
}

void
NameChangeSender::invokeSendHandler(const NameChangeSender::Result result) {
    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        invokeSendHandlerInternal(result);
    } else {
        invokeSendHandlerInternal(result);
    }
}

void
NameChangeSender::invokeSendHandlerInternal(const NameChangeSender::Result result) {
    // @todo reset defense timer
    if (result == SUCCESS) {
        // It shipped so pull it off the queue.
        send_queue_.pop_front();
    }

    // Invoke the completion handler passing in the result and a pointer
    // the request involved.
    // Surround the invocation with a try-catch. The invoked handler is
    // not supposed to throw, but in the event it does we will at least
    // report it.
    try {
        (*send_handler_)(result, ncr_to_send_);
    } catch (const std::exception& ex) {
        LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_UNCAUGHT_NCR_SEND_HANDLER_ERROR)
                  .arg(ex.what());
    }

    // Clear the pending ncr pointer.
    ncr_to_send_.reset();

    // Set up the next send
    try {
        if (amSending()) {
            sendNext();
        }
    } catch (const isc::Exception& ex) {<--- Shadowed declaration
        // It is possible though unlikely, for sendNext to fail without
        // scheduling the send. While, unlikely, it does mean the callback
        // will not get called with a failure. A throw here would surface
        // at the IOService::run (or run variant) invocation.  So we will
        // close the window by invoking the application handler with
        // a failed result, and let the application layer sort it out.
        LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_SEND_NEXT_ERROR)
                  .arg(ex.what());

        // Invoke the completion handler passing in failed result.
        // Surround the invocation with a try-catch. The invoked handler is
        // not supposed to throw, but in the event it does we will at least
        // report it.
        try {
            (*send_handler_)(ERROR, ncr_to_send_);
        } catch (const std::exception& ex) {<--- Shadow variable
            LOG_ERROR(dhcp_ddns_logger,
                      DHCP_DDNS_UNCAUGHT_NCR_SEND_HANDLER_ERROR).arg(ex.what());
        }
    }
}

void
NameChangeSender::skipNext() {
    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        skipNextInternal();
    } else {
        skipNextInternal();
    }
}

void
NameChangeSender::skipNextInternal() {
    if (!send_queue_.empty()) {
        // Discards the request at the front of the queue.
        send_queue_.pop_front();
    }
}

void
NameChangeSender::clearSendQueue() {
    if (amSending()) {
        isc_throw(NcrSenderError, "Cannot clear queue while sending");
    }

    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        send_queue_.clear();
    } else {
        send_queue_.clear();
    }
}

void
NameChangeSender::setQueueMaxSize(const size_t new_max) {
    if (new_max == 0) {
        isc_throw(NcrSenderError, "NameChangeSender:"
                  " queue size must be greater than zero");
    }

    send_queue_max_ = new_max;
}

size_t
NameChangeSender::getQueueSize() const {
    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        return (getQueueSizeInternal());
    } else {
        return (getQueueSizeInternal());
    }
}

size_t
NameChangeSender::getQueueSizeInternal() const {
    return (send_queue_.size());
}

const NameChangeRequestPtr&
NameChangeSender::peekAt(const size_t index) const {
    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        return (peekAtInternal(index));
    } else {
        return (peekAtInternal(index));
    }
}

const NameChangeRequestPtr&
NameChangeSender::peekAtInternal(const size_t index) const {
    auto size = getQueueSizeInternal();
    if (index >= size) {
        isc_throw(NcrSenderError,
                  "NameChangeSender::peekAt peek beyond end of queue attempted"
                  << " index: " << index << " queue size: " << size);
    }

    return (send_queue_.at(index));
}

bool
NameChangeSender::isSendInProgress() const {
    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        return ((ncr_to_send_) ? true : false);
    } else {
        return ((ncr_to_send_) ? true : false);
    }
}

void
NameChangeSender::assumeQueue(NameChangeSender& source_sender) {
    if (source_sender.amSending()) {
        isc_throw(NcrSenderError, "Cannot assume queue:"
                  " source sender is actively sending");
    }

    if (amSending()) {
        isc_throw(NcrSenderError, "Cannot assume queue:"
                  " target sender is actively sending");
    }

    if (getQueueMaxSize() < source_sender.getQueueSize()) {
        isc_throw(NcrSenderError, "Cannot assume queue:"
                  " source queue count exceeds target queue max");
    }

    if (MultiThreadingMgr::instance().getMode()) {
        lock_guard<mutex> lock(*mutex_);
        assumeQueueInternal(source_sender);
    } else {
        assumeQueueInternal(source_sender);
    }
}

void
NameChangeSender::assumeQueueInternal(NameChangeSender& source_sender) {
    if (!send_queue_.empty()) {
        isc_throw(NcrSenderError, "Cannot assume queue:"
                  " target queue is not empty");
    }

    send_queue_.swap(source_sender.getSendQueue());
}

int
NameChangeSender::getSelectFd() {
    isc_throw(NotImplemented, "NameChangeSender::getSelectFd is not supported");
}

void
NameChangeSender::runReadyIO() {
    if (!io_service_) {
        isc_throw(NcrSenderError, "NameChangeSender::runReadyIO"
                  " sender io service is null");
    }

    // We shouldn't be here if IO isn't ready to execute.
    // By running poll we're guaranteed not to hang.
    io_service_->pollOne();
}

}  // namespace dhcp_ddns
}  // namespace isc