Kea 3.3.3
host_cache_callout.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7// Functions accessed by the hooks framework use C linkage to avoid the name
8// mangling that accompanies use of the C++ compiler as well as to avoid
9// issues related to namespaces.
10
11#include <config.h>
12
14#include <dhcpsrv/cfgmgr.h>
16#include <dhcpsrv/host_mgr.h>
17#include <hooks/hooks.h>
19#include <process/daemon.h>
20
21#include <host_cache_log.h>
22
23using namespace isc::db;
24using namespace isc::dhcp;
25using namespace isc::process;
26
27namespace isc {
28namespace host_cache {
29
32
38
39} // end of namespace isc::host_cache
40} // end of namespace isc
41
42using namespace isc::config;
43using namespace isc::data;
44using namespace isc::hooks;
45using namespace isc::host_cache;
46
47extern "C" {
48
56 return (hcptr->cacheSizeHandler(handle));
57}
58
66 return (hcptr->cacheClearHandler(handle));
67}
68
76 return (hcptr->cacheFlushHandler(handle));
77}
78
86 return (hcptr->cacheGetHandler(handle));
87}
88
96 return (hcptr->cacheGetByIdHandler(handle));
97}
98
106 return (hcptr->cacheInsertHandler(handle));
107}
108
116 return (hcptr->cacheWriteHandler(handle));
117}
118
126 return (hcptr->cacheLoadHandler(handle));
127}
128
136 return (hcptr->cacheRemoveHandler(handle));
137}
138
143int load(LibraryHandle& handle) {
144 try {
145 // Make the hook library not loadable by d2.
146 uint16_t family = CfgMgr::instance().getFamily();
147 const std::string& proc_name = Daemon::getProcName();
148 if (family == AF_INET) {
149 if (proc_name != "kea-dhcp4") {
150 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
151 << ", expected kea-dhcp4");
152 }
153 } else {
154 if (proc_name != "kea-dhcp6") {
155 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
156 << ", expected kea-dhcp6");
157 }
158 }
159
160 // Get and decode parameters.
161 hcptr.reset(new HostCache());
162 ConstElementPtr config = handle.getParameter("host-cache");
163 hcptr->configure(config);
165 handle.registerCommandCallout("cache-clear", cache_clear);
166 handle.registerCommandCallout("cache-flush", cache_flush);
167 handle.registerCommandCallout("cache-get", cache_get);
168 handle.registerCommandCallout("cache-get-by-id", cache_get_by_id);
169 handle.registerCommandCallout("cache-insert", cache_insert);
170 handle.registerCommandCallout("cache-load", cache_load);
171 handle.registerCommandCallout("cache-remove", cache_remove);
172 handle.registerCommandCallout("cache-size", cache_size);
173 handle.registerCommandCallout("cache-write", cache_write);
174 HostMgr::instance().addBackend("type=cache");
175 } catch (const std::exception& ex) {
177 .arg(ex.what());
178 return (CONTROL_RESULT_ERROR);
179 }
180
182 return (CONTROL_RESULT_SUCCESS);
183}
184
195
200 return (1);
201}
202
203} // end extern "C"
A generic exception that is thrown when an unexpected error condition occurs.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:246
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
static bool deregisterFactory(const std::string &db_type, bool no_log=false)
Deregister a host data source factory.
static bool registerFactory(const std::string &db_type, const Factory &factory, bool no_log=false, DBVersion db_version=DBVersion())
Register a host data source factory.
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
Definition host_mgr.cc:62
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Definition host_mgr.cc:57
static HostMgr & instance()
Returns a sole instance of the HostMgr.
Definition host_mgr.cc:114
Per-packet callout handle.
isc::data::ConstElementPtr getParameter(const std::string &name)
Returns configuration parameter for the library.
void registerCommandCallout(const std::string &command_name, CalloutPtr callout)
Register control command handler.
Host Cache implementation.
Definition host_cache.h:35
static std::string getProcName()
returns the process name This value is used as when forming the default PID file name
Definition daemon.cc:156
This file contains several functions and constants that are used for handling commands and responses ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
int cache_load(CalloutHandle &handle)
This is a command callout for 'cache-load' command.
int cache_remove(CalloutHandle &handle)
This is a command callout for 'cache-remove' command.
int cache_get(CalloutHandle &handle)
This is a command callout for 'cache-get' command.
int multi_threading_compatible()
This function is called to retrieve the multi-threading compatibility.
int cache_flush(CalloutHandle &handle)
This is a command callout for 'cache-flush' command.
int unload()
This function is called when the library is unloaded.
int cache_insert(CalloutHandle &handle)
This is a command callout for 'cache-insert' command.
int cache_get_by_id(CalloutHandle &handle)
This is a command callout for 'cache-get-by-id' command.
int cache_clear(CalloutHandle &handle)
This is a command callout for 'cache-clear' command.
int load(LibraryHandle &handle)
This function is called when the library is loaded.
int cache_write(CalloutHandle &handle)
This is a command callout for 'cache-write' command.
int cache_size(CalloutHandle &handle)
This is a command callout for 'cache-size' command.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< BaseHostDataSource > HostDataSourcePtr
HostDataSource pointer.
const isc::log::MessageID HOST_CACHE_CONFIGURATION_FAILED
boost::shared_ptr< HostCache > HostCachePtr
Pointer to the Host Cache hooks library implementation.
Definition host_cache.h:681
const isc::log::MessageID HOST_CACHE_INIT_OK
const isc::log::MessageID HOST_CACHE_DEINIT_OK
HostDataSourcePtr factory(const DatabaseConnection::ParameterMap &)
Host Cache factory.
HostCachePtr hcptr
Pointer to the Host Cache instance.
isc::log::Logger host_cache_logger("host-cache-hooks")
Host Cache Logger.
Defines the logger used by the top-level component of kea-lfc.