Kea 1.5.0
ifaces_config_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2018 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
37 : protocol_(protocol) {
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) {
48 // Interface clear will drop opened socket information
49 // so close them if the caller did not.
53 }
54
55 bool socket_type_specified = false;
56 BOOST_FOREACH(ConfigPair element, ifaces_config->mapValue()) {
57 try {
58 if (element.first == "re-detect") {
59 continue;
60 }
61
62 if (element.first == "interfaces") {
63 parseInterfacesList(cfg, element.second);
64 continue;
65
66 }
67
68 if (element.first == "dhcp-socket-type") {
69 if (protocol_ == AF_INET) {
70 cfg->useSocketType(AF_INET, element.second->stringValue());
71 socket_type_specified = true;
72 continue;
73 } else {
75 "dhcp-socket-type is not supported in DHCPv6");
76 }
77 }
78
79 if (element.first == "outbound-interface") {
80 if (protocol_ == AF_INET) {
82 CfgIface::textToOutboundIface(element.second->stringValue());
83 cfg->setOutboundIface(type);
84 continue;
85 } else {
87 "outbound-interface is not supported in DHCPv6");
88 }
89 }
90
91 if (element.first == "user-context") {
92 cfg->setContext(element.second);
93 continue;
94 }
95
96 // This should never happen as the input produced by the parser
97 // see (src/bin/dhcpX/dhcpX_parser.yy) should not produce any
98 // other parameter, so this case is only to catch bugs in
99 // the parser.
100 isc_throw(DhcpConfigError, "unsupported parameter '"
101 << element.first << "'");
102 } catch (const std::exception& ex) {
103 // Append line number where the error occurred.
104 isc_throw(DhcpConfigError, ex.what() << " ("
105 << element.second->getPosition() << ")");
106 }
107 }
108
109 // User hasn't specified the socket type. Log that we are using
110 // the default type. Log it only if this is DHCPv4. (DHCPv6 does not use
111 // raw sockets).
112 if (!socket_type_specified && (protocol_ == AF_INET) ) {
113 LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_SOCKET_TYPE_DEFAULT)
114 .arg(cfg->socketTypeToText());
115 }
116}
117
118} // end of namespace isc::dhcp
119} // 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:273
To be removed. Please use ConfigError instead.
void clearIfaces()
Removes detected interfaces.
Definition: iface_mgr.cc:772
void detectIfaces()
Detects network interfaces.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:53
void closeSockets()
Closes all open sockets.
Definition: iface_mgr.cc:282
IfacesConfigParser(const uint16_t protocol)
Constructor.
void parse(const CfgIfacePtr &config, const isc::data::ConstElementPtr &values)
Parses content of the "interfaces-config".
#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:23
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:173
boost::shared_ptr< CfgIface > CfgIfacePtr
A pointer to the CfgIface .
Definition: cfg_iface.h:387
Defines the logger used by the top-level component of kea-dhcp-ddns.