lease.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include <iostream>
  18. using namespace isc::util;
  19. using namespace std;
  20. namespace isc {
  21. namespace dhcp {
  22. const uint32_t Lease::STATE_DEFAULT = 0x0;
  23. const uint32_t Lease::STATE_DECLINED = 0x1;
  24. const uint32_t Lease::STATE_EXPIRED_RECLAIMED = 0x2;
  25. Lease::Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
  26. uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
  27. const bool fqdn_fwd, const bool fqdn_rev,
  28. const std::string& hostname, const HWAddrPtr& hwaddr)
  29. :addr_(addr), t1_(t1), t2_(t2), valid_lft_(valid_lft), cltt_(cltt),
  30. subnet_id_(subnet_id), hostname_(hostname), fqdn_fwd_(fqdn_fwd),
  31. fqdn_rev_(fqdn_rev), hwaddr_(hwaddr), state_(STATE_DEFAULT) {
  32. }
  33. std::string
  34. Lease::typeToText(Lease::Type type) {
  35. switch (type) {
  36. case Lease::TYPE_V4:
  37. return string("V4");
  38. case Lease::TYPE_NA:
  39. return string("IA_NA");
  40. case Lease::TYPE_TA:
  41. return string("IA_TA");
  42. case Lease::TYPE_PD:
  43. return string("IA_PD");
  44. break;
  45. default: {
  46. stringstream tmp;
  47. tmp << "unknown (" << type << ")";
  48. return (tmp.str());
  49. }
  50. }
  51. }
  52. std::string
  53. Lease::basicStatesToText(const uint32_t state) {
  54. switch (state) {
  55. case STATE_DEFAULT:
  56. return ("default");
  57. case STATE_DECLINED:
  58. return ("declined");
  59. case STATE_EXPIRED_RECLAIMED:
  60. return ("expired-reclaimed");
  61. default:
  62. // The default case will be handled further on
  63. ;
  64. }
  65. std::ostringstream s;
  66. s << "unknown (" << state << ")";
  67. return s.str();
  68. }
  69. bool
  70. Lease::expired() const {
  71. return (getExpirationTime() < time(NULL));
  72. }
  73. bool
  74. Lease::stateExpiredReclaimed() const {
  75. return (state_ == STATE_EXPIRED_RECLAIMED);
  76. }
  77. int64_t
  78. Lease::getExpirationTime() const {
  79. return (static_cast<int64_t>(cltt_) + valid_lft_);
  80. }
  81. bool
  82. Lease::hasIdenticalFqdn(const Lease& other) const {
  83. return (hostname_ == other.hostname_ &&
  84. fqdn_fwd_ == other.fqdn_fwd_ &&
  85. fqdn_rev_ == other.fqdn_rev_);
  86. }
  87. Lease4::Lease4(const Lease4& other)
  88. : Lease(other.addr_, other.t1_, other.t2_, other.valid_lft_,
  89. other.subnet_id_, other.cltt_, other.fqdn_fwd_,
  90. other.fqdn_rev_, other.hostname_, other.hwaddr_) {
  91. // Copy over fields derived from Lease.
  92. state_ = other.state_;
  93. // Copy the hardware address if it is defined.
  94. if (other.hwaddr_) {
  95. hwaddr_.reset(new HWAddr(*other.hwaddr_));
  96. } else {
  97. hwaddr_.reset();
  98. }
  99. if (other.client_id_) {
  100. client_id_.reset(new ClientId(other.client_id_->getClientId()));
  101. } else {
  102. client_id_.reset();
  103. }
  104. }
  105. Lease4::Lease4(const isc::asiolink::IOAddress& address,
  106. const HWAddrPtr& hw_address,
  107. const ClientIdPtr& client_id,
  108. const uint32_t valid_lifetime,
  109. const uint32_t t1,
  110. const uint32_t t2,
  111. const time_t cltt,
  112. const SubnetID subnet_id,
  113. const bool fqdn_fwd,
  114. const bool fqdn_rev,
  115. const std::string& hostname)
  116. : Lease(address, t1, t2, valid_lifetime, subnet_id, cltt, fqdn_fwd,
  117. fqdn_rev, hostname, hw_address),
  118. client_id_(client_id) {
  119. }
  120. std::string
  121. Lease4::statesToText(const uint32_t state) {
  122. return (Lease::basicStatesToText(state));
  123. }
  124. const std::vector<uint8_t>&
  125. Lease4::getClientIdVector() const {
  126. if(!client_id_) {
  127. static std::vector<uint8_t> empty_vec;
  128. return (empty_vec);
  129. }
  130. return (client_id_->getClientId());
  131. }
  132. const std::vector<uint8_t>&
  133. Lease::getHWAddrVector() const {
  134. if (!hwaddr_) {
  135. static std::vector<uint8_t> empty_vec;
  136. return (empty_vec);
  137. }
  138. return (hwaddr_->hwaddr_);
  139. }
  140. bool
  141. Lease4::belongsToClient(const HWAddrPtr& hw_address,
  142. const ClientIdPtr& client_id) const {
  143. // If client id matches, lease matches.
  144. if (equalValues(client_id, client_id_)) {
  145. return (true);
  146. } else if (!client_id || !client_id_) {
  147. // If client id is unspecified, use HW address.
  148. if (equalValues(hw_address, hwaddr_)) {
  149. return (true);
  150. }
  151. }
  152. return (false);
  153. }
  154. void
  155. Lease4::decline(uint32_t probation_period) {
  156. hwaddr_.reset(new HWAddr());
  157. client_id_.reset();
  158. t1_ = 0;
  159. t2_ = 0;
  160. cltt_ = time(NULL);
  161. hostname_ = string("");
  162. fqdn_fwd_ = false;
  163. fqdn_rev_ = false;
  164. state_ = STATE_DECLINED;
  165. valid_lft_ = probation_period;
  166. }
  167. Lease4&
  168. Lease4::operator=(const Lease4& other) {
  169. if (this != &other) {
  170. addr_ = other.addr_;
  171. t1_ = other.t1_;
  172. t2_ = other.t2_;
  173. valid_lft_ = other.valid_lft_;
  174. cltt_ = other.cltt_;
  175. subnet_id_ = other.subnet_id_;
  176. hostname_ = other.hostname_;
  177. fqdn_fwd_ = other.fqdn_fwd_;
  178. fqdn_rev_ = other.fqdn_rev_;
  179. state_ = other.state_;
  180. // Copy the hardware address if it is defined.
  181. if (other.hwaddr_) {
  182. hwaddr_.reset(new HWAddr(*other.hwaddr_));
  183. } else {
  184. hwaddr_.reset();
  185. }
  186. if (other.client_id_) {
  187. client_id_.reset(new ClientId(other.client_id_->getClientId()));
  188. } else {
  189. client_id_.reset();
  190. }
  191. }
  192. return (*this);
  193. }
  194. Lease6::Lease6(Type type, const isc::asiolink::IOAddress& addr,
  195. DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
  196. uint32_t t1, uint32_t t2, SubnetID subnet_id,
  197. const HWAddrPtr& hwaddr, uint8_t prefixlen)
  198. : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/, false, false, "", hwaddr),
  199. type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
  200. preferred_lft_(preferred) {
  201. if (!duid) {
  202. isc_throw(InvalidOperation, "DUID is mandatory for an IPv6 lease");
  203. }
  204. cltt_ = time(NULL);
  205. }
  206. Lease6::Lease6(Type type, const isc::asiolink::IOAddress& addr,
  207. DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
  208. uint32_t t1, uint32_t t2, SubnetID subnet_id,
  209. const bool fqdn_fwd, const bool fqdn_rev,
  210. const std::string& hostname, const HWAddrPtr& hwaddr,
  211. uint8_t prefixlen)
  212. : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/,
  213. fqdn_fwd, fqdn_rev, hostname, hwaddr),
  214. type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
  215. preferred_lft_(preferred) {
  216. if (!duid) {
  217. isc_throw(InvalidOperation, "DUID is mandatory for an IPv6 lease");
  218. }
  219. cltt_ = time(NULL);
  220. }
  221. Lease6::Lease6()
  222. : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0, false, false, "",
  223. HWAddrPtr()), type_(TYPE_NA), prefixlen_(0), iaid_(0),
  224. duid_(DuidPtr()), preferred_lft_(0) {
  225. }
  226. std::string
  227. Lease6::statesToText(const uint32_t state) {
  228. return (Lease::basicStatesToText(state));
  229. }
  230. const std::vector<uint8_t>&
  231. Lease6::getDuidVector() const {
  232. if (!duid_) {
  233. static std::vector<uint8_t> empty_vec;
  234. return (empty_vec);
  235. }
  236. return (duid_->getDuid());
  237. }
  238. void
  239. Lease6::decline(uint32_t ) {
  240. /// @todo: implement this
  241. }
  242. std::string
  243. Lease6::toText() const {
  244. ostringstream stream;
  245. /// @todo: print out DUID
  246. stream << "Type: " << typeToText(type_) << "("
  247. << static_cast<int>(type_) << ")\n";
  248. stream << "Address: " << addr_ << "\n"
  249. << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
  250. << "IAID: " << iaid_ << "\n"
  251. << "Pref life: " << preferred_lft_ << "\n"
  252. << "Valid life: " << valid_lft_ << "\n"
  253. << "Cltt: " << cltt_ << "\n"
  254. << "Hardware addr: " << (hwaddr_?hwaddr_->toText(false):"(none)") << "\n"
  255. << "Subnet ID: " << subnet_id_ << "\n"
  256. << "State: " << statesToText(state_) << "\n";
  257. return (stream.str());
  258. }
  259. std::string
  260. Lease4::toText() const {
  261. ostringstream stream;
  262. stream << "Address: " << addr_ << "\n"
  263. << "Valid life: " << valid_lft_ << "\n"
  264. << "T1: " << t1_ << "\n"
  265. << "T2: " << t2_ << "\n"
  266. << "Cltt: " << cltt_ << "\n"
  267. << "Hardware addr: " << (hwaddr_ ? hwaddr_->toText(false) : "(none)") << "\n"
  268. << "Client id: " << (client_id_ ? client_id_->toText() : "(none)") << "\n"
  269. << "Subnet ID: " << subnet_id_ << "\n"
  270. << "State: " << statesToText(state_) << "\n";
  271. return (stream.str());
  272. }
  273. bool
  274. Lease4::operator==(const Lease4& other) const {
  275. return (nullOrEqualValues(hwaddr_, other.hwaddr_) &&
  276. nullOrEqualValues(client_id_, other.client_id_) &&
  277. addr_ == other.addr_ &&
  278. subnet_id_ == other.subnet_id_ &&
  279. t1_ == other.t1_ &&
  280. t2_ == other.t2_ &&
  281. valid_lft_ == other.valid_lft_ &&
  282. cltt_ == other.cltt_ &&
  283. hostname_ == other.hostname_ &&
  284. fqdn_fwd_ == other.fqdn_fwd_ &&
  285. fqdn_rev_ == other.fqdn_rev_ &&
  286. state_ == other.state_);
  287. }
  288. bool
  289. Lease6::operator==(const Lease6& other) const {
  290. return (nullOrEqualValues(duid_, other.duid_) &&
  291. nullOrEqualValues(hwaddr_, other.hwaddr_) &&
  292. addr_ == other.addr_ &&
  293. type_ == other.type_ &&
  294. prefixlen_ == other.prefixlen_ &&
  295. iaid_ == other.iaid_ &&
  296. preferred_lft_ == other.preferred_lft_ &&
  297. valid_lft_ == other.valid_lft_ &&
  298. t1_ == other.t1_ &&
  299. t2_ == other.t2_ &&
  300. cltt_ == other.cltt_ &&
  301. subnet_id_ == other.subnet_id_ &&
  302. hostname_ == other.hostname_ &&
  303. fqdn_fwd_ == other.fqdn_fwd_ &&
  304. fqdn_rev_ == other.fqdn_rev_ &&
  305. state_ == other.state_);
  306. }
  307. std::ostream&
  308. operator<<(std::ostream& os, const Lease& lease) {
  309. os << lease.toText();
  310. return (os);
  311. }
  312. } // namespace isc::dhcp
  313. } // namespace isc