simple-mdns 0.6.3

Rust implementation of mDNS for service discovering (DNS-SD)
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#![allow(unused)]
use std::collections::{BTreeMap, HashMap, HashSet};
use std::time::{Duration, Instant};

use radix_trie::{Trie, TrieCommon};
use simple_dns::{Name, ResourceRecord};

#[derive(Debug)]
pub struct ResourceRecordManager<'a> {
    authoritative: Trie<Vec<u8>, HashSet<ResourceRecord<'a>>>,
    cached: Trie<Vec<u8>, HashMap<ResourceRecord<'a>, ExpirationInfo>>,
}

impl<'a> ResourceRecordManager<'a> {
    pub fn new() -> Self {
        Self {
            authoritative: Default::default(),
            cached: Default::default(),
        }
    }

    /// Register a Resource Record
    pub fn add_authoritative_resource(&mut self, resource: ResourceRecord<'a>) {
        let key = get_key(&resource.name);
        match self.authoritative.get_mut(&key) {
            Some(resources) => {
                resources.insert(resource);
            }
            None => {
                let mut resources = HashSet::new();
                resources.insert(resource);

                self.authoritative.insert(key.clone(), resources);
            }
        }

        // FIXME: the trie implementation is not able to find subtries unless the parent prefix has
        // been manually added, this workaround is required to be able to find cached resources
        // related to subdomains. This assumes the main domain will be added in the authoritative
        // resources at some point
        if self.cached.get(&key).is_none() {
            self.cached.insert(key, Default::default());
        }
    }

    pub fn add_cached_resource(&mut self, resource: ResourceRecord<'a>) {
        let key = get_key(&resource.name);

        let exp_info = ExpirationInfo::new(resource.ttl);
        match self.cached.get_mut(&key) {
            Some(resources) if resource.cache_flush => {
                resources.remove(&resource);
            }
            Some(resources) => {
                resources.insert(resource, exp_info);
            }
            None if !resource.cache_flush => {
                let mut resources = HashMap::new();
                resources.insert(resource, exp_info);

                self.cached.insert(key, resources);
            }
            _ => {}
        }
    }

    pub fn remove_resource_record(&mut self, resource_record: &ResourceRecord<'a>) {
        let key = get_key(&resource_record.name);
        let remove_key = self
            .authoritative
            .get_mut(&key)
            .map(|resources| {
                resources.remove(&resource_record.clone());
                resources.is_empty()
            })
            .unwrap_or_default();

        if remove_key {
            self.authoritative.remove(&key);
        }
    }

    pub fn remove_domain_resources(&mut self, name: &Name) -> usize {
        let key = get_key(name);
        self.authoritative
            .remove(&key)
            .map(|resources| resources.len())
            .unwrap_or(0)
    }

    /// Remove all resource records
    pub fn clear(&mut self) {
        self.authoritative = Default::default();
        self.cached = Default::default();
    }

    pub fn get_next_refresh(&self) -> Option<Instant> {
        self.cached
            .iter()
            .flat_map(|(_, resources)| {
                resources.values().filter_map(|exp_info| {
                    if !exp_info.should_refresh() {
                        None
                    } else {
                        Some(exp_info.refresh_at)
                    }
                })
            })
            .min_by(|a, b| a.cmp(b))
    }

