Skip to content

Commit 2272052

Browse files
committed
net: bind to :: TCP address by default
Try binding TCP socket to `::` first before falling back to `0.0.0.0`.
1 parent c61b0e9 commit 2272052

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/net.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,16 @@ var createServerHandle = exports._createServerHandle =
10831083
}
10841084

10851085
if (address || port) {
1086-
debug('bind to ' + address);
1087-
if (addressType === 6) {
1086+
debug('bind to ' + (address || 'anycast'));
1087+
if (!address) {
1088+
// Try binding to ipv6 first
1089+
err = handle.bind6('::', port);
1090+
if (err) {
1091+
handle.close();
1092+
// Fallback to ipv4
1093+
return createServerHandle('0.0.0.0', port);
1094+
}
1095+
} else if (addressType === 6) {
10881096
err = handle.bind6(address, port);
10891097
} else {
10901098
err = handle.bind(address, port);
@@ -1214,7 +1222,7 @@ Server.prototype.listen = function() {
12141222

12151223
if (arguments.length == 0 || util.isFunction(arguments[0])) {
12161224
// Bind to a random port.
1217-
listen(self, '0.0.0.0', 0, null, backlog);
1225+
listen(self, null, 0, null, backlog);
12181226

12191227
} else if (arguments[0] && util.isObject(arguments[0])) {
12201228
var h = arguments[0];
@@ -1240,15 +1248,15 @@ Server.prototype.listen = function() {
12401248
util.isFunction(arguments[1]) ||
12411249
util.isNumber(arguments[1])) {
12421250
// The first argument is the port, no IP given.
1243-
listen(self, '0.0.0.0', port, 4, backlog);
1251+
listen(self, null, port, 4, backlog);
12441252

12451253
} else {
12461254
// The first argument is the port, the second an IP.
12471255
require('dns').lookup(arguments[1], function(err, ip, addressType) {
12481256
if (err) {
12491257
self.emit('error', err);
12501258
} else {
1251-
listen(self, ip || '0.0.0.0', port, ip ? addressType : 4, backlog);
1259+
listen(self, ip, port, ip ? addressType : 4, backlog);
12521260
}
12531261
});
12541262
}

0 commit comments

Comments
 (0)