csv_lease_file6.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 CSV_LEASE_FILE6_H
  15. #define CSV_LEASE_FILE6_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcpsrv/lease.h>
  19. #include <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/lease_file_stats.h>
  21. #include <util/csv_file.h>
  22. #include <stdint.h>
  23. #include <string>
  24. namespace isc {
  25. namespace dhcp {
  26. /// @brief Provides methods to access CSV file with DHCPv6 leases.
  27. ///
  28. /// This class contains methods customized to read and write DHCPv6 leases from
  29. /// and to the CSV file. It expects that the CSV file being parsed contains a
  30. /// set of columns with well known names (initialized in the class constructor).
  31. ///
  32. /// @todo This class doesn't validate the lease values read from the file.
  33. /// The @c Lease6 is a structure that should be itself responsible for this
  34. /// validation (see http://kea.isc.org/ticket/2405). However, when #2405
  35. /// is implemented, the @c next function may need to be updated to use the
  36. /// validation capablity of @c Lease6.
  37. class CSVLeaseFile6 : public isc::util::CSVFile, public LeaseFileStats {
  38. public:
  39. /// @brief Constructor.
  40. ///
  41. /// Initializes columns of the lease file.
  42. ///
  43. /// @param filename Name of the lease file.
  44. CSVLeaseFile6(const std::string& filename);
  45. /// @brief Opens a lease file.
  46. ///
  47. /// This function calls the base class open to do the
  48. /// work of opening a file. It is used to clear any
  49. /// statistics associated with any previous use of the file
  50. /// While it doesn't throw any exceptions of its own
  51. /// the base class may do so.
  52. virtual void open(const bool seek_to_end = false);
  53. /// @brief Appends the lease record to the CSV file.
  54. ///
  55. /// This function doesn't throw exceptions itself. In theory, exceptions
  56. /// are possible when the index of the indexes of the values being written
  57. /// to the file are invalid. However, this would have been a programming
  58. /// error.
  59. ///
  60. /// @param lease Structure representing a DHCPv6 lease.
  61. void append(const Lease6& lease);
  62. /// @brief Reads next lease from the CSV file.
  63. ///
  64. /// If this function hits an error during lease read, it sets the error
  65. /// message using @c CSVFile::setReadMsg and returns false. The error
  66. /// string may be read using @c CSVFile::getReadMsg.
  67. ///
  68. /// This function is exception safe.
  69. ///
  70. /// @param [out] lease Pointer to the lease read from CSV file or
  71. /// NULL pointer if lease hasn't been read.
  72. ///
  73. /// @return Boolean value indicating that the new lease has been
  74. /// read from the CSV file (if true), or that the error has occurred
  75. /// (false).
  76. ///
  77. /// @todo Make sure that the values read from the file are correct.
  78. /// The appropriate @c Lease6 validation mechanism should be used once
  79. /// ticket http://kea.isc.org/ticket/2405 is implemented.
  80. bool next(Lease6Ptr& lease);
  81. private:
  82. /// @brief Initializes columns of the CSV file holding leases.
  83. ///
  84. /// This function initializes the following columns:
  85. /// - address
  86. /// - duid
  87. /// - valid_lifetime
  88. /// - expire
  89. /// - subnet_id
  90. /// - pref_lifetime
  91. /// - lease_type
  92. /// - iaid
  93. /// - prefix_len
  94. /// - fqdn_fwd
  95. /// - fqdn_rev
  96. /// - hostname
  97. /// - hwaddr
  98. /// - state
  99. void initColumns();
  100. ///
  101. /// @name Methods which read specific lease fields from the CSV row.
  102. ///
  103. //@{
  104. ///
  105. /// @brief Reads lease type from the CSV file row.
  106. ///
  107. /// @param row CSV file row holding lease information.
  108. Lease::Type readType(const util::CSVRow& row);
  109. /// @brief Reads lease address from the CSV file row.
  110. ///
  111. /// @param row CSV file row holding lease information.
  112. asiolink::IOAddress readAddress(const util::CSVRow& row);
  113. /// @brief Reads DUID from the CSV file row.
  114. ///
  115. /// @param row CSV file row holding lease information.
  116. DuidPtr readDUID(const util::CSVRow& row);
  117. /// @brief Reads IAID from the CSV file row.
  118. ///
  119. /// @param row CSV file row holding lease information.
  120. uint32_t readIAID(const util::CSVRow& row);
  121. /// @brief Reads preferred lifetime from the CSV file row.
  122. ///
  123. /// @param row CSV file row holding lease information.
  124. uint32_t readPreferred(const util::CSVRow& row);
  125. /// @brief Reads valid lifetime from the CSV file row.
  126. ///
  127. /// @param row CSV file row holding lease information.
  128. uint32_t readValid(const util::CSVRow& row);
  129. /// @brief Reads cltt value from the CSV file row.
  130. ///
  131. /// @param row CSV file row holding lease information.
  132. uint32_t readCltt(const util::CSVRow& row);
  133. /// @brief Reads subnet id from the CSV file row.
  134. ///
  135. /// @param row CSV file row holding lease information.
  136. SubnetID readSubnetID(const util::CSVRow& row);
  137. /// @brief Reads prefix length from the CSV file row.
  138. ///
  139. /// @param row CSV file row holding lease information.
  140. uint8_t readPrefixLen(const util::CSVRow& row);
  141. /// @brief Reads the FQDN forward flag from the CSV file row.
  142. ///
  143. /// @param row CSV file row holding lease information.
  144. bool readFqdnFwd(const util::CSVRow& row);
  145. /// @brief Reads the FQDN reverse flag from the CSV file row.
  146. ///
  147. /// @param row CSV file row holding lease information.
  148. bool readFqdnRev(const util::CSVRow& row);
  149. /// @brief Reads hostname from the CSV file row.
  150. ///
  151. /// @param row CSV file row holding lease information.
  152. std::string readHostname(const util::CSVRow& row);
  153. /// @brief Reads HW address from the CSV file row.
  154. ///
  155. /// @param row CSV file row holding lease information.
  156. /// @return pointer to the HWAddr structure that was read
  157. HWAddrPtr readHWAddr(const util::CSVRow& row);
  158. /// @brief Reads lease state from the CSV file row.
  159. ///
  160. /// @param row CSV file row holding lease information.
  161. uint32_t readState(const util::CSVRow& row);
  162. //@}
  163. };
  164. } // namespace isc::dhcp
  165. } // namespace isc
  166. #endif // CSV_LEASE_FILE6_H