Skip to content

Commit

Permalink
net: bind to :: TCP address by default
Browse files Browse the repository at this point in the history
Try binding TCP socket to `::` first before falling back to
`0.0.0.0`.
  • Loading branch information
indutny committed Apr 14, 2014
1 parent c61b0e9 commit 2272052
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,8 +1083,16 @@ var createServerHandle = exports._createServerHandle =
}

if (address || port) {
debug('bind to ' + address);
if (addressType === 6) {
debug('bind to ' + (address || 'anycast'));
if (!address) {
// Try binding to ipv6 first
err = handle.bind6('::', port);
if (err) {
handle.close();
// Fallback to ipv4
return createServerHandle('0.0.0.0', port);
}
} else if (addressType === 6) {
err = handle.bind6(address, port);
} else {
err = handle.bind(address, port);
Expand Down Expand Up @@ -1214,7 +1222,7 @@ Server.prototype.listen = function() {

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

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

} else {
// The first argument is the port, the second an IP.
require('dns').lookup(arguments[1], function(err, ip, addressType) {
if (err) {
self.emit('error', err);
} else {
listen(self, ip || '0.0.0.0', port, ip ? addressType : 4, backlog);
listen(self, ip, port, ip ? addressType : 4, backlog);
}
});
}
Expand Down

0 comments on commit 2272052

Please sign in to comment.