ifaces_config_parser.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2015-2017 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. #include <config.h>
  7. #include <cc/data.h>
  8. #include <dhcpsrv/cfgmgr.h>
  9. #include <dhcpsrv/dhcpsrv_log.h>
  10. #include <dhcpsrv/parsers/ifaces_config_parser.h>
  11. #include <boost/foreach.hpp>
  12. #include <string>
  13. #include <sys/types.h>
  14. using namespace isc::data;
  15. namespace isc {
  16. namespace dhcp {
  17. void
  18. IfacesConfigParser::parseInterfacesList(const CfgIfacePtr& cfg_iface,
  19. ConstElementPtr ifaces_list) {
  20. BOOST_FOREACH(ConstElementPtr iface, ifaces_list->listValue()) {
  21. std::string iface_name = iface->stringValue();
  22. try {
  23. cfg_iface->use(protocol_, iface_name);
  24. } catch (const std::exception& ex) {
  25. isc_throw(DhcpConfigError, "Failed to select interface: "
  26. << ex.what() << " (" << iface->getPosition() << ")");
  27. }
  28. }
  29. }
  30. IfacesConfigParser::IfacesConfigParser(const uint16_t protocol)
  31. : protocol_(protocol) {
  32. }
  33. void
  34. IfacesConfigParser::parse(const CfgIfacePtr& cfg,
  35. const isc::data::ConstElementPtr& ifaces_config) {
  36. // Check for re-detect before calling parseInterfacesList()
  37. bool re_detect = getBoolean(ifaces_config, "re-detect");
  38. cfg->setReDetect(re_detect);
  39. if (re_detect) {
  40. // Interface clear will drop opened socket information
  41. // so close them if the caller did not.
  42. IfaceMgr::instance().closeSockets();
  43. IfaceMgr::instance().clearIfaces();
  44. IfaceMgr::instance().detectIfaces();
  45. }
  46. bool socket_type_specified = false;
  47. BOOST_FOREACH(ConfigPair element, ifaces_config->mapValue()) {
  48. try {
  49. if (element.first == "re-detect") {
  50. continue;
  51. }
  52. if (element.first == "interfaces") {
  53. parseInterfacesList(cfg, element.second);
  54. continue;
  55. }
  56. if (element.first == "dhcp-socket-type") {
  57. if (protocol_ == AF_INET) {
  58. cfg->useSocketType(AF_INET, element.second->stringValue());
  59. socket_type_specified = true;
  60. continue;
  61. } else {
  62. isc_throw(DhcpConfigError,
  63. "dhcp-socket-type is not supported in DHCPv6");
  64. }
  65. }
  66. if (element.first == "outbound-interface") {
  67. if (protocol_ == AF_INET) {
  68. CfgIface::OutboundIface type =
  69. CfgIface::textToOutboundIface(element.second->stringValue());
  70. cfg->setOutboundIface(type);
  71. continue;
  72. } else {
  73. isc_throw(DhcpConfigError,
  74. "outbound-interface is not supported in DHCPv6");
  75. }
  76. }
  77. // This should never happen as the input produced by the parser
  78. // see (src/bin/dhcpX/dhcpX_parser.yy) should not produce any
  79. // other parameter, so this case is only to catch bugs in
  80. // the parser.
  81. isc_throw(DhcpConfigError, "unsupported parameter '"
  82. << element.first << "'");
  83. } catch (const std::exception& ex) {
  84. // Append line number where the error occurred.
  85. isc_throw(DhcpConfigError, ex.what() << " ("
  86. << element.second->getPosition() << ")");
  87. }
  88. }
  89. // User hasn't specified the socket type. Log that we are using
  90. // the default type. Log it only if this is DHCPv4. (DHCPv6 does not use
  91. // raw sockets).
  92. if (!socket_type_specified && (protocol_ == AF_INET) ) {
  93. LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_SOCKET_TYPE_DEFAULT)
  94. .arg(cfg->socketTypeToText());
  95. }
  96. }
  97. } // end of namespace isc::dhcp
  98. } // end of namespace isc