Kea: callout_handle.cc Source File

| Kea 2.2.0

callout_handle.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2022 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_handle.h>
10#include <hooks/callout_manager.h>
11#include <hooks/library_handle.h>
12#include <hooks/server_hooks.h>
13
14#include <string>
15#include <utility>
16#include <vector>
17
18using namespace std;
19
20namespace isc {
21 namespace hooks {
22
23// Constructor.
24CalloutHandle::CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
25 const boost::shared_ptr<LibraryManagerCollection>& lmcoll)
26 : lm_collection_(lmcoll), arguments_(), context_collection_(),
27 manager_(manager), server_hooks_(ServerHooks::getServerHooks()),
28 current_library_(-1), current_hook_(-1), next_step_(NEXT_STEP_CONTINUE) {
29
30 // Call the "context_create" hook. We should be OK doing this - although
31 // the constructor has not finished running, all the member variables
32 // have been created.
33 manager_->callCallouts(ServerHooks::CONTEXT_CREATE, *this);
34}
35
36// Destructor
37CalloutHandle::~CalloutHandle() {
38 // Call the "context_destroy" hook. We should be OK doing this - although
39 // the destructor is being called, all the member variables are still in
40 // existence.
41 manager_->callCallouts(ServerHooks::CONTEXT_DESTROY, *this);
42
43 // Explicitly clear the argument and context objects. This should free up
44 // all memory that could have been allocated by libraries that were loaded.
45 arguments_.clear();
46 context_collection_.clear();
47
48 // Normal destruction of the remaining variables will include the
49 // destruction of lm_collection_, an action that decrements the reference
50 // count on the library manager collection (which holds the libraries that
51 // could have allocated memory in the argument and context members.) When
52 // that goes to zero, the libraries will be unloaded: at that point nothing
53 // in the hooks framework will be pointing to memory in the libraries'
54 // address space.
55 //
56 // It is possible that some other data structure in the server (the program
57 // using the hooks library) still references the address space and attempts
58 // to access it causing a segmentation fault. That issue is outside the
59 // scope of this framework and is not addressed by it.
60}
61
62// Return the name of all argument items.
63
64vector<string>
65CalloutHandle::getArgumentNames() const {
66 vector<string> names;
67 for (ElementCollection::const_iterator i = arguments_.begin();
68 i != arguments_.end(); ++i) {
69 names.push_back(i->first);
70 }
71
72 return (names);
73}
74
75 ParkingLotHandlePtr
76CalloutHandle::getParkingLotHandlePtr() const {
77 return (boost::make_shared<ParkingLotHandle>(server_hooks_.getParkingLotPtr(current_hook_)));
78}
79
80// Return the context for the currently pointed-to library. This version is
81// used by the "setContext()" method and creates a context for the current
82// library if it does not exist.
83
84 CalloutHandle::ElementCollection&
85CalloutHandle::getContextForLibrary() {
86 // Access a reference to the element collection for the given index,
87 // creating a new element collection if necessary, and return it.
88 return (context_collection_[current_library_]);
89}
90
91// The "const" version of the above, used by the "getContext()" method. If
92// the context for the current library doesn't exist, throw an exception.
93
94const CalloutHandle::ElementCollection&
95CalloutHandle::getContextForLibrary() const {
96 auto libcontext = context_collection_.find(current_library_);
97 if (libcontext == context_collection_.end()) {
98 isc_throw(NoSuchCalloutContext, "unable to find callout context "
99 "associated with the current library index (" << current_library_ <<
100 ")");
101 }
102
103 // Return a reference to the context's element collection.
104 return (libcontext->second);
105}
106
107// Return the name of all items in the context associated with the current]
108// library.
109
110vector<string>
111CalloutHandle::getContextNames() const {
112 vector<string> names;
113 const ElementCollection& elements = getContextForLibrary();
114 for (ElementCollection::const_iterator i = elements.begin();
115 i != elements.end(); ++i) {
116 names.push_back(i->first);
117 }
118
119 return (names);
120}
121
122// Return name of current hook (the hook to which the current callout is
123// attached) or the empty string if not called within the context of a
124// callout.
125
126string
127CalloutHandle::getHookName() const {
128 string hook = "";
129 try {
130 hook = server_hooks_.getName(current_hook_);
131 } catch (const NoSuchHook&) {
132 // Hook index is invalid, so this methods probably called from outside
133 // a callout being executed via a call to CalloutManager::callCallouts.
134 // In this case, the empty string is returned.
135 }
136
137 return (hook);
138}
139
140ScopedCalloutHandleState::
141 ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle)
142 : callout_handle_(callout_handle) {
143 if (!callout_handle_) {
144 isc_throw(BadValue, "callout_handle argument must not be null");
145 }
146
147 resetState();
148}
149
150ScopedCalloutHandleState::~ScopedCalloutHandleState() {
151 resetState();
152
153 if (on_completion_) {
154 on_completion_();
155 }
156}
157
158void
159ScopedCalloutHandleState::resetState() {
160 // No need to check if the handle is null because the constructor
161 // already checked that.
162 callout_handle_->deleteAllArguments();
163 callout_handle_->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
164}
165
166} // namespace hooks
167} // namespace isc
callout_handle.h
callout_manager.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
isc::hooks::CalloutHandle::NEXT_STEP_CONTINUE
@ NEXT_STEP_CONTINUE
continue normally
Definition: callout_handle.h:87
isc::hooks::CalloutHandle::getParkingLotHandlePtr
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
Definition: callout_handle.cc:76
isc::hooks::CalloutHandle::getHookName
std::string getHookName() const
Get hook name.
Definition: callout_handle.cc:127
isc::hooks::CalloutHandle::ElementCollection
std::map< std::string, boost::any > ElementCollection
Typedef to allow abbreviation of iterator specification in methods.
Definition: callout_handle.h:97
isc::hooks::CalloutHandle::~CalloutHandle
~CalloutHandle()
Destructor.
Definition: callout_handle.cc:37
isc::hooks::CalloutHandle::getContextNames
std::vector< std::string > getContextNames() const
Get context names.
Definition: callout_handle.cc:111
isc::hooks::CalloutHandle::CalloutHandle
CalloutHandle(const boost::shared_ptr< CalloutManager > &manager, const boost::shared_ptr< LibraryManagerCollection > &lmcoll=boost::shared_ptr< LibraryManagerCollection >())
Constructor.
Definition: callout_handle.cc:24
isc::hooks::CalloutHandle::getArgumentNames
std::vector< std::string > getArgumentNames() const
Get argument names.
Definition: callout_handle.cc:65
isc::hooks::NoSuchHook
Invalid hook.
Definition: server_hooks.h:36
isc::hooks::ScopedCalloutHandleState::ScopedCalloutHandleState
ScopedCalloutHandleState(const CalloutHandlePtr &callout_handle)
Constructor.
Definition: callout_handle.cc:141
isc::hooks::ScopedCalloutHandleState::on_completion_
std::function< void()> on_completion_
Continuation callback.
Definition: callout_handle.h:495
isc::hooks::ScopedCalloutHandleState::~ScopedCalloutHandleState
~ScopedCalloutHandleState()
Destructor.
Definition: callout_handle.cc:150
isc::hooks::ServerHooks
Server hook collection.
Definition: server_hooks.h:62
isc::hooks::ServerHooks::getParkingLotPtr
ParkingLotPtr getParkingLotPtr(const int hook_index)
Returns pointer to the ParkingLot for the specified hook index.
Definition: server_hooks.cc:190
isc::hooks::ServerHooks::CONTEXT_DESTROY
static const int CONTEXT_DESTROY
Definition: server_hooks.h:67
isc::hooks::ServerHooks::CONTEXT_CREATE
static const int CONTEXT_CREATE
Index numbers for pre-defined hooks.
Definition: server_hooks.h:66
isc::hooks::ServerHooks::getName
std::string getName(int index) const
Get hook name.
Definition: server_hooks.cc:124
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
library_handle.h
isc::hooks::ParkingLotHandlePtr
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:381
isc::hooks::CalloutHandlePtr
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Definition: callout_handle.h:437
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
server_hooks.h