ctrl_dhcp4_srv.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <cassert>
  16. #include <iostream>
  17. #include <cc/session.h>
  18. #include <cc/data.h>
  19. #include <exceptions/exceptions.h>
  20. #include <cc/session.h>
  21. #include <config/ccsession.h>
  22. #include <util/buffer.h>
  23. #include <dhcp4/spec_config.h>
  24. #include <dhcp4/ctrl_dhcp4_srv.h>
  25. #include <dhcp/iface_mgr.h>
  26. #include <asiolink/asiolink.h>
  27. using namespace std;
  28. using namespace isc::util;
  29. using namespace isc::dhcp;
  30. using namespace isc::util;
  31. using namespace isc::data;
  32. using namespace isc::cc;
  33. using namespace isc::config;
  34. using namespace isc::asiolink;
  35. namespace isc {
  36. namespace dhcp {
  37. ControlledDhcpv4Srv* ControlledDhcpv4Srv::server_ = NULL;
  38. ConstElementPtr
  39. ControlledDhcpv4Srv::dhcp4ConfigHandler(ConstElementPtr new_config) {
  40. cout << "b10-dhcp4: Received new config:" << new_config->str() << endl;
  41. ConstElementPtr answer = isc::config::createAnswer(0,
  42. "Thank you for sending config.");
  43. return (answer);
  44. }
  45. ConstElementPtr
  46. ControlledDhcpv4Srv::dhcp4CommandHandler(const string& command, ConstElementPtr args) {
  47. cout << "b10-dhcp4: Received new command: [" << command << "], args="
  48. << args->str() << endl;
  49. if (command == "shutdown") {
  50. if (ControlledDhcpv4Srv::server_) {
  51. ControlledDhcpv4Srv::server_->shutdown();
  52. } else {
  53. cout << "Server not initialized yet or already shut down." << endl;
  54. ConstElementPtr answer = isc::config::createAnswer(1,
  55. "Shutdown failure.");
  56. return (answer);
  57. }
  58. ConstElementPtr answer = isc::config::createAnswer(0,
  59. "Shutting down.");
  60. return (answer);
  61. }
  62. ConstElementPtr answer = isc::config::createAnswer(1,
  63. "Unrecognized command.");
  64. return (answer);
  65. }
  66. void ControlledDhcpv4Srv::sessionReader(void) {
  67. // Process one asio event. If there are more events, iface_mgr will call
  68. // this callback more than once.
  69. if (server_) {
  70. server_->io_service_.run_one();
  71. }
  72. }
  73. void ControlledDhcpv4Srv::establishSession() {
  74. string specfile;
  75. if (getenv("B10_FROM_BUILD")) {
  76. specfile = string(getenv("B10_FROM_BUILD")) +
  77. "/src/bin/dhcp4/dhcp4.spec";
  78. } else {
  79. specfile = string(DHCP4_SPECFILE_LOCATION);
  80. }
  81. /// @todo: Check if session is not established already. Throw, if it is.
  82. cout << "b10-dhcp4: my specfile is " << specfile << endl;
  83. cc_session_ = new Session(io_service_.get_io_service());
  84. config_session_ = new ModuleCCSession(specfile, *cc_session_,
  85. dhcp4ConfigHandler,
  86. dhcp4CommandHandler, false);
  87. config_session_->start();
  88. /// Integrate the asynchronous I/O model of BIND 10 configuration
  89. /// control with the "select" model of the DHCP server. This is
  90. /// fully explained in \ref dhcpv4Session.
  91. int ctrl_socket = cc_session_->getSocketDesc();
  92. cout << "b10-dhcp4: Control session started, socket="
  93. << ctrl_socket << endl;
  94. IfaceMgr::instance().set_session_socket(ctrl_socket, sessionReader);
  95. }
  96. void ControlledDhcpv4Srv::disconnectSession() {
  97. if (config_session_) {
  98. delete config_session_;
  99. config_session_ = NULL;
  100. }
  101. if (cc_session_) {
  102. cc_session_->disconnect();
  103. delete cc_session_;
  104. cc_session_ = NULL;
  105. }
  106. // deregister session socket
  107. IfaceMgr::instance().set_session_socket(IfaceMgr::INVALID_SOCKET, NULL);
  108. }
  109. ControlledDhcpv4Srv::ControlledDhcpv4Srv(uint16_t port /*= DHCP4_SERVER_PORT*/)
  110. :Dhcpv4Srv(port), cc_session_(NULL), config_session_(NULL) {
  111. server_ = this; // remember this instance for use in callback
  112. }
  113. void ControlledDhcpv4Srv::shutdown() {
  114. io_service_.stop(); // Stop ASIO transmissions
  115. Dhcpv4Srv::shutdown(); // Initiate DHCPv4 shutdown procedure.
  116. }
  117. ControlledDhcpv4Srv::~ControlledDhcpv4Srv() {
  118. disconnectSession();
  119. server_ = NULL; // forget this instance. There should be no callback anymore
  120. // at this stage anyway.
  121. }
  122. isc::data::ConstElementPtr
  123. ControlledDhcpv4Srv::execDhcpv4ServerCommand(const std::string& command_id,
  124. isc::data::ConstElementPtr args) {
  125. try {
  126. return (dhcp4CommandHandler(command_id, args));
  127. } catch (const Exception& ex) {
  128. ConstElementPtr answer = isc::config::createAnswer(1, ex.what());
  129. return (answer);
  130. }
  131. }
  132. };
  133. };