ctrl_dhcp6_srv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef CTRL_DHCPV6_SRV_H
  7. #define CTRL_DHCPV6_SRV_H
  8. #include <asiolink/asio_wrapper.h>
  9. #include <asiolink/asiolink.h>
  10. #include <cc/data.h>
  11. #include <cc/command_interpreter.h>
  12. #include <dhcpsrv/timer_mgr.h>
  13. #include <dhcp6/dhcp6_srv.h>
  14. namespace isc {
  15. namespace dhcp {
  16. /// @brief Controlled version of the DHCPv6 server
  17. ///
  18. /// This is a class that is responsible for DHCPv6 server being controllable,
  19. /// by reading configuration file from disk.
  20. class ControlledDhcpv6Srv : public isc::dhcp::Dhcpv6Srv {
  21. public:
  22. /// @brief Constructor
  23. ///
  24. /// @param port UDP port to be opened for DHCP traffic
  25. ControlledDhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT);
  26. /// @brief Destructor.
  27. virtual ~ControlledDhcpv6Srv();
  28. /// @brief Initializes the server.
  29. ///
  30. /// It reads the JSON file from disk or may perform any other setup
  31. /// operation. In particular, it also install signal handlers.
  32. ///
  33. /// This method may throw if initialization fails.
  34. void init(const std::string& config_file);
  35. /// @brief loads specific configuration file
  36. ///
  37. /// This utility method is called whenever we know a filename of the config
  38. /// and need to load it. It calls config-set command once the content of
  39. /// the file has been loaded and verified to be a sane JSON configuration.
  40. /// config-set handler will process the config file (apply it as current
  41. /// configuration).
  42. ///
  43. /// @param file_name name of the file to be loaded
  44. /// @return status of the file loading and outcome of config-set
  45. isc::data::ConstElementPtr
  46. loadConfigFile(const std::string& file_name);
  47. /// @brief Performs cleanup, immediately before termination
  48. ///
  49. /// This method performs final clean up, just before the Dhcpv6Srv object
  50. /// is destroyed. Currently it is a no-op.
  51. void cleanup();
  52. /// @brief Initiates shutdown procedure for the whole DHCPv6 server.
  53. void shutdown();
  54. /// @brief command processor
  55. ///
  56. /// This method is uniform for all config backends. It processes received
  57. /// command (as a string + JSON arguments). Internally, it's just a
  58. /// wrapper that calls process*Command() methods and catches exceptions
  59. /// in them.
  60. ///
  61. /// Currently supported commands are:
  62. /// - config-reload
  63. /// - config-test
  64. /// - leases-reclaim
  65. /// - libreload
  66. /// - shutdown
  67. /// ...
  68. ///
  69. /// @note It never throws.
  70. ///
  71. /// @param command Text representation of the command (e.g. "shutdown")
  72. /// @param args Optional parameters
  73. ///
  74. /// @return status of the command
  75. static isc::data::ConstElementPtr
  76. processCommand(const std::string& command, isc::data::ConstElementPtr args);
  77. /// @brief configuration processor
  78. ///
  79. /// This is a method for handling incoming configuration updates.
  80. /// This method should be called by all configuration backends when the
  81. /// server is starting up or when configuration has changed.
  82. ///
  83. /// As pointer to this method is used a callback in ASIO used in
  84. /// ModuleCCSession, it has to be static.
  85. ///
  86. /// @param new_config textual representation of the new configuration
  87. ///
  88. /// @return status of the config update
  89. static isc::data::ConstElementPtr
  90. processConfig(isc::data::ConstElementPtr new_config);
  91. /// @brief Configuration checker
  92. ///
  93. /// This is a method for checking incoming configuration.
  94. ///
  95. /// @param new_config JSON representation of the new configuration
  96. ///
  97. /// @return status of the config check
  98. isc::data::ConstElementPtr
  99. checkConfig(isc::data::ConstElementPtr new_config);
  100. /// @brief returns pointer to the sole instance of Dhcpv6Srv
  101. ///
  102. /// @return server instance (may return NULL, if called before server is spawned)
  103. static ControlledDhcpv6Srv* getInstance() {
  104. return (server_);
  105. }
  106. private:
  107. /// @brief Callback that will be called from iface_mgr when data
  108. /// is received over control socket.
  109. ///
  110. /// This static callback method is called from IfaceMgr::receive6() method,
  111. /// when there is a new command or configuration sent over control socket
  112. /// (that was sent from some yet unspecified sender).
  113. static void sessionReader(void);
  114. /// @brief handler for processing 'shutdown' command
  115. ///
  116. /// This handler processes shutdown command, which initializes shutdown
  117. /// procedure.
  118. /// @param command (parameter ignored)
  119. /// @param args (parameter ignored)
  120. ///
  121. /// @return status of the command
  122. isc::data::ConstElementPtr
  123. commandShutdownHandler(const std::string& command,
  124. isc::data::ConstElementPtr args);
  125. /// @brief handler for processing 'libreload' command
  126. ///
  127. /// This handler processes libreload command, which unloads all hook
  128. /// libraries and reloads them.
  129. ///
  130. /// @param command (parameter ignored)
  131. /// @param args (parameter ignored)
  132. ///
  133. /// @return status of the command
  134. isc::data::ConstElementPtr
  135. commandLibReloadHandler(const std::string& command,
  136. isc::data::ConstElementPtr args);
  137. /// @brief handler for processing 'config-reload' command
  138. ///
  139. /// This handler processes config-reload command, which processes
  140. /// configuration specified in args parameter.
  141. ///
  142. /// @param command (parameter ignored)
  143. /// @param args configuration to be processed
  144. ///
  145. /// @return status of the command
  146. isc::data::ConstElementPtr
  147. commandConfigReloadHandler(const std::string& command,
  148. isc::data::ConstElementPtr args);
  149. /// @brief handler for processing 'get-config' command
  150. ///
  151. /// This handler processes get-config command, which retrieves
  152. /// the current configuration and returns it in response.
  153. ///
  154. /// @param command (ignored)
  155. /// @param args (ignored)
  156. /// @return current configuration wrapped in a response
  157. isc::data::ConstElementPtr
  158. commandConfigGetHandler(const std::string& command,
  159. isc::data::ConstElementPtr args);
  160. /// @brief handler for processing 'write-config' command
  161. ///
  162. /// This handle processes write-config comamnd, which writes the
  163. /// current configuration to disk. This command takes one optional
  164. /// parameter called filename. If specified, the current configuration
  165. /// will be written to that file. If not specified, the file used during
  166. /// Kea start-up will be used. To avoid any exploits, the path is
  167. /// always relative and .. is not allowed in the filename. This is
  168. /// a security measure against exploiting file writes remotely.
  169. ///
  170. /// @param command (ignored)
  171. /// @param args may contain optional string argument filename
  172. /// @return status of the configuration file write
  173. isc::data::ConstElementPtr
  174. commandConfigWriteHandler(const std::string& command,
  175. isc::data::ConstElementPtr args);
  176. /// @brief handler for processing 'set-config' command
  177. ///
  178. /// This handler processes set-config command, which processes
  179. /// configuration specified in args parameter.
  180. /// @param command (parameter ignored)
  181. /// @param args configuration to be processed. Expected format:
  182. /// map containing Dhcp6 map that contains DHCPv6 server configuration.
  183. /// May also contain Logging map that specifies logging configuration.
  184. ///
  185. /// @return status of the command
  186. isc::data::ConstElementPtr
  187. commandSetConfigHandler(const std::string& command,
  188. isc::data::ConstElementPtr args);
  189. /// @brief handler for processing 'config-test' command
  190. ///
  191. /// This handler processes config-test command, which checks
  192. /// configuration specified in args parameter.
  193. /// @param command (parameter ignored)
  194. /// @param args configuration to be checked. Expected format:
  195. /// map containing Dhcp6 map that contains DHCPv6 server configuration.
  196. /// May also contain Logging map that specifies logging configuration.
  197. ///
  198. /// @return status of the command
  199. isc::data::ConstElementPtr
  200. commandConfigTestHandler(const std::string& command,
  201. isc::data::ConstElementPtr args);
  202. /// @Brief handler for processing 'version-get' command
  203. ///
  204. /// This handler processes version-get command, which returns
  205. /// over the control channel the -v and -V command line arguments.
  206. /// @param command (parameter ignored)
  207. /// @param args (parameter ignored)
  208. ///
  209. /// @return status of the command with the version in text and
  210. /// the extended version in arguments.
  211. isc::data::ConstElementPtr
  212. commandVersionGetHandler(const std::string& command,
  213. isc::data::ConstElementPtr args);
  214. /// @brief handler for processing 'build-report' command
  215. ///
  216. /// This handler processes build-report command, which returns
  217. /// over the control channel the -W command line argument.
  218. /// @param command (parameter ignored)
  219. /// @param args (parameter ignored)
  220. ///
  221. /// @return status of the command with the config report
  222. isc::data::ConstElementPtr
  223. commandBuildReportHandler(const std::string& command,
  224. isc::data::ConstElementPtr args);
  225. /// @brief Handler for processing 'leases-reclaim' command
  226. ///
  227. /// This handler processes leases-reclaim command, which triggers
  228. /// the leases reclamation immediately.
  229. /// No limit for processing time or number of processed leases applies.
  230. ///
  231. /// @param command (parameter ignored)
  232. /// @param args arguments map { "remove": <bool> }
  233. /// if true a lease is removed when it is reclaimed,
  234. /// if false its state is changed to "expired-reclaimed".
  235. ///
  236. /// @return status of the command (should be success unless args
  237. /// was not a Bool Element).
  238. isc::data::ConstElementPtr
  239. commandLeasesReclaimHandler(const std::string& command,
  240. isc::data::ConstElementPtr args);
  241. /// @brief Reclaims expired IPv6 leases and reschedules timer.
  242. ///
  243. /// This is a wrapper method for @c AllocEngine::reclaimExpiredLeases6.
  244. /// It reschedules the timer for leases reclamation upon completion of
  245. /// this method.
  246. ///
  247. /// @param max_leases Maximum number of leases to be reclaimed.
  248. /// @param timeout Maximum amount of time that the reclamation routine
  249. /// may be processing expired leases, expressed in milliseconds.
  250. /// @param remove_lease A boolean value indicating if the lease should
  251. /// be removed when it is reclaimed (if true) or it should be left in the
  252. /// database in the "expired-reclaimed" state (if false).
  253. /// @param max_unwarned_cycles A number of consecutive processing cycles
  254. /// of expired leases, after which the system issues a warning if there
  255. /// are still expired leases in the database. If this value is 0, the
  256. /// warning is never issued.
  257. void reclaimExpiredLeases(const size_t max_leases, const uint16_t timeout,
  258. const bool remove_lease,
  259. const uint16_t max_unwarned_cycles);
  260. /// @brief Deletes reclaimed leases and reschedules the timer.
  261. ///
  262. /// This is a wrapper method for @c AllocEngine::deleteExpiredReclaimed6.
  263. /// It reschedules the timer for leases reclamation upon completion of
  264. /// this method.
  265. ///
  266. /// @param secs Minimum number of seconds after which a lease can be
  267. /// deleted.
  268. void deleteExpiredReclaimedLeases(const uint32_t secs);
  269. /// @brief Static pointer to the sole instance of the DHCP server.
  270. ///
  271. /// This is required for config and command handlers to gain access to
  272. /// the server. Some of them need to be static methods.
  273. static ControlledDhcpv6Srv* server_;
  274. /// @brief IOService object, used for all ASIO operations.
  275. isc::asiolink::IOService io_service_;
  276. /// @brief Instance of the @c TimerMgr.
  277. ///
  278. /// Shared pointer to the instance of timer @c TimerMgr is held here to
  279. /// make sure that the @c TimerMgr outlives instance of this class.
  280. TimerMgrPtr timer_mgr_;
  281. };
  282. }; // namespace isc::dhcp
  283. }; // namespace isc
  284. #endif