lease.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright (C) 2012-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. #include <dhcpsrv/lease.h>
  15. #include <util/pointer_util.h>
  16. #include <sstream>
  17. using namespace isc::util;
  18. using namespace std;
  19. namespace isc {
  20. namespace dhcp {
  21. Lease::Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
  22. uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
  23. const bool fqdn_fwd, const bool fqdn_rev,
  24. const std::string& hostname, const HWAddrPtr& hwaddr)
  25. :addr_(addr), t1_(t1), t2_(t2), valid_lft_(valid_lft), cltt_(cltt),
  26. subnet_id_(subnet_id), fixed_(false), hostname_(hostname),
  27. fqdn_fwd_(fqdn_fwd), fqdn_rev_(fqdn_rev), hwaddr_(hwaddr) {
  28. }
  29. std::string
  30. Lease::typeToText(Lease::Type type) {
  31. switch (type) {
  32. case Lease::TYPE_V4:
  33. return string("V4");
  34. case Lease::TYPE_NA:
  35. return string("IA_NA");
  36. case Lease::TYPE_TA:
  37. return string("IA_TA");
  38. case Lease::TYPE_PD:
  39. return string("IA_PD");
  40. break;
  41. default: {
  42. stringstream tmp;
  43. tmp << "unknown (" << type << ")";
  44. return (tmp.str());
  45. }
  46. }
  47. }
  48. bool Lease::expired() const {
  49. // Let's use int64 to avoid problems with negative/large uint32 values
  50. int64_t expire_time = cltt_ + valid_lft_;
  51. return (expire_time < time(NULL));
  52. }
  53. bool
  54. Lease::hasIdenticalFqdn(const Lease& other) const {
  55. return (hostname_ == other.hostname_ &&
  56. fqdn_fwd_ == other.fqdn_fwd_ &&
  57. fqdn_rev_ == other.fqdn_rev_);
  58. }
  59. Lease4::Lease4(const Lease4& other)
  60. : Lease(other.addr_, other.t1_, other.t2_, other.valid_lft_,
  61. other.subnet_id_, other.cltt_, other.fqdn_fwd_,
  62. other.fqdn_rev_, other.hostname_, other.hwaddr_),
  63. ext_(other.ext_) {
  64. fixed_ = other.fixed_;
  65. comments_ = other.comments_;
  66. // Copy the hardware address if it is defined.
  67. if (other.hwaddr_) {
  68. hwaddr_.reset(new HWAddr(*other.hwaddr_));
  69. } else {
  70. hwaddr_.reset();
  71. }
  72. if (other.client_id_) {
  73. client_id_.reset(new ClientId(other.client_id_->getClientId()));
  74. } else {
  75. client_id_.reset();
  76. }
  77. }
  78. Lease4::Lease4(const isc::asiolink::IOAddress& address,
  79. const HWAddrPtr& hw_address,
  80. const ClientIdPtr& client_id,
  81. const uint32_t valid_lifetime,
  82. const uint32_t t1,
  83. const uint32_t t2,
  84. const time_t cltt,
  85. const SubnetID subnet_id,
  86. const bool fqdn_fwd,
  87. const bool fqdn_rev,
  88. const std::string& hostname)
  89. : Lease(address, t1, t2, valid_lifetime, subnet_id, cltt, fqdn_fwd,
  90. fqdn_rev, hostname, hw_address),
  91. ext_(0), client_id_(client_id) {
  92. }
  93. const std::vector<uint8_t>&
  94. Lease4::getClientIdVector() const {
  95. if(!client_id_) {
  96. static std::vector<uint8_t> empty_vec;
  97. return (empty_vec);
  98. }
  99. return (client_id_->getClientId());
  100. }
  101. const std::vector<uint8_t>&
  102. Lease::getHWAddrVector() const {
  103. if (!hwaddr_) {
  104. static std::vector<uint8_t> empty_vec;
  105. return (empty_vec);
  106. }
  107. return (hwaddr_->hwaddr_);
  108. }
  109. bool
  110. Lease4::belongsToClient(const HWAddrPtr& hw_address,
  111. const ClientIdPtr& client_id) const {
  112. // If client id matches, lease matches.
  113. if (equalValues(client_id, client_id_)) {
  114. return (true);
  115. } else if (!client_id || !client_id_) {
  116. // If client id is unspecified, use HW address.
  117. if (equalValues(hw_address, hwaddr_)) {
  118. return (true);
  119. }
  120. }
  121. return (false);
  122. }
  123. Lease4&
  124. Lease4::operator=(const Lease4& other) {
  125. if (this != &other) {
  126. addr_ = other.addr_;
  127. t1_ = other.t1_;
  128. t2_ = other.t2_;
  129. valid_lft_ = other.valid_lft_;
  130. cltt_ = other.cltt_;
  131. subnet_id_ = other.subnet_id_;
  132. fixed_ = other.fixed_;
  133. hostname_ = other.hostname_;
  134. fqdn_fwd_ = other.fqdn_fwd_;
  135. fqdn_rev_ = other.fqdn_rev_;
  136. comments_ = other.comments_;
  137. ext_ = other.ext_;
  138. // Copy the hardware address if it is defined.
  139. if (other.hwaddr_) {
  140. hwaddr_.reset(new HWAddr(*other.hwaddr_));
  141. } else {
  142. hwaddr_.reset();
  143. }
  144. if (other.client_id_) {
  145. client_id_.reset(new ClientId(other.client_id_->getClientId()));
  146. } else {
  147. client_id_.reset();
  148. }
  149. }
  150. return (*this);
  151. }
  152. Lease6::Lease6(Type type, const isc::asiolink::IOAddress& addr,
  153. DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
  154. uint32_t t1, uint32_t t2, SubnetID subnet_id,
  155. const HWAddrPtr& hwaddr, uint8_t prefixlen)
  156. : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/, false, false, "", hwaddr),
  157. type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
  158. preferred_lft_(preferred) {
  159. if (!duid) {
  160. isc_throw(InvalidOperation, "DUID is mandatory for an IPv6 lease");
  161. }
  162. cltt_ = time(NULL);
  163. }
  164. Lease6::Lease6(Type type, const isc::asiolink::IOAddress& addr,
  165. DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
  166. uint32_t t1, uint32_t t2, SubnetID subnet_id,
  167. const bool fqdn_fwd, const bool fqdn_rev,
  168. const std::string& hostname, const HWAddrPtr& hwaddr,
  169. uint8_t prefixlen)
  170. : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/,
  171. fqdn_fwd, fqdn_rev, hostname, hwaddr),
  172. type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
  173. preferred_lft_(preferred) {
  174. if (!duid) {
  175. isc_throw(InvalidOperation, "DUID is mandatory for an IPv6 lease");
  176. }
  177. cltt_ = time(NULL);
  178. }
  179. Lease6::Lease6()
  180. : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0, false, false, "",
  181. HWAddrPtr()), type_(TYPE_NA), prefixlen_(0), iaid_(0),
  182. duid_(DuidPtr()), preferred_lft_(0) {
  183. }
  184. const std::vector<uint8_t>&
  185. Lease6::getDuidVector() const {
  186. if (!duid_) {
  187. static std::vector<uint8_t> empty_vec;
  188. return (empty_vec);
  189. }
  190. return (duid_->getDuid());
  191. }
  192. std::string
  193. Lease6::toText() const {
  194. ostringstream stream;
  195. /// @todo: print out DUID
  196. stream << "Type: " << typeToText(type_) << "("
  197. << static_cast<int>(type_) << ")\n";
  198. stream << "Address: " << addr_ << "\n"
  199. << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
  200. << "IAID: " << iaid_ << "\n"
  201. << "Pref life: " << preferred_lft_ << "\n"
  202. << "Valid life: " << valid_lft_ << "\n"
  203. << "Cltt: " << cltt_ << "\n"
  204. << "Hardware addr: " << (hwaddr_?hwaddr_->toText(false):"(none)") << "\n"
  205. << "Subnet ID: " << subnet_id_ << "\n";
  206. return (stream.str());
  207. }
  208. std::string
  209. Lease4::toText() const {
  210. ostringstream stream;
  211. stream << "Address: " << addr_ << "\n"
  212. << "Valid life: " << valid_lft_ << "\n"
  213. << "T1: " << t1_ << "\n"
  214. << "T2: " << t2_ << "\n"
  215. << "Cltt: " << cltt_ << "\n"
  216. << "Hardware addr: " << (hwaddr_ ? hwaddr_->toText(false) : "(none)") << "\n"
  217. << "Client id: " << (client_id_ ? client_id_->toText() : "(none)") << "\n"
  218. << "Subnet ID: " << subnet_id_ << "\n";
  219. return (stream.str());
  220. }
  221. bool
  222. Lease4::operator==(const Lease4& other) const {
  223. return (nullOrEqualValues(hwaddr_, other.hwaddr_) &&
  224. nullOrEqualValues(client_id_, other.client_id_) &&
  225. addr_ == other.addr_ &&
  226. ext_ == other.ext_ &&
  227. subnet_id_ == other.subnet_id_ &&
  228. t1_ == other.t1_ &&
  229. t2_ == other.t2_ &&
  230. valid_lft_ == other.valid_lft_ &&
  231. cltt_ == other.cltt_ &&
  232. fixed_ == other.fixed_ &&
  233. hostname_ == other.hostname_ &&
  234. fqdn_fwd_ == other.fqdn_fwd_ &&
  235. fqdn_rev_ == other.fqdn_rev_ &&
  236. comments_ == other.comments_);
  237. }
  238. bool
  239. Lease6::operator==(const Lease6& other) const {
  240. return (nullOrEqualValues(duid_, other.duid_) &&
  241. nullOrEqualValues(hwaddr_, other.hwaddr_) &&
  242. addr_ == other.addr_ &&
  243. type_ == other.type_ &&
  244. prefixlen_ == other.prefixlen_ &&
  245. iaid_ == other.iaid_ &&
  246. preferred_lft_ == other.preferred_lft_ &&
  247. valid_lft_ == other.valid_lft_ &&
  248. t1_ == other.t1_ &&
  249. t2_ == other.t2_ &&
  250. cltt_ == other.cltt_ &&
  251. subnet_id_ == other.subnet_id_ &&
  252. fixed_ == other.fixed_ &&
  253. hostname_ == other.hostname_ &&
  254. fqdn_fwd_ == other.fqdn_fwd_ &&
  255. fqdn_rev_ == other.fqdn_rev_ &&
  256. comments_ == other.comments_);
  257. }
  258. std::ostream&
  259. operator<<(std::ostream& os, const Lease& lease) {
  260. os << lease.toText();
  261. return (os);
  262. }
  263. } // namespace isc::dhcp
  264. } // namespace isc