Replace lazy_static with once_cell

This commit is contained in:
dAxpeDDa 2023-05-24 17:38:52 +02:00 committed by Dirkjan Ochtman
parent 1d9d50cbe0
commit a10b0b1a64
16 changed files with 475 additions and 305 deletions

8
Cargo.lock generated
View File

@ -1854,7 +1854,7 @@ dependencies = [
"futures",
"futures-channel",
"futures-util",
"lazy_static",
"once_cell",
"openssl",
"radix_trie",
"rand",
@ -1887,7 +1887,7 @@ version = "0.23.0-alpha.2"
dependencies = [
"async-trait",
"futures",
"lazy_static",
"once_cell",
"openssl",
"rand",
"rusqlite",
@ -1923,8 +1923,8 @@ dependencies = [
"idna",
"ipnet",
"js-sys",
"lazy_static",
"native-tls",
"once_cell",
"openssl",
"quinn",
"rand",
@ -1987,8 +1987,8 @@ dependencies = [
"futures-executor",
"futures-util",
"ipconfig",
"lazy_static",
"lru-cache",
"once_cell",
"parking_lot",
"rand",
"resolv-conf",

View File

@ -86,7 +86,7 @@ idna = "0.3.0"
ipconfig = "0.3.0"
ipnet = "2.3.0"
js-sys = "0.3.44"
lazy_static = "1.2.0"
once_cell = "1.17.1"
libfuzzer-sys = "0.4"
lru-cache = "0.1.2"
pin-utils = "0.1.0"

View File

@ -67,7 +67,7 @@ cfg-if.workspace = true
data-encoding.workspace = true
futures-channel = { workspace = true, default-features = false, features = ["std"] }
futures-util = { workspace = true, default-features = false, features = ["std"] }
lazy_static.workspace = true
once_cell.workspace = true
openssl = { workspace = true, features = ["v102", "v110"], optional = true }
radix_trie.workspace = true
rand.workspace = true

View File

@ -4,7 +4,7 @@ pub use crate::proto::rr::domain::usage::*;
use crate::proto::rr::domain::{Label, Name};
use crate::proto::serialize::binary::BinEncodable;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use radix_trie::{Trie, TrieKey};
// Reserved reverse IPs
@ -25,48 +25,178 @@ use radix_trie::{Trie, TrieKey};
// 19.172.in-addr.arpa. 24.172.in-addr.arpa. 31.172.in-addr.arpa.
// 20.172.in-addr.arpa. 25.172.in-addr.arpa. 168.192.in-addr.arpa.
// ```
lazy_static! {
/// 10.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_10: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("10").unwrap().append_domain(&IN_ADDR_ARPA).unwrap());
static ref IN_ADDR_ARPA_172: Name = Name::from_ascii("172").unwrap().append_domain(&IN_ADDR_ARPA).unwrap();
/// 10.in-addr.arpa. usage
pub static IN_ADDR_ARPA_10: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("10")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap(),
)
});
/// 16.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_16: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("16").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 17.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_17: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("17").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 18.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_18: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("18").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 19.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_19: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("19").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 20.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_20: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("20").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 21.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_21: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("21").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 22.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_22: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("22").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 23.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_23: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("23").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 24.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_24: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("24").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 25.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_25: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("25").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 26.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_26: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("26").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 27.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_27: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("27").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 28.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_28: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("28").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 29.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_29: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("29").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 30.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_30: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("30").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 31.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_31: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("31").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
static IN_ADDR_ARPA_172: Lazy<Name> = Lazy::new(|| {
Name::from_ascii("172")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap()
});
/// 168.192.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_192_168: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("168.192").unwrap().append_domain(&IN_ADDR_ARPA).unwrap());
}
/// 16.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_16: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("16")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 17.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_17: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("17")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 18.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_18: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("18")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 19.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_19: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("19")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 20.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_20: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("20")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 21.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_21: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("21")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 22.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_22: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("22")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 23.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_23: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("23")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 24.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_24: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("24")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 25.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_25: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("25")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 26.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_26: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("26")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 27.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_27: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("27")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 28.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_28: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("28")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 29.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_29: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("29")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 30.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_30: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("30")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 31.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_31: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("31")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 168.192.in-addr.arpa. usage
pub static IN_ADDR_ARPA_192_168: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("168.192")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap(),
)
});
// example., example.com., example.net., and example.org.
//
@ -79,21 +209,27 @@ lazy_static! {
// "example.org.", and any names falling within those domains, are
// special in the following ways:
// ```
lazy_static! {
static ref COM: Label = Label::from_ascii("com").unwrap();
static ref NET: Label = Label::from_ascii("net").unwrap();
static ref ORG: Label = Label::from_ascii("org").unwrap();
static ref EXAMPLE_L: Label = Label::from_ascii("example").unwrap();
/// example. usage
pub static ref EXAMPLE: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone()]).unwrap());
/// example.com. usage
pub static ref EXAMPLE_COM: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), COM.clone()]).unwrap());
/// example.com. usage
pub static ref EXAMPLE_NET: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), NET.clone()]).unwrap());
/// example.com. usage
pub static ref EXAMPLE_ORG: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), ORG.clone()]).unwrap());
}
static COM: Lazy<Label> = Lazy::new(|| Label::from_ascii("com").unwrap());
static NET: Lazy<Label> = Lazy::new(|| Label::from_ascii("net").unwrap());
static ORG: Lazy<Label> = Lazy::new(|| Label::from_ascii("org").unwrap());
static EXAMPLE_L: Lazy<Label> = Lazy::new(|| Label::from_ascii("example").unwrap());
/// example. usage
pub static EXAMPLE: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone()]).unwrap()));
/// example.com. usage
pub static EXAMPLE_COM: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), COM.clone()]).unwrap())
});
/// example.com. usage
pub static EXAMPLE_NET: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), NET.clone()]).unwrap())
});
/// example.com. usage
pub static EXAMPLE_ORG: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), ORG.clone()]).unwrap())
});
// test.
//
@ -105,10 +241,10 @@ lazy_static! {
// The domain "test.", and any names falling within ".test.", are
// special in the following ways:
// ```
lazy_static! {
/// test. usage
pub static ref TEST: ZoneUsage = ZoneUsage::test(Name::from_ascii("test.").unwrap());
}
/// test. usage
pub static TEST: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::test(Name::from_ascii("test.").unwrap()));
#[derive(Clone, Eq, PartialEq)]
struct TrieName(Name);
@ -258,10 +394,8 @@ impl UsageTrie {
}
}
lazy_static! {
/// All default usage mappings
pub static ref USAGE: UsageTrie = UsageTrie::default();
}
/// All default usage mappings
pub static USAGE: Lazy<UsageTrie> = Lazy::new(UsageTrie::default);
#[cfg(test)]
mod tests {

View File

@ -81,8 +81,8 @@ http = { workspace = true, optional = true }
idna.workspace = true
ipnet.workspace = true
js-sys = { workspace = true, optional = true }
lazy_static.workspace = true
native-tls = { workspace = true, optional = true }
once_cell.workspace = true
openssl = { workspace = true, features = ["v102", "v110"], optional = true }
quinn = { workspace = true, optional = true, features = ["log", "runtime-tokio", "tls-rustls"] }
rand.workspace = true

View File

@ -16,7 +16,7 @@ use std::{fmt, io, sync};
pub use backtrace::Backtrace as ExtBacktrace;
use enum_as_inner::EnumAsInner;
#[cfg(feature = "backtrace")]
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use thiserror::Error;
use crate::op::Header;
@ -26,16 +26,14 @@ use crate::rr::dnssec::rdata::tsig::TsigAlgorithm;
use crate::rr::{Name, RecordType};
use crate::serialize::binary::DecodeError;
/// Boolean for checking if backtrace is enabled at runtime
#[cfg(feature = "backtrace")]
#[cfg_attr(docsrs, doc(cfg(feature = "backtrace")))]
lazy_static! {
/// Boolean for checking if backtrace is enabled at runtime
pub static ref ENABLE_BACKTRACE: bool = {
use std::env;
let bt = env::var("RUST_BACKTRACE");
matches!(bt.as_ref().map(|s| s as &str), Ok("full") | Ok("1"))
};
}
pub static ENABLE_BACKTRACE: Lazy<bool> = Lazy::new(|| {
use std::env;
let bt = env::var("RUST_BACKTRACE");
matches!(bt.as_ref().map(|s| s as &str), Ok("full") | Ok("1"))
});
/// Generate a backtrace
///

View File

@ -14,7 +14,7 @@ use std::task::{Context, Poll};
use futures_util::stream::{Stream, StreamExt};
use futures_util::{future, future::Future, ready, FutureExt, TryFutureExt};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use rand;
use rand::distributions::{uniform::Uniform, Distribution};
use socket2::{self, Socket};
@ -27,12 +27,16 @@ use crate::xfer::SerialMessage;
use crate::BufDnsStreamHandle;
pub(crate) const MDNS_PORT: u16 = 5353;
lazy_static! {
/// mDNS ipv4 address https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml
pub static ref MDNS_IPV4: SocketAddr = SocketAddr::new(Ipv4Addr::new(224,0,0,251).into(), MDNS_PORT);
/// link-local mDNS ipv6 address https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
pub static ref MDNS_IPV6: SocketAddr = SocketAddr::new(Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0x00FB).into(), MDNS_PORT);
}
/// mDNS ipv4 address https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml
pub static MDNS_IPV4: Lazy<SocketAddr> =
Lazy::new(|| SocketAddr::new(Ipv4Addr::new(224, 0, 0, 251).into(), MDNS_PORT));
/// link-local mDNS ipv6 address https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
pub static MDNS_IPV6: Lazy<SocketAddr> = Lazy::new(|| {
SocketAddr::new(
Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0x00FB).into(),
MDNS_PORT,
)
});
/// A UDP stream of DNS binary packets
#[must_use = "futures do nothing unless polled"]
@ -430,12 +434,11 @@ pub(crate) mod tests {
// TODO: is there a better way?
const BASE_TEST_PORT: u16 = 5379;
lazy_static! {
/// 250 appears to be unused/unregistered
static ref TEST_MDNS_IPV4: IpAddr = Ipv4Addr::new(224,0,0,250).into();
/// FA appears to be unused/unregistered
static ref TEST_MDNS_IPV6: IpAddr = Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0x00FA).into();
}
/// 250 appears to be unused/unregistered
static TEST_MDNS_IPV4: Lazy<IpAddr> = Lazy::new(|| Ipv4Addr::new(224, 0, 0, 250).into());
/// FA appears to be unused/unregistered
static TEST_MDNS_IPV6: Lazy<IpAddr> =
Lazy::new(|| Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0x00FA).into());
// one_shot tests are basically clones from the udp tests
#[test]

