rename Domain -> FQDN
This commit is contained in:
parent
e29b901bc1
commit
7f7d9f7ccf
|
@ -4,7 +4,7 @@ use core::str::FromStr;
|
|||
use std::net::Ipv4Addr;
|
||||
|
||||
use crate::container::Container;
|
||||
use crate::{Domain, Error, Result};
|
||||
use crate::{Error, Result, FQDN};
|
||||
|
||||
pub struct Client {
|
||||
inner: Container,
|
||||
|
@ -22,7 +22,7 @@ impl Client {
|
|||
recurse: Recurse,
|
||||
server: Ipv4Addr,
|
||||
record_type: RecordType,
|
||||
domain: &Domain<'_>,
|
||||
domain: &FQDN<'_>,
|
||||
) -> Result<DigOutput> {
|
||||
let output = self.inner.stdout(&[
|
||||
"dig",
|
||||
|
@ -252,7 +252,7 @@ impl FromStr for Record {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct A {
|
||||
pub domain: Domain<'static>,
|
||||
pub domain: FQDN<'static>,
|
||||
pub ttl: u32,
|
||||
pub ipv4_addr: Ipv4Addr,
|
||||
}
|
||||
|
@ -288,10 +288,10 @@ impl FromStr for A {
|
|||
#[allow(clippy::upper_case_acronyms)]
|
||||
#[derive(Debug)]
|
||||
pub struct SOA {
|
||||
pub domain: Domain<'static>,
|
||||
pub domain: FQDN<'static>,
|
||||
pub ttl: u32,
|
||||
pub nameserver: Domain<'static>,
|
||||
pub admin: Domain<'static>,
|
||||
pub nameserver: FQDN<'static>,
|
||||
pub admin: FQDN<'static>,
|
||||
pub serial: u32,
|
||||
pub refresh: u32,
|
||||
pub retry: u32,
|
||||
|
|
|
@ -5,13 +5,13 @@ use std::borrow::Cow;
|
|||
use crate::{Error, Result};
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct Domain<'a> {
|
||||
pub struct FQDN<'a> {
|
||||
inner: Cow<'a, str>,
|
||||
}
|
||||
|
||||
// TODO likely needs further validation
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Domain<'a>(input: impl Into<Cow<'a, str>>) -> Result<Domain<'a>> {
|
||||
pub fn FQDN<'a>(input: impl Into<Cow<'a, str>>) -> Result<FQDN<'a>> {
|
||||
let input = input.into();
|
||||
if !input.ends_with('.') {
|
||||
return Err("domain must end with a `.`".into());
|
||||
|
@ -21,15 +21,15 @@ pub fn Domain<'a>(input: impl Into<Cow<'a, str>>) -> Result<Domain<'a>> {
|
|||
return Err("non-root domain cannot start with a `.`".into());
|
||||
}
|
||||
|
||||
Ok(Domain { inner: input })
|
||||
Ok(FQDN { inner: input })
|
||||
}
|
||||
|
||||
impl<'a> Domain<'a> {
|
||||
pub const ROOT: Domain<'static> = Domain {
|
||||
impl<'a> FQDN<'a> {
|
||||
pub const ROOT: FQDN<'static> = FQDN {
|
||||
inner: Cow::Borrowed("."),
|
||||
};
|
||||
|
||||
pub const COM: Domain<'static> = Domain {
|
||||
pub const COM: FQDN<'static> = FQDN {
|
||||
inner: Cow::Borrowed("com."),
|
||||
};
|
||||
|
||||
|
@ -41,33 +41,33 @@ impl<'a> Domain<'a> {
|
|||
&self.inner
|
||||
}
|
||||
|
||||
pub fn into_owned(self) -> Domain<'static> {
|
||||
pub fn into_owned(self) -> FQDN<'static> {
|
||||
let owned = match self.inner {
|
||||
Cow::Borrowed(borrowed) => borrowed.to_string(),
|
||||
Cow::Owned(owned) => owned,
|
||||
};
|
||||
|
||||
Domain {
|
||||
FQDN {
|
||||
inner: Cow::Owned(owned),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Domain<'static> {
|
||||
impl FromStr for FQDN<'static> {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(input: &str) -> Result<Self> {
|
||||
Ok(Domain(input)?.into_owned())
|
||||
Ok(FQDN(input)?.into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Domain<'_> {
|
||||
impl fmt::Debug for FQDN<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Domain<'_> {
|
||||
impl fmt::Display for FQDN<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.inner)
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use std::sync::atomic::{self, AtomicUsize};
|
||||
|
||||
pub use crate::client::Client;
|
||||
pub use crate::domain::Domain;
|
||||
pub use crate::fqdn::FQDN;
|
||||
pub use crate::name_server::NameServer;
|
||||
pub use crate::recursive_resolver::RecursiveResolver;
|
||||
|
||||
|
@ -12,7 +12,7 @@ const CHMOD_RW_EVERYONE: &str = "666";
|
|||
|
||||
mod client;
|
||||
pub mod container;
|
||||
mod domain;
|
||||
mod fqdn;
|
||||
mod name_server;
|
||||
pub mod record;
|
||||
mod recursive_resolver;
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::process::Child;
|
|||
|
||||
use crate::container::Container;
|
||||
use crate::record::{self, Referral, SoaSettings, ZoneFile};
|
||||
use crate::{Domain, Result, CHMOD_RW_EVERYONE};
|
||||
use crate::{Result, CHMOD_RW_EVERYONE, FQDN};
|
||||
|
||||
pub struct NameServer<'a, State> {
|
||||
container: Container,
|
||||
|
@ -23,7 +23,7 @@ impl<'a> NameServer<'a, Stopped> {
|
|||
///
|
||||
/// - one SOA record, with the primary name server set to the name server domain
|
||||
/// - one NS record, with the name server domain set as the only available name server for
|
||||
pub fn new(zone: Domain<'a>) -> Result<Self> {
|
||||
pub fn new(zone: FQDN<'a>) -> Result<Self> {
|
||||
let ns_count = crate::nameserver_count();
|
||||
let nameserver = primary_ns(ns_count);
|
||||
|
||||
|
@ -54,7 +54,7 @@ impl<'a> NameServer<'a, Stopped> {
|
|||
}
|
||||
|
||||
/// Adds an A record pair to the zone file
|
||||
pub fn a(&mut self, domain: Domain<'a>, ipv4_addr: Ipv4Addr) -> &mut Self {
|
||||
pub fn a(&mut self, domain: FQDN<'a>, ipv4_addr: Ipv4Addr) -> &mut Self {
|
||||
self.zone_file.record(record::A { domain, ipv4_addr });
|
||||
self
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ impl<'a, S> NameServer<'a, S> {
|
|||
&self.zone_file
|
||||
}
|
||||
|
||||
pub fn nameserver(&self) -> &Domain<'a> {
|
||||
pub fn nameserver(&self) -> &FQDN<'a> {
|
||||
&self.zone_file.soa.ns
|
||||
}
|
||||
}
|
||||
|
@ -119,15 +119,15 @@ impl Drop for Running {
|
|||
}
|
||||
}
|
||||
|
||||
fn primary_ns(ns_count: usize) -> Domain<'static> {
|
||||
Domain(format!("primary{ns_count}.nameservers.com.")).unwrap()
|
||||
fn primary_ns(ns_count: usize) -> FQDN<'static> {
|
||||
FQDN(format!("primary{ns_count}.nameservers.com.")).unwrap()
|
||||
}
|
||||
|
||||
fn admin_ns(ns_count: usize) -> Domain<'static> {
|
||||
Domain(format!("admin{ns_count}.nameservers.com.")).unwrap()
|
||||
fn admin_ns(ns_count: usize) -> FQDN<'static> {
|
||||
FQDN(format!("admin{ns_count}.nameservers.com.")).unwrap()
|
||||
}
|
||||
|
||||
fn nsd_conf(domain: &Domain) -> String {
|
||||
fn nsd_conf(domain: &FQDN) -> String {
|
||||
minijinja::render!(
|
||||
include_str!("templates/nsd.conf.jinja"),
|
||||
domain => domain.as_str()
|
||||
|
@ -145,11 +145,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn simplest() -> Result<()> {
|
||||
let tld_ns = NameServer::new(Domain::COM)?.start()?;
|
||||
let tld_ns = NameServer::new(FQDN::COM)?.start()?;
|
||||
let ip_addr = tld_ns.ipv4_addr();
|
||||
|
||||
let client = Client::new()?;
|
||||
let output = client.dig(Recurse::No, ip_addr, RecordType::SOA, &Domain::COM)?;
|
||||
let output = client.dig(Recurse::No, ip_addr, RecordType::SOA, &FQDN::COM)?;
|
||||
|
||||
assert!(output.status.is_noerror());
|
||||
|
||||
|
@ -159,11 +159,11 @@ mod tests {
|
|||
#[test]
|
||||
fn with_referral() -> Result<()> {
|
||||
let expected_ip_addr = Ipv4Addr::new(172, 17, 200, 1);
|
||||
let mut root_ns = NameServer::new(Domain::ROOT)?;
|
||||
let mut root_ns = NameServer::new(FQDN::ROOT)?;
|
||||
root_ns.referral(&Referral {
|
||||
domain: Domain::COM,
|
||||
domain: FQDN::COM,
|
||||
ipv4_addr: expected_ip_addr,
|
||||
ns: Domain("primary.tld-server.com.")?,
|
||||
ns: FQDN("primary.tld-server.com.")?,
|
||||
});
|
||||
let root_ns = root_ns.start()?;
|
||||
|
||||
|
@ -172,7 +172,7 @@ mod tests {
|
|||
let ipv4_addr = root_ns.ipv4_addr();
|
||||
|
||||
let client = Client::new()?;
|
||||
let output = client.dig(Recurse::No, ipv4_addr, RecordType::NS, &Domain::COM)?;
|
||||
let output = client.dig(Recurse::No, ipv4_addr, RecordType::NS, &FQDN::COM)?;
|
||||
|
||||
assert!(output.status.is_noerror());
|
||||
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
use core::fmt;
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
use crate::Domain;
|
||||
use crate::FQDN;
|
||||
|
||||
pub struct ZoneFile<'a> {
|
||||
pub origin: Domain<'a>,
|
||||
pub origin: FQDN<'a>,
|
||||
pub ttl: u32,
|
||||
pub soa: Soa<'a>,
|
||||
pub records: Vec<Record<'a>>,
|
||||
|
@ -16,7 +16,7 @@ pub struct ZoneFile<'a> {
|
|||
|
||||
impl<'a> ZoneFile<'a> {
|
||||
/// Convenience constructor that uses "reasonable" defaults
|
||||
pub fn new(origin: Domain<'a>, soa: Soa<'a>) -> Self {
|
||||
pub fn new(origin: FQDN<'a>, soa: Soa<'a>) -> Self {
|
||||
Self {
|
||||
origin,
|
||||
ttl: 1800,
|
||||
|
@ -71,20 +71,20 @@ impl fmt::Display for ZoneFile<'_> {
|
|||
}
|
||||
|
||||
pub struct Referral<'a> {
|
||||
pub domain: Domain<'a>,
|
||||
pub domain: FQDN<'a>,
|
||||
pub ipv4_addr: Ipv4Addr,
|
||||
pub ns: Domain<'a>,
|
||||
pub ns: FQDN<'a>,
|
||||
}
|
||||
|
||||
pub struct Root<'a> {
|
||||
pub ipv4_addr: Ipv4Addr,
|
||||
pub ns: Domain<'a>,
|
||||
pub ns: FQDN<'a>,
|
||||
pub ttl: u32,
|
||||
}
|
||||
|
||||
impl<'a> Root<'a> {
|
||||
/// Convenience constructor that uses "reasonable" defaults
|
||||
pub fn new(ns: Domain<'a>, ipv4_addr: Ipv4Addr) -> Self {
|
||||
pub fn new(ns: FQDN<'a>, ipv4_addr: Ipv4Addr) -> Self {
|
||||
Self {
|
||||
ipv4_addr,
|
||||
ns,
|
||||
|
@ -130,7 +130,7 @@ impl fmt::Display for Record<'_> {
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct A<'a> {
|
||||
pub domain: Domain<'a>,
|
||||
pub domain: FQDN<'a>,
|
||||
pub ipv4_addr: Ipv4Addr,
|
||||
}
|
||||
|
||||
|
@ -143,8 +143,8 @@ impl fmt::Display for A<'_> {
|
|||
}
|
||||
|
||||
pub struct Ns<'a> {
|
||||
pub domain: Domain<'a>,
|
||||
pub ns: Domain<'a>,
|
||||
pub domain: FQDN<'a>,
|
||||
pub ns: FQDN<'a>,
|
||||
}
|
||||
|
||||
impl fmt::Display for Ns<'_> {
|
||||
|
@ -156,9 +156,9 @@ impl fmt::Display for Ns<'_> {
|
|||
}
|
||||
|
||||
pub struct Soa<'a> {
|
||||
pub domain: Domain<'a>,
|
||||
pub ns: Domain<'a>,
|
||||
pub admin: Domain<'a>,
|
||||
pub domain: FQDN<'a>,
|
||||
pub ns: FQDN<'a>,
|
||||
pub admin: FQDN<'a>,
|
||||
pub settings: SoaSettings,
|
||||
}
|
||||
|
||||
|
@ -237,7 +237,7 @@ mod tests {
|
|||
fn root_to_string() -> Result<()> {
|
||||
let expected = ". 3600000 NS a.root-servers.net.
|
||||
a.root-servers.net. 3600000 A 198.41.0.4";
|
||||
let root = Root::new(Domain("a.root-servers.net.")?, Ipv4Addr::new(198, 41, 0, 4));
|
||||
let root = Root::new(FQDN("a.root-servers.net.")?, Ipv4Addr::new(198, 41, 0, 4));
|
||||
assert_eq!(expected, root.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
@ -260,7 +260,7 @@ $TTL 1800
|
|||
com. IN NS e.gtld-servers.net.
|
||||
e.gtld-servers.net. IN A 192.12.94.30
|
||||
";
|
||||
let mut zone = ZoneFile::new(Domain::ROOT, example_soa()?);
|
||||
let mut zone = ZoneFile::new(FQDN::ROOT, example_soa()?);
|
||||
zone.record(example_ns()?);
|
||||
zone.record(example_a()?);
|
||||
|
||||
|
@ -271,23 +271,23 @@ e.gtld-servers.net. IN A 192.12.94.30
|
|||
|
||||
fn example_a() -> Result<A<'static>> {
|
||||
Ok(A {
|
||||
domain: Domain("e.gtld-servers.net.")?,
|
||||
domain: FQDN("e.gtld-servers.net.")?,
|
||||
ipv4_addr: Ipv4Addr::new(192, 12, 94, 30),
|
||||
})
|
||||
}
|
||||
|
||||
fn example_ns() -> Result<Ns<'static>> {
|
||||
Ok(Ns {
|
||||
domain: Domain("com.")?,
|
||||
ns: Domain("e.gtld-servers.net.")?,
|
||||
domain: FQDN::COM,
|
||||
ns: FQDN("e.gtld-servers.net.")?,
|
||||
})
|
||||
}
|
||||
|
||||
fn example_soa() -> Result<Soa<'static>> {
|
||||
Ok(Soa {
|
||||
domain: Domain(".")?,
|
||||
ns: Domain("a.root-servers.net.")?,
|
||||
admin: Domain("nstld.verisign-grs.com.")?,
|
||||
domain: FQDN::ROOT,
|
||||
ns: FQDN("a.root-servers.net.")?,
|
||||
admin: FQDN("nstld.verisign-grs.com.")?,
|
||||
settings: SoaSettings::default(),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ mod tests {
|
|||
use crate::{
|
||||
client::{RecordType, Recurse},
|
||||
record::Referral,
|
||||
Client, Domain, NameServer,
|
||||
Client, NameServer, FQDN,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
@ -51,12 +51,12 @@ mod tests {
|
|||
#[test]
|
||||
fn can_resolve() -> Result<()> {
|
||||
let expected_ipv4_addr = Ipv4Addr::new(1, 2, 3, 4);
|
||||
let needle = Domain("example.nameservers.com.")?;
|
||||
let needle = FQDN("example.nameservers.com.")?;
|
||||
|
||||
let mut root_ns = NameServer::new(Domain::ROOT)?;
|
||||
let mut com_ns = NameServer::new(Domain::COM)?;
|
||||
let mut root_ns = NameServer::new(FQDN::ROOT)?;
|
||||
let mut com_ns = NameServer::new(FQDN::COM)?;
|
||||
|
||||
let nameservers_domain = Domain("nameservers.com.")?;
|
||||
let nameservers_domain = FQDN("nameservers.com.")?;
|
||||
let mut nameservers_ns = NameServer::new(nameservers_domain.clone())?;
|
||||
nameservers_ns
|
||||
.a(root_ns.nameserver().clone(), root_ns.ipv4_addr())
|
||||
|
@ -76,7 +76,7 @@ mod tests {
|
|||
eprintln!("com.zone:\n{}", com_ns.zone_file());
|
||||
|
||||
root_ns.referral(&Referral {
|
||||
domain: Domain::COM,
|
||||
domain: FQDN::COM,
|
||||
ipv4_addr: com_ns.ipv4_addr(),
|
||||
ns: com_ns.nameserver().clone(),
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user