dhcp4_srv.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (C) 2011-2012 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 DHCPV4_SRV_H
  15. #define DHCPV4_SRV_H
  16. #include <dhcp/dhcp4.h>
  17. #include <dhcp/pkt4.h>
  18. #include <dhcp/option.h>
  19. #include <boost/noncopyable.hpp>
  20. #include <iostream>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief DHCPv4 server service.
  24. ///
  25. /// This singleton class represents DHCPv4 server. It contains all
  26. /// top-level methods and routines necessary for server operation.
  27. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  28. /// that is going to be used as server-identifier, receives incoming
  29. /// packets, processes them, manages leases assignment and generates
  30. /// appropriate responses.
  31. ///
  32. /// This class does not support any controlling mechanisms directly.
  33. /// See the derived \ref ControlledDhcpv4Srv class for support for
  34. /// command and configuration updates over msgq.
  35. ///
  36. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  37. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  38. class Dhcpv4Srv : public boost::noncopyable {
  39. public:
  40. /// @brief Default constructor.
  41. ///
  42. /// Instantiates necessary services, required to run DHCPv4 server.
  43. /// In particular, creates IfaceMgr that will be responsible for
  44. /// network interaction. Will instantiate lease manager, and load
  45. /// old or create new DUID. It is possible to specify alternate
  46. /// port on which DHCPv4 server will listen on. That is mostly useful
  47. /// for testing purposes.
  48. ///
  49. /// @param port specifies port number to listen on
  50. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
  51. /// @brief Destructor. Used during DHCPv4 service shutdown.
  52. ~Dhcpv4Srv();
  53. /// @brief Main server processing loop.
  54. ///
  55. /// Main server processing loop. Receives incoming packets, verifies
  56. /// their correctness, generates appropriate answer (if needed) and
  57. /// transmits respones.
  58. ///
  59. /// @return true, if being shut down gracefully, fail if experienced
  60. /// critical error.
  61. bool run();
  62. /// @brief Instructs the server to shut down.
  63. void shutdown();
  64. /// @brief Return textual type of packet received by server
  65. ///
  66. /// Returns the name of valid packet received by the server (e.g. DISCOVER).
  67. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  68. /// expected to be received by the server (such as an OFFER), the string
  69. /// "UNKNOWN" is returned. This method is used in debug messages.
  70. ///
  71. /// As the operation of the method does not depend on any server state, it
  72. /// is declared static.
  73. ///
  74. /// @param type DHCPv4 packet type
  75. ///
  76. /// @return Pointer to "const" string containing the packet name.
  77. /// Note that this string is statically allocated and MUST NOT
  78. /// be freed by the caller.
  79. static const char* serverReceivedPacketName(uint8_t type);
  80. protected:
  81. /// @brief Processes incoming DISCOVER and returns response.
  82. ///
  83. /// Processes received DISCOVER message and verifies that its sender
  84. /// should be served. In particular, a lease is selected and sent
  85. /// as an offer to a client if it should be served.
  86. ///
  87. /// @param discover DISCOVER message received from client
  88. ///
  89. /// @return OFFER message or NULL
  90. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  91. /// @brief Processes incoming REQUEST and returns REPLY response.
  92. ///
  93. /// Processes incoming REQUEST message and verifies that its sender
  94. /// should be served. In particular, verifies that requested lease
  95. /// is valid, not expired, not reserved, not used by other client and
  96. /// that requesting client is allowed to use it.
  97. ///
  98. /// Returns ACK message, NAK message, or NULL
  99. ///
  100. /// @param request a message received from client
  101. ///
  102. /// @return ACK or NAK message
  103. Pkt4Ptr processRequest(Pkt4Ptr& request);
  104. /// @brief Stub function that will handle incoming RELEASE messages.
  105. ///
  106. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  107. /// this function does not return anything.
  108. ///
  109. /// @param release message received from client
  110. void processRelease(Pkt4Ptr& release);
  111. /// @brief Stub function that will handle incoming DHCPDECLINE messages.
  112. ///
  113. /// @param decline message received from client
  114. void processDecline(Pkt4Ptr& decline);
  115. /// @brief Stub function that will handle incoming INFORM messages.
  116. ///
  117. /// @param inform message received from client
  118. Pkt4Ptr processInform(Pkt4Ptr& inform);
  119. /// @brief Copies default parameters from client's to server's message
  120. ///
  121. /// Some fields are copied from client's message into server's response,
  122. /// e.g. client HW address, number of hops, transaction-id etc.
  123. ///
  124. /// @param question any message sent by client
  125. /// @param answer any message server is going to send as response
  126. void copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer);
  127. /// @brief Appends options requested by client.
  128. ///
  129. /// This method assigns options that were requested by client
  130. /// (sent in PRL) or are enforced by server.
  131. ///
  132. /// @param msg outgoing message (options will be added here)
  133. void appendRequestedOptions(Pkt4Ptr& msg);
  134. /// @brief Assigns a lease and appends corresponding options
  135. ///
  136. /// This method chooses the most appropriate lease for reqesting
  137. /// client and assigning it. Options corresponding to the lease
  138. /// are added to specific message.
  139. ///
  140. /// Note: Lease manager is not implemented yet, so this method
  141. /// used fixed, hardcoded lease.
  142. ///
  143. /// @param msg OFFER or ACK message (lease options will be added here)
  144. void tryAssignLease(Pkt4Ptr& msg);
  145. /// @brief Appends default options to a message
  146. ///
  147. /// @param msg message object (options will be added to it)
  148. /// @param msg_type specifies message type
  149. void appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type);
  150. /// @brief Returns server-intentifier option
  151. ///
  152. /// @return server-id option
  153. OptionPtr getServerID() { return serverid_; }
  154. /// @brief Sets server-identifier.
  155. ///
  156. /// This method attempts to set server-identifier DUID. It tries to
  157. /// load previously stored IP from configuration. If there is no previously
  158. /// stored server identifier, it will pick up one address from configured
  159. /// and supported network interfaces.
  160. ///
  161. /// @throws isc::Unexpected Failed to obtain server identifier (i.e. no
  162. // previously stored configuration and no network interfaces available)
  163. void setServerID();
  164. /// server DUID (to be sent in server-identifier option)
  165. OptionPtr serverid_;
  166. /// indicates if shutdown is in progress. Setting it to true will
  167. /// initiate server shutdown procedure.
  168. volatile bool shutdown_;
  169. };
  170. }; // namespace isc::dhcp
  171. }; // namespace isc
  172. #endif // DHCP4_SRV_H