SOA lookup using the trust-dns-resolver Crate

This is a second article on using the trust-dns-resolver crate
Warning: This code is not intended to be used in production. You should review and adjust to your own needs.

Getting Started

First we will need to create our development environment.

cargo new trust-dns-resolver && cd $_

This will give us our standard rust directly structure. We need to add our crate to the Cargo.toml

-snip-
[dependencies]
trust-dns-resolver = "0.20.1"

Next edit the src/main.rs as follows.

Compile and run the code to make sure everything is ok.

cargo run

This outputs the following

Admin: dns-admin.google.com.
Admin Email: dns-admin@google.com
Primary: ns1.google.com.
Serial: 364996005

Explaining the code

Documentation for the SOA RData can be found here
Let’s take a look at what we are doing here.
Like the first example we create a resolver which will do the work of doing the DNS lookups.
Next we use the resolver to call soa_lookup() and store the result into soa_response
soa_response will contain either and SoaLookup or an Err. For this reason we need to handle these two cases. I have changed things up a little this time and I am passing a reference to the display_soa() function. So I am allowing display_soa() to borrow the data. In doing this I am saving on memory and I am also promising that I am not going to mutate soa_response within this function. I will cheat a little and clone() some data later.
Within display_soa I again use match to handle my two cases.
In the case of Err. I do nothing and just report there were no records.
In the case of SoaLookup, I will need to do some more processing.

  • First lets make an iter() out of soa_response and loop over it.
  • In the loop we get the rname() or the administrative contact and the mname() or primary name server for this domain as well as serial() and access to a few more elements.

Line 26 is just a cobbled together function to redisplay rname() as a more valid email address which is what it really represents. If anyone can point in the direction of a better way to handle this it would greatly appreciated.

Closing

I hope you find this interesting and useful.


See also