tsigrecord.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2011 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 <ostream>
  15. #include <string>
  16. #include <util/buffer.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrttl.h>
  20. #include <dns/tsigrecord.h>
  21. using namespace isc::util;
  22. namespace {
  23. // Internally used constants:
  24. // Size in octets for the RR type, class TTL fields.
  25. const size_t RR_COMMON_LEN = 8;
  26. // Size in octets for the fixed part of TSIG RDATAs.
  27. // - Time Signed (6)
  28. // - Fudge (2)
  29. // - MAC Size (2)
  30. // - Original ID (2)
  31. // - Error (2)
  32. // - Other Len (2)
  33. const size_t RDATA_COMMON_LEN = 16;
  34. }
  35. namespace isc {
  36. namespace dns {
  37. TSIGRecord::TSIGRecord(const Name& key_name,
  38. const rdata::any::TSIG& tsig_rdata) :
  39. key_name_(key_name), rdata_(tsig_rdata),
  40. length_(RR_COMMON_LEN + RDATA_COMMON_LEN + key_name_.getLength() +
  41. rdata_.getAlgorithm().getLength() +
  42. rdata_.getMACSize() + rdata_.getOtherLen())
  43. {}
  44. const RRClass&
  45. TSIGRecord::getClass() {
  46. return (RRClass::ANY());
  47. }
  48. namespace {
  49. template <typename OUTPUT>
  50. void
  51. toWireCommon(OUTPUT& output, const rdata::any::TSIG& rdata) {
  52. // RR type, class, TTL are fixed constants.
  53. RRType::TSIG().toWire(output);
  54. TSIGRecord::getClass().toWire(output);
  55. output.writeUint32(TSIGRecord::TSIG_TTL);
  56. // RDLEN
  57. output.writeUint16(RDATA_COMMON_LEN + rdata.getAlgorithm().getLength() +
  58. rdata.getMACSize() + rdata.getOtherLen());
  59. // TSIG RDATA
  60. rdata.toWire(output);
  61. }
  62. }
  63. void
  64. TSIGRecord::toWire(AbstractMessageRenderer& renderer) const {
  65. // key name = owner. note that we disable compression.
  66. renderer.writeName(key_name_, false);
  67. toWireCommon(renderer, rdata_);
  68. }
  69. void
  70. TSIGRecord::toWire(OutputBuffer& buffer) const {
  71. key_name_.toWire(buffer);
  72. toWireCommon(buffer, rdata_);
  73. }
  74. std::string
  75. TSIGRecord::toText() const {
  76. return (key_name_.toText() + " " + RRTTL(TSIG_TTL).toText() + " " +
  77. getClass().toText() + " " + RRType::TSIG().toText() + " " +
  78. rdata_.toText() + "\n");
  79. }
  80. std::ostream&
  81. operator<<(std::ostream& os, const TSIGRecord& record) {
  82. return (os << record.toText());
  83. }
  84. } // namespace dns
  85. } // namespace isc