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
use nom::bytes::streaming::take;
use nom::number::complete::be_u32;
use nom::number::streaming::be_u16;

use sawp_flags::{Flag, Flags};

use crate::enums::{RecordClass, RecordType};
use crate::rdata::RDataType;
use crate::{custom_count, ErrorFlags, IResult, Name};

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

/// Per RFC1035/RFC4408: max RDATA len = 65535 octets. Since TXT RDATA includes a length byte before
/// each TXT string, min size per TXT is 2 bytes, leaving maximum of 65535/2 parser runs needed.
const MAX_TXT_PARSES: usize = 32767;
/// First three bytes of an OPT AR - determines whether an AR should be parsed with special "OPT logic".
const OPT_RR_START: [u8; 3] = [0, 0, 41];

/// A parsed DNS answer
#[cfg_attr(feature = "ffi", derive(GenerateFFI))]
#[cfg_attr(feature = "ffi", sawp_ffi(prefix = "sawp_dns"))]
#[derive(Debug, PartialEq)]
pub struct Answer {
    pub name: Vec<u8>,
    #[cfg_attr(feature = "ffi", sawp_ffi(copy))]
    pub rtype: RecordType,
    pub rtype_raw: u16,
    #[cfg_attr(feature = "ffi", sawp_ffi(copy))]
    pub rclass: RecordClass,
    pub rclass_raw: u16,
    pub ttl: u32,
    pub data: RDataType,
}

impl Answer {
    fn parse<'a>(
        input: &'a [u8],
        reference_bytes: &'a [u8],
    ) -> IResult<'a, (Answer, Flags<ErrorFlags>)> {
        let (input, (name, mut error_flags)) = Name::parse(reference_bytes)(input)?;

        let (input, working_rtype) = be_u16(input)?;
        let rtype = RecordType::from_raw(working_rtype);
        if rtype == RecordType::UNKNOWN {
            error_flags |= ErrorFlags::UnknownRtype;
        }

        let (input, working_rclass) = be_u16(input)?;
        let rclass = RecordClass::from_raw(working_rclass);
        if rclass == RecordClass::UNKNOWN {
            error_flags |= ErrorFlags::UnknownRclass;
        }

        let (input, ttl) = be_u32(input)?;

        let mut answer: Answer = Answer {
            name,
            rtype,
            rtype_raw: working_rtype,
            rclass,
            rclass_raw: working_rclass,
            ttl,
            data: RDataType::UNKNOWN(vec![]),
        };

        let (input, data_len) = be_u16(input)?;
        let (rem, local_data) = take(data_len)(input)?;

        // always call once
        let (mut local_data, (mut rdata, inner_error_flags)) =
            RDataType::parse(local_data, reference_bytes, rtype)?;
        error_flags |= inner_error_flags;

        // get ref to buffer we will extend first, if TXT
        if let RDataType::TXT(ref mut current_rdata) = rdata {
            for _ in 0..MAX_TXT_PARSES - 1 {
                if local_data.is_empty() {
                    break;
                }
                let (new_data, (rdata, inner_error_flags)) =
                    RDataType::parse(local_data, reference_bytes, rtype)?;
                error_flags |= inner_error_flags;
                if let RDataType::TXT(new_rdata) = rdata {
                    current_rdata.extend(new_rdata);
                    local_data = new_data;
                } else {
                    break;
                }
            }
        }
        answer.data = rdata;
        Ok((rem, (answer, error_flags)))
    }

    fn parse_additional<'a>(
        input: &'a [u8],
        reference_bytes: &'a [u8],
    ) -> IResult<'a, (Answer, Flags<ErrorFlags>, bool)> {
        let mut opt_rr_present = false;
        if input.len() >= 3 && input[0..3] == OPT_RR_START[0..3] {
            let (input, (data, inner_error_flags)) = RDataType::parse_rdata_opt(&input[3..])?;
            opt_rr_present = true;
            Ok((
                input,
                (
                    Answer {
                        name: vec![0], // OPT RRs must be named 0 <root>
                        rtype: RecordType::OPT,
                        rtype_raw: 41,
                        rclass: RecordClass::NONE, // OPT RRs have no class
                        rclass_raw: 254,
                        ttl: 0, // OPT RRs do not contain a TTL
                        data,
                    },
                    inner_error_flags,
                    opt_rr_present,
                ),
            ))
        } else {
            let (input, (answer, inner_error_flags)) = Answer::parse(input, reference_bytes)?;
            Ok((input, (answer, inner_error_flags, opt_rr_present)))
        }
    }

    pub fn parse_additionals<'a>(
        input: &'a [u8],
        reference_bytes: &'a [u8],
        acnt: usize,
    ) -> IResult<'a, (Vec<Answer>, Flags<ErrorFlags>)> {
        let mut opt_rr_present = false;
        let mut error_flags = ErrorFlags::none();
        let (input, answers) = custom_count(
            |input, reference_bytes| {
                let (input, (answer, inner_error_flags, inner_opt_rr_present)) =
                    Answer::parse_additional(input, reference_bytes)?;
                if inner_opt_rr_present {
                    if opt_rr_present {
                        error_flags |= ErrorFlags::ExtraOptRr;
                    } else {
                        opt_rr_present = true;
                    }
                }
                error_flags |= inner_error_flags;
                Ok((input, answer))
            },
            acnt,
        )(input, reference_bytes)?;

        Ok((input, (answers, error_flags)))
    }

    pub fn parse_answers<'a>(
        input: &'a [u8],
        reference_bytes: &'a [u8],
        acnt: usize,
    ) -> IResult<'a, (Vec<Answer>, Flags<ErrorFlags>)> {
        let mut error_flags = ErrorFlags::none();
        let (input, answers) = custom_count(
            |input, reference_bytes| {
                let (input, (answer, inner_error_flags)) = Answer::parse(input, reference_bytes)?;
                error_flags |= inner_error_flags;
                Ok((input, answer))
            },
            acnt,
        )(input, reference_bytes)?;

        Ok((input, (answers, error_flags)))
    }
}