View File

@ -11,130 +11,173 @@
use std::ops::Deref;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use crate::rr::domain::Name;
lazy_static! {
/// Default Name usage, everything is normal...
pub static ref DEFAULT: ZoneUsage = ZoneUsage::default();
}
/// Default Name usage, everything is normal...
pub static DEFAULT: Lazy<ZoneUsage> = Lazy::new(ZoneUsage::default);
lazy_static! {
static ref ARPA: Name = Name::from_ascii("arpa.").unwrap();
/// zone for ipv4 reverse addresses
pub static ref IN_ADDR_ARPA: Name = Name::from_ascii("in-addr").unwrap().append_domain(&ARPA).unwrap();
/// zone for ipv6 reverse addresses
pub static ref IP6_ARPA: Name = Name::from_ascii("ip6").unwrap().append_domain(&ARPA).unwrap();
}
static ARPA: Lazy<Name> = Lazy::new(|| Name::from_ascii("arpa.").unwrap());
/// zone for ipv4 reverse addresses
pub static IN_ADDR_ARPA: Lazy<Name> = Lazy::new(|| {
Name::from_ascii("in-addr")
.unwrap()
.append_domain(&ARPA)
.unwrap()
});
/// zone for ipv6 reverse addresses
pub static IP6_ARPA: Lazy<Name> = Lazy::new(|| {
Name::from_ascii("ip6")
.unwrap()
.append_domain(&ARPA)
.unwrap()
});
lazy_static! {
/// localhost.
///
/// [Special-Use Domain Names](https://tools.ietf.org/html/rfc6761), RFC 6761 February, 2013
///
/// ```text
/// 6.3. Domain Name Reservation Considerations for "localhost."
///
/// The domain "localhost." and any names falling within ".localhost."
/// are special in the following ways:
/// ```
/// localhost.
///
/// [Special-Use Domain Names](https://tools.ietf.org/html/rfc6761), RFC 6761 February, 2013
///
/// ```text
/// 6.3. Domain Name Reservation Considerations for "localhost."
///
/// The domain "localhost." and any names falling within ".localhost."
/// are special in the following ways:
/// ```
/// localhost. usage
pub static ref LOCALHOST: ZoneUsage = ZoneUsage::localhost(Name::from_ascii("localhost.").unwrap());
/// localhost. usage
pub static LOCALHOST: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::localhost(Name::from_ascii("localhost.").unwrap()));
/// 127.in-addr.arpa. usage; 127/8 is reserved for loopback
pub static ref IN_ADDR_ARPA_127: ZoneUsage = ZoneUsage::localhost(Name::from_ascii("127").unwrap().append_domain(&IN_ADDR_ARPA).unwrap());
/// 127.in-addr.arpa. usage; 127/8 is reserved for loopback
pub static IN_ADDR_ARPA_127: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::localhost(
Name::from_ascii("127")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap(),
)
});
/// 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. usage; 1/128 is the only address in ipv6 loopback
pub static ref IP6_ARPA_1: ZoneUsage = ZoneUsage::localhost(Name::from_ascii("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0").unwrap().append_domain(&IP6_ARPA).unwrap());
}
/// 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. usage; 1/128 is the only address in ipv6 loopback
pub static IP6_ARPA_1: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::localhost(
Name::from_ascii("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0")
.unwrap()
.append_domain(&IP6_ARPA)
.unwrap(),
)
});
lazy_static! {
/// .local.
///
/// [Multicast DNS](https://tools.ietf.org/html/rfc6762), RFC 6762 February 2013
///
/// ```text
/// This document specifies that the DNS top-level domain ".local." is a
/// special domain with special semantics, namely that any fully
/// qualified name ending in ".local." is link-local, and names within
/// this domain are meaningful only on the link where they originate.
/// This is analogous to IPv4 addresses in the 169.254/16 prefix or IPv6
/// addresses in the FE80::/10 prefix, which are link-local and
/// meaningful only on the link where they originate.
/// ```
/// .local.
///
/// [Multicast DNS](https://tools.ietf.org/html/rfc6762), RFC 6762 February 2013
///
/// ```text
/// This document specifies that the DNS top-level domain ".local." is a
/// special domain with special semantics, namely that any fully
/// qualified name ending in ".local." is link-local, and names within
/// this domain are meaningful only on the link where they originate.
/// This is analogous to IPv4 addresses in the 169.254/16 prefix or IPv6
/// addresses in the FE80::/10 prefix, which are link-local and
/// meaningful only on the link where they originate.
/// ```
/// localhost. usage
pub static ref LOCAL: ZoneUsage = ZoneUsage::local(Name::from_ascii("local.").unwrap());
/// localhost. usage
pub static LOCAL: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::local(Name::from_ascii("local.").unwrap()));
// RFC 6762 Multicast DNS February 2013
// RFC 6762 Multicast DNS February 2013
// Any DNS query for a name ending with "254.169.in-addr.arpa." MUST
// be sent to the mDNS IPv4 link-local multicast address 224.0.0.251
// or the mDNS IPv6 multicast address FF02::FB. Since names under
// this domain correspond to IPv4 link-local addresses, it is logical
// that the local link is the best place to find information
// pertaining to those names.
//
// Likewise, any DNS query for a name within the reverse mapping
// domains for IPv6 link-local addresses ("8.e.f.ip6.arpa.",
// "9.e.f.ip6.arpa.", "a.e.f.ip6.arpa.", and "b.e.f.ip6.arpa.") MUST
// be sent to the mDNS IPv6 link-local multicast address FF02::FB or
// the mDNS IPv4 link-local multicast address 224.0.0.251.
// Any DNS query for a name ending with "254.169.in-addr.arpa." MUST
// be sent to the mDNS IPv4 link-local multicast address 224.0.0.251
// or the mDNS IPv6 multicast address FF02::FB. Since names under
// this domain correspond to IPv4 link-local addresses, it is logical
// that the local link is the best place to find information
// pertaining to those names.
//
// Likewise, any DNS query for a name within the reverse mapping
// domains for IPv6 link-local addresses ("8.e.f.ip6.arpa.",
// "9.e.f.ip6.arpa.", "a.e.f.ip6.arpa.", and "b.e.f.ip6.arpa.") MUST
// be sent to the mDNS IPv6 link-local multicast address FF02::FB or
// the mDNS IPv4 link-local multicast address 224.0.0.251.
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static ref IN_ADDR_ARPA_169_254: ZoneUsage = ZoneUsage::local(Name::from_ascii("254.169").unwrap().append_domain(&IN_ADDR_ARPA).unwrap());
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static IN_ADDR_ARPA_169_254: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::local(
Name::from_ascii("254.169")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap(),
)
});
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static ref IP6_ARPA_FE_8: ZoneUsage = ZoneUsage::local(Name::from_ascii("8.e.f").unwrap().append_domain(&IP6_ARPA).unwrap());
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static ref IP6_ARPA_FE_9: ZoneUsage = ZoneUsage::local(Name::from_ascii("9.e.f").unwrap().append_domain(&IP6_ARPA).unwrap());
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static ref IP6_ARPA_FE_B: ZoneUsage = ZoneUsage::local(Name::from_ascii("b.e.f").unwrap().append_domain(&IP6_ARPA).unwrap());
}
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static IP6_ARPA_FE_8: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::local(
Name::from_ascii("8.e.f")
.unwrap()
.append_domain(&IP6_ARPA)
.unwrap(),
)
});
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static IP6_ARPA_FE_9: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::local(
Name::from_ascii("9.e.f")
.unwrap()
.append_domain(&IP6_ARPA)
.unwrap(),
)
});
/// 254.169.in-addr.arpa. usage link-local, i.e. mDNS
pub static IP6_ARPA_FE_B: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::local(
Name::from_ascii("b.e.f")
.unwrap()
.append_domain(&IP6_ARPA)
.unwrap(),
)
});
lazy_static! {
/// invalid.
///
/// [Special-Use Domain Names](https://tools.ietf.org/html/rfc6761), RFC 6761 February, 2013
///
/// ```text
/// 6.4. Domain Name Reservation Considerations for "invalid."
///
/// The domain "invalid." and any names falling within ".invalid." are
/// special in the ways listed below. In the text below, the term
/// "invalid" is used in quotes to signify such names, as opposed to
/// names that may be invalid for other reasons (e.g., being too long).
/// ```
/// invalid.
///
/// [Special-Use Domain Names](https://tools.ietf.org/html/rfc6761), RFC 6761 February, 2013
///
/// ```text
/// 6.4. Domain Name Reservation Considerations for "invalid."
///
/// The domain "invalid." and any names falling within ".invalid." are
/// special in the ways listed below. In the text below, the term
/// "invalid" is used in quotes to signify such names, as opposed to
/// names that may be invalid for other reasons (e.g., being too long).
/// ```
/// invalid. name usage
pub static ref INVALID: ZoneUsage = ZoneUsage::invalid(Name::from_ascii("invalid.").unwrap());
}
/// invalid. name usage
pub static INVALID: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::invalid(Name::from_ascii("invalid.").unwrap()));
lazy_static! {
/// invalid.
///
/// [The ".onion" Special-Use Domain Name](https://tools.ietf.org/html/rfc7686), RFC 7686 October, 2015
///
/// ```text
/// 1. Introduction
///
/// The Tor network has the ability to host network
/// services using the ".onion" Special-Use Top-Level Domain Name. Such
/// names can be used as other domain names would be (e.g., in URLs
/// [RFC3986]), but instead of using the DNS infrastructure, .onion names
/// functionally correspond to the identity of a given service, thereby
/// combining location and authentication.
/// ```
/// invalid.
///
/// [The ".onion" Special-Use Domain Name](https://tools.ietf.org/html/rfc7686), RFC 7686 October, 2015
///
/// ```text
/// 1. Introduction
///
/// The Tor network has the ability to host network
/// services using the ".onion" Special-Use Top-Level Domain Name. Such
/// names can be used as other domain names would be (e.g., in URLs
/// [RFC3986]), but instead of using the DNS infrastructure, .onion names
/// functionally correspond to the identity of a given service, thereby
/// combining location and authentication.
/// ```
/// onion. name usage
pub static ref ONION: ZoneUsage = ZoneUsage {
user: UserUsage::Normal, // the domain is special, but this is what seems to match the most
app: AppUsage::Normal, // the domain is special, but this is what seems to match the most
.. ZoneUsage::invalid(Name::from_ascii("onion.").unwrap())
};
}
/// onion. name usage
pub static ONION: Lazy<ZoneUsage> = Lazy::new(|| ZoneUsage {
user: UserUsage::Normal, // the domain is special, but this is what seems to match the most
app: AppUsage::Normal, // the domain is special, but this is what seems to match the most
..ZoneUsage::invalid(Name::from_ascii("onion.").unwrap())
});
/// Users:
///

