1#![no_std]
10#![warn(
11 clippy::alloc_instead_of_core,
12 clippy::print_stdout,
13 clippy::std_instead_of_core,
14 clippy::std_instead_of_alloc,
15 missing_docs
16)]
17#![cfg_attr(docsrs, feature(doc_cfg))]
18
19#[cfg(feature = "std")]
22extern crate std;
23
24#[macro_use]
25extern crate alloc;
26
27#[cfg(feature = "access-control")]
28pub mod access_control;
29
30#[cfg(any(feature = "dnssec-aws-lc-rs", feature = "dnssec-ring"))]
31pub mod dnssec;
32
33mod error;
34pub use error::ProtoError;
35
36pub mod op;
37pub mod rr;
38pub mod serialize;
39
40#[cfg(feature = "std")]
41pub(crate) use rand::random;
42
43#[cfg(all(not(feature = "std"), feature = "no-std-rand"))]
44pub(crate) use no_std_rand::random;
45#[cfg(all(not(feature = "std"), feature = "no-std-rand"))]
46pub use no_std_rand::seed;
47
48use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
49
50#[cfg(all(not(feature = "std"), feature = "no-std-rand"))]
52mod no_std_rand {
53 use core::cell::RefCell;
54
55 use critical_section::Mutex;
56 use rand::distr::{Distribution, StandardUniform};
57 use rand::{RngExt, SeedableRng, rngs::StdRng};
58
59 pub(crate) fn random<T>() -> T
65 where
66 StandardUniform: Distribution<T>,
67 {
68 critical_section::with(|cs| {
69 RNG.borrow_ref_mut(cs)
70 .as_mut()
71 .expect("the no_std rng was not seeded using `hickory_proto::seed()`")
72 .random()
73 })
74 }
75
76 pub fn seed(seed: u64) {
78 critical_section::with(|cs| *RNG.borrow_ref_mut(cs) = Some(StdRng::seed_from_u64(seed)));
79 }
80
81 static RNG: Mutex<RefCell<Option<StdRng>>> = Mutex::new(RefCell::new(None));
82
83 #[cfg(test)]
84 mod test {
85 use super::*;
86
87 #[test]
88 fn test_no_std_rand() {
89 seed(0x1337);
91 let _ = random::<u32>();
92 }
93 }
94}
95
96pub const ROOTS: &[IpAddr] = &[
100 IpAddr::V4(Ipv4Addr::new(198, 41, 0, 4)),
102 IpAddr::V6(Ipv6Addr::new(
103 0x2001, 0x503, 0xba3e, 0x0, 0x0, 0x0, 0x2, 0x30,
104 )),
105 IpAddr::V4(Ipv4Addr::new(170, 247, 170, 2)),
107 IpAddr::V6(Ipv6Addr::new(0x2801, 0x1b8, 0x10, 0x0, 0x0, 0x0, 0x0, 0xb)),
108 IpAddr::V4(Ipv4Addr::new(192, 33, 4, 12)),
110 IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x2, 0x0, 0x0, 0x0, 0x0, 0xc)),
111 IpAddr::V4(Ipv4Addr::new(199, 7, 91, 13)),
113 IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x2d, 0x0, 0x0, 0x0, 0x0, 0xd)),
114 IpAddr::V4(Ipv4Addr::new(192, 203, 230, 10)),
116 IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0xa8, 0x0, 0x0, 0x0, 0x0, 0xe)),
117 IpAddr::V4(Ipv4Addr::new(192, 5, 5, 241)),
119 IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x2f, 0x0, 0x0, 0x0, 0x0, 0xf)),
120 IpAddr::V4(Ipv4Addr::new(192, 112, 36, 4)),
122 IpAddr::V6(Ipv6Addr::new(
123 0x2001, 0x500, 0x12, 0x0, 0x0, 0x0, 0x0, 0xd0d,
124 )),
125 IpAddr::V4(Ipv4Addr::new(198, 97, 190, 53)),
127 IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x1, 0x0, 0x0, 0x0, 0x0, 0x53)),
128 IpAddr::V4(Ipv4Addr::new(192, 36, 148, 17)),
130 IpAddr::V6(Ipv6Addr::new(0x2001, 0x7fe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x53)),
131 IpAddr::V4(Ipv4Addr::new(192, 58, 128, 30)),
133 IpAddr::V6(Ipv6Addr::new(
134 0x2001, 0x503, 0xc27, 0x0, 0x0, 0x0, 0x2, 0x30,
135 )),
136 IpAddr::V4(Ipv4Addr::new(193, 0, 14, 129)),
138 IpAddr::V6(Ipv6Addr::new(0x2001, 0x7fd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1)),
139 IpAddr::V4(Ipv4Addr::new(199, 7, 83, 42)),
141 IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x9f, 0x0, 0x0, 0x0, 0x0, 0x42)),
142 IpAddr::V4(Ipv4Addr::new(202, 12, 27, 33)),
144 IpAddr::V6(Ipv6Addr::new(0x2001, 0xdc3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x35)),
145];