dhcp4_srv.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // Copyright (C) 2011-2014 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 <dhcp/option_string.h>
  20. #include <dhcp/option4_client_fqdn.h>
  21. #include <dhcp/option_custom.h>
  22. #include <dhcp_ddns/ncr_msg.h>
  23. #include <dhcpsrv/d2_client_mgr.h>
  24. #include <dhcpsrv/subnet.h>
  25. #include <dhcpsrv/alloc_engine.h>
  26. #include <hooks/callout_handle.h>
  27. #include <dhcpsrv/daemon.h>
  28. #include <boost/noncopyable.hpp>
  29. #include <iostream>
  30. #include <queue>
  31. namespace isc {
  32. namespace dhcp {
  33. /// @brief Exception thrown when DHCID computation failed.
  34. class DhcidComputeError : public isc::Exception {
  35. public:
  36. DhcidComputeError(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what) { };
  38. };
  39. /// @brief DHCPv4 server service.
  40. ///
  41. /// This singleton class represents DHCPv4 server. It contains all
  42. /// top-level methods and routines necessary for server operation.
  43. /// In particular, it instantiates IfaceMgr, loads or generates DUID
  44. /// that is going to be used as server-identifier, receives incoming
  45. /// packets, processes them, manages leases assignment and generates
  46. /// appropriate responses.
  47. ///
  48. /// This class does not support any controlling mechanisms directly.
  49. /// See the derived \ref ControlledDhcpv4Srv class for support for
  50. /// command and configuration updates over msgq.
  51. ///
  52. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  53. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  54. class Dhcpv4Srv : public Daemon {
  55. public:
  56. /// @brief defines if certain option may, must or must not appear
  57. typedef enum {
  58. FORBIDDEN,
  59. MANDATORY,
  60. OPTIONAL
  61. } RequirementLevel;
  62. /// @brief Default constructor.
  63. ///
  64. /// Instantiates necessary services, required to run DHCPv4 server.
  65. /// In particular, creates IfaceMgr that will be responsible for
  66. /// network interaction. Will instantiate lease manager, and load
  67. /// old or create new DUID. It is possible to specify alternate
  68. /// port on which DHCPv4 server will listen on. That is mostly useful
  69. /// for testing purposes. The Last two arguments of the constructor
  70. /// should be left at default values for normal server operation.
  71. /// They should be set to 'false' when creating an instance of this
  72. /// class for unit testing because features they enable require
  73. /// root privileges.
  74. ///
  75. /// @param port specifies port number to listen on
  76. /// @param use_bcast configure sockets to support broadcast messages.
  77. /// @param direct_response_desired specifies if it is desired to
  78. /// use direct V4 traffic.
  79. Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT,
  80. const bool use_bcast = true,
  81. const bool direct_response_desired = true);
  82. /// @brief Destructor. Used during DHCPv4 service shutdown.
  83. virtual ~Dhcpv4Srv();
  84. /// @brief Main server processing loop.
  85. ///
  86. /// Main server processing loop. Receives incoming packets, verifies
  87. /// their correctness, generates appropriate answer (if needed) and
  88. /// transmits respones.
  89. ///
  90. /// @return true, if being shut down gracefully, fail if experienced
  91. /// critical error.
  92. bool run();
  93. /// @brief Instructs the server to shut down.
  94. void shutdown();
  95. /// @brief Return textual type of packet received by server
  96. ///
  97. /// Returns the name of valid packet received by the server (e.g. DISCOVER).
  98. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  99. /// expected to be received by the server (such as an OFFER), the string
  100. /// "UNKNOWN" is returned. This method is used in debug messages.
  101. ///
  102. /// As the operation of the method does not depend on any server state, it
  103. /// is declared static.
  104. ///
  105. /// @todo: This should be named static Pkt4::getName()
  106. ///
  107. /// @param type DHCPv4 packet type
  108. ///
  109. /// @return Pointer to "const" string containing the packet name.
  110. /// Note that this string is statically allocated and MUST NOT
  111. /// be freed by the caller.
  112. static const char* serverReceivedPacketName(uint8_t type);
  113. ///
  114. /// @name Public accessors returning values required to (re)open sockets.
  115. ///
  116. /// These accessors must be public because sockets are reopened from the
  117. /// static configuration callback handler. This callback handler invokes
  118. /// @c ControlledDhcpv4Srv::openActiveSockets which requires parameters
  119. /// which has to be retrieved from the @c ControlledDhcpv4Srv object.
  120. /// They are retrieved using these public functions
  121. //@{
  122. ///
  123. /// @brief Get UDP port on which server should listen.
  124. ///
  125. /// Typically, server listens on UDP port number 67. Other ports are used
  126. /// for testing purposes only.
  127. ///
  128. /// @return UDP port on which server should listen.
  129. uint16_t getPort() const {
  130. return (port_);
  131. }
  132. /// @brief Return bool value indicating that broadcast flags should be set
  133. /// on sockets.
  134. ///
  135. /// @return A bool value indicating that broadcast should be used (if true).
  136. bool useBroadcast() const {
  137. return (use_bcast_);
  138. }
  139. //@}
  140. /// @brief Open sockets which are marked as active in @c CfgMgr.
  141. ///
  142. /// This function reopens sockets according to the current settings in the
  143. /// Configuration Manager. It holds the list of the interfaces which server
  144. /// should listen on. This function will open sockets on these interfaces
  145. /// only. This function is not exception safe.
  146. ///
  147. /// @param port UDP port on which server should listen.
  148. /// @param use_bcast should broadcast flags be set on the sockets.
  149. static void openActiveSockets(const uint16_t port, const bool use_bcast);
  150. /// @brief Starts DHCP_DDNS client IO if DDNS updates are enabled.
  151. ///
  152. /// If updates are enabled, it Instructs the D2ClientMgr singleton to
  153. /// enter send mode. If D2ClientMgr encounters errors it may throw
  154. /// D2ClientErrors. This method does not catch exceptions.
  155. void startD2();
  156. /// @brief Implements the error handler for DHCP_DDNS IO errors
  157. ///
  158. /// Invoked when a NameChangeRequest send to b10-dhcp-ddns completes with
  159. /// a failed status. These are communications errors, not data related
  160. /// failures.
  161. ///
  162. /// This method logs the failure and then suspends all further updates.
  163. /// Updating can only be restored by reconfiguration or restarting the
  164. /// server. There is currently no retry logic so the first IO error that
  165. /// occurs will suspend updates.
  166. /// @todo We may wish to make this more robust or sophisticated.
  167. ///
  168. /// @param result Result code of the send operation.
  169. /// @param ncr NameChangeRequest which failed to send.
  170. virtual void d2ClientErrorHandler(const dhcp_ddns::
  171. NameChangeSender::Result result,
  172. dhcp_ddns::NameChangeRequestPtr& ncr);
  173. protected:
  174. /// @name Functions filtering and sanity-checking received messages.
  175. ///
  176. /// @todo These functions are supposed to be moved to a new class which
  177. /// will manage different rules for accepting and rejecting messages.
  178. /// Perhaps ticket #3116 is a good opportunity to do it.
  179. ///
  180. //@{
  181. /// @brief Checks whether received message should be processed or discarded.
  182. ///
  183. /// This function checks whether received message should be processed or
  184. /// discarded. It should be called on the beginning of message processing
  185. /// (just after the message has been decoded). This message calls a number
  186. /// of other functions which check whether message should be processed,
  187. /// using different criteria.
  188. ///
  189. /// This function should be extended when new criteria for accepting
  190. /// received message have to be implemented. This function is meant to
  191. /// aggregate all early filtering checks on the received message. By having
  192. /// a single function like this, we are avoiding bloat of the server's main
  193. /// loop.
  194. ///
  195. /// @warning This function should remain exception safe.
  196. ///
  197. /// @param query Received message.
  198. ///
  199. /// @return true if the message should be further processed, or false if
  200. /// the message should be discarded.
  201. bool accept(const Pkt4Ptr& query) const;
  202. /// @brief Check if a message sent by directly connected client should be
  203. /// accepted or discared.
  204. ///
  205. /// This function checks if the received message is from directly connected
  206. /// client. If it is, it checks that it should be processed or discarded.
  207. ///
  208. /// Note that this function doesn't validate all addresses being carried in
  209. /// the message. The primary purpose of this function is to filter out
  210. /// direct messages in the local network for which there is no suitable
  211. /// subnet configured. For example, this function accepts unicast messages
  212. /// because unicasts may be used by clients located in remote networks to
  213. /// to renew existing leases. If their notion of address is wrong, the
  214. /// server will have to sent a NAK, instead of dropping the message.
  215. /// Detailed validation of such messages is performed at later stage of
  216. /// processing.
  217. ///
  218. /// This function accepts the following messages:
  219. /// - all valid relayed messages,
  220. /// - all unicast messages,
  221. /// - all broadcast messages received on the interface for which the
  222. /// suitable subnet exists (is configured).
  223. ///
  224. /// @param query Message sent by a client.
  225. ///
  226. /// @return true if message is accepted for further processing, false
  227. /// otherwise.
  228. bool acceptDirectRequest(const Pkt4Ptr& query) const;
  229. /// @brief Check if received message type is valid for the server to
  230. /// process.
  231. ///
  232. /// This function checks that the received message type belongs to the range
  233. /// of types regonized by the server and that the message of this type
  234. /// should be processed by the server.
  235. ///
  236. /// The messages types accepted for processing are:
  237. /// - Discover
  238. /// - Request
  239. /// - Release
  240. /// - Decline
  241. /// - Inform
  242. ///
  243. /// @param query Message sent by a client.
  244. ///
  245. /// @return true if message is accepted for further processing, false
  246. /// otherwise.
  247. bool acceptMessageType(const Pkt4Ptr& query) const;
  248. /// @brief Verifies if the server id belongs to our server.
  249. ///
  250. /// This function checks if the server identifier carried in the specified
  251. /// DHCPv4 message belongs to this server. If the server identifier option
  252. /// is absent or the value carried by this option is equal to one of the
  253. /// server identifiers used by the server, the true is returned. If the
  254. /// server identifier option is present, but it doesn't match any server
  255. /// identifier used by this server, the false value is returned.
  256. ///
  257. /// @param pkt DHCPv4 message which server identifier is to be checked.
  258. ///
  259. /// @return true, if the server identifier is absent or matches one of the
  260. /// server identifiers that the server is using; false otherwise.
  261. bool acceptServerId(const Pkt4Ptr& pkt) const;
  262. //@}
  263. /// @brief verifies if specified packet meets RFC requirements
  264. ///
  265. /// Checks if mandatory option is really there, that forbidden option
  266. /// is not there, and that client-id or server-id appears only once.
  267. ///
  268. /// @param pkt packet to be checked
  269. /// @param serverid expectation regarding server-id option
  270. /// @throw RFCViolation if any issues are detected
  271. static void sanityCheck(const Pkt4Ptr& pkt, RequirementLevel serverid);
  272. /// @brief Processes incoming DISCOVER and returns response.
  273. ///
  274. /// Processes received DISCOVER message and verifies that its sender
  275. /// should be served. In particular, a lease is selected and sent
  276. /// as an offer to a client if it should be served.
  277. ///
  278. /// @param discover DISCOVER message received from client
  279. ///
  280. /// @return OFFER message or NULL
  281. Pkt4Ptr processDiscover(Pkt4Ptr& discover);
  282. /// @brief Processes incoming REQUEST and returns REPLY response.
  283. ///
  284. /// Processes incoming REQUEST message and verifies that its sender
  285. /// should be served. In particular, verifies that requested lease
  286. /// is valid, not expired, not reserved, not used by other client and
  287. /// that requesting client is allowed to use it.
  288. ///
  289. /// Returns ACK message, NAK message, or NULL
  290. ///
  291. /// @param request a message received from client
  292. ///
  293. /// @return ACK or NAK message
  294. Pkt4Ptr processRequest(Pkt4Ptr& request);
  295. /// @brief Stub function that will handle incoming RELEASE messages.
  296. ///
  297. /// In DHCPv4, server does not respond to RELEASE messages, therefore
  298. /// this function does not return anything.
  299. ///
  300. /// @param release message received from client
  301. void processRelease(Pkt4Ptr& release);
  302. /// @brief Stub function that will handle incoming DHCPDECLINE messages.
  303. ///
  304. /// @param decline message received from client
  305. void processDecline(Pkt4Ptr& decline);
  306. /// @brief Stub function that will handle incoming INFORM messages.
  307. ///
  308. /// @param inform message received from client
  309. Pkt4Ptr processInform(Pkt4Ptr& inform);
  310. /// @brief Copies default parameters from client's to server's message
  311. ///
  312. /// Some fields are copied from client's message into server's response,
  313. /// e.g. client HW address, number of hops, transaction-id etc.
  314. ///
  315. /// @param question any message sent by client
  316. /// @param answer any message server is going to send as response
  317. void copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer);
  318. /// @brief Appends options requested by client.
  319. ///
  320. /// This method assigns options that were requested by client
  321. /// (sent in PRL) or are enforced by server.
  322. ///
  323. /// @param question DISCOVER or REQUEST message from a client.
  324. /// @param msg outgoing message (options will be added here)
  325. void appendRequestedOptions(const Pkt4Ptr& question, Pkt4Ptr& msg);
  326. /// @brief Appends requested vendor options as requested by client.
  327. ///
  328. /// This method is similar to \ref appendRequestedOptions(), but uses
  329. /// vendor options. The major difference is that vendor-options use
  330. /// its own option spaces (there may be more than one distinct set of vendor
  331. /// options, each with unique vendor-id). Vendor options are requested
  332. /// using separate options within their respective vendor-option spaces.
  333. ///
  334. /// @param question DISCOVER or REQUEST message from a client.
  335. /// @param answer outgoing message (options will be added here)
  336. void appendRequestedVendorOptions(const Pkt4Ptr& question, Pkt4Ptr& answer);
  337. /// @brief Assigns a lease and appends corresponding options
  338. ///
  339. /// This method chooses the most appropriate lease for reqesting
  340. /// client and assigning it. Options corresponding to the lease
  341. /// are added to specific message.
  342. ///
  343. /// @param question DISCOVER or REQUEST message from client
  344. /// @param answer OFFER or ACK/NAK message (lease options will be added here)
  345. void assignLease(const Pkt4Ptr& question, Pkt4Ptr& answer);
  346. /// @brief Append basic options if they are not present.
  347. ///
  348. /// This function adds the following basic options if they
  349. /// are not yet added to the message:
  350. /// - Subnet Mask,
  351. /// - Router,
  352. /// - Name Server,
  353. /// - Domain Name.
  354. ///
  355. /// @param question DISCOVER or REQUEST message from a client.
  356. /// @param msg the message to add options to.
  357. void appendBasicOptions(const Pkt4Ptr& question, Pkt4Ptr& msg);
  358. /// @brief Processes Client FQDN and Hostname Options sent by a client.
  359. ///
  360. /// Client may send Client FQDN or Hostname option to communicate its name
  361. /// to the server. Server may use this name to perform DNS update for the
  362. /// lease being assigned to a client. If server takes responsibility for
  363. /// updating DNS for a client it may communicate it by sending the Client
  364. /// FQDN or Hostname %Option back to the client. Server select a different
  365. /// name than requested by a client to update DNS. In such case, the server
  366. /// stores this different name in its response.
  367. ///
  368. /// Client should not send both Client FQDN and Hostname options. However,
  369. /// if client sends both options, server should prefer Client FQDN option
  370. /// and ignore the Hostname option. If Client FQDN option is not present,
  371. /// the Hostname option is processed.
  372. ///
  373. /// The Client FQDN %Option is processed by this function as described in
  374. /// RFC4702.
  375. ///
  376. /// In response to a Hostname %Option sent by a client, the server may send
  377. /// Hostname option with the same or different hostname. If different
  378. /// hostname is sent, it is an indication to the client that server has
  379. /// overridden the client's preferred name and will rather use this
  380. /// different name to update DNS. However, since Hostname option doesn't
  381. /// carry an information whether DNS update will be carried by the server
  382. /// or not, the client is responsible for checking whether DNS update
  383. /// has been performed.
  384. ///
  385. /// After successful processing options stored in the first parameter,
  386. /// this function may add Client FQDN or Hostname option to the response
  387. /// message. In some cases, server may cease to add any options to the
  388. /// response, i.e. when server doesn't support DNS updates.
  389. ///
  390. /// This function does not throw. It simply logs the debug message if the
  391. /// processing of the FQDN or Hostname failed.
  392. ///
  393. /// @param query A DISCOVER or REQUEST message from a cient.
  394. /// @param [out] answer A response message to be sent to a client.
  395. void processClientName(const Pkt4Ptr& query, Pkt4Ptr& answer);
  396. /// @brief this is a prefix added to the contend of vendor-class option
  397. ///
  398. /// If incoming packet has a vendor class option, its content is
  399. /// prepended with this prefix and then interpreted as a class.
  400. /// For example, a packet that sends vendor class with value of "FOO"
  401. /// will cause the packet to be assigned to class VENDOR_CLASS_FOO.
  402. static const std::string VENDOR_CLASS_PREFIX;
  403. private:
  404. /// @brief Process Client FQDN %Option sent by a client.
  405. ///
  406. /// This function is called by the @c Dhcpv4Srv::processClientName when
  407. /// the client has sent the FQDN option in its message to the server.
  408. /// It comprises the actual logic to parse the FQDN option and prepare
  409. /// the FQDN option to be sent back to the client in the server's
  410. /// response.
  411. ///
  412. /// @param fqdn An DHCPv4 Client FQDN %Option sent by a client.
  413. /// @param [out] answer A response message to be sent to a client.
  414. void processClientFqdnOption(const Option4ClientFqdnPtr& fqdn,
  415. Pkt4Ptr& answer);
  416. /// @brief Process Hostname %Option sent by a client.
  417. ///
  418. /// This function is called by the @c DHcpv4Srv::processClientName when
  419. /// the client has sent the Hostname option in its message to the server.
  420. /// It comprises the actual logic to parse the Hostname option and
  421. /// prepare the Hostname option to be sent back to the client in the
  422. /// server's response.
  423. ///
  424. /// @param opt_hostname An @c OptionString object encapsulating the Hostname
  425. /// %Option.
  426. /// @param [out] answer A response message to be sent to a client.
  427. void processHostnameOption(const OptionStringPtr& opt_hostname,
  428. Pkt4Ptr& answer);
  429. protected:
  430. /// @brief Creates NameChangeRequests which correspond to the lease
  431. /// which has been acquired.
  432. ///
  433. /// If this function is called whe an existing lease is renewed, it
  434. /// may generate NameChangeRequest to remove existing DNS entries which
  435. /// correspond to the old lease instance. This function may cease to
  436. /// generate NameChangeRequests if the notion of the client's FQDN hasn't
  437. /// changed between an old and new lease.
  438. ///
  439. /// @param lease A pointer to the new lease which has been acquired.
  440. /// @param old_lease A pointer to the instance of the old lease which has
  441. /// been replaced by the new lease passed in the first argument. The NULL
  442. /// value indicates that the new lease has been allocated, rather than
  443. /// lease being renewed.
  444. void createNameChangeRequests(const Lease4Ptr& lease,
  445. const Lease4Ptr& old_lease);
  446. /// @brief Creates the NameChangeRequest and adds to the queue for
  447. /// processing.
  448. ///
  449. /// This creates the @c isc::dhcp_ddns::NameChangeRequest; emits a
  450. /// the debug message which indicates whether the request being added is
  451. /// to remove DNS entry or add a new entry; and then sends the request
  452. /// to the D2ClientMgr for transmission to b10-dhcp-ddns.
  453. ///
  454. /// @param chg_type A type of the NameChangeRequest (ADD or REMOVE).
  455. /// @param lease A lease for which the NameChangeRequest is created and
  456. /// queued.
  457. void queueNameChangeRequest(const isc::dhcp_ddns::NameChangeType chg_type,
  458. const Lease4Ptr& lease);
  459. /// @brief Attempts to renew received addresses
  460. ///
  461. /// Attempts to renew existing lease. This typically includes finding a lease that
  462. /// corresponds to the received address. If no such lease is found, a status code
  463. /// response is generated.
  464. ///
  465. /// @param renew client's message asking for renew
  466. /// @param reply server's response (ACK or NAK)
  467. void renewLease(const Pkt4Ptr& renew, Pkt4Ptr& reply);
  468. /// @brief Appends default options to a message
  469. ///
  470. /// Currently it is only a Message Type option. This function does not add
  471. /// the Server Identifier option as this option must be added using
  472. /// @c Dhcpv4Srv::appendServerID.
  473. ///
  474. ///
  475. /// @param msg message object (options will be added to it)
  476. /// @param msg_type specifies message type
  477. void appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type);
  478. /// @brief Adds server identifier option to the server's response.
  479. ///
  480. /// This method adds a server identifier to the DHCPv4 message. It epxects
  481. /// that the local (source) address is set for this message. If address is
  482. /// not set, it will throw an exception. This method also expects that the
  483. /// server identifier option is not present in the specified message.
  484. /// Otherwise, it will throw an exception on attempt to add a duplicate
  485. /// server identifier option.
  486. ///
  487. /// @note This method doesn't throw exceptions by itself but the underlying
  488. /// classes being used my throw. The reason for this method to not sanity
  489. /// check the specified message is that it is meant to be called internally
  490. /// by the @c Dhcpv4Srv class.
  491. ///
  492. /// @note This method is static because it is not dependent on the class
  493. /// state.
  494. ///
  495. /// @param [out] response DHCPv4 message to which the server identifier
  496. /// option should be added.
  497. static void appendServerID(const Pkt4Ptr& response);
  498. /// @brief Set IP/UDP and interface parameters for the DHCPv4 response.
  499. ///
  500. /// This method sets the following parameters for the DHCPv4 message being
  501. /// sent to a client:
  502. /// - client unicast or a broadcast address,
  503. /// - client or relay port,
  504. /// - server address,
  505. /// - server port,
  506. /// - name and index of the interface which is to be used to send the
  507. /// message.
  508. ///
  509. /// Internally it calls the @c Dhcpv4Srv::adjustRemoteAddr to figure
  510. /// out the destination address (client unicast address or broadcast
  511. /// address).
  512. ///
  513. /// The destination port is always DHCPv4 client (68) or relay (67) port,
  514. /// depending if the response will be sent directly to a client.
  515. ///
  516. /// The source port is always set to DHCPv4 server port (67).
  517. ///
  518. /// The interface selected for the response is always the same as the
  519. /// one through which the query has been received.
  520. ///
  521. /// The source address for the response is the IPv4 address assigned to
  522. /// the interface being used to send the response. This function uses
  523. /// @c IfaceMgr to get the socket bound to the IPv4 address on the
  524. /// particular interface.
  525. ///
  526. /// @note This method is static because it is not dependent on the class
  527. /// state.
  528. static void adjustIfaceData(const Pkt4Ptr& query, const Pkt4Ptr& response);
  529. /// @brief Sets remote addresses for outgoing packet.
  530. ///
  531. /// This method sets the local and remote addresses on outgoing packet.
  532. /// The addresses being set depend on the following conditions:
  533. /// - has incoming packet been relayed,
  534. /// - is direct response to a client without address supported,
  535. /// - type of the outgoing packet,
  536. /// - broadcast flag set in the incoming packet.
  537. ///
  538. /// @warning This method does not check whether provided packet pointers
  539. /// are valid. Make sure that pointers are correct before calling this
  540. /// function.
  541. ///
  542. /// @note This method is static because it is not dependent on the class
  543. /// state.
  544. ///
  545. /// @param question instance of a packet received by a server.
  546. /// @param [out] response response packet which addresses are to be
  547. /// adjusted.
  548. static void adjustRemoteAddr(const Pkt4Ptr& question,
  549. const Pkt4Ptr& response);
  550. /// @brief converts server-id to text
  551. /// Converts content of server-id option to a text representation, e.g.
  552. /// "192.0.2.1"
  553. ///
  554. /// @param opt option that contains server-id
  555. /// @return string representation
  556. static std::string srvidToString(const OptionPtr& opt);
  557. /// @brief Computes DHCID from a lease.
  558. ///
  559. /// This method creates an object which represents DHCID. The DHCID value
  560. /// is computed as described in RFC4701. The section 3.3. of RFC4701
  561. /// specifies the DHCID RR Identifier Type codes:
  562. /// - 0x0000 The 1 octet htype followed by glen octets of chaddr
  563. /// - 0x0001 The data octets from the DHCPv4 client's Client Identifier
  564. /// option.
  565. /// - 0x0002 The client's DUID.
  566. ///
  567. /// Currently this function supports first two of these identifiers.
  568. /// The 0x0001 is preferred over the 0x0000 - if the client identifier
  569. /// option is present, the former is used. If the client identifier
  570. /// is absent, the latter is used.
  571. ///
  572. /// @todo Implement support for 0x0002 DHCID identifier type.
  573. ///
  574. /// @param lease A pointer to the structure describing a lease.
  575. /// @return An object encapsulating DHCID to be used for DNS updates.
  576. /// @throw DhcidComputeError If the computation of the DHCID failed.
  577. static isc::dhcp_ddns::D2Dhcid computeDhcid(const Lease4Ptr& lease);
  578. /// @brief Selects a subnet for a given client's packet.
  579. ///
  580. /// @param question client's message
  581. /// @return selected subnet (or NULL if no suitable subnet was found)
  582. isc::dhcp::Subnet4Ptr selectSubnet(const Pkt4Ptr& question) const;
  583. /// indicates if shutdown is in progress. Setting it to true will
  584. /// initiate server shutdown procedure.
  585. volatile bool shutdown_;
  586. /// @brief dummy wrapper around IfaceMgr::receive4
  587. ///
  588. /// This method is useful for testing purposes, where its replacement
  589. /// simulates reception of a packet. For that purpose it is protected.
  590. virtual Pkt4Ptr receivePacket(int timeout);
  591. /// @brief dummy wrapper around IfaceMgr::send()
  592. ///
  593. /// This method is useful for testing purposes, where its replacement
  594. /// simulates transmission of a packet. For that purpose it is protected.
  595. virtual void sendPacket(const Pkt4Ptr& pkt);
  596. /// @brief Implements a callback function to parse options in the message.
  597. ///
  598. /// @param buf a A buffer holding options in on-wire format.
  599. /// @param option_space A name of the option space which holds definitions
  600. /// of to be used to parse options in the packets.
  601. /// @param [out] options A reference to the collection where parsed options
  602. /// will be stored.
  603. /// @return An offset to the first byte after last parsed option.
  604. size_t unpackOptions(const OptionBuffer& buf,
  605. const std::string& option_space,
  606. isc::dhcp::OptionCollection& options);
  607. /// @brief Assigns incoming packet to zero or more classes.
  608. ///
  609. /// @note For now, the client classification is very simple. It just uses
  610. /// content of the vendor-class-identifier option as a class. The resulting
  611. /// class will be stored in packet (see @ref isc::dhcp::Pkt4::classes_ and
  612. /// @ref isc::dhcp::Pkt4::inClass).
  613. ///
  614. /// @param pkt packet to be classified
  615. void classifyPacket(const Pkt4Ptr& pkt);
  616. /// @brief Performs packet processing specific to a class
  617. ///
  618. /// This processing is a likely candidate to be pushed into hooks.
  619. ///
  620. /// @param query incoming client's packet
  621. /// @param rsp server's response
  622. /// @return true if successful, false otherwise (will prevent sending response)
  623. bool classSpecificProcessing(const Pkt4Ptr& query, const Pkt4Ptr& rsp);
  624. private:
  625. /// @brief Constructs netmask option based on subnet4
  626. /// @param subnet subnet for which the netmask will be calculated
  627. ///
  628. /// @return Option that contains netmask information
  629. static OptionPtr getNetmaskOption(const Subnet4Ptr& subnet);
  630. /// @brief Implements the error handler for socket open failure.
  631. ///
  632. /// This callback function is installed on the @c isc::dhcp::IfaceMgr
  633. /// when IPv4 sockets are being open. When socket fails to open for
  634. /// any reason, this function is called. It simply logs the error message.
  635. ///
  636. /// @param errmsg An error message containing a cause of the failure.
  637. static void ifaceMgrSocket4ErrorHandler(const std::string& errmsg);
  638. /// @brief Allocation Engine.
  639. /// Pointer to the allocation engine that we are currently using
  640. /// It must be a pointer, because we will support changing engines
  641. /// during normal operation (e.g. to use different allocators)
  642. boost::shared_ptr<AllocEngine> alloc_engine_;
  643. uint16_t port_; ///< UDP port number on which server listens.
  644. bool use_bcast_; ///< Should broadcast be enabled on sockets (if true).
  645. /// Indexes for registered hook points
  646. int hook_index_pkt4_receive_;
  647. int hook_index_subnet4_select_;
  648. int hook_index_pkt4_send_;
  649. };
  650. }; // namespace isc::dhcp
  651. }; // namespace isc
  652. #endif // DHCP4_SRV_H