PacketHeader.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Endian.h>
  8. #include <AK/Types.h>
  9. namespace DNS {
  10. class [[gnu::packed]] PacketHeader {
  11. public:
  12. PacketHeader()
  13. : m_recursion_desired(false)
  14. , m_truncated(false)
  15. , m_authoritative_answer(false)
  16. , m_opcode(0)
  17. , m_query_or_response(false)
  18. , m_response_code(0)
  19. , m_checking_disabled(false)
  20. , m_authenticated_data(false)
  21. , m_zero(false)
  22. , m_recursion_available(false)
  23. {
  24. }
  25. u16 id() const { return m_id; }
  26. void set_id(u16 w) { m_id = w; }
  27. bool recursion_desired() const { return m_recursion_desired; }
  28. void set_recursion_desired(bool b) { m_recursion_desired = b; }
  29. bool is_truncated() const { return m_truncated; }
  30. void set_truncated(bool b) { m_truncated = b; }
  31. bool is_authoritative_answer() const { return m_authoritative_answer; }
  32. void set_authoritative_answer(bool b) { m_authoritative_answer = b; }
  33. u8 opcode() const { return m_opcode; }
  34. void set_opcode(u8 b) { m_opcode = b; }
  35. bool is_query() const { return !m_query_or_response; }
  36. bool is_response() const { return m_query_or_response; }
  37. void set_is_query() { m_query_or_response = false; }
  38. void set_is_response() { m_query_or_response = true; }
  39. u8 response_code() const { return m_response_code; }
  40. void set_response_code(u8 b) { m_response_code = b; }
  41. bool checking_disabled() const { return m_checking_disabled; }
  42. void set_checking_disabled(bool b) { m_checking_disabled = b; }
  43. bool is_authenticated_data() const { return m_authenticated_data; }
  44. void set_authenticated_data(bool b) { m_authenticated_data = b; }
  45. bool is_recursion_available() const { return m_recursion_available; }
  46. void set_recursion_available(bool b) { m_recursion_available = b; }
  47. u16 question_count() const { return m_question_count; }
  48. void set_question_count(u16 w) { m_question_count = w; }
  49. u16 answer_count() const { return m_answer_count; }
  50. void set_answer_count(u16 w) { m_answer_count = w; }
  51. u16 authority_count() const { return m_authority_count; }
  52. void set_authority_count(u16 w) { m_authority_count = w; }
  53. u16 additional_count() const { return m_additional_count; }
  54. void set_additional_count(u16 w) { m_additional_count = w; }
  55. void* payload() { return this + 1; }
  56. void const* payload() const { return this + 1; }
  57. private:
  58. NetworkOrdered<u16> m_id;
  59. bool m_recursion_desired : 1;
  60. bool m_truncated : 1;
  61. bool m_authoritative_answer : 1;
  62. u8 m_opcode : 4;
  63. bool m_query_or_response : 1;
  64. u8 m_response_code : 4;
  65. bool m_checking_disabled : 1;
  66. bool m_authenticated_data : 1;
  67. bool m_zero : 1;
  68. bool m_recursion_available : 1;
  69. NetworkOrdered<u16> m_question_count;
  70. NetworkOrdered<u16> m_answer_count;
  71. NetworkOrdered<u16> m_authority_count;
  72. NetworkOrdered<u16> m_additional_count;
  73. };
  74. static_assert(sizeof(PacketHeader) == 12);
  75. }
  76. template<>
  77. struct AK::Traits<DNS::PacketHeader> : public AK::DefaultTraits<DNS::PacketHeader> {
  78. static constexpr bool is_trivially_serializable() { return true; }
  79. };