[GH-ISSUE #607] How to transform trust_dns_proto::rr::rdata::mx::MX into &str or IpAddr? #249

Closed
opened 2026-03-07 23:00:28 +03:00 by kerem · 2 comments
Owner

Originally created by @ghost on GitHub (Nov 8, 2018).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/607

Rust beginner here.

    let hosts = mx_hosts::get_mx_lookup(my_domain);

    for host in hosts.iter() {
        debug!("{:?}", host);
        // I want host either as &str (the dns name) or as IpAddr, is that possible?
    }

Thanks.

Originally created by @ghost on GitHub (Nov 8, 2018). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/607 Rust beginner here. ```rust let hosts = mx_hosts::get_mx_lookup(my_domain); for host in hosts.iter() { debug!("{:?}", host); // I want host either as &str (the dns name) or as IpAddr, is that possible? } ``` Thanks.
kerem 2026-03-07 23:00:28 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@bluejekyll commented on GitHub (Nov 8, 2018):

I'm not familiar with the mx:hosts::get_mx_lookup function you mention, so I'm going to assume it is calling this function in the Resolver (I'm writing this free form, so apologies if the code I write here doesn't compile as-is):

https://docs.rs/trust-dns-resolver/0.10.0/trust_dns_resolver/struct.Resolver.html#method.mx_lookup

Assuming you have a Resolver already constructed in some manner, let resolver = Resolver::new...;

let mx_lookup = resolver.mx_lookup("example.com.")?;

We now have the mx_lookup response, this returns an MxLookup which has a function, iter which returns an MxLookupIter, this can be used to iterate over the MX, which has an exchange and preference component, records:

for mx in mx_lookup.iter() {
    let name: &str = mx.exchange();
    let preference: u16 = mx.preference();
}

Does that all make sense? After this, you'd generally want to do an A or AAAA lookup, or use lookup_ip which will lookup both A or AAAA for you. I hope this helps!

<!-- gh-comment-id:437099775 --> @bluejekyll commented on GitHub (Nov 8, 2018): I'm not familiar with the `mx:hosts::get_mx_lookup` function you mention, so I'm going to assume it is calling this function in the Resolver (I'm writing this free form, so apologies if the code I write here doesn't compile as-is): https://docs.rs/trust-dns-resolver/0.10.0/trust_dns_resolver/struct.Resolver.html#method.mx_lookup Assuming you have a Resolver already constructed in some manner, `let resolver = Resolver::new...;` ```rust let mx_lookup = resolver.mx_lookup("example.com.")?; ``` We now have the mx_lookup response, this returns an `MxLookup` which has a function, `iter` which returns an `MxLookupIter`, this can be used to iterate over the MX, which has an `exchange` and `preference` [component](https://docs.rs/trust-dns-proto/0.5.0/trust_dns_proto/rr/rdata/mx/struct.MX.html), records: ```rust for mx in mx_lookup.iter() { let name: &str = mx.exchange(); let preference: u16 = mx.preference(); } ``` Does that all make sense? After this, you'd generally want to do an `A` or `AAAA` lookup, or use `lookup_ip` which will lookup both A or AAAA for you. I hope this helps!
Author
Owner

@ghost commented on GitHub (Nov 18, 2018):

Thanks, this helped a lot.

<!-- gh-comment-id:439689712 --> @ghost commented on GitHub (Nov 18, 2018): Thanks, this helped a lot.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hickory-dns#249
No description provided.