Kea 2.5.8
ifaces_config_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2024 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 <string>
13#include <sys/types.h>
14
15using namespace isc::data;
16
17namespace isc {
18namespace dhcp {
19
20void
21IfacesConfigParser::parseInterfacesList(const CfgIfacePtr& cfg_iface,
22 ConstElementPtr ifaces_list) {
23 for (auto const& iface : ifaces_list->listValue()) {
24 std::string iface_name = iface->stringValue();
25 try {
26 cfg_iface->use(protocol_, iface_name);
27
28 } catch (const std::exception& ex) {
29 isc_throw(DhcpConfigError, "Failed to select interface: "
30 << ex.what() << " (" << iface->getPosition() << ")");
31 }
32 }
33}
34
35IfacesConfigParser::IfacesConfigParser(const uint16_t protocol, bool test_mode)
36 : protocol_(protocol), test_mode_(test_mode) {
37}
38
39void
41 const isc::data::ConstElementPtr& ifaces_config) {
42
43 // Check for re-detect before calling parseInterfacesList()
44 bool re_detect = getBoolean(ifaces_config, "re-detect");
45 cfg->setReDetect(re_detect);
46 if (re_detect && !test_mode_) {
49 }
50
51 bool socket_type_specified = false;
52 for (auto const& element : ifaces_config->mapValue()) {
53 try {
54 if (element.first == "re-detect") {
55 continue;
56 }
57
58 if (element.first == "interfaces") {
59 parseInterfacesList(cfg, element.second);
60 continue;
61 }
62
63 if (element.first == "dhcp-socket-type") {
64 if (protocol_ == AF_INET) {
65 cfg->useSocketType(AF_INET, element.second->stringValue());
66 socket_type_specified = true;
67 continue;
68 } else {
70 "dhcp-socket-type is not supported in DHCPv6");
71 }
72 }
73
74 if (element.first == "outbound-interface") {
75 if (protocol_ == AF_INET) {
77 CfgIface::textToOutboundIface(element.second->stringValue());
78 cfg->setOutboundIface(type);
79 continue;
80 } else {
82 "outbound-interface is not supported in DHCPv6");
83 }
84 }
85
86 if (element.first == "service-sockets-require-all") {
87 cfg->setServiceSocketsRequireAll(element.second->boolValue());
88 continue;
89 }
90
91 if (element.first == "service-sockets-retry-wait-time") {
92 cfg->setServiceSocketsRetryWaitTime(static_cast<uint32_t>(element.second->intValue()));
93 continue;
94 }
95
96 if (element.first == "service-sockets-max-retries") {
97 cfg->setServiceSocketsMaxRetries(static_cast<uint32_t>(element.second->intValue()));
98 continue;
99 }
100
101 if (element.first == "user-context") {
102 cfg->setContext(element.second);
103 continue;
104 }
105
106 // This should never happen as the input produced by the parser
107 // see (src/bin/dhcpX/dhcpX_parser.yy) should not produce any
108 // other parameter, so this case is only to catch bugs in
109 // the parser.
110 isc_throw(DhcpConfigError, "unsupported parameter '"
111 << element.first << "'");
112 } catch (const std::exception& ex) {
113 // Append line number where the error occurred.
114 isc_throw(DhcpConfigError, ex.what() << " ("
115 << element.second->getPosition() << ")");
116 }
117 }
118
119 // User hasn't specified the socket type. Log that we are using
120 // the default type. Log it only if this is DHCPv4. (DHCPv6 does not use
121 // raw sockets).
122 if (!socket_type_specified && (protocol_ == AF_INET) ) {
124 .arg(cfg->socketTypeToText());
125 }
126}
127
128} // end of namespace isc::dhcp
129} // 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:361
To be removed. Please use ConfigError instead.
void clearIfaces()
Removes detected interfaces.
Definition: iface_mgr.cc:901
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
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.