header_flags.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. namespace isc {
  17. namespace badpacket {
  18. /// \brief Header Flags
  19. ///
  20. /// Simple class providing easy conversion between the header flags in a DNS
  21. /// message and a 16-bit value.
  22. class HeaderFlags {
  23. public:
  24. // The following declaration describes the various fields in the DNS
  25. // packet header.
  26. enum FieldParameter {
  27. QR_MASK = 0x8000, // Maskd efining the field
  28. QR_OFF = 15, // Offset of field in the flags word (i.e. shift
  29. OP_MASK = 0x7800, // ... this number of bits to the left)
  30. OP_OFF = 11,
  31. AA_MASK = 0x0400,
  32. AA_OFF = 10,
  33. TC_MASK = 0x0200,
  34. TC_OFF = 9,
  35. RD_MASK = 0x0100,
  36. RD_OFF = 8,
  37. RA_MASK = 0x0080,
  38. RA_OFF = 7,
  39. Z_MASK = 0x0040,
  40. Z_OFF = 6,
  41. AD_MASK = 0x0020,
  42. AD_OFF = 5,
  43. CD_MASK = 0x0010,
  44. CD_OFF = 4,
  45. RC_MASK = 0x000F,
  46. RC_OFF = 0
  47. };
  48. /// \brief Constructor
  49. HeaderFlags() {
  50. reset();
  51. }
  52. /// \brief Reset Values to Zero
  53. ///
  54. /// Clears all flags.
  55. void reset() {
  56. setValue(0);
  57. }
  58. /// \brief Get Header Flags as 16-bit Value
  59. uint16_t getValue() const {
  60. return (flags_);
  61. }
  62. /// \brief Set Header Flags as 16-Bit Value
  63. ///
  64. /// \param value 16-bit value to put into object as representing the
  65. /// header flags.
  66. void setValue(uint16_t value) {
  67. flags_ = value;
  68. }
  69. /// \brief Get QR Bit
  70. uint16_t getQR() const {
  71. return (getField(QR_MASK, QR_OFF));
  72. }
  73. /// \brief Set QR Bit
  74. ///
  75. /// \param value New value of the field, which must be 0 or 1: values
  76. /// outside that range are coerced to the nearest boundary.
  77. void setQR(uint16_t value) {
  78. setField(value, QR_MASK, QR_OFF);
  79. }
  80. /// \brief Get OP Value
  81. uint16_t getOP() const {
  82. return (getField(OP_MASK, OP_OFF));
  83. }
  84. /// \brief Set OP Value
  85. ///
  86. /// \param value New value of the field, which must in the range 0 to 15:
  87. /// values outside that range are coerced to the nearest boundary.
  88. void setOP(uint16_t value) {
  89. setField(value, OP_MASK, OP_OFF);
  90. }
  91. /// \brief Get AA Bit
  92. uint16_t getAA() const {
  93. return (getField(AA_MASK, AA_OFF));
  94. }
  95. /// \brief Set AA Bit
  96. ///
  97. /// \param value New value of the field, which must be 0 or 1: values
  98. /// outside that range are coerced to the nearest boundary.
  99. void setAA(uint16_t value) {
  100. setField(value, AA_MASK, AA_OFF);
  101. }
  102. /// \brief Get TC Bit
  103. uint16_t getTC() const {
  104. return (getField(TC_MASK, TC_OFF));
  105. }
  106. /// \brief Set TC Bit
  107. ///
  108. /// \param value New value of the field, which must be 0 or 1: values
  109. /// outside that range are coerced to the nearest boundary.
  110. void setTC(uint16_t value) {
  111. setField(value, TC_MASK, TC_OFF);
  112. }
  113. /// \brief Get RD Bit
  114. uint16_t getRD() const {
  115. return (getField(RD_MASK, RD_OFF));
  116. }
  117. /// \brief Set RD Bit
  118. ///
  119. /// \param value New value of the field, which must be 0 or 1: values
  120. /// outside that range are coerced to the nearest boundary.
  121. void setRD(uint16_t value) {
  122. setField(value, RD_MASK, RD_OFF);
  123. }
  124. /// \brief Get RA Bit
  125. uint16_t getRA() const {
  126. return (getField(RA_MASK, RA_OFF));
  127. }
  128. /// \brief Set RA Bit
  129. ///
  130. /// \param value New value of the field, which must be 0 or 1: values
  131. /// outside that range are coerced to the nearest boundary.
  132. void setRA(uint16_t value) {
  133. setField(value, RA_MASK, RA_OFF);
  134. }
  135. /// \brief Get Z Bit
  136. uint16_t getZ() const {
  137. return (getField(Z_MASK, Z_OFF));
  138. }
  139. /// \brief Set Z Bit
  140. ///
  141. /// \param value New value of the field, which must be 0 or 1: values
  142. /// outside that range are coerced to the nearest boundary.
  143. void setZ(uint16_t value) {
  144. setField(value, Z_MASK, Z_OFF);
  145. }
  146. /// \brief Get AD Bit
  147. uint16_t getAD() const {
  148. return (getField(AD_MASK, AD_OFF));
  149. }
  150. /// \brief Set AD Bit
  151. ///
  152. /// \param value New value of the field, which must be 0 or 1: values
  153. /// outside that range are coerced to the nearest boundary.
  154. void setAD(uint16_t value) {
  155. setField(value, AD_MASK, AD_OFF);
  156. }
  157. /// \brief Get CD Bit
  158. uint16_t getCD() const {
  159. return (getField(CD_MASK, CD_OFF));
  160. }
  161. /// \brief Set CD Bit
  162. ///
  163. /// \param value New value of the field, which must be 0 or 1: values
  164. /// outside that range are coerced to the nearest boundary.
  165. void setCD(uint16_t value) {
  166. setField(value, CD_MASK, CD_OFF);
  167. }
  168. /// \brief Get RC Value
  169. uint16_t getRC() const {
  170. return (getField(RC_MASK, RC_OFF));
  171. }
  172. /// \brief Set RC Value
  173. ///
  174. /// \param value New value of the field, which must be in the range 0 to 15:
  175. /// values outside that range are coerced to the nearest boundary.
  176. void setRC(uint16_t value) {
  177. setField(value, RC_MASK, RC_OFF);
  178. }
  179. private:
  180. /// \brief Get Field
  181. ///
  182. /// Return the value of a bit field in the flags word.
  183. ///
  184. /// \param mask Bit mask identifying the field.
  185. /// \param offset Offset of the field in the flags word.
  186. ///
  187. /// \return Value of the field.
  188. uint16_t getField(FieldParameter mask, FieldParameter offset) const {
  189. return ((flags_ & mask) >> offset);
  190. }
  191. /// \brief Set Field
  192. ///
  193. /// Sets the value of a bit field.
  194. ///
  195. /// \param value Value to set. If the value is more than the field can
  196. /// hold, it is set to the maximum.
  197. /// \param mask Bit mask identifying the field.
  198. /// \param offset Offset of the field in the flags word.
  199. void setField(uint16_t value, FieldParameter mask, FieldParameter offset) {
  200. // Ensure the value is within limits.
  201. uint16_t maxval = mask >> offset;
  202. uint16_t val = (value > maxval) ? maxval : value;
  203. // Clear the field then set it with the value.
  204. flags_ &= ~mask;
  205. flags_ |= (val << offset);
  206. }
  207. uint16_t flags_; ///< Variable holding field values
  208. };
  209. } // namespace badpacket
  210. } // namespace isc
  211. #endif // __HEADER_FLAGS_H