    /// Get all the resources matching the filter criteria and returns the results grouped by
    /// domain.
    ///
    /// if the filter does not include subdomains, the resulting iterator will contain a single
    /// value (the requested domain) and all the resource records for that domain.
    ///
    /// otherwise, the resulting iterator will contain one item for each subdomain found.
    pub fn get_domain_resources<'b>(
        &'a self,
        name: &'b Name,
        filter: DomainResourceFilter,
    ) -> impl Iterator<Item = impl Iterator<Item = &'a ResourceRecord<'a>>> {
        let key = get_key(name);
        let mut found: Vec<Vec<&'a ResourceRecord>> = Vec::new();

        fn filter_expired<'b>(
            (resource, exp_info): (&'b ResourceRecord<'b>, &'b ExpirationInfo),
        ) -> Option<&'b ResourceRecord<'b>> {
            if exp_info.is_expired() {
                None
            } else {
                Some(resource)
            }
        }

        if filter.subdomain {
            let mut domain_resources: BTreeMap<&Vec<u8>, HashSet<&ResourceRecord>> =
                Default::default();
            if filter.authoritative {
                if let Some(authoritative) = self.authoritative.subtrie(&key) {
                    for (domain, resources) in authoritative.iter() {
                        domain_resources
                            .entry(domain)
                            .or_default()
                            .extend(resources);
                    }
                }
            }

            if filter.cached {
                if let Some(cached) = self.cached.subtrie(&key) {
                    for (domain, resources) in cached.iter() {
                        let non_expired: Vec<&ResourceRecord> =
                            resources.iter().filter_map(filter_expired).collect();

                        if !non_expired.is_empty() {
                            domain_resources
                                .entry(domain)
                                .or_default()
                                .extend(non_expired);
                        }
                    }
                }
            }

            found.extend(
                domain_resources
                    .into_values()
                    .filter(|resources| !resources.is_empty())
                    .map(|inner| inner.into_iter().collect()),
            );
        } else {
            let mut resources = Vec::new();
            if filter.authoritative {
                if let Some(authoritative) = self.authoritative.get(&key) {
                    resources.extend(authoritative);
                }
            }

            if filter.cached {
                if let Some(cached) = self.cached.get(&key) {
                    resources.extend(cached.iter().filter_map(filter_expired));
                }
            }

            if !resources.is_empty() {
                found.push(resources);
            }
        }

        found
            .into_iter()
            .filter(|resources| !resources.is_empty())
            .map(|inner| inner.into_iter())
    }
}

#[derive(Debug)]
pub struct DomainResourceFilter {
    subdomain: bool,
    authoritative: bool,
    cached: bool,
}

impl DomainResourceFilter {
    pub fn authoritative(include_subdomains: bool) -> Self {
        Self {
            authoritative: true,
            subdomain: include_subdomains,
            cached: false,
        }
    }
    pub fn cached() -> Self {
        Self {
            authoritative: false,
            subdomain: true,
            cached: true,
        }
    }
    #[allow(dead_code)]
    pub fn all() -> Self {
        Self {
            authoritative: true,
            subdomain: true,
            cached: true,
        }
    }
}

fn get_key(name: &Name) -> Vec<u8> {
    name.get_labels()
        .iter()
        .rev()
        .flat_map(|label| label.to_string().into_bytes())
        .collect()
}

/// Provides known service expiration and refresh times
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
struct ExpirationInfo {
    refresh_at: Instant,
    expire_at: Instant,
}

impl ExpirationInfo {
    pub fn new(ttl: u32) -> Self {
        let ttl = ttl as u64;
        let added = Instant::now();
        let expire_at = added + Duration::from_secs(ttl);
        let refresh_at = match ttl {
            0 => expire_at,
            ttl if ttl < 60 => added + Duration::from_secs(ttl / 2),
            ttl => added + Duration::from_secs(ttl / 10 * 8),
        };

        Self {
            expire_at,
            refresh_at,
        }
    }

    pub fn should_refresh(&self) -> bool {
        self.refresh_at < Instant::now()
    }

    pub fn is_expired(&self) -> bool {
        self.expire_at < Instant::now()
    }
}

#[cfg(test)]
mod tests {
    use std::{convert::TryInto, net::Ipv4Addr, str::FromStr};

    use simple_dns::{rdata::RData, rdata::A, rdata::TXT, Name};

    use super::*;

    #[test]
    pub fn test_add_authoritative_resource() {
        let mut resources = ResourceRecordManager::new();
        resources.add_authoritative_resource(ResourceRecord::new(
            Name::new_unchecked("_srv1._tcp"),
            simple_dns::CLASS::IN,
            0,
            RData::TXT(TXT::new().with_string("version=1").unwrap()),
        ));

        assert_eq!(1, resources.authoritative.len());
    }

    #[test]
    pub fn test_add_cached_resource() {
        let mut resources = ResourceRecordManager::new();
        resources.add_cached_resource(ResourceRecord::new(
            Name::new_unchecked("_srv1._tcp"),
            simple_dns::CLASS::IN,
            0,
            RData::TXT(TXT::new().with_string("version=1").unwrap()),
        ));

        assert_eq!(1, resources.cached.len());
    }

