~nickbp/originz

ref: d94f181c5fbfdf0f62b40ea46d3f301f4ec681e5 originz/src/codec/encoder.rs -rw-r--r-- 8.0 KiB
d94f181cNick Parker Backport current benchmark to older code 1 year, 8 months ago
                                                                                
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
#![deny(warnings)]

use std::net::IpAddr;

use anyhow::{Context, Result};
use bytes::BytesMut;
use packed_struct::prelude::*;

use crate::codec::{domain_name, message, rdata};
use crate::fbs::dns_enums_generated::{ResourceType, ResponseCode};
use crate::fbs::dns_message_generated::{Message, Question, OPT};

/// Encodes DNS `Message`s to bytes to be sent over the wire.
pub struct DNSMessageEncoder {}

/// TTL of NXDOMAIN responses due to a domain being blocked.
/// We don't want this to be too long, in case the domain is later unblocked.
static CACHED_NXDOMAIN_TTL: u32 = 300; // 5 minutes

impl DNSMessageEncoder {
    /// Returns a `DNSMessageEncoder` for encoding DNS messages.
    pub fn new() -> DNSMessageEncoder {
        DNSMessageEncoder {}
    }

    /// Encodes a filter-based DNS response containing the provided overrides.
    /// The response could be e.g. an NXDOMAIN value for a blocked domain, or a custom A record for an overridden domain.
    pub fn encode_local_response<'a>(
        &self,
        response_code: ResponseCode,
        orig_header_id: u16,
        orig_question: &Question<'a>,
        orig_opt: Option<OPT<'a>>,
        ip: Option<IpAddr>,
        udp_size_override: Option<u16>,
        buf: &mut BytesMut,
    ) -> Result<()> {
        // Cache of string offsets for use in name compression
        let mut ptr_offsets = domain_name::LabelOffsets::new();

        message::write_header_bits(
            message::HeaderBits {
                id: orig_header_id,

                is_response: true,
                op_code: Integer::from(0 /*QUERY*/),
                authoritative: false,
                truncated: false,
                recursion_desired: true,
                recursion_available: true,
                reserved_9: false,
                authentic_data: false,
                checking_disabled: false,
                response_code: Integer::from(response_code as u8),

                question_count: 1,
                answer_count: match ip {
                    Some(_) => 1, // A or AAA resource
                    None => 0,
                },
                authority_count: match ip {
                    Some(_) => 0,
                    None => 1, // SOA resource
                },
                additional_count: match orig_opt {
                    Some(_) => 1,
                    None => 0,
                },
            },
            buf,
        )?;

        message::write_question(orig_question, buf, &mut ptr_offsets)?;

        // The name should really be there - how else would we know that we wanted to filter for it?
        let question_name = orig_question
            .name()
            .with_context(|| "missing resource.name")?;

        // Write either an A/AAAA Answer resource or an SOA Authority resource, depending on ip
        match ip {
            Some(ip) => message::write_resource_fields(
                &message::ResourceFields {
                    name: question_name,
                    resource_type: orig_question.resource_type(),
                    resource_class: orig_question.resource_class(),
                    ttl: 300,
                    rdata: message::RDataFields::IP(ip),
                },
                buf,
                &mut ptr_offsets,
            )?,
            None => {
                let rname = format!("kapiti.{}", question_name);
                message::write_resource_fields(
                    &message::ResourceFields {
                        name: question_name,
                        resource_type: ResourceType::TYPE_SOA as u16,
                        resource_class: orig_question.resource_class(),
                        ttl: CACHED_NXDOMAIN_TTL,
                        rdata: match ip {
                            Some(ip) => message::RDataFields::IP(ip),
                            None => message::RDataFields::SOA(rdata::SOAFields {
                                mname: question_name,
                                rname: rname.as_str(),
                                serial: 42069,
                                refresh: 6 * 60 * 60,
                                retry: 60 * 60,
                                expire: 3 * 24 * 60 * 60,
                                minimum: 5 * 60,
                            }),
                        },
                    },
                    buf,
                    &mut ptr_offsets,
                )?;
            }
        }

        // Ensure OPT resource is written within the Additional section
        if let Some(opt) = orig_opt {
            message::write_opt(&opt, udp_size_override, buf)?;
        }

        Ok(())
    }

    /// Encodes a cached DNS response containing the provided overrides.
    pub fn encode_cached_response<'a>(
        &self,
        message: &Message,
        orig_header_id: u16,
        ttl_subtract: u32,
        udp_size_override: Option<u16>,
        buf: &mut BytesMut,
    ) -> Result<()> {
        // Cache of string offsets for use in name compression
        let mut ptr_offsets = domain_name::LabelOffsets::new();

        message::write_header_id(message, orig_header_id, buf)?;

        if let Some(resources) = message.question() {
            for i in 0..resources.len() {
                message::write_question(&resources.get(i), buf, &mut ptr_offsets)?;
            }
        }

        if let Some(resources) = message.answer() {
            for i in 0..resources.len() {
                message::write_resource_ttl_subtract(
                    &resources.get(i),
                    ttl_subtract,
                    buf,
                    &mut ptr_offsets,
                )?;
            }
        }

        if let Some(resources) = message.authority() {
            for i in 0..resources.len() {
                message::write_resource_ttl_subtract(
                    &resources.get(i),
                    ttl_subtract,
                    buf,
                    &mut ptr_offsets,
                )?;
            }
        }

        // Ensure OPT resource is written within the Additional section
        if let Some(opt) = message.opt() {
            message::write_opt(&opt, udp_size_override, buf)?;
        }
        if let Some(resources) = message.additional() {
            for i in 0..resources.len() {
                message::write_resource_ttl_subtract(
                    &resources.get(i),
                    ttl_subtract,
                    buf,
                    &mut ptr_offsets,
                )?;
            }
        }

        Ok(())
    }

    /// Encodes a response from the provided flatbuffer representation
    pub fn encode(
        &self,
        message: &Message,
        udp_size_override: Option<u16>,
        buf: &mut BytesMut,
    ) -> Result<()> {
        // Cache of string offsets for use in name compression
        let mut ptr_offsets = domain_name::LabelOffsets::new();

        message::write_header_id(
            message,
            message
                .header()
                .with_context(|| "required header is missing")?
                .id(),
            buf,
        )?;

        if let Some(resources) = message.question() {
            for i in 0..resources.len() {
                message::write_question(&resources.get(i), buf, &mut ptr_offsets)?;
            }
        }

        if let Some(resources) = message.answer() {
            for i in 0..resources.len() {
                message::write_resource(&resources.get(i), buf, &mut ptr_offsets)?;
            }
        }

        if let Some(resources) = message.authority() {
            for i in 0..resources.len() {
                message::write_resource(&resources.get(i), buf, &mut ptr_offsets)?;
            }
        }

        // Ensure OPT resource is written within the Additional section
        if let Some(opt) = message.opt() {
            message::write_opt(&opt, udp_size_override, buf)?;
        }
        if let Some(resources) = message.additional() {
            for i in 0..resources.len() {
                message::write_resource(&resources.get(i), buf, &mut ptr_offsets)?;
            }
        }

        Ok(())
    }
}