csv_lease_file6.h 6.5 KB

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