Kea 2.0.2
ifaces_config_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2020 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
16using namespace isc::data;
17
18namespace isc {
19namespace dhcp {
20
21void
22IfacesConfigParser::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
36IfacesConfigParser::IfacesConfigParser(const uint16_t protocol, bool test_mode)
37 : protocol_(protocol), test_mode_(test_mode) {
38}
39
40void
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 == "user-context") {
89 cfg->setContext(element.second);
90 continue;
91 }
92
93 // This should never happen as the input produced by the parser
94 // see (src/bin/dhcpX/dhcpX_parser.yy) should not produce any
95 // other parameter, so this case is only to catch bugs in
96 // the parser.
97 isc_throw(DhcpConfigError, "unsupported parameter '"
98 << element.first << "'");
99 } catch (const std::exception& ex) {
100 // Append line number where the error occurred.
101 isc_throw(DhcpConfigError, ex.what() << " ("
102 << element.second->getPosition() << ")");
103 }
104 }
105
106 // User hasn't specified the socket type. Log that we are using
107 // the default type. Log it only if this is DHCPv4. (DHCPv6 does not use
108 // raw sockets).
109 if (!socket_type_specified && (protocol_ == AF_INET) ) {
111 .arg(cfg->socketTypeToText());
112 }
113}
114
115} // end of namespace isc::dhcp
116} // end of namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
OutboundIface
Indicates how outbound interface is selected for relayed traffic.
Definition: cfg_iface.h:142
static OutboundIface textToOutboundIface(const std::string &txt)
Converts text to outbound interface selection mode.
Definition: cfg_iface.cc:271
To be removed. Please use ConfigError instead.
void clearIfaces()
Removes detected interfaces.
Definition: iface_mgr.cc:900
void detectIfaces()
Detects network interfaces.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:53
void parse(const CfgIfacePtr &config, const isc::data::ConstElementPtr &values)
Parses content of the "interfaces-config".
IfacesConfigParser(const uint16_t protocol, bool test_mode)
Constructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
std::pair< std::string, isc::data::ConstElementPtr > ConfigPair
Combination of parameter name and configuration contents.
Definition: dhcp_parsers.h:175
boost::shared_ptr< CfgIface > CfgIfacePtr
A pointer to the CfgIface .
Definition: cfg_iface.h:387
const isc::log::MessageID DHCPSRV_CFGMGR_SOCKET_TYPE_DEFAULT
Defines the logger used by the top-level component of kea-lfc.