3

If I ask gethostbyaddr() to look up a NAT-generated IP address, such as 192.168.0.4, should I expect it to work?

Background: I'm running tests at home on some control code intended for a networked environment. At times, this code does a gethostbyaddr() call to get details of the machine that sent it a message. The address will often be that of a local machine, and at times even turns out to be the address of the current machine itself. Normally, this causes no problems. However, I have replaced my old Netcomm ADSL router by a Netgear router, and discovered that a gethostbyaddr() call for a machine in the local NAT environment now a) times out after 30 seconds, b) returns NULL to indicate an error. Experimentally going back to my old router, I find that the same call also fails, returning NULL, but does so immediately. The code can handle the error return (which is why I'd never noticed this before), but the new 30-sec timeout is a huge nuisance. Obviously, there are a host of ways to code around this problem, but most involve trapping the case where the address is a 192.168 address and avoiding the gethostbyaddr() call. My question is, does a gethostbyaddr() call always fail in a NAT environment, or are there cases where it will work? (In which case I don't want to end up avoiding them.) Or is it just that I've not set up my routers properly? By the way, I'm running this on OS X, and see the same behaviour on both Snow Leopard and Lion.

KeithS
  • 113
  • 1
  • 7

1 Answers1

1

They won't work unless you setup private dns or add them to your hosts file. Sendmail provides some good documentation on this:

http://www.sendmail.com/sm/open_source/tips/private_dns/

The calls that end up reaching the root name servers get blackholed.

Some routers might already provide the reverse DNS. You don't need to have every host in there, just be authoritative for the address space so the request doesn't get forwarded.

Shawn Balestracci
  • 7,380
  • 1
  • 34
  • 52
  • Thanks. I found that sendmail link very helpful. Essentially, the answer to my question seems to be that _normally_ gethostbyaddr() won't work on NAT addresses (and may be time consuming) and so should be avoided, but there _might_ be cases where it has been configured to work and I should allow for that possibility. I think I'll get the code to normally bypass the call for 192.168 addresses but provide some override setting that will allow the call to be made. Does that sound sensible? – KeithS Apr 24 '13 at 05:35
  • Note there are other private addresses which are defined in http://tools.ietf.org/html/rfc1918: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. The standard is to offer an option to disable gethostbyaddr globally and to timeout. Sometimes public address lookups hang as well and 30 seconds is too long to wait. – Shawn Balestracci Apr 24 '13 at 05:55
  • For the moment I'm going to assume that 192.168 addresses probably don't have reverse DNS working, but the larger address space options probably do. And I'll provide some override option. For our purposes I think that's enough. Our organisation's 10 network handles this fine and if it also works on home modems I'll settle for that. In any case all this networking code is due to be reviewed soon in the light of IPV6. Thanks for your help and I'm ticking this as answered. – KeithS Apr 24 '13 at 10:28