@@ -96,6 +96,10 @@ const {
9696
9797function noop ( ) { }
9898
99+ function getFlags ( ipv6Only ) {
100+ return ipv6Only === true ? TCPConstants . UV_TCP_IPV6ONLY : 0 ;
101+ }
102+
99103function createHandle ( fd , is_server ) {
100104 validateInt32 ( fd , 'fd' , 0 ) ;
101105 const type = TTYWrap . guessHandleType ( fd ) ;
@@ -826,7 +830,7 @@ function checkBindError(err, port, handle) {
826830
827831
828832function internalConnect (
829- self , address , port , addressType , localAddress , localPort ) {
833+ self , address , port , addressType , localAddress , localPort , flags ) {
830834 // TODO return promise from Socket.prototype.connect which
831835 // wraps _connectReq.
832836
@@ -840,7 +844,7 @@ function internalConnect(
840844 err = self . _handle . bind ( localAddress , localPort ) ;
841845 } else { // addressType === 6
842846 localAddress = localAddress || '::' ;
843- err = self . _handle . bind6 ( localAddress , localPort ) ;
847+ err = self . _handle . bind6 ( localAddress , localPort , flags ) ;
844848 }
845849 debug ( 'binding to localAddress: %s and localPort: %d (addressType: %d)' ,
846850 localAddress , localPort , addressType ) ;
@@ -1176,7 +1180,7 @@ util.inherits(Server, EventEmitter);
11761180function toNumber ( x ) { return ( x = Number ( x ) ) >= 0 ? x : false ; }
11771181
11781182// Returns handle if it can be created, or error code if it can't
1179- function createServerHandle ( address , port , addressType , fd ) {
1183+ function createServerHandle ( address , port , addressType , fd , flags ) {
11801184 var err = 0 ;
11811185 // assign handle in listen, and clean up if bind or listen fails
11821186 var handle ;
@@ -1215,14 +1219,14 @@ function createServerHandle(address, port, addressType, fd) {
12151219 debug ( 'bind to' , address || 'any' ) ;
12161220 if ( ! address ) {
12171221 // Try binding to ipv6 first
1218- err = handle . bind6 ( '::' , port ) ;
1222+ err = handle . bind6 ( '::' , port , flags ) ;
12191223 if ( err ) {
12201224 handle . close ( ) ;
12211225 // Fallback to ipv4
12221226 return createServerHandle ( '0.0.0.0' , port ) ;
12231227 }
12241228 } else if ( addressType === 6 ) {
1225- err = handle . bind6 ( address , port ) ;
1229+ err = handle . bind6 ( address , port , flags ) ;
12261230 } else {
12271231 err = handle . bind ( address , port ) ;
12281232 }
@@ -1236,7 +1240,7 @@ function createServerHandle(address, port, addressType, fd) {
12361240 return handle ;
12371241}
12381242
1239- function setupListenHandle ( address , port , addressType , backlog , fd ) {
1243+ function setupListenHandle ( address , port , addressType , backlog , fd , flags ) {
12401244 debug ( 'setupListenHandle' , address , port , addressType , backlog , fd ) ;
12411245
12421246 // If there is not yet a handle, we need to create one and bind.
@@ -1250,7 +1254,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
12501254
12511255 // Try to bind to the unspecified IPv6 address, see if IPv6 is available
12521256 if ( ! address && typeof fd !== 'number' ) {
1253- rval = createServerHandle ( '::' , port , 6 , fd ) ;
1257+ rval = createServerHandle ( '::' , port , 6 , fd , flags ) ;
12541258
12551259 if ( typeof rval === 'number' ) {
12561260 rval = null ;
@@ -1263,7 +1267,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
12631267 }
12641268
12651269 if ( rval === null )
1266- rval = createServerHandle ( address , port , addressType , fd ) ;
1270+ rval = createServerHandle ( address , port , addressType , fd , flags ) ;
12671271
12681272 if ( typeof rval === 'number' ) {
12691273 var error = uvExceptionWithHostPort ( rval , 'listen' , address , port ) ;
@@ -1322,7 +1326,7 @@ function emitListeningNT(self) {
13221326
13231327
13241328function listenInCluster ( server , address , port , addressType ,
1325- backlog , fd , exclusive ) {
1329+ backlog , fd , exclusive , flags ) {
13261330 exclusive = ! ! exclusive ;
13271331
13281332 if ( cluster === undefined ) cluster = require ( 'cluster' ) ;
@@ -1331,7 +1335,7 @@ function listenInCluster(server, address, port, addressType,
13311335 // Will create a new handle
13321336 // _listen2 sets up the listened handle, it is still named like this
13331337 // to avoid breaking code that wraps this method
1334- server . _listen2 ( address , port , addressType , backlog , fd ) ;
1338+ server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
13351339 return ;
13361340 }
13371341
@@ -1340,7 +1344,7 @@ function listenInCluster(server, address, port, addressType,
13401344 port : port ,
13411345 addressType : addressType ,
13421346 fd : fd ,
1343- flags : 0
1347+ flags,
13441348 } ;
13451349
13461350 // Get the master's server handle, and listen on it
@@ -1358,7 +1362,7 @@ function listenInCluster(server, address, port, addressType,
13581362 server . _handle = handle ;
13591363 // _listen2 sets up the listened handle, it is still named like this
13601364 // to avoid breaking code that wraps this method
1361- server . _listen2 ( address , port , addressType , backlog , fd ) ;
1365+ server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
13621366 }
13631367}
13641368
@@ -1381,6 +1385,7 @@ Server.prototype.listen = function(...args) {
13811385 toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
13821386
13831387 options = options . _handle || options . handle || options ;
1388+ const flags = getFlags ( options . ipv6Only ) ;
13841389 // (handle[, backlog][, cb]) where handle is an object with a handle
13851390 if ( options instanceof TCP ) {
13861391 this . _handle = options ;
@@ -1415,7 +1420,7 @@ Server.prototype.listen = function(...args) {
14151420 // start TCP server listening on host:port
14161421 if ( options . host ) {
14171422 lookupAndListen ( this , options . port | 0 , options . host , backlog ,
1418- options . exclusive ) ;
1423+ options . exclusive , flags ) ;
14191424 } else { // Undefined host, listens on unspecified address
14201425 // Default addressType 4 will be used to search for master server
14211426 listenInCluster ( this , null , options . port | 0 , 4 ,
@@ -1462,15 +1467,15 @@ Server.prototype.listen = function(...args) {
14621467 throw new ERR_INVALID_OPT_VALUE ( 'options' , util . inspect ( options ) ) ;
14631468} ;
14641469
1465- function lookupAndListen ( self , port , address , backlog , exclusive ) {
1470+ function lookupAndListen ( self , port , address , backlog , exclusive , flags ) {
14661471 if ( dns === undefined ) dns = require ( 'dns' ) ;
14671472 dns . lookup ( address , function doListen ( err , ip , addressType ) {
14681473 if ( err ) {
14691474 self . emit ( 'error' , err ) ;
14701475 } else {
14711476 addressType = ip ? addressType : 4 ;
14721477 listenInCluster ( self , ip , port , addressType ,
1473- backlog , undefined , exclusive ) ;
1478+ backlog , undefined , exclusive , flags ) ;
14741479 }
14751480 } ) ;
14761481}
0 commit comments