Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve different types of DNS records in Rust?

Tags:

rust

dns

I need to send a DNS request to check the A, AAAA, MX and SOA records of a domain.

There're 2 popular DNS libraries: trust-dns and dns-parser. Neither of them has an example. trust-dns doesn't support what I need to do and dns-parser's documentation doesn't help.

like image 603
Jodimoro Avatar asked Jan 21 '26 12:01

Jodimoro


1 Answers

The domain crate appears to support your usecases. It also is built on top of futures which is nice for the needed network requests.

extern crate domain;
extern crate futures;
extern crate tokio_core;

use std::str::FromStr;
use domain::bits::{DNameBuf, ParsedDName};
use domain::iana::{Class, Rtype};
use domain::rdata::{A, Aaaa, Mx, Soa};
use domain::resolv::Resolver;
use futures::Future;
use tokio_core::reactor::Core;

fn main() {
    let mut core = Core::new().unwrap();
    let resolv = Resolver::new(&core.handle());

    let name = DNameBuf::from_str("www.rust-lang.org.").unwrap();

    let v4 = resolv.clone().query((name.clone(), Rtype::A, Class::In));
    let v6 = resolv.clone().query((name.clone(), Rtype::Aaaa, Class::In));
    let mx = resolv.clone().query((name.clone(), Rtype::Mx, Class::In));
    let soa = resolv.query((name, Rtype::Soa, Class::In));

    let addrs = v4.join4(v6, mx, soa);

    let (v4, v6, mx, soa) = core.run(addrs).unwrap();

    println!("-- A --");

    for record in v4.answer().unwrap().limit_to::<A>() {
        println!("{}", record.unwrap());
    }

    println!("-- AAAA --");

    for record in v6.answer().unwrap().limit_to::<Aaaa>() {
        println!("{}", record.unwrap());
    }

    println!("-- MX --");

    for record in mx.answer().unwrap().limit_to::<Mx<ParsedDName>>() {
        println!("{}", record.unwrap());
    }

    println!("-- SOA --");

    for record in soa.answer().unwrap().limit_to::<Soa<ParsedDName>>() {
        println!("{}", record.unwrap());
    }
}

I've never seen this crate before today, so I don't know that I'm using it correctly or efficiently, but it does seem to work.

like image 122
Shepmaster Avatar answered Jan 25 '26 21:01

Shepmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!