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
11#include <hooks/hooks_manager.h>
12
13#include <iostream>
14
15namespace isc {
16namespace hooks {
17
18// Callout manipulation - all deferred to the CalloutManager.
19
20void
21LibraryHandle::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
40void
41LibraryHandle::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
50bool
51LibraryHandle::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
66bool
67LibraryHandle::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
83LibraryHandle::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.
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
110LibraryHandle::getParameter(const std::string& name) {
111 // Try to find appropriate parameter. May return null pointer
112 isc::data::ConstElementPtr params = getParameters();
113 if (!params) {
115 }
116
117 // May return null pointer if there's no parameter.
118 return (params->get(name));
119}
120
121std::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
void registerCallout(const std::string &name, CalloutPtr callout)
Register a callout on a hook for the current library.
bool deregisterCallout(const std::string &name, CalloutPtr callout)
De-Register a callout on a hook for the current library.
int getLibraryIndex() const
Get current library index.
void setLibraryIndex(int library_index)
Set current library index.
bool deregisterAllCallouts(const std::string &name)
Removes all callouts on a hook for the current library.
void registerCommandHook(const std::string &command_name)
Registers a hook point for the specified command name.
static HookLibsCollection getLibraryInfo()
Return list of loaded libraries with its parameters.
static HooksManager & getHooksManager()
Get singleton hooks manager.
void registerCallout(const std::string &name, CalloutPtr callout)
Register a callout on a hook.
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.
std::vector< std::string > getParameterNames()
Returns names of configuration parameters for the library.
void registerCommandCallout(const std::string &command_name, CalloutPtr callout)
Register control command handler.
bool deregisterCallout(const std::string &name, CalloutPtr callout)
De-Register a callout on a hook.
static std::string commandToHookName(const std::string &command_name)
Generates hook point name for the given control command name.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
int(* CalloutPtr)(CalloutHandle &)
Typedef for a callout pointer. (Callouts must have "C" linkage.)
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
Defines the logger used by the top-level component of kea-dhcp-ddns.