Kea  1.9.9-git
library_handle.cc
Go to the documentation of this file.
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 
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
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
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
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
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 
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 
101 LibraryHandle::getParameter(const std::string& name) {
102  // Try to find appropriate parameter. May return null pointer
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>
114  std::vector<std::string> names;
115  // Find all parameter names.
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
std::vector< std::string > getParameterNames()
Returns names of configuration parameters for the library.
int(* CalloutPtr)(CalloutHandle &)
Typedef for a callout pointer. (Callouts must have "C" linkage.)
bool deregisterCallout(const std::string &name, CalloutPtr callout)
De-Register a callout on a hook.
void registerCommandHook(const std::string &command_name)
Registers a hook point for the specified command name.
void registerCallout(const std::string &name, CalloutPtr callout, int library_index)
Register a callout on a hook for the current library.
void registerCommandCallout(const std::string &command_name, CalloutPtr callout)
Register control command handler.
static HookLibsCollection getLibraryInfo()
Return list of loaded libraries with its parameters.
isc::data::ConstElementPtr getParameters()
Get configuration parameter common code.
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
void registerCallout(const std::string &name, CalloutPtr callout)
Register a callout on a hook.
Defines the logger used by the top-level component of kea-dhcp-ddns.
bool deregisterAllCallouts(const std::string &name, int library_index)
Removes all callouts on a hook for the current library.
static std::string commandToHookName(const std::string &command_name)
Generates hook point name for the given control command name.
bool deregisterAllCallouts(const std::string &name)
Removes all callouts on a hook.
isc::data::ConstElementPtr getParameter(const std::string &name)
Returns configuration parameter for the library.
int getLibraryIndex() const
Get current library index.
bool deregisterCallout(const std::string &name, CalloutPtr callout, int library_index)
De-Register a callout on a hook for the current library.