View File

@ -14,7 +14,7 @@ use std::fmt;
use serde::{Deserialize, Serialize};
use data_encoding::{Encoding, Specification};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use crate::{
error::{ProtoError, ProtoResult},
@ -22,17 +22,15 @@ use crate::{
serialize::binary::{BinDecoder, BinEncodable, BinEncoder, Restrict, RestrictedMath},
};
lazy_static! {
/// HEX formatting specific to TLSA and SSHFP encodings
pub static ref HEX: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789abcdef");
spec.ignore.push_str(" \t\r\n");
spec.translate.from.push_str("ABCDEF");
spec.translate.to.push_str("abcdef");
spec.encoding().expect("error in sshfp HEX encoding")
};
}
/// HEX formatting specific to TLSA and SSHFP encodings
pub static HEX: Lazy<Encoding> = Lazy::new(|| {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789abcdef");
spec.ignore.push_str(" \t\r\n");
spec.translate.from.push_str("ABCDEF");
spec.translate.to.push_str("abcdef");
spec.encoding().expect("error in sshfp HEX encoding")
});
/// [RFC 4255](https://tools.ietf.org/html/rfc4255#section-3.1)
///

View File

@ -66,8 +66,8 @@ path = "src/lib.rs"
#backtrace = { version = "0.3.50", optional = true }
cfg-if.workspace = true
futures-util = { workspace = true, default-features = false, features = ["std"] }
lazy_static.workspace = true
lru-cache.workspace = true
once_cell.workspace = true
parking_lot.workspace = true
rand.workspace = true
resolv-conf = { workspace = true, optional = true, features = ["system"] }

