Kea 2.7.5
callout_handle.h
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#ifndef CALLOUT_HANDLE_H
8#define CALLOUT_HANDLE_H
9
12#include <hooks/parking_lots.h>
13#include <util/dhcp_space.h>
14
15#include <boost/any.hpp>
16#include <boost/shared_ptr.hpp>
17
18#include <map>
19#include <string>
20#include <vector>
21
22namespace isc {
23namespace hooks {
24
25class ServerHooks;
26
30
31class NoSuchArgument : public Exception {
32public:
33 NoSuchArgument(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) {}
35};
36
42
44public:
45 NoSuchCalloutContext(const char* file, size_t line, const char* what) :
46 isc::Exception(file, line, what) {}
47};
48
49// Forward declaration of the library handle and related collection classes.
50
51class CalloutManager;
52class LibraryManagerCollection;
53
77
79public:
80
92
93
97 typedef std::map<std::string, boost::any> ElementCollection;
98
109 typedef std::map<int, ElementCollection> ContextCollection;
110
129 CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
130 const boost::shared_ptr<LibraryManagerCollection>& lmcoll =
131 boost::shared_ptr<LibraryManagerCollection>());
132
138
146 template <typename T>
147 void setArgument(const std::string& name, T value) {
148 arguments_[name] = value;
149 }
150
163 template <typename T>
164 void getArgument(const std::string& name, T& value) const {
165 ElementCollection::const_iterator element_ptr = arguments_.find(name);
166 if (element_ptr == arguments_.end()) {
167 isc_throw(NoSuchArgument, "unable to find argument with name " <<
168 name);
169 }
170
171 value = boost::any_cast<T>(element_ptr->second);
172 }
173
180 std::vector<std::string> getArgumentNames() const;
181
191 void deleteArgument(const std::string& name) {
192 static_cast<void>(arguments_.erase(name));
193 }
194
202 arguments_.clear();
203 }
204
231 void setStatus(const CalloutNextStep next) {
232 next_step_ = next;
233 }
234
242 return (next_step_);
243 }
244
252 template <typename T>
253 void setContext(const std::string& name, T value) {
254 getContextForLibrary()[name] = value;
255 }
256
270 template <typename T>
271 void getContext(const std::string& name, T& value) const {
272 const ElementCollection& lib_context = getContextForLibrary();
273
274 ElementCollection::const_iterator element_ptr = lib_context.find(name);
275 if (element_ptr == lib_context.end()) {
276 isc_throw(NoSuchCalloutContext, "unable to find callout context "
277 "item " << name << " in the context associated with "
278 "current library");
279 }
280
281 value = boost::any_cast<T>(element_ptr->second);
282 }
283
299 template <typename T>
300 bool getOptionalContext(const std::string& name, T& value) const {
301 const auto context_iter = context_collection_.find(current_library_);
302 if (context_iter == context_collection_.end()) {
303 // No context, punt.
304 return (false);
305 }
306
307 auto lib_context = context_iter->second;
308 ElementCollection::const_iterator element_ptr = lib_context.find(name);
309 if (element_ptr == lib_context.end()) {
310 return (false);
311 }
312
313 value = boost::any_cast<T>(element_ptr->second);
314 return (true);
315 }
316
324 std::vector<std::string> getContextNames() const;
325
336 void deleteContext(const std::string& name) {
337 static_cast<void>(getContextForLibrary().erase(name));
338 }
339
347 getContextForLibrary().clear();
348 }
349
357 std::string getHookName() const;
358
363
367 int getCurrentLibrary() const {
368 return (current_library_);
369 }
370
374 void setCurrentLibrary(int library_index) {
375 current_library_ = library_index;
376 }
377
381 int getCurrentHook() const {
382 return (current_hook_);
383 }
384
388 void setCurrentHook(int hook_index) {
389 current_hook_ = hook_index;
390 }
391
392private:
393
403 int getLibraryIndex() const;
404
416 ElementCollection& getContextForLibrary();
417
429 const ElementCollection& getContextForLibrary() const;
430
431 // Member variables
432
435 boost::shared_ptr<LibraryManagerCollection> lm_collection_;
436
438 ElementCollection arguments_;
439
441 ContextCollection context_collection_;
442
444 boost::shared_ptr<CalloutManager> manager_;
445
449 ServerHooks& server_hooks_;
450
456 int current_library_;
457
463 int current_hook_;
464
466 CalloutNextStep next_step_;
467};
468
470typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
471
511public:
512
520 explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
521
526
528 std::function<void()> on_completion_;
529
530private:
531
535 void resetState();
536
538 CalloutHandlePtr callout_handle_;
539};
540
541} // namespace hooks
542} // namespace isc
543
544
545#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.
bool getOptionalContext(const std::string &name, T &value) const
Fetch an optional named element from the current library context.
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.
std::function< void()> on_completion_
Continuation callback.
Server hook collection.
#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.
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Defines the logger used by the top-level component of kea-lfc.