csv_lease_file6.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. protected:
  82. /// @brief This function validates the header of the Lease6 CSV file.
  83. ///
  84. /// It works similar to @c CSVFile::validateHeader, but if the validation
  85. /// fails, it attempts to add hwaddr column and retry validation.
  86. /// That's useful when attmepting to read CSV file generated in 0.9
  87. /// (did not have hwaddr field) in 0.9.1 or later code.
  88. ///
  89. /// @param header A row holding a header.
  90. /// @return true if header matches the columns; false otherwise.
  91. virtual bool validateHeader(const isc::util::CSVRow& header);
  92. private:
  93. /// @brief Initializes columns of the CSV file holding leases.
  94. ///
  95. /// This function initializes the following columns:
  96. /// - address
  97. /// - duid
  98. /// - valid_lifetime
  99. /// - expire
  100. /// - subnet_id
  101. /// - pref_lifetime
  102. /// - lease_type
  103. /// - iaid
  104. /// - prefix_len
  105. /// - fqdn_fwd
  106. /// - fqdn_rev
  107. /// - hostname
  108. /// - hwaddr
  109. void initColumns();
  110. ///
  111. /// @name Methods which read specific lease fields from the CSV row.
  112. ///
  113. //@{
  114. ///
  115. /// @brief Reads lease type from the CSV file row.
  116. ///
  117. /// @param row CSV file holding lease values.
  118. Lease::Type readType(const util::CSVRow& row);
  119. /// @brief Reads lease address from the CSV file row.
  120. ///
  121. /// @param row CSV file holding lease values.
  122. asiolink::IOAddress readAddress(const util::CSVRow& row);
  123. /// @brief Reads DUID from the CSV file row.
  124. ///
  125. /// @param row CSV file holding lease values.
  126. DuidPtr readDUID(const util::CSVRow& row);
  127. /// @brief Reads IAID from the CSV file row.
  128. ///
  129. /// @param row CSV file holding lease values.
  130. uint32_t readIAID(const util::CSVRow& row);
  131. /// @brief Reads preferred lifetime from the CSV file row.
  132. ///
  133. /// @param row CSV file holding lease values.
  134. uint32_t readPreferred(const util::CSVRow& row);
  135. /// @brief Reads valid lifetime from the CSV file row.
  136. ///
  137. /// @param row CSV file holding lease values.
  138. uint32_t readValid(const util::CSVRow& row);
  139. /// @brief Reads cltt value from the CSV file row.
  140. ///
  141. /// @param row CSV file holding lease values.
  142. uint32_t readCltt(const util::CSVRow& row);
  143. /// @brief Reads subnet id from the CSV file row.
  144. ///
  145. /// @param row CSV file holding lease values.
  146. SubnetID readSubnetID(const util::CSVRow& row);
  147. /// @brief Reads prefix length from the CSV file row.
  148. ///
  149. /// @param row CSV file holding lease values.
  150. uint8_t readPrefixLen(const util::CSVRow& row);
  151. /// @brief Reads the FQDN forward flag from the CSV file row.
  152. ///
  153. /// @param row CSV file holding lease values.
  154. bool readFqdnFwd(const util::CSVRow& row);
  155. /// @brief Reads the FQDN reverse flag from the CSV file row.
  156. ///
  157. /// @param row CSV file holding lease values.
  158. bool readFqdnRev(const util::CSVRow& row);
  159. /// @brief Reads hostname from the CSV file row.
  160. ///
  161. /// @param row CSV file holding lease values.
  162. std::string readHostname(const util::CSVRow& row);
  163. /// @brief Reads HW address from the CSV file row.
  164. ///
  165. /// @param row CSV file holding lease values.
  166. /// @return pointer to the HWAddr structure that was read
  167. HWAddrPtr readHWAddr(const util::CSVRow& row);
  168. //@}
  169. };
  170. } // namespace isc::dhcp
  171. } // namespace isc
  172. #endif // CSV_LEASE_FILE6_H