Kea: callout_handle.h Source File

| Kea 3.0.3-git

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
10#include <exceptions/exceptions.h>
11#include <hooks/library_handle.h>
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
31 class NoSuchArgument : public Exception {
32public:
33 NoSuchArgument(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) {}
35};
36
42
43 class NoSuchCalloutContext : public Exception {
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
78 class CalloutHandle {
79public:
80
86 enum CalloutNextStep {
87 NEXT_STEP_CONTINUE = 0,
88 NEXT_STEP_SKIP = 1,
89 NEXT_STEP_DROP = 2,
90 NEXT_STEP_PARK = 3
91 };
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
137 ~CalloutHandle();
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
201 void deleteAllArguments() {
202 arguments_.clear();
203 }
204
231 void setStatus(const CalloutNextStep next) {
232 next_step_ = next;
233 }
234
241 CalloutNextStep getStatus() const {
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
346 void deleteAllContext() {
347 getContextForLibrary().clear();
348 }
349
357 std::string getHookName() const;
358
362 ParkingLotHandlePtr getParkingLotHandlePtr() const;
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
470 typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
471
510 class ScopedCalloutHandleState {
511public:
512
520 explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
521
525 ~ScopedCalloutHandleState();
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
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::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::hooks::CalloutHandle::setCurrentLibrary
void setCurrentLibrary(int library_index)
Set current library index.
Definition callout_handle.h:374
isc::hooks::CalloutHandle::deleteContext
void deleteContext(const std::string &name)
Delete context element.
Definition callout_handle.h:336
isc::hooks::CalloutHandle::getContext
void getContext(const std::string &name, T &value) const
Get context.
Definition callout_handle.h:271
isc::hooks::CalloutHandle::CalloutNextStep
CalloutNextStep
Specifies allowed next steps.
Definition callout_handle.h:86
isc::hooks::CalloutHandle::NEXT_STEP_PARK
@ NEXT_STEP_PARK
park the packet
Definition callout_handle.h:90
isc::hooks::CalloutHandle::NEXT_STEP_CONTINUE
@ NEXT_STEP_CONTINUE
continue normally
Definition callout_handle.h:87
isc::hooks::CalloutHandle::NEXT_STEP_DROP
@ NEXT_STEP_DROP
drop the packet
Definition callout_handle.h:89
isc::hooks::CalloutHandle::NEXT_STEP_SKIP
@ NEXT_STEP_SKIP
skip the next processing step
Definition callout_handle.h:88
isc::hooks::CalloutHandle::getParkingLotHandlePtr
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
Definition callout_handle.cc:75
isc::hooks::CalloutHandle::setContext
void setContext(const std::string &name, T value)
Set context.
Definition callout_handle.h:253
isc::hooks::CalloutHandle::getStatus
CalloutNextStep getStatus() const
Returns the next processing step.
Definition callout_handle.h:241
isc::hooks::CalloutHandle::getHookName
std::string getHookName() const
Get hook name.
Definition callout_handle.cc:125
isc::hooks::CalloutHandle::deleteAllArguments
void deleteAllArguments()
Delete all arguments.
Definition callout_handle.h:201
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::setStatus
void setStatus(const CalloutNextStep next)
Sets the next processing step.
Definition callout_handle.h:231
isc::hooks::CalloutHandle::~CalloutHandle
~CalloutHandle()
Destructor.
Definition callout_handle.cc:37
isc::hooks::CalloutHandle::setCurrentHook
void setCurrentHook(int hook_index)
Set current hook index.
Definition callout_handle.h:388
isc::hooks::CalloutHandle::deleteArgument
void deleteArgument(const std::string &name)
Delete argument.
Definition callout_handle.h:191
isc::hooks::CalloutHandle::getArgument
void getArgument(const std::string &name, T &value) const
Get argument.
Definition callout_handle.h:164
isc::hooks::CalloutHandle::deleteAllContext
void deleteAllContext()
Delete all context items.
Definition callout_handle.h:346
isc::hooks::CalloutHandle::getCurrentHook
int getCurrentHook() const
Get current hook index.
Definition callout_handle.h:381
isc::hooks::CalloutHandle::getCurrentLibrary
int getCurrentLibrary() const
Get current library index.
Definition callout_handle.h:367
isc::hooks::CalloutHandle::getContextNames
std::vector< std::string > getContextNames() const
Get context names.
Definition callout_handle.cc:110
isc::hooks::CalloutHandle::setArgument
void setArgument(const std::string &name, T value)
Set argument.
Definition callout_handle.h:147
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::CalloutHandle::getOptionalContext
bool getOptionalContext(const std::string &name, T &value) const
Fetch an optional named element from the current library context.
Definition callout_handle.h:300
isc::hooks::CalloutHandle::ContextCollection
std::map< int, ElementCollection > ContextCollection
Typedef to allow abbreviations in specifications when accessing context.
Definition callout_handle.h:109
isc::hooks::NoSuchArgument
No such argument.
Definition callout_handle.h:31
isc::hooks::NoSuchArgument::NoSuchArgument
NoSuchArgument(const char *file, size_t line, const char *what)
Definition callout_handle.h:33
isc::hooks::NoSuchCalloutContext
No such callout context item.
Definition callout_handle.h:43
isc::hooks::NoSuchCalloutContext::NoSuchCalloutContext
NoSuchCalloutContext(const char *file, size_t line, const char *what)
Definition callout_handle.h:45
isc::hooks::ScopedCalloutHandleState::ScopedCalloutHandleState
ScopedCalloutHandleState(const CalloutHandlePtr &callout_handle)
Constructor.
Definition callout_handle.cc:139
isc::hooks::ScopedCalloutHandleState::on_completion_
std::function< void()> on_completion_
Continuation callback.
Definition callout_handle.h:528
isc::hooks::ScopedCalloutHandleState::~ScopedCalloutHandleState
~ScopedCalloutHandleState()
Destructor.
Definition callout_handle.cc:148
isc::hooks::ServerHooks
Server hook collection.
Definition server_hooks.h:62
dhcp_space.h
exceptions.h
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
Definition callout_handle.cc:21
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:470
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
parking_lots.h