ctrl_dhcp4_srv.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2012-2013 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 establishing connection
  26. /// with msqg (receving commands and configuration). This is an extended
  27. /// version of Dhcpv4Srv class that is purely a DHCPv4 server, without
  28. /// external control. ControlledDhcpv4Srv should be used in typical BIND10
  29. /// (i.e. featuring msgq) environment, while Dhcpv4Srv should be used in
  30. /// embedded environments.
  31. ///
  32. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  33. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  34. class ControlledDhcpv4Srv : public isc::dhcp::Dhcpv4Srv {
  35. public:
  36. /// @brief Constructor
  37. ///
  38. /// @param port UDP port to be opened for DHCP traffic
  39. ControlledDhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
  40. /// @brief Destructor.
  41. ~ControlledDhcpv4Srv();
  42. /// @brief Establishes msgq session.
  43. ///
  44. /// Creates session that will be used to receive commands and updated
  45. /// configuration from cfgmgr (or indirectly from user via bindctl).
  46. void establishSession();
  47. /// @brief Terminates existing msgq session.
  48. ///
  49. /// This method terminates existing session with msgq. After calling
  50. /// it, no further messages over msgq (commands or configuration updates)
  51. /// may be received.
  52. ///
  53. /// It is ok to call this method when session is disconnected already.
  54. void disconnectSession();
  55. /// @brief Initiates shutdown procedure for the whole DHCPv4 server.
  56. void shutdown();
  57. /// @brief Session callback, processes received commands.
  58. ///
  59. /// @param command Text represenation of the command (e.g. "shutdown")
  60. /// @param args Optional parameters
  61. /// @param command text represenation of the command (e.g. "shutdown")
  62. /// @param args optional parameters
  63. ///
  64. /// @return status of the command
  65. static isc::data::ConstElementPtr
  66. execDhcpv4ServerCommand(const std::string& command,
  67. isc::data::ConstElementPtr args);
  68. protected:
  69. /// @brief Static pointer to the sole instance of the DHCP server.
  70. ///
  71. /// This is required for config and command handlers to gain access to
  72. /// the server
  73. static ControlledDhcpv4Srv* server_;
  74. /// @brief A callback for handling incoming configuration updates.
  75. ///
  76. /// As pointer to this method is used a callback in ASIO used in
  77. /// ModuleCCSession, it has to be static.
  78. ///
  79. /// @param new_config textual representation of the new configuration
  80. ///
  81. /// @return status of the config update
  82. static isc::data::ConstElementPtr
  83. dhcp4ConfigHandler(isc::data::ConstElementPtr new_config);
  84. /// @brief A dummy configuration handler that always returns success.
  85. ///
  86. /// This configuration handler does not perform configuration
  87. /// parsing and always returns success. A dummy handler should
  88. /// be installed using \ref isc::config::ModuleCCSession ctor
  89. /// to get the initial configuration. This initial configuration
  90. /// comprises values for only those elements that were modified
  91. /// the previous session. The \ref dhcp4ConfigHandler can't be
  92. /// used to parse the initial configuration because it needs the
  93. /// full configuration to satisfy dependencies between the
  94. /// various configuration values. Installing the dummy handler
  95. /// that guarantees to return success causes initial configuration
  96. /// to be stored for the session being created and that it can
  97. /// be later accessed with \ref isc::config::ConfigData::getFullConfig.
  98. ///
  99. /// @param new_config new configuration.
  100. ///
  101. /// @return success configuration status.
  102. static isc::data::ConstElementPtr
  103. dhcp4StubConfigHandler(isc::data::ConstElementPtr new_config);
  104. /// @brief A callback for handling incoming commands.
  105. ///
  106. /// @param command textual representation of the command
  107. /// @param args parameters of the command
  108. ///
  109. /// @return status of the processed command
  110. static isc::data::ConstElementPtr
  111. dhcp4CommandHandler(const std::string& command, isc::data::ConstElementPtr args);
  112. /// @brief Callback that will be called from iface_mgr when command/config arrives.
  113. ///
  114. /// This static callback method is called from IfaceMgr::receive4() method,
  115. /// when there is a new command or configuration sent over msgq.
  116. static void sessionReader(void);
  117. /// @brief IOService object, used for all ASIO operations.
  118. isc::asiolink::IOService io_service_;
  119. /// @brief Helper session object that represents raw connection to msgq.
  120. isc::cc::Session* cc_session_;
  121. /// @brief Session that receives configuration and commands
  122. isc::config::ModuleCCSession* config_session_;
  123. };
  124. }; // namespace isc::dhcp
  125. }; // namespace isc
  126. #endif