Kea 2.0.0
callout_handle.h
Go to the documentation of this file.
1// Copyright (C) 2013-2020 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#ifndef CALLOUT_HANDLE_H
8#define CALLOUT_HANDLE_H
9
12#include <hooks/parking_lots.h>
13
14#include <boost/any.hpp>
15#include <boost/shared_ptr.hpp>
16
17#include <map>
18#include <string>
19#include <vector>
20
21namespace isc {
22namespace hooks {
23
24class ServerHooks;
25
29
30class NoSuchArgument : public Exception {
31public:
32 NoSuchArgument(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) {}
34};
35
41
43public:
44 NoSuchCalloutContext(const char* file, size_t line, const char* what) :
45 isc::Exception(file, line, what) {}
46};
47
48// Forward declaration of the library handle and related collection classes.
49
50class CalloutManager;
51class LibraryManagerCollection;
52
76
78public:
79
90 };
91
92
96 typedef std::map<std::string, boost::any> ElementCollection;
97
108 typedef std::map<int, ElementCollection> ContextCollection;
109
128 CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
129 const boost::shared_ptr<LibraryManagerCollection>& lmcoll =
130 boost::shared_ptr<LibraryManagerCollection>());
131
137
145 template <typename T>
146 void setArgument(const std::string& name, T value) {
147 arguments_[name] = value;
148 }
149
162 template <typename T>
163 void getArgument(const std::string& name, T& value) const {
164 ElementCollection::const_iterator element_ptr = arguments_.find(name);
165 if (element_ptr == arguments_.end()) {
166 isc_throw(NoSuchArgument, "unable to find argument with name " <<
167 name);
168 }
169
170 value = boost::any_cast<T>(element_ptr->second);
171 }
172
179 std::vector<std::string> getArgumentNames() const;
180
190 void deleteArgument(const std::string& name) {
191 static_cast<void>(arguments_.erase(name));
192 }
193
201 arguments_.clear();
202 }
203
230 void setStatus(const CalloutNextStep next) {
231 next_step_ = next;
232 }
233
241 return (next_step_);
242 }
243
251 template <typename T>
252 void setContext(const std::string& name, T value) {
253 getContextForLibrary()[name] = value;
254 }
255
269 template <typename T>
270 void getContext(const std::string& name, T& value) const {
271 const ElementCollection& lib_context = getContextForLibrary();
272
273 ElementCollection::const_iterator element_ptr = lib_context.find(name);
274 if (element_ptr == lib_context.end()) {
275 isc_throw(NoSuchCalloutContext, "unable to find callout context "
276 "item " << name << " in the context associated with "
277 "current library");
278 }
279
280 value = boost::any_cast<T>(element_ptr->second);
281 }
282
290 std::vector<std::string> getContextNames() const;
291
302 void deleteContext(const std::string& name) {
303 static_cast<void>(getContextForLibrary().erase(name));
304 }
305
313 getContextForLibrary().clear();
314 }
315
323 std::string getHookName() const;
324
329
333 int getCurrentLibrary() const {
334 return (current_library_);
335 }
336
340 void setCurrentLibrary(int library_index) {
341 current_library_ = library_index;
342 }
343
347 int getCurrentHook() const {
348 return (current_hook_);
349 }
350
354 void setCurrentHook(int hook_index) {
355 current_hook_ = hook_index;
356 }
357
358private:
359
369 int getLibraryIndex() const;
370
382 ElementCollection& getContextForLibrary();
383
395 const ElementCollection& getContextForLibrary() const;
396
397 // Member variables
398
401 boost::shared_ptr<LibraryManagerCollection> lm_collection_;
402
404 ElementCollection arguments_;
405
407 ContextCollection context_collection_;
408
410 boost::shared_ptr<CalloutManager> manager_;
411
415 ServerHooks& server_hooks_;
416
422 int current_library_;
423
429 int current_hook_;
430
432 CalloutNextStep next_step_;
433};
434
436typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
437
477public:
478
486 explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
487
492
493private:
494
498 void resetState();
499
501 CalloutHandlePtr callout_handle_;
502};
503
504} // namespace hooks
505} // namespace isc
506
507
508#endif // CALLOUT_HANDLE_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Per-packet callout handle.
void setCurrentLibrary(int library_index)
Set current library index.
void deleteContext(const std::string &name)
Delete context element.
void getContext(const std::string &name, T &value) const
Get context.
CalloutNextStep
Specifies allowed next steps.
@ NEXT_STEP_PARK
park the packet
@ NEXT_STEP_CONTINUE
continue normally
@ NEXT_STEP_DROP
drop the packet
@ NEXT_STEP_SKIP
skip the next processing step
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
void setContext(const std::string &name, T value)
Set context.
CalloutNextStep getStatus() const
Returns the next processing step.
std::string getHookName() const
Get hook name.
void deleteAllArguments()
Delete all arguments.
std::map< std::string, boost::any > ElementCollection
Typedef to allow abbreviation of iterator specification in methods.
void setStatus(const CalloutNextStep next)
Sets the next processing step.
void setCurrentHook(int hook_index)
Set current hook index.
void deleteArgument(const std::string &name)
Delete argument.
void getArgument(const std::string &name, T &value) const
Get argument.
void deleteAllContext()
Delete all context items.
int getCurrentHook() const
Get current hook index.
int getCurrentLibrary() const
Get current library index.
std::vector< std::string > getContextNames() const
Get context names.
void setArgument(const std::string &name, T value)
Set argument.
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.
std::map< int, ElementCollection > ContextCollection
Typedef to allow abbreviations in specifications when accessing context.
NoSuchArgument(const char *file, size_t line, const char *what)
No such callout context item.
NoSuchCalloutContext(const char *file, size_t line, const char *what)
Wrapper class around callout handle which automatically resets handle's state.
ScopedCalloutHandleState(const CalloutHandlePtr &callout_handle)
Constructor.
Server hook collection.
Definition: server_hooks.h:62
#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.