dns_types/zones/
serialise.rs

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
use bytes::Bytes;
use std::collections::HashSet;
use std::fmt::Write as _;

use crate::protocol::types::*;
use crate::zones::types::*;

impl Zone {
    pub fn serialise(&self) -> String {
        let mut out = String::new();

        if let Some(soa) = self.get_soa() {
            let show_origin = !self.get_apex().is_root();
            let serialised_apex = serialise_octets(
                &self
                    .get_apex()
                    .to_dotted_string()
                    .bytes()
                    .collect::<Bytes>(),
                false,
            );

            if show_origin {
                _ = writeln!(&mut out, "$ORIGIN {serialised_apex}");
                out.push('\n');
            }

            _ = writeln!(
                &mut out,
                "{} IN SOA {}",
                if show_origin { "@" } else { &serialised_apex },
                self.serialise_rdata(&soa.to_rdata()),
            );
            out.push('\n');
        }

        let all_records = self.all_records();
        let all_wildcard_records = self.all_wildcard_records();

        let sorted_domains = {
            let mut set = HashSet::new();
            for name in all_records.keys() {
                set.insert(*name);
            }
            for name in all_wildcard_records.keys() {
                set.insert(*name);
            }
            let mut vec = set.into_iter().collect::<Vec<&DomainName>>();
            vec.sort();
            vec
        };

        for domain in sorted_domains {
            if let Some(zrs) = all_records.get(domain) {
                let has_wildcards = all_wildcard_records.contains_key(domain);
                for zr in zrs {
                    if zr.rtype_with_data.rtype() == RecordType::SOA {
                        // already handled above, and it's invalid for
                        // a zone to have multiple SOA records
                        continue;
                    }

                    _ = writeln!(
                        &mut out,
                        "{}{} {} IN {} {}",
                        self.serialise_domain(domain),
                        if has_wildcards { "  " } else { "" },
                        zr.ttl,
                        zr.rtype_with_data.rtype(),
                        self.serialise_rdata(&zr.rtype_with_data)
                    );
                }
            }
            if let Some(zrs) = all_wildcard_records.get(domain) {
                for zr in zrs {
                    _ = writeln!(
                        &mut out,
                        "*.{} {} IN {} {}",
                        self.serialise_domain(domain),
                        zr.ttl,
                        zr.rtype_with_data.rtype(),
                        self.serialise_rdata(&zr.rtype_with_data)
                    );
                }
            }
            out.push('\n');
        }

        out
    }

    /// Serialise a domain name: dotted string format, with the apex
    /// chopped off if this is an authoritative zone (unless the apex
    /// is the root domain, because that's only a single character
    /// long so we may as well show it).
    fn serialise_domain(&self, name: &DomainName) -> String {
        let domain_str = {
            let apex = self.get_apex();
            if apex.is_root() || !self.is_authoritative() || !name.is_subdomain_of(apex) {
                name.to_dotted_string()
            } else if name == apex {
                "@".to_string()
            } else {
                let labels_to_keep = name.labels.len() - apex.labels.len();
                DomainName {
                    labels: Vec::from(&name.labels[..labels_to_keep]),
                    len: name.len - apex.len,
                }
                .to_dotted_string()
            }
        };

        serialise_octets(&domain_str.bytes().collect::<Bytes>(), false)
    }

    /// Serialise the RDATA, with domains displayed relative to the apex (if
    /// authoritative).
    pub fn serialise_rdata(&self, rtype_with_data: &RecordTypeWithData) -> String {
        match rtype_with_data {
            RecordTypeWithData::A { address } => format!("{address}"),
            RecordTypeWithData::NS { nsdname } => self.serialise_domain(nsdname),
            RecordTypeWithData::MD { madname } => self.serialise_domain(madname),
            RecordTypeWithData::MF { madname } => self.serialise_domain(madname),
            RecordTypeWithData::CNAME { cname } => self.serialise_domain(cname),
            RecordTypeWithData::SOA {
                mname,
                rname,
                serial,
                refresh,
                retry,
                expire,
                minimum,
            } => format!(
                "{} {} {serial} {refresh} {retry} {expire} {minimum}",
                self.serialise_domain(mname),
                self.serialise_domain(rname),
            ),
            RecordTypeWithData::MB { madname } => self.serialise_domain(madname),
            RecordTypeWithData::MG { mdmname } => self.serialise_domain(mdmname),
            RecordTypeWithData::MR { newname } => self.serialise_domain(newname),
            RecordTypeWithData::NULL { octets } => serialise_octets(octets, true),
            RecordTypeWithData::WKS { octets } => serialise_octets(octets, true),
            RecordTypeWithData::PTR { ptrdname } => self.serialise_domain(ptrdname),
            RecordTypeWithData::HINFO { octets } => serialise_octets(octets, true),
            RecordTypeWithData::MINFO { rmailbx, emailbx } => format!(
                "{} {}",
                self.serialise_domain(rmailbx),
                self.serialise_domain(emailbx)
            ),
            RecordTypeWithData::MX {
                preference,
                exchange,
            } => format!("{preference} {}", self.serialise_domain(exchange)),
            RecordTypeWithData::TXT { octets } => serialise_octets(octets, true),
            RecordTypeWithData::AAAA { address } => format!("{address}"),
            RecordTypeWithData::SRV {
                priority,
                weight,
                port,
                target,
            } => format!(
                "{priority} {weight} {port} {}",
                self.serialise_domain(target)
            ),
            RecordTypeWithData::Unknown { octets, .. } => serialise_octets(octets, true),
        }
    }
}

/// Serialise a string of octets to a quoted or unquoted string with
/// the appropriate escaping.
fn serialise_octets(octets: &[u8], quoted: bool) -> String {
    let mut out = String::with_capacity(2 + octets.len());

    if quoted {
        out.push('"');
    }

    for octet in octets {
        if *octet == b'"' || *octet == b'\\' || *octet == b';' || *octet == b'(' || *octet == b')' {
            out.push('\\');
            out.push(*octet as char);
        } else if *octet < 32 || *octet > 126 || (*octet == 32 && !quoted) {
            out.push('\\');
            let digit3 = *octet % 10;
            let digit2 = (*octet / 10) % 10;
            let digit1 = (*octet / 100) % 10;
            out.push((digit1 + 48) as char);
            out.push((digit2 + 48) as char);
            out.push((digit3 + 48) as char);
        } else {
            out.push(*octet as char);
        }
    }

    if quoted {
        out.push('"');
    }

    out
}

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

    #[test]
    fn serialise_octets_special() {
        assert_eq!("\\012", serialise_octets(&[12], false));
        assert_eq!("\\234", serialise_octets(&[234], false));

        assert_eq!("\\\\", serialise_octets(b"\\", false));
        assert_eq!("\\\"", serialise_octets(b"\"", false));
    }

    #[test]
    fn serialise_octets_space() {
        assert_eq!("\\032", serialise_octets(b" ", false));
        assert_eq!("\" \"", serialise_octets(b" ", true));
    }
}