0

I want to use getaddrinfo() but get only the first result.

more specifically, I want the function to first scan the hosts file and fetch the first result found, and only if not found in hosts I want to query the dns server.

is it possible?

thanks.

Ronen Ness
  • 9,923
  • 4
  • 33
  • 50
  • 2
    querying dns if not found in the hosts file should be the default behavior on most operating systems, what kind of setup/operating system are you on ? – nos Oct 01 '13 at 18:24
  • Have you seen http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostent.html? (gethostent and friends) – Mark Nunberg Oct 01 '13 at 22:47

2 Answers2

4

You can't. It behaves as documented. You only have to use one result: that's up to you.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

The order on how resolving hosts is done is define in /etc/host.conf using the keyword order:

order

This keyword specifies how host lookups are to be performed. It should be followed by one or more lookup methods, separated by commas. Valid methods are bind, hosts, and nis.

However I doubt you can restrict the number of results to 1 per this configuration directly.

alk
  • 69,737
  • 10
  • 105
  • 255