lease_mgr.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2012 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_mgr.h>
  15. #include <exceptions/exceptions.h>
  16. #include <boost/foreach.hpp>
  17. #include <boost/algorithm/string.hpp>
  18. #include <algorithm>
  19. #include <iostream>
  20. #include <iterator>
  21. #include <map>
  22. #include <sstream>
  23. #include <string>
  24. #include <time.h>
  25. using namespace std;
  26. using namespace isc::dhcp;
  27. Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr,
  28. DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
  29. uint32_t t1, uint32_t t2, SubnetID subnet_id, uint8_t prefixlen)
  30. : addr_(addr), type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
  31. preferred_lft_(preferred), valid_lft_(valid), t1_(t1), t2_(t2),
  32. subnet_id_(subnet_id), fixed_(false), fqdn_fwd_(false),
  33. fqdn_rev_(false) {
  34. if (!duid) {
  35. isc_throw(InvalidOperation, "DUID must be specified for a lease");
  36. }
  37. cltt_ = time(NULL);
  38. }
  39. std::string LeaseMgr::getParameter(const std::string& name) const {
  40. ParameterMap::const_iterator param = parameters_.find(name);
  41. if (param == parameters_.end()) {
  42. isc_throw(BadValue, "Parameter not found");
  43. }
  44. return (param->second);
  45. }
  46. std::string
  47. Lease6::toText() {
  48. ostringstream stream;
  49. stream << "Type: " << static_cast<int>(type_) << " (";
  50. switch (type_) {
  51. case Lease6::LEASE_IA_NA:
  52. stream << "IA_NA)\n";
  53. break;
  54. case Lease6::LEASE_IA_TA:
  55. stream << "IA_TA)\n";
  56. break;
  57. case Lease6::LEASE_IA_PD:
  58. stream << "IA_PD)\n";
  59. break;
  60. default:
  61. stream << "unknown)\n";
  62. }
  63. stream << "Address: " << addr_.toText() << "\n"
  64. << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
  65. << "IAID: " << iaid_ << "\n"
  66. << "Pref life: " << preferred_lft_ << "\n"
  67. << "Valid life: " << valid_lft_ << "\n"
  68. << "Cltt: " << cltt_ << "\n"
  69. << "Subnet ID: " << subnet_id_ << "\n";
  70. return (stream.str());
  71. }
  72. bool
  73. Lease6::operator==(const Lease6& other) const {
  74. return (
  75. type_ == other.type_ &&
  76. addr_ == other.addr_ &&
  77. prefixlen_ == other.prefixlen_ &&
  78. iaid_ == other.iaid_ &&
  79. *duid_ == *other.duid_ &&
  80. preferred_lft_ == other.preferred_lft_ &&
  81. valid_lft_ == other.valid_lft_ &&
  82. cltt_ == other.cltt_ &&
  83. subnet_id_ == other.subnet_id_
  84. );
  85. }