ctrl_dhcp4_srv.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #ifndef CTRL_DHCPV4_SRV_H
  15. #define CTRL_DHCPV4_SRV_H
  16. #include <asiolink/asiolink.h>
  17. #include <cc/data.h>
  18. #include <cc/session.h>
  19. #include <config/ccsession.h>
  20. #include <dhcp4/dhcp4_srv.h>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief Controlled version of the DHCPv4 server
  24. ///
  25. /// This is a class that is responsible for DHCPv4 server being controllable.
  26. /// It does various things, depending on the configuration backend.
  27. /// For Bundy backend it establishes a connection with msqg and later receives
  28. /// commands over it. For Kea backend, it reads configuration file from disk.
  29. ///
  30. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  31. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  32. class ControlledDhcpv4Srv : public isc::dhcp::Dhcpv4Srv {
  33. public:
  34. /// @brief Constructor
  35. ///
  36. /// @param port UDP port to be opened for DHCP traffic
  37. ControlledDhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
  38. /// @brief Destructor.
  39. ~ControlledDhcpv4Srv();
  40. /// @brief Initializes the server.
  41. ///
  42. /// Depending on the configuration backend, it establishes msgq session,
  43. /// reads the JSON file from disk or may perform any other setup
  44. /// operation. For specific details, see actual implementation in
  45. /// *_backend.cc
  46. ///
  47. /// This method may throw if initialization fails. Exception types may be
  48. /// specific to used configuration backend.
  49. void init(const std::string& config_file);
  50. /// @brief Performs cleanup, immediately before termination
  51. ///
  52. /// This method performs final clean up, just before the Dhcpv4Srv object
  53. /// is destroyed. The actual behavior is backend dependent. For Bundy
  54. /// backend, it terminates existing session with msgq. After calling
  55. /// it, no further messages over msgq (commands or configuration updates)
  56. /// may be received. For JSON backend, it is no-op.
  57. ///
  58. /// For specific details, see actual implementation in *_backend.cc
  59. void cleanup();
  60. /// @brief Initiates shutdown procedure for the whole DHCPv4 server.
  61. void shutdown();
  62. /// @brief Command processor
  63. ///
  64. /// This method is uniform for all config backends. It processes received
  65. /// command (as a string + JSON arguments). Internally, it's just a
  66. /// wrapper that calls process*Command() methods and catches exceptions
  67. /// in them.
  68. ///
  69. /// Currently supported commands are:
  70. /// - shutdown
  71. /// - libreload
  72. /// - config-reload
  73. ///
  74. /// @note It never throws.
  75. ///
  76. /// @param command Text represenation of the command (e.g. "shutdown")
  77. /// @param args Optional parameters
  78. ///
  79. /// @return status of the command
  80. static isc::data::ConstElementPtr
  81. processCommand(const std::string& command, isc::data::ConstElementPtr args);
  82. /// @brief Configuration processor
  83. ///
  84. /// This is a method for handling incoming configuration updates.
  85. /// This method should be called by all configuration backends when the
  86. /// server is starting up or when configuration has changed.
  87. ///
  88. /// As pointer to this method is used a callback in ASIO used in
  89. /// ModuleCCSession, it has to be static.
  90. ///
  91. /// @param new_config textual representation of the new configuration
  92. ///
  93. /// @return status of the config update
  94. static isc::data::ConstElementPtr
  95. processConfig(isc::data::ConstElementPtr new_config);
  96. /// @brief Returns pointer to the sole instance of Dhcpv4Srv
  97. ///
  98. /// @return server instance (may return NULL, if called before server is spawned)
  99. static ControlledDhcpv4Srv* getInstance() {
  100. return (server_);
  101. }
  102. protected:
  103. /// @brief Static pointer to the sole instance of the DHCP server.
  104. ///
  105. /// This is required for config and command handlers to gain access to
  106. /// the server
  107. static ControlledDhcpv4Srv* server_;
  108. /// @brief Callback that will be called from iface_mgr when data
  109. /// is received over control socket.
  110. ///
  111. /// This static callback method is called from IfaceMgr::receive6() method,
  112. /// when there is a new command or configuration sent over control socket
  113. /// (that was sent from msgq if backend is Bundy, or some yet unspecified
  114. /// sender if the backend is JSON file).
  115. static void sessionReader(void);
  116. /// @brief IOService object, used for all ASIO operations.
  117. isc::asiolink::IOService io_service_;
  118. /// @brief Handler for processing 'shutdown' command
  119. ///
  120. /// This handler processes shutdown command, which initializes shutdown
  121. /// procedure.
  122. /// @param command (parameter ignored)
  123. /// @param args (parameter ignored)
  124. ///
  125. /// @return status of the command
  126. isc::data::ConstElementPtr
  127. commandShutdownHandler(const std::string& command,
  128. isc::data::ConstElementPtr args);
  129. /// @brief Handler for processing 'libreload' command
  130. ///
  131. /// This handler processes libreload command, which unloads all hook
  132. /// libraries and reloads them.
  133. ///
  134. /// @param command (parameter ignored)
  135. /// @param args (parameter ignored)
  136. ///
  137. /// @return status of the command
  138. isc::data::ConstElementPtr
  139. commandLibReloadHandler(const std::string& command,
  140. isc::data::ConstElementPtr args);
  141. /// @brief Handler for processing 'config-reload' command
  142. ///
  143. /// This handler processes config-reload command, which processes
  144. /// configuration specified in args parameter.
  145. ///
  146. /// @param command (parameter ignored)
  147. /// @param args configuration to be processed
  148. ///
  149. /// @return status of the command
  150. isc::data::ConstElementPtr
  151. commandConfigReloadHandler(const std::string& command,
  152. isc::data::ConstElementPtr args);
  153. };
  154. }; // namespace isc::dhcp
  155. }; // namespace isc
  156. #endif