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
use crate::enums::{PacketType, Rcode};
use crate::resourcerecord::{DNSCharString, InternalResourceRecord};
use crate::{Header, Question};
use crate::{ResourceRecord, UDP_BUFFER_SIZE};
use log::error;
use packed_struct::prelude::*;

#[derive(Debug, Clone)]
pub struct Reply {
    pub header: Header,
    pub question: Option<Question>,
    pub answers: Vec<InternalResourceRecord>,
    pub authorities: Vec<ResourceRecord>,
    pub additional: Vec<ResourceRecord>,
}

impl Reply {
    /// This is used to turn into a series of bytes to yeet back to the client, needs to take a mutable self because the answers record length goes into the header
    pub async fn as_bytes(&self) -> Result<Vec<u8>, String> {
        let mut retval: Vec<u8> = vec![];

        // so we can set the headers
        let mut final_reply = self.clone();
        final_reply.header.ancount = final_reply.answers.len() as u16;
        // use the packed_struct to build the bytes
        let reply_header = match final_reply.header.pack() {
            Ok(value) => value,
            Err(err) => return Err(format!("Failed to pack reply header bytes: {:?}", err)),
        };
        retval.extend(reply_header);

        // need to add the question in here
        if let Some(question) = &final_reply.question {
            retval.extend(question.to_bytes());

            for answer in &final_reply.answers {
                let ttl: &u32 = match answer {
                    InternalResourceRecord::A { ttl, .. } => ttl,
                    InternalResourceRecord::AAAA { ttl, .. } => ttl,
                    InternalResourceRecord::AXFR { ttl, .. } => ttl,
                    InternalResourceRecord::CAA { ttl, .. } => ttl,
                    InternalResourceRecord::CNAME { ttl, .. } => ttl,
                    InternalResourceRecord::HINFO { ttl, .. } => ttl,
                    InternalResourceRecord::InvalidType => &1u32,
                    InternalResourceRecord::LOC { ttl, .. } => ttl,
                    InternalResourceRecord::MX { ttl, .. } => ttl,
                    InternalResourceRecord::NAPTR { ttl, .. } => ttl,
                    InternalResourceRecord::NS { ttl, .. } => ttl,
                    InternalResourceRecord::PTR { ttl, .. } => ttl,
                    InternalResourceRecord::SOA { minimum, .. } => minimum,
                    InternalResourceRecord::TXT { ttl, .. } => ttl,
                    InternalResourceRecord::URI { ttl, .. } => ttl,
                };

                let answer_record = ResourceRecord {
                    name: question.qname.clone(),
                    record_type: answer.to_owned().into(),
                    class: question.qclass,
                    ttl: *ttl,
                    rdata: answer.as_bytes(&question.qname),
                };
                let reply_bytes: Vec<u8> = answer_record.into();
                retval.extend(reply_bytes);
            }
        }

        for authority in &final_reply.authorities {
            error!(
                "Should be handling authority rr's in reply: {:?}",
                authority
            );
        }

        for additional in &final_reply.additional {
            error!(
                "Should be handling additional rr's in reply: {:?}",
                additional
            );
        }

        Ok(retval)
    }

    /// because sometimes you need to trunc that junk
    pub async fn as_bytes_udp(&self) -> Result<Vec<u8>, String> {
        let mut result = self.as_bytes().await?;
        if result.len() > UDP_BUFFER_SIZE {
            result.truncate(UDP_BUFFER_SIZE);
        };
        Ok(result)
    }

    /// checks to see if it's over the max length set in [UDP_BUFFER_SIZE] and set the truncated flag if it is
    pub async fn check_set_truncated(&self) -> Reply {
        if let Ok(ret_bytes) = self.as_bytes().await {
            if ret_bytes.len() > UDP_BUFFER_SIZE {
                let mut header = self.header.clone();
                header.truncated = true;
                return Self {
                    header,
                    ..self.clone()
                };
            }
        }
        self.clone()
    }
}

/// Want a generic empty reply with an ID and an RCODE? Here's your function.
pub fn reply_builder(id: u16, rcode: Rcode) -> Result<Reply, String> {
    let header = Header {
        id,
        qr: PacketType::Answer,
        rcode,
        ..Default::default()
    };
    Ok(Reply {
        header,
        question: None,
        answers: vec![],
        authorities: vec![],
        additional: vec![],
    })
}

/// Build a NXDOMAIN response
pub fn reply_nxdomain(id: u16) -> Result<Reply, String> {
    // RFC 2308  - 2.1 Name Error - <https://www.rfc-editor.org/rfc/rfc2308#section-2.1>
    reply_builder(id, Rcode::NameError)
}

/// Reply to an ANY request with a HINFO "RFC8482" "" response
pub fn reply_any(id: u16, question: Question) -> Result<Reply, String> {
    Ok(Reply {
        header: Header {
            id,
            qr: PacketType::Answer,
            rcode: Rcode::NoError,
            authoritative: true,
            qdcount: 1,
            ancount: 1,
            ..Header::default()
        },
        question: Some(question.clone()),
        answers: vec![InternalResourceRecord::HINFO {
            cpu: Some(DNSCharString::from("RFC8482")),
            os: Some(DNSCharString::from("")),
            ttl: 3789,
            rclass: question.qclass,
        }],
        authorities: vec![],
        additional: vec![],
    })
}