Kea 2.7.6
hooks_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2024 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 <cc/data.h>
11#include <hooks/hooks_parser.h>
12#include <boost/algorithm/string.hpp>
13#include <util/str.h>
14#include <vector>
15
16using namespace std;
17using namespace isc::data;
18using namespace isc::hooks;
19using namespace isc::dhcp;
20
21namespace isc {
22namespace hooks {
23
24// @todo use the flat style, split into list and item
25
26string HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH);
27
28void
30 // Initialize.
31 libraries.clear();
32
33 if (!value) {
34 isc_throw(DhcpConfigError, "Tried to parse null hooks libraries");
35 }
36
37 // This is the new syntax. Iterate through it and get each map.
38 for (auto const& library_entry : value->listValue()) {
39 ConstElementPtr parameters;
40
41 // Is it a map?
42 if (library_entry->getType() != Element::map) {
43 isc_throw(DhcpConfigError, "hooks library configuration error:"
44 " one or more entries in the hooks-libraries list is not"
45 " a map (" << library_entry->getPosition() << ")");
46 }
47
48 // Iterate through each element in the map. We check
49 // whether we have found a library element.
50 bool lib_found = false;
51
52 string libname = "";
53
54 // Let's explicitly reset the parameters, so we won't cover old
55 // values from the previous loop round.
56 parameters.reset();
57
58 for (auto const& entry_item : library_entry->mapValue()) {
59 if (entry_item.first == "library") {
60 if (entry_item.second->getType() != Element::string) {
61 isc_throw(DhcpConfigError, "hooks library configuration"
62 " error: value of 'library' element is not a string"
63 " giving the path to a hooks library (" <<
64 entry_item.second->getPosition() << ")");
65 }
66
67 // Get the name of the library and add it to the list after
68 // removing quotes.
69 libname = (entry_item.second)->stringValue();
70
71 // Remove leading/trailing quotes and any leading/trailing
72 // spaces.
73 boost::erase_all(libname, "\"");
74 libname = isc::util::str::trim(libname);
75 if (libname.empty()) {
76 isc_throw(DhcpConfigError, "hooks library configuration"
77 " error: value of 'library' element must not be"
78 " blank (" <<
79 entry_item.second->getPosition() << ")");
80 }
81
82 // If only the name of the library was provided, add the full path.
83 if (libname.find("/") == string::npos) {
84 libname = HooksLibrariesParser::default_hooks_path_ + "/" + libname;
85 }
86
87 // Note we have found the library name.
88 lib_found = true;
89 continue;
90 }
91
92 // If there are parameters, let's remember them.
93 if (entry_item.first == "parameters") {
94 parameters = entry_item.second;
95 continue;
96 }
97
98 // For all other parameters we will throw.
99 isc_throw(DhcpConfigError, "unknown hooks library parameter: "
100 << entry_item.first << "("
101 << library_entry->getPosition() << ")");
102 }
103
104 if (! lib_found) {
105 isc_throw(DhcpConfigError, "hooks library configuration error:"
106 " one or more hooks-libraries elements are missing the"
107 " name of the library" <<
108 " (" << library_entry->getPosition() << ")");
109 }
110
111 libraries.add(libname, parameters);
112 }
113}
114
115}
116}
To be removed. Please use ConfigError instead.
Wrapper class that holds hooks libraries configuration.
void clear()
Removes all configured hooks libraries.
void add(std::string libname, isc::data::ConstElementPtr parameters)
Adds additional hooks libraries.
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
static std::string default_hooks_path_
The default installation path for hook libraries, used to generate full path if only the hook library...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
Defines the logger used by the top-level component of kea-lfc.