[erlang-questions] inet:gethostbyname question

Raimo Niskanen <>
Fri Jan 22 16:31:25 CET 2010


Alright, I think I have figured this one out...

R13 relies very much on the native resolver. If it fails it
is considered to be right. It is not even tried to see
if the lookup is for the own name and this is a bug.
You are supposed to always be able to look up
your own hostname at least into the loopback address.
We will correct this while taking a look at translation
of IP address string hostnames.

There was a change I think sometime during R13 into not
using the inet_hosts file mechanism for this fallback
and this odd case where the native resolver fails
fools the new fallback.

If you manage to configure your native resolver you will be fine.
These are the configuration files involved:

/etc/hosts
/etc/resolv.con
/etc/nsswich.conf

You can test the native resolver with
inet_gethost_native:gethostbyname(Host).
And almost the same from the Unix shell:
$ getent hosts Host

The symptom of the changing resolver search option is unimportant.

I bet you have no "search" line, but possibly a "domain" line.

The normal pass through inet_config:init() will manage
to figure out your domain and sets the search option
but at the very end of the function when it finds the
resolver option resolv_conf is not set it reads the
/etc/resolv.conf file but first clears nameservers and
search list, and since there is no "search" line it
stays cleared. Your fix call of inet_config:init()
will find do the same but find that the resolv_conf option
is set and not clear the search list.

That the search option is empty is no problem for inet_res
(the [dns] lookup method) since if it is empty inet_res
uses the domain option as search option.

And if you do not have the [naitive] lookup method
the fallback of translating the local hostname
into 127.0.0.1 will not be fooled.

/ Raimo



