Skip to content

Commit

Permalink
net: remoteAddress always undefined called before connected
Browse files Browse the repository at this point in the history
PR-URL: #43011
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
y1d7ng authored and bengl committed May 30, 2022
1 parent 7d8d9d6 commit 11c783f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ Socket.prototype._destroy = function(exception, cb) {
};

Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
if (!this._handle || !this._handle.getpeername || this.connecting) {
return this._peername || {};
} else if (!this._peername) {
this._peername = {};
Expand All @@ -760,7 +760,9 @@ protoGetter('remoteAddress', function remoteAddress() {
});

protoGetter('remoteFamily', function remoteFamily() {
return `IPv${this._getpeername().family}`;
const { family } = this._getpeername();

return family ? `IPv${family}` : family;
});

protoGetter('remotePort', function remotePort() {
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-net-remote-address-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ const server = net.createServer(common.mustCall(function(socket) {
server.listen(0, function() {
const client = net.createConnection(this.address().port, '127.0.0.1');
const client2 = net.createConnection(this.address().port);

assert.strictEqual(client.remoteAddress, undefined);
assert.strictEqual(client.remoteFamily, undefined);
assert.strictEqual(client.remotePort, undefined);
assert.strictEqual(client2.remoteAddress, undefined);
assert.strictEqual(client2.remoteFamily, undefined);
assert.strictEqual(client2.remotePort, undefined);

client.on('connect', function() {
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
Expand Down

0 comments on commit 11c783f

Please sign in to comment.