Skip to content

Instantly share code, notes, and snippets.

@kallydev
Last active May 7, 2024 04:34
Show Gist options
  • Save kallydev/57c73c85751f681332e63371d0f40954 to your computer and use it in GitHub Desktop.
Save kallydev/57c73c85751f681332e63371d0f40954 to your computer and use it in GitHub Desktop.
Hickory DNS UDP client.
use std::str::FromStr;
use hickory_client::client::{AsyncClient, ClientHandle};
use hickory_client::rr::{DNSClass, Name, RecordType};
use hickory_client::udp::UdpClientStream;
use tokio::net::UdpSocket;
#[tokio::test]
async fn test() -> anyhow::Result<()> {
let dns_socket_addr = ([223, 5, 5, 5], 53).into();
let udp_client_stream = UdpClientStream::<UdpSocket>::new(dns_socket_addr);
let (mut dns_client, dns_exchange_background) = AsyncClient::connect(udp_client_stream).await?;
tokio::spawn(dns_exchange_background);
let name = Name::from_str("github.com")?;
let dns_response = dns_client.query(name, DNSClass::IN, RecordType::A).await?;
let records = dns_response.answers();
for record in records {
println!("{:?}", record);
}
Ok(())
}
/Users/rustist/.cargo/bin/cargo test --color=always --package playground --lib test --no-fail-fast -- --format=json --exact -Z unstable-options --show-output
Testing started at 6:44 PM ...
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.03s
Running unittests src/lib.rs (target/debug/deps/playground-d71fc203d211dfe7)
Record { name_labels: Name("github.com."), rr_type: A, dns_class: IN, ttl: 1, rdata: Some(A(A(20.205.243.166))) }
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment