Kea  1.5.0
library_handle.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2018 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  // Reset library index if required, saving the current value.
23  int saved_index = callout_manager_->getLibraryIndex();
24  if (index_ >= 0) {
25  callout_manager_->setLibraryIndex(index_);
26  }
27 
28  // Register the callout.
29  callout_manager_->registerCallout(name, callout);
30 
31  // Restore the library index if required. We know that the saved index
32  // is valid for the number of libraries (or is -1, which is an internal
33  // state indicating there is no current library index) as we obtained it
34  // from the callout manager.
35  if (index_ >= 0) {
36  callout_manager_->setLibraryIndex(saved_index);
37  }
38 }
39 
40 void
41 LibraryHandle::registerCommandCallout(const std::string& command_name,
42  CalloutPtr callout) {
43  // Register hook point for this command, if one doesn't exist.
44  callout_manager_->registerCommandHook(command_name);
45  // Register the command handler as a callout.
46  registerCallout(ServerHooks::commandToHookName(command_name), callout);
47 }
48 
49 
50 bool
51 LibraryHandle::deregisterCallout(const std::string& name, CalloutPtr callout) {
52  int saved_index = callout_manager_->getLibraryIndex();
53  if (index_ >= 0) {
54  callout_manager_->setLibraryIndex(index_);
55  }
56 
57  bool status = callout_manager_->deregisterCallout(name, callout);
58 
59  if (index_ >= 0) {
60  callout_manager_->setLibraryIndex(saved_index);
61  }
62 
63  return (status);
64 }
65 
66 bool
67 LibraryHandle::deregisterAllCallouts(const std::string& name) {
68  int saved_index = callout_manager_->getLibraryIndex();
69  if (index_ >= 0) {
70  callout_manager_->setLibraryIndex(index_);
71  }
72 
73  bool status = callout_manager_->deregisterAllCallouts(name);
74 
75  if (index_ >= 0) {
76  callout_manager_->setLibraryIndex(saved_index);
77  }
78 
79  return (status);
80 }
81 
83 LibraryHandle::getParameters() {
85 
86  int index = index_;
87 
88  if (index == -1) {
89  // -1 means that current index is stored in CalloutManager.
90  // So let's get the index from there. See comment for
91  // LibraryHandle::index_.
92  index = callout_manager_->getLibraryIndex();
93  }
94 
95  if ((index > libinfo.size()) || (index <= 0)) {
96  // Something is very wrong here. The library index is out of bounds.
97  // However, this is user facing interface, so we should not throw here.
98  return (isc::data::ConstElementPtr());
99  }
100 
101  // Some indexes have special meaning:
102  // * 0 - pre-user library callout
103  // * 1..numlib - indexes for actual libraries
104  // * INT_MAX - post-user library callout
105 
106  return (libinfo[index - 1].second);
107 }
108 
110 LibraryHandle::getParameter(const std::string& name) {
111  // Try to find appropriate parameter. May return null pointer
112  isc::data::ConstElementPtr params = getParameters();
113  if (!params) {
114  return (isc::data::ConstElementPtr());
115  }
116 
117  // May return null pointer if there's no parameter.
118  return (params->get(name));
119 }
120 
121 std::vector<std::string>
123  std::vector<std::string> names;
124  // Find all parameter names.
125  isc::data::ConstElementPtr params = getParameters();
126  if (!params ||
127  (params->getType() != isc::data::Element::map) ||
128  (params->size() == 0)) {
129  return (names);
130  }
131  auto map = params->mapValue();
132  for (auto elem = map.begin(); elem != map.end(); ++elem) {
133  names.push_back(elem->first);
134  }
135  return (names);
136 }
137 
138 
139 } // namespace util
140 } // namespace isc
isc::hooks::CalloutManager::deregisterAllCallouts
bool deregisterAllCallouts(const std::string &name)
Removes all callouts on a hook for the current library.
Definition: callout_manager.cc:288
isc::hooks::HooksManager::getHooksManager
static HooksManager & getHooksManager()
Get singleton hooks manager.
Definition: hooks_manager.cc:35
library_handle.h
hooks_manager.h
isc::hooks::CalloutManager::setLibraryIndex
void setLibraryIndex(int library_index)
Set current library index.
Definition: callout_manager.h:317
isc::hooks::HooksManager::getLibraryInfo
static HookLibsCollection getLibraryInfo()
Return list of loaded libraries with its parameters.
Definition: hooks_manager.cc:174
isc::hooks::CalloutManager::registerCommandHook
void registerCommandHook(const std::string &command_name)
Registers a hook point for the specified command name.
Definition: callout_manager.cc:325
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
callout_manager.h
isc::hooks::CalloutManager::getLibraryIndex
int getLibraryIndex() const
Get current library index.
Definition: callout_manager.h:299
isc::hooks::LibraryHandle::getParameterNames
std::vector< std::string > getParameterNames()
Returns names of configuration parameters for the library.
Definition: library_handle.cc:122
isc::hooks::ServerHooks::commandToHookName
static std::string commandToHookName(const std::string &command_name)
Generates hook point name for the given control command name.
Definition: server_hooks.cc:200
isc::hooks::LibraryHandle::getParameter
isc::data::ConstElementPtr getParameter(const std::string &name)
Returns configuration parameter for the library.
Definition: library_handle.cc:110
isc::hooks::CalloutPtr
int(* CalloutPtr)(CalloutHandle &)
Typedef for a callout pointer. (Callouts must have "C" linkage.)
Definition: library_handle.h:22
isc::data::Element::map
@ map
Definition: data.h:151
isc::hooks::LibraryHandle::registerCommandCallout
void registerCommandCallout(const std::string &command_name, CalloutPtr callout)
Register control command handler.
Definition: library_handle.cc:41
isc::hooks::LibraryHandle::registerCallout
void registerCallout(const std::string &name, CalloutPtr callout)
Register a callout on a hook.
Definition: library_handle.cc:21
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::hooks::CalloutManager::registerCallout
void registerCallout(const std::string &name, CalloutPtr callout)
Register a callout on a hook for the current library.
Definition: callout_manager.cc:61
isc::hooks::HookLibsCollection
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
isc::hooks::LibraryHandle::deregisterCallout
bool deregisterCallout(const std::string &name, CalloutPtr callout)
De-Register a callout on a hook.
Definition: library_handle.cc:51
isc::hooks::CalloutManager::deregisterCallout
bool deregisterCallout(const std::string &name, CalloutPtr callout)
De-Register a callout on a hook for the current library.
Definition: callout_manager.cc:235
isc::hooks::LibraryHandle::deregisterAllCallouts
bool deregisterAllCallouts(const std::string &name)
Removes all callouts on a hook.
Definition: library_handle.cc:67