[GH-ISSUE #1152] How to get CNAME records? #614

Closed
opened 2026-03-15 23:28:08 +03:00 by kerem · 4 comments
Owner

Originally created by @Edu4rdSHL on GitHub (Jun 22, 2020).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/1152

I have been using trust_dns_resolver for a while, now I need to get the CNAME record of the host but I didn't found a way for it, not examples in the documentation and nothing at all searching by "CNAME". Additionally according to this document there is not way to perform a CNAME lookup but the initial page says that CNAME lookups are supported.

Regards,
Eduard

Originally created by @Edu4rdSHL on GitHub (Jun 22, 2020). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/1152 I have been using [trust_dns_resolver](https://docs.rs/trust-dns-resolver/0.19.5/trust_dns_resolver/) for a while, now I need to get the CNAME record of the host but I didn't found a way for it, not examples in the documentation and nothing at all searching by "CNAME". Additionally according to [this document](https://docs.rs/trust-dns-resolver/0.19.5/trust_dns_resolver/lookup/index.html) there is not way to perform a CNAME lookup but the initial page says that CNAME lookups are supported. Regards, Eduard
kerem 2026-03-15 23:28:08 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@bluejekyll commented on GitHub (Jun 22, 2020):

You'll want this method: https://docs.rs/trust-dns-resolver/0.19.5/trust_dns_resolver/struct.AsyncResolver.html#method.lookup and pass in the record type of CNAME. Have you tried that?

<!-- gh-comment-id:647683944 --> @bluejekyll commented on GitHub (Jun 22, 2020): You'll want this method: https://docs.rs/trust-dns-resolver/0.19.5/trust_dns_resolver/struct.AsyncResolver.html#method.lookup and pass in the record type of CNAME. Have you tried that?
Author
Owner

@Edu4rdSHL commented on GitHub (Jun 22, 2020):

Thank you, this worked. Now I have the following data:

Lookup { query: Query { name: Name { is_fqdn: true, labels: [blog, securityhacklabs, net] }, query_type: CNAME, query_class: IN }, records: [Record { name_labels: Name { is_fqdn: true, labels: [blog, securityhacklabs, net] }, rr_type: CNAME, dns_class: IN, ttl: 3, rdata: CNAME(Name { is_fqdn: true, labels: [ghs, google, com] }) }], valid_until: Instant { tv_sec: 21101, tv_nsec: 925270204 } }

How can I access to the CNAME labels in a valid format (String or str)? In this case should be ghs.google.com. I appreciate if you can help me with that, otherwise feel free to close the issue since that I have the CNAME records data.

<!-- gh-comment-id:647701653 --> @Edu4rdSHL commented on GitHub (Jun 22, 2020): Thank you, this worked. Now I have the following data: ``` Lookup { query: Query { name: Name { is_fqdn: true, labels: [blog, securityhacklabs, net] }, query_type: CNAME, query_class: IN }, records: [Record { name_labels: Name { is_fqdn: true, labels: [blog, securityhacklabs, net] }, rr_type: CNAME, dns_class: IN, ttl: 3, rdata: CNAME(Name { is_fqdn: true, labels: [ghs, google, com] }) }], valid_until: Instant { tv_sec: 21101, tv_nsec: 925270204 } } ``` How can I access to the CNAME labels in a valid format (String or str)? In this case should be `ghs.google.com`. I appreciate if you can help me with that, otherwise feel free to close the issue since that I have the CNAME records data.
Author
Owner

@bluejekyll commented on GitHub (Jun 22, 2020):

roughly, you want something along these lines:

let lookup = resolver...;

let name_strs: Vec<String> = lookup
    .iter()
    .filter_map(|rdata| rdata.as_cname())
    .map(|name| name.to_string())
    .collect();

to_string is based off the Display implementation for Name. If you want to be more explicit there is to_utf8 (which should be the same as Display) or to_ascii. to_ascii will encode the name properly for IDNA (internationalized form). Depends on what you're using the string for where you might want it in IDNA form.

<!-- gh-comment-id:647724892 --> @bluejekyll commented on GitHub (Jun 22, 2020): roughly, you want something along these lines: ```rust let lookup = resolver...; let name_strs: Vec<String> = lookup .iter() .filter_map(|rdata| rdata.as_cname()) .map(|name| name.to_string()) .collect(); ``` `to_string` is based off the `Display` implementation for `Name`. If you want to be more explicit there is `to_utf8` (which should be the same as `Display`) or `to_ascii`. `to_ascii` will encode the name properly for IDNA (internationalized form). Depends on what you're using the string for where you might want it in IDNA form.
Author
Owner

@Edu4rdSHL commented on GitHub (Jun 22, 2020):

Thank you a lot, @bluejekyll!

<!-- gh-comment-id:647728203 --> @Edu4rdSHL commented on GitHub (Jun 22, 2020): Thank you a lot, @bluejekyll!
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#614
No description provided.