client_class_def_parser.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2015-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 <config.h>
  7. #include <dhcpsrv/cfgmgr.h>
  8. #include <dhcpsrv/client_class_def.h>
  9. #include <dhcpsrv/parsers/dhcp_parsers.h>
  10. #include <dhcpsrv/parsers/client_class_def_parser.h>
  11. #include <eval/eval_context.h>
  12. #include <asiolink/io_address.h>
  13. #include <asiolink/io_error.h>
  14. #include <boost/foreach.hpp>
  15. using namespace isc::data;
  16. using namespace isc::asiolink;
  17. using namespace std;
  18. /// @file client_class_def_parser.cc
  19. ///
  20. /// @brief Method implementations for client class definition parsing
  21. namespace isc {
  22. namespace dhcp {
  23. // ********************** ExpressionParser ****************************
  24. void
  25. ExpressionParser::parse(ExpressionPtr& expression,
  26. ConstElementPtr expression_cfg,
  27. uint16_t family) {
  28. if (expression_cfg->getType() != Element::string) {
  29. isc_throw(DhcpConfigError, "expression ["
  30. << expression_cfg->str() << "] must be a string, at ("
  31. << expression_cfg->getPosition() << ")");
  32. }
  33. // Get the expression's text via getValue() as the text returned
  34. // by str() enclosed in quotes.
  35. std::string value;
  36. expression_cfg->getValue(value);
  37. try {
  38. EvalContext eval_ctx(family == AF_INET ? Option::V4 : Option::V6);
  39. eval_ctx.parseString(value);
  40. expression.reset(new Expression());
  41. *expression = eval_ctx.expression;
  42. } catch (const std::exception& ex) {
  43. // Append position if there is a failure.
  44. isc_throw(DhcpConfigError,
  45. "expression: [" << value
  46. << "] error: " << ex.what() << " at ("
  47. << expression_cfg->getPosition() << ")");
  48. }
  49. }
  50. // ********************** ClientClassDefParser ****************************
  51. void
  52. ClientClassDefParser::parse(ClientClassDictionaryPtr& class_dictionary,
  53. ConstElementPtr class_def_cfg,
  54. uint16_t family) {
  55. // name is now mandatory
  56. std::string name = getString(class_def_cfg, "name");
  57. if (name.empty()) {
  58. isc_throw(DhcpConfigError,
  59. "not empty parameter 'name' is required "
  60. << getPosition("name", class_def_cfg) << ")");
  61. }
  62. // Parse matching expression
  63. ExpressionPtr match_expr;
  64. ConstElementPtr test = class_def_cfg->get("test");
  65. if (test) {
  66. ExpressionParser parser;
  67. parser.parse(match_expr, test, family);
  68. }
  69. // Parse option data
  70. CfgOptionPtr options(new CfgOption());
  71. ConstElementPtr option_data = class_def_cfg->get("option-data");
  72. if (option_data) {
  73. OptionDataListParser opts_parser(family);
  74. opts_parser.parse(options, option_data);
  75. }
  76. // Let's try to parse the next-server field
  77. IOAddress next_server("0.0.0.0");
  78. if (class_def_cfg->contains("next-server")) {
  79. std::string next_server_txt = getString(class_def_cfg, "next-server");
  80. try {
  81. next_server = IOAddress(next_server_txt);
  82. } catch (const IOError& ex) {
  83. isc_throw(DhcpConfigError,
  84. "Invalid next-server value specified: '"
  85. << next_server_txt << "' ("
  86. << getPosition("next-server", class_def_cfg) << ")");
  87. }
  88. if (next_server.getFamily() != AF_INET) {
  89. isc_throw(DhcpConfigError, "Invalid next-server value: '"
  90. << next_server_txt
  91. << "', must be IPv4 address ("
  92. << getPosition("next-server", class_def_cfg) << ")");
  93. }
  94. if (next_server.isV4Bcast()) {
  95. isc_throw(DhcpConfigError, "Invalid next-server value: '"
  96. << next_server_txt
  97. << "', must not be a broadcast ("
  98. << getPosition("next-server", class_def_cfg) << ")");
  99. }
  100. }
  101. // Let's try to parse server-hostname
  102. std::string sname;
  103. if (class_def_cfg->contains("server-hostname")) {
  104. sname = getString(class_def_cfg, "server-hostname");
  105. if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
  106. isc_throw(DhcpConfigError, "server-hostname must be at most "
  107. << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
  108. << sname.length() << " ("
  109. << getPosition("server-hostname", class_def_cfg) << ")");
  110. }
  111. }
  112. // Let's try to parse boot-file-name
  113. std::string filename;
  114. if (class_def_cfg->contains("boot-file-name")) {
  115. filename = getString(class_def_cfg, "boot-file-name");
  116. if (filename.length() > Pkt4::MAX_FILE_LEN) {
  117. isc_throw(DhcpConfigError, "boot-file-name must be at most "
  118. << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
  119. << filename.length() << " ("
  120. << getPosition("boot-file-name", class_def_cfg) << ")");
  121. }
  122. }
  123. // Add the client class definition
  124. try {
  125. class_dictionary->addClass(name, match_expr, options, next_server,
  126. sname, filename);
  127. } catch (const std::exception& ex) {
  128. isc_throw(DhcpConfigError, ex.what()
  129. << " (" << class_def_cfg->getPosition() << ")");
  130. }
  131. }
  132. // ****************** ClientClassDefListParser ************************
  133. ClientClassDictionaryPtr
  134. ClientClassDefListParser::parse(ConstElementPtr client_class_def_list,
  135. uint16_t family) {
  136. ClientClassDictionaryPtr dictionary(new ClientClassDictionary());
  137. BOOST_FOREACH(ConstElementPtr client_class_def,
  138. client_class_def_list->listValue()) {
  139. ClientClassDefParser parser;
  140. parser.parse(dictionary, client_class_def, family);
  141. }
  142. return (dictionary);
  143. }
  144. } // end of namespace isc::dhcp
  145. } // end of namespace isc