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