dhcp4_srv.h 6.7 KB

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