Kea  2.1.6-git
flex_option_callouts.cc
Go to the documentation of this file.
1 // Copyright (C) 2019-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 #include <config.h>
8 
9 #include <flex_option.h>
10 #include <flex_option_log.h>
11 #include <cc/command_interpreter.h>
12 #include <hooks/hooks.h>
13 #include <dhcp/pkt4.h>
14 #include <dhcp/pkt6.h>
15 
16 namespace isc {
17 namespace flex_option {
18 
20 
21 } // end of namespace flex_option
22 } // end of namespace isc
23 
24 using namespace isc;
25 using namespace isc::data;
26 using namespace isc::dhcp;
27 using namespace isc::hooks;
28 using namespace isc::flex_option;
29 
30 // Functions accessed by the hooks framework use C linkage to avoid the name
31 // mangling that accompanies use of the C++ compiler as well as to avoid
32 // issues related to namespaces.
33 extern "C" {
34 
44 int pkt4_send(CalloutHandle& handle) {
45  CalloutHandle::CalloutNextStep status = handle.getStatus();
46  if (status == CalloutHandle::NEXT_STEP_DROP) {
47  return (0);
48  }
49 
50  // Sanity.
51  if (!impl) {
52  return (0);
53  }
54 
55  // Get the parameters.
56  Pkt4Ptr query;
57  Pkt4Ptr response;
58  handle.getArgument("query4", query);
59  handle.getArgument("response4", response);
60 
61  if (status == CalloutHandle::NEXT_STEP_SKIP) {
62  isc_throw(InvalidOperation, "packet pack already handled");
63  }
64 
65  try {
66  impl->process<Pkt4Ptr>(Option::V4, query, response);
67  } catch (const std::exception& ex) {
69  .arg(query->getLabel())
70  .arg(ex.what());
71  }
72 
73  return (0);
74 }
75 
85 int pkt6_send(CalloutHandle& handle) {
86  CalloutHandle::CalloutNextStep status = handle.getStatus();
87  if (status == CalloutHandle::NEXT_STEP_DROP) {
88  return (0);
89  }
90 
91  // Sanity.
92  if (!impl) {
93  return (0);
94  }
95 
96  if (status == CalloutHandle::NEXT_STEP_SKIP) {
97  isc_throw(InvalidOperation, "packet pack already handled");
98  }
99 
100  // Get the parameters.
101  Pkt6Ptr query;
102  Pkt6Ptr response;
103  handle.getArgument("query6", query);
104  handle.getArgument("response6", response);
105 
106  try {
107  impl->process<Pkt6Ptr>(Option::V6, query, response);
108  } catch (const std::exception& ex) {
110  .arg(query->getLabel())
111  .arg(ex.what());
112  }
113 
114  return (0);
115 }
116 
121 int load(LibraryHandle& handle) {
122  try {
123  impl.reset(new FlexOptionImpl());
124  ConstElementPtr options = handle.getParameter("options");
125  impl->configure(options);
126  } catch (const std::exception& ex) {
128  .arg(ex.what());
129  return (1);
130  }
131 
132  return (0);
133 }
134 
138 int unload() {
139  impl.reset();
141  return (0);
142 }
143 
148  return (1);
149 }
150 
151 } // end extern "C"
int load(LibraryHandle &handle)
This function is called when the library is loaded.
boost::shared_ptr< FlexOptionImpl > FlexOptionImplPtr
The type of shared pointers to Flex Option implementations.
Definition: flex_option.h:645
const isc::log::MessageID FLEX_OPTION_LOAD_ERROR
int unload()
This function is called when the library is unloaded.
isc::log::Logger flex_option_logger("flex-option-hooks")
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
CalloutNextStep getStatus() const
Returns the next processing step.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
int pkt4_send(CalloutHandle &handle)
This callout is called at the "pkt4_send" hook.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
Per-packet callout handle.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
CalloutNextStep
Specifies allowed next steps.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Defines the logger used by the top-level component of kea-lfc.
void getArgument(const std::string &name, T &value) const
Get argument.
This file contains several functions and constants that are used for handling commands and responses ...
const isc::log::MessageID FLEX_OPTION_UNLOAD
FlexOptionImplPtr impl
A generic exception that is thrown if a function is called in a prohibited way.
isc::data::ConstElementPtr getParameter(const std::string &name)
Returns configuration parameter for the library.
Flex Option implementation.
Definition: flex_option.h:37
int pkt6_send(CalloutHandle &handle)
This callout is called at the "pkt6_send" hook.
const isc::log::MessageID FLEX_OPTION_PROCESS_ERROR
int multi_threading_compatible()
This function is called to retrieve the multi-threading compatibility.