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
use byteorder::{BigEndian, ByteOrder};

use super::{OPCODE, RCODE};

mod flag {
    pub const QUERY: u16 = 0b1000_0000_0000_0000;
    pub const OPCODE_MASK: u16 = 0b0111_1000_0000_0000;
    pub const AUTHORITATIVE: u16 = 0b0000_0100_0000_0000;
    pub const TRUNCATED: u16 = 0b0000_0010_0000_0000;
    pub const RECURSION_DESIRED: u16 = 0b0000_0001_0000_0000;
    pub const RECURSION_AVAILABLE: u16 = 0b0000_0000_1000_0000;
    pub const AUTHENTIC_DATA: u16 = 0b0000_0000_0010_0000;
    pub const CHECKING_DISABLED: u16 = 0b0000_0000_0001_0000;
    pub const RESERVED_MASK: u16 = 0b0000_0000_0100_0000;
    pub const RESPONSE_CODE_MASK: u16 = 0b0000_0000_0000_1111;
}

/// Contains general information about the packet
#[derive(Debug)]
pub struct PacketHeader {
    /// The identification of the packet, must be defined when querying
    pub id: u16,
    /// Indicates if this packet is a query or a response.
    pub query: bool,
    /// Indicates the type of query in this packet
    pub opcode: OPCODE,
    /// Authoritative Answer - this bit is valid in responses,  
    /// and specifies that the responding name server is an authority for the domain name in question section.
    pub authoritative_answer: bool,
    /// TrunCation - specifies that this message was truncated due to  
    /// length greater than that permitted on the transmission channel.
    pub truncated: bool,
    /// Recursion Desired may be set in a query andis copied into the response.  
    /// If RD is set, it directs the name server to pursue the query recursively.  
    /// Recursive query support is optional.
    pub recursion_desired: bool,
    /// Recursion Available is set or cleared in a response.  
    /// It denotes whether recursive query support is available in the name server.
    pub recursion_available: bool,
    /// [RCODE](`RCODE`) indicates the response code for this packet
    pub response_code: RCODE,
    /// Indicate the number of questions in the packet
    pub questions_count: u16,
    /// Indicate the number of answers in the packet
    pub answers_count: u16,
    /// Indicate the number of name servers resource records in the packet
    pub name_servers_count: u16,
    /// Indicate the number of additional records in the packet
    pub additional_records_count: u16,
    pub authentic_data: bool,
    pub checking_disabled: bool,
}

impl PacketHeader {
    /// Creates a new header for a query packet
    pub fn new_query(id: u16, recursion_desired: bool) -> Self {
        Self {
            id,
            query: true,
            opcode: OPCODE::StandardQuery,
            authoritative_answer: false,
            truncated: false,
            recursion_desired,
            recursion_available: false,
            response_code: RCODE::NoError,
            additional_records_count: 0,
            answers_count: 0,
            name_servers_count: 0,
            questions_count: 0,
            authentic_data: false,
            checking_disabled: false,
        }
    }

    /// Creates a new header for a reply packet
    pub fn new_reply(id: u16, opcode: OPCODE) -> Self {
        Self {
            id,
            query: false,
            opcode,
            authoritative_answer: false,
            truncated: false,
            recursion_desired: false,
            recursion_available: false,
            response_code: RCODE::NoError,
            questions_count: 0,
            answers_count: 0,
            name_servers_count: 0,
            additional_records_count: 0,
            authentic_data: false,
            checking_disabled: false,
        }
    }

    /// Parse a slice of 12 bytes into a Packet header
    pub fn parse(data: &[u8]) -> crate::Result<Self> {
        if data.len() < 12 {
            return Err(crate::SimpleDnsError::InvalidHeaderData);
        }

        let flags = BigEndian::read_u16(&data[2..4]);
        if flags & flag::RESERVED_MASK != 0 {
            return Err(crate::SimpleDnsError::InvalidHeaderData);
        }

        let header = Self {
            id: BigEndian::read_u16(&data[..2]),
            query: flags & flag::QUERY == 0,
            opcode: ((flags & flag::OPCODE_MASK) >> flag::OPCODE_MASK.trailing_zeros()).into(),
            authoritative_answer: flags & flag::AUTHORITATIVE != 0,
            truncated: flags & flag::TRUNCATED != 0,
            recursion_desired: flags & flag::RECURSION_DESIRED != 0,
            recursion_available: flags & flag::RECURSION_AVAILABLE != 0,
            authentic_data: flags & flag::AUTHENTIC_DATA != 0,
            checking_disabled: flags & flag::CHECKING_DISABLED != 0,
            response_code: (flags & flag::RESPONSE_CODE_MASK).into(),
            questions_count: BigEndian::read_u16(&data[4..6]),
            answers_count: BigEndian::read_u16(&data[6..8]),
            name_servers_count: BigEndian::read_u16(&data[8..10]),
            additional_records_count: BigEndian::read_u16(&data[10..12]),
        };
        Ok(header)
    }

