@@ -97,6 +97,10 @@ const {
9797
9898function noop ( ) { }
9999
100+ function getFlags ( ipv6Only ) {
101+ return ipv6Only === true ? TCPConstants . UV_TCP_IPV6ONLY : 0 ;
102+ }
103+
100104function createHandle ( fd , is_server ) {
101105 validateInt32 ( fd , 'fd' , 0 ) ;
102106 const type = TTYWrap . guessHandleType ( fd ) ;
@@ -798,7 +802,7 @@ function checkBindError(err, port, handle) {
798802
799803
800804function internalConnect (
801- self , address , port , addressType , localAddress , localPort ) {
805+ self , address , port , addressType , localAddress , localPort , flags ) {
802806 // TODO return promise from Socket.prototype.connect which
803807 // wraps _connectReq.
804808
@@ -812,7 +816,7 @@ function internalConnect(
812816 err = self . _handle . bind ( localAddress , localPort ) ;
813817 } else { // addressType === 6
814818 localAddress = localAddress || '::' ;
815- err = self . _handle . bind6 ( localAddress , localPort ) ;
819+ err = self . _handle . bind6 ( localAddress , localPort , flags ) ;
816820 }
817821 debug ( 'binding to localAddress: %s and localPort: %d (addressType: %d)' ,
818822 localAddress , localPort , addressType ) ;
@@ -1148,7 +1152,7 @@ util.inherits(Server, EventEmitter);
11481152function toNumber ( x ) { return ( x = Number ( x ) ) >= 0 ? x : false ; }
11491153
11501154// Returns handle if it can be created, or error code if it can't
1151- function createServerHandle ( address , port , addressType , fd ) {
1155+ function createServerHandle ( address , port , addressType , fd , flags ) {
11521156 var err = 0 ;
11531157 // assign handle in listen, and clean up if bind or listen fails
11541158 var handle ;
@@ -1187,14 +1191,14 @@ function createServerHandle(address, port, addressType, fd) {
11871191 debug ( 'bind to' , address || 'any' ) ;
11881192 if ( ! address ) {
11891193 // Try binding to ipv6 first
1190- err = handle . bind6 ( '::' , port ) ;
1194+ err = handle . bind6 ( '::' , port , flags ) ;
11911195 if ( err ) {
11921196 handle . close ( ) ;
11931197 // Fallback to ipv4
11941198 return createServerHandle ( '0.0.0.0' , port ) ;
11951199 }
11961200 } else if ( addressType === 6 ) {
1197- err = handle . bind6 ( address , port ) ;
1201+ err = handle . bind6 ( address , port , flags ) ;
11981202 } else {
11991203 err = handle . bind ( address , port ) ;
12001204 }
@@ -1208,7 +1212,7 @@ function createServerHandle(address, port, addressType, fd) {
12081212 return handle ;
12091213}
12101214
1211- function setupListenHandle ( address , port , addressType , backlog , fd ) {
1215+ function setupListenHandle ( address , port , addressType , backlog , fd , flags ) {
12121216 debug ( 'setupListenHandle' , address , port , addressType , backlog , fd ) ;
12131217
12141218 // If there is not yet a handle, we need to create one and bind.
@@ -1222,7 +1226,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
12221226
12231227 // Try to bind to the unspecified IPv6 address, see if IPv6 is available
12241228 if ( ! address && typeof fd !== 'number' ) {
1225- rval = createServerHandle ( '::' , port , 6 , fd ) ;
1229+ rval = createServerHandle ( '::' , port , 6 , fd , flags ) ;
12261230
12271231 if ( typeof rval === 'number' ) {
12281232 rval = null ;
@@ -1235,7 +1239,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
12351239 }
12361240
12371241 if ( rval === null )
1238- rval = createServerHandle ( address , port , addressType , fd ) ;
1242+ rval = createServerHandle ( address , port , addressType , fd , flags ) ;
12391243
12401244 if ( typeof rval === 'number' ) {
12411245 var error = uvExceptionWithHostPort ( rval , 'listen' , address , port ) ;
@@ -1294,7 +1298,7 @@ function emitListeningNT(self) {
12941298
12951299
12961300function listenInCluster ( server , address , port , addressType ,
1297- backlog , fd , exclusive ) {
1301+ backlog , fd , exclusive , flags ) {
12981302 exclusive = ! ! exclusive ;
12991303
13001304 if ( cluster === undefined ) cluster = require ( 'cluster' ) ;
@@ -1303,7 +1307,7 @@ function listenInCluster(server, address, port, addressType,
13031307 // Will create a new handle
13041308 // _listen2 sets up the listened handle, it is still named like this
13051309 // to avoid breaking code that wraps this method
1306- server . _listen2 ( address , port , addressType , backlog , fd ) ;
1310+ server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
13071311 return ;
13081312 }
13091313
@@ -1312,7 +1316,7 @@ function listenInCluster(server, address, port, addressType,
13121316 port : port ,
13131317 addressType : addressType ,
13141318 fd : fd ,
1315- flags : 0
1319+ flags,
13161320 } ;
13171321
13181322 // Get the master's server handle, and listen on it
@@ -1330,7 +1334,7 @@ function listenInCluster(server, address, port, addressType,
13301334 server . _handle = handle ;
13311335 // _listen2 sets up the listened handle, it is still named like this
13321336 // to avoid breaking code that wraps this method
1333- server . _listen2 ( address , port , addressType , backlog , fd ) ;
1337+ server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
13341338 }
13351339}
13361340
@@ -1353,6 +1357,7 @@ Server.prototype.listen = function(...args) {
13531357 toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
13541358
13551359 options = options . _handle || options . handle || options ;
1360+ const flags = getFlags ( options . ipv6Only ) ;
13561361 // (handle[, backlog][, cb]) where handle is an object with a handle
13571362 if ( options instanceof TCP ) {
13581363 this . _handle = options ;
@@ -1387,7 +1392,7 @@ Server.prototype.listen = function(...args) {
13871392 // start TCP server listening on host:port
13881393 if ( options . host ) {
13891394 lookupAndListen ( this , options . port | 0 , options . host , backlog ,
1390- options . exclusive ) ;
1395+ options . exclusive , flags ) ;
13911396 } else { // Undefined host, listens on unspecified address
13921397 // Default addressType 4 will be used to search for master server
13931398 listenInCluster ( this , null , options . port | 0 , 4 ,
@@ -1434,15 +1439,15 @@ Server.prototype.listen = function(...args) {
14341439 throw new ERR_INVALID_OPT_VALUE ( 'options' , util . inspect ( options ) ) ;
14351440} ;
14361441
1437- function lookupAndListen ( self , port , address , backlog , exclusive ) {
1442+ function lookupAndListen ( self , port , address , backlog , exclusive , flags ) {
14381443 if ( dns === undefined ) dns = require ( 'dns' ) ;
14391444 dns . lookup ( address , function doListen ( err , ip , addressType ) {
14401445 if ( err ) {
14411446 self . emit ( 'error' , err ) ;
14421447 } else {
14431448 addressType = ip ? addressType : 4 ;
14441449 listenInCluster ( self , ip , port , addressType ,
1445- backlog , undefined , exclusive ) ;
1450+ backlog , undefined , exclusive , flags ) ;
14461451 }
14471452 } ) ;
14481453}
0 commit comments