dhcp4_srv.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Returns server-intentifier option
  97. ///
  98. /// @return server-id option
  99. boost::shared_ptr<isc::dhcp::Option>
  100. getServerID() { return serverid_; }
  101. /// @brief Sets server-identifier.
  102. ///
  103. /// This method attempts to set server-identifier DUID. It tries to
  104. /// load previously stored IP from configuration. If there is no previously
  105. /// stored server identifier, it will pick up one address from configured
  106. /// and supported network interfaces.
  107. ///
  108. /// @throws isc::Unexpected Failed to obtain server identifier (i.e. no
  109. // previously stored configuration and no network interfaces available)
  110. void setServerID();
  111. /// server DUID (to be sent in server-identifier option)
  112. boost::shared_ptr<isc::dhcp::Option> serverid_;
  113. /// indicates if shutdown is in progress. Setting it to true will
  114. /// initiate server shutdown procedure.
  115. volatile bool shutdown_;
  116. };
  117. }; // namespace isc::dhcp
  118. }; // namespace isc
  119. #endif // DHCP4_SRV_H