Skip to main content

hickory_proto/
lib.rs

1// Copyright 2015-2017 Benjamin Fry <benjaminfry@me.com>
2// Copyright 2017 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
5// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
6// https://opensource.org/licenses/MIT>, at your option. This file may not be
7// copied, modified, or distributed except according to those terms.
8
9#![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//! Hickory DNS Protocol library
20
21#[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/// A simple shim that allows us to use a [`random`] in `no_std` environments.
51#[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    /// Generates a random value on `no_std`.
60    ///
61    /// # Panics
62    /// This function will panic if the rng has not been seeded.
63    /// The rng needs to be seeded using [`crate::seed`] before it can be used!
64    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    /// Seed the rng that is used to create random DNS IDs throughout the lib (no_std-only).
77    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            // In practice, the seed needs to be a secure random number.
90            seed(0x1337);
91            let _ = random::<u32>();
92        }
93    }
94}
95
96/// Authoritative DNS root servers.
97///
98/// <https://www.iana.org/domains/root/servers>
99pub const ROOTS: &[IpAddr] = &[
100    // a.root-servers.net
101    IpAddr::V4(Ipv4Addr::new(198, 41, 0, 4)),
102    IpAddr::V6(Ipv6Addr::new(
103        0x2001, 0x503, 0xba3e, 0x0, 0x0, 0x0, 0x2, 0x30,
104    )),
105    // b.root-servers.net
106    IpAddr::V4(Ipv4Addr::new(170, 247, 170, 2)),
107    IpAddr::V6(Ipv6Addr::new(0x2801, 0x1b8, 0x10, 0x0, 0x0, 0x0, 0x0, 0xb)),
108    // c.root-servers.net
109    IpAddr::V4(Ipv4Addr::new(192, 33, 4, 12)),
110    IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x2, 0x0, 0x0, 0x0, 0x0, 0xc)),
111    // d.root-servers.net
112    IpAddr::V4(Ipv4Addr::new(199, 7, 91, 13)),
113    IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x2d, 0x0, 0x0, 0x0, 0x0, 0xd)),
114    // e.root-servers.net
115    IpAddr::V4(Ipv4Addr::new(192, 203, 230, 10)),
116    IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0xa8, 0x0, 0x0, 0x0, 0x0, 0xe)),
117    // f.root-servers.net
118    IpAddr::V4(Ipv4Addr::new(192, 5, 5, 241)),
119    IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x2f, 0x0, 0x0, 0x0, 0x0, 0xf)),
120    // g.root-servers.net
121    IpAddr::V4(Ipv4Addr::new(192, 112, 36, 4)),
122    IpAddr::V6(Ipv6Addr::new(
123        0x2001, 0x500, 0x12, 0x0, 0x0, 0x0, 0x0, 0xd0d,
124    )),
125    // h.root-servers.net
126    IpAddr::V4(Ipv4Addr::new(198, 97, 190, 53)),
127    IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x1, 0x0, 0x0, 0x0, 0x0, 0x53)),
128    // i.root-servers.net
129    IpAddr::V4(Ipv4Addr::new(192, 36, 148, 17)),
130    IpAddr::V6(Ipv6Addr::new(0x2001, 0x7fe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x53)),
131    // j.root-servers.net
132    IpAddr::V4(Ipv4Addr::new(192, 58, 128, 30)),
133    IpAddr::V6(Ipv6Addr::new(
134        0x2001, 0x503, 0xc27, 0x0, 0x0, 0x0, 0x2, 0x30,
135    )),
136    // k.root-servers.net
137    IpAddr::V4(Ipv4Addr::new(193, 0, 14, 129)),
138    IpAddr::V6(Ipv6Addr::new(0x2001, 0x7fd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1)),
139    // l.root-servers.net
140    IpAddr::V4(Ipv4Addr::new(199, 7, 83, 42)),
141    IpAddr::V6(Ipv6Addr::new(0x2001, 0x500, 0x9f, 0x0, 0x0, 0x0, 0x0, 0x42)),
142    // m.root-servers.net
143    IpAddr::V4(Ipv4Addr::new(202, 12, 27, 33)),
144    IpAddr::V6(Ipv6Addr::new(0x2001, 0xdc3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x35)),
145];