Kea 2.5.8
callout_handle.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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
12#include <hooks/server_hooks.h>
13
14#include <string>
15#include <utility>
16#include <vector>
17
18using namespace std;
19
20namespace isc {
21namespace 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
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>
66 vector<string> names;
67 for (auto const& i : arguments_) {
68 names.push_back(i.first);
69 }
70
71 return (names);
72}
73
76 return (boost::make_shared<ParkingLotHandle>(server_hooks_.getParkingLotPtr(current_hook_)));
77}
78
79// Return the context for the currently pointed-to library. This version is
80// used by the "setContext()" method and creates a context for the current
81// library if it does not exist.
82
84CalloutHandle::getContextForLibrary() {
85 // Access a reference to the element collection for the given index,
86 // creating a new element collection if necessary, and return it.
87 return (context_collection_[current_library_]);
88}
89
90// The "const" version of the above, used by the "getContext()" method. If
91// the context for the current library doesn't exist, throw an exception.
92
94CalloutHandle::getContextForLibrary() const {
95 auto libcontext = context_collection_.find(current_library_);
96 if (libcontext == context_collection_.end()) {
97 isc_throw(NoSuchCalloutContext, "unable to find callout context "
98 "associated with the current library index (" << current_library_ <<
99 ")");
100 }
101
102 // Return a reference to the context's element collection.
103 return (libcontext->second);
104}
105
106// Return the name of all items in the context associated with the current]
107// library.
108
109vector<string>
111 vector<string> names;
112 const ElementCollection& elements = getContextForLibrary();
113 for (auto const& i : elements) {
114 names.push_back(i.first);
115 }
116
117 return (names);
118}
119
120// Return name of current hook (the hook to which the current callout is
121// attached) or the empty string if not called within the context of a
122// callout.
123
124string
126 string hook = "";
127 try {
128 hook = server_hooks_.getName(current_hook_);
129 } catch (const NoSuchHook&) {
130 // Hook index is invalid, so this methods probably called from outside
131 // a callout being executed via a call to CalloutManager::callCallouts.
132 // In this case, the empty string is returned.
133 }
134
135 return (hook);
136}
137
139ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle)
140 : callout_handle_(callout_handle) {
141 if (!callout_handle_) {
142 isc_throw(BadValue, "callout_handle argument must not be null");
143 }
144
145 resetState();
146}
147
149 resetState();
150
151 if (on_completion_) {
153 }
154}
155
156void
157ScopedCalloutHandleState::resetState() {
158 // No need to check if the handle is null because the constructor
159 // already checked that.
160 callout_handle_->deleteAllArguments();
161 callout_handle_->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
162}
163
164} // namespace hooks
165} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
@ NEXT_STEP_CONTINUE
continue normally
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
std::string getHookName() const
Get hook name.
std::map< std::string, boost::any > ElementCollection
Typedef to allow abbreviation of iterator specification in methods.
std::vector< std::string > getContextNames() const
Get context names.
CalloutHandle(const boost::shared_ptr< CalloutManager > &manager, const boost::shared_ptr< LibraryManagerCollection > &lmcoll=boost::shared_ptr< LibraryManagerCollection >())
Constructor.
std::vector< std::string > getArgumentNames() const
Get argument names.
ScopedCalloutHandleState(const CalloutHandlePtr &callout_handle)
Constructor.
std::function< void()> on_completion_
Continuation callback.
Server hook collection.
Definition: server_hooks.h:62
ParkingLotPtr getParkingLotPtr(const int hook_index)
Returns pointer to the ParkingLot for the specified hook index.
static const int CONTEXT_DESTROY
Definition: server_hooks.h:67
static const int CONTEXT_CREATE
Index numbers for pre-defined hooks.
Definition: server_hooks.h:66
std::string getName(int index) const
Get hook name.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:381
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Defines the logger used by the top-level component of kea-lfc.