1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use super::*;

// Header is a representation of a DNS message header.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct Header {
    pub id: u16,
    pub response: bool,
    pub op_code: OpCode,
    pub authoritative: bool,
    pub truncated: bool,
    pub recursion_desired: bool,
    pub recursion_available: bool,
    pub rcode: RCode,
}

impl fmt::Display for Header {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "dnsmessage.Header{{id: {}, response: {}, op_code: {}, authoritative: {}, truncated: {}, recursion_desired: {}, recursion_available: {}, rcode: {} }}",
            self.id,
            self.response,
            self.op_code,
            self.authoritative,
            self.truncated,
            self.recursion_desired,
            self.recursion_available,
            self.rcode
        )
    }
}

impl Header {
    pub fn pack(&self) -> (u16, u16) {
        let id = self.id;
        let mut bits = self.op_code << 11 | self.rcode as u16;
        if self.recursion_available {
            bits |= HEADER_BIT_RA
        }
        if self.recursion_desired {
            bits |= HEADER_BIT_RD
        }
        if self.truncated {
            bits |= HEADER_BIT_TC
        }
        if self.authoritative {
            bits |= HEADER_BIT_AA
        }
        if self.response {
            bits |= HEADER_BIT_QR
        }

        (id, bits)
    }
}

#[derive(Default, Copy, Clone, PartialOrd, PartialEq, Eq)]
pub enum Section {
    #[default]
    NotStarted = 0,
    Header = 1,
    Questions = 2,
    Answers = 3,
    Authorities = 4,
    Additionals = 5,
    Done = 6,
}

impl From<u8> for Section {
    fn from(v: u8) -> Self {
        match v {
            0 => Section::NotStarted,
            1 => Section::Header,
            2 => Section::Questions,
            3 => Section::Answers,
            4 => Section::Authorities,
            5 => Section::Additionals,
            _ => Section::Done,
        }
    }
}

impl fmt::Display for Section {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match *self {
            Section::NotStarted => "NotStarted",
            Section::Header => "Header",
            Section::Questions => "question",
            Section::Answers => "answer",
            Section::Authorities => "authority",
            Section::Additionals => "additional",
            Section::Done => "Done",
        };
        write!(f, "{s}")
    }
}

// header is the wire format for a DNS message header.
#[derive(Default)]
pub struct HeaderInternal {
    pub id: u16,
    pub bits: u16,
    pub questions: u16,
    pub answers: u16,
    pub authorities: u16,
    pub additionals: u16,
}

impl HeaderInternal {
    pub(crate) fn count(&self, sec: Section) -> u16 {
        match sec {
            Section::Questions => self.questions,
            Section::Answers => self.answers,
            Section::Authorities => self.authorities,
            Section::Additionals => self.additionals,
            _ => 0,
        }
    }

    // pack appends the wire format of the header to msg.
    pub(crate) fn pack(&self, mut msg: Vec<u8>) -> Vec<u8> {
        msg = pack_uint16(msg, self.id);
        msg = pack_uint16(msg, self.bits);
        msg = pack_uint16(msg, self.questions);
        msg = pack_uint16(msg, self.answers);
        msg = pack_uint16(msg, self.authorities);
        msg = pack_uint16(msg, self.additionals);
        msg
    }

    pub(crate) fn unpack(&mut self, msg: &[u8], off: usize) -> Result<usize> {
        let (id, off) = unpack_uint16(msg, off)?;
        self.id = id;

        let (bits, off) = unpack_uint16(msg, off)?;
        self.bits = bits;

        let (questions, off) = unpack_uint16(msg, off)?;
        self.questions = questions;

        let (answers, off) = unpack_uint16(msg, off)?;
        self.answers = answers;

        let (authorities, off) = unpack_uint16(msg, off)?;
        self.authorities = authorities;

        let (additionals, off) = unpack_uint16(msg, off)?;
        self.additionals = additionals;

        Ok(off)
    }

    pub(crate) fn header(&self) -> Header {
        Header {
            id: self.id,
            response: (self.bits & HEADER_BIT_QR) != 0,
            op_code: ((self.bits >> 11) & 0xF) as OpCode,
            authoritative: (self.bits & HEADER_BIT_AA) != 0,
            truncated: (self.bits & HEADER_BIT_TC) != 0,
            recursion_desired: (self.bits & HEADER_BIT_RD) != 0,
            recursion_available: (self.bits & HEADER_BIT_RA) != 0,
            rcode: RCode::from((self.bits & 0xF) as u8),
        }
    }
}