Answer.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Name.h"
  8. #include <AK/ByteString.h>
  9. #include <AK/Format.h>
  10. #include <AK/Traits.h>
  11. #include <AK/Types.h>
  12. #include <LibIPC/Forward.h>
  13. namespace DNS {
  14. enum class RecordType : u16 {
  15. A = 1,
  16. NS = 2,
  17. CNAME = 5,
  18. SOA = 6,
  19. PTR = 12,
  20. MX = 15,
  21. TXT = 16,
  22. AAAA = 28,
  23. SRV = 33,
  24. };
  25. enum class RecordClass : u16 {
  26. IN = 1
  27. };
  28. #define MDNS_CACHE_FLUSH 0x8000
  29. class Answer {
  30. public:
  31. Answer() = default;
  32. Answer(Name const& name, RecordType type, RecordClass class_code, u32 ttl, ByteString const& record_data, bool mdns_cache_flush);
  33. Name const& name() const { return m_name; }
  34. RecordType type() const { return m_type; }
  35. RecordClass class_code() const { return m_class_code; }
  36. u16 raw_class_code() const { return (u16)m_class_code | (m_mdns_cache_flush ? MDNS_CACHE_FLUSH : 0); }
  37. u32 ttl() const { return m_ttl; }
  38. time_t received_time() const { return m_received_time; }
  39. ByteString const& record_data() const { return m_record_data; }
  40. bool mdns_cache_flush() const { return m_mdns_cache_flush; }
  41. bool has_expired() const;
  42. unsigned hash() const;
  43. bool operator==(Answer const&) const;
  44. private:
  45. Name m_name;
  46. RecordType m_type { 0 };
  47. RecordClass m_class_code { 0 };
  48. u32 m_ttl { 0 };
  49. time_t m_received_time { 0 };
  50. ByteString m_record_data;
  51. bool m_mdns_cache_flush { false };
  52. };
  53. }
  54. template<>
  55. struct AK::Traits<DNS::Answer> : public DefaultTraits<DNS::Answer> {
  56. static constexpr bool is_trivial() { return false; }
  57. static unsigned hash(DNS::Answer a) { return a.hash(); }
  58. };
  59. template<>
  60. struct AK::Formatter<DNS::RecordType> : StandardFormatter {
  61. Formatter() = default;
  62. explicit Formatter(StandardFormatter formatter)
  63. : StandardFormatter(formatter)
  64. {
  65. }
  66. ErrorOr<void> format(AK::FormatBuilder&, DNS::RecordType);
  67. };
  68. template<>
  69. struct AK::Formatter<DNS::RecordClass> : StandardFormatter {
  70. Formatter() = default;
  71. explicit Formatter(StandardFormatter formatter)
  72. : StandardFormatter(formatter)
  73. {
  74. }
  75. ErrorOr<void> format(AK::FormatBuilder&, DNS::RecordClass);
  76. };
  77. namespace IPC {
  78. template<>
  79. ErrorOr<void> encode(Encoder&, DNS::Answer const&);
  80. template<>
  81. ErrorOr<DNS::Answer> decode(Decoder&);
  82. }