    #[test]
    pub fn test_add_cached_resource_dont_override_authoritative() {
        let name = Name::new_unchecked("_srv1._tcp");
        let mut resources = ResourceRecordManager::new();
        resources.add_authoritative_resource(ResourceRecord::new(
            name.clone(),
            simple_dns::CLASS::IN,
            0,
            RData::TXT(TXT::new().with_string("version=1").unwrap()),
        ));

        resources.add_cached_resource(ResourceRecord::new(
            name.clone(),
            simple_dns::CLASS::IN,
            0,
            RData::TXT(TXT::new().with_string("version=2").unwrap()),
        ));

        assert_eq!(
            ResourceRecord::new(
                name.clone(),
                simple_dns::CLASS::IN,
                0,
                RData::TXT(TXT::new().with_string("version=1").unwrap()),
            ),
            *resources
                .authoritative
                .get(&get_key(&name))
                .unwrap()
                .iter()
                .next()
                .unwrap()
        );
    }

    #[test]
    pub fn test_get_domain_resources() {
        let mut resources = ResourceRecordManager::new();
        resources.add_authoritative_resource(ResourceRecord::new(
            "a._srv._tcp.local".try_into().unwrap(),
            simple_dns::CLASS::IN,
            0,
            RData::A(A::from(Ipv4Addr::from_str("127.0.0.1").unwrap())),
        ));
        resources.add_cached_resource(ResourceRecord::new(
            "b._srv._tcp.local".try_into().unwrap(),
            simple_dns::CLASS::IN,
            60,
            RData::A(A::from(Ipv4Addr::from_str("127.0.0.2").unwrap())),
        ));
        resources.add_authoritative_resource(ResourceRecord::new(
            "_srv._tcp.local".try_into().unwrap(),
            simple_dns::CLASS::IN,
            0,
            RData::A(A::from(Ipv4Addr::from_str("127.0.0.3").unwrap())),
        ));

        let get_records =
            |domain: &str, filter: DomainResourceFilter| -> Vec<Vec<&ResourceRecord>> {
                resources
                    .get_domain_resources(&domain.try_into().unwrap(), filter)
                    .map(|r| r.collect())
                    .collect()
            };

        let compare_ips = |record: &ResourceRecord, ip: &str| {
            if let RData::A(address) = &record.rdata {
                assert_eq!(
                    Ipv4Addr::from(address.address),
                    Ipv4Addr::from_str(ip).unwrap()
                )
            } else {
                panic!("something is wrong");
            }
        };

        let records = get_records(
            "a._srv._tcp.local",
            DomainResourceFilter::authoritative(false),
        );
        assert_eq!(1, records.len());
        compare_ips(records[0][0], "127.0.0.1");

        let records = get_records("b._srv._tcp.local", DomainResourceFilter::cached());
        assert_eq!(1, records.len());
        compare_ips(records[0][0], "127.0.0.2");

        let records = get_records("_srv._tcp.local", DomainResourceFilter::authoritative(true));
        assert_eq!(2, records.len());
        compare_ips(records[0][0], "127.0.0.3");
        compare_ips(records[1][0], "127.0.0.1");

        let records = get_records("_srv._tcp.local", DomainResourceFilter::all());
        assert_eq!(3, records.len());

        assert!(get_records(
            "_xxx._tcp.local",
            DomainResourceFilter::authoritative(false)
        )
        .is_empty());
        assert!(get_records(
            "b._srv._tcp.local",
            DomainResourceFilter::authoritative(false)
        )
        .is_empty());
        assert!(get_records(
            "_xxx._tcp.local",
            DomainResourceFilter::authoritative(false)
        )
        .is_empty());
    }

    #[test]
    pub fn test_remove_resources() {
        let mut resources = ResourceRecordManager::new();
        resources.add_authoritative_resource(ResourceRecord::new(
            "a._srv._tcp.local".try_into().unwrap(),
            simple_dns::CLASS::IN,
            0,
            RData::A(A::from(Ipv4Addr::from_str("127.0.0.1").unwrap())),
        ));
        resources.add_cached_resource(ResourceRecord::new(
            "b._srv._tcp.local".try_into().unwrap(),
            simple_dns::CLASS::IN,
            60,
            RData::A(A::from(Ipv4Addr::from_str("127.0.0.2").unwrap())),
        ));
        resources.add_authoritative_resource(ResourceRecord::new(
            "_srv._tcp.local".try_into().unwrap(),
            simple_dns::CLASS::IN,
            0,
            RData::A(A::from(Ipv4Addr::from_str("127.0.0.3").unwrap())),
        ));

        assert_eq!(
            1,
            resources.remove_domain_resources(&Name::new_unchecked("a._srv._tcp.local"))
        );

        assert_eq!(
            1,
            resources.remove_domain_resources(&Name::new_unchecked("_srv._tcp.local"))
        );
    }
}