dhcp4_srv.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.
  40. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
  41. /// @brief Destructor. Used during DHCPv6 service shutdown.
  42. ~Dhcpv4Srv();
  43. /// @brief Main server processing loop.
  44. ///
  45. /// Main server processing loop. Receives incoming packets, verifies
  46. /// their correctness, generates appropriate answer (if needed) and
  47. /// transmits respones.
  48. ///
  49. /// @return true, if being shut down gracefully, fail if experienced
  50. /// critical error.
  51. bool run();
  52. protected:
  53. /// @brief Processes incoming DISCOVER and returns response.
  54. ///
  55. /// Processes received DISCOVER message and verifies that its sender
  56. /// should be served. In particular, a lease is selected and sent
  57. /// as an offer to a client if it should be served.
  58. ///
  59. /// @param solicit DISCOVER message received from client
  60. ///
  61. /// @return OFFER message or NULL
  62. boost::shared_ptr<Pkt4>
  63. processDiscover(boost::shared_ptr<Pkt4> discover);
  64. /// @brief Processes incoming REQUEST and returns REPLY response.
  65. ///
  66. /// Processes incoming REQUEST message and verifies that its sender
  67. /// should be served. In particular, verifies that requested lease
  68. /// is valid, not expired, not reserved, not used by other client and
  69. /// that requesting client is allowed to use it.
  70. ///
  71. /// Returns ACK message, NACK message, or NULL
  72. ///
  73. /// @param request a message received from client
  74. ///
  75. /// @return ACK or NACK message
  76. boost::shared_ptr<Pkt4> processRequest(boost::shared_ptr<Pkt4> request);
  77. /// @brief Stub function that will handle incoming RELEASE messages.
  78. ///
  79. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  80. /// this function does not return anything.
  81. ///
  82. /// @param release message received from client
  83. void processRelease(boost::shared_ptr<Pkt4> release);
  84. /// @brief Stub function that will handle incoming DHCPDECLINE messages.
  85. ///
  86. /// @param decline message received from client
  87. void processDecline(boost::shared_ptr<Pkt4> decline);
  88. /// @brief Stub function that will handle incoming INFORM messages.
  89. ///
  90. /// @param infRequest message received from client
  91. boost::shared_ptr<Pkt4> processInform(boost::shared_ptr<Pkt4> inform);
  92. /// @brief Returns server-intentifier option
  93. ///
  94. /// @return server-id option
  95. boost::shared_ptr<isc::dhcp::Option>
  96. getServerID() { return serverid_; }
  97. /// @brief Sets server-identifier.
  98. ///
  99. /// This method attempts to set server-identifier DUID. It tries to
  100. /// load previously stored IP from configuration. If there is no previously
  101. /// stored server identifier, it will pick up one address from configured
  102. /// and supported network interfaces.
  103. ///
  104. /// @throws isc::Unexpected Failed to obtain server identifier (i.e. no
  105. // previously stored configuration and no network interfaces available)
  106. void setServerID();
  107. /// server DUID (to be sent in server-identifier option)
  108. boost::shared_ptr<isc::dhcp::Option> serverid_;
  109. /// indicates if shutdown is in progress. Setting it to true will
  110. /// initiate server shutdown procedure.
  111. volatile bool shutdown;
  112. };
  113. }; // namespace isc::dhcp
  114. }; // namespace isc
  115. #endif // DHCP4_SRV_H