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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::{Buffer, DnsMessage, DnsMessageError};
use crate::name::DnsName;
use crate::parse::{Parse, ParseBytes};
use crate::write::WriteBytes;

/// A DNS message questions section.
pub struct DnsQuestions<
    const PTR_STORAGE: usize,
    B: Buffer,
> {
    message: DnsMessage<PTR_STORAGE, 0, B>,
    remaining: usize,
}

impl<
    const PTR_STORAGE: usize,
    B: Buffer,
> DnsQuestions<PTR_STORAGE, B> {
    #[inline(always)]
    pub(crate) fn new(message: DnsMessage<PTR_STORAGE, 0, B>) -> Self {
        let remaining = message.header().unwrap().question_count() as usize;
        Self {
            message,
            remaining,
        }
    }

    /// Append a question to the message. This will override the next
    /// question or further sections, if any.
    pub fn append(&mut self, question: DnsQuestion) -> Result<(), DnsMessageError> {
        let (buffer, position) = self.message.buffer_and_position();
        // Truncate the buffer to the current position.
        buffer.truncate(*position)?;
        question.write(&mut self.message)?;
        // Set question_count in the header to the current question count + 1.
        let question_count = self.message.header().unwrap().question_count();
        let question_count = question_count + 1 - self.remaining as u16;
        self.message.header_mut()?.set_question_count(question_count);
        self.message.header_mut()?.set_answer_count(0);
        self.message.header_mut()?.set_name_server_count(0);
        self.message.header_mut()?.set_additional_records_count(0);
        self.remaining = 0;

        Ok(())
    }

    /// Return an iterator over the question section.
    #[inline(always)]
    pub fn iter(&mut self) -> Result<DnsQuestionIterator, DnsMessageError> {
        let (buffer, position) = self.message.buffer_and_position();
        let buffer = buffer.bytes();

        Ok(DnsQuestionIterator {
            buffer,
            current_position: position,
            remaining: &mut self.remaining,
        })
    }

    /// Complete the message. This will check the remaining questions and
    /// return the message if successful.
    #[inline(always)]
    pub fn complete(mut self) -> Result<DnsMessage<PTR_STORAGE, 1, B>, DnsMessageError> {
        if self.remaining != 0 {
            for x in self.iter()? { x?; }
        }

        Ok(DnsMessage {
            buffer: self.message.buffer,
            position: self.message.position,
            ptr_storage: self.message.ptr_storage,
            ptr_len: self.message.ptr_len,
        })
    }
}

/// An iterator over the questions section of a DNS message.
pub struct DnsQuestionIterator<'a> {
    buffer: &'a [u8],
    current_position: &'a mut usize,
    remaining: &'a mut usize,
}

impl<'a> Iterator for DnsQuestionIterator<'a> {
    type Item = Result<DnsQuestion<'a>, DnsMessageError>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if *self.remaining == 0 {
            return None;
        }

        let question = DnsQuestion::parse(
            self.buffer, self.current_position
        );
        *self.remaining -= 1;

        Some(question)
    }
}

/// A DNS message question.
#[derive(Debug, PartialEq)]
pub struct DnsQuestion<'a> {
    /// The domain name being queried.
    pub name: DnsName<'a>,
    /// The type of the query.
    pub qtype: DnsQType,
    /// The class of the query.
    pub qclass: DnsQClass,
}

impl<'a> ParseBytes<'a> for DnsQuestion<'a> {
    fn parse_bytes(bytes: &'a [u8], i: &mut usize) -> Result<Self, DnsMessageError> {
        let name = DnsName::parse(bytes, i)?;
        let qtype = u16::parse(bytes, i)?.into();
        let qclass = u16::parse(bytes, i)?.into();

        Ok(Self {
            name,
            qtype,
            qclass,
        })
    }
}

impl<'a> WriteBytes for DnsQuestion<'a> {
    fn write<
        const PTR_STORAGE: usize,
        const DNS_SECTION: usize,
        B: Buffer,
    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
        let mut bytes = 0;

        bytes += self.name.write(message)?;
        bytes += self.qtype.id().write(message)?;
        bytes += self.qclass.id().write(message)?;

