simple_parser4.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <dhcp4/simple_parser4.h>
  7. #include <cc/data.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_parser4.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 DHCPv4.
  27. ///
  28. /// Dhcp4 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 SimpleParser4::OPTION4_DEF_DEFAULTS = {
  31. { "record-types", Element::string, ""},
  32. { "space", Element::string, "dhcp4"},
  33. { "array", Element::boolean, "false"},
  34. { "encapsulate", Element::string, "" }
  35. };
  36. /// @brief This table defines default values for options in DHCPv4.
  37. ///
  38. /// Dhcp4 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 SimpleParser4::OPTION4_DEFAULTS = {
  42. { "space", Element::string, "dhcp4"},
  43. { "csv-format", Element::boolean, "true"}
  44. };
  45. /// @brief This table defines default global values for DHCPv4
  46. ///
  47. /// Some of the global parameters defined in the global scope (i.e. directly
  48. /// in Dhcp4) are optional. If not defined, the following values will be
  49. /// used.
  50. const SimpleDefaults SimpleParser4::GLOBAL4_DEFAULTS = {
  51. { "renew-timer", Element::integer, "900" },
  52. { "rebind-timer", Element::integer, "1800" },
  53. { "valid-lifetime", Element::integer, "7200" },
  54. { "decline-probation-period", Element::integer, "86400" }, // 24h
  55. { "dhcp4o6-port", Element::integer, "0" },
  56. { "echo-client-id", Element::boolean, "true" },
  57. { "match-client-id", Element::boolean, "true" },
  58. { "next-server", Element::string, "0.0.0.0" }
  59. };
  60. /// @brief This table defines default values for each IPv4 subnet.
  61. const SimpleDefaults SimpleParser4::SUBNET4_DEFAULTS = {
  62. { "id", Element::integer, "0" }, // 0 means autogenerate
  63. { "interface", Element::string, "" },
  64. { "client-class", Element::string, "" },
  65. { "reservation-mode", Element::string, "all" },
  66. { "4o6-interface", Element::string, "" },
  67. { "4o6-interface-id", Element::string, "" },
  68. { "4o6-subnet", Element::string, "" },
  69. };
  70. /// @brief This table defines default values for interfaces for DHCPv4.
  71. const SimpleDefaults SimpleParser4::IFACE4_DEFAULTS = {
  72. { "re-detect", Element::boolean, "true" }
  73. };
  74. /// @brief List of parameters that can be inherited from the global to subnet4 scope.
  75. ///
  76. /// Some parameters may be defined on both global (directly in Dhcp4) and
  77. /// subnet (Dhcp4/subnet4/...) scope. If not defined in the subnet scope,
  78. /// the value is being inherited (derived) from the global scope. This
  79. /// array lists all of such parameters.
  80. const ParamsList SimpleParser4::INHERIT_GLOBAL_TO_SUBNET4 = {
  81. "renew-timer",
  82. "rebind-timer",
  83. "valid-lifetime",
  84. "match-client-id",
  85. "next-server"
  86. };
  87. /// @}
  88. /// ---------------------------------------------------------------------------
  89. /// --- end of default values -------------------------------------------------
  90. /// ---------------------------------------------------------------------------
  91. size_t SimpleParser4::setAllDefaults(isc::data::ElementPtr global) {
  92. size_t cnt = 0;
  93. // Set global defaults first.
  94. cnt = setDefaults(global, GLOBAL4_DEFAULTS);
  95. // Now set option definition defaults for each specified option definition
  96. ConstElementPtr option_defs = global->get("option-def");
  97. if (option_defs) {
  98. BOOST_FOREACH(ElementPtr option_def, option_defs->listValue()) {
  99. cnt += SimpleParser::setDefaults(option_def, OPTION4_DEF_DEFAULTS);
  100. }
  101. }
  102. // Set the defaults for option data
  103. ConstElementPtr options = global->get("option-data");
  104. if (options) {
  105. cnt += setListDefaults(options, OPTION4_DEFAULTS);
  106. }
  107. // Now set the defaults for defined subnets
  108. ConstElementPtr subnets = global->get("subnet4");
  109. if (subnets) {
  110. cnt += setListDefaults(subnets, SUBNET4_DEFAULTS);
  111. }
  112. // Set the defaults for interfaces config
  113. ConstElementPtr ifaces_cfg = global->get("interfaces-config");
  114. if (ifaces_cfg) {
  115. ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(ifaces_cfg);
  116. cnt += setDefaults(mutable_cfg, IFACE4_DEFAULTS);
  117. }
  118. return (cnt);
  119. }
  120. size_t SimpleParser4::deriveParameters(isc::data::ElementPtr global) {
  121. size_t cnt = 0;
  122. // Now derive global parameters into subnets.
  123. ConstElementPtr subnets = global->get("subnet4");
  124. if (subnets) {
  125. BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
  126. cnt += SimpleParser::deriveParams(global, single_subnet,
  127. INHERIT_GLOBAL_TO_SUBNET4);
  128. }
  129. }
  130. return (cnt);
  131. }
  132. };
  133. };