dhcp6_client.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Copyright (C) 2014-2015 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 DHCP6_CLIENT_H
  15. #define DHCP6_CLIENT_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp6/tests/dhcp6_test_utils.h>
  20. #include <boost/noncopyable.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include <set>
  23. #include <vector>
  24. namespace isc {
  25. namespace dhcp {
  26. namespace test {
  27. /// @brief DHCPv6 client used for unit testing.
  28. ///
  29. /// This class implements a DHCPv6 "client" which interoperates with the
  30. /// @c NakedDhcpv6Srv class. It calls @c NakedDhcpv6Srv::fakeRecive to
  31. /// deliver client messages to the server for processing. The server places
  32. /// the response in the @c NakedDhcpv6Srv::fake_sent_ container. The client
  33. /// pops messages from this container which simulates reception of the
  34. /// response from the server.
  35. ///
  36. /// The client maintains the leases it acquired from the server. If it has
  37. /// acquired the lease as a result of SARR exchange, it will use this lease
  38. /// when the Rebind process is triggered by the unit test.
  39. ///
  40. /// The client exposes a set of functions which simulate different exchange
  41. /// types between the client and the server. It also provides the access to
  42. /// the objects encapsulating responses from the server so as it is possible
  43. /// to verify from the unit test that the server's response is correct.
  44. ///
  45. /// @todo This class has been implemented to simplify the structure of the
  46. /// unit test and to make unit tests code self-explanatory. Currently,
  47. /// this class is mostly used by the unit tests which test Rebind processing
  48. /// logic. At some point we may want to use this class to test some other
  49. /// message types, e.g. Renew, in which case it may need to be extended.
  50. /// Also, once we implement the support for multiple IAAddr and IAPrefix
  51. /// options within single IA, the logic which maintains leases will have
  52. /// to be extended to support it.
  53. class Dhcp6Client : public boost::noncopyable {
  54. public:
  55. /// @brief Holds an information about single lease.
  56. struct LeaseInfo {
  57. /// @brief A structure describing the lease.
  58. Lease6 lease_;
  59. /// @brief Holds the last status code that server has sent for
  60. /// the particular lease.
  61. uint16_t status_code_;
  62. /// @brief Default constructor for the structure.
  63. LeaseInfo() :
  64. lease_(), status_code_(0) { }
  65. };
  66. /// @brief Holds the current client configuration obtained from the
  67. /// server over DHCP.
  68. ///
  69. /// Currently it simply contains the collection of leases acquired.
  70. struct Configuration {
  71. std::vector<LeaseInfo> leases_;
  72. /// @brief Status code received in the global option scope.
  73. uint16_t status_code_;
  74. /// @brief Indicates if the status code has been received in the
  75. /// last transaction.
  76. bool received_status_code_;
  77. /// @brief Constructor.
  78. Configuration() {
  79. clear();
  80. }
  81. /// @brief Clears configuration.
  82. void clear() {
  83. leases_.clear();
  84. resetGlobalStatusCode();
  85. }
  86. /// @brief Clears global status code.
  87. ///
  88. /// This function should be called before the new message is received.
  89. void resetGlobalStatusCode() {
  90. status_code_ = 0;
  91. received_status_code_ = false;
  92. }
  93. };
  94. /// @brief Holds the DHCPv6 messages taking part in transaction between
  95. /// the client and the server.
  96. struct Context {
  97. /// @brief Holds the last sent message from the client to the server.
  98. Pkt6Ptr query_;
  99. /// @brief Holds the last sent message by the server to the client.
  100. Pkt6Ptr response_;
  101. };
  102. /// @brief Creates a new client.
  103. ///
  104. /// This constructor initializes the class members to default values:
  105. /// - relay link-addr = 3000:1::1
  106. /// - first transaction id = 0
  107. /// - dest-addr = All_DHCP_Relay_Agents_and_Servers
  108. /// - duid (LLT) = <random 4 bytes>00010203040506
  109. /// - link-local-addr = fe80::3a60:77ff:fed5:cdef
  110. /// - IA_NA not requested
  111. /// - IA_PD not requested
  112. /// - not relayed
  113. Dhcp6Client();
  114. /// @brief Creates a new client that communicates with a specified server.
  115. ///
  116. /// This constructor allows passing a pointer to the server object which
  117. /// should be used in a test. The server may be preconfigured before passed
  118. /// to the constructor. The default configuration used by the client is:
  119. /// - relay link-addr = 3000:1::1
  120. /// - first transaction id = 0
  121. /// - dest-addr = All_DHCP_Relay_Agents_and_Servers
  122. /// - duid (LLT) = <random 4 bytes>00010203040506
  123. /// - link-local-addr = fe80::3a60:77ff:fed5:cdef
  124. /// - IA_NA not requested
  125. /// - IA_PD not requested
  126. /// - not relayed
  127. ///
  128. /// @param srv Object representing server under test.
  129. Dhcp6Client(boost::shared_ptr<isc::test::NakedDhcpv6Srv>& srv);
  130. /// @brief Create lease for the client.
  131. ///
  132. /// This function creates new lease on the client side without contacting
  133. /// the server. This may be useful for the negative tests in which the
  134. /// client is supposed to send invalid addresses/prefixes to the server
  135. /// and expect certain responses.
  136. ///
  137. /// @param lease A lease to be applied for the client.
  138. void createLease(const Lease6& lease);
  139. /// @brief Performs a 4-way echange between the client and the server.
  140. ///
  141. /// If the 4-way exchange is successful, the client should acquire leases
  142. /// according to the server's current configuration and the type of leases
  143. /// that have been requested (IA_NA, IA_PD).
  144. ///
  145. /// The leases acquired are accessible through the @c config_ member.
  146. ///
  147. /// @throw This function doesn't throw exceptions on its own, but it calls
  148. /// functions that are not exception safe, so it may throw exceptions if
  149. /// error occurs.
  150. ///
  151. /// @todo Perform sanity checks on returned messages.
  152. void doSARR();
  153. /// @brief Send Solicit and receive Advertise.
  154. ///
  155. /// This function simulates the first transaction of the 4-way exchange,
  156. /// i.e. sends a Solicit to the server and receives Advertise. It doesn't
  157. /// set the lease configuration in the @c config_.
  158. ///
  159. /// @throw This function doesn't throw exceptions on its own, but it calls
  160. /// functions that are not exception safe, so it may throw exceptions if
  161. /// error occurs.
  162. ///
  163. /// @todo Perform sanity checks on returned messages.
  164. void doSolicit();
  165. /// @brief Sends a Rebind to the server and receives the Reply.
  166. ///
  167. /// This function simulates sending the Rebind message to the server and
  168. /// receiving server's response (if any). The client uses existing leases
  169. /// (either address or prefixes) and places them in the Rebind message.
  170. /// If the server responds to the Rebind (and extends the lease lifetimes)
  171. /// the current lease configuration is updated.
  172. ///
  173. /// @throw This function doesn't throw exceptions on its own, but it calls
  174. /// functions that are not exception safe, so it may throw exceptions if
  175. /// error occurs.
  176. ///
  177. /// @todo Perform sanity checks on returned messages.
  178. void doRebind();
  179. /// @brief Sends Request to the server and receives Reply.
  180. ///
  181. /// This function simulates sending the Request message to the server and
  182. /// receiving server's response (if any). The client copies IA options
  183. /// from the current context (server's Advertise) to request acquisition
  184. /// of offered IAs. If the server responds to the Request (leases are
  185. /// acquired) the client's lease configuration is updated.
  186. ///
  187. /// @throw This function doesn't throw exceptions on its own, but it calls
  188. /// functions that are not exception safe, so it may throw exceptions if
  189. /// error occurs.
  190. ///
  191. /// @todo Perform sanity checks on returned messages.
  192. void doRequest();
  193. /// @brief Sends Confirm to the server and receives Reply.
  194. ///
  195. /// This function simulates sending the Confirm message to the server and
  196. /// receiving server's response (if any).
  197. void doConfirm();
  198. /// @brief Performs stateless (inf-request / reply) exchange.
  199. ///
  200. /// This function generates Information-request message, sends it
  201. /// to the server and then receives the reply. Contents of the Inf-Request
  202. /// are controlled by use_na_, use_pd_, use_client_id_ and use_oro_
  203. /// fields. This method does not process the response in any specific
  204. /// way, just stores it.
  205. void doInfRequest();
  206. /// @brief Removes the stateful configuration obtained from the server.
  207. ///
  208. /// It removes all leases held by the client.
  209. void clearConfig() {
  210. config_.clear();
  211. }
  212. /// @brief Simulates aging of leases by the specified number of seconds.
  213. ///
  214. /// This function moves back the time of acquired leases by the specified
  215. /// number of seconds. It is useful for checking whether the particular
  216. /// lease has been later updated (e.g. as a result of Rebind) as it is
  217. /// expected that the fresh lease has cltt set to "now" (not to the time
  218. /// in the past).
  219. void fastFwdTime(const uint32_t secs);
  220. /// @brief Returns DUID option used by the client.
  221. OptionPtr getClientId() const;
  222. /// @brief Returns current context.
  223. const Context& getContext() const {
  224. return (context_);
  225. }
  226. /// @brief Returns the collection of IAIDs held by the client.
  227. std::set<uint32_t> getIAIDs() const;
  228. /// @brief Returns lease at specified index.
  229. ///
  230. /// @warning This method doesn't check if the specified index is out of
  231. /// range. The caller is responsible for using a correct offset by
  232. /// invoking the @c getLeaseNum function.
  233. ///
  234. /// @param at Index of the lease held by the client.
  235. /// @return A lease at the specified index.
  236. Lease6 getLease(const size_t at) const {
  237. return (config_.leases_[at].lease_);
  238. }
  239. /// @brief Returns collection of leases for specified IAID.
  240. ///
  241. /// @param iaid IAID for which the leases should be returned.
  242. ///
  243. /// @return Vector containing leases for the IAID.
  244. std::vector<Lease6> getLeasesByIAID(const uint32_t iaid) const;
  245. /// @brief Returns the value of the global status code for the last
  246. /// transaction.
  247. uint16_t getStatusCode() const {
  248. return (config_.status_code_);
  249. }
  250. /// @brief Returns status code set by the server for the lease.
  251. ///
  252. /// @warning This method doesn't check if the specified index is out of
  253. /// range. The caller is responsible for using a correct offset by
  254. /// invoking the @c getLeaseNum function.
  255. ///
  256. /// @param at Index of the lease held by the client.
  257. /// @return A status code for the lease at the specified index.
  258. uint16_t getStatusCode(const size_t at) const {
  259. return (config_.leases_[at].status_code_);
  260. }
  261. /// @brief Returns number of acquired leases.
  262. size_t getLeaseNum() const {
  263. return (config_.leases_.size());
  264. }
  265. /// @brief Returns the server that the client is communicating with.
  266. boost::shared_ptr<isc::test::NakedDhcpv6Srv> getServer() const {
  267. return (srv_);
  268. }
  269. /// @brief Modifies the client's DUID (adds one to it).
  270. ///
  271. /// The DUID should be modified to test negative scenarios when the client
  272. /// acquires a lease and tries to renew it with a different DUID. The server
  273. /// should detect the DUID mismatch and react accordingly.
  274. ///
  275. /// The DUID modification affects the value returned by the
  276. /// @c Dhcp6Client::getClientId
  277. void modifyDUID();
  278. /// @brief Checks if the global status code was received in the response
  279. /// from the server.
  280. ///
  281. /// @return true if the global status code option was received.
  282. bool receivedStatusCode() const {
  283. return (config_.received_status_code_);
  284. }
  285. /// @brief Sets destination address for the messages being sent by the
  286. /// client.
  287. ///
  288. /// By default, the client uses All_DHCP_Relay_Agents_and_Servers
  289. /// multicast address to communicate with the server. In certain cases
  290. /// it ay be desired that different address is used (e.g. unicast in Renew).
  291. /// This function sets the new address for all future exchanges with the
  292. /// server.
  293. ///
  294. /// @param dest_addr New destination address.
  295. void setDestAddress(const asiolink::IOAddress& dest_addr) {
  296. dest_addr_ = dest_addr;
  297. }
  298. /// @brief Sets a prefix hint to be sent to a server.
  299. ///
  300. /// @param pref_lft Preferred lifetime.
  301. /// @param valid_lft Valid lifetime.
  302. /// @param len Prefix length.
  303. /// @param prefix Prefix for which the client has a preference.
  304. void useHint(const uint32_t pref_lft, const uint32_t valid_lft,
  305. const uint8_t len, const std::string& prefix);
  306. /// @brief Place IA_NA options to request address assignment.
  307. ///
  308. /// This function configures the client to place IA_NA options in its
  309. /// Solicit messages to request the IPv6 address assignment.
  310. ///
  311. /// @param use Parameter which 'true' value indicates that client should
  312. /// request address assignment.
  313. void useNA(const bool use = true) {
  314. use_na_ = use;
  315. }
  316. /// @brief Place IA_PD options to request prefix assignment.
  317. ///
  318. /// This function configures the client to place IA_PD options in its
  319. /// Solicit messages to request the IPv6 address assignment.
  320. ///
  321. /// @param use Parameter which 'true' value indicates that client should
  322. /// request prefix assignment.
  323. void usePD(const bool use = true) {
  324. use_pd_ = use;
  325. }
  326. /// @brief Simulate sending messages through a relay.
  327. ///
  328. /// @param use Parameter which 'true' value indicates that client should
  329. /// simulate sending messages via relay.
  330. /// @param link_addr Relay link-addr.
  331. void useRelay(const bool use = true,
  332. const asiolink::IOAddress& link_addr = asiolink::IOAddress("3000:1::1")) {
  333. use_relay_ = use;
  334. relay_link_addr_ = link_addr;
  335. }
  336. /// @brief Controls whether the client should send a client-id or not
  337. /// @param send should the client-id be sent?
  338. void useClientId(bool send) {
  339. use_client_id_ = send;
  340. }
  341. /// @brief Lease configuration obtained by the client.
  342. Configuration config_;
  343. /// @brief Link address of the relay to be used for relayed messages.
  344. asiolink::IOAddress relay_link_addr_;
  345. /// @brief Controls whether the client will send ORO
  346. ///
  347. /// The actual content of the ORO is specified in oro_.
  348. /// It is useful to split the actual content and the ORO sending
  349. /// decision, so we could test cases of sending empty ORO.
  350. /// @param send controls whether ORO will be sent or not.
  351. void useORO(bool send) {
  352. use_oro_ = send;
  353. }
  354. /// @brief Instructs client to request specified option in ORO
  355. ///
  356. /// @param option_code client will request this option code
  357. void requestOption(uint16_t option_code) {
  358. use_oro_ = true;
  359. oro_.push_back(option_code);
  360. }
  361. private:
  362. /// @brief Applies the new leases for the client.
  363. ///
  364. /// This method is called when the client obtains a new configuration
  365. /// from the server in the Reply message. This function adds new leases
  366. /// or replaces existing ones, on the client's side. Client uses these
  367. /// leases in any later communication with the server when doing Renew
  368. /// or Rebind.
  369. ///
  370. /// @param reply Server response.
  371. ///
  372. /// @todo Currently this function supports one IAAddr or IAPrefix option
  373. /// within IA. We will need to extend it to support multiple options
  374. /// within a single IA once server supports that.
  375. void applyRcvdConfiguration(const Pkt6Ptr& reply);
  376. /// @brief Applies configuration for the single lease.
  377. ///
  378. /// This method is called by the @c Dhcp6Client::applyRcvdConfiguration for
  379. /// each individual lease.
  380. ///
  381. /// @param lease_info Structure holding new lease information.
  382. void applyLease(const LeaseInfo& lease_info);
  383. /// @brief Copy IA options from one message to another.
  384. ///
  385. /// This method copies IA_NA and IA_PD options from one message to another.
  386. /// It is useful when the client needs to construct the Request message
  387. /// using addresses and prefixes returned by the client in Advertise.
  388. ///
  389. /// @param source Message from which IA options will be copied.
  390. /// @param dest Message to which IA options will be copied.
  391. ///
  392. /// @todo Add support for IA_TA.
  393. void copyIAs(const Pkt6Ptr& source, const Pkt6Ptr& dest);
  394. /// @brief Creates IA options from existing configuration.
  395. ///
  396. /// This method iterates over existing leases that client acquired and
  397. /// places corresponding IA_NA or IA_PD options into a specified message.
  398. /// This is useful to construct Renew, Rebind or Confirm message from the
  399. /// existing configuration that client has obtained using 4-way exchange.
  400. ///
  401. /// If there are no leases no IA options will be added. If the lease exists
  402. /// but any of the lifetime values is set to 0, the IA option will be added
  403. /// but the IAAddr (or IAPrefix) option will not be added.
  404. ///
  405. /// @param dest Message to which the IA options will be added.
  406. void copyIAsFromLeases(const Pkt6Ptr& dest) const;
  407. /// @brief Creates client's side DHCP message.
  408. ///
  409. /// @param msg_type Type of the message to be created.
  410. /// @return An instance of the message created.
  411. Pkt6Ptr createMsg(const uint8_t msg_type);
  412. /// @brief Generates DUID for the client.
  413. ///
  414. /// @param duid_type Type of the DUID. Currently, only LLT is accepted.
  415. /// @return Object encapsulating a DUID.
  416. DuidPtr generateDUID(DUID::DUIDType duid_type) const;
  417. /// @brief Simulates reception of the message from the server.
  418. ///
  419. /// @return Received message.
  420. Pkt6Ptr receiveOneMsg();
  421. /// @brief Simulates sending a message to the server.
  422. ///
  423. /// This function instantly triggers processing of the message by the
  424. /// server. The server's response can be gathered by invoking the
  425. /// @c receiveOneMsg function.
  426. ///
  427. /// @param msg Message to be sent.
  428. void sendMsg(const Pkt6Ptr& msg);
  429. /// @brief Current context (sent and received message).
  430. Context context_;
  431. /// @biref Current transaction id (altered on each send).
  432. uint32_t curr_transid_;
  433. /// @brief Currently use destination address.
  434. asiolink::IOAddress dest_addr_;
  435. /// @brief Currently used DUID.
  436. DuidPtr duid_;
  437. /// @brief Currently used link local address.
  438. asiolink::IOAddress link_local_;
  439. /// @brief Pointer to the server that the client is communicating with.
  440. boost::shared_ptr<isc::test::NakedDhcpv6Srv> srv_;
  441. bool use_na_; ///< Enable address assignment.
  442. bool use_pd_; ///< Enable prefix delegation.
  443. bool use_relay_; ///< Enable relaying messages to the server.
  444. bool use_oro_; ///< Conth
  445. bool use_client_id_;
  446. /// @brief Pointer to the option holding a prefix hint.
  447. Option6IAPrefixPtr prefix_hint_;
  448. /// @brief List of options to be requested
  449. ///
  450. /// Content of this vector will be sent as ORO if use_oro_ is set
  451. /// to true. See @ref sendORO for details.
  452. std::vector<uint16_t> oro_;
  453. };
  454. } // end of namespace isc::dhcp::test
  455. } // end of namespace isc::dhcp
  456. } // end of namespace isc
  457. #endif // DHCP6_CLIENT