Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

net.connect fails looking up localhost when offline #25489

Closed
seanpoulter opened this issue Jun 8, 2015 · 11 comments
Closed

net.connect fails looking up localhost when offline #25489

seanpoulter opened this issue Jun 8, 2015 · 11 comments

Comments

@seanpoulter
Copy link

Socket.prototype.connect does not properly handle localhost or the ENOENT error thrown when performing dns.lookup.

This is reproducible with v0.12.2 x64 on Windows 7 when offline:

var net = require("net");
var socket = net.connect(23053, "localhost");
/**
> Error: getaddrinfo ENOENT
    at Object.exports._errnoException (util.js:746:11)
    at errnoException (dns.js:49:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)
*/
var socket = net.connect(23053, "127.0.0.1");
/**
> { ... object }
*/

ENOENT is the expected behaviour per the docs (via joyent/25338:

dns.ADDRCONFIG: Returned address types are determined by the types of addresses supported by the current system. For example, IPv4 addresses are only returned if the current system has at least one IPv4 address configured. Loopback addresses are not considered.


I'd propose switching from localhost to 127.0.0.1 or ::1 as required, but it wouldn't be a a complete solution since localhost is name resolved per system in the host file. Refer to:

@misterdjules
Copy link

This seems to be a Windows specific issue. I haven't been able to reproduce it on Linux. I haven't tested it on OSX or on SmartOS. The documentation for GetAddrInfoW (i.e on Windows) states that:

If the AI_ADDRCONFIG bit is set, GetAddrInfoW will resolve only if a global address is configured. [...] The IPv4 or IPv6 loopback address is not considered a valid global address.

getaddrinfo man pages on Linux and OSX don't mention a distinction between global and local addresses, which seems to confirm that the behavior on Windows is different from the behavior on other operating systems.

This is has been fixed in other open source software projects by not using the AI_ADDRCONFIG flag on Windows when performing lookups. See for instance http://code.google.com/p/chromium/issues/detail?id=5234 and https://svn.boost.org/trac/boost/ticket/8503.

So I would suggest not using AI_ADDRCONFIG on Windows, and updating the documentation change of 4306786 about dns.ADDRCONFIG to say that the behavior regarding the lack of global addresses is specific to Windows.

/cc @joyent/node-tsc Thoughts?

@misterdjules
Copy link

So I would suggest not using AI_ADDRCONFIG on Windows

I'm actually not sure that this is the best option. I should have investigated the impact of such a change more thoroughly before making such a suggestion, but that's one option.

@orangemocha orangemocha self-assigned this Jun 12, 2015
@sheymann
Copy link

Reproduced on Windows 8 with node v0.12.4

orangemocha referenced this issue in vladimir-shcherbakov/node Oct 1, 2015
vladimir-shcherbakov referenced this issue in vladimir-shcherbakov/node Oct 6, 2015
@vladimir-shcherbakov
Copy link

If the issue is still relevant - please join the discussion here.

@3foil
Copy link

3foil commented Oct 12, 2015

Is there any example that on Windows using getaddrinfo function without AI_ADDRCONFIG flag is impossible or inconvenient or slow??? When AI_ADDRCONFIG flag could be extremely important???

@RobertDiebels
Copy link

Would like to confirm the issue is still relevant (see Karma issue above).

@sam-github
Copy link

@RobertDiebels can re-open a new issue on nodejs/node if this is still relevant. Its unrelated to the issue you thought it was related to (nodejs/node#8436)

@aquamoth
Copy link

Just want to chip in that @RobertDiebels is still relevant. Feels really stupid bode doesnt resolve localhost when the network is missing..

@sam-github
Copy link

Could you provide some more info:

  • When you do ping localhost with no net, what happens?
  • When you go to http://localhost:8080 with no net, what happens?

Is it just node having trouble, or is this just how domain resolution works?

@gtirloni
Copy link

gtirloni commented Jun 1, 2017

System tools can resolve 'localhost' when the network is offline:

C:\>telnet localhost 8080
Connecting To localhost...Could not open connection to the host, on port 8080: Connect failed

C:\>ping -n 1 localhost

Pinging vagrant-10 [::1] with 32 bytes of data:
Reply from ::1: time<1ms

Ping statistics for ::1:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

node.js cannot resolve 'localhost' when the network is offline:

C:\>node -e "var net = require('net'); var client = new net.Socket(); client.connect(8080,'localhost', function() { console.log('Connected'); });"
events.js:182
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOENT localhost:8080
    at Object.exports._errnoException (util.js:1026:11)
    at errnoException (dns.js:55:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:95:26)

node version:

C:\>node -v
v8.0.0

@bnoordhuis
Copy link
Member

@gtirloni See nodejs/node#11320 for more discussion. I'll close out this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests