simple_dns_server/
config.rs1use crate::dns::{get_record_name, to_rdata};
2use crate::SimpleDnsError;
3use serde::{Deserialize, Serialize};
4use std::{collections::BTreeMap, net::SocketAddr};
5use trust_dns_proto::rr::RecordType;
6use trust_dns_proto::rr::{Record, RecordSet};
7
8const DEFAULT_TTL: u32 = 3600;
9
10#[derive(Serialize, Deserialize, Debug)]
11pub struct Config {
12 pub bind: SocketAddr,
13 pub domains: BTreeMap<String, Vec<RecordInfo>>,
14}
15#[derive(Serialize, Deserialize, Debug)]
16pub struct RecordInfo {
17 pub name: String,
18 pub records: Vec<String>,
19 #[serde(
20 rename = "type",
21 default = "default_record_type",
22 skip_serializing_if = "is_default_record_type"
23 )]
24 pub record_type: RecordType,
25}
26
27pub fn default_record_type() -> RecordType {
28 RecordType::A
29}
30
31pub fn is_default_record_type(record_type: &RecordType) -> bool {
32 *record_type == default_record_type()
33}
34
35impl RecordInfo {
36 pub fn new(name: &str, records: &[&str], record_type: RecordType) -> Self {
37 Self {
38 name: name.to_string(),
39 records: records.iter().map(|s| s.to_string()).collect(),
40 record_type,
41 }
42 }
43
44 pub fn into_record_set(self, origin: &str) -> Result<RecordSet, SimpleDnsError> {
45 let name = get_record_name(&self.name, origin)?;
46 let record_type = self.record_type;
47
48 let mut record_set = RecordSet::with_ttl(name.clone(), record_type, DEFAULT_TTL);
49 for r in self.records {
50 let rdata = to_rdata(record_type, &r, origin)?;
51 let record = Record::from_rdata(name.clone(), DEFAULT_TTL, rdata);
52 record_set.insert(record, 0);
53 }
54
55 Ok(record_set)
56 }
57}