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_cb/mysql_cb_callouts.cc

123 lines
4.0 KiB
C++
Raw Normal View History

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
#include <dhcpsrv/cfgmgr.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>
2018-09-14 09:37:47 +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;
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");
}
}
2019-05-09 12:30:27 +03:00
LOG_INFO(mysql_cb_logger, MYSQL_CB_INIT_OK);
// Register MySQL CB factories with CB Managers
isc::dhcp::MySqlConfigBackendDHCPv4::registerBackendType();
isc::dhcp::MySqlConfigBackendDHCPv6::registerBackendType();
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) {
isc::asiolink::IOServicePtr io_service;
handle.getArgument("io_context", io_service);
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);
}
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);
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);
}
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);
// Unregister the factories and remove MySQL backends
isc::dhcp::MySqlConfigBackendDHCPv4::unregisterBackendType();
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.
///
/// @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"