header_flags.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. struct Flags {
  27. unsigned int rc : 4;
  28. unsigned int cd : 1;
  29. unsigned int ad : 1;
  30. unsigned int z : 1; // Reserved
  31. unsigned int ra : 1;
  32. unsigned int rd : 1;
  33. unsigned int tc : 1;
  34. unsigned int aa : 1;
  35. unsigned int op : 4;
  36. unsigned int qr : 1;
  37. };
  38. /// \brief Constructor
  39. HeaderFlags() {
  40. reset();
  41. }
  42. /// \brief Reset Values to Zero
  43. ///
  44. /// Clears all flags.
  45. void reset() {
  46. setValue(0);
  47. }
  48. /// \brief Set Header Flags as 16-Bit Value
  49. ///
  50. /// \param value 16-bit value to put into object as representing the
  51. /// header flags.
  52. void setValue(uint16_t value) {
  53. flags_.value = value;
  54. }
  55. /// \brief Get Header Flags as 16-bit Value
  56. uint16_t getValue() const {
  57. return flags_.value;
  58. }
  59. /// \brief Get QR Bit
  60. uint16_t getQR() const {
  61. return flags_.fields.qr;
  62. }
  63. /// \brief Set QR Bit
  64. ///
  65. /// \param value New value of the field, which must be 0 or 1: values
  66. /// outside that range are coerced to the nearest boundary.
  67. void setQR(uint16_t value) {
  68. flags_.fields.qr = (value > 1) ? 1 : value;
  69. }
  70. /// \brief Get OP Value
  71. uint16_t getOP() const {
  72. return flags_.fields.op;
  73. }
  74. /// \brief Set OP Value
  75. ///
  76. /// \param value New value of the field, which must in the range 0 to 15:
  77. /// values outside that range are coerced to the nearest boundary.
  78. void setOP(uint16_t value) {
  79. flags_.fields.op = (value > 15) ? 15 : value;
  80. }
  81. /// \brief Get AA Bit
  82. uint16_t getAA() const {
  83. return flags_.fields.aa;
  84. }
  85. /// \brief Set AA Bit
  86. ///
  87. /// \param value New value of the field, which must be 0 or 1: values
  88. /// outside that range are coerced to the nearest boundary.
  89. void setAA(uint16_t value) {
  90. flags_.fields.aa = (value > 1) ? 1 : value;
  91. }
  92. /// \brief Get TC Bit
  93. uint16_t getTC() const {
  94. return flags_.fields.tc;
  95. }
  96. /// \brief Set TC Bit
  97. ///
  98. /// \param value New value of the field, which must be 0 or 1: values
  99. /// outside that range are coerced to the nearest boundary.
  100. void setTC(uint16_t value) {
  101. flags_.fields.tc = (value > 1) ? 1 : value;
  102. }
  103. /// \brief Get RD Bit
  104. uint16_t getRD() const {
  105. return flags_.fields.rd;
  106. }
  107. /// \brief Set RD Bit
  108. ///
  109. /// \param value New value of the field, which must be 0 or 1: values
  110. /// outside that range are coerced to the nearest boundary.
  111. void setRD(uint16_t value) {
  112. flags_.fields.rd = (value > 1) ? 1 : value;
  113. }
  114. /// \brief Get RA Bit
  115. uint16_t getRA() const {
  116. return flags_.fields.ra;
  117. }
  118. /// \brief Set RA Bit
  119. ///
  120. /// \param value New value of the field, which must be 0 or 1: values
  121. /// outside that range are coerced to the nearest boundary.
  122. void setRA(uint16_t value) {
  123. flags_.fields.ra = (value > 1) ? 1 : value;
  124. }
  125. /// \brief Get Z Bit
  126. uint16_t getZ() const {
  127. return flags_.fields.z;
  128. }
  129. /// \brief Set Z Bit
  130. ///
  131. /// \param value New value of the field, which must be 0 or 1: values
  132. /// outside that range are coerced to the nearest boundary.
  133. void setZ(uint16_t value) {
  134. flags_.fields.z = (value > 1) ? 1 : value;
  135. }
  136. /// \brief Get AD Bit
  137. uint16_t getAD() const {
  138. return flags_.fields.ad;
  139. }
  140. /// \brief Set AD Bit
  141. ///
  142. /// \param value New value of the field, which must be 0 or 1: values
  143. /// outside that range are coerced to the nearest boundary.
  144. void setAD(uint16_t value) {
  145. flags_.fields.ad = (value > 1) ? 1 : value;
  146. }
  147. /// \brief Get CD Bit
  148. uint16_t getCD() const {
  149. return flags_.fields.cd;
  150. }
  151. /// \brief Set CD Bit
  152. ///
  153. /// \param value New value of the field, which must be 0 or 1: values
  154. /// outside that range are coerced to the nearest boundary.
  155. void setCD(uint16_t value) {
  156. flags_.fields.cd = (value > 1) ? 1 : value;
  157. }
  158. /// \brief Get RC Value
  159. uint16_t getRC() const {
  160. return flags_.fields.rc;
  161. }
  162. /// \brief Set RC Value
  163. ///
  164. /// \param value New value of the field, which must be in the range 0 to 15:
  165. /// values outside that range are coerced to the nearest boundary.
  166. void setRC(uint16_t value) {
  167. flags_.fields.rc = (value > 15) ? 15 : value;
  168. }
  169. private:
  170. // The variable that performs the conversion
  171. union {
  172. uint16_t value;
  173. Flags fields;
  174. } flags_;
  175. };
  176. } // namespace badpacket
  177. } // namespace isc
  178. #endif // __HEADER_FLAGS_H