chen
chen

Reputation: 41

Getting hostname aliases from IP

I want to get the list of aliases for a hostname in C++. This input is a IP. Currently we call gethostbyaddr(), which returns a hostent struct. This struct has a field for list of aliases.

     struct  hostent {
        char    FAR * h_name;           /* official name of host */
        char    FAR * FAR * h_aliases;  /* alias list */
        short   h_addrtype;             /* host address type */
        short   h_length;               /* length of address */
        char    FAR * FAR * h_addr_list; /* list of addresses */
};

gethostbyaddr() API is legacy, and also we wanted to support IPv6 inputs. So we replaced gethostbyaddr() with getnameinfo() as recommended in msdn. But after this changes, there is no way to get the list of aliases for a hostname. getnameinfo() simply returns the hostname as a string, and there is no alias list returned by it. Can somebody help me to get the alias list in this case?

Upvotes: 4

Views: 1273

Answers (1)

jch
jch

Reputation: 5651

I don't think that's possible. The DNS stores:

  • for each name, one or many IP/IPv6 addresses;
  • for each IP/IPv6 address, up to one "canonical" name.

So you can query the addresses associated to a name (getaddrinfo), or the canonical name associated to an address (getaddrinfo), but there's no way to query the set of names that map to a given address.

Upvotes: 1

Related Questions