lease.cc 9.9 KB

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