ctrl_dhcp4_srv.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. } else if (command == "libreload") {
  129. // TODO Reload libraries
  130. }
  131. ConstElementPtr answer = isc::config::createAnswer(1,
  132. "Unrecognized command.");
  133. return (answer);
  134. }
  135. void ControlledDhcpv4Srv::sessionReader(void) {
  136. // Process one asio event. If there are more events, iface_mgr will call
  137. // this callback more than once.
  138. if (server_) {
  139. server_->io_service_.run_one();
  140. }
  141. }
  142. void ControlledDhcpv4Srv::establishSession() {
  143. string specfile;
  144. if (getenv("B10_FROM_BUILD")) {
  145. specfile = string(getenv("B10_FROM_BUILD")) +
  146. "/src/bin/dhcp4/dhcp4.spec";
  147. } else {
  148. specfile = string(DHCP4_SPECFILE_LOCATION);
  149. }
  150. /// @todo: Check if session is not established already. Throw, if it is.
  151. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTING)
  152. .arg(specfile);
  153. cc_session_ = new Session(io_service_.get_io_service());
  154. // Create a session with the dummy configuration handler.
  155. // Dumy configuration handler is internally invoked by the
  156. // constructor and on success the constructor updates
  157. // the current session with the configuration that had been
  158. // committed in the previous session. If we did not install
  159. // the dummy handler, the previous configuration would have
  160. // been lost.
  161. config_session_ = new ModuleCCSession(specfile, *cc_session_,
  162. dhcp4StubConfigHandler,
  163. dhcp4CommandHandler, false);
  164. config_session_->start();
  165. // We initially create ModuleCCSession() without configHandler, as
  166. // the session module is too eager to send partial configuration.
  167. // We want to get the full configuration, so we explicitly call
  168. // getFullConfig() and then pass it to our configHandler.
  169. config_session_->setConfigHandler(dhcp4ConfigHandler);
  170. try {
  171. configureDhcp4Server(*this, config_session_->getFullConfig());
  172. // Configuration may disable or enable interfaces so we have to
  173. // reopen sockets according to new configuration.
  174. openActiveSockets(getPort(), useBroadcast());
  175. } catch (const DhcpConfigError& ex) {
  176. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL).arg(ex.what());
  177. }
  178. /// Integrate the asynchronous I/O model of BIND 10 configuration
  179. /// control with the "select" model of the DHCP server. This is
  180. /// fully explained in \ref dhcpv4Session.
  181. int ctrl_socket = cc_session_->getSocketDesc();
  182. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_CCSESSION_STARTED)
  183. .arg(ctrl_socket);
  184. IfaceMgr::instance().set_session_socket(ctrl_socket, sessionReader);
  185. }
  186. void ControlledDhcpv4Srv::disconnectSession() {
  187. if (config_session_) {
  188. delete config_session_;
  189. config_session_ = NULL;
  190. }
  191. if (cc_session_) {
  192. cc_session_->disconnect();
  193. delete cc_session_;
  194. cc_session_ = NULL;
  195. }
  196. // deregister session socket
  197. IfaceMgr::instance().set_session_socket(IfaceMgr::INVALID_SOCKET, NULL);
  198. }
  199. ControlledDhcpv4Srv::ControlledDhcpv4Srv(uint16_t port /*= DHCP4_SERVER_PORT*/)
  200. :Dhcpv4Srv(port), cc_session_(NULL), config_session_(NULL) {
  201. server_ = this; // remember this instance for use in callback
  202. }
  203. void ControlledDhcpv4Srv::shutdown() {
  204. io_service_.stop(); // Stop ASIO transmissions
  205. Dhcpv4Srv::shutdown(); // Initiate DHCPv4 shutdown procedure.
  206. }
  207. ControlledDhcpv4Srv::~ControlledDhcpv4Srv() {
  208. disconnectSession();
  209. server_ = NULL; // forget this instance. There should be no callback anymore
  210. // at this stage anyway.
  211. }
  212. isc::data::ConstElementPtr
  213. ControlledDhcpv4Srv::execDhcpv4ServerCommand(const std::string& command_id,
  214. isc::data::ConstElementPtr args) {
  215. try {
  216. return (dhcp4CommandHandler(command_id, args));
  217. } catch (const Exception& ex) {
  218. ConstElementPtr answer = isc::config::createAnswer(1, ex.what());
  219. return (answer);
  220. }
  221. }
  222. };
  223. };