2

On Mac OS X El Capitan, I am running a C++ application that calls gethostbyname() and sometimes passes the argument string matching the current machine:

bash-3.2$ hostname
Ws-MacBook-Pro

This normally works, however, on rare occasions, I get a NULL result and h_errno is set to HOST_NOT_FOUND.

Is this a known issue? How can my application recover from this? I'd like to retry on the hopes it succeeds, but how would I distinguish a real host not found failure from this transient one for the current machine's name? How long would such a failure likely persist and what might be causing it?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • have you tried printing the value passed to gethostbyname() when you get a null to make absolutely sure it's not a bad argument? – kfsone Jun 08 '16 at 21:10
  • @kfsone Yes, my error message printed the value and showed it was not corrupted. – WilliamKF Jun 08 '16 at 21:13
  • 1
    The usual reason is a flaky/unreachable DNS server. – Sam Varshavchik Jun 08 '16 at 21:19
  • Have you tested this with the [loopback address](http://superuser.com/questions/255835/whats-a-loopback-address-i-e-127-0-0-1) to see if your NIC has an issue? If it passes with a zero failures, it is likely a flaky DNS server as @SamVarshavchik pointed out. Otherwise there is some fault in your local machine's hardware. Do you know what protocol `gethostbyname()` is using? – NonCreature0714 Jun 08 '16 at 21:51
  • 1
    @SamVarshavchik The local hostname shouldn't even be in DNS. – Barmar Jun 08 '16 at 21:58
  • @barmar - gethostbyname() looks up any name in DNS. Even local hostname's. – Sam Varshavchik Jun 09 '16 at 00:55
  • @SamVarshavchik How would your local hostname get into the public DNS? There isn't even a domain suffix on it. OS X actually uses mDNS, which consults a local database first before going to the real DNS server. – Barmar Jun 09 '16 at 00:59
  • @Barmar - who said anything about public DNS? If the OP is on a corporate network or a LAN, the local DNS servers will be authoritative for the zone. – Sam Varshavchik Jun 09 '16 at 01:45
  • @SamVarshavchik Who said anything about a corporate network? Absent anything in the question about a local DNS server, I'll assume it's a personal Macintosh using his ISP's DNS (or something like Google DNS). – Barmar Jun 09 '16 at 01:48
  • @Barmar Yes, using ISP's DNS. – WilliamKF Jun 09 '16 at 02:27
  • You really should stop using `gethostbyname()` altogether, use `getaddrinfo()` instead. If you want the address(es) of the local host, use `getifaddrs()` instead. – Remy Lebeau Jun 09 '16 at 02:43

0 Answers0