介绍
Trust-DNS是一个实现DNS协议和客户端功能的库。该库包含DNS记录序列化和通信的基本实现,通过它能够执行查询、更新和通知等操作。Trust-DNS Resolver是对应的DNS解析库,包括对应的IPv4和IPv6的一些实现。
使用示例
本例子中,我们来实现一个查询dns记录的功能。
依赖
[dependencies]
trust-dns-resolver = "0.11.0"
trust-dns = "0.16.0"
源码
use std::env;
use trust_dns_resolver::Resolver;
use trust_dns_resolver::config::*;
use trust_dns::rr::record_type::RecordType;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Please provide a name to query");
std::process::exit(1);
}
let query = format!("{}.", args[1]);
// 创建默认解析器
println!("Using the synchronous resolver");
let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default()).unwrap();
let response = resolver.lookup_ip(query.as_str());
for ans in response.iter() {
println!("{:?}", ans);
}
// 根据系统配置文件创建解析器,linux下为从/etc/resolv.conf创建
println!("Using the system resolver");
let system_resolver = Resolver::from_system_conf().unwrap();
let system_response = system_resolver.lookup_ip(query.as_str());
for ans in system_response.iter() {
println!("{:?}", ans);
}
println!("NS records using the synchronous resolver");
let ns = resolver.lookup(query.as_str(), RecordType::NS);
for ans in ns.iter() {
println!("{:?}", ans);
}
}
运行
例如查询baidu.com的信息
cargo run baidu.com//注意,不要用www.baidu.com,否则会查询不到
网友评论