I'm using libresolv to fetch a list of authoritative nameservers for a domain and then query one of those nameservers directly to resolve a host.
That way, I can get the configured TTL for the A record and then I choose to refresh my DNS records in my code at that TTL.
However, I've run into a snafu in that i'm not sure how to handle hosts that refer to CNAMES. For example, output from www.microsoft.com
looks like this:
; <<>> DiG 9.7.3 <<>> www.microsoft.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3870
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 8, ADDITIONAL: 7
;; QUESTION SECTION:
;www.microsoft.com. IN A
;; ANSWER SECTION:
www.microsoft.com. 5 IN CNAME toggle.www.ms.akadns.net.
toggle.www.ms.akadns.net. 5 IN CNAME www.microsoft.com-c.edgekey.net.
www.microsoft.com-c.edgekey.net. 5 IN CNAME www.microsoft.com-c.edgekey.net.globalredir.akadns.net.
www.microsoft.com-c.edgekey.net.globalredir.akadns.net. 5 IN CNAME e10088.dspb.akamaiedge.net.
e10088.dspb.akamaiedge.net. 5 IN A 23.194.222.159
Should I be refreshing my host using the CNAME TTL? If so, how do I get that configured TTL for the CNAME?
Or, should I be refreshing my using using the A record which is linked to the CNAMEs (i.e. e10088.dspb.akamaiedge.net
)? If so, how do I fetch the configured TTL for that host?
Any information you experts can provide would be greatly appreciated.
In the case of CNAME's for load balanced servers, you'd want to refresh at the closest record to you, which is the initial lookup. Consider the rest of the lookups as backend processing, which in this case, I like to call the Akamai shuffle.
EDIT: If you query the authoritative NS directly, you should get the "real" TTL:
The TTL countdown is a function of a caching LDNS or intermediary server. So, in your example:
First, do your primary lookup:
Then you need to get the NS/SOA of the last CNAME:
You should then be able to query the authoritative server for the record: