rdns-server 1.17.21

A high-performance, security-focused DNS server written in Rust
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use super::events::{BlockEvent, BlockEvents};
use super::policy::{PolicyAction, RpzTrigger, action_from_rdata};
use crate::protocol::header::Header;
use crate::protocol::message::Message;
use crate::protocol::name::DnsName;
use crate::protocol::opcode::Opcode;
use crate::protocol::rcode::Rcode;
use crate::protocol::rdata::RData;
use crate::protocol::record::{RecordClass, RecordType, ResourceRecord};
use arc_swap::ArcSwap;
use parking_lot::{Mutex, RwLock};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

/// Per-zone hit counter shared across all rules belonging to the zone.
type ZoneCounter = Arc<AtomicU64>;

/// Snapshot of statistics for a single RPZ zone.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ZoneStat {
    pub name: String,
    pub rules: u64,
    pub hits: u64,
}

/// Persistent record of a loaded zone — survives `clear()` and reloads,
/// keyed by zone name. Holds the source path so [`reload_all`] can rebuild.
#[derive(Clone)]
struct ZoneInfo {
    name: String,
    rules: u64,
    counter: ZoneCounter,
    source_path: Option<PathBuf>,
}

/// Rule payload stored alongside each match key.
/// Hot path: match → counter.fetch_add → return action.
#[derive(Clone)]
struct RuleEntry {
    action: PolicyAction,
    counter: ZoneCounter,
    zone_name: Arc<str>,
}

/// RPZ engine. Cheap to clone — internally `Arc`s.
#[derive(Clone)]
pub struct RpzEngine {
    /// Lock-free hot read path via ArcSwap. Writers serialize on `write_lock`.
    inner: Arc<ArcSwap<RpzState>>,
    write_lock: Arc<Mutex<()>>,
    /// Persistent zone registry. Counters in here outlive the rule state.
    zones: Arc<RwLock<HashMap<String, ZoneInfo>>>,
    /// Optional event sink for live block streaming.
    events: Arc<RwLock<Option<BlockEvents>>>,
}

#[derive(Clone, Default)]
struct RpzState {
    /// Exact QName matches for O(1) lookup.
    exact_rules: HashMap<DnsName, RuleEntry>,
    /// Wildcard rules stored as (base domain -> entry) for suffix matching.
    wildcard_rules: Vec<(DnsName, RuleEntry)>,
}

impl Default for RpzEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl RpzEngine {
    pub fn new() -> Self {
        Self {
            inner: Arc::new(ArcSwap::from_pointee(RpzState::default())),
            write_lock: Arc::new(Mutex::new(())),
            zones: Arc::new(RwLock::new(HashMap::new())),
            events: Arc::new(RwLock::new(None)),
        }
    }

    /// Install a [`BlockEvents`] sink. Subsequent matches will be recorded.
    pub fn set_event_sink(&self, sink: BlockEvents) {
        *self.events.write() = Some(sink);
    }

    /// Look up (or create) the persistent counter for a zone name.
    fn zone_counter(&self, zone_name: &str) -> ZoneCounter {
        let mut zones = self.zones.write();
        zones
            .entry(zone_name.to_string())
            .or_insert_with(|| ZoneInfo {
                name: zone_name.to_string(),
                rules: 0,
                counter: Arc::new(AtomicU64::new(0)),
                source_path: None,
            })
            .counter
            .clone()
    }

    fn set_zone_meta(&self, zone_name: &str, rules: u64, source_path: Option<PathBuf>) {
        let mut zones = self.zones.write();
        if let Some(info) = zones.get_mut(zone_name) {
            info.rules = rules;
            if source_path.is_some() {
                info.source_path = source_path;
            }
        }
    }

    /// Load an RPZ zone file. Records the path so [`reload_all`] can rebuild.
    pub fn load_zone_file(&self, path: &Path, zone_name: &DnsName) -> anyhow::Result<usize> {
        let content = std::fs::read_to_string(path)?;
        let count = self.load_zone_inner(&content, zone_name, Some(path.to_path_buf()))?;
        tracing::info!(
            zone = %zone_name,
            rules = count,
            path = %path.display(),
            "Loaded RPZ zone"
        );
        Ok(count)
    }

    /// Load RPZ rules from a string (no source path tracked — used by tests).
    pub fn load_zone_str(&self, content: &str, zone_name: &DnsName) -> anyhow::Result<usize> {
        self.load_zone_inner(content, zone_name, None)
    }