    /// Writes this header to a buffer of 12 bytes
    pub fn write_to(&self, buffer: &mut [u8]) {
        assert_eq!(12, buffer.len(), "Header buffer must have length of 12");

        BigEndian::write_u16(&mut buffer[0..2], self.id);
        BigEndian::write_u16(&mut buffer[2..4], self.get_flags());

        BigEndian::write_u16(&mut buffer[4..6], self.questions_count);
        BigEndian::write_u16(&mut buffer[6..8], self.answers_count);
        BigEndian::write_u16(&mut buffer[8..10], self.name_servers_count);
        BigEndian::write_u16(&mut buffer[10..12], self.additional_records_count);
    }

    fn get_flags(&self) -> u16 {
        let mut flags = 0u16;
        flags |= (self.opcode as u16) << flag::OPCODE_MASK.trailing_zeros();
        flags |= self.response_code as u16;
        if !self.query {
            flags |= flag::QUERY;
        }
        if self.authoritative_answer {
            flags |= flag::AUTHORITATIVE;
        }
        if self.recursion_desired {
            flags |= flag::RECURSION_DESIRED;
        }
        if self.recursion_available {
            flags |= flag::RECURSION_AVAILABLE;
        }
        if self.truncated {
            flags |= flag::TRUNCATED;
        }

        flags
    }

    /// Returns the packet id from the header buffer
    pub fn id(buffer: &[u8]) -> u16 {
        BigEndian::read_u16(&buffer[..2])
    }

    /// Returns the questions count from the header buffer
    pub fn read_questions(buffer: &[u8]) -> u16 {
        BigEndian::read_u16(&buffer[4..6])
    }

    /// Writes the questions count in the header buffer
    pub fn write_questions(buffer: &mut [u8], question_count: u16) {
        BigEndian::write_u16(&mut buffer[4..6], question_count);
    }

    /// Returns the answers count from the header buffer
    pub fn read_answers(buffer: &[u8]) -> u16 {
        BigEndian::read_u16(&buffer[6..8])
    }

    /// Writes the answers count in the header buffer
    pub fn write_answers(buffer: &mut [u8], answers_count: u16) {
        BigEndian::write_u16(&mut buffer[6..8], answers_count);
    }

    /// Returns the name servers count from the header buffer
    pub fn read_name_servers(buffer: &[u8]) -> u16 {
        BigEndian::read_u16(&buffer[8..10])
    }

    /// Writes the name servers count in the header buffer
    pub fn write_name_servers(buffer: &mut [u8], name_servers_count: u16) {
        BigEndian::write_u16(&mut buffer[8..10], name_servers_count);
    }

    /// Returns the additional records from the header buffer
    pub fn read_additional_records(buffer: &[u8]) -> u16 {
        BigEndian::read_u16(&buffer[10..12])
    }

    /// Writes the additional records count in the header buffer
    pub fn write_additional_records(buffer: &mut [u8], additional_records_count: u16) {
        BigEndian::write_u16(&mut buffer[10..12], additional_records_count);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn write_example_query() {
        let header = PacketHeader {
            id: core::u16::MAX,
            query: true,
            opcode: OPCODE::StandardQuery,
            authoritative_answer: false,
            truncated: true,
            recursion_desired: true,
            recursion_available: false,
            response_code: RCODE::NoError,
            additional_records_count: 2,
            answers_count: 2,
            name_servers_count: 2,
            questions_count: 2,
            authentic_data: false,
            checking_disabled: false,
        };

        let mut buf = [0u8; 12];
        header.write_to(&mut buf);

        assert_eq!(b"\xff\xff\x03\x00\x00\x02\x00\x02\x00\x02\x00\x02", &buf);
    }

    #[test]
    fn parse_example_query() {
        let header =
            PacketHeader::parse(b"\xff\xff\x03\x00\x00\x02\x00\x02\x00\x02\x00\x02").unwrap();

        assert_eq!(core::u16::MAX, header.id);
        assert_eq!(true, header.query);
        assert_eq!(OPCODE::StandardQuery, header.opcode);
        assert_eq!(false, header.authoritative_answer);
        assert_eq!(true, header.truncated);
        assert_eq!(true, header.recursion_desired);
        assert_eq!(false, header.recursion_available);
        assert_eq!(RCODE::NoError, header.response_code);
        assert_eq!(2, header.additional_records_count);
        assert_eq!(2, header.answers_count);
        assert_eq!(2, header.name_servers_count);
        assert_eq!(2, header.questions_count);
    }

    #[test]
    fn read_write_questions_count() {
        let mut buffer = [0u8; 12];
        PacketHeader::write_questions(&mut buffer, 1);
        assert_eq!(1, PacketHeader::read_questions(&buffer));
    }

    #[test]
    fn read_write_answers_count() {
        let mut buffer = [0u8; 12];
        PacketHeader::write_answers(&mut buffer, 1);
        assert_eq!(1, PacketHeader::read_answers(&buffer));
    }

    #[test]
    fn read_write_name_servers_count() {
        let mut buffer = [0u8; 12];
        PacketHeader::write_name_servers(&mut buffer, 1);
        assert_eq!(1, PacketHeader::read_name_servers(&buffer));
    }

    #[test]
    fn read_write_additional_records_count() {
        let mut buffer = [0u8; 12];
        PacketHeader::write_additional_records(&mut buffer, 1);
        assert_eq!(1, PacketHeader::read_additional_records(&buffer));
    }
}