Skip to content

net: ensure Socket reported address is current #2095

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

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
4 changes: 4 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function initSocketHandle(self) {
self.destroyed = false;
self.bytesRead = 0;
self._bytesDispatched = 0;
self._sockname = null;

// Handle creation may be deferred to bind() or connect() time.
if (self._handle) {
Expand Down Expand Up @@ -469,6 +470,7 @@ Socket.prototype._destroy = function(exception, cb) {
});
this._handle.onread = noop;
this._handle = null;
this._sockname = null;
}

// we set destroyed to true before firing error callbacks in order
Expand Down Expand Up @@ -871,6 +873,7 @@ Socket.prototype.connect = function(options, cb) {
this.destroyed = false;
this._handle = null;
this._peername = null;
this._sockname = null;
}

var self = this;
Expand Down Expand Up @@ -1023,6 +1026,7 @@ function afterConnect(status, handle, req, readable, writable) {

assert.ok(self._connecting);
self._connecting = false;
self._sockname = null;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it logically still be null here? Not that an extra assignment hurts, just wondering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it probably is already be null here, but to me this also is the most logical place to be invalidating the cached value.


if (status == 0) {
self.readable = readable;
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-net-socket-local-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var net = require('net');

var conns = 0;
var clientLocalPorts = [];
var serverRemotePorts = [];

var server = net.createServer(function(socket) {
serverRemotePorts.push(socket.remotePort);
conns++;
});

var client = new net.Socket();

server.on('close', function() {
assert.deepEqual(clientLocalPorts, serverRemotePorts,
'client and server should agree on the ports used');
assert.equal(2, conns);
});

server.listen(common.PORT, common.localhostIPv4, testConnect);

function testConnect() {
if (conns == 2) {
return server.close();
}
client.connect(common.PORT, common.localhostIPv4, function() {
clientLocalPorts.push(this.localPort);
this.once('close', testConnect);
this.destroy();
});
}