summaryrefslogtreecommitdiffstats
path: root/src/lib/hooks/library_handle.cc
blob: b070448e7a7f1246c95212cc5f8502dacb65460b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2013-2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <hooks/callout_manager.h>
#include <hooks/library_handle.h>
#include <hooks/hooks_manager.h>

#include <iostream>

namespace isc {
namespace hooks {

// Callout manipulation - all deferred to the CalloutManager.

void
LibraryHandle::registerCallout(const std::string& name, CalloutPtr callout) {
    int index = index_;

    if (index_ == -1) {
        // -1 means that current index is stored in CalloutManager.
        // So let's get the index from there. See comment for
        // LibraryHandle::index_.
        index = callout_manager_.getLibraryIndex();
    }

    // Register the callout.
    callout_manager_.registerCallout(name, callout, index);
}

void
LibraryHandle::registerCommandCallout(const std::string& command_name,
                                      CalloutPtr callout) {
    // Register hook point for this command, if one doesn't exist.
    callout_manager_.registerCommandHook(command_name);
    // Register the command handler as a callout.
    registerCallout(ServerHooks::commandToHookName(command_name), callout);
}


bool
LibraryHandle::deregisterCallout(const std::string& name, CalloutPtr callout) {
    int index = index_;

    if (index_ == -1) {
        // -1 means that current index is stored in CalloutManager.
        // So let's get the index from there. See comment for
        // LibraryHandle::index_.
        index = callout_manager_.getLibraryIndex();
    }

    return (callout_manager_.deregisterCallout(name, callout, index));
}

bool
LibraryHandle::deregisterAllCallouts(const std::string& name) {
    int index = index_;

    if (index_ == -1) {
        // -1 means that current index is stored in CalloutManager.
        // So let's get the index from there. See comment for
        // LibraryHandle::index_.
        index = callout_manager_.getLibraryIndex();
    }

    return (callout_manager_.deregisterAllCallouts(name, index));
}

isc::data::ConstElementPtr
LibraryHandle::getParameters() {
    HookLibsCollection libinfo = HooksManager::getLibraryInfo();

    int index = index_;

    if (index == -1) {
        // -1 means that current index is stored in CalloutManager.
        // So let's get the index from there. See comment for
        // LibraryHandle::index_.
        index = callout_manager_.getLibraryIndex();
    }

    if ((index > libinfo.size()) || (index <= 0)) {
        // Something is very wrong here. The library index is out of bounds.
        // However, this is user facing interface, so we should not throw here.
        return (isc::data::ConstElementPtr());
    }

    // Some indexes have special meaning:
    // * 0           - pre-user library callout
    // * 1 -> numlib - indexes for actual libraries
    // * INT_MAX     - post-user library callout

    return (libinfo[index - 1].second);
}

isc::data::ConstElementPtr
LibraryHandle::getParameter(const std::string& name) {
    // Try to find appropriate parameter. May return null pointer
    isc::data::ConstElementPtr params = getParameters();
    if (!params || (params->getType() != isc::data::Element::map)) {
        return (isc::data::ConstElementPtr());
    }

    // May return null pointer if there's no parameter.
    return (params->get(name));
}

std::vector<std::string>
LibraryHandle::getParameterNames() {
    std::vector<std::string> names;
    // Find all parameter names.
    isc::data::ConstElementPtr params = getParameters();
    if (!params ||
        (params->getType() != isc::data::Element::map) ||
        (params->size() == 0)) {
        return (names);
    }
    auto map = params->mapValue();
    for (auto elem = map.begin(); elem != map.end(); ++elem) {
        names.push_back(elem->first);
    }
    return (names);
}


} // namespace util
} // namespace isc