ctrl_dhcp4_srv.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (C) 2012-2015 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/command_interpreter.h>
  19. #include <dhcpsrv/timer_mgr.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. /// by reading configuration file from disk.
  27. class ControlledDhcpv4Srv : public isc::dhcp::Dhcpv4Srv {
  28. public:
  29. /// @brief Constructor
  30. ///
  31. /// @param port UDP port to be opened for DHCP traffic
  32. ControlledDhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
  33. /// @brief Destructor.
  34. ~ControlledDhcpv4Srv();
  35. /// @brief Initializes the server.
  36. ///
  37. /// Depending on the configuration backend, it establishes msgq session,
  38. /// reads the JSON file from disk or may perform any other setup
  39. /// operation. For specific details, see actual implementation in
  40. /// *_backend.cc
  41. ///
  42. /// This method may throw if initialization fails. Exception types may be
  43. /// specific to used configuration backend.
  44. void init(const std::string& config_file);
  45. /// @brief Performs cleanup, immediately before termination
  46. ///
  47. /// This method performs final clean up, just before the Dhcpv4Srv object
  48. /// is destroyed. Currently it is a no-op.
  49. void cleanup();
  50. /// @brief Initiates shutdown procedure for the whole DHCPv4 server.
  51. void shutdown();
  52. /// @brief Command processor
  53. ///
  54. /// This method is uniform for all config backends. It processes received
  55. /// command (as a string + JSON arguments). Internally, it's just a
  56. /// wrapper that calls process*Command() methods and catches exceptions
  57. /// in them.
  58. ///
  59. /// Currently supported commands are:
  60. /// - shutdown
  61. /// - libreload
  62. /// - config-reload
  63. ///
  64. /// @note It never throws.
  65. ///
  66. /// @param command Text representation of the command (e.g. "shutdown")
  67. /// @param args Optional parameters
  68. ///
  69. /// @return status of the command
  70. static isc::data::ConstElementPtr
  71. processCommand(const std::string& command, isc::data::ConstElementPtr args);
  72. /// @brief Configuration processor
  73. ///
  74. /// This is a method for handling incoming configuration updates.
  75. /// This method should be called by all configuration backends when the
  76. /// server is starting up or when configuration has changed.
  77. ///
  78. /// As pointer to this method is used a callback in ASIO used in
  79. /// ModuleCCSession, it has to be static.
  80. ///
  81. /// @param new_config textual representation of the new configuration
  82. ///
  83. /// @return status of the config update
  84. static isc::data::ConstElementPtr
  85. processConfig(isc::data::ConstElementPtr new_config);
  86. /// @brief Returns pointer to the sole instance of Dhcpv4Srv
  87. ///
  88. /// @return server instance (may return NULL, if called before server is spawned)
  89. static ControlledDhcpv4Srv* getInstance() {
  90. return (server_);
  91. }
  92. private:
  93. /// @brief Callback that will be called from iface_mgr when data
  94. /// is received over control socket.
  95. ///
  96. /// This static callback method is called from IfaceMgr::receive6() method,
  97. /// when there is a new command or configuration sent over control socket
  98. /// (that was sent from some yet unspecified sender).
  99. static void sessionReader(void);
  100. /// @brief Handler for processing 'shutdown' command
  101. ///
  102. /// This handler processes shutdown command, which initializes shutdown
  103. /// procedure.
  104. /// @param command (parameter ignored)
  105. /// @param args (parameter ignored)
  106. ///
  107. /// @return status of the command
  108. isc::data::ConstElementPtr
  109. commandShutdownHandler(const std::string& command,
  110. isc::data::ConstElementPtr args);
  111. /// @brief Handler for processing 'libreload' command
  112. ///
  113. /// This handler processes libreload command, which unloads all hook
  114. /// libraries and reloads them.
  115. ///
  116. /// @param command (parameter ignored)
  117. /// @param args (parameter ignored)
  118. ///
  119. /// @return status of the command
  120. isc::data::ConstElementPtr
  121. commandLibReloadHandler(const std::string& command,
  122. isc::data::ConstElementPtr args);
  123. /// @brief Handler for processing 'config-reload' command
  124. ///
  125. /// This handler processes config-reload command, which processes
  126. /// configuration specified in args parameter.
  127. ///
  128. /// @param command (parameter ignored)
  129. /// @param args configuration to be processed
  130. ///
  131. /// @return status of the command
  132. isc::data::ConstElementPtr
  133. commandConfigReloadHandler(const std::string& command,
  134. isc::data::ConstElementPtr args);
  135. /// @brief handler for processing 'leases-reclaim' command
  136. ///
  137. /// This handler processes leases-reclaim command, which triggers
  138. /// the leases reclamation immediately
  139. ///
  140. /// @param command (parameter ignored)
  141. /// @param args (parameter ignored)
  142. ///
  143. /// @return status of the command
  144. isc::data::ConstElementPtr
  145. commandLeasesReclaimHandler(const std::string& command,
  146. isc::data::ConstElementPtr args);
  147. /// @brief Reclaims expired IPv4 leases and reschedules timer.
  148. ///
  149. /// This is a wrapper method for @c AllocEngine::reclaimExpiredLeases4.
  150. /// It reschedules the timer for leases reclamation upon completion of
  151. /// this method.
  152. ///
  153. /// @param max_leases Maximum number of leases to be reclaimed.
  154. /// @param timeout Maximum amount of time that the reclaimation routine
  155. /// may be processing expired leases, expressed in milliseconds.
  156. /// @param remove_lease A boolean value indicating if the lease should
  157. /// be removed when it is reclaimed (if true) or it should be left in the
  158. /// database in the "expired-reclaimed" state (if false).
  159. /// @param max_unwarned_cycles A number of consecutive processing cycles
  160. /// of expired leases, after which the system issues a warning if there
  161. /// are still expired leases in the database. If this value is 0, the
  162. /// warning is never issued.
  163. void reclaimExpiredLeases(const size_t max_leases, const uint16_t timeout,
  164. const bool remove_lease,
  165. const uint16_t max_unwarned_cycles);
  166. /// @brief Deletes reclaimed leases and reschedules the timer.
  167. ///
  168. /// This is a wrapper method for @c AllocEngine::deleteExpiredReclaimed4.
  169. /// It reschedules the timer for leases reclamation upon completion of
  170. /// this method.
  171. ///
  172. /// @param secs Minimum number of seconds after which a lease can be
  173. /// deleted.
  174. void deleteExpiredReclaimedLeases(const uint32_t secs);
  175. /// @brief Static pointer to the sole instance of the DHCP server.
  176. ///
  177. /// This is required for config and command handlers to gain access to
  178. /// the server
  179. static ControlledDhcpv4Srv* server_;
  180. /// @brief IOService object, used for all ASIO operations.
  181. isc::asiolink::IOService io_service_;
  182. /// @brief Instance of the @c TimerMgr.
  183. ///
  184. /// Shared pointer to the instance of timer @c TimerMgr is held here to
  185. /// make sure that the @c TimerMgr outlives instance of this class.
  186. TimerMgrPtr timer_mgr_;
  187. };
  188. }; // namespace isc::dhcp
  189. }; // namespace isc
  190. #endif