trust_dns_server/store/recursor/
config.rs

1// Copyright 2015-2022 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use std::{
9    borrow::Cow,
10    fs::File,
11    io::Read,
12    net::SocketAddr,
13    path::{Path, PathBuf},
14};
15
16use serde::Deserialize;
17
18use crate::error::ConfigError;
19use crate::proto::{
20    rr::{RData, Record, RecordSet},
21    serialize::txt::{Lexer, Parser},
22};
23use crate::resolver::Name;
24
25/// Configuration for file based zones
26#[derive(Clone, Deserialize, Eq, PartialEq, Debug)]
27pub struct RecursiveConfig {
28    /// File with roots, aka hints
29    pub roots: PathBuf,
30}
31
32impl RecursiveConfig {
33    pub(crate) fn read_roots(
34        &self,
35        root_dir: Option<&Path>,
36    ) -> Result<Vec<SocketAddr>, ConfigError> {
37        let path = if let Some(root_dir) = root_dir {
38            Cow::Owned(root_dir.join(&self.roots))
39        } else {
40            Cow::Borrowed(&self.roots)
41        };
42
43        let mut roots = File::open(path.as_ref())?;
44        let mut roots_str = String::new();
45        roots.read_to_string(&mut roots_str)?;
46
47        let lexer = Lexer::new(&roots_str);
48        let mut parser = Parser::new();
49
50        let (_zone, roots_zone) = parser.parse(lexer, Some(Name::root()))?;
51
52        // TODO: we may want to deny some of the root nameservers, for reasons...
53        Ok(roots_zone
54            .values()
55            .flat_map(RecordSet::records_without_rrsigs)
56            .filter_map(Record::data)
57            .filter_map(RData::ip_addr) // we only want IPs
58            .map(|ip| SocketAddr::from((ip, 53))) // all the roots only have tradition DNS ports
59            .collect())
60    }
61}