Kea: hooks_config.cc Source File

| Kea 3.0.0

hooks_config.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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#include <config.h>
8
9#include <hooks/hooks_config.h>
10#include <hooks/hooks_manager.h>
11
12using namespace std;
13using namespace isc;
14using namespace isc::data;
15
16namespace isc {
17namespace hooks {
18
19void
20HooksConfig::add(const std::string& libname,
21 isc::data::ConstElementPtr parameters,
22 const std::string& cfgname /* = "" */) {
23 HookLibInfo info(libname, parameters, cfgname);
24 libraries_.push_back(info);
25}
26
27void
28HooksConfig::verifyLibraries(const Element::Position& position,
29 bool multi_threading_enabled) const {
30 // The code used to follow this logic:
31 //
32 // Check if the list of libraries has changed. If not, nothing is done.
33 //
34 // We no longer rely on this. Parameters can change. And even if the
35 // parameters stay the same, they could point to files that could
36 // change. We can skip loading routines only if there were and there still
37 // are no libraries specified.
38 vector<string> current_libraries = HooksManager::getLibraryNames();
39 if (current_libraries.empty() && libraries_.empty()) {
40 return;
41 }
42
43 // Library list has changed, validate each of the libraries specified.
44 vector<string> lib_names = isc::hooks::extractNames(libraries_);
45 vector<string> error_libs = HooksManager::validateLibraries(lib_names,
46 multi_threading_enabled);
47 if (!error_libs.empty()) {
48
49 // Construct the list of libraries in error for the message.
50 string error_list = error_libs[0];
51 for (size_t i = 1; i < error_libs.size(); ++i) {
52 error_list += (string(", ") + error_libs[i]);
53 }
54 isc_throw(InvalidHooksLibraries,
55 "hooks libraries failed to validate - "
56 "library or libraries in error are: "
57 << error_list << " (" << position << ")");
58 }
59}
60
61void
62HooksConfig::loadLibraries(bool multi_threading_enabled) const {
67 if (!HooksManager::loadLibraries(libraries_, multi_threading_enabled)) {
68 isc_throw(InvalidHooksLibraries,
69 "One or more hook libraries failed to load");
70 }
71}
72
73bool
74HooksConfig::equal(const HooksConfig& other) const {
75
84 for (auto const& this_it : libraries_) {
85 bool match = false;
86 for (auto const& other_it : other.libraries_) {
87 if (this_it == other_it) {
88 match = true;
89 break;
90 }
91 }
92
93 // No match found for the particular hooks library so return false.
94 if (!match) {
95 return(false);
96 }
97 }
98
99 return (true);
100}
101
102 ElementPtr
103HooksConfig::toElement() const {
104 // hooks-libraries is a list of maps
105 ElementPtr result = Element::createList();
106 // Iterate through libraries
107 for (auto const& hl : libraries_) {
108 // Entries are maps
109 ElementPtr map = Element::createMap();
110 // Set the library name
111 map->set("library", Element::create(hl.cfgname_));
112 // Set parameters (not set vs set empty map)
113 if (!isNull(hl.parameters_)) {
114 map->set("parameters", hl.parameters_);
115 }
116 // Push to the list
117 result->add(map);
118 }
119 return (result);
120}
121
122}
123}
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
isc::hooks::HooksConfig::equal
bool equal(const HooksConfig &other) const
Compares two Hooks Config classes for equality.
Definition hooks_config.cc:74
isc::hooks::HooksConfig::add
void add(const std::string &libname, isc::data::ConstElementPtr parameters, const std::string &cfgname="")
Adds additional hooks libraries.
Definition hooks_config.cc:20
isc::hooks::HooksConfig::verifyLibraries
void verifyLibraries(const isc::data::Element::Position &position, bool multi_threading_enabled) const
Verifies that libraries stored in libraries_ are valid.
Definition hooks_config.cc:28
isc::hooks::HooksConfig::toElement
isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition hooks_config.cc:103
isc::hooks::HooksConfig::HooksConfig
HooksConfig()
Default constructor.
Definition hooks_config.h:40
isc::hooks::HooksConfig::loadLibraries
void loadLibraries(bool multi_threading_enabled) const
Commits hooks libraries configuration.
Definition hooks_config.cc:62
isc::hooks::HooksManager::getLibraryNames
static std::vector< std::string > getLibraryNames()
Return list of loaded libraries.
Definition hooks_manager.cc:220
isc::hooks::HooksManager::validateLibraries
static std::vector< std::string > validateLibraries(const std::vector< std::string > &libraries, bool multi_threading_enabled=false)
Validate library list.
Definition hooks_manager.cc:261
isc::hooks::HooksManager::loadLibraries
static bool loadLibraries(const HookLibsCollection &libraries, bool multi_threading_enabled=false)
Load and reload libraries.
Definition hooks_manager.cc:133
isc::hooks::InvalidHooksLibraries
Exception thrown when a library failed to validate.
Definition hooks_config.h:19
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
hooks_config.h
hooks_manager.h
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::isNull
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
Definition data.cc:1148
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::hooks
Definition callout_handle.cc:21
isc::hooks::extractNames
std::vector< std::string > extractNames(const isc::hooks::HookLibsCollection &libraries)
Extracts names from HookLibsCollection.
Definition libinfo.cc:26
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
Element::Position
Represents the position of the data element within a configuration string.
Definition data.h:94
isc::hooks::HookLibInfo
Entity that holds information about hook libraries and their parameters.
Definition libinfo.h:23