/src/serenity/Userland/Libraries/LibDNS/Packet.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include "Answer.h" |
11 | | #include "Question.h" |
12 | | #include <AK/Optional.h> |
13 | | #include <AK/Types.h> |
14 | | #include <AK/Vector.h> |
15 | | |
16 | | namespace DNS { |
17 | | |
18 | | enum class ShouldRandomizeCase { |
19 | | No = 0, |
20 | | Yes |
21 | | }; |
22 | | |
23 | | class Packet { |
24 | | public: |
25 | 1.36k | Packet() = default; |
26 | | |
27 | | static ErrorOr<Packet> from_raw_packet(ReadonlyBytes bytes); |
28 | | ErrorOr<ByteBuffer> to_byte_buffer() const; |
29 | | |
30 | 453 | bool is_query() const { return !m_query_or_response; } |
31 | 0 | bool is_response() const { return m_query_or_response; } |
32 | 0 | bool is_authoritative_answer() const { return m_authoritative_answer; } |
33 | 0 | bool recursion_desired() const { return m_recursion_desired; } |
34 | 0 | bool recursion_available() const { return m_recursion_available; } |
35 | 0 | void set_is_query() { m_query_or_response = false; } |
36 | 0 | void set_is_response() { m_query_or_response = true; } |
37 | 0 | void set_authoritative_answer(bool authoritative_answer) { m_authoritative_answer = authoritative_answer; } |
38 | 0 | void set_recursion_desired(bool recursion_desired) { m_recursion_desired = recursion_desired; } |
39 | 0 | void set_recursion_available(bool recursion_available) { m_recursion_available = recursion_available; } |
40 | | |
41 | 0 | u16 id() const { return m_id; } |
42 | 0 | void set_id(u16 id) { m_id = id; } |
43 | | |
44 | 0 | Vector<Question> const& questions() const { return m_questions; } |
45 | 0 | Vector<Answer> const& answers() const { return m_answers; } |
46 | | |
47 | | u16 question_count() const |
48 | 0 | { |
49 | 0 | VERIFY(m_questions.size() <= UINT16_MAX); |
50 | 0 | return m_questions.size(); |
51 | 0 | } |
52 | | |
53 | | u16 answer_count() const |
54 | 0 | { |
55 | 0 | VERIFY(m_answers.size() <= UINT16_MAX); |
56 | 0 | return m_answers.size(); |
57 | 0 | } |
58 | | |
59 | | void add_question(Question const&); |
60 | | void add_answer(Answer const&); |
61 | | |
62 | | enum class Code : u8 { |
63 | | NOERROR = 0, |
64 | | FORMERR = 1, |
65 | | SERVFAIL = 2, |
66 | | NXDOMAIN = 3, |
67 | | NOTIMP = 4, |
68 | | REFUSED = 5, |
69 | | YXDOMAIN = 6, |
70 | | XRRSET = 7, |
71 | | NOTAUTH = 8, |
72 | | NOTZONE = 9, |
73 | | }; |
74 | | |
75 | 1.36k | Code code() const { return (Code)m_code; } |
76 | 0 | void set_code(Code code) { m_code = (u8)code; } |
77 | | |
78 | | private: |
79 | | u16 m_id { 0 }; |
80 | | u8 m_code { 0 }; |
81 | | bool m_authoritative_answer { false }; |
82 | | bool m_query_or_response { false }; |
83 | | bool m_recursion_desired { true }; |
84 | | bool m_recursion_available { true }; |
85 | | Vector<Question> m_questions; |
86 | | Vector<Answer> m_answers; |
87 | | }; |
88 | | |
89 | | } |