2022-03-28 01:56:00 -07:00
|
|
|
// Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
|
2018-09-14 09:37:47 +02:00
|
|
|
//
|
|
|
|
// 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/.
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <config.h>
|
2022-03-15 11:43:20 +02:00
|
|
|
|
2022-06-15 16:06:25 +02:00
|
|
|
#include <dhcpsrv/cfgmgr.h>
|
2018-09-14 09:37:47 +02:00
|
|
|
#include <hooks/hooks.h>
|
2022-06-15 16:06:25 +02:00
|
|
|
#include <process/daemon.h>
|
2022-03-15 11:43:20 +02:00
|
|
|
#include <mysql_cb_impl.h>
|
|
|
|
|
2018-10-10 14:51:59 -04:00
|
|
|
#include <mysql_cb_dhcp4.h>
|
2019-05-14 21:23:50 +02:00
|
|
|
#include <mysql_cb_dhcp6.h>
|
2022-03-15 11:43:20 +02:00
|
|
|
#include <mysql_cb_log.h>
|
2018-09-14 09:37:47 +02:00
|
|
|
|
2023-03-24 08:37:40 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2019-05-09 12:30:27 +03:00
|
|
|
using namespace isc::cb;
|
|
|
|
using namespace isc::dhcp;
|
2018-09-14 09:37:47 +02:00
|
|
|
using namespace isc::hooks;
|
2019-05-09 12:30:27 +03:00
|
|
|
using namespace isc::log;
|
2022-06-15 16:06:25 +02:00
|
|
|
using namespace isc::process;
|
2023-03-24 08:37:40 +02:00
|
|
|
using namespace std;
|
2018-09-14 09:37:47 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
/// @brief This function is called when the library is loaded.
|
|
|
|
///
|
|
|
|
/// @param handle library handle
|
|
|
|
/// @return 0 when initialization is successful, 1 otherwise
|
2018-10-10 14:51:59 -04:00
|
|
|
|
2018-09-27 17:04:09 +02:00
|
|
|
int load(LibraryHandle& /* handle */) {
|
2022-06-15 16:06:25 +02:00
|
|
|
// Make the hook library not loadable by d2 or ca.
|
|
|
|
uint16_t family = CfgMgr::instance().getFamily();
|
|
|
|
const std::string& proc_name = Daemon::getProcName();
|
|
|
|
if (family == AF_INET) {
|
|
|
|
if (proc_name != "kea-dhcp4") {
|
|
|
|
isc_throw(isc::Unexpected, "Bad process name: " << proc_name
|
|
|
|
<< ", expected kea-dhcp4");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (proc_name != "kea-dhcp6") {
|
|
|
|
isc_throw(isc::Unexpected, "Bad process name: " << proc_name
|
|
|
|
<< ", expected kea-dhcp6");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:30:27 +03:00
|
|
|
LOG_INFO(mysql_cb_logger, MYSQL_CB_INIT_OK);
|
2019-05-14 21:23:50 +02:00
|
|
|
// Register MySQL CB factories with CB Managers
|
2018-10-10 14:51:59 -04:00
|
|
|
isc::dhcp::MySqlConfigBackendDHCPv4::registerBackendType();
|
2019-05-14 21:23:50 +02:00
|
|
|
isc::dhcp::MySqlConfigBackendDHCPv6::registerBackendType();
|
2018-10-10 14:51:59 -04:00
|
|
|
|
2018-09-14 09:37:47 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2020-11-12 23:36:48 +02:00
|
|
|
/// @brief This function is called by the DHCPv4 server when it is configured.
|
|
|
|
///
|
|
|
|
/// The only purpose of this callout is to retrieve io_service_ reference.
|
|
|
|
///
|
|
|
|
/// @param handle callout handle passed to the callout.
|
|
|
|
/// @return 0 on success, 1 otherwise.
|
|
|
|
int dhcp4_srv_configured(CalloutHandle& handle) {
|
|
|
|
isc::asiolink::IOServicePtr io_service;
|
|
|
|
handle.getArgument("io_context", io_service);
|
2023-03-24 08:37:40 +02:00
|
|
|
if (!io_service) {
|
|
|
|
const string error("Error: io_context is null");
|
|
|
|
handle.setArgument("error", error);
|
|
|
|
handle.setStatus(isc::hooks::CalloutHandle::NEXT_STEP_DROP);
|
|
|
|
return (1);
|
|
|
|
}
|
2020-11-12 23:36:48 +02:00
|
|
|
isc::dhcp::MySqlConfigBackendImpl::setIOService(io_service);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief This function is called by the DHCPv6 server when it is configured.
|
|
|
|
///
|
|
|
|
/// The only purpose of this callout is to retrieve io_service_ reference.
|
|
|
|
///
|
|
|
|
/// @param handle callout handle passed to the callout.
|
|
|
|
/// @return 0 on success, 1 otherwise.
|
|
|
|
int dhcp6_srv_configured(CalloutHandle& handle) {
|
|
|
|
isc::asiolink::IOServicePtr io_service;
|
|
|
|
handle.getArgument("io_context", io_service);
|
2023-03-24 08:37:40 +02:00
|
|
|
if (!io_service) {
|
|
|
|
const string error("Error: io_context is null");
|
|
|
|
handle.setArgument("error", error);
|
|
|
|
handle.setStatus(isc::hooks::CalloutHandle::NEXT_STEP_DROP);
|
|
|
|
return (1);
|
|
|
|
}
|
2020-11-12 23:36:48 +02:00
|
|
|
isc::dhcp::MySqlConfigBackendImpl::setIOService(io_service);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2018-09-14 09:37:47 +02:00
|
|
|
/// @brief This function is called when the library is unloaded.
|
|
|
|
///
|
|
|
|
/// @return 0 if deregistration was successful, 1 otherwise
|
|
|
|
int unload() {
|
2019-05-09 12:30:27 +03:00
|
|
|
LOG_INFO(mysql_cb_logger, MYSQL_CB_DEINIT_OK);
|
2019-05-14 21:23:50 +02:00
|
|
|
// Unregister the factories and remove MySQL backends
|
2018-10-10 14:51:59 -04:00
|
|
|
isc::dhcp::MySqlConfigBackendDHCPv4::unregisterBackendType();
|
2019-05-14 21:23:50 +02:00
|
|
|
isc::dhcp::MySqlConfigBackendDHCPv6::unregisterBackendType();
|
2018-09-14 09:37:47 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2020-02-12 17:08:27 +02:00
|
|
|
/// @brief This function is called to retrieve the multi-threading compatibility.
|
2019-11-07 01:38:38 +01:00
|
|
|
///
|
|
|
|
/// @note: the compatibility is based on the assumption this hook library
|
|
|
|
/// is always called from the main thread.
|
|
|
|
///
|
|
|
|
/// @return 1 which means compatible with multi-threading.
|
|
|
|
int multi_threading_compatible() {
|
|
|
|
return (1);
|
|
|
|
}
|
2018-09-14 09:37:47 +02:00
|
|
|
|
|
|
|
} // end extern "C"
|