Kea  2.3.8
callout_handle.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2022 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 
22 namespace isc {
23 namespace hooks {
24 
25 class ServerHooks;
26 
30 
31 class NoSuchArgument : public Exception {
32 public:
33  NoSuchArgument(const char* file, size_t line, const char* what) :
34  isc::Exception(file, line, what) {}
35 };
36 
42 
44 public:
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 
51 class CalloutManager;
52 class LibraryManagerCollection;
53 
77 
79 public:
80 
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 
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 
291  std::vector<std::string> getContextNames() const;
292 
303  void deleteContext(const std::string& name) {
304  static_cast<void>(getContextForLibrary().erase(name));
305  }
306 
314  getContextForLibrary().clear();
315  }
316 
324  std::string getHookName() const;
325 
330 
334  int getCurrentLibrary() const {
335  return (current_library_);
336  }
337 
341  void setCurrentLibrary(int library_index) {
342  current_library_ = library_index;
343  }
344 
348  int getCurrentHook() const {
349  return (current_hook_);
350  }
351 
355  void setCurrentHook(int hook_index) {
356  current_hook_ = hook_index;
357  }
358 
359 private:
360 
370  int getLibraryIndex() const;
371 
383  ElementCollection& getContextForLibrary();
384 
396  const ElementCollection& getContextForLibrary() const;
397 
398  // Member variables
399 
402  boost::shared_ptr<LibraryManagerCollection> lm_collection_;
403 
405  ElementCollection arguments_;
406 
408  ContextCollection context_collection_;
409 
411  boost::shared_ptr<CalloutManager> manager_;
412 
416  ServerHooks& server_hooks_;
417 
423  int current_library_;
424 
430  int current_hook_;
431 
433  CalloutNextStep next_step_;
434 };
435 
437 typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
438 
478 public:
479 
487  explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
488 
493 
495  std::function<void()> on_completion_;
496 
497 private:
498 
502  void resetState();
503 
505  CalloutHandlePtr callout_handle_;
506 };
507 
508 } // namespace hooks
509 } // namespace isc
510 
511 
512 #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.
std::function< void()> on_completion_
Continuation callback.
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.