simple_parser6.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <dhcp6/simple_parser6.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_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. { "encapsulate", Element::string, "" }
  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. };
  57. /// @brief List of parameters that can be inherited from the global to subnet6 scope.
  58. ///
  59. /// Some parameters may be defined on both global (directly in Dhcp6) and
  60. /// subnet (Dhcp6/subnet6/...) scope. If not defined in the subnet scope,
  61. /// the value is being inherited (derived) from the global scope. This
  62. /// array lists all of such parameters.
  63. const ParamsList SimpleParser6::INHERIT_GLOBAL_TO_SUBNET6 = {
  64. "renew-timer",
  65. "rebind-timer",
  66. "preferred-lifetime",
  67. "valid-lifetime"
  68. };
  69. /// @brief This table defines default values for D2 client configuration
  70. ///
  71. const SimpleDefaults SimpleParser6::D2_CLIENT_CONFIG_DEFAULTS = {
  72. { "server-ip", Element::string, "127.0.0.1" },
  73. { "server-port", Element::integer, "53001" },
  74. // default sender-ip depends on server-ip family, so we leave default blank
  75. // parser knows to use the appropriate ZERO address based on server-ip
  76. { "sender-ip", Element::string, "" },
  77. { "sender-port", Element::integer, "0" },
  78. { "max-queue-size", Element::integer, "1024" },
  79. { "ncr-protocol", Element::string, "UDP" },
  80. { "ncr-format", Element::string, "JSON" },
  81. { "always-include-fqdn", Element::boolean, "false" },
  82. { "override-no-update", Element::boolean, "false" },
  83. { "override-client-update", Element::boolean, "false" },
  84. { "replace-client-name", Element::string, "NEVER" },
  85. { "generated-prefix", Element::string, "myhost" },
  86. { "qualifying-suffix", Element::string, "" }
  87. };
  88. /// @}
  89. /// ---------------------------------------------------------------------------
  90. /// --- end of default values -------------------------------------------------
  91. /// ---------------------------------------------------------------------------
  92. size_t SimpleParser6::setAllDefaults(isc::data::ElementPtr global) {
  93. size_t cnt = 0;
  94. // Set global defaults first.
  95. cnt = setDefaults(global, GLOBAL6_DEFAULTS);
  96. // Now set the defaults for each specified option definition
  97. ConstElementPtr option_defs = global->get("option-def");
  98. if (option_defs) {
  99. BOOST_FOREACH(ElementPtr option_def, option_defs->listValue()) {
  100. cnt += SimpleParser::setDefaults(option_def, OPTION6_DEF_DEFAULTS);
  101. }
  102. }
  103. // Finally, set the defaults for option data
  104. ConstElementPtr options = global->get("option-data");
  105. if (options) {
  106. BOOST_FOREACH(ElementPtr single_option, options->listValue()) {
  107. cnt += SimpleParser::setDefaults(single_option, OPTION6_DEFAULTS);
  108. }
  109. }
  110. ConstElementPtr d2_client = global->get("dhcp-ddns");
  111. /// @todo - what if it's not in global? should we add it?
  112. if (d2_client) {
  113. // Because "dhcp-ddns" is a MapElement and global->get()
  114. // returns a ConstElementPtr, then we get a map we can't
  115. // change. So go thru gyrations to create a non-const
  116. // map, update it with default values and then replace
  117. // the one in global with the new one. Ick.
  118. std::map<std::string, ConstElementPtr> d2_map;
  119. d2_client->getValue(d2_map);
  120. ElementPtr new_map(new MapElement());
  121. new_map->setValue(d2_map);
  122. cnt += SimpleParser::setDefaults(new_map, D2_CLIENT_CONFIG_DEFAULTS);
  123. global->set("dhcp-ddns", new_map);
  124. }
  125. return (cnt);
  126. }
  127. };
  128. };