client_class_def_parser.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. try {
  56. std::string name;
  57. std::string next_server_txt = "0.0.0.0";
  58. std::string sname;
  59. std::string filename;
  60. ExpressionPtr match_expr;
  61. CfgOptionPtr options(new CfgOption());
  62. // Parse the elements that make up the client class definition.
  63. BOOST_FOREACH(ConfigPair param, class_def_cfg->mapValue()) {
  64. std::string entry(param.first);
  65. ConstElementPtr value(param.second);
  66. if (entry == "name") {
  67. name = value->stringValue();
  68. } else if (entry == "test") {
  69. ExpressionParser parser;
  70. parser.parse(match_expr, value, family);
  71. } else if (entry == "option-data") {
  72. OptionDataListParser opts_parser(family);
  73. opts_parser.parse(options, value);
  74. } else if (entry == "next-server") {
  75. next_server_txt = value->stringValue();
  76. } else if (entry == "server-hostname") {
  77. sname = value->stringValue();
  78. } else if (entry == "boot-file-name") {
  79. filename = value->stringValue();
  80. } else {
  81. isc_throw(DhcpConfigError, "invalid parameter '" << entry
  82. << "' (" << value->getPosition() << ")");
  83. }
  84. }
  85. // name is now mandatory
  86. if (name.empty()) {
  87. isc_throw(DhcpConfigError,
  88. "not empty parameter 'name' is required");
  89. }
  90. // Let's parse the next-server field
  91. IOAddress next_server("0.0.0.0");
  92. try {
  93. next_server = IOAddress(next_server_txt);
  94. } catch (const IOError& ex) {
  95. isc_throw(DhcpConfigError, "Invalid next-server value specified: '"
  96. << next_server_txt);
  97. }
  98. if (next_server.getFamily() != AF_INET) {
  99. isc_throw(DhcpConfigError, "Invalid next-server value: '"
  100. << next_server_txt << "', must be IPv4 address");
  101. }
  102. if (next_server.isV4Bcast()) {
  103. isc_throw(DhcpConfigError, "Invalid next-server value: '"
  104. << next_server_txt << "', must not be a broadcast");
  105. }
  106. // Let's try to parse server-hostname
  107. if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
  108. isc_throw(DhcpConfigError, "server-hostname must be at most "
  109. << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
  110. << sname.length());
  111. }
  112. // Let's try to parse boot-file-name
  113. if (filename.length() > Pkt4::MAX_FILE_LEN) {
  114. isc_throw(DhcpConfigError, "boot-file-name must be at most "
  115. << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
  116. << filename.length());
  117. }
  118. // Add the client class definition
  119. class_dictionary->addClass(name, match_expr, options, next_server,
  120. sname, filename);
  121. } catch (const std::exception& ex) {
  122. isc_throw(DhcpConfigError, ex.what()
  123. << " (" << class_def_cfg->getPosition() << ")");
  124. }
  125. }
  126. // ****************** ClientClassDefListParser ************************
  127. ClientClassDictionaryPtr
  128. ClientClassDefListParser::parse(ConstElementPtr client_class_def_list,
  129. uint16_t family) {
  130. ClientClassDictionaryPtr dictionary(new ClientClassDictionary());
  131. BOOST_FOREACH(ConstElementPtr client_class_def,
  132. client_class_def_list->listValue()) {
  133. ClientClassDefParser parser;
  134. parser.parse(dictionary, client_class_def, family);
  135. }
  136. return (dictionary);
  137. }
  138. } // end of namespace isc::dhcp
  139. } // end of namespace isc