ifaces_config_parser.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cc/data.h>
  15. #include <dhcpsrv/cfgmgr.h>
  16. #include <dhcpsrv/parsers/ifaces_config_parser.h>
  17. #include <boost/foreach.hpp>
  18. #include <string>
  19. #include <sys/types.h>
  20. using namespace isc::data;
  21. namespace isc {
  22. namespace dhcp {
  23. InterfaceListConfigParser::InterfaceListConfigParser(const int protocol)
  24. : protocol_(protocol) {
  25. }
  26. void
  27. InterfaceListConfigParser::build(ConstElementPtr value) {
  28. CfgIface cfg_iface;
  29. BOOST_FOREACH(ConstElementPtr iface, value->listValue()) {
  30. std::string iface_name = iface->stringValue();
  31. try {
  32. cfg_iface.use(protocol_, iface_name);
  33. } catch (const std::exception& ex) {
  34. isc_throw(DhcpConfigError, "Failed to select interface: "
  35. << ex.what() << " (" << value->getPosition() << ")");
  36. }
  37. }
  38. CfgMgr::instance().getStagingCfg()->setCfgIface(cfg_iface);
  39. }
  40. void
  41. InterfaceListConfigParser::commit() {
  42. // Nothing to do.
  43. }
  44. IfacesConfigParser::IfacesConfigParser(const int protocol)
  45. : protocol_(protocol) {
  46. }
  47. void
  48. IfacesConfigParser::build(isc::data::ConstElementPtr ifaces_config) {
  49. BOOST_FOREACH(ConfigPair element, ifaces_config->mapValue()) {
  50. try {
  51. if (element.first == "interfaces") {
  52. InterfaceListConfigParser parser(protocol_);
  53. parser.build(element.second);
  54. }
  55. } catch (const std::exception& ex) {
  56. // Append line number where the error occurred.
  57. isc_throw(DhcpConfigError, ex.what() << " ("
  58. << element.second->getPosition() << ")");
  59. }
  60. }
  61. }
  62. bool
  63. IfacesConfigParser::isGenericParameter(const std::string& parameter) const {
  64. // Currently, the "interfaces" is the only common parameter for
  65. // DHCPv4 and DHCPv6.
  66. return (parameter == "interfaces");
  67. }
  68. IfacesConfigParser4::IfacesConfigParser4()
  69. : IfacesConfigParser(AF_INET) {
  70. }
  71. void
  72. IfacesConfigParser4::build(isc::data::ConstElementPtr ifaces_config) {
  73. IfacesConfigParser::build(ifaces_config);
  74. BOOST_FOREACH(ConfigPair element, ifaces_config->mapValue()) {
  75. try {
  76. if (element.first == "socket-type") {
  77. /// @todo set socket-type
  78. } else if (!isGenericParameter(element.first)) {
  79. isc_throw(DhcpConfigError, "usupported parameter '"
  80. << element.first << "'");
  81. }
  82. } catch (const std::exception& ex) {
  83. // Append line number where the error occurred.
  84. isc_throw(DhcpConfigError, ex.what() << " ("
  85. << element.second->getPosition() << ")");
  86. }
  87. }
  88. }
  89. IfacesConfigParser6::IfacesConfigParser6()
  90. : IfacesConfigParser(AF_INET6) {
  91. }
  92. void
  93. IfacesConfigParser6::build(isc::data::ConstElementPtr ifaces_config) {
  94. IfacesConfigParser::build(ifaces_config);
  95. BOOST_FOREACH(ConfigPair element, ifaces_config->mapValue()) {
  96. try {
  97. if (!isGenericParameter(element.first)) {
  98. isc_throw(DhcpConfigError, "usupported parameter '"
  99. << element.first << "'");
  100. }
  101. } catch (const std::exception& ex) {
  102. // Append line number where the error occurred.
  103. isc_throw(DhcpConfigError, ex.what() << " ("
  104. << element.second->getPosition() << ")");
  105. }
  106. }
  107. }
  108. } // end of namespace isc::dhcp
  109. } // end of namespace isc