kea_controller.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/asiolink.h>
  16. #include <dhcp/iface_mgr.h>
  17. #include <dhcpsrv/dhcp_config_parser.h>
  18. #include <dhcpsrv/cfgmgr.h>
  19. #include <dhcp6/json_config_parser.h>
  20. #include <dhcp6/ctrl_dhcp6_srv.h>
  21. #include <dhcp6/dhcp6_log.h>
  22. #include <dhcp6/spec_config.h>
  23. #include <log/logger_level.h>
  24. #include <log/logger_name.h>
  25. #include <log/logger_manager.h>
  26. #include <log/logger_specification.h>
  27. #include <log/logger_support.h>
  28. #include <log/output_option.h>
  29. #include <exceptions/exceptions.h>
  30. #include <util/buffer.h>
  31. #include <cassert>
  32. #include <iostream>
  33. #include <string>
  34. #include <vector>
  35. using namespace isc::asiolink;
  36. using namespace isc::cc;
  37. using namespace isc::config;
  38. using namespace isc::data;
  39. using namespace isc::dhcp;
  40. using namespace isc::log;
  41. using namespace isc::util;
  42. using namespace std;
  43. namespace isc {
  44. namespace dhcp {
  45. bool
  46. ControlledDhcpv6Srv::init(const std::string& file_name) {
  47. // This is a configuration backend implementation that reads the
  48. // configuration from a JSON file.
  49. isc::data::ConstElementPtr json;
  50. isc::data::ConstElementPtr dhcp6;
  51. isc::data::ConstElementPtr result;
  52. // Basic sanity check: file name must not be empty.
  53. if (file_name.empty()) {
  54. isc_throw(BadValue, "JSON configuration file not specified");
  55. }
  56. try {
  57. // Read contents of the file and parse it as JSON
  58. json = Element::fromJSONFile(file_name, true);
  59. if (!json) {
  60. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  61. .arg("Config file " + file_name + " missing or empty.");
  62. isc_throw(BadValue, "Unable to process JSON configuration file:"
  63. + file_name);
  64. }
  65. // Get Dhcp6 component from the config
  66. dhcp6 = json->get("Dhcp6");
  67. if (!dhcp6) {
  68. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  69. .arg("Config file " + file_name + " does not include 'Dhcp6' entry.");
  70. isc_throw(BadValue, "Unable to process JSON configuration file:"
  71. + file_name);
  72. }
  73. // Use parsed JSON structures to configure the server
  74. result = processCommand("config-reload", dhcp6);
  75. } catch (const std::exception& ex) {
  76. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL).arg(ex.what());
  77. isc_throw(BadValue, "Unable to process JSON configuration file:"
  78. + file_name);
  79. }
  80. if (!result) {
  81. // Undetermined status of the configuration. This should never happen,
  82. // but as the configureDhcp6Server returns a pointer, it is theoretically
  83. // possible that it will return NULL.
  84. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  85. .arg("Configuration failed: Undefined result of configureDhcp6Server"
  86. "() function after attempting to read " + file_name);
  87. return (false);
  88. }
  89. // Now check is the returned result is successful (rcode=0) or not
  90. ConstElementPtr comment; /// see @ref isc::config::parseAnswer
  91. int rcode;
  92. comment = parseAnswer(rcode, result);
  93. if (rcode != 0) {
  94. string reason = "";
  95. if (comment) {
  96. reason = string(" (") + comment->stringValue() + string(")");
  97. }
  98. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL).arg(reason);
  99. return (false);
  100. }
  101. // We don't need to call openActiveSockets() or startD2() as these
  102. // methods are called in processConfig() which is called by
  103. // processCommand("reload-config", ...)
  104. return (true);
  105. }
  106. void ControlledDhcpv6Srv::cleanup() {
  107. // Nothing to do here. No need to disconnect from anything.
  108. }
  109. /// This is a logger initialization for JSON file backend.
  110. /// For now, it's just setting log messages to be printed on stdout.
  111. /// @todo: Implement this properly (see #3427)
  112. void Daemon::loggerInit(const char* log_name, bool verbose, bool ) {
  113. // This method configures logger. For now it is very simple.
  114. // We'll make it more robust once we add support for JSON-based logging
  115. // configuration.
  116. using namespace isc::log;
  117. Severity severity = b10LoggerSeverity(verbose ? isc::log::DEBUG : isc::log::INFO);
  118. // Set a directory for creating lockfiles when running tests
  119. // @todo: Find out why this is needed. Without this, the logger doesn't
  120. // work.
  121. setenv("B10_LOCKFILE_DIR_FROM_BUILD", TOP_BUILDDIR, 1);
  122. // Initialize logging
  123. initLogger(log_name, severity, isc::log::MAX_DEBUG_LEVEL, NULL);
  124. // Now configure logger output to stdout.
  125. /// @todo: Make this configurable as part of #3427.
  126. LoggerSpecification spec(log_name, severity,
  127. b10LoggerDbglevel(isc::log::MAX_DEBUG_LEVEL));
  128. OutputOption option;
  129. option.destination = OutputOption::DEST_CONSOLE;
  130. option.stream = OutputOption::STR_STDOUT;
  131. spec.addOutputOption(option);
  132. LoggerManager manager;
  133. manager.process(spec);
  134. }
  135. };
  136. };