Skip to content

Commit 8de89ec

Browse files
vkurchatkinbnoordhuis
authored andcommitted
lib: move default address logic to net._listen2
When address is not provided to `server.listen()`, `_connectionKey` and error messages should include actual address and correct family. PR-URL: #539 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 3143d73 commit 8de89ec

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/net.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,9 +1110,29 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
11101110

11111111
// If there is not yet a handle, we need to create one and bind.
11121112
// In the case of a server sent via IPC, we don't need to do this.
1113-
if (!self._handle) {
1113+
if (self._handle) {
1114+
debug('_listen2: have a handle already');
1115+
} else {
11141116
debug('_listen2: create a handle');
1115-
var rval = createServerHandle(address, port, addressType, fd);
1117+
1118+
var rval = null;
1119+
1120+
if (!address && !util.isNumber(fd)) {
1121+
rval = createServerHandle('::', port, 6, fd);
1122+
1123+
if (util.isNumber(rval)) {
1124+
rval = null;
1125+
address = '0.0.0.0';
1126+
addressType = 4;
1127+
} else {
1128+
address = '::';
1129+
addressType = 6;
1130+
}
1131+
}
1132+
1133+
if (rval === null)
1134+
rval = createServerHandle(address, port, addressType, fd);
1135+
11161136
if (util.isNumber(rval)) {
11171137
var error = exceptionWithHostPort(rval, 'listen', address, port);
11181138
process.nextTick(function() {
@@ -1121,8 +1141,6 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
11211141
return;
11221142
}
11231143
self._handle = rval;
1124-
} else {
1125-
debug('_listen2: have a handle already');
11261144
}
11271145

11281146
self._handle.onconnection = onconnection;

test/sequential/test-net-server-bind.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ server0.listen(function() {
1818
// No callback to listen(), assume we can bind in 100 ms
1919

2020
var address1;
21+
var connectionKey1;
2122
var server1 = net.createServer(function(socket) { });
2223

2324
server1.listen(common.PORT);
2425

2526
setTimeout(function() {
2627
address1 = server1.address();
28+
connectionKey1 = server1._connectionKey;
2729
console.log('address1 %j', address1);
2830
server1.close();
2931
}, 100);
@@ -68,6 +70,15 @@ server4.listen(common.PORT + 3, 127, function() {
6870
process.on('exit', function() {
6971
assert.ok(address0.port > 100);
7072
assert.equal(common.PORT, address1.port);
73+
74+
var expectedConnectionKey1;
75+
76+
if (address1.family === 'IPv6')
77+
expectedConnectionKey1 = '6::::' + address1.port;
78+
else
79+
expectedConnectionKey1 = '4:0.0.0.0:' + address1.port;
80+
81+
assert.equal(connectionKey1, expectedConnectionKey1);
7182
assert.equal(common.PORT + 1, address2.port);
7283
assert.equal(common.PORT + 2, address3.port);
7384
assert.equal(common.PORT + 3, address4.port);

0 commit comments

Comments
 (0)