header_flags.h 3.1 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. #ifndef HEADER_FLAGS_H
  15. #define HEADER_FLAGS_H
  16. #include <exceptions/exceptions.h>
  17. #include "option_info.h"
  18. namespace isc {
  19. namespace badpacket {
  20. /// \brief Header Flags
  21. ///
  22. /// Simple class providing easy conversion between the header flags in a DNS
  23. /// message and a 16-bit value.
  24. class HeaderFlags {
  25. public:
  26. /// \brief Constructor
  27. HeaderFlags() {
  28. reset();
  29. }
  30. /// \brief Reset values to zero
  31. ///
  32. /// Clears all flags.
  33. void reset() {
  34. setValue(0);
  35. }
  36. /// \brief Get header flags as 16-bit value
  37. uint16_t getValue() const {
  38. return (flags_);
  39. }
  40. /// \brief Set header flags as 16-bit value
  41. ///
  42. /// \param value 16-bit value to put into object as representing the
  43. /// header flags.
  44. void setValue(uint16_t value) {
  45. flags_ = value;
  46. }
  47. /// \brief Get field
  48. ///
  49. /// Return the value of a bit field in the flags word.
  50. ///
  51. /// \param int Index of the bit field in the OptionInfo data structure
  52. ///
  53. /// \return Value of the field.
  54. uint16_t get(int index) const {
  55. OptionInfo::checkIndex(index);
  56. return ((flags_ & OptionInfo::mask(index)) >> OptionInfo::offset(index));
  57. }
  58. /// \brief Set field
  59. ///
  60. /// Sets the value of a bit field.
  61. ///
  62. /// \param int Index of the bit field in the OptionInfo data structure
  63. /// \param value Value to set. If the value is more than the field can
  64. /// hold, a BadValue exception is thrown.
  65. void set(int index, uint16_t value) {
  66. // Declare an OptionInfo object for brevity and check the index is
  67. // valid.
  68. OptionInfo o;
  69. o.checkIndex(index);
  70. // Ensure the value is within limits and throw an exception if not. (This
  71. // should not really be needed, as the command line parsing should have
  72. // checked the limits. But be safe.)
  73. if ((value < o.minval(index)) || (value > o.maxval(index))) {
  74. isc_throw(isc::BadValue, "value of index " << index << " is out of range");
  75. }
  76. // Clear the field then set it with the value.
  77. flags_ &= ~o.mask(index);
  78. flags_ |= (value << o.offset(index));
  79. }
  80. private:
  81. uint16_t flags_; ///< Variable holding field values
  82. };
  83. } // namespace badpacket
  84. } // namespace isc
  85. #endif // HEADER_FLAGS_H