option_data_parser.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 OPTION_DATA_PARSER_H
  7. #define OPTION_DATA_PARSER_H
  8. #include <cc/data.h>
  9. #include <cc/simple_parser.h>
  10. #include <dhcpsrv/cfg_option.h>
  11. #include <util/optional_value.h>
  12. #include <cstdint>
  13. #include <string>
  14. #include <utility>
  15. namespace isc {
  16. namespace dhcp {
  17. /// @brief Parser for option data value.
  18. ///
  19. /// This parser parses configuration entries that specify value of
  20. /// a single option. These entries include option name, option code
  21. /// and data carried by the option. The option data can be specified
  22. /// in one of the two available formats: binary value represented as
  23. /// a string of hexadecimal digits or a list of comma separated values.
  24. /// The format being used is controlled by csv-format configuration
  25. /// parameter. When setting this value to True, the latter format is
  26. /// used. The subsequent values in the CSV format apply to relevant
  27. /// option data fields in the configured option. For example the
  28. /// configuration: "data" : "192.168.2.0, 56, hello world" can be
  29. /// used to set values for the option comprising IPv4 address,
  30. /// integer and string data field. Note that order matters. If the
  31. /// order of values does not match the order of data fields within
  32. /// an option the configuration will not be accepted. If parsing
  33. /// is successful then an instance of an option is created and
  34. /// added to the storage provided by the calling class.
  35. class OptionDataParser : public isc::data::SimpleParser {
  36. public:
  37. /// @brief Constructor.
  38. ///
  39. /// @param address_family Address family: @c AF_INET or @c AF_INET6.
  40. explicit OptionDataParser(const uint16_t address_family);
  41. /// @brief Parses ElementPtr containing option definition
  42. ///
  43. /// This method parses ElementPtr containing the option definition,
  44. /// instantiates the option for it and then returns a pair
  45. /// of option descriptor (that holds that new option) and
  46. /// a string that specifies the option space.
  47. ///
  48. /// Note: ElementPtr is expected to contain all fields. If your
  49. /// ElementPtr does not have them, please use
  50. /// @ref isc::data::SimpleParser::setDefaults to fill the missing fields
  51. /// with default values.
  52. ///
  53. /// @param single_option ElementPtr containing option definition
  54. /// @return Option object wrapped in option description and an option
  55. /// space
  56. std::pair<OptionDescriptor, std::string>
  57. parse(isc::data::ConstElementPtr single_option);
  58. private:
  59. /// @brief Finds an option definition within an option space
  60. ///
  61. /// Given an option space and an option code, find the corresponding
  62. /// option definition within the option definition storage.
  63. ///
  64. /// @param option_space name of the parameter option space
  65. /// @param search_key an option code or name to be used to lookup the
  66. /// option definition.
  67. /// @tparam A numeric type for searching using an option code or the
  68. /// string for searching using the option name.
  69. ///
  70. /// @return OptionDefinitionPtr of the option definition or an
  71. /// empty OptionDefinitionPtr if not found.
  72. /// @throw DhcpConfigError if the option space requested is not valid
  73. /// for this server.
  74. template<typename SearchKey>
  75. OptionDefinitionPtr findOptionDefinition(const std::string& option_space,
  76. const SearchKey& search_key) const;
  77. /// @brief Create option instance.
  78. ///
  79. /// Creates an instance of an option and adds it to the provided
  80. /// options storage. If the option data parsed by \ref build function
  81. /// are invalid or insufficient this function emits an exception.
  82. ///
  83. /// @param option_data An element holding data for a single option being
  84. /// created.
  85. ///
  86. /// @return created option descriptor
  87. ///
  88. /// @throw DhcpConfigError if parameters provided in the configuration
  89. /// are invalid.
  90. std::pair<OptionDescriptor, std::string>
  91. createOption(isc::data::ConstElementPtr option_data);
  92. /// @brief Retrieves parsed option code as an optional value.
  93. ///
  94. /// @param parent A data element holding full option data configuration.
  95. ///
  96. /// @return Option code, possibly unspecified.
  97. /// @throw DhcpConfigError if option code is invalid.
  98. util::OptionalValue<uint32_t>
  99. extractCode(data::ConstElementPtr parent) const;
  100. /// @brief Retrieves parsed option name as an optional value.
  101. ///
  102. /// @param parent A data element holding full option data configuration.
  103. ///
  104. /// @return Option name, possibly unspecified.
  105. /// @throw DhcpConfigError if option name is invalid.
  106. util::OptionalValue<std::string>
  107. extractName(data::ConstElementPtr parent) const;
  108. /// @brief Retrieves csv-format parameter as an optional value.
  109. ///
  110. /// @return Value of the csv-format parameter, possibly unspecified.
  111. util::OptionalValue<bool> extractCSVFormat(data::ConstElementPtr parent) const;
  112. /// @brief Retrieves option data as a string.
  113. ///
  114. /// @param parent A data element holding full option data configuration.
  115. /// @return Option data as a string. It will return empty string if
  116. /// option data is unspecified.
  117. std::string extractData(data::ConstElementPtr parent) const;
  118. /// @brief Retrieves option space name.
  119. ///
  120. /// If option space name is not specified in the configuration the
  121. /// 'dhcp4' or 'dhcp6' option space name is returned, depending on
  122. /// the universe specified in the parser context.
  123. ///
  124. /// @param parent A data element holding full option data configuration.
  125. ///
  126. /// @return Option space name.
  127. std::string extractSpace(data::ConstElementPtr parent) const;
  128. /// @brief Retrieves persistent/always-send parameter as an optional value.
  129. ///
  130. /// @return Value of the persistent parameter, possibly unspecified.
  131. util::OptionalValue<bool> extractPersistent(data::ConstElementPtr parent) const;
  132. /// @brief Address family: @c AF_INET or @c AF_INET6.
  133. uint16_t address_family_;
  134. };
  135. /// @brief Parser for option data values within a subnet.
  136. ///
  137. /// This parser iterates over all entries that define options
  138. /// data for a particular subnet and creates a collection of options.
  139. /// If parsing is successful, all these options are added to the Subnet
  140. /// object.
  141. class OptionDataListParser : public isc::data::SimpleParser {
  142. public:
  143. /// @brief Constructor.
  144. ///
  145. /// @param address_family Address family: @c AF_INET or AF_INET6
  146. explicit OptionDataListParser(const uint16_t address_family);
  147. /// @brief Parses a list of options, instantiates them and stores in cfg
  148. ///
  149. /// This method expects to get a list of options in option_data_list,
  150. /// iterates over them, creates option objects, wraps them with
  151. /// option descriptor and stores in specified cfg.
  152. ///
  153. /// @param cfg created options will be stored here
  154. /// @param option_data_list configuration that describes the options
  155. void parse(const CfgOptionPtr& cfg,
  156. isc::data::ConstElementPtr option_data_list);
  157. private:
  158. /// @brief Address family: @c AF_INET or @c AF_INET6
  159. uint16_t address_family_;
  160. };
  161. } // end of namespace isc::dhcp
  162. } // end of namespace isc
  163. #endif // OPTION_DATA_PARSER_H