shared_network_parser.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 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. #ifndef SHARED_NETWORK_PARSER_H
  7. #define SHARED_NETWORK_PARSER_H
  8. #include <cc/data.h>
  9. #include <dhcpsrv/cfg_option.h>
  10. #include <dhcpsrv/parsers/dhcp_parsers.h>
  11. #include <dhcpsrv/parsers/option_data_parser.h>
  12. #include <dhcpsrv/parsers/shared_network_parser.h>
  13. #include <dhcpsrv/shared_network.h>
  14. #include <boost/pointer_cast.hpp>
  15. #include <string>
  16. using namespace isc::data;
  17. namespace isc {
  18. namespace dhcp {
  19. SharedNetwork4Ptr
  20. SharedNetwork4Parser::parse(const data::ConstElementPtr& shared_network_data) {
  21. SharedNetwork4Ptr shared_network;
  22. std::string name;
  23. try {
  24. // Shared network is a map.
  25. const auto& element = shared_network_data->mapValue();
  26. // Make sure that the network name has been specified. The name is required
  27. // to create a SharedNetwork4 object.
  28. const auto& name_it = element.find("name");
  29. if (name_it == element.cend()) {
  30. isc_throw(DhcpConfigError, "parameter \"name\" must be specified for"
  31. " a shared network");
  32. }
  33. shared_network.reset(new SharedNetwork4(name_it->second->stringValue()));
  34. // Iterate over all parameters within the map and assign them to the
  35. // shared network.
  36. for (auto param = element.cbegin(); param != element.cend(); ++param) {
  37. if (param->first == "interface") {
  38. shared_network->setIface(param->second->stringValue());
  39. } else if (param->first == "option-data") {
  40. // Create parser instance for option-data.
  41. CfgOptionPtr cfg_option = shared_network->getCfgOption();
  42. OptionDataListParser parser(AF_INET);
  43. parser.parse(cfg_option, param->second);
  44. } else if (param->first == "subnet4") {
  45. // Create parser instance of subnet4.
  46. Subnets4ListConfigParser parser;
  47. Subnet4Collection subnets;
  48. parser.parse(subnets, param->second);
  49. // Add all returned subnets into shared network.
  50. for (auto subnet = subnets.cbegin(); subnet != subnets.cend();
  51. ++subnet) {
  52. shared_network->add(*subnet);
  53. }
  54. }
  55. }
  56. } catch (const std::exception& ex) {
  57. isc_throw(DhcpConfigError, ex.what() << " ("
  58. << shared_network_data->getPosition() << ")");
  59. }
  60. return (shared_network);
  61. }
  62. SharedNetwork6Ptr
  63. SharedNetwork6Parser::parse(const data::ConstElementPtr& shared_network_data) {
  64. SharedNetwork6Ptr shared_network;
  65. std::string name;
  66. try {
  67. // Shared network is a map.
  68. const auto& element = shared_network_data->mapValue();
  69. // Make sure that the network name has been specified. The name is required
  70. // to create a SharedNetwork6 object.
  71. const auto& name_it = element.find("name");
  72. if (name_it == element.cend()) {
  73. isc_throw(DhcpConfigError, "parameter \"name\" must be specified for"
  74. " a shared network");
  75. }
  76. shared_network.reset(new SharedNetwork6(name_it->second->stringValue()));
  77. // Iterate over all parameters within the map and assign them to the
  78. // shared network.
  79. for (auto param = element.cbegin(); param != element.cend(); ++param) {
  80. if (param->first == "interface") {
  81. shared_network->setIface(param->second->stringValue());
  82. } else if (param->first == "option-data") {
  83. // Create parser instance for option-data.
  84. CfgOptionPtr cfg_option = shared_network->getCfgOption();
  85. OptionDataListParser parser(AF_INET6);
  86. parser.parse(cfg_option, param->second);
  87. } else if (param->first == "subnet6") {
  88. // Create parser instance of subnet6.
  89. Subnets6ListConfigParser parser;
  90. Subnet6Collection subnets;
  91. parser.parse(subnets, param->second);
  92. // Add all returned subnets into shared network.
  93. for (auto subnet = subnets.cbegin(); subnet != subnets.cend();
  94. ++subnet) {
  95. shared_network->add(*subnet);
  96. }
  97. }
  98. }
  99. } catch (const std::exception& ex) {
  100. isc_throw(DhcpConfigError, ex.what() << " ("
  101. << shared_network_data->getPosition() << ")");
  102. }
  103. return (shared_network);
  104. }
  105. } // end of namespace isc::dhcp
  106. } // end of namespace isc
  107. #endif // SHARED_NETWORK_PARSER_H