    fn load_zone_inner(
        &self,
        content: &str,
        zone_name: &DnsName,
        source_path: Option<PathBuf>,
    ) -> anyhow::Result<usize> {
        let zone_name_str = zone_name.to_dotted();
        let counter = self.zone_counter(&zone_name_str);
        let zone_arc: Arc<str> = Arc::from(zone_name_str.as_str());

        let (new_exact, new_wildcards) = parse_rpz(content, zone_name, &counter, &zone_arc);
        let count = new_exact.len() + new_wildcards.len();

        // Serialize writers; copy-on-write the state and atomic-swap.
        let _g = self.write_lock.lock();
        let mut next: RpzState = (**self.inner.load()).clone();
        for (name, entry) in new_exact {
            next.exact_rules.insert(name, entry);
        }
        next.wildcard_rules.extend(new_wildcards);
        self.inner.store(Arc::new(next));

        self.set_zone_meta(&zone_name_str, count as u64, source_path);
        Ok(count)
    }

    /// Rebuild the entire RPZ state from disk by re-reading every zone whose
    /// `source_path` is known. Counters and zone registry are preserved.
    /// Returns total rule count after reload.
    pub fn reload_all(&self) -> anyhow::Result<usize> {
        let pairs: Vec<(PathBuf, String, ZoneCounter)> = {
            let zones = self.zones.read();
            zones
                .values()
                .filter_map(|z| {
                    z.source_path
                        .clone()
                        .map(|p| (p, z.name.clone(), z.counter.clone()))
                })
                .collect()
        };

        let mut next = RpzState::default();
        let mut totals: Vec<(String, u64)> = Vec::with_capacity(pairs.len());

        for (path, zone_name_str, counter) in pairs {
            let content = match std::fs::read_to_string(&path) {
                Ok(c) => c,
                Err(e) => {
                    tracing::error!(zone = %zone_name_str, path = %path.display(), error = %e, "Reload: failed to read zone file");
                    continue;
                }
            };
            let zone_name = DnsName::from_str(zone_name_str.trim_end_matches('.'))
                .unwrap_or_else(|_| DnsName::root());
            let zone_arc: Arc<str> = Arc::from(zone_name_str.as_str());
            let (exact, wild) = parse_rpz(&content, &zone_name, &counter, &zone_arc);
            let z_total = (exact.len() + wild.len()) as u64;
            for (k, v) in exact {
                next.exact_rules.insert(k, v);
            }
            next.wildcard_rules.extend(wild);
            totals.push((zone_name_str, z_total));
        }

        let total: usize = next.exact_rules.len() + next.wildcard_rules.len();

        let _g = self.write_lock.lock();
        self.inner.store(Arc::new(next));

        // Update per-zone rule counts.
        {
            let mut zones = self.zones.write();
            for z in zones.values_mut() {
                z.rules = 0;
            }
            for (name, n) in totals {
                if let Some(z) = zones.get_mut(&name) {
                    z.rules = n;
                }
            }
        }

        tracing::info!(rules = total, "RPZ state reloaded");
        Ok(total)
    }

    /// Check a query name against all RPZ rules.
    /// On match: bump zone counter, record event, return cloned action.
    pub fn check(&self, qname: &DnsName) -> Option<PolicyAction> {
        let state = self.inner.load();

        if let Some(entry) = state.exact_rules.get(qname) {
            entry.counter.fetch_add(1, Ordering::Relaxed);
            self.notify(qname, &entry.action, &entry.zone_name);
            return Some(entry.action.clone());
        }

        for (base, entry) in &state.wildcard_rules {
            if qname.is_subdomain_of(base) && qname != base {
                entry.counter.fetch_add(1, Ordering::Relaxed);
                self.notify(qname, &entry.action, &entry.zone_name);
                return Some(entry.action.clone());
            }
        }

        None
    }

    fn notify(&self, qname: &DnsName, action: &PolicyAction, zone: &Arc<str>) {
        let guard = self.events.read();
        let Some(events) = guard.as_ref() else { return };
        let action_str = match action {
            PolicyAction::NxDomain => "nxdomain",
            PolicyAction::NoData => "nodata",
            PolicyAction::Passthru => "passthru",
            PolicyAction::Drop => "drop",
            PolicyAction::RedirectA(_)
            | PolicyAction::RedirectAAAA(_)
            | PolicyAction::RedirectCname(_) => "redirect",
        };
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        events.record(BlockEvent {
            ts,
            qname: qname.to_dotted(),
            action: action_str,
            zone: zone.to_string(),
        });
    }

