Kea 3.3.3
callout_handle.h
Go to the documentation of this file.
1// Copyright (C) 2013-2026 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 <cstdint>
16#include <map>
17#include <string>
18#include <vector>
19
20#include <boost/any.hpp>
21#include <boost/shared_ptr.hpp>
22
23namespace isc {
24namespace hooks {
25
26class ServerHooks;
27
31
32class NoSuchArgument : public Exception {
33public:
34 NoSuchArgument(const char* file, size_t line, const char* what) :
35 isc::Exception(file, line, what) {}
36};
37
43
45public:
46 NoSuchCalloutContext(const char* file, size_t line, const char* what) :
47 isc::Exception(file, line, what) {}
48};
49
50// Forward declaration of the library handle and related collection classes.
51
52class CalloutManager;
53class LibraryManagerCollection;
54
78
80public:
81
93
94
98 typedef std::map<std::string, boost::any> ElementCollection;
99
110 typedef std::map<int, ElementCollection> ContextCollection;
111
130 CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
131 const boost::shared_ptr<LibraryManagerCollection>& lmcoll =
132 boost::shared_ptr<LibraryManagerCollection>());
133
139
147 template <typename T>
148 void setArgument(const std::string& name, T value) {
149 arguments_[name] = value;
150 }
151
164 template <typename T>
165 void getArgument(const std::string& name, T& value) const {
166 ElementCollection::const_iterator element_ptr = arguments_.find(name);
167 if (element_ptr == arguments_.end()) {
168 isc_throw(NoSuchArgument, "unable to find argument with name " <<
169 name);
170 }
171
172 value = boost::any_cast<T>(element_ptr->second);
173 }
174
181 std::vector<std::string> getArgumentNames() const;
182
192 void deleteArgument(const std::string& name) {
193 static_cast<void>(arguments_.erase(name));
194 }
195
203 arguments_.clear();
204 }
205
232 void setStatus(const CalloutNextStep next) {
233 next_step_ = next;
234 }
235
243 return (next_step_);
244 }
245
253 template <typename T>
254 void setContext(const std::string& name, T value) {
255 getContextForLibrary()[name] = value;
256 }
257
271 template <typename T>
272 void getContext(const std::string& name, T& value) const {
273 const ElementCollection& lib_context = getContextForLibrary();
274
275 ElementCollection::const_iterator element_ptr = lib_context.find(name);
276 if (element_ptr == lib_context.end()) {
277 isc_throw(NoSuchCalloutContext, "unable to find callout context "
278 "item " << name << " in the context associated with "
279 "current library");
280 }
281
282 value = boost::any_cast<T>(element_ptr->second);
283 }
284
300 template <typename T>
301 bool getOptionalContext(const std::string& name, T& value) const {
302 const auto context_iter = context_collection_.find(current_library_);
303 if (context_iter == context_collection_.end()) {
304 // No context, punt.
305 return (false);
306 }
307
308 auto lib_context = context_iter->second;
309 ElementCollection::const_iterator element_ptr = lib_context.find(name);
310 if (element_ptr == lib_context.end()) {
311 return (false);
312 }
313
314 value = boost::any_cast<T>(element_ptr->second);
315 return (true);
316 }
317
325 std::vector<std::string> getContextNames() const;
326
337 void deleteContext(const std::string& name) {
338 static_cast<void>(getContextForLibrary().erase(name));
339 }
340
348 getContextForLibrary().clear();
349 }
350
358 std::string getHookName() const;
359
364
368 int getCurrentLibrary() const {
369 return (current_library_);
370 }
371
375 void setCurrentLibrary(int library_index) {
376 current_library_ = library_index;
377 }
378
382 int getCurrentHook() const {
383 return (current_hook_);
384 }
385
389 void setCurrentHook(int hook_index) {
390 current_hook_ = hook_index;
391 }
392
393private:
394
404 int getLibraryIndex() const;
405
417 ElementCollection& getContextForLibrary();
418
430 const ElementCollection& getContextForLibrary() const;
431
432 // Member variables
433
436 boost::shared_ptr<LibraryManagerCollection> lm_collection_;
437
439 ElementCollection arguments_;
440
442 ContextCollection context_collection_;
443
445 boost::shared_ptr<CalloutManager> manager_;
446
450 ServerHooks& server_hooks_;
451
457 int current_library_;
458
464 int current_hook_;
465
467 CalloutNextStep next_step_;
468};
469
471typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
472
512public:
513
521 explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
522
527
529 std::function<void()> on_completion_;
530
531private:
532
536 void resetState();
537
539 CalloutHandlePtr callout_handle_;
540};
541
542} // namespace hooks
543} // namespace isc
544
545
546#endif // CALLOUT_HANDLE_H
CalloutHandle(const boost::shared_ptr< CalloutManager > &manager, const boost::shared_ptr< LibraryManagerCollection > &lmcoll=boost::shared_ptr< LibraryManagerCollection >())
Constructor.
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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
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.
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)
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.