11

Using C++, I would like to obtain the DNS servers being used by a host for three operating systems: OS X, FreeBSD, and Windows. I'd like confirmation that the approaches below are indeed best practice, and if not, a superior alternative.

Thanks in advance for your help!

Community
  • 1
  • 1
Nicholas Palko
  • 813
  • 3
  • 11
  • 21
  • On Windows, a better function is `GetAdaptersAddresses()`. It'll return both IPv4 and IPv6 addresses and give you a ton of other information too. – CubicleSoft May 27 '21 at 13:54

1 Answers1

11

On many unix systems (linux, bsd) you can use the resolver functions to obtain the list of DNS servers: man 3 resolver.

After calling res_init() the resolver structure is initialized. The resolver structure stores all the information you need. The list of DNS servers are stored in the struct entry nsaddr_list.

The exact specification of the resolver structure can most likely be found in resolv.h.

Using the resolver functions is the preferred way to obtain the list of DNS servers. res_init() will most likely fill the resolver structure with the information found in /etc/resolv.conf.

Also see Use of resolv.h

joke
  • 654
  • 9
  • 11
  • Yep, this does seem of the preferred way to get the DNS servers though the *nix API. Thanks! – Nicholas Palko Dec 03 '10 at 04:05
  • 1
    use res_ninit and res_nclose, not depend on global state variables. – tangxinfa Aug 14 '15 at 10:00
  • Is there any good alternative to res_ninit( ). There are problems that i am seeing such as - https://stackoverflow.com/questions/49579030/retrieve-ipv4-and-ipv6-nameservers-programmatically – badri Feb 26 '19 at 20:24