UberGeek has asked for the wisdom of the Perl Monks concerning the following question:

I have a need to collect DNS aliases (CNAME records) and use them, given a hostname. Sounds simple, no?

It isn't. Our DNS is secure, so I can't simply transfer the zone local and parse it myself; I have to gethostbyname the addresses and then address-by-address perform a gethostbyaddr to obtain all the possible aliases.

There should be something simpler than this - I can't be the first person to need a list of all related aliases to a hostname, can I? - but I can't find anything. Two days of manuals and perusing CPAN aren't getting me anywhere... can someone help?


No freaking way it compiled on the first try...

Replies are listed 'Best First'.
Re: DNS aliases
by ikegami (Patriarch) on Dec 21, 2004 at 16:12 UTC

    A domain might have a "A" tag pointing to an address, so we can go from name to address:
    jupiter.sun.csd.unb.ca. A 131.202.3.8

    We can do a reverse lookup on an IP address, because IP addresses are also located in the DNS tree:
    8.3.202.131.in-addr.arpa. PTR jupiter.sun.csd.unb.ca

    Aliases have a "CNAME" tag pointing to a domain:
    unix.unb.ca. CNAME jupiter.sun.csd.unb.ca

    However, there's no equivalent to PTR for aliases -- there's nothing under jupiter.sun.csd.unb.ca that points back to unix.unb.ca -- so doing a reverse CNAME lookup is impossible.

    You need to either get the zone file from the DNS server (which you said you couldn't) or from the file system.

      Not quite - when you do a gethostbyaddr, it returns a few different things... from perldoc:

      ($name,$aliases,$addrtype,$length,@addrs) = gethost*

      if I gethostbyname, however, it populates $aliases with the same name you specified in the initial call to gethostbyname...


      No freaking way it compiled on the first try...

        Did you try those commands? They support what I said.

        gethostbyname("unix.unb.ca") (an alias) returns jupiter.sun.csd.unb.ca for name, unix.unb.ca for alias and 131.202.3.8 for address.

        gethostbyname("jupiter.sun.csd.unb.ca") returns jupiter.sun.csd.unb.ca for name, nothing for alias (even though it has unix.unb.ca for alias) and 131.202.3.8 for address.

        gethostbyaddr(inet_aton("131.202.3.8"), AF_INET)) returns jupiter.sun.csd.unb.ca for name, nothing for alias (even though it has unix.unb.ca for alias) and 131.202.3.8 for address.

        DNS simply does not provide the means to specify or query the information needed. You could parse the zone file if you had access to it, but the OP indicated he does not (at least, not via DNS).

        There's also the scenario where domains from different zone files point to the same address:
        cosplay.ca.  A 63.111.28.139
        adaelis.com. A 63.111.28.139
        Unless you had the zone files for every domain out in existance -- and no machine ever does or ever could -- you'd never be sure that you knew all the domains that point to an address. However, this paragraph is probably beyond the scope of the OP's question.