Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: move default address logic to _listen2 #539

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,30 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {

// If there is not yet a handle, we need to create one and bind.
// In the case of a server sent via IPC, we don't need to do this.
if (!self._handle) {
if (self._handle) {
debug('_listen2: have a handle already');
} else {
debug('_listen2: create a handle');
var rval = createServerHandle(address, port, addressType, fd);

var rval = null;

if (!address && !util.isNumber(fd)) {
rval = createServerHandle('::', port, 6, fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferring IPv6 is a rather breaking change, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right. Okay, complaint withdrawn. :-)

Maybe you can swap the clauses of the if !(self.handle) statement. The consequent is big enough now that it doesn't fit on a single page anymore, somewhat hiding the alternate.


if (util.isNumber(rval)) {
rval = null;
address = '0.0.0.0';
addressType = 4;
} else {
address = '::';
addressType = 6;
}

}

if (rval === null)
rval = createServerHandle(address, port, addressType, fd);

if (util.isNumber(rval)) {
var error = exceptionWithHostPort(rval, 'listen', address, port);
process.nextTick(function() {
Expand All @@ -1121,8 +1142,6 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
return;
}
self._handle = rval;
} else {
debug('_listen2: have a handle already');
}

self._handle.onconnection = onconnection;
Expand Down
11 changes: 11 additions & 0 deletions test/sequential/test-net-server-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ server0.listen(function() {
// No callback to listen(), assume we can bind in 100 ms

var address1;
var connectionKey1;
var server1 = net.createServer(function(socket) { });

server1.listen(common.PORT);

setTimeout(function() {
address1 = server1.address();
connectionKey1 = server1._connectionKey;
console.log('address1 %j', address1);
server1.close();
}, 100);
Expand Down Expand Up @@ -68,6 +70,15 @@ server4.listen(common.PORT + 3, 127, function() {
process.on('exit', function() {
assert.ok(address0.port > 100);
assert.equal(common.PORT, address1.port);

var expectedConnectionKey1;

if (address1.family === 'IPv6')
expectedConnectionKey1 = '6::::' + address1.port;
else
expectedConnectionKey1 = '4:0.0.0.0:' + address1.port;

assert.equal(connectionKey1, expectedConnectionKey1);
assert.equal(common.PORT + 1, address2.port);
assert.equal(common.PORT + 2, address3.port);
assert.equal(common.PORT + 3, address4.port);
Expand Down