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
#![allow(clippy::upper_case_acronyms)]

use nom::number::streaming::be_u16;

use sawp::error::Result;

use sawp_flags::{BitFlags, Flag, Flags};

use crate::enums::{OpCode, QueryResponse, ResponseCode};

use crate::ErrorFlags;
#[cfg(feature = "ffi")]
use sawp_ffi::GenerateFFI;

/// Masks for extracting DNS header flags
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, BitFlags)]
#[repr(u16)]
pub enum header_masks {
    QUERY_RESPONSE = 0b1000_0000_0000_0000,
    OPCODE = 0b0111_1000_0000_0000,
    AUTH = 0b0000_0100_0000_0000,
    TRUNC = 0b0000_0010_0000_0000,
    RECUR_DESIRED = 0b0000_0001_0000_0000,
    RECUR_AVAIL = 0b0000_0000_1000_0000,
    Z = 0b0000_0000_0100_0000,
    AUTH_DATA = 0b0000_0000_0010_0000,
    CHECK_DISABLED = 0b0000_0000_0001_0000,
    RCODE = 0b0000_0000_0000_1111,
}

/// A parsed DNS header
#[cfg_attr(feature = "ffi", derive(GenerateFFI))]
#[cfg_attr(feature = "ffi", sawp_ffi(prefix = "sawp_dns"))]
#[derive(Debug, PartialEq)]
pub struct Header {
    /// Transaction ID
    pub transaction_id: u16,
    /// Raw header flags
    pub flags: u16,
    #[cfg_attr(feature = "ffi", sawp_ffi(copy))]
    /// QueryResponse::Query or QueryResponse::Response
    pub query_response: QueryResponse,
    #[cfg_attr(feature = "ffi", sawp_ffi(copy))]
    /// Type of query
    pub opcode: OpCode,
    /// Is the name server an authority for this domain name?
    pub authoritative: bool,
    /// Was this msg truncated?
    pub truncated: bool,
    /// Should the name server pursue the query recursively?
    pub recursion_desired: bool,
    /// Can the name server pursue the query recursively?
    pub recursion_available: bool,
    /// Z flag is set?
    pub zflag: bool,

    /// All data authenticated by the server
    pub authenticated_data: bool,

    pub check_disabled: bool,
    #[cfg_attr(feature = "ffi", sawp_ffi(copy))]
    /// Name server success/error state
    pub rcode: ResponseCode,
    /// Number of questions provided
    pub qdcount: u16,
    /// Number of answers provided
    pub ancount: u16,
    /// Number of name server resource records in the auth records
    pub nscount: u16,
    /// Number of resource records in the additional records section
    pub arcount: u16,
}

impl Header {
    #[allow(clippy::type_complexity)]
    pub fn parse(input: &[u8]) -> Result<(&[u8], (Header, Flags<ErrorFlags>))> {
        let mut error_flags = ErrorFlags::none();

        let (input, txid) = be_u16(input)?;
        let (input, flags) = be_u16(input)?;
        let wrapped_flags = Flags::<header_masks>::from_bits(flags);
        let query = if wrapped_flags.intersects(header_masks::QUERY_RESPONSE) {
            QueryResponse::Response
        } else {
            QueryResponse::Query
        };
        let opcode: OpCode = OpCode::from_raw((wrapped_flags & header_masks::OPCODE).bits() >> 10);
        if opcode == OpCode::UNKNOWN {
            error_flags |= ErrorFlags::UnknownOpcode;
        }
        let rcode: ResponseCode =
            ResponseCode::from_raw((wrapped_flags & header_masks::RCODE).bits());
        if rcode == ResponseCode::UNKNOWN {
            error_flags |= ErrorFlags::UnknownRcode;
        }
        let (input, qcnt) = be_u16(input)?;
        let (input, acnt) = be_u16(input)?;
        let (input, nscnt) = be_u16(input)?;
        let (input, arcnt) = be_u16(input)?;

        Ok((
            input,
            (
                Header {
                    transaction_id: txid,
                    flags,
                    query_response: query,
                    opcode,
                    authoritative: wrapped_flags.intersects(header_masks::AUTH),
                    truncated: wrapped_flags.intersects(header_masks::TRUNC),
                    recursion_desired: wrapped_flags.intersects(header_masks::RECUR_DESIRED),
                    recursion_available: wrapped_flags.intersects(header_masks::RECUR_AVAIL),
                    zflag: wrapped_flags.intersects(header_masks::Z),
                    authenticated_data: wrapped_flags.intersects(header_masks::AUTH_DATA),
                    check_disabled: wrapped_flags.intersects(header_masks::CHECK_DISABLED),
                    rcode,
                    qdcount: qcnt,
                    ancount: acnt,
                    nscount: nscnt,
                    arcount: arcnt,
                },
                error_flags,
            ),
        ))
    }
}

