dhcp4_srv.h 6.4 KB

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