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
// Copyright (C) 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/.

/// @file
/// @brief Callout library for testing the scenarios when a hook library
/// posts some work to the IO service as a result of configuration.
/// Using a callout that always throws we can ensure that the server polls
/// this initial work before it starts working.
///
static const int LIBRARY_NUMBER = 4;

#include <config.h>

#include <asiolink/io_service.h>
#include <asiolink/io_service_mgr.h>
#include <dhcp4/tests/callout_library_common.h>
#include <dhcpsrv/srv_config.h>

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

using namespace isc::asiolink;
using namespace isc::hooks;

namespace {

// Start service method: always throw.
void start_service(void) {
    isc_throw(isc::Unexpected, "start service failed");
};

IOServicePtr io_service;

} // end anonymous

// Functions accessed by the hooks framework use C linkage to avoid the name
// mangling that accompanies use of the C++ compiler as well as to avoid
// issues related to namespaces.
extern "C" {

int
do_load_impl(LibraryHandle& handle) {
    io_service.reset(new IOService());
    // Determine if this callout is configured to fail.
    isc::dhcp::SrvConfigPtr config;
    isc::data::ConstElementPtr const& parameters(handle.getParameters());
    isc::data::ConstElementPtr mode_element(parameters ? parameters->get("mode") : 0);
    std::string mode(mode_element ? mode_element->stringValue() : "");
    if (mode == "fail-on-load") {
        return (1);
    }
    return (0);
}

int
do_unload_impl() {
    IOServiceMgr::instance().unregisterIOService(io_service);
    return (0);
}

int (*do_load)(LibraryHandle& handle) = do_load_impl;

int (*do_unload)() = do_unload_impl;

/// @brief Callout which appends library number and provided arguments to
/// the marker file for dhcp4_srv_configured callout.
///
/// @param handle callout handle passed to the callout.
///
/// @return 0 on success, 1 otherwise.
int
dhcp4_srv_configured(CalloutHandle& handle) {

    // Append library number.
    if (appendDigit(SRV_CONFIG_MARKER_FILE)) {
        return (1);
    }

    // Append argument names.
    std::vector<std::string> args = handle.getArgumentNames();
    for (auto const& arg : args) {
        if (appendArgument(SRV_CONFIG_MARKER_FILE, arg.c_str()) != 0) {<--- Consider using std::any_of algorithm instead of a raw loop.
            return (1);
        }
    }

    // Get the IO context to post start_service on it.
    std::string error("");
    try {
        IOServiceMgr::instance().registerIOService(io_service);
        io_service->post(start_service);
    } catch (const std::exception& ex) {
        error = "unknown error";
    }
    if (!error.empty()) {
        handle.setArgument("error", error);
        handle.setStatus(CalloutHandle::NEXT_STEP_DROP);
    }

    return (0);
}

/// @brief This function is called to retrieve the multi-threading compatibility.
///
/// @return 1 which means compatible with multi-threading.
int multi_threading_compatible() {
    return (1);
}

} // end extern "C"