Skip to content

Commit

Permalink
net: use cached peername to resolve remote props
Browse files Browse the repository at this point in the history
Allows socket.remote* properties to still be accessed even after the
socket is closed. Fixes nodejs#9287
  • Loading branch information
James Hartig committed Mar 10, 2015
1 parent 2b18916 commit 8edf9ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,10 @@ function onread(nread, buffer) {


Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
return {};
}
if (!this._peername) {
if (!this._handle || !this._handle.getpeername) {
return {};
}
var out = {};
var err = this._handle.getpeername(out);
if (err) return {}; // FIXME(bnoordhuis) Throw?
Expand Down Expand Up @@ -857,6 +857,7 @@ Socket.prototype.connect = function(options, cb) {
this._writableState.errorEmitted = false;
this.destroyed = false;
this._handle = null;
this._peername = null;
}

var self = this;
Expand Down
12 changes: 12 additions & 0 deletions test/simple/test-net-remote-address-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ var server = net.createServer(function(socket) {
socket.on('end', function() {
if (++conns_closed == 2) server.close();
});
socket.on('close', function() {
assert.notEqual(-1, remoteAddrCandidates.indexOf(socket.remoteAddress));
assert.notEqual(-1, remoteFamilyCandidates.indexOf(socket.remoteFamily));
});
socket.resume();
});

Expand All @@ -53,12 +57,20 @@ server.listen(common.PORT, 'localhost', function() {
assert.equal(common.PORT, client.remotePort);
client.end();
});
client.on('close', function() {
assert.notEqual(-1, remoteAddrCandidates.indexOf(client.remoteAddress));
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client.remoteFamily));
});
client2.on('connect', function() {
assert.notEqual(-1, remoteAddrCandidates.indexOf(client2.remoteAddress));
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client2.remoteFamily));
assert.equal(common.PORT, client2.remotePort);
client2.end();
});
client2.on('close', function() {
assert.notEqual(-1, remoteAddrCandidates.indexOf(client2.remoteAddress));
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client2.remoteFamily));
});
});

process.on('exit', function() {
Expand Down

0 comments on commit 8edf9ed

Please sign in to comment.