On Thu, Jan 21, 2010 at 11:43:31PM -0800, Yogish Baliga wrote:
> Attaching the output between "********************************"
> 
> Thanx,
> -- baliga
> 
> 
> ________________________________
> From: Raimo Niskanen <>
> To: Yogish Baliga <>; 
> Sent: Thu, January 21, 2010 12:43:26 AM
> Subject: Re: [erlang-questions] inet:gethostbyname question
> 
> On Wed, Jan 20, 2010 at 10:11:39AM -0800, Yogish Baliga wrote:
> > 
> > On Mon, Jan 18, 2010 at 09:21:06PM -0800, Yogish Baliga wrote:
> > > 
> > > Which R13 release is it? The code involved was
> > > extensively rewritten in R13B02.
> > > 
> > > Using latest version of R13.
> > > 
> > > And what does inet_db:gethostname() acutally return?
> > > 
> > > 
> > > inet_db:gethostname() return gw06
> > > 
> > 
> > Then we continue with these commands:
> >     erlang:system_info(system_version).
> >     application:get_env(kernel, inetrc).
> >     node().
> >     inet_db:gethostname().
> >     io:format("~p~n", [ets:tab2list(inet_db)]).
> 
> Oh, I was unclear, I wanted to see the output of those commands...
> 
> ***************************************************
> ()1> erlang:system_info(system_version).
> "Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:8:8] [rq:8] [async-threads:0] [hipe] [kernel-poll:false]\n"
> ()2> application:get_env(kernel, inetrc).
> undefined
> ()3> node().
> ''
> ()4> inet_db:gethostname().
> "gw06"
> ()5> io:format("~p~n", [ets:tab2list(inet_db)]).
> [{res_resolv_conf_info,{file_info,254,regular,undefined,undefined,
>                                   {{2010,1,21},{23,24,59}},
>                                   {{2010,1,21},{23,24,59}},
>                                   33188,1,64769,0,39458288,0,0}},
>  {res_hosts_file_info,{file_info,155,regular,undefined,undefined,
>                                  {{2010,1,19},{12,24,24}},
>                                  {{2010,1,19},{12,24,24}},
>                                  33188,1,64769,0,39454378,0,0}},
>  {res_domain,"yahoo.com"},
>  {res_hosts_file_tm,1264145110},
>  {res_resolv_conf_tm,1264145110},
>  {cache_refresh_interval,3600000},
>  {socks5_noproxy,[]},
>  {res_retry,3},
>  {res_search,[]},
>  {res_resolv_conf,"/etc/resolv.conf"},
>  {res_inet6,false},
>  {res_recurse,true},
>  {hostname,"gw06"},
>  {res_udp_payload_size,1280},
>  {cache_size,100},
>  {sctp_module,inet_sctp},
>  {udp_module,inet_udp},
>  {tcp_module,inet_tcp},
>  {res_hosts_file,"/etc/hosts"},
>  {res_id,0},
>  {socks5_port,1080},
>  {res_timeout,2000},
>  {res_edns,false},
>  {res_alt_ns,[]},
>  {socks5_methods,[none]},
>  {res_ns,[{{68,180,184,17},53},{{68,180,184,18},53},{{68,180,184,19},53}]},
>  {res_usevc,false},
>  {socks5_server,[]},
>  {res_lookup,[native]}]
> ok
> ****************************************************************
> > immediately after starting a node, and then after
> > your fix by calling inet_config:init():
> >     inet_db:gethostname().
> >     io:format("~p~n", [ets:tab2list(inet_db)]).
> 
> And those...
> 
> **************************************************************
> ()5> inet_db:gethostname().
> "gw06"
> ()6> io:format("~p~n", [ets:tab2list(inet_db)]).
> [{res_resolv_conf_info,{file_info,254,regular,undefined,undefined,
>                                   {{2010,1,21},{23,24,59}},
>                                   {{2010,1,21},{23,24,59}},
>                                   33188,1,64769,0,39458288,0,0}},
>  {res_hosts_file_info,{file_info,155,regular,undefined,undefined,
>                                  {{2010,1,19},{12,24,24}},
>                                  {{2010,1,19},{12,24,24}},
>                                  33188,1,64769,0,39454378,0,0}},
>  {res_domain,"yahoo.com"},
>  {res_hosts_file_tm,1264145922},
>  {res_resolv_conf_tm,1264145922},
>  {cache_refresh_interval,3600000},
>  {socks5_noproxy,[]},
>  {res_retry,3},
>  {res_search,["oss.search.sk1.yahoo.com"]},
>  {res_resolv_conf,"/etc/resolv.conf"},
>  {res_inet6,false},
>  {res_recurse,true},
>  {hostname,"gw06"},
>  {res_udp_payload_size,1280},
>  {cache_size,100},
>  {sctp_module,inet_sctp},
>  {udp_module,inet_udp},
>  {tcp_module,inet_tcp},
>  {res_hosts_file,"/etc/hosts"},
>  {res_id,0},
>  {socks5_port,1080},
>  {res_timeout,2000},
>  {res_edns,false},
>  {res_alt_ns,[]},
>  {socks5_methods,[none]},
>  {res_ns,[{{68,180,184,17},53},{{68,180,184,18},53},{{68,180,184,19},53}]},
>  {res_usevc,false},
>  {socks5_server,[]},
>  {res_lookup,[native]}]
> ok
> **************************************************************
> 
> > again, and we'll see what changed...
> > 
> > 
> >    ***** Nothing changed after inet_config:init(). 
> 
> Previously you stated that the return values of
> inet_db:res_option(search) and inet_db:res_option(domain)
> changed after calling inet_config:init(), and those
> are among the entries in the ets table inet_db, so
> now you are contradicting yourself or just missed
> that little change... Can I see the complete outputs, please?
> 
> I take my words back on "nothing changed after inet_config:init(). Actually res_search changed in inet_db ets table.
> 
> > 
> > Try also to start a fresh node, check ets:i(inet_db)
> > to see that the table is as above. Then call
> > inet_res:gethostbyname("gw06") and then:
> >     inet_db:gethostname().
> >     io:format("~p~n", [ets:tab2list(inet_db)]).
> > to see if that call changes something...
> > 
> >   ************* Nothing changed after this too.
> > 
> > > > 
> > > > inet_db:res_option(lookup).
> > > > [dns,native,file].
> > > > inet:gethostbyname(inet_db:gethostname())
> > > > {error,nxdomain}
> > > > inet_db:set_lookup([dns]).
> > > > inet:gethostbyname(inet_db:gethostname())
> > > > {ok,{hostent,"gw06",[],inet,4,[{127,0,0,1}]}}
> > 
> > Try the individual resolver components:
> >     inet_res:gethostbyname("gw06").
> > {ok, {hostent.....}} [works]
> > 
> >     inet_gethost_native:gethostbyname("gw06").
> > {error, not_found}.
> > 
> >     inet_hosts:gethostbyname("gw06").
> > {error,nxdomain}
> > 
> > and we'll see what they think.
> > 
> > Looks like dns resolver work but the native and file resolver does not work. 
> > 
> > For the native resolver, I tried remvoing "domain" line from /etc/resolv.conf. It seems to be working.
> 
> And the contents of /etc/resolv.conf is also interesting
> to explain the native resolver and on some systems
> /etc/nsswitc.conf. As is the contents of /etc/hosts...
> 
> And, of course, what is your operating system:
>    1> os:type()
> and
>    $ uname -a
> 
> ********************************
> 
> ()8> os:type().
> {unix,linux}
> 
> [ etc]$ uname -a
> Linux gw06.oss.search.sk1.yahoo.com 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64
> 
> ********************************
> > 
> > In R13, default lookup is [native] and in R12 is [native,file].
> > 
> > Setting lookup to [native,file] in R13 does not work either.
> > 
> > In both versions, dns resolver works.
> > 
> > For the time being, I am removing domain entry from /etc/resolv.conf file.
> > 
> > Thanx,
> > -- baliga
> > 
> > > > 
> > > > Having lookup method to 3 elements [dns,native,file] is not working. If I set it to only "dns", it is working. Also noticed that
> > > > 
> > > > inet_db:res_option(search).
> > > > []
> > > > inet_db:res_option(domain).
> > > > "example.com"
> > > > 
> > > > Then I execute,
> > > > 
> > > > inet_config:init().
> > > > 
> > > > This function is called by inet_db:start_link(), which is called by kernel:init().
> > > > 
> > > > After executing inet_config:init(),
> > > > 
> > > > inet_db:res_option(search).
> > > > ["sk1.example.com"].
> > > > inet_db:res_option(domain).
> > > > "example.com".
> > > > 
> > > >  
> > > > After executing inet_config:init(), order of lookup methods doesn't matter. As long as I have dns in the list, it works.
> > > > 
> > > > inet:gethostbyname(inet_db:gethostname()).
> > > > {ok,{hostent,"gw06.sk1.example.com",[],inet,4,[{127,0,0,1}]}}
> > > > 
> > > > Any idea, why inet_config:init() called by kernel->inet_db:start_link() is not working as expected?
> > > > 
> > > > Thanx,
> > > > -- baliga
> > > > 
> > > > "Point of view is worth 80 IQ points" --Alan Kay
> > > > 
> > > > http://dudefrommangalore.blogspot.com/
> > > -- 
> > > 
> > > / Raimo Niskanen, Erlang/OTP, Ericsson AB
> > > 
> > > ________________________________________________________________
> > > erlang-questions mailing list. See http://www.erlang.org/faq.html
> > > erlang-questions (at) erlang.org
> > -- 
> > 
> > / Raimo Niskanen, Erlang/OTP, Ericsson AB
> 
> -- 
> 
> / Raimo Niskanen, Erlang/OTP, Ericsson AB

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list