rdataset.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef DATASRC_MEMORY_RDATASET_H
  15. #define DATASRC_MEMORY_RDATASET_H 1
  16. #include <util/memory_segment.h>
  17. #include <dns/rrclass.h>
  18. #include <dns/rrtype.h>
  19. #include <dns/rrset.h>
  20. #include <dns/rrttl.h>
  21. #include <boost/interprocess/offset_ptr.hpp>
  22. #include <stdint.h>
  23. namespace isc {
  24. namespace datasrc {
  25. namespace memory {
  26. class RdataEncoder;
  27. /// \brief General error on creating RdataSet.
  28. ///
  29. /// This is thrown when creating \c RdataSet encounters a rare, unsupported
  30. /// situation.
  31. class RdataSetError : public Exception {
  32. public:
  33. RdataSetError(const char* file, size_t line, const char* what) :
  34. Exception(file, line, what) {}
  35. };
  36. class RdataSet {
  37. public:
  38. static RdataSet* create(util::MemorySegment& mem_sgmt,
  39. RdataEncoder& encoder,
  40. dns::ConstRRsetPtr rrset,
  41. dns::ConstRRsetPtr sig_rrset);
  42. static void destroy(util::MemorySegment& mem_sgmt, dns::RRClass rrclass,
  43. RdataSet* rdataset);
  44. typedef boost::interprocess::offset_ptr<RdataSet> RdataSetPtr;
  45. typedef boost::interprocess::offset_ptr<const RdataSet> ConstRdataSetPtr;
  46. // Note: the size and order of the members are carefully chosen to
  47. // maximize efficiency. Don't change them unless there's strong reason
  48. // for that and the consequences are considered.
  49. RdataSetPtr next;
  50. const dns::RRType type;
  51. private:
  52. const uint16_t sig_rdata_count_ : 3;
  53. const uint16_t rdata_count_ : 13;
  54. const uint32_t ttl_; ///< TTL of the RdataSet, net byte order
  55. static const size_t MAX_RDATA_COUNT = (1 << 13) - 1;
  56. static const size_t MAX_RRSIG_COUNT = (1 << 16) - 1;
  57. static const size_t MANY_RRSIG_COUNT = (1 << 3) - 1;
  58. public:
  59. size_t getRdataCount() const { return (rdata_count_); }
  60. size_t getSigRdataCount() const {
  61. if (sig_rdata_count_ < MANY_RRSIG_COUNT) {
  62. return (sig_rdata_count_);
  63. } else {
  64. return (*getExtSIGCountBuf());
  65. }
  66. }
  67. const void* getTTLData() const { return (&ttl_); }
  68. void* getDataBuf() {
  69. return (getDataBuf<void, RdataSet>(this));
  70. }
  71. const void* getDataBuf() const {
  72. return (getDataBuf<const void, const RdataSet>(this));
  73. }
  74. private:
  75. uint16_t* getExtSIGCountBuf() {
  76. return (reinterpret_cast<uint16_t*>(this + 1));
  77. }
  78. const uint16_t* getExtSIGCountBuf() const {
  79. return (reinterpret_cast<const uint16_t*>(this + 1));
  80. }
  81. template <typename RetType, typename ThisType>
  82. static RetType* getDataBuf(ThisType* rdataset) {
  83. if (rdataset->sig_rdata_count_ < MANY_RRSIG_COUNT) {
  84. return (rdataset + 1);
  85. } else {
  86. return (rdataset->getExtSIGCountBuf() + 1);
  87. }
  88. }
  89. RdataSet(dns::RRType type, size_t rdata_count, size_t sig_rdata_count,
  90. dns::RRTTL ttl);
  91. ~RdataSet() {}
  92. };
  93. } // namespace memory
  94. } // namespace datasrc
  95. } // namespace isc
  96. #endif // DATASRC_MEMORY_RDATASET_H
  97. // Local Variables:
  98. // mode: c++
  99. // End: