Files
hickory-dns/crates/resolver/examples/custom_provider.rs
Benjamin Fry 408d0baca0 Prepare 0.24 release with branding change to Hickory DNS (#2054)
* update all READMEs with notices about the name change

* update changelog for 0.24

* bump crate versions to 0.24

* update version notice information

* update readmes to back reference trust-dns

* rename all crates to hickory counterparts

* replace all Trust-DNS references in code and comments with Hickory DNS

* rename all Trust-DNS references to Hickory DNS in non-code

* rename all trust-dns-resolver references to hickory-resolver

* rename all trust-dns-client references to hickory-client

* rename all trust-dns-proto references to hickory-proto

* rename all trust-dns-server references to hickory-server

* rename all trust-dns-compatibility references to hickory-compatability

* rename all trust-dns-integration references to hickory-integration

* rename all trust-dns-util references to hickory-util

* Update MIT licenses to reference Hickory DNS

* update all trust-dns references to hickory-dns

* update all bluejekyll github references to hickorydns org

* Update name in Changelog

* make sure hickory-dns logs during tests

* add changelogs for recent main additions

* fix references to trust-dns and hickory in architecture

* update a few trust-dns references in READMEs

* fixup some dangling trust_dns references

* replace fka with formerly in change log

* replace all hickoydns org references to hickory-dns

* replace all http links with https

* update logos

* update hickorydns to hickory-dns for all other org references

* fix Notices of Trust-DNS to Hickory in each Readme
2023-10-13 18:39:28 -07:00

110 lines
3.4 KiB
Rust

#![recursion_limit = "128"]
#[cfg(any(feature = "webpki-roots", feature = "native-certs"))]
use {
hickory_resolver::config::{ResolverConfig, ResolverOpts},
hickory_resolver::name_server::{ConnectionProvider, GenericConnector, RuntimeProvider},
hickory_resolver::proto::iocompat::AsyncIoTokioAsStd,
hickory_resolver::proto::TokioTime,
hickory_resolver::{AsyncResolver, TokioHandle},
std::future::Future,
std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
std::pin::Pin,
tokio::net::{TcpStream, UdpSocket},
};
#[cfg(any(feature = "webpki-roots", feature = "native-certs"))]
#[derive(Clone, Default)]
struct PrintProvider {
handle: TokioHandle,
}
#[cfg(any(feature = "webpki-roots", feature = "native-certs"))]
impl RuntimeProvider for PrintProvider {
type Handle = TokioHandle;
type Timer = TokioTime;
type Udp = UdpSocket;
type Tcp = AsyncIoTokioAsStd<TcpStream>;
fn create_handle(&self) -> Self::Handle {
self.handle.clone()
}
fn connect_tcp(
&self,
server_addr: SocketAddr,
) -> Pin<Box<dyn Send + Future<Output = std::io::Result<Self::Tcp>>>> {
println!("Create tcp server_addr: {}", server_addr);
Box::pin(async move {
let tcp = TcpStream::connect(server_addr).await?;
Ok(AsyncIoTokioAsStd(tcp))
})
}
fn bind_udp(
&self,
local_addr: SocketAddr,
server_addr: SocketAddr,
) -> Pin<Box<dyn Send + Future<Output = std::io::Result<Self::Udp>>>> {
// The server_addr parameter is used only when you need to establish a tunnel or something similar.
// For example, you try to use a http proxy and encapsulate UDP packets inside a TCP stream.
println!(
"Create udp local_addr: {}, server_addr: {}",
local_addr, server_addr
);
Box::pin(UdpSocket::bind(local_addr))
}
}
#[cfg(any(feature = "webpki-roots", feature = "native-certs"))]
async fn lookup_test<R: ConnectionProvider>(resolver: AsyncResolver<R>) {
let response = resolver.lookup_ip("www.example.com.").await.unwrap();
// There can be many addresses associated with the name,
// this can return IPv4 and/or IPv6 addresses
let address = response.iter().next().expect("no addresses returned!");
if address.is_ipv4() {
assert_eq!(address, IpAddr::V4(Ipv4Addr::new(93, 184, 216, 34)));
} else {
assert_eq!(
address,
IpAddr::V6(Ipv6Addr::new(
0x2606, 0x2800, 0x220, 0x1, 0x248, 0x1893, 0x25c8, 0x1946
))
);
}
}
#[cfg(any(feature = "webpki-roots", feature = "native-certs"))]
#[tokio::main]
async fn main() {
let resolver = AsyncResolver::new(
ResolverConfig::google(),
ResolverOpts::default(),
GenericConnector::new(PrintProvider::default()),
);
lookup_test(resolver).await;
#[cfg(feature = "dns-over-https-rustls")]
{
let resolver2 = AsyncResolver::new(
ResolverConfig::cloudflare_https(),
ResolverOpts::default(),
GenericConnector::new(PrintProvider::default()),
);
lookup_test(resolver2).await;
}
println!("Hello, world!");
}
#[cfg(not(any(feature = "webpki-roots", feature = "native-certs")))]
fn main() {
println!("either `webpki-roots` or `native-certs` feature must be enabled")
}
#[test]
fn test_custom_provider() {
main()
}