ncr_generator.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <dhcp/option_data_types.h>
  7. #include <dhcpsrv/cfgmgr.h>
  8. #include <dhcpsrv/dhcpsrv_log.h>
  9. #include <dhcpsrv/d2_client_mgr.h>
  10. #include <dhcpsrv/ncr_generator.h>
  11. #include <stdint.h>
  12. #include <vector>
  13. using namespace isc;
  14. using namespace isc::dhcp;
  15. using namespace isc::dhcp_ddns;
  16. namespace {
  17. /// @brief Sends name change request to D2 using lease information.
  18. ///
  19. /// This method is exception safe.
  20. ///
  21. /// @param lease Pointer to a lease for which NCR should be sent.
  22. /// @param identifier Identifier to be used to generate DHCID for
  23. /// the DNS update. For DHCPv4 it will be hardware address or client
  24. /// identifier. For DHCPv6 it will be a DUID.
  25. /// @param label Client identification information in the textual format.
  26. /// This is used for logging purposes.
  27. ///
  28. /// @tparam LeasePtrType Pointer to a lease.
  29. /// @tparam IdentifierType HW Address, Client Identifier or DUID.
  30. template<typename LeasePtrType, typename IdentifierType>
  31. void queueNCRCommon(const NameChangeType& chg_type, const LeasePtrType& lease,
  32. const IdentifierType& identifier, const std::string& label) {
  33. // Check if there is a need for update.
  34. if (lease->hostname_.empty() || (!lease->fqdn_fwd_ && !lease->fqdn_rev_)
  35. || !CfgMgr::instance().getD2ClientMgr().ddnsEnabled()) {
  36. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
  37. DHCPSRV_QUEUE_NCR_SKIP)
  38. .arg(label)
  39. .arg(lease->addr_.toText());
  40. return;
  41. }
  42. try {
  43. // Create DHCID
  44. std::vector<uint8_t> hostname_wire;
  45. OptionDataTypeUtil::writeFqdn(lease->hostname_, hostname_wire, true);
  46. D2Dhcid dhcid = D2Dhcid(identifier, hostname_wire);
  47. // Create name change request.
  48. NameChangeRequestPtr ncr
  49. (new NameChangeRequest(chg_type, lease->fqdn_fwd_, lease->fqdn_rev_,
  50. lease->hostname_, lease->addr_.toText(),
  51. dhcid, lease->cltt_ + lease->valid_lft_,
  52. lease->valid_lft_));
  53. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL_DATA, DHCPSRV_QUEUE_NCR)
  54. .arg(label)
  55. .arg(chg_type == CHG_ADD ? "add" : "remove")
  56. .arg(ncr->toText());
  57. // Send name change request.
  58. CfgMgr::instance().getD2ClientMgr().sendRequest(ncr);
  59. } catch (const std::exception& ex) {
  60. LOG_ERROR(dhcpsrv_logger, DHCPSRV_QUEUE_NCR_FAILED)
  61. .arg(label)
  62. .arg(chg_type == CHG_ADD ? "add" : "remove")
  63. .arg(lease->addr_.toText())
  64. .arg(ex.what());
  65. }
  66. }
  67. } // end of anonymous namespace
  68. namespace isc {
  69. namespace dhcp {
  70. void queueNCR(const NameChangeType& chg_type, const Lease4Ptr& lease) {
  71. if (lease) {
  72. // Client id takes precedence over HW address.
  73. if (lease->client_id_) {
  74. queueNCRCommon(chg_type, lease, lease->client_id_->getClientId(),
  75. Pkt4::makeLabel(lease->hwaddr_, lease->client_id_));
  76. } else {
  77. // Client id is not specified for the lease. Use HW address
  78. // instead.
  79. queueNCRCommon(chg_type, lease, lease->hwaddr_,
  80. Pkt4::makeLabel(lease->hwaddr_, lease->client_id_));
  81. }
  82. }
  83. }
  84. void queueNCR(const NameChangeType& chg_type, const Lease6Ptr& lease) {
  85. // DUID is required to generate NCR.
  86. if (lease && (lease->type_ != Lease::TYPE_PD) && lease->duid_) {
  87. queueNCRCommon(chg_type, lease, *(lease->duid_),
  88. Pkt6::makeLabel(lease->duid_, lease->hwaddr_));
  89. }
  90. }
  91. }
  92. }