@@ -97,6 +97,10 @@ const {
97
97
98
98
function noop ( ) { }
99
99
100
+ function getFlags ( ipv6Only ) {
101
+ return ipv6Only === true ? TCPConstants . UV_TCP_IPV6ONLY : 0 ;
102
+ }
103
+
100
104
function createHandle ( fd , is_server ) {
101
105
validateInt32 ( fd , 'fd' , 0 ) ;
102
106
const type = TTYWrap . guessHandleType ( fd ) ;
@@ -798,7 +802,7 @@ function checkBindError(err, port, handle) {
798
802
799
803
800
804
function internalConnect (
801
- self , address , port , addressType , localAddress , localPort ) {
805
+ self , address , port , addressType , localAddress , localPort , flags ) {
802
806
// TODO return promise from Socket.prototype.connect which
803
807
// wraps _connectReq.
804
808
@@ -812,7 +816,7 @@ function internalConnect(
812
816
err = self . _handle . bind ( localAddress , localPort ) ;
813
817
} else { // addressType === 6
814
818
localAddress = localAddress || '::' ;
815
- err = self . _handle . bind6 ( localAddress , localPort ) ;
819
+ err = self . _handle . bind6 ( localAddress , localPort , flags ) ;
816
820
}
817
821
debug ( 'binding to localAddress: %s and localPort: %d (addressType: %d)' ,
818
822
localAddress , localPort , addressType ) ;
@@ -1148,7 +1152,7 @@ util.inherits(Server, EventEmitter);
1148
1152
function toNumber ( x ) { return ( x = Number ( x ) ) >= 0 ? x : false ; }
1149
1153
1150
1154
// 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 ) {
1152
1156
var err = 0 ;
1153
1157
// assign handle in listen, and clean up if bind or listen fails
1154
1158
var handle ;
@@ -1187,14 +1191,14 @@ function createServerHandle(address, port, addressType, fd) {
1187
1191
debug ( 'bind to' , address || 'any' ) ;
1188
1192
if ( ! address ) {
1189
1193
// Try binding to ipv6 first
1190
- err = handle . bind6 ( '::' , port ) ;
1194
+ err = handle . bind6 ( '::' , port , flags ) ;
1191
1195
if ( err ) {
1192
1196
handle . close ( ) ;
1193
1197
// Fallback to ipv4
1194
1198
return createServerHandle ( '0.0.0.0' , port ) ;
1195
1199
}
1196
1200
} else if ( addressType === 6 ) {
1197
- err = handle . bind6 ( address , port ) ;
1201
+ err = handle . bind6 ( address , port , flags ) ;
1198
1202
} else {
1199
1203
err = handle . bind ( address , port ) ;
1200
1204
}
@@ -1208,7 +1212,7 @@ function createServerHandle(address, port, addressType, fd) {
1208
1212
return handle ;
1209
1213
}
1210
1214
1211
- function setupListenHandle ( address , port , addressType , backlog , fd ) {
1215
+ function setupListenHandle ( address , port , addressType , backlog , fd , flags ) {
1212
1216
debug ( 'setupListenHandle' , address , port , addressType , backlog , fd ) ;
1213
1217
1214
1218
// 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) {
1222
1226
1223
1227
// Try to bind to the unspecified IPv6 address, see if IPv6 is available
1224
1228
if ( ! address && typeof fd !== 'number' ) {
1225
- rval = createServerHandle ( '::' , port , 6 , fd ) ;
1229
+ rval = createServerHandle ( '::' , port , 6 , fd , flags ) ;
1226
1230
1227
1231
if ( typeof rval === 'number' ) {
1228
1232
rval = null ;
@@ -1235,7 +1239,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
1235
1239
}
1236
1240
1237
1241
if ( rval === null )
1238
- rval = createServerHandle ( address , port , addressType , fd ) ;
1242
+ rval = createServerHandle ( address , port , addressType , fd , flags ) ;
1239
1243
1240
1244
if ( typeof rval === 'number' ) {
1241
1245
var error = uvExceptionWithHostPort ( rval , 'listen' , address , port ) ;
@@ -1294,7 +1298,7 @@ function emitListeningNT(self) {
1294
1298
1295
1299
1296
1300
function listenInCluster ( server , address , port , addressType ,
1297
- backlog , fd , exclusive ) {
1301
+ backlog , fd , exclusive , flags ) {
1298
1302
exclusive = ! ! exclusive ;
1299
1303
1300
1304
if ( cluster === undefined ) cluster = require ( 'cluster' ) ;
@@ -1303,7 +1307,7 @@ function listenInCluster(server, address, port, addressType,
1303
1307
// Will create a new handle
1304
1308
// _listen2 sets up the listened handle, it is still named like this
1305
1309
// 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 ) ;
1307
1311
return ;
1308
1312
}
1309
1313
@@ -1312,7 +1316,7 @@ function listenInCluster(server, address, port, addressType,
1312
1316
port : port ,
1313
1317
addressType : addressType ,
1314
1318
fd : fd ,
1315
- flags : 0
1319
+ flags,
1316
1320
} ;
1317
1321
1318
1322
// Get the master's server handle, and listen on it
@@ -1330,7 +1334,7 @@ function listenInCluster(server, address, port, addressType,
1330
1334
server . _handle = handle ;
1331
1335
// _listen2 sets up the listened handle, it is still named like this
1332
1336
// 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 ) ;
1334
1338
}
1335
1339
}
1336
1340
@@ -1353,6 +1357,7 @@ Server.prototype.listen = function(...args) {
1353
1357
toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
1354
1358
1355
1359
options = options . _handle || options . handle || options ;
1360
+ const flags = getFlags ( options . ipv6Only ) ;
1356
1361
// (handle[, backlog][, cb]) where handle is an object with a handle
1357
1362
if ( options instanceof TCP ) {
1358
1363
this . _handle = options ;
@@ -1387,7 +1392,7 @@ Server.prototype.listen = function(...args) {
1387
1392
// start TCP server listening on host:port
1388
1393
if ( options . host ) {
1389
1394
lookupAndListen ( this , options . port | 0 , options . host , backlog ,
1390
- options . exclusive ) ;
1395
+ options . exclusive , flags ) ;
1391
1396
} else { // Undefined host, listens on unspecified address
1392
1397
// Default addressType 4 will be used to search for master server
1393
1398
listenInCluster ( this , null , options . port | 0 , 4 ,
@@ -1434,15 +1439,15 @@ Server.prototype.listen = function(...args) {
1434
1439
throw new ERR_INVALID_OPT_VALUE ( 'options' , util . inspect ( options ) ) ;
1435
1440
} ;
1436
1441
1437
- function lookupAndListen ( self , port , address , backlog , exclusive ) {
1442
+ function lookupAndListen ( self , port , address , backlog , exclusive , flags ) {
1438
1443
if ( dns === undefined ) dns = require ( 'dns' ) ;
1439
1444
dns . lookup ( address , function doListen ( err , ip , addressType ) {
1440
1445
if ( err ) {
1441
1446
self . emit ( 'error' , err ) ;
1442
1447
} else {
1443
1448
addressType = ip ? addressType : 4 ;
1444
1449
listenInCluster ( self , ip , port , addressType ,
1445
- backlog , undefined , exclusive ) ;
1450
+ backlog , undefined , exclusive , flags ) ;
1446
1451
}
1447
1452
} ) ;
1448
1453
}
0 commit comments