    /// Build a DNS response based on the RPZ policy action.
    pub fn apply_action(&self, action: &PolicyAction, query: &Message) -> Option<Message> {
        let q = query.questions.first()?;
        match action {
            PolicyAction::NxDomain => Some(empty_response(query, Rcode::NxDomain)),
            PolicyAction::NoData => Some(empty_response(query, Rcode::NoError)),
            PolicyAction::Passthru => None,
            PolicyAction::Drop => None,
            PolicyAction::RedirectA(ip) => Some(redirect_response(
                query,
                ResourceRecord {
                    name: q.name.clone(),
                    rtype: RecordType::A,
                    rclass: RecordClass::IN,
                    ttl: 60,
                    rdata: RData::A(*ip),
                },
            )),
            PolicyAction::RedirectAAAA(ip) => Some(redirect_response(
                query,
                ResourceRecord {
                    name: q.name.clone(),
                    rtype: RecordType::AAAA,
                    rclass: RecordClass::IN,
                    ttl: 60,
                    rdata: RData::AAAA(*ip),
                },
            )),
            PolicyAction::RedirectCname(target) => Some(redirect_response(
                query,
                ResourceRecord {
                    name: q.name.clone(),
                    rtype: RecordType::CNAME,
                    rclass: RecordClass::IN,
                    ttl: 60,
                    rdata: RData::CNAME(target.clone()),
                },
            )),
        }
    }

    pub fn rule_count(&self) -> usize {
        let state = self.inner.load();
        state.exact_rules.len() + state.wildcard_rules.len()
    }

    pub fn zone_count(&self) -> usize {
        self.zones.read().len()
    }

    pub fn zone_stats(&self) -> Vec<ZoneStat> {
        let zones = self.zones.read();
        let mut out: Vec<ZoneStat> = zones
            .values()
            .map(|z| ZoneStat {
                name: z.name.clone(),
                rules: z.rules,
                hits: z.counter.load(Ordering::Relaxed),
            })
            .collect();
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }

    pub fn total_hits(&self) -> u64 {
        self.zones
            .read()
            .values()
            .map(|z| z.counter.load(Ordering::Relaxed))
            .sum()
    }

    pub fn clear(&self) {
        let _g = self.write_lock.lock();
        self.inner.store(Arc::new(RpzState::default()));
        let mut zones = self.zones.write();
        for z in zones.values_mut() {
            z.rules = 0;
        }
    }

    pub fn reset_counters(&self) {
        for z in self.zones.read().values() {
            z.counter.store(0, Ordering::Relaxed);
        }
    }
}

fn empty_response(query: &Message, rcode: Rcode) -> Message {
    Message {
        header: Header {
            id: query.header.id,
            qr: true,
            opcode: Opcode::Query,
            aa: false,
            tc: false,
            rd: query.header.rd,
            ra: true,
            ad: false,
            cd: false,
            rcode,
            qd_count: 1,
            an_count: 0,
            ns_count: 0,
            ar_count: 0,
        },
        questions: query.questions.clone(),
        answers: vec![],
        authority: vec![],
        additional: vec![],
        edns: None,
    }
}

fn redirect_response(query: &Message, answer: ResourceRecord) -> Message {
    Message {
        header: Header {
            id: query.header.id,
            qr: true,
            opcode: Opcode::Query,
            aa: false,
            tc: false,
            rd: query.header.rd,
            ra: true,
            ad: false,
            cd: false,
            rcode: Rcode::NoError,
            qd_count: 1,
            an_count: 1,
            ns_count: 0,
            ar_count: 0,
        },
        questions: query.questions.clone(),
        answers: vec![answer],
        authority: vec![],
        additional: vec![],
        edns: None,
    }
}