#[cfg(test)]
mod test {
    #![allow(clippy::type_complexity)]

    use crate::{ErrorFlags, Header, OpCode, QueryResponse, ResponseCode};
    use rstest::rstest;
    use sawp::error::{Error, Result};
    use sawp_flags::{Flag, Flags};

    #[rstest(
        input,
        expected,
        case::parse_simple_header(
            & [
                0x31, 0x21, // Transaction ID: 0x3121
                0x81, 0x00, // Flags: response, recursion desired
                0x00, 0x01, // QDCOUNT: 1
                0x00, 0x01, // ANCOUNT: 1
                0x00, 0x00, // NSCOUNT: 0
                0x00, 0x00, // ARCOUNT: 0
            ],
            Ok((
                b"".as_ref(),
                (Header {
                    transaction_id: 0x3121,
                    flags: 0b1000_0001_0000_0000,
                    query_response: QueryResponse::Response,
                    opcode: OpCode::QUERY,
                    authoritative: false,
                    truncated: false,
                    recursion_desired: true,
                    recursion_available: false,
                    zflag: false,
                    authenticated_data: false,
                    check_disabled: false,
                    rcode: ResponseCode::NOERROR,
                    qdcount: 1,
                    ancount: 1,
                    nscount: 0,
                    arcount: 0,
                },
                ErrorFlags::none())
            ))
        ),
        case::parse_too_short_header(
            & [
                0x31, 0x21, // Transaction ID: 0x3121
                0x81, 0x00, // Flags: response, recursion desired
                0x00, 0x01, // QDCOUNT: 1
                0x00, 0x01, // ANCOUNT: 1
                0x00, 0x00, // NSCOUNT: 0
            ],
            Err(Error::incomplete_needed(2))
        ),
        case::parse_header_bad_opcode(
            & [
                0x31, 0x21, // Transaction ID: 0x3121
                0xb1, 0x00, // Flags: invalid opcode, recursion desired, authenticated data, format error
                0x00, 0x01, // QDCOUNT: 1
                0x00, 0x01, // ANCOUNT: 1
                0x00, 0x00, // NSCOUNT: 0
                0x00, 0x00, // ARCOUNT: 0
            ],
            Ok((
                b"".as_ref(),
                (Header {
                    transaction_id: 0x3121,
                    flags: 0b1011_0001_0000_0000,
                    query_response: QueryResponse::Response,
                    opcode: OpCode::UNKNOWN,
                    authoritative: false,
                    truncated: false,
                    recursion_desired: true,
                    recursion_available: false,
                    zflag: false,
                    authenticated_data: false,
                    check_disabled: false,
                    rcode: ResponseCode::NOERROR,
                    qdcount: 1,
                    ancount: 1,
                    nscount: 0,
                    arcount: 0,
                },
                ErrorFlags::UnknownOpcode.into())
            ))
        ),
        case::parse_header_bad_rcode(
            & [
                0x31, 0x21, // Transaction ID: 0x3121
                0x81, 0x0c, // Flags: response, recursion desired, invalid rcode
                0x00, 0x01, // QDCOUNT: 1
                0x00, 0x01, // ANCOUNT: 1
                0x00, 0x00, // NSCOUNT: 0
                0x00, 0x00, // ARCOUNT: 0
            ],
            Ok((
            b"".as_ref(),
            (Header {
                transaction_id: 0x3121,
                flags: 0b1000_0001_0000_1100,
                query_response: QueryResponse::Response,
                opcode: OpCode::QUERY,
                authoritative: false,
                truncated: false,
                recursion_desired: true,
                recursion_available: false,
                zflag: false,
                authenticated_data: false,
                check_disabled: false,
                rcode: ResponseCode::UNKNOWN,
                qdcount: 1,
                ancount: 1,
                nscount: 0,
                arcount: 0,
            },
            ErrorFlags::UnknownRcode.into())
            ))
        ),
    )]
    fn header(input: &[u8], expected: Result<(&[u8], (Header, Flags<ErrorFlags>))>) {
        assert_eq!(Header::parse(input), expected);
    }
}