dhcp4_srv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/alloc_engine.h>
  21. #include <boost/noncopyable.hpp>
  22. #include <iostream>
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief file name of a server-id file
  26. ///
  27. /// Server must store its server identifier in persistent storage that must not
  28. /// change between restarts. This is name of the file that is created in dataDir
  29. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  30. /// regular IPv4 address, e.g. 192.0.2.1. Server will create it during
  31. /// first run and then use it afterwards.
  32. static const char* SERVER_ID_FILE = "b10-dhcp4-serverid";
  33. /// @brief DHCPv4 server service.
  34. ///
  35. /// This singleton class represents DHCPv4 server. It contains all
  36. /// top-level methods and routines necessary for server operation.
  37. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  38. /// that is going to be used as server-identifier, receives incoming
  39. /// packets, processes them, manages leases assignment and generates
  40. /// appropriate responses.
  41. ///
  42. /// This class does not support any controlling mechanisms directly.
  43. /// See the derived \ref ControlledDhcpv4Srv class for support for
  44. /// command and configuration updates over msgq.
  45. ///
  46. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  47. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  48. class Dhcpv4Srv : public boost::noncopyable {
  49. public:
  50. /// @brief defines if certain option may, must or must not appear
  51. typedef enum {
  52. FORBIDDEN,
  53. MANDATORY,
  54. OPTIONAL
  55. } RequirementLevel;
  56. /// @brief Default constructor.
  57. ///
  58. /// Instantiates necessary services, required to run DHCPv4 server.
  59. /// In particular, creates IfaceMgr that will be responsible for
  60. /// network interaction. Will instantiate lease manager, and load
  61. /// old or create new DUID. It is possible to specify alternate
  62. /// port on which DHCPv4 server will listen on. That is mostly useful
  63. /// for testing purposes.
  64. ///
  65. /// @param port specifies port number to listen on
  66. /// @param dbconfig Lease manager configuration string. The default
  67. /// of the "memfile" manager is used for testing.
  68. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
  69. const char* dbconfig = "type=memfile");
  70. /// @brief Destructor. Used during DHCPv4 service shutdown.
  71. ~Dhcpv4Srv();
  72. /// @brief Main server processing loop.
  73. ///
  74. /// Main server processing loop. Receives incoming packets, verifies
  75. /// their correctness, generates appropriate answer (if needed) and
  76. /// transmits respones.
  77. ///
  78. /// @return true, if being shut down gracefully, fail if experienced
  79. /// critical error.
  80. bool run();
  81. /// @brief Instructs the server to shut down.
  82. void shutdown();
  83. /// @brief Return textual type of packet received by server
  84. ///
  85. /// Returns the name of valid packet received by the server (e.g. DISCOVER).
  86. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  87. /// expected to be received by the server (such as an OFFER), the string
  88. /// "UNKNOWN" is returned. This method is used in debug messages.
  89. ///
  90. /// As the operation of the method does not depend on any server state, it
  91. /// is declared static.
  92. ///
  93. /// @todo: This should be named static Pkt4::getName()
  94. ///
  95. /// @param type DHCPv4 packet type
  96. ///
  97. /// @return Pointer to "const" string containing the packet name.
  98. /// Note that this string is statically allocated and MUST NOT
  99. /// be freed by the caller.
  100. static const char* serverReceivedPacketName(uint8_t type);
  101. protected:
  102. /// @brief verifies if specified packet meets RFC requirements
  103. ///
  104. /// Checks if mandatory option is really there, that forbidden option
  105. /// is not there, and that client-id or server-id appears only once.
  106. ///
  107. /// @param pkt packet to be checked
  108. /// @param serverid expectation regarding server-id option
  109. /// @throw RFCViolation if any issues are detected
  110. void sanityCheck(const Pkt4Ptr& pkt, RequirementLevel serverid);
  111. /// @brief Processes incoming DISCOVER and returns response.
  112. ///
  113. /// Processes received DISCOVER message and verifies that its sender
  114. /// should be served. In particular, a lease is selected and sent
  115. /// as an offer to a client if it should be served.
  116. ///
  117. /// @param discover DISCOVER message received from client
  118. ///
  119. /// @return OFFER message or NULL
  120. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  121. /// @brief Processes incoming REQUEST and returns REPLY response.
  122. ///
  123. /// Processes incoming REQUEST message and verifies that its sender
  124. /// should be served. In particular, verifies that requested lease
  125. /// is valid, not expired, not reserved, not used by other client and
  126. /// that requesting client is allowed to use it.
  127. ///
  128. /// Returns ACK message, NAK message, or NULL
  129. ///
  130. /// @param request a message received from client
  131. ///
  132. /// @return ACK or NAK message
  133. Pkt4Ptr processRequest(Pkt4Ptr& request);
  134. /// @brief Stub function that will handle incoming RELEASE messages.
  135. ///
  136. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  137. /// this function does not return anything.
  138. ///
  139. /// @param release message received from client
  140. void processRelease(Pkt4Ptr& release);
  141. /// @brief Stub function that will handle incoming DHCPDECLINE messages.
  142. ///
  143. /// @param decline message received from client
  144. void processDecline(Pkt4Ptr& decline);
  145. /// @brief Stub function that will handle incoming INFORM messages.
  146. ///
  147. /// @param inform message received from client
  148. Pkt4Ptr processInform(Pkt4Ptr& inform);
  149. /// @brief Copies default parameters from client's to server's message
  150. ///
  151. /// Some fields are copied from client's message into server's response,
  152. /// e.g. client HW address, number of hops, transaction-id etc.
  153. ///
  154. /// @param question any message sent by client
  155. /// @param answer any message server is going to send as response
  156. void copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer);
  157. /// @brief Appends options requested by client.
  158. ///
  159. /// This method assigns options that were requested by client
  160. /// (sent in PRL) or are enforced by server.
  161. ///
  162. /// @param msg outgoing message (options will be added here)
  163. void appendRequestedOptions(Pkt4Ptr& msg);
  164. /// @brief Assigns a lease and appends corresponding options
  165. ///
  166. /// This method chooses the most appropriate lease for reqesting
  167. /// client and assigning it. Options corresponding to the lease
  168. /// are added to specific message.
  169. ///
  170. /// @param question DISCOVER or REQUEST message from client
  171. /// @param answer OFFER or ACK/NAK message (lease options will be added here)
  172. void assignLease(const Pkt4Ptr& question, Pkt4Ptr& answer);
  173. /// @brief Attempts to renew received addresses
  174. ///
  175. /// Attempts to renew existing lease. This typically includes finding a lease that
  176. /// corresponds to the received address. If no such lease is found, a status code
  177. /// response is generated.
  178. ///
  179. /// @param renew client's message asking for renew
  180. /// @param reply server's response (ACK or NAK)
  181. void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
  182. /// @brief Appends default options to a message
  183. ///
  184. /// @param msg message object (options will be added to it)
  185. /// @param msg_type specifies message type
  186. void appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type);
  187. /// @brief Returns server-intentifier option
  188. ///
  189. /// @return server-id option
  190. OptionPtr getServerID() { return serverid_; }
  191. /// @brief Sets server-identifier.
  192. ///
  193. /// This method attempts to set server-identifier DUID. It tries to
  194. /// load previously stored IP from configuration. If there is no previously
  195. /// stored server identifier, it will pick up one address from configured
  196. /// and supported network interfaces.
  197. ///
  198. /// @throws isc::Unexpected Failed to obtain server identifier (i.e. no
  199. // previously stored configuration and no network interfaces available)
  200. void generateServerID();
  201. /// @brief attempts to load server-id from a file
  202. ///
  203. /// Tries to load duid from a text file. If the load is successful,
  204. /// it creates server-id option and stores it in serverid_ (to be used
  205. /// later by getServerID()).
  206. ///
  207. /// @param file_name name of the server-id file to load
  208. /// @return true if load was successful, false otherwise
  209. bool loadServerID(const std::string& file_name);
  210. /// @brief attempts to write server-id to a file
  211. /// Tries to write server-id content (stored in serverid_) to a text file.
  212. ///
  213. /// @param file_name name of the server-id file to write
  214. /// @return true if write was successful, false otherwise
  215. bool writeServerID(const std::string& file_name);
  216. /// @brief converts server-id to text
  217. /// Converts content of server-id option to a text representation, e.g.
  218. /// "192.0.2.1"
  219. ///
  220. /// @param opt option that contains server-id
  221. /// @return string representation
  222. static std::string srvidToString(const OptionPtr& opt);
  223. /// @brief Selects a subnet for a given client's packet.
  224. ///
  225. /// @param question client's message
  226. /// @return selected subnet (or NULL if no suitable subnet was found)
  227. isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& question);
  228. /// server DUID (to be sent in server-identifier option)
  229. OptionPtr serverid_;
  230. /// indicates if shutdown is in progress. Setting it to true will
  231. /// initiate server shutdown procedure.
  232. volatile bool shutdown_;
  233. private:
  234. /// @brief Constructs netmask option based on subnet4
  235. /// @param subnet subnet for which the netmask will be calculated
  236. ///
  237. /// @return Option that contains netmask information
  238. static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
  239. /// @brief Allocation Engine.
  240. /// Pointer to the allocation engine that we are currently using
  241. /// It must be a pointer, because we will support changing engines
  242. /// during normal operation (e.g. to use different allocators)
  243. boost::shared_ptr<AllocEngine> alloc_engine_;
  244. };
  245. }; // namespace isc::dhcp
  246. }; // namespace isc
  247. #endif // DHCP4_SRV_H