ctrl_dhcp4_srv.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright (C) 2012-2013 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 <dhcpsrv/cfgmgr.h>
  22. #include <dhcp4/ctrl_dhcp4_srv.h>
  23. #include <dhcp4/dhcp4_log.h>
  24. #include <dhcp4/spec_config.h>
  25. #include <dhcp4/config_parser.h>
  26. #include <exceptions/exceptions.h>
  27. #include <util/buffer.h>
  28. #include <cassert>
  29. #include <iostream>
  30. #include <sstream>
  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. ConstElementPtr answer = configureDhcp4Server(*server_, merged_config);
  93. // Check that configuration was successful. If not, do not reopen sockets.
  94. int rcode = 0;
  95. parseAnswer(rcode, answer);
  96. if (rcode != 0) {
  97. return (answer);
  98. }
  99. // Configuration may change active interfaces. Therefore, we have to reopen
  100. // sockets according to new configuration. This operation is not exception
  101. // safe and we really don't want to emit exceptions to the callback caller.
  102. // Instead, catch an exception and create appropriate answer.
  103. try {
  104. server_->openActiveSockets(server_->getPort(), server_->useBroadcast());
  105. } catch (std::exception& ex) {
  106. std::ostringstream err;
  107. err << "failed to open sockets after server reconfiguration: " << ex.what();
  108. answer = isc::config::createAnswer(1, err.str());
  109. }
  110. return (answer);
  111. }
  112. ConstElementPtr
  113. ControlledDhcpv4Srv::dhcp4CommandHandler(const string& command, ConstElementPtr args) {
  114. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND, DHCP4_COMMAND_RECEIVED)
  115. .arg(command).arg(args->str());
  116. if (command == "shutdown") {
  117. if (ControlledDhcpv4Srv::server_) {
  118. ControlledDhcpv4Srv::server_->shutdown();
  119. } else {
  120. LOG_WARN(dhcp4_logger, DHCP4_NOT_RUNNING);
  121. ConstElementPtr answer = isc::config::createAnswer(1,
  122. "Shutdown failure.");
  123. return (answer);
  124. }
  125. ConstElementPtr answer = isc::config::createAnswer(0,
  126. "Shutting down.");
  127. return (answer);
  128. }
  129. ConstElementPtr answer = isc::config::createAnswer(1,
  130. "Unrecognized command.");
  131. return (answer);
  132. }
  133. void ControlledDhcpv4Srv::sessionReader(void) {
  134. // Process one asio event. If there are more events, iface_mgr will call
  135. // this callback more than once.
  136. if (server_) {
  137. server_->io_service_.run_one();
  138. }
  139. }
  140. void ControlledDhcpv4Srv::establishSession() {
  141. string specfile;
  142. if (getenv("B10_FROM_BUILD")) {
  143. specfile = string(getenv("B10_FROM_BUILD")) +
  144. "/src/bin/dhcp4/dhcp4.spec";
  145. } else {
  146. specfile = string(DHCP4_SPECFILE_LOCATION);
  147. }
  148. /// @todo: Check if session is not established already. Throw, if it is.
  149. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTING)
  150. .arg(specfile);
  151. cc_session_ = new Session(io_service_.get_io_service());
  152. // Create a session with the dummy configuration handler.
  153. // Dumy configuration handler is internally invoked by the
  154. // constructor and on success the constructor updates
  155. // the current session with the configuration that had been
  156. // committed in the previous session. If we did not install
  157. // the dummy handler, the previous configuration would have
  158. // been lost.
  159. config_session_ = new ModuleCCSession(specfile, *cc_session_,
  160. dhcp4StubConfigHandler,
  161. dhcp4CommandHandler, false);
  162. config_session_->start();
  163. // We initially create ModuleCCSession() without configHandler, as
  164. // the session module is too eager to send partial configuration.
  165. // We want to get the full configuration, so we explicitly call
  166. // getFullConfig() and then pass it to our configHandler.
  167. config_session_->setConfigHandler(dhcp4ConfigHandler);
  168. try {
  169. configureDhcp4Server(*this, config_session_->getFullConfig());
  170. // Configuration may disable or enable interfaces so we have to
  171. // reopen sockets according to new configuration.
  172. openActiveSockets(getPort(), useBroadcast());
  173. } catch (const DhcpConfigError& ex) {
  174. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL).arg(ex.what());
  175. }
  176. /// Integrate the asynchronous I/O model of BIND 10 configuration
  177. /// control with the "select" model of the DHCP server. This is
  178. /// fully explained in \ref dhcpv4Session.
  179. int ctrl_socket = cc_session_->getSocketDesc();
  180. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTED)
  181. .arg(ctrl_socket);
  182. IfaceMgr::instance().set_session_socket(ctrl_socket, sessionReader);
  183. }
  184. void ControlledDhcpv4Srv::disconnectSession() {
  185. if (config_session_) {
  186. delete config_session_;
  187. config_session_ = NULL;
  188. }
  189. if (cc_session_) {
  190. cc_session_->disconnect();
  191. delete cc_session_;
  192. cc_session_ = NULL;
  193. }
  194. // deregister session socket
  195. IfaceMgr::instance().set_session_socket(IfaceMgr::INVALID_SOCKET, NULL);
  196. }
  197. ControlledDhcpv4Srv::ControlledDhcpv4Srv(uint16_t port /*= DHCP4_SERVER_PORT*/)
  198. :Dhcpv4Srv(port), cc_session_(NULL), config_session_(NULL) {
  199. server_ = this; // remember this instance for use in callback
  200. }
  201. void ControlledDhcpv4Srv::shutdown() {
  202. io_service_.stop(); // Stop ASIO transmissions
  203. Dhcpv4Srv::shutdown(); // Initiate DHCPv4 shutdown procedure.
  204. }
  205. ControlledDhcpv4Srv::~ControlledDhcpv4Srv() {
  206. disconnectSession();
  207. server_ = NULL; // forget this instance. There should be no callback anymore
  208. // at this stage anyway.
  209. }
  210. isc::data::ConstElementPtr
  211. ControlledDhcpv4Srv::execDhcpv4ServerCommand(const std::string& command_id,
  212. isc::data::ConstElementPtr args) {
  213. try {
  214. return (dhcp4CommandHandler(command_id, args));
  215. } catch (const Exception& ex) {
  216. ConstElementPtr answer = isc::config::createAnswer(1, ex.what());
  217. return (answer);
  218. }
  219. }
  220. };
  221. };