csv_lease_file4.h 5.9 KB

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