serial.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef __SERIAL_H
  15. #define __SERIAL_H 1
  16. #include <stdint.h>
  17. #include <iostream>
  18. namespace isc {
  19. namespace dns {
  20. /// The maximum difference between two serial numbers. If the (plain uint32_t)
  21. /// difference between two serials is greater than this number, the smaller one
  22. /// is considered greater.
  23. const uint32_t MAX_SERIAL_INCREMENT = 2147483647;
  24. /// Maximum value a serial can have, used in + operator.
  25. const uint64_t MAX_SERIAL_VALUE = 4294967296ull;
  26. /// \brief This class defines DNS serial numbers and serial arithmetic.
  27. ///
  28. /// DNS Serial number are in essence unsigned 32-bits numbers, with one
  29. /// catch; they should be compared using sequence space arithmetic.
  30. /// So given that they are 32-bits; as soon as the difference between two
  31. /// serial numbers is greater than 2147483647 (2^31 - 1), the lower number
  32. /// (in plain comparison) is considered the higher one.
  33. ///
  34. /// In order to do this as transparently as possible, these numbers are
  35. /// stored in the Serial class, which overrides the basic comparison operators.
  36. ///
  37. /// In this specific context, these operations are called 'serial number
  38. /// arithmetic', and they are defined in RFC 1982.
  39. ///
  40. /// \note RFC 1982 defines everything based on the value SERIAL_BITS. Since
  41. /// the serial number has a fixed length of 32 bits, the values we use are
  42. /// hard-coded, and not computed based on variable bit lengths.
  43. class Serial {
  44. public:
  45. /// \brief Constructor with value
  46. ///
  47. /// \param value The uint32_t value of the serial
  48. explicit Serial(uint32_t value) : value_(value) {}
  49. /// \brief Copy constructor
  50. Serial(const Serial& other) : value_(other.getValue()) {}
  51. /// \brief Direct assignment from other Serial
  52. ///
  53. /// \param other The Serial to assign the value from
  54. void operator=(const Serial& other) { value_ = other.getValue(); }
  55. /// \brief Direct assignment from value
  56. ///
  57. /// \param value the uint32_t value to assing
  58. void operator=(uint32_t value) { value_ = value; }
  59. /// \brief Returns the uint32_t representation of this serial value
  60. ///
  61. /// \return The uint32_t value of this Serial
  62. uint32_t getValue() const { return (value_); }
  63. /// \brief Returns true if the serial values are equal
  64. ///
  65. /// \return True if the values are equal
  66. bool operator==(const Serial& other) const;
  67. /// \brief Returns true if the serial values are not equal
  68. ///
  69. /// \return True if the values are not equal
  70. bool operator!=(const Serial& other) const;
  71. /// \brief Returns true if the serial value of this serial is smaller than
  72. /// the other, according to serial arithmetic as described in RFC 1982
  73. ///
  74. /// \param other The Serial to compare to
  75. ///
  76. /// \return True if this is smaller than the given value
  77. bool operator<(const Serial& other) const;
  78. /// \brief Returns true if the serial value of this serial is equal to or
  79. /// smaller than the other, according to serial arithmetic as described
  80. /// in RFC 1982
  81. ///
  82. /// \param other The Serial to compare to
  83. ///
  84. /// \return True if this is smaller than or equal to the given value
  85. bool operator<=(const Serial& other) const;
  86. /// \brief Returns true if the serial value of this serial is greater than
  87. /// the other, according to serial arithmetic as described in RFC 1982
  88. ///
  89. /// \param other The Serial to compare to
  90. ///
  91. /// \return True if this is greater than the given value
  92. bool operator>(const Serial& other) const;
  93. /// \brief Returns true if the serial value of this serial is equal to or
  94. /// greater than the other, according to serial arithmetic as described in
  95. /// RFC 1982
  96. ///
  97. /// \param other The Serial to compare to
  98. ///
  99. /// \return True if this is greater than or equal to the given value
  100. bool operator>=(const Serial& other) const;
  101. /// \brief Adds the given value to the serial number. If this would make
  102. /// the number greater than 2^32-1, it is 'wrapped'.
  103. /// \note According to the specification, an addition greater than
  104. /// MAX_SERIAL_INCREMENT is undefined. We do NOT catch this error (so as not
  105. /// to raise exceptions), but this behaviour remains undefined.
  106. ///
  107. /// \param other The Serial to add
  108. ///
  109. /// \return The result of the addition
  110. Serial operator+(const Serial& other) const;
  111. /// \brief Adds the given value to the serial number. If this would make
  112. /// the number greater than 2^32-1, it is 'wrapped'.
  113. ///
  114. /// \note According to the specification, an addition greater than
  115. /// MAX_SERIAL_INCREMENT is undefined. We do NOT catch this error (so as not
  116. /// to raise exceptions), but this behaviour remains undefined.
  117. ///
  118. /// \param other_val The uint32_t value to add
  119. ///
  120. /// \return The result of the addition
  121. Serial operator+(uint32_t other_val) const;
  122. private:
  123. uint32_t value_;
  124. };
  125. /// \brief Helper operator for output streams, writes the value to the stream
  126. ///
  127. /// \param os The ostream to write to
  128. /// \param serial The Serial to write
  129. /// \return the output stream
  130. std::ostream& operator<<(std::ostream& os, const Serial& serial);
  131. } // end namespace dns
  132. } // end namespace isc
  133. #endif // __SERIAL_H