ctrl_dhcp4_srv.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (C) 2012 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 <dhcpsrv/dhcp_config_parser.h>
  21. #include <dhcp4/ctrl_dhcp4_srv.h>
  22. #include <dhcp4/dhcp4_log.h>
  23. #include <dhcp4/spec_config.h>
  24. #include <dhcp4/config_parser.h>
  25. #include <exceptions/exceptions.h>
  26. #include <util/buffer.h>
  27. #include <cassert>
  28. #include <iostream>
  29. #include <cassert>
  30. #include <iostream>
  31. using namespace isc::asiolink;
  32. using namespace isc::cc;
  33. using namespace isc::config;
  34. using namespace isc::data;
  35. using namespace isc::dhcp;
  36. using namespace isc::log;
  37. using namespace isc::util;
  38. using namespace std;
  39. namespace isc {
  40. namespace dhcp {
  41. ControlledDhcpv4Srv* ControlledDhcpv4Srv::server_ = NULL;
  42. ConstElementPtr
  43. ControlledDhcpv4Srv::dhcp4StubConfigHandler(ConstElementPtr) {
  44. // This configuration handler is intended to be used only
  45. // when the initial configuration comes in. To receive this
  46. // configuration a pointer to this handler must be passed
  47. // using ModuleCCSession constructor. This constructor will
  48. // invoke the handler and will store the configuration for
  49. // the configuration session when the handler returns success.
  50. // Since this configuration is partial we just pretend to
  51. // parse it and always return success. The function that
  52. // initiates the session must get the configuration on its
  53. // own using getFullConfig.
  54. return (isc::config::createAnswer(0, "Configuration accepted."));
  55. }
  56. ConstElementPtr
  57. ControlledDhcpv4Srv::dhcp4ConfigHandler(ConstElementPtr new_config) {
  58. if (!server_ || !server_->config_session_) {
  59. // That should never happen as we install config_handler
  60. // after we instantiate the server.
  61. ConstElementPtr answer =
  62. isc::config::createAnswer(1, "Configuration rejected,"
  63. " server is during startup/shutdown phase.");
  64. return (answer);
  65. }
  66. // The configuration passed to this handler function is partial.
  67. // In other words, it just includes the values being modified.
  68. // In the same time, there are dependencies between various
  69. // DHCP configuration parsers. For example: the option value can
  70. // be set if the definition of this option is set. If someone removes
  71. // an existing option definition then the partial configuration that
  72. // removes that definition is triggered while a relevant option value
  73. // may remain configured. This eventually results in the DHCP server
  74. // configuration being in the inconsistent state.
  75. // In order to work around this problem we need to merge the new
  76. // configuration with the existing (full) configuration.
  77. // Let's create a new object that will hold the merged configuration.
  78. boost::shared_ptr<MapElement> merged_config(new MapElement());
  79. // Let's get the existing configuration.
  80. ConstElementPtr full_config = server_->config_session_->getFullConfig();
  81. // The full_config and merged_config should be always non-NULL
  82. // but to provide some level of exception safety we check that they
  83. // really are (in case we go out of memory).
  84. if (full_config && merged_config) {
  85. merged_config->setValue(full_config->mapValue());
  86. // Merge an existing and new configuration.
  87. isc::data::merge(merged_config, new_config);
  88. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND, DHCP4_CONFIG_UPDATE)
  89. .arg(full_config->str());
  90. }
  91. // Configure the server.
  92. return (configureDhcp4Server(*server_, merged_config));
  93. }
  94. ConstElementPtr
  95. ControlledDhcpv4Srv::dhcp4CommandHandler(const string& command, ConstElementPtr args) {
  96. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND, DHCP4_COMMAND_RECEIVED)
  97. .arg(command).arg(args->str());
  98. if (command == "shutdown") {
  99. if (ControlledDhcpv4Srv::server_) {
  100. ControlledDhcpv4Srv::server_->shutdown();
  101. } else {
  102. LOG_WARN(dhcp4_logger, DHCP4_NOT_RUNNING);
  103. ConstElementPtr answer = isc::config::createAnswer(1,
  104. "Shutdown failure.");
  105. return (answer);
  106. }
  107. ConstElementPtr answer = isc::config::createAnswer(0,
  108. "Shutting down.");
  109. return (answer);
  110. } else if (command == "libreload") {
  111. // TODO Reload libraries
  112. }
  113. ConstElementPtr answer = isc::config::createAnswer(1,
  114. "Unrecognized command.");
  115. return (answer);
  116. }
  117. void ControlledDhcpv4Srv::sessionReader(void) {
  118. // Process one asio event. If there are more events, iface_mgr will call
  119. // this callback more than once.
  120. if (server_) {
  121. server_->io_service_.run_one();
  122. }
  123. }
  124. void ControlledDhcpv4Srv::establishSession() {
  125. string specfile;
  126. if (getenv("B10_FROM_BUILD")) {
  127. specfile = string(getenv("B10_FROM_BUILD")) +
  128. "/src/bin/dhcp4/dhcp4.spec";
  129. } else {
  130. specfile = string(DHCP4_SPECFILE_LOCATION);
  131. }
  132. /// @todo: Check if session is not established already. Throw, if it is.
  133. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTING)
  134. .arg(specfile);
  135. cc_session_ = new Session(io_service_.get_io_service());
  136. // Create a session with the dummy configuration handler.
  137. // Dumy configuration handler is internally invoked by the
  138. // constructor and on success the constructor updates
  139. // the current session with the configuration that had been
  140. // committed in the previous session. If we did not install
  141. // the dummy handler, the previous configuration would have
  142. // been lost.
  143. config_session_ = new ModuleCCSession(specfile, *cc_session_,
  144. dhcp4StubConfigHandler,
  145. dhcp4CommandHandler, false);
  146. config_session_->start();
  147. // We initially create ModuleCCSession() without configHandler, as
  148. // the session module is too eager to send partial configuration.
  149. // We want to get the full configuration, so we explicitly call
  150. // getFullConfig() and then pass it to our configHandler.
  151. config_session_->setConfigHandler(dhcp4ConfigHandler);
  152. try {
  153. configureDhcp4Server(*this, config_session_->getFullConfig());
  154. } catch (const DhcpConfigError& ex) {
  155. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL).arg(ex.what());
  156. }
  157. /// Integrate the asynchronous I/O model of BIND 10 configuration
  158. /// control with the "select" model of the DHCP server. This is
  159. /// fully explained in \ref dhcpv4Session.
  160. int ctrl_socket = cc_session_->getSocketDesc();
  161. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTED)
  162. .arg(ctrl_socket);
  163. IfaceMgr::instance().set_session_socket(ctrl_socket, sessionReader);
  164. }
  165. void ControlledDhcpv4Srv::disconnectSession() {
  166. if (config_session_) {
  167. delete config_session_;
  168. config_session_ = NULL;
  169. }
  170. if (cc_session_) {
  171. cc_session_->disconnect();
  172. delete cc_session_;
  173. cc_session_ = NULL;
  174. }
  175. // deregister session socket
  176. IfaceMgr::instance().set_session_socket(IfaceMgr::INVALID_SOCKET, NULL);
  177. }
  178. ControlledDhcpv4Srv::ControlledDhcpv4Srv(uint16_t port /*= DHCP4_SERVER_PORT*/)
  179. :Dhcpv4Srv(port), cc_session_(NULL), config_session_(NULL) {
  180. server_ = this; // remember this instance for use in callback
  181. }
  182. void ControlledDhcpv4Srv::shutdown() {
  183. io_service_.stop(); // Stop ASIO transmissions
  184. Dhcpv4Srv::shutdown(); // Initiate DHCPv4 shutdown procedure.
  185. }
  186. ControlledDhcpv4Srv::~ControlledDhcpv4Srv() {
  187. disconnectSession();
  188. server_ = NULL; // forget this instance. There should be no callback anymore
  189. // at this stage anyway.
  190. }
  191. isc::data::ConstElementPtr
  192. ControlledDhcpv4Srv::execDhcpv4ServerCommand(const std::string& command_id,
  193. isc::data::ConstElementPtr args) {
  194. try {
  195. return (dhcp4CommandHandler(command_id, args));
  196. } catch (const Exception& ex) {
  197. ConstElementPtr answer = isc::config::createAnswer(1, ex.what());
  198. return (answer);
  199. }
  200. }
  201. };
  202. };