bundy_controller.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (C) 2012-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 <cc/data.h>
  17. #include <cc/session.h>
  18. #include <config/ccsession.h>
  19. #include <dhcp/iface_mgr.h>
  20. #include <dhcp4/json_config_parser.h>
  21. #include <dhcp4/ctrl_dhcp4_srv.h>
  22. #include <dhcp4/dhcp4_log.h>
  23. #include <dhcp4/spec_config.h>
  24. #include <dhcpsrv/cfgmgr.h>
  25. #include <dhcpsrv/dhcp_config_parser.h>
  26. #include <exceptions/exceptions.h>
  27. #include <hooks/hooks_manager.h>
  28. #include <util/buffer.h>
  29. #include <cassert>
  30. #include <iostream>
  31. #include <sstream>
  32. #include <string>
  33. #include <vector>
  34. using namespace isc::asiolink;
  35. using namespace isc::cc;
  36. using namespace isc::config;
  37. using namespace isc::data;
  38. using namespace isc::dhcp;
  39. using namespace isc::hooks;
  40. using namespace isc::log;
  41. using namespace isc::util;
  42. using namespace std;
  43. namespace isc {
  44. namespace dhcp {
  45. /// @brief Helper session object that represents raw connection to msgq.
  46. isc::cc::Session* cc_session_ = NULL;
  47. /// @brief Session that receives configuration and commands
  48. isc::config::ModuleCCSession* config_session_ = NULL;
  49. /// @brief A dummy configuration handler that always returns success.
  50. ///
  51. /// This configuration handler does not perform configuration
  52. /// parsing and always returns success. A dummy handler should
  53. /// be installed using \ref isc::config::ModuleCCSession ctor
  54. /// to get the initial configuration. This initial configuration
  55. /// comprises values for only those elements that were modified
  56. /// the previous session. The \ref dhcp4ConfigHandler can't be
  57. /// used to parse the initial configuration because it needs the
  58. /// full configuration to satisfy dependencies between the
  59. /// various configuration values. Installing the dummy handler
  60. /// that guarantees to return success causes initial configuration
  61. /// to be stored for the session being created and that it can
  62. /// be later accessed with
  63. /// \ref isc::config::ConfigData::getFullConfig().
  64. ///
  65. /// @param new_config new configuration.
  66. ///
  67. /// @return success configuration status.
  68. ConstElementPtr
  69. dhcp4StubConfigHandler(ConstElementPtr) {
  70. // This configuration handler is intended to be used only
  71. // when the initial configuration comes in. To receive this
  72. // configuration a pointer to this handler must be passed
  73. // using ModuleCCSession constructor. This constructor will
  74. // invoke the handler and will store the configuration for
  75. // the configuration session when the handler returns success.
  76. // Since this configuration is partial we just pretend to
  77. // parse it and always return success. The function that
  78. // initiates the session must get the configuration on its
  79. // own using getFullConfig.
  80. return (isc::config::createAnswer(0, "Configuration accepted."));
  81. }
  82. ConstElementPtr
  83. bundyConfigHandler(ConstElementPtr new_config) {
  84. if (!ControlledDhcpv4Srv::getInstance() || !config_session_) {
  85. // That should never happen as we install config_handler
  86. // after we instantiate the server.
  87. ConstElementPtr answer =
  88. isc::config::createAnswer(1, "Configuration rejected,"
  89. " server is during startup/shutdown phase.");
  90. return (answer);
  91. }
  92. // The configuration passed to this handler function is partial.
  93. // In other words, it just includes the values being modified.
  94. // In the same time, there are dependencies between various
  95. // DHCP configuration parsers. For example: the option value can
  96. // be set if the definition of this option is set. If someone removes
  97. // an existing option definition then the partial configuration that
  98. // removes that definition is triggered while a relevant option value
  99. // may remain configured. This eventually results in the DHCP server
  100. // configuration being in the inconsistent state.
  101. // In order to work around this problem we need to merge the new
  102. // configuration with the existing (full) configuration.
  103. // Let's create a new object that will hold the merged configuration.
  104. boost::shared_ptr<MapElement> merged_config(new MapElement());
  105. // Let's get the existing configuration.
  106. ConstElementPtr full_config = config_session_->getFullConfig();
  107. // The full_config and merged_config should be always non-NULL
  108. // but to provide some level of exception safety we check that they
  109. // really are (in case we go out of memory).
  110. if (full_config && merged_config) {
  111. merged_config->setValue(full_config->mapValue());
  112. // Merge an existing and new configuration.
  113. isc::data::merge(merged_config, new_config);
  114. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND, DHCP4_CONFIG_UPDATE)
  115. .arg(full_config->str());
  116. }
  117. // Configure the server.
  118. return (ControlledDhcpv4Srv::processConfig(merged_config));
  119. }
  120. void ControlledDhcpv4Srv::init(const std::string& config_file) {
  121. // Call base class's init.
  122. Daemon::init(config_file);
  123. string specfile;
  124. if (getenv("KEA_FROM_BUILD")) {
  125. specfile = string(getenv("KEA_FROM_BUILD")) +
  126. "/src/bin/dhcp4/dhcp4.spec";
  127. } else {
  128. specfile = string(DHCP4_SPECFILE_LOCATION);
  129. }
  130. /// @todo: Check if session is not established already. Throw, if it is.
  131. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTING)
  132. .arg(specfile);
  133. cc_session_ = new Session(io_service_.get_io_service());
  134. // Create a session with the dummy configuration handler.
  135. // Dumy configuration handler is internally invoked by the
  136. // constructor and on success the constructor updates
  137. // the current session with the configuration that had been
  138. // committed in the previous session. If we did not install
  139. // the dummy handler, the previous configuration would have
  140. // been lost.
  141. config_session_ = new ModuleCCSession(specfile, *cc_session_,
  142. dhcp4StubConfigHandler,
  143. processCommand, false);
  144. config_session_->start();
  145. // We initially create ModuleCCSession() without configHandler, as
  146. // the session module is too eager to send partial configuration.
  147. // We want to get the full configuration, so we explicitly call
  148. // getFullConfig() and then pass it to our configHandler.
  149. config_session_->setConfigHandler(bundyConfigHandler);
  150. try {
  151. configureDhcp4Server(*this, config_session_->getFullConfig());
  152. // Server will start DDNS communications if its enabled.
  153. server_->startD2();
  154. // Configuration may disable or enable interfaces so we have to
  155. // reopen sockets according to new configuration.
  156. CfgMgr::instance().getConfiguration()->cfg_iface_
  157. .openSockets(getPort(), useBroadcast());
  158. } catch (const std::exception& ex) {
  159. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL).arg(ex.what());
  160. }
  161. /// Integrate the asynchronous I/O model of former BIND 10/Bundy
  162. /// configuration control with the "select" model of the DHCP server.
  163. /// This is fully explained in \ref dhcpv4Session.
  164. int ctrl_socket = cc_session_->getSocketDesc();
  165. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTED)
  166. .arg(ctrl_socket);
  167. IfaceMgr::instance().addExternalSocket(ctrl_socket, sessionReader);
  168. }
  169. void ControlledDhcpv4Srv::cleanup() {
  170. if (config_session_) {
  171. delete config_session_;
  172. config_session_ = NULL;
  173. }
  174. if (cc_session_) {
  175. int ctrl_socket = cc_session_->getSocketDesc();
  176. cc_session_->disconnect();
  177. IfaceMgr::instance().deleteExternalSocket(ctrl_socket);
  178. delete cc_session_;
  179. cc_session_ = NULL;
  180. }
  181. }
  182. void
  183. Daemon::loggerInit(const char* log_name, bool verbose) {
  184. isc::log::initLogger(log_name,
  185. (verbose ? isc::log::DEBUG : isc::log::INFO),
  186. isc::log::MAX_DEBUG_LEVEL, NULL, true);
  187. }
  188. };
  189. };