simple_parser6.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (C) 2016-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 <cc/data.h>
  7. #include <dhcpsrv/parsers/simple_parser6.h>
  8. #include <boost/foreach.hpp>
  9. using namespace isc::data;
  10. namespace isc {
  11. namespace dhcp {
  12. /// @brief This sets of arrays define the default values and
  13. /// values inherited (derived) between various scopes.
  14. ///
  15. /// Each of those is documented in @file simple_parser6.cc. This
  16. /// is different than most other comments in Kea code. The reason
  17. /// for placing those in .cc rather than .h file is that it
  18. /// is expected to be one centralized place to look at for
  19. /// the default values. This is expected to be looked at also by
  20. /// people who are not skilled in C or C++, so they may be
  21. /// confused with the differences between declaration and definition.
  22. /// As such, there's one file to look at that hopefully is readable
  23. /// without any C or C++ skills.
  24. ///
  25. /// @{
  26. /// @brief This table defines default values for option definitions in DHCPv6.
  27. ///
  28. /// Dhcp6 may contain an array called option-def that enumerates new option
  29. /// definitions. This array lists default values for those option definitions.
  30. const SimpleDefaults SimpleParser6::OPTION6_DEF_DEFAULTS = {
  31. { "record-types", Element::string, ""},
  32. { "space", Element::string, "dhcp6"},
  33. { "array", Element::boolean, "false"},
  34. { "encapsulate", Element::string, "" }
  35. };
  36. /// @brief This table defines default values for options in DHCPv6.
  37. ///
  38. /// Dhcp6 usually contains option values (option-data) defined in global,
  39. /// subnet, class or host reservations scopes. This array lists default values
  40. /// for those option-data declarations.
  41. const SimpleDefaults SimpleParser6::OPTION6_DEFAULTS = {
  42. { "space", Element::string, "dhcp6"},
  43. { "csv-format", Element::boolean, "true"},
  44. { "always-send", Element::boolean, "false"}
  45. };
  46. /// @brief This table defines default global values for DHCPv6
  47. ///
  48. /// Some of the global parameters defined in the global scope (i.e. directly
  49. /// in Dhcp6) are optional. If not defined, the following values will be
  50. /// used.
  51. const SimpleDefaults SimpleParser6::GLOBAL6_DEFAULTS = {
  52. { "renew-timer", Element::integer, "900" },
  53. { "rebind-timer", Element::integer, "1800" },
  54. { "preferred-lifetime", Element::integer, "3600" },
  55. { "valid-lifetime", Element::integer, "7200" },
  56. { "decline-probation-period", Element::integer, "86400" }, // 24h
  57. { "dhcp4o6-port", Element::integer, "0" }
  58. };
  59. /// @brief This table defines default values for each IPv6 subnet.
  60. const SimpleDefaults SimpleParser6::SUBNET6_DEFAULTS = {
  61. { "id", Element::integer, "0" }, // 0 means autogenerate
  62. { "interface", Element::string, "" },
  63. { "client-class", Element::string, "" },
  64. { "reservation-mode", Element::string, "all" },
  65. { "rapid-commit", Element::boolean, "false" }, // rapid-commit disabled by default
  66. { "interface-id", Element::string, "" },
  67. };
  68. /// @brief This table defines default values for each IPv6 subnet.
  69. const SimpleDefaults SimpleParser6::SHARED_SUBNET6_DEFAULTS = {
  70. { "id", Element::integer, "0" }, // 0 means autogenerate
  71. { "client-class", Element::string, "" }
  72. };
  73. /// @brief This table defines default values for each IPv6 shared network.
  74. const SimpleDefaults SimpleParser6::SHARED_NETWORK6_DEFAULTS = {
  75. { "interface", Element::string, "" },
  76. { "interface-id", Element::string, "" },
  77. { "reservation-mode", Element::string, "all" },
  78. { "rapid-commit", Element::boolean, "false" } // rapid-commit disabled by default
  79. };
  80. /// @brief This table defines default values for interfaces for DHCPv6.
  81. const SimpleDefaults SimpleParser6::IFACE6_DEFAULTS = {
  82. { "re-detect", Element::boolean, "true" }
  83. };
  84. /// @brief List of parameters that can be inherited from the global to subnet6 scope.
  85. ///
  86. /// Some parameters may be defined on both global (directly in Dhcp6) and
  87. /// subnet (Dhcp6/subnet6/...) scope. If not defined in the subnet scope,
  88. /// the value is being inherited (derived) from the global scope. This
  89. /// array lists all of such parameters.
  90. ///
  91. /// This list is also used for inheriting from global to shared networks
  92. /// and from shared networks to subnets within it.
  93. const ParamsList SimpleParser6::INHERIT_TO_SUBNET6 = {
  94. "interface",
  95. "interface-id",
  96. "preferred-lifetime",
  97. "rapid-commit",
  98. "rebind-timer",
  99. "relay",
  100. "renew-timer",
  101. "reservation-mode",
  102. "valid-lifetime"
  103. };
  104. /// @}
  105. /// ---------------------------------------------------------------------------
  106. /// --- end of default values -------------------------------------------------
  107. /// ---------------------------------------------------------------------------
  108. size_t SimpleParser6::setAllDefaults(isc::data::ElementPtr global) {
  109. size_t cnt = 0;
  110. // Set global defaults first.
  111. cnt = setDefaults(global, GLOBAL6_DEFAULTS);
  112. // Now set the defaults for each specified option definition
  113. ConstElementPtr option_defs = global->get("option-def");
  114. if (option_defs) {
  115. BOOST_FOREACH(ElementPtr option_def, option_defs->listValue()) {
  116. cnt += SimpleParser::setDefaults(option_def, OPTION6_DEF_DEFAULTS);
  117. }
  118. }
  119. // Set the defaults for option data
  120. ConstElementPtr options = global->get("option-data");
  121. if (options) {
  122. BOOST_FOREACH(ElementPtr single_option, options->listValue()) {
  123. cnt += SimpleParser::setDefaults(single_option, OPTION6_DEFAULTS);
  124. }
  125. }
  126. // Now set the defaults for defined subnets
  127. ConstElementPtr subnets = global->get("subnet6");
  128. if (subnets) {
  129. cnt += setListDefaults(subnets, SUBNET6_DEFAULTS);
  130. }
  131. // Set the defaults for interfaces config
  132. ConstElementPtr ifaces_cfg = global->get("interfaces-config");
  133. if (ifaces_cfg) {
  134. ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(ifaces_cfg);
  135. cnt += setDefaults(mutable_cfg, IFACE6_DEFAULTS);
  136. }
  137. // Set defaults for shared networks
  138. ConstElementPtr shared = global->get("shared-networks");
  139. if (shared) {
  140. BOOST_FOREACH(ElementPtr net, shared->listValue()) {
  141. cnt += setDefaults(net, SHARED_NETWORK6_DEFAULTS);
  142. ConstElementPtr subs = net->get("subnet6");
  143. if (subs) {
  144. cnt += setListDefaults(subs, SHARED_SUBNET6_DEFAULTS);
  145. }
  146. }
  147. }
  148. return (cnt);
  149. }
  150. size_t SimpleParser6::deriveParameters(isc::data::ElementPtr global) {
  151. size_t cnt = 0;
  152. // Now derive global parameters into subnets.
  153. ConstElementPtr subnets = global->get("subnet6");
  154. if (subnets) {
  155. BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
  156. cnt += SimpleParser::deriveParams(global, single_subnet,
  157. INHERIT_TO_SUBNET6);
  158. }
  159. }
  160. // Deriving parameters for shared networks is a bit more involved.
  161. // First, the shared-network level derives from global, and then
  162. // subnets within derive from it.
  163. ConstElementPtr shared = global->get("shared-networks");
  164. if (shared) {
  165. BOOST_FOREACH(ElementPtr net, shared->listValue()) {
  166. // First try to inherit the parameters from shared network,
  167. // if defined there.
  168. // Then try to inherit them from global.
  169. cnt += SimpleParser::deriveParams(global, net,
  170. INHERIT_TO_SUBNET6);
  171. // Now we need to go thrugh all the subnets in this net.
  172. subnets = net->get("subnet6");
  173. if (subnets) {
  174. BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
  175. cnt += SimpleParser::deriveParams(net, single_subnet,
  176. INHERIT_TO_SUBNET6);
  177. }
  178. }
  179. }
  180. }
  181. return (cnt);
  182. }
  183. };
  184. };