fn parse_rpz(
    content: &str,
    zone_name: &DnsName,
    counter: &ZoneCounter,
    zone_arc: &Arc<str>,
) -> (Vec<(DnsName, RuleEntry)>, Vec<(DnsName, RuleEntry)>) {
    let zone_suffix = format!(".{}", zone_name.to_dotted().trim_end_matches('.'));
    let mut exact = Vec::new();
    let mut wild = Vec::new();

    for line in content.lines() {
        let line = line.split(';').next().unwrap_or("").trim();
        if line.is_empty() || line.starts_with('$') {
            continue;
        }

        let tokens: Vec<&str> = line.split_whitespace().collect();
        if tokens.len() < 3 {
            continue;
        }

        if tokens.iter().any(|t| t.eq_ignore_ascii_case("SOA"))
            || tokens.iter().enumerate().any(|(i, t)| {
                i > 0 && t.eq_ignore_ascii_case("NS") && !tokens[0].eq_ignore_ascii_case("NS")
            })
        {
            continue;
        }

        let owner = tokens[0];
        let (rtype_idx, _rtype) = tokens
            .iter()
            .enumerate()
            .skip(1)
            .find(|(_, t)| matches!(t.to_uppercase().as_str(), "CNAME" | "A" | "AAAA" | "TXT"))
            .unwrap_or((0, &""));

        if rtype_idx == 0 || rtype_idx + 1 >= tokens.len() {
            continue;
        }

        let rtype_str = tokens[rtype_idx].to_uppercase();
        let rdata_str = tokens[rtype_idx + 1];

        let trigger_name = if owner == "@" {
            continue;
        } else if owner.ends_with(&zone_suffix) {
            owner.strip_suffix(&zone_suffix).unwrap_or(owner)
        } else {
            owner
        };

        let trigger = if let Some(stripped) = trigger_name.strip_prefix("*.") {
            let name = DnsName::from_str(stripped)
                .unwrap_or_else(|_| DnsName::from_str("invalid.").unwrap());
            RpzTrigger::QNameWildcard(name)
        } else {
            let name = DnsName::from_str(trigger_name)
                .unwrap_or_else(|_| DnsName::from_str("invalid.").unwrap());
            RpzTrigger::QName(name)
        };

        let action = match rtype_str.as_str() {
            "CNAME" => {
                let target = DnsName::from_str(rdata_str)
                    .unwrap_or_else(|_| DnsName::from_str(".").unwrap());
                action_from_rdata(&RData::CNAME(target))
            }
            "A" => match rdata_str.parse() {
                Ok(ip) => PolicyAction::RedirectA(ip),
                Err(_) => continue,
            },
            "AAAA" => match rdata_str.parse() {
                Ok(ip) => PolicyAction::RedirectAAAA(ip),
                Err(_) => continue,
            },
            _ => continue,
        };

        let entry = RuleEntry {
            action,
            counter: counter.clone(),
            zone_name: zone_arc.clone(),
        };

        match trigger {
            RpzTrigger::QName(name) => exact.push((name, entry)),
            RpzTrigger::QNameWildcard(name) => wild.push((name, entry)),
        }
    }

    (exact, wild)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::record::Question;

    #[test]
    fn test_rpz_load_and_check() {
        let engine = RpzEngine::new();
        let zone_name = DnsName::from_str("rpz.local").unwrap();

        let rpz_content = r#"
$TTL 300
@   SOA localhost. admin.localhost. 1 3600 900 604800 300
@   NS  localhost.
ads.example.com CNAME .
*.tracking.com CNAME .
malware.com A 127.0.0.1
safe.tracking.com CNAME rpz-passthru.
"#;

        let count = engine.load_zone_str(rpz_content, &zone_name).unwrap();
        assert_eq!(count, 4);

        assert_eq!(
            engine.check(&DnsName::from_str("ads.example.com").unwrap()),
            Some(PolicyAction::NxDomain)
        );
        assert_eq!(
            engine.check(&DnsName::from_str("foo.tracking.com").unwrap()),
            Some(PolicyAction::NxDomain)
        );
        assert_eq!(
            engine.check(&DnsName::from_str("malware.com").unwrap()),
            Some(PolicyAction::RedirectA(std::net::Ipv4Addr::new(127, 0, 0, 1)))
        );
        assert_eq!(
            engine.check(&DnsName::from_str("safe.tracking.com").unwrap()),
            Some(PolicyAction::Passthru)
        );
        assert_eq!(
            engine.check(&DnsName::from_str("clean.example.com").unwrap()),
            None
        );
    }

    #[test]
    fn test_rpz_apply_nxdomain() {
        let engine = RpzEngine::new();
        let query = Message {
            header: Header {
                id: 0x1234,
                qr: false,
                opcode: Opcode::Query,
                aa: false,
                tc: false,
                rd: true,
                ra: false,
                ad: false,
                cd: false,
                rcode: Rcode::NoError,
                qd_count: 1,
                an_count: 0,
                ns_count: 0,
                ar_count: 0,
            },
            questions: vec![Question {
                name: DnsName::from_str("blocked.com").unwrap(),
                qtype: RecordType::A,
                qclass: RecordClass::IN,
            }],
            answers: vec![],
            authority: vec![],
            additional: vec![],
            edns: None,
        };

        let response = engine.apply_action(&PolicyAction::NxDomain, &query).unwrap();
        assert_eq!(response.header.rcode, Rcode::NxDomain);
        assert_eq!(response.header.id, 0x1234);
        assert!(response.header.rd);
    }

    #[test]
    fn test_per_zone_hit_counters() {
        let engine = RpzEngine::new();
        let ads = DnsName::from_str("ads.local").unwrap();
        let mal = DnsName::from_str("mal.local").unwrap();

        engine.load_zone_str("a.example.com CNAME .\n", &ads).unwrap();
        engine
            .load_zone_str("b.example.com CNAME .\n*.tracker.com CNAME .\n", &mal)
            .unwrap();

        engine.check(&DnsName::from_str("a.example.com").unwrap());
        engine.check(&DnsName::from_str("b.example.com").unwrap());
        engine.check(&DnsName::from_str("x.tracker.com").unwrap());
        engine.check(&DnsName::from_str("y.tracker.com").unwrap());
        engine.check(&DnsName::from_str("nope.example.com").unwrap());

        let stats = engine.zone_stats();
        assert_eq!(stats.len(), 2);
        let ads_stat = stats.iter().find(|s| s.name == "ads.local.").unwrap();
        let mal_stat = stats.iter().find(|s| s.name == "mal.local.").unwrap();
        assert_eq!(ads_stat.hits, 1);
        assert_eq!(ads_stat.rules, 1);
        assert_eq!(mal_stat.hits, 3);
        assert_eq!(mal_stat.rules, 2);
        assert_eq!(engine.total_hits(), 4);
    }

    #[test]
    fn test_counters_persist_across_clear() {
        let engine = RpzEngine::new();
        let zone = DnsName::from_str("test.zone").unwrap();
        engine.load_zone_str("blocked.com CNAME .\n", &zone).unwrap();
        engine.check(&DnsName::from_str("blocked.com").unwrap());
        engine.check(&DnsName::from_str("blocked.com").unwrap());
        assert_eq!(engine.total_hits(), 2);

        engine.clear();
        assert_eq!(engine.rule_count(), 0);
        let stats = engine.zone_stats();
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].rules, 0);
        assert_eq!(stats[0].hits, 2);

        engine.load_zone_str("blocked.com CNAME .\n", &zone).unwrap();
        engine.check(&DnsName::from_str("blocked.com").unwrap());
        assert_eq!(engine.total_hits(), 3);
    }

    #[test]
    fn test_reload_all_from_files() {
        let engine = RpzEngine::new();
        let dir = tempdir();
        let zone_path = dir.join("test.rpz");
        std::fs::write(&zone_path, "blocked.com CNAME .\n").unwrap();
        let zone_name = DnsName::from_str("file.test").unwrap();
        engine.load_zone_file(&zone_path, &zone_name).unwrap();
        assert_eq!(engine.rule_count(), 1);

        // Bump counter so we can confirm it survives reload.
        engine.check(&DnsName::from_str("blocked.com").unwrap());
        assert_eq!(engine.total_hits(), 1);

        // Edit on disk → reload → rules update, counters retained.
        std::fs::write(&zone_path, "blocked.com CNAME .\nalso.com CNAME .\n").unwrap();
        let total = engine.reload_all().unwrap();
        assert_eq!(total, 2);
        assert_eq!(engine.total_hits(), 1);
        assert!(engine.check(&DnsName::from_str("also.com").unwrap()).is_some());
        assert_eq!(engine.total_hits(), 2);

        std::fs::remove_dir_all(dir).ok();
    }

    fn tempdir() -> std::path::PathBuf {
        let p = std::env::temp_dir().join(format!(
            "rdns-rpz-test-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&p).unwrap();
        p
    }

    #[test]
    fn test_event_sink_records_block() {
        let engine = RpzEngine::new();
        let sink = BlockEvents::new();
        engine.set_event_sink(sink.clone());
        let z = DnsName::from_str("z.local").unwrap();
        engine.load_zone_str("blocked.com CNAME .\n", &z).unwrap();
        engine.check(&DnsName::from_str("blocked.com").unwrap());
        let recent = sink.recent(10);
        assert_eq!(recent.len(), 1);
        assert_eq!(recent[0].qname, "blocked.com.");
        assert_eq!(recent[0].action, "nxdomain");
        assert_eq!(recent[0].zone, "z.local.");
    }
}