0

I am trying to get both the IPv4 and IPv6 value of a URL, say 'stackoverflow.com' for example.

Ideally returning the same results as dig stackoverflow.com A stackoverflow.com AAAA +short

I currently have the following, but for me it is only returning the IPv4 address

struct hostent *host_entry = gethostbyname("stackoverflow.com");
                    char *buff;
                    buff = inet_ntoa(*((struct in_addr *)host_entry->h_addr_list));
  • 1
    You will need to use [getaddrinfo](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html) and then look in the `results` array for `AF_INET6` results. 'gethostbyname` is not ipv6 aware – Paulw11 Apr 26 '23 at 21:29
  • @Paulw11 Thank you! Looking at the following link, do you know how I could alter it to return the IPv4 & IPv6 addresses from a given URL? https://stackoverflow.com/a/15571849/10111607 – Bob The Coder Apr 27 '23 at 15:35
  • 1
    You would call `getaddrinfo` with your host name and then loop through the returned results looking for results were `ai_family` is `AF_INET` (which is IPv4) and `AF_INET6`. `ai_addr` will then contain the relevant address bytes. Take a look at this https://subscription.packtpub.com/book/general/9781849698085/1/ch01lvl1sec11/performing-a-network-address-resolution – Paulw11 Apr 27 '23 at 20:38
  • @Paulw11 That worked perfectly thank you. Do you know if there's any built in way to exclude reserved addresses from being returned in this? – Bob The Coder May 04 '23 at 14:03

0 Answers0