        Ok(bytes)
    }
}

/// The kind of a DNS query.
///
/// According to [RFC 1035 Section 3.2.2](https://tools.ietf.org/rfc/rfc1035#section-3.2.2)
/// and [RFC 1035 Section 3.2.3](https://tools.ietf.org/rfc/rfc1035#section-3.2.3).
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u16)]
pub enum DnsQType {
    A = 1,
    NS = 2,
    CNAME = 5,
    SOA = 6,
    PTR = 12,
    HINFO = 13,
    MX = 15,
    TXT = 16,
    RP = 17,
    AFSDB = 18,
    SIG = 24,
    KEY = 25,
    AAAA = 28,
    LOC = 29,
    SRV = 33,
    NAPTR = 35,
    KX = 36,
    CERT = 37,
    DNAME = 39,
    OPT = 41,
    APL = 42,
    DS = 43,
    SSHFP = 44,
    IPSECKEY = 45,
    RRSIG = 46,
    NSEC = 47,
    DNSKEY = 48,
    DHCID = 49,
    NSEC3 = 50,
    NSEC3PARAM = 51,
    TLSA = 52,
    SMIMEA = 53,
    HIP = 55,
    CDS = 59,
    CDNSKEY = 60,
    OPENPGPKEY = 61,
    CSYNC = 62,
    ZONEMD = 63,
    SVCB = 64,
    HTTPS = 65,
    EUI48 = 108,
    EUI64 = 109,
    TKEY = 249,
    TSIG = 250,
    IXFR = 251,
    AXFR = 252,
    ALL = 255,
    URI = 256,
    CAA = 257,
    TA = 32768,
    DLV = 32769,
    Reserved,
}

impl DnsQType {
    /// Create a new QType from an ID.
    #[inline(always)]
    pub fn from_id(id: u16) -> Self {
        match id {
            1..=2 | 5..=6 | 12..=13 | 15..=18 | 24..=25 | 28..=29
            | 33 | 35..=37 | 39 | 41..=53 | 55 | 59..=65
            | 108..=109 | 249..=252 | 255 | 256 | 257
            | 32768..=32769 => unsafe {
                core::mem::transmute(id)
            },
            _ => DnsQType::Reserved,
        }
    }

    /// Get the ID of the QType.
    #[inline(always)]
    pub fn id(&self) -> u16 {
        match self {
            DnsQType::Reserved => panic!("Reserved QType"),
            _ => *self as u16,
        }
    }
}

impl From<DnsQType> for u16 {
    #[inline(always)]
    fn from(q: DnsQType) -> Self {
        DnsQType::id(&q)
    }
}

impl From<u16> for DnsQType {
    #[inline(always)]
    fn from(n: u16) -> Self {
        DnsQType::from_id(n)
    }
}

/// The class of a DNS query.
///
/// According to [RFC 1035 Section 3.2.4](https://tools.ietf.org/rfc/rfc1035#section-3.2.4).
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u16)]
pub enum DnsQClass {
    /// Internet
    IN = 1,
    /// CSNET
    CS = 2,
    /// CHAOS
    CH = 3,
    /// Hesiod
    HS = 4,
    /// Any
    ANY = 255,
    Reserved,
}

impl DnsQClass {
    /// Create a new QClass from an ID.
    #[inline(always)]
    pub fn from_id(id: u16) -> Self {
        match id {
            1..=4 | 255 => unsafe { core::mem::transmute(id) },
            _ => DnsQClass::Reserved,
        }
    }

    /// Get the ID of the QClass.
    #[inline(always)]
    pub fn id(&self) -> u16 {
        match self {
            DnsQClass::Reserved => panic!("Reserved QClass"),
            _ => *self as u16,
        }
    }
}

impl From<DnsQClass> for u16 {
    #[inline(always)]
    fn from(q: DnsQClass) -> Self {
        DnsQClass::id(&q)
    }
}

impl From<u16> for DnsQClass {
    #[inline(always)]
    fn from(n: u16) -> Self {
        DnsQClass::from_id(n)
    }
}