1 // Copyright (C) 2013-2020 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/callout_manager.h>
10 #include <hooks/library_handle.h>
11 #include <hooks/hooks_manager.h>
12
13 #include <iostream>
14
15 namespace isc {
16 namespace hooks {
17
18 // Callout manipulation - all deferred to the CalloutManager.
19
20 void
registerCallout(const std::string & name,CalloutPtr callout)21 LibraryHandle::registerCallout(const std::string& name, CalloutPtr callout) {
22 int index = index_;
23
24 if (index_ == -1) {
25 // -1 means that current index is stored in CalloutManager.
26 // So let's get the index from there. See comment for
27 // LibraryHandle::index_.
28 index = callout_manager_.getLibraryIndex();
29 }
30
31 // Register the callout.
32 callout_manager_.registerCallout(name, callout, index);
33 }
34
35 void
registerCommandCallout(const std::string & command_name,CalloutPtr callout)36 LibraryHandle::registerCommandCallout(const std::string& command_name,
37 CalloutPtr callout) {
38 // Register hook point for this command, if one doesn't exist.
39 callout_manager_.registerCommandHook(command_name);
40 // Register the command handler as a callout.
41 registerCallout(ServerHooks::commandToHookName(command_name), callout);
42 }
43
44
45 bool
deregisterCallout(const std::string & name,CalloutPtr callout)46 LibraryHandle::deregisterCallout(const std::string& name, CalloutPtr callout) {
47 int index = index_;
48
49 if (index_ == -1) {
50 // -1 means that current index is stored in CalloutManager.
51 // So let's get the index from there. See comment for
52 // LibraryHandle::index_.
53 index = callout_manager_.getLibraryIndex();
54 }
55
56 return (callout_manager_.deregisterCallout(name, callout, index));
57 }
58
59 bool
deregisterAllCallouts(const std::string & name)60 LibraryHandle::deregisterAllCallouts(const std::string& name) {
61 int index = index_;
62
63 if (index_ == -1) {
64 // -1 means that current index is stored in CalloutManager.
65 // So let's get the index from there. See comment for
66 // LibraryHandle::index_.
67 index = callout_manager_.getLibraryIndex();
68 }
69
70 return (callout_manager_.deregisterAllCallouts(name, index));
71 }
72
73 isc::data::ConstElementPtr
getParameters()74 LibraryHandle::getParameters() {
75 HookLibsCollection libinfo = HooksManager::getLibraryInfo();
76
77 int index = index_;
78
79 if (index == -1) {
80 // -1 means that current index is stored in CalloutManager.
81 // So let's get the index from there. See comment for
82 // LibraryHandle::index_.
83 index = callout_manager_.getLibraryIndex();
84 }
85
86 if ((index > libinfo.size()) || (index <= 0)) {
87 // Something is very wrong here. The library index is out of bounds.
88 // However, this is user facing interface, so we should not throw here.
89 return (isc::data::ConstElementPtr());
90 }
91
92 // Some indexes have special meaning:
93 // * 0 - pre-user library callout
94 // * 1 -> numlib - indexes for actual libraries
95 // * INT_MAX - post-user library callout
96
97 return (libinfo[index - 1].second);
98 }
99
100 isc::data::ConstElementPtr
getParameter(const std::string & name)101 LibraryHandle::getParameter(const std::string& name) {
102 // Try to find appropriate parameter. May return null pointer
103 isc::data::ConstElementPtr params = getParameters();
104 if (!params || (params->getType() != isc::data::Element::map)) {
105 return (isc::data::ConstElementPtr());
106 }
107
108 // May return null pointer if there's no parameter.
109 return (params->get(name));
110 }
111
112 std::vector<std::string>
getParameterNames()113 LibraryHandle::getParameterNames() {
114 std::vector<std::string> names;
115 // Find all parameter names.
116 isc::data::ConstElementPtr params = getParameters();
117 if (!params ||
118 (params->getType() != isc::data::Element::map) ||
119 (params->size() == 0)) {
120 return (names);
121 }
122 auto map = params->mapValue();
123 for (auto elem = map.begin(); elem != map.end(); ++elem) {
124 names.push_back(elem->first);
125 }
126 return (names);
127 }
128
129
130 } // namespace util
131 } // namespace isc
132