2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-23 10:27:36 +00:00
kea/src/hooks/dhcp/mysql/mysql_callouts.cc

143 lines
5.0 KiB
C++
Raw Normal View History

2024-04-29 08:14:59 +00:00
// Copyright (C) 2018-2024 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
2024-04-19 17:53:32 +03:00
#include <asiolink/io_service_mgr.h>
2024-10-01 15:27:45 +03:00
#include <dhcpsrv/base_host_data_source.h>
#include <dhcpsrv/cfgmgr.h>
2024-10-01 15:27:45 +03:00
#include <dhcpsrv/lease_mgr_factory.h>
2018-09-14 09:37:47 +02:00
#include <hooks/hooks.h>
#include <process/daemon.h>
2022-03-15 11:43:20 +02:00
#include <mysql_cb_impl.h>
#include <mysql_cb_dhcp4.h>
#include <mysql_cb_dhcp6.h>
2022-03-15 11:43:20 +02:00
#include <mysql_cb_log.h>
2024-10-01 15:27:45 +03:00
#include <mysql_hb_log.h>
#include <mysql_host_data_source.h>
#include <mysql_lb_log.h>
#include <mysql_lease_mgr.h>
2018-09-14 09:37:47 +02:00
#include <sstream>
#include <string>
using namespace isc::asiolink;
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;
using namespace isc::process;
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
int load(LibraryHandle& /* handle */) {
// 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");
}
}
// Register MySQL CB factories with CB Managers
2024-10-02 19:00:27 +03:00
MySqlConfigBackendDHCPv4::registerBackendType();
MySqlConfigBackendDHCPv6::registerBackendType();
2024-10-01 15:27:45 +03:00
LOG_INFO(mysql_cb_logger, MYSQL_CB_INIT_OK);
2024-10-01 15:27:45 +03:00
// Register MySQL HB factories with Host Managers
HostDataSourceFactory::registerFactory("mysql",
MySqlHostDataSource::factory,
true,
MySqlHostDataSource::getDBVersion);
LOG_INFO(mysql_hb_logger, MYSQL_HB_INIT_OK);
// Register MySQL LB factories with Lease Managers
LeaseMgrFactory::registerFactory("mysql",
MySqlLeaseMgr::factory,
true,
MySqlLeaseMgr::getDBVersion);
LOG_INFO(mysql_lb_logger, MYSQL_LB_INIT_OK);
2018-09-14 09:37:47 +02:00
return (0);
}
/// @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 */) {
2024-10-02 19:00:27 +03:00
MySqlConfigBackendImpl::setIOService(IOServicePtr(new IOService()));
IOServiceMgr::instance().registerIOService(MySqlConfigBackendImpl::getIOService());
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 */) {
2024-10-02 19:00:27 +03:00
MySqlConfigBackendImpl::setIOService(IOServicePtr(new IOService()));
IOServiceMgr::instance().registerIOService(MySqlConfigBackendImpl::getIOService());
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() {
// Unregister the factories and remove MySQL backends
2024-10-02 19:00:27 +03:00
MySqlConfigBackendDHCPv4::unregisterBackendType();
MySqlConfigBackendDHCPv6::unregisterBackendType();
IOServicePtr io_service = MySqlConfigBackendImpl::getIOService();
2024-06-26 16:59:27 +03:00
if (io_service) {
IOServiceMgr::instance().unregisterIOService(io_service);
io_service->stopAndPoll();
2024-10-02 19:00:27 +03:00
MySqlConfigBackendImpl::setIOService(IOServicePtr());
}
2024-10-01 15:27:45 +03:00
LOG_INFO(mysql_cb_logger, MYSQL_CB_DEINIT_OK);
// Unregister the factories and remove MySQL backends
HostDataSourceFactory::deregisterFactory("mysql", true);
LOG_INFO(mysql_hb_logger, MYSQL_HB_DEINIT_OK);
// Unregister the factories and remove MySQL backends
LeaseMgrFactory::deregisterFactory("mysql", true);
LOG_INFO(mysql_lb_logger, MYSQL_LB_DEINIT_OK);
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.
///
/// @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"