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