View File

@ -3,7 +3,7 @@
#[cfg(all(feature = "tokio-runtime", feature = "system-config"))]
use {
futures_util::future,
lazy_static::lazy_static,
once_cell::sync::Lazy,
std::fmt::Display,
std::io,
std::net::SocketAddr,
@ -21,71 +21,71 @@ use {
// TODO: this example can probably be made much simpler with the new
// `AsyncResolver`.
#[cfg(all(feature = "tokio-runtime", feature = "system-config"))]
lazy_static! {
// First we need to setup the global Resolver
static ref GLOBAL_DNS_RESOLVER: TokioAsyncResolver = {
use std::sync::{Arc, Mutex, Condvar};
use std::thread;
// First we need to setup the global Resolver
static GLOBAL_DNS_RESOLVER: Lazy<TokioAsyncResolver> = Lazy::new(|| {
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
// We'll be using this condvar to get the Resolver from the thread...
let pair = Arc::new((Mutex::new(None::<TokioAsyncResolver>), Condvar::new()));
let pair2 = pair.clone();
// We'll be using this condvar to get the Resolver from the thread...
let pair = Arc::new((Mutex::new(None::<TokioAsyncResolver>), Condvar::new()));
let pair2 = pair.clone();
// Spawn the runtime to a new thread...
//
// This thread will manage the actual resolution runtime
thread::spawn(move || {
// A runtime for this new thread
let runtime = tokio::runtime::Runtime::new().expect("failed to launch Runtime");
// Spawn the runtime to a new thread...
//
// This thread will manage the actual resolution runtime
thread::spawn(move || {
// A runtime for this new thread
let runtime = tokio::runtime::Runtime::new().expect("failed to launch Runtime");
// our platform independent future, result, see next blocks
let resolver = {
// To make this independent, if targeting macOS, BSD, Linux, or Windows, we can use the system's configuration:
#[cfg(any(unix, windows))]
{
// use the system resolver configuration
TokioAsyncResolver::from_system_conf(TokioRuntimeProvider::new())
}
// our platform independent future, result, see next blocks
let resolver = {
// For other operating systems, we can use one of the preconfigured definitions
#[cfg(not(any(unix, windows)))]
{
// Directly reference the config types
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
// To make this independent, if targeting macOS, BSD, Linux, or Windows, we can use the system's configuration:
#[cfg(any(unix, windows))]
{
// use the system resolver configuration
TokioAsyncResolver::from_system_conf(TokioRuntimeProvider::new())
}
// Get a new resolver with the google nameservers as the upstream recursive resolvers
TokioAsyncResolver::new(
ResolverConfig::google(),
ResolverOpts::default(),
runtime.handle().clone(),
)
}
};
// For other operating systems, we can use one of the preconfigured definitions
#[cfg(not(any(unix, windows)))]
{
// Directly reference the config types
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
let (lock, cvar) = &*pair2;
let mut started = lock.lock().unwrap();
// Get a new resolver with the google nameservers as the upstream recursive resolvers
TokioAsyncResolver::new(ResolverConfig::google(), ResolverOpts::default(), runtime.handle().clone())
}
};
let resolver = resolver.expect("failed to create trust-dns-resolver");
let (lock, cvar) = &*pair2;
let mut started = lock.lock().unwrap();
*started = Some(resolver);
cvar.notify_one();
drop(started);
let resolver = resolver.expect("failed to create trust-dns-resolver");
runtime.block_on(future::poll_fn(|_cx| Poll::<()>::Pending))
});
*started = Some(resolver);
cvar.notify_one();
drop(started);
// Wait for the thread to start up.
let (lock, cvar) = &*pair;
let mut resolver = lock.lock().unwrap();
while resolver.is_none() {
resolver = cvar.wait(resolver).unwrap();
}
runtime.block_on(future::poll_fn(|_cx| Poll::<()>::Pending))
});
// take the started resolver
let resolver = std::mem::replace(&mut *resolver, None);
// Wait for the thread to start up.
let (lock, cvar) = &*pair;
let mut resolver = lock.lock().unwrap();
while resolver.is_none() {
resolver = cvar.wait(resolver).unwrap();
}
// take the started resolver
let resolver = std::mem::replace(&mut *resolver, None);
// set the global resolver
resolver.expect("resolver should not be none")
};
}
// set the global resolver
resolver.expect("resolver should not be none")
});
/// Provide a general purpose resolution function.
///

View File

@ -19,6 +19,7 @@ use std::{
};
use futures_util::future::Future;
use once_cell::sync::Lazy;
use crate::{
dns_lru::{self, DnsLru, TtlConfig},
@ -42,11 +43,10 @@ use crate::{
const MAX_QUERY_DEPTH: u8 = 8; // arbitrarily chosen number...
lazy_static! {
static ref LOCALHOST: RData = RData::PTR(PTR(Name::from_ascii("localhost.").unwrap()));
static ref LOCALHOST_V4: RData = RData::A(A::new(127, 0, 0, 1));
static ref LOCALHOST_V6: RData = RData::AAAA(AAAA::new(0, 0, 0, 0, 0, 0, 0, 1));
}
static LOCALHOST: Lazy<RData> =
Lazy::new(|| RData::PTR(PTR(Name::from_ascii("localhost.").unwrap())));
static LOCALHOST_V4: Lazy<RData> = Lazy::new(|| RData::A(A::new(127, 0, 0, 1)));
static LOCALHOST_V6: Lazy<RData> = Lazy::new(|| RData::AAAA(AAAA::new(0, 0, 0, 0, 0, 0, 0, 1)));
struct DepthTracker {
query_depth: Arc<AtomicU8>,

View File

@ -248,8 +248,6 @@
#[cfg(feature = "dns-over-tls")]
#[macro_use]
extern crate cfg_if;
#[macro_use]
extern crate lazy_static;
#[cfg(feature = "serde-config")]
#[macro_use]
extern crate serde;

View File

@ -14,6 +14,7 @@ use std::pin::Pin;
use std::sync::Arc;
use futures_util::future::Future;
use once_cell::sync::Lazy;
use rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};
use proto::error::ProtoError;
@ -26,34 +27,32 @@ use crate::config::TlsClientConfig;
const ALPN_H2: &[u8] = b"h2";
lazy_static! {
// using the mozilla default root store
pub(crate) static ref CLIENT_CONFIG: Arc<ClientConfig> = {
let mut root_store = RootCertStore::empty();
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
// using the mozilla default root store
pub(crate) static CLIENT_CONFIG: Lazy<Arc<ClientConfig>> = Lazy::new(|| {
let mut root_store = RootCertStore::empty();
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let mut client_config = ClientConfig::builder()
.with_safe_default_cipher_suites()
.with_safe_default_kx_groups()
.with_safe_default_protocol_versions()
.unwrap()
.with_root_certificates(root_store)
.with_no_client_auth();
let mut client_config = ClientConfig::builder()
.with_safe_default_cipher_suites()
.with_safe_default_kx_groups()
.with_safe_default_protocol_versions()
.unwrap()
.with_root_certificates(root_store)
.with_no_client_auth();
// The port (853) of DOT is for dns dedicated, SNI is unnecessary. (ISP block by the SNI name)
client_config.enable_sni = false;
// The port (853) of DOT is for dns dedicated, SNI is unnecessary. (ISP block by the SNI name)
client_config.enable_sni = false;
client_config.alpn_protocols.push(ALPN_H2.to_vec());
client_config.alpn_protocols.push(ALPN_H2.to_vec());
Arc::new(client_config)
};
}
Arc::new(client_config)
});
#[allow(clippy::type_complexity)]
pub(crate) fn new_tls_stream_with_future<S, F>(

View File

@ -66,8 +66,8 @@ sqlite = ["rusqlite", "trust-dns-server/sqlite"]
[dependencies]
async-trait.workspace = true
lazy_static.workspace = true
futures.workspace = true
once_cell.workspace = true
openssl = { workspace = true, features = ["v102", "v110"] }
rand.workspace = true
rusqlite = { workspace = true, features = ["bundled"], optional = true }

View File

@ -1,8 +1,5 @@
#![cfg(feature = "mdns")]
#[macro_use]
extern crate lazy_static;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Barrier};
@ -11,6 +8,7 @@ use std::time::Duration;
use futures::future::Either;
use futures::{future, StreamExt};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
use trust_dns_client::client::{AsyncClient, ClientHandle};
@ -23,12 +21,11 @@ use trust_dns_proto::xfer::SerialMessage;
const MDNS_PORT: u16 = 5363;
lazy_static! {
/// 250 appears to be unused/unregistered
static ref TEST_MDNS_IPV4: IpAddr = Ipv4Addr::new(224,0,0,249).into();
/// FA appears to be unused/unregistered
static ref TEST_MDNS_IPV6: IpAddr = Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0x00F9).into();
}
/// 250 appears to be unused/unregistered
static TEST_MDNS_IPV4: Lazy<IpAddr> = Lazy::new(|| Ipv4Addr::new(224, 0, 0, 249).into());
/// FA appears to be unused/unregistered
static TEST_MDNS_IPV6: Lazy<IpAddr> =
Lazy::new(|| Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0x00F9).into());
fn mdns_responsder(
test_name: &'static str,