[erlang-questions] gen_tcp:connect unable to resolve hostname if network interface goes up after being initially down

César Varona cesar.varona@REDACTED
Wed May 10 16:39:42 CEST 2017


Hi,

using this Erlang websocket library 
<https://github.com/jeremyong/websocket_client> 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 
<https://github.com/jeremyong/websocket_client/blob/master/src/websocket_client.erl>):

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



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170510/b0a9527f/attachment.htm>


More information about the erlang-questions mailing list