Skip to main content

rings_dns_relay/
config.rs

1//! The relay's configuration: its zones, prefix, ports, listeners and state directory.
2//!
3//! This is the serialized form an operator writes (the node embeds it in `config.yaml`). It is
4//! validated once, by [`DnsRelay::open`](crate::DnsRelay::open), into the typed values the relay
5//! runs on; unknown keys are rejected.
6
7use std::net::IpAddr;
8use std::net::Ipv4Addr;
9use std::net::Ipv6Addr;
10use std::net::SocketAddr;
11use std::path::PathBuf;
12
13use ipnet::Ipv6Net;
14use serde::Deserialize;
15use serde::Serialize;
16
17use crate::ingress::DEFAULT_MAX_CONNECTIONS;
18use crate::ingress::DEFAULT_MAX_CONNECTIONS_PER_OWNER;
19
20/// The port DNS is served on.
21const DNS_PORT: u16 = 53;
22/// Default TTL of positive answers, in seconds.
23const DEFAULT_TTL_SECS: u32 = 60;
24/// Default TTL of negative answers, in seconds.
25const DEFAULT_NEGATIVE_TTL_SECS: u32 = 60;
26/// Default time a binding stays in the address book unanswered, in seconds: eight days, beyond
27/// the grace period, so a remembered owner keeps its address too.
28const DEFAULT_BOOK_RETENTION_SECS: u64 = 8 * 86_400;
29/// Default grace period of a vanished owner, in seconds: seven days, beyond the five days a
30/// mail server commonly retries a delivery before it bounces it.
31const DEFAULT_GRACE_SECS: u64 = 7 * 86_400;
32/// Default freshness window of an owner's last lookup, in seconds.
33const DEFAULT_FRESH_SECS: u64 = 15;
34/// Default bound on the owners the relay keeps an address and a memory for.
35const DEFAULT_MAX_OWNERS: usize = 65_536;
36/// Default bound on the directory lookups in flight.
37const DEFAULT_MAX_LOOKUPS: usize = 64;
38
39/// One zone delegated to the relay.
40#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
41#[serde(deny_unknown_fields)]
42pub struct ZoneConfig {
43    /// The zone's origin, such as `rings.rs` or `p2p.example.com`.
44    pub origin: String,
45    /// The zone's nameservers, as the parent's NS records name them, the primary (which the SOA
46    /// names) first. There is at least one, and they lie outside every relayed zone.
47    pub nameservers: Vec<String>,
48    /// The mailbox the zone's SOA names, in DNS form: `hostmaster.example.com` for
49    /// `hostmaster@example.com`.
50    pub contact: String,
51}
52
53/// The relay's configuration.
54#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
55#[serde(deny_unknown_fields)]
56pub struct DnsRelayConfig {
57    /// The zones delegated to this relay.
58    pub zones: Vec<ZoneConfig>,
59    /// The IPv6 prefix routed to this host, in which every owner gets its own address.
60    pub prefix: Ipv6Net,
61    /// The ports the ingress accepts on, for every address in the prefix.
62    pub ports: Vec<u16>,
63    /// The directory holding the address key, the address book and the memory of
64    /// announcements.
65    pub state_dir: PathBuf,
66    /// The addresses DNS is served on, over UDP and TCP.
67    #[serde(default = "default_dns_listen")]
68    pub dns_listen: Vec<SocketAddr>,
69    /// The IPv6 address the ingress listens on; the wildcard receives the whole prefix.
70    #[serde(default = "default_ingress_listen")]
71    pub ingress_listen: Ipv6Addr,
72    /// TTL of positive answers, in seconds.
73    #[serde(default = "default_ttl_secs")]
74    pub ttl_secs: u32,
75    /// TTL of negative answers, in seconds.
76    #[serde(default = "default_negative_ttl_secs")]
77    pub negative_ttl_secs: u32,
78    /// How long a binding stays in the address book unanswered, in seconds.
79    #[serde(default = "default_book_retention_secs")]
80    pub book_retention_secs: u64,
81    /// How long the relay keeps answering for an owner whose announcement vanished from the
82    /// directory, in seconds.
83    #[serde(default = "default_grace_secs")]
84    pub grace_secs: u64,
85    /// How long an owner's last lookup answers without asking the directory again, in seconds.
86    #[serde(default = "default_fresh_secs")]
87    pub fresh_secs: u64,
88    /// Most owners the relay keeps an address binding and a memory for; at least 1.
89    #[serde(default = "default_max_owners")]
90    pub max_owners: usize,
91    /// Most directory lookups in flight; at least 1. A query that finds them all taken is
92    /// answered from memory, or with `SERVFAIL`.
93    #[serde(default = "default_max_lookups")]
94    pub max_lookups: usize,
95    /// Most connections carried at once.
96    #[serde(default = "default_max_connections")]
97    pub max_connections: usize,
98    /// Most connections carried at once to one owner.
99    #[serde(default = "default_max_connections_per_owner")]
100    pub max_connections_per_owner: usize,
101}
102
103/// DNS on port 53 of every IPv6 and IPv4 address.
104fn default_dns_listen() -> Vec<SocketAddr> {
105    vec![
106        SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), DNS_PORT),
107        SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), DNS_PORT),
108    ]
109}
110
111/// The IPv6 wildcard.
112const fn default_ingress_listen() -> Ipv6Addr {
113    Ipv6Addr::UNSPECIFIED
114}
115
116/// [`DEFAULT_TTL_SECS`].
117const fn default_ttl_secs() -> u32 {
118    DEFAULT_TTL_SECS
119}
120
121/// [`DEFAULT_NEGATIVE_TTL_SECS`].
122const fn default_negative_ttl_secs() -> u32 {
123    DEFAULT_NEGATIVE_TTL_SECS
124}
125
126/// [`DEFAULT_BOOK_RETENTION_SECS`].
127const fn default_book_retention_secs() -> u64 {
128    DEFAULT_BOOK_RETENTION_SECS
129}
130
131/// [`DEFAULT_GRACE_SECS`].
132const fn default_grace_secs() -> u64 {
133    DEFAULT_GRACE_SECS
134}
135
136/// [`DEFAULT_FRESH_SECS`].
137const fn default_fresh_secs() -> u64 {
138    DEFAULT_FRESH_SECS
139}
140
141/// [`DEFAULT_MAX_OWNERS`].
142const fn default_max_owners() -> usize {
143    DEFAULT_MAX_OWNERS
144}
145
146/// [`DEFAULT_MAX_LOOKUPS`].
147const fn default_max_lookups() -> usize {
148    DEFAULT_MAX_LOOKUPS
149}
150
151/// [`DEFAULT_MAX_CONNECTIONS`].
152const fn default_max_connections() -> usize {
153    DEFAULT_MAX_CONNECTIONS
154}
155
156/// [`DEFAULT_MAX_CONNECTIONS_PER_OWNER`].
157const fn default_max_connections_per_owner() -> usize {
158    DEFAULT_MAX_CONNECTIONS_PER_OWNER
159}