From 30666f22ca5041a991ab9e7311916ac218ed4c0f Mon Sep 17 00:00:00 2001 From: James Hartig Date: Tue, 10 Mar 2015 16:48:19 -0400 Subject: [PATCH] net: use cached peername to resolve remote fields Allows socket.remote* properties to still be accessed even after the socket is closed. Fixes: https://github.com/joyent/node/issues/9287 PR-URL: https://github.com/joyent/node/pull/9366 Reviewed-By: Colin Ihrig --- lib/net.js | 7 ++++--- test/parallel/test-net-remote-address-port.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index aafb707ca45654..9aca39200033b8 100644 --- a/lib/net.js +++ b/lib/net.js @@ -556,10 +556,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? @@ -865,6 +865,7 @@ Socket.prototype.connect = function(options, cb) { this._writableState.errorEmitted = false; this.destroyed = false; this._handle = null; + this._peername = null; } var self = this; diff --git a/test/parallel/test-net-remote-address-port.js b/test/parallel/test-net-remote-address-port.js index 7be95ed15cc501..62b28f9cb465c9 100644 --- a/test/parallel/test-net-remote-address-port.js +++ b/test/parallel/test-net-remote-address-port.js @@ -20,6 +20,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(); }); @@ -32,12 +36,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() {