Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically obtain DNS servers of host

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.

  • OS X: already answered; updated link at developer.apple.com
  • Windows: GetNetworkParams
  • FreeBSD: /etc/resolv.conf

Thanks in advance for your help!

like image 227
Nicholas Palko Avatar asked May 26 '10 20:05

Nicholas Palko


1 Answers

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

like image 64
joke Avatar answered Sep 27 '22 19:09

joke