Closed
Description
What is the problem this feature will solve?
Naive calls to createServer happily resolve host names to link-local addresses, which then fail to listen:
$ node -e "net.createServer(c=>console.log).listen(5555,'zen')"
node:events:492
throw er; // Unhandled 'error' event
^
Error: listen EINVAL: invalid argument fe80::2d8:xxxx:xxxx:xxxx:5555
at Server.setupListenHandle [as _listen2] (node:net:1855:21)
at listenInCluster (node:net:1920:12)
at GetAddrInfoReqWrap.doListen [as callback] (node:net:2069:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:8)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1899:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'EINVAL',
errno: -22,
syscall: 'listen',
address: 'fe80::2d8:xxxx:xxxx:xxxx',
port: 5555
}
Node.js v20.10.0
What is the feature you are proposing to solve the problem?
DNS lookup randomly picks the first available entry
> dns.lookup('zen', { verbatim: true }, console.info)
GetAddrInfoReqWrap {
callback: [Function: info],
family: 0,
hostname: 'zen',
oncomplete: [Function: onlookup]
}
> null fe80::2d8:xxxx:xxxx:xxxx 6
if there are multiple addresses returned by getaddrinfo
> dns.lookup('zen', { verbatim: true, all: true }, console.info)
GetAddrInfoReqWrap {
callback: [Function: info],
family: 0,
hostname: 'zen',
oncomplete: [Function: onlookupall]
}
> null [
{ address: 'fe80::2d8:xxxx:xxxx:xxxx', family: 6 },
{ address: '2a02:8109:xxxx:xxxx:2d8:xxxx:xxxx:xxxx', family: 6 },
{ address: '192.168.178.3', family: 4 },
{ address: '192.168.122.1', family: 4 }
Instead, it should pick the first non-link-local address. It is extremely unlikely that you want to listen to a link-local address (starting with fe80::
). And in the unlikely case that you do, you certainly would call with options.all
to get all resolutions.
What alternatives have you considered?
We could alter all client code to always add options.all
and filter, but it seems like node is the correct way to fix this once and for all.