:inet_res.gethostbyaddr locally

I want to use :inet_res.gethostbyaddr/1 to get a hostname from an ip. It is ultimately for use in Phoenix, but I had the following problem just in iex.

First I got this error:
49

And a few minutes later I got this properly with just :ines:
04

Edited original post after here:

I was using the :inet_res in my Phoenix app and it worked locally until today. But I want to ask why would using the :inet implementation work in iex like above, but the :inet_res one doesn’t seem to?

I tried reading http://erlang.org/doc/apps/erts/inet_cfg.html and http://erlang.org/doc/man/inet_res.html but find it somehow hard to find the answer there with my current knowledge.

2 Likes

I tried again today and I got this:

07

Could it be something related to the network I’m using? I was on my home network when I wrote the original post, now I’m at my office. I thought about that before, but thought it doesn’t make sense for localhost.

This is my best guess at what is most likely going on in this case.

As near as I can figure after a quick look at the links you posted is that :inet_res.get... directly uses a DNS server to resolve the request which fails because whatever you have set as your DNS resolver(s) (possibly your ISPs upstream DNS servers or your LAN router) cannot resolve for localhost or 127.0.0.1 and responds with NXDOMAIN (Non-existent Domain).

In the docs for inet_res it states this (emphasis mine):

This is not a full-fledged resolver, only a DNS client that relies on asking trusted recursive name servers.

One the other hand :inet.get... probably uses the OS resolver lib for the request which uses the lookup order set in /etc/host.conf (on Linux/Unix etc. OS) which usually includes the hosts file as the first resource. Since localhost is most always set in the hosts file pointing to 127.0.0.1, the resolver lib resolved it correctly.

At your office location the router sitting on the LAN is probably set to resolve a lookup for localhost or 127.0.0.1 and :inet_res.get... succeeds.

The real clue to it is here (linked from the docs you referenced):

If no user configuration file is specified and Erlang is started in non-distributed or short name distributed mode, Erlang uses default configuration settings and a native lookup method that works correctly under most circumstances. Erlang reads no information from system inet configuration files (such as /etc/host.conf and /etc/nsswitch.conf) in these modes, except for /etc/resolv.conf and /etc/hosts that is read and monitored for changes on Unix platforms for the internal DNS client inet_res(3).

But it’s hard to say exactly without knowing how each network is set up.

1 Like

Thank you very much for the reply. :slight_smile: It definitely helped me understand it a bit more.