Quantcast

gen_tcp:connect unable to resolve hostname if network interface goes up after being initially down

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

gen_tcp:connect unable to resolve hostname if network interface goes up after being initially down

César Varona

Hi,

using this Erlang websocket library I've come across an unpleasant situation: should the network interface be initially down upon attempting to establish a websocket connection with some server, it will keep indefinitely on failing with {error, nxdomain}, even if the network interface eventually comes up.

This is the line that fails (full source here):

gen_tcp:connect(Host, Port, [binary,{active, false},{packet, 0}], 6000)

When executed with my wireless interface disabled (no other connection available):

iex(10)> :gen_tcp.connect(myHost, somePort, [:binary, {:active, false}, {:packet, 0}], 6000)
{:error, :nxdomain}

Please note that

iex(11)> :inet_res.gethostbyname myHost
{:error, :nxdomain}

and

iex(12)> :inet.getifaddrs()
{:ok,
 [{'lo',
   [flags: [:up, :loopback, :running], hwaddr: [0, 0, 0, 0, 0, 0],
    addr: {127, 0, 0, 1}, netmask: {255, 0, 0, 0},
    addr: {0, 0, 0, 0, 0, 0, 0, 1},
    netmask: {65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535}]},
  {'enp4s0f2',
   [flags: [:up, :broadcast, :running, :multicast],
    hwaddr: [0, 144, 245, 236, 118, 237]]},
  {'wlp3s0',
   [flags: [:broadcast, :multicast], hwaddr: [238, 247, 218, 133, 87, 184]]},

   ...

being wlp3s0 my wireless network interface; now I turn on my wireless card again, so that

iex(13)> :inet.getifaddrs()
{:ok,
 [{'lo',
   [flags: [:up, :loopback, :running], hwaddr: [0, 0, 0, 0, 0, 0],
    addr: {127, 0, 0, 1}, netmask: {255, 0, 0, 0},
    addr: {0, 0, 0, 0, 0, 0, 0, 1},
    netmask: {65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535}]},
  {'enp4s0f2',
   [flags: [:up, :broadcast, :running, :multicast],
    hwaddr: [0, 144, 245, 236, 118, 237]]},
  {'wlp3s0',
   [flags: [:up, :broadcast, :running, :multicast],
    hwaddr: [96, 108, 102, 213, 237, 125], addr: {192, 168, 1, 36},
    netmask: {255, 255, 255, 0}, broadaddr: {192, 168, 1, 255},
    addr: {65152, 0, 0, 0, 44736, 36, 25537, 54250},
    netmask: {65535, 65535, 65535, 65535, 0, 0, 0, 0}]},

    ...

and

iex(14)> :inet_res.gethostbyname myHost
{:ok, {:hostent, '[obfuscated]', [], :inet, 4, [{94, 23, 48, 116}]}}

but any attempt to gen_tcp:connect again (from a newly launched proc) will keep on yielding a nxdomain error.

I'm running this on a Fedora 25 box with Erlang 19 and Elixir 1.4.2. I use Erlang's out-of-the-box inet configuration:

iex(4)> :inet.get_rc()
[domain: 'localdomain', search: ['localdomain'],
 resolv_conf: '/etc/resolv.conf', hosts_file: '/etc/hosts', lookup: [:native]]

Any clue as to what is happening and how could it be fixed? Is it a configuration problem or rather am I or the Jeremyoung library using gen_tcp improperly?

Kind regards




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: gen_tcp:connect unable to resolve hostname if network interface goes up after being initially down

Danil Zagoskin-2
Hi!

On Wed, May 10, 2017 at 5:39 PM, César Varona <[hidden email]> wrote:
lookup: [:native]

You are using system resolver, and problems you have are likely not related to Erlang/OTP.
Try {lookup, [dns, file]} instead. Use inet_db:set_lookup([dns, file]) or create an inetrc file.


--
Danil Zagoskin | [hidden email]

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: gen_tcp:connect unable to resolve hostname if network interface goes up after being initially down

Raimo Niskanen-2
On Thu, May 11, 2017 at 01:05:17AM +0300, Danil Zagoskin wrote:

> Hi!
>
> On Wed, May 10, 2017 at 5:39 PM, César Varona <[hidden email]> wrote:
>
> > lookup: [:native]
>
>
> You are using system resolver, and problems you have are likely not related
> to Erlang/OTP.
> Try {lookup, [dns, file]} instead. Use inet_db:set_lookup([dns, file]) or
> create an inetrc file.

The system resolver has on some systems got a tendency to cache resolved
hosts within the resolver library data itself, so if you restart the
process(es) that calls the resolver library it will in some cases resolve
(pun intended) the problem.

There is an undocumented feature in the native resolver that does exactly
this.  Try to call inet_gethost_native:control(soft_restart) and see if
it helps.

--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...