ctrl_dhcp4_srv.h 6.5 KB

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