Kea  2.1.7-git
ifaces_config_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-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 #include <cc/data.h>
9 #include <dhcpsrv/cfgmgr.h>
10 #include <dhcpsrv/dhcpsrv_log.h>
12 #include <boost/foreach.hpp>
13 #include <string>
14 #include <sys/types.h>
15 
16 using namespace isc::data;
17 
18 namespace isc {
19 namespace dhcp {
20 
21 void
22 IfacesConfigParser::parseInterfacesList(const CfgIfacePtr& cfg_iface,
23  ConstElementPtr ifaces_list) {
24  BOOST_FOREACH(ConstElementPtr iface, ifaces_list->listValue()) {
25  std::string iface_name = iface->stringValue();
26  try {
27  cfg_iface->use(protocol_, iface_name);
28 
29  } catch (const std::exception& ex) {
30  isc_throw(DhcpConfigError, "Failed to select interface: "
31  << ex.what() << " (" << iface->getPosition() << ")");
32  }
33  }
34 }
35 
36 IfacesConfigParser::IfacesConfigParser(const uint16_t protocol, bool test_mode)
37  : protocol_(protocol), test_mode_(test_mode) {
38 }
39 
40 void
42  const isc::data::ConstElementPtr& ifaces_config) {
43 
44  // Check for re-detect before calling parseInterfacesList()
45  bool re_detect = getBoolean(ifaces_config, "re-detect");
46  cfg->setReDetect(re_detect);
47  if (re_detect && !test_mode_) {
50  }
51 
52  bool socket_type_specified = false;
53  BOOST_FOREACH(ConfigPair element, ifaces_config->mapValue()) {
54  try {
55  if (element.first == "re-detect") {
56  continue;
57  }
58 
59  if (element.first == "interfaces") {
60  parseInterfacesList(cfg, element.second);
61  continue;
62 
63  }
64 
65  if (element.first == "dhcp-socket-type") {
66  if (protocol_ == AF_INET) {
67  cfg->useSocketType(AF_INET, element.second->stringValue());
68  socket_type_specified = true;
69  continue;
70  } else {
72  "dhcp-socket-type is not supported in DHCPv6");
73  }
74  }
75 
76  if (element.first == "outbound-interface") {
77  if (protocol_ == AF_INET) {
79  CfgIface::textToOutboundIface(element.second->stringValue());
80  cfg->setOutboundIface(type);
81  continue;
82  } else {
84  "outbound-interface is not supported in DHCPv6");
85  }
86  }
87 
88  if (element.first == "service-sockets-require-all") {
89  cfg->setServiceSocketsRequireAll(element.second->boolValue());
90  continue;
91  }
92 
93  if (element.first == "service-sockets-retry-wait-time") {
94  cfg->setServiceSocketsRetryWaitTime(static_cast<uint32_t>(element.second->intValue()));
95  continue;
96  }
97 
98  if (element.first == "service-sockets-max-retries") {
99  cfg->setServiceSocketsMaxRetries(static_cast<uint32_t>(element.second->intValue()));
100  continue;
101  }
102 
103  if (element.first == "user-context") {
104  cfg->setContext(element.second);
105  continue;
106  }
107 
108  // This should never happen as the input produced by the parser
109  // see (src/bin/dhcpX/dhcpX_parser.yy) should not produce any
110  // other parameter, so this case is only to catch bugs in
111  // the parser.
112  isc_throw(DhcpConfigError, "unsupported parameter '"
113  << element.first << "'");
114  } catch (const std::exception& ex) {
115  // Append line number where the error occurred.
116  isc_throw(DhcpConfigError, ex.what() << " ("
117  << element.second->getPosition() << ")");
118  }
119  }
120 
121  // User hasn't specified the socket type. Log that we are using
122  // the default type. Log it only if this is DHCPv4. (DHCPv6 does not use
123  // raw sockets).
124  if (!socket_type_specified && (protocol_ == AF_INET) ) {
126  .arg(cfg->socketTypeToText());
127  }
128 }
129 
130 } // end of namespace isc::dhcp
131 } // end of namespace isc
void clearIfaces()
Removes detected interfaces.
Definition: iface_mgr.cc:919
std::pair< std::string, isc::data::ConstElementPtr > ConfigPair
Combination of parameter name and configuration contents.
Definition: dhcp_parsers.h:175
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
static OutboundIface textToOutboundIface(const std::string &txt)
Converts text to outbound interface selection mode.
Definition: cfg_iface.cc:365
boost::shared_ptr< CfgIface > CfgIfacePtr
A pointer to the CfgIface .
Definition: cfg_iface.h:489
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
To be removed. Please use ConfigError instead.
void detectIfaces(bool update_only=false)
Detects network interfaces.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
void parse(const CfgIfacePtr &config, const isc::data::ConstElementPtr &values)
Parses content of the "interfaces-config".
Defines the logger used by the top-level component of kea-lfc.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:53
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_CFGMGR_SOCKET_TYPE_DEFAULT
OutboundIface
Indicates how outbound interface is selected for relayed traffic.
Definition: cfg_iface.h:143