tdns_cli/
query.rs

1use futures::{future, Future};
2
3use trust_dns::{
4    client::ClientHandle,
5    proto::xfer::{DnsHandle, DnsResponse},
6    rr,
7};
8
9#[derive(Debug, Clone)]
10pub struct Query {
11    pub entry: rr::Name,
12    pub record_types: Vec<rr::RecordType>,
13}
14
15pub fn perform_query(
16    mut resolver: impl DnsHandle,
17    options: Query,
18) -> impl Future<Item = Vec<DnsResponse>, Error = failure::Error> {
19    let entry = options.entry;
20    future::join_all(options.record_types.into_iter().map(move |rtype| {
21        resolver
22            .query(entry.clone(), rr::DNSClass::IN, rtype)
23            .map_err(Into::into)
24    }))
25}
26
27pub fn print_dns_response(responses: &[DnsResponse], _options: &Query) {
28    for response in responses {
29        for answer in response.answers() {
30            use rr::RData::*;
31            match answer.rdata() {
32                A(addr) => println!("{}", addr),
33                AAAA(addr) => println!("{}", addr),
34                TXT(txt) => {
35                    for (i, data) in txt.txt_data().iter().enumerate() {
36                        // TODO: proper (lossless) display of TXT data
37                        let content = String::from_utf8_lossy(data);
38                        if i + 1 < txt.txt_data().len() {
39                            print!(r#""{}" "#, content);
40                        } else {
41                            println!(r#""{}""#, content);
42                        }
43                    }
44                }
45                // TODO: display other records properly
46                other => println!("{:?}", other),
47            }
48        }
49    }
50}