Kea 3.1.1
host_cache_callout.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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
13#include <host_cache.h>
14#include <host_cache_log.h>
16#include <hooks/hooks.h>
17#include <dhcpsrv/cfgmgr.h>
18#include <dhcpsrv/host_mgr.h>
20#include <process/daemon.h>
21
22using namespace isc::db;
23using namespace isc::dhcp;
24using namespace isc::process;
25
26namespace isc {
27namespace host_cache {
28
31
37
38} // end of namespace isc::host_cache
39} // end of namespace isc
40
41using namespace isc::config;
42using namespace isc::data;
43using namespace isc::hooks;
44using namespace isc::host_cache;
45
46extern "C" {
47
55 return (hcptr->cacheSizeHandler(handle));
56}
57
65 return (hcptr->cacheClearHandler(handle));
66}
67
75 return (hcptr->cacheFlushHandler(handle));
76}
77
85 return (hcptr->cacheGetHandler(handle));
86}
87
95 return (hcptr->cacheGetByIdHandler(handle));
96}
97
105 return (hcptr->cacheInsertHandler(handle));
106}
107
115 return (hcptr->cacheWriteHandler(handle));
116}
117
125 return (hcptr->cacheLoadHandler(handle));
126}
127
135 return (hcptr->cacheRemoveHandler(handle));
136}
137
142int load(LibraryHandle& handle) {
143 try {
144 // Make the hook library not loadable by d2 or ca.
145 uint16_t family = CfgMgr::instance().getFamily();
146 const std::string& proc_name = Daemon::getProcName();
147 if (family == AF_INET) {
148 if (proc_name != "kea-dhcp4") {
149 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
150 << ", expected kea-dhcp4");
151 }
152 } else {
153 if (proc_name != "kea-dhcp6") {
154 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
155 << ", expected kea-dhcp6");
156 }
157 }
158
159 // Get and decode parameters.
160 hcptr.reset(new HostCache());
161 ConstElementPtr config = handle.getParameter("host-cache");
162 hcptr->configure(config);
164 handle.registerCommandCallout("cache-clear", cache_clear);
165 handle.registerCommandCallout("cache-flush", cache_flush);
166 handle.registerCommandCallout("cache-get", cache_get);
167 handle.registerCommandCallout("cache-get-by-id", cache_get_by_id);
168 handle.registerCommandCallout("cache-insert", cache_insert);
169 handle.registerCommandCallout("cache-load", cache_load);
170 handle.registerCommandCallout("cache-remove", cache_remove);
171 handle.registerCommandCallout("cache-size", cache_size);
172 handle.registerCommandCallout("cache-write", cache_write);
173 HostMgr::instance().addBackend("type=cache");
174 } catch (const std::exception& ex) {
176 .arg(ex.what());
177 return (CONTROL_RESULT_ERROR);
178 }
179
181 return (CONTROL_RESULT_SUCCESS);
182}
183
194
199 return (1);
200}
201
202} // 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:151
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:29
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.