Files
tsunagi/crates/tsunagi-core/src/dns/mod.rs
T

134 lines
4.7 KiB
Rust
Raw Normal View History

2026-09-21 16:50:55 +01:00
//! A DNS view of the overlay.
//!
//! The names and addresses of a network, served to the host that runs the
//! agent, so members can be reached by name. Answered from signed state, so a
//! member that is switched off still resolves.
//!
//! Three parts, kept apart on purpose:
//!
//! * [`zone`] decides what the answer is. Pure, and knows nothing about
//! packets or sockets.
//! * [`server`] puts that on the wire.
//! * `publish` tells the operating system where to send its questions,
//! which is the only part that differs between platforms.
2026-09-21 17:05:24 +01:00
pub mod publish;
2026-09-21 16:50:55 +01:00
pub mod server;
pub mod zone;
2026-09-21 17:05:24 +01:00
pub use publish::{DnsPublisher, PublishError, Published};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
2026-09-21 17:05:24 +01:00
/// Where the server listens, one list per address family.
2026-09-21 17:05:24 +01:00
///
/// Loopback, and only loopback. The zones are a view *for the host running
/// the agent*: it is that host's resolver that is pointed at them. Binding
/// an overlay address instead would put them in front of the whole mesh —
/// and an agent in several networks would then answer one network's
/// questions about another's names, which is precisely what separate
/// networks are for.
///
/// Both families, and independently: a question arrives over whichever one
/// the resolver happens to use, and on a host with one of them switched off
/// the other must still answer. So neither list failing says anything about
/// the other, and a listener is opened from each.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListenPlan {
/// IPv4 candidates, best first.
pub v4: Vec<SocketAddr>,
/// IPv6 candidates, best first.
pub v6: Vec<SocketAddr>,
}
impl ListenPlan {
/// The two lists, so a caller can bind one listener from each.
pub fn families(&self) -> [&[SocketAddr]; 2] {
[&self.v4, &self.v6]
}
}
/// Builds the listen plan for one port.
pub fn listen_plan(port: u16) -> ListenPlan {
ListenPlan {
v4: vec![SocketAddr::from((Ipv4Addr::LOCALHOST, port))],
v6: vec![SocketAddr::from((Ipv6Addr::LOCALHOST, port))],
2026-09-21 17:05:24 +01:00
}
}
2026-09-21 16:50:55 +01:00
pub use server::{DnsServer, SharedZone};
pub use zone::{Answer, Query, Zone, ZoneError, ZoneName, Zones};
2026-09-21 17:05:24 +01:00
/// Authoritative zones and the publication diagnostics for one agent snapshot.
pub struct RosterZones {
/// Member and service records indexed by zone.
pub zones: Zones,
/// Zone names in configured-network order.
pub labels: Vec<String>,
/// Unusable or potentially conflicting zone names.
pub warnings: Vec<String>,
}
impl RosterZones {
/// Build the desktop and mobile DNS view from the same signed state.
pub fn from_networks(networks: &[crate::NetworkStatus]) -> Self {
let mut zones = Vec::new();
let mut labels = Vec::new();
let mut warnings = Vec::new();
for network in networks {
let zone = match ZoneName::new(network.name.as_str()) {
Ok(zone) => zone,
Err(err) => {
warnings.push(format!(
"`{}` cannot be a zone, so its members have no names: {err}",
network.name
));
continue;
}
};
if let Some(warning) = zone.collision() {
warnings.push(warning);
}
labels.push(zone.as_str().to_string());
let members = network
.members
.iter()
.filter_map(|member| Some((member.hostname.clone()?, member.overlay_address_v4?)));
let services = network
.vips
.iter()
.filter(|vip| vip.error.is_none())
.filter_map(|vip| Some((vip.config.name.clone(), vip.address?)));
zones.push(Zone::new(zone, members.chain(services)).with_reverse_range(network.range));
}
Self {
zones: Zones::new(zones),
labels,
warnings,
}
}
}
2026-09-21 17:05:24 +01:00
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn both_families_are_planned_for_and_neither_is_reachable_off_this_host() {
let plan = listen_plan(5354);
2026-09-21 17:05:24 +01:00
assert_eq!(
plan.v4,
2026-09-21 17:05:24 +01:00
vec!["127.0.0.1:5354".parse::<SocketAddr>().unwrap()]
);
assert_eq!(plan.v6, vec!["[::1]:5354".parse::<SocketAddr>().unwrap()]);
assert_eq!(plan.families().len(), 2);
// Nothing here is reachable from another member: the zones are a
// view for this host, and an agent in two networks must not answer
// one network's questions about the other's names.
for address in plan.families().concat() {
assert!(address.ip().is_loopback(), "{address} is not loopback");
}
2026-09-21 17:05:24 +01:00
}
}