From: Fedor Indutny Date: Sun, 13 Apr 2014 15:11:56 +0000 (+0400) Subject: net: bind to `::` TCP address by default X-Git-Tag: v0.11.13~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2272052461445dfcae729cc6420a3d3229362158;p=platform%2Fupstream%2Fnodejs.git net: bind to `::` TCP address by default Try binding TCP socket to `::` first before falling back to `0.0.0.0`. --- diff --git a/lib/net.js b/lib/net.js index 751b04b..84c6833 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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); @@ -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]; @@ -1240,7 +1248,7 @@ 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. @@ -1248,7 +1256,7 @@ Server.prototype.listen = function() { 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); } }); }