cfg_option.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2014-2016 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 <dhcp/libdhcp++.h>
  7. #include <dhcp/option_space.h>
  8. #include <dhcpsrv/cfg_option.h>
  9. #include <dhcp/dhcp6.h>
  10. #include <string>
  11. namespace isc {
  12. namespace dhcp {
  13. bool
  14. OptionDescriptor::equals(const OptionDescriptor& other) const {
  15. return (persistent_ == other.persistent_ &&
  16. formatted_value_ == other.formatted_value_ &&
  17. option_->equals(other.option_));
  18. }
  19. CfgOption::CfgOption() {
  20. }
  21. bool
  22. CfgOption::empty() const {
  23. return (options_.empty() && vendor_options_.empty());
  24. }
  25. bool
  26. CfgOption::equals(const CfgOption& other) const {
  27. return (options_.equals(other.options_) &&
  28. vendor_options_.equals(other.vendor_options_));
  29. }
  30. void
  31. CfgOption::add(const OptionPtr& option, const bool persistent,
  32. const std::string& option_space) {
  33. add(OptionDescriptor(option, persistent), option_space);
  34. }
  35. void
  36. CfgOption::add(const OptionDescriptor& desc, const std::string& option_space) {
  37. if (!desc.option_) {
  38. isc_throw(isc::BadValue, "option being configured must not be NULL");
  39. } else if (!OptionSpace::validateName(option_space)) {
  40. isc_throw(isc::BadValue, "invalid option space name: '"
  41. << option_space << "'");
  42. }
  43. const uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(option_space);
  44. if (vendor_id) {
  45. vendor_options_.addItem(desc, vendor_id);
  46. } else {
  47. options_.addItem(desc, option_space);
  48. }
  49. }
  50. std::list<std::string>
  51. CfgOption::getVendorIdsSpaceNames() const {
  52. std::list<uint32_t> ids = getVendorIds();
  53. std::list<std::string> names;
  54. for (std::list<uint32_t>::const_iterator id = ids.begin();
  55. id != ids.end(); ++id) {
  56. std::ostringstream s;
  57. s << "vendor-" << *id;
  58. names.push_back(s.str());
  59. }
  60. return (names);
  61. }
  62. void
  63. CfgOption::mergeTo(CfgOption& other) const {
  64. // Merge non-vendor options.
  65. mergeInternal(options_, other.options_);
  66. // Merge vendor options.
  67. mergeInternal(vendor_options_, other.vendor_options_);
  68. }
  69. void
  70. CfgOption::copyTo(CfgOption& other) const {
  71. // Remove any existing data in the destination.
  72. other.options_.clearItems();
  73. other.vendor_options_.clearItems();
  74. mergeTo(other);
  75. }
  76. void
  77. CfgOption::encapsulate() {
  78. // Append sub-options to the top level "dhcp4" option space.
  79. encapsulateInternal(DHCP4_OPTION_SPACE);
  80. // Append sub-options to the top level "dhcp6" option space.
  81. encapsulateInternal(DHCP6_OPTION_SPACE);
  82. }
  83. void
  84. CfgOption::encapsulateInternal(const std::string& option_space) {
  85. // Get all options for the particular option space.
  86. OptionContainerPtr options = getAll(option_space);
  87. // For each option in the option space we will append sub-options
  88. // from the option spaces they encapsulate.
  89. for (OptionContainer::const_iterator opt = options->begin();
  90. opt != options->end(); ++opt) {
  91. // Get encapsulated option space for the option.
  92. const std::string& encap_space = opt->option_->getEncapsulatedSpace();
  93. // Empty value means that no option space is encapsulated.
  94. if (!encap_space.empty()) {
  95. // Retrieve all options from the encapsulated option space.
  96. OptionContainerPtr encap_options = getAll(encap_space);
  97. for (OptionContainer::const_iterator encap_opt =
  98. encap_options->begin(); encap_opt != encap_options->end();
  99. ++encap_opt) {
  100. // Add sub-option if there isn't one added already.
  101. if (!opt->option_->getOption(encap_opt->option_->getType())) {
  102. opt->option_->addOption(encap_opt->option_);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. template <typename Selector>
  109. void
  110. CfgOption::mergeInternal(const OptionSpaceContainer<OptionContainer,
  111. OptionDescriptor, Selector>& src_container,
  112. OptionSpaceContainer<OptionContainer,
  113. OptionDescriptor, Selector>& dest_container) const {
  114. // Get all option spaces used in source container.
  115. std::list<Selector> selectors = src_container.getOptionSpaceNames();
  116. // For each space in the source container retrieve the actual options and
  117. // match them with the options held in the destination container under
  118. // the same space.
  119. for (typename std::list<Selector>::const_iterator it = selectors.begin();
  120. it != selectors.end(); ++it) {
  121. // Get all options in the destination container for the particular
  122. // option space.
  123. OptionContainerPtr dest_all = dest_container.getItems(*it);
  124. OptionContainerPtr src_all = src_container.getItems(*it);
  125. // For each option under this option space check if there is a
  126. // corresponding option in the destination container. If not,
  127. // add one.
  128. for (OptionContainer::const_iterator src_opt = src_all->begin();
  129. src_opt != src_all->end(); ++src_opt) {
  130. const OptionContainerTypeIndex& idx = dest_all->get<1>();
  131. const OptionContainerTypeRange& range =
  132. idx.equal_range(src_opt->option_->getType());
  133. // If there is no such option in the destination container,
  134. // add one.
  135. if (std::distance(range.first, range.second) == 0) {
  136. dest_container.addItem(OptionDescriptor(src_opt->option_,
  137. src_opt->persistent_),
  138. *it);
  139. }
  140. }
  141. }
  142. }
  143. OptionContainerPtr
  144. CfgOption::getAll(const std::string& option_space) const {
  145. return (options_.getItems(option_space));
  146. }
  147. OptionContainerPtr
  148. CfgOption::getAll(const uint32_t vendor_id) const {
  149. return (vendor_options_.getItems(vendor_id));
  150. }
  151. } // end of namespace isc::dhcp
  152. } // end of namespace isc