Kea 2.5.5
ifaces_config_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2023 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 if (element.first == "dhcp-socket-type") {
65 if (protocol_ == AF_INET) {
66 cfg->useSocketType(AF_INET, element.second->stringValue());
67 socket_type_specified = true;
68 continue;
69 } else {
71 "dhcp-socket-type is not supported in DHCPv6");
72 }
73 }
74
75 if (element.first == "outbound-interface") {
76 if (protocol_ == AF_INET) {
78 CfgIface::textToOutboundIface(element.second->stringValue());
79 cfg->setOutboundIface(type);
80 continue;
81 } else {
83 "outbound-interface is not supported in DHCPv6");
84 }
85 }
86
87 if (element.first == "service-sockets-require-all") {
88 cfg->setServiceSocketsRequireAll(element.second->boolValue());
89 continue;
90 }
91
92 if (element.first == "service-sockets-retry-wait-time") {
93 cfg->setServiceSocketsRetryWaitTime(static_cast<uint32_t>(element.second->intValue()));
94 continue;
95 }
96
97 if (element.first == "service-sockets-max-retries") {
98 cfg->setServiceSocketsMaxRetries(static_cast<uint32_t>(element.second->intValue()));
99 continue;
100 }
101
102 if (element.first == "user-context") {
103 cfg->setContext(element.second);
104 continue;
105 }
106
107 // This should never happen as the input produced by the parser
108 // see (src/bin/dhcpX/dhcpX_parser.yy) should not produce any
109 // other parameter, so this case is only to catch bugs in
110 // the parser.
111 isc_throw(DhcpConfigError, "unsupported parameter '"
112 << element.first << "'");
113 } catch (const std::exception& ex) {
114 // Append line number where the error occurred.
115 isc_throw(DhcpConfigError, ex.what() << " ("
116 << element.second->getPosition() << ")");
117 }
118 }
119
120 // User hasn't specified the socket type. Log that we are using
121 // the default type. Log it only if this is DHCPv4. (DHCPv6 does not use
122 // raw sockets).
123 if (!socket_type_specified && (protocol_ == AF_INET) ) {
125 .arg(cfg->socketTypeToText());
126 }
127}
128
129} // end of namespace isc::dhcp
130} // 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:143
static OutboundIface textToOutboundIface(const std::string &txt)
Converts text to outbound interface selection mode.
Definition: cfg_iface.cc:365
To be removed. Please use ConfigError instead.
void clearIfaces()
Removes detected interfaces.
Definition: iface_mgr.cc:888
void detectIfaces(bool update_only=false)
Detects network interfaces.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:54
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:29
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:501
const isc::log::MessageID DHCPSRV_CFGMGR_SOCKET_TYPE_DEFAULT
Defines the logger used by the top-level component of kea-lfc.