Skip to content
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
net: remoteAddress always undefined called before connected
  • Loading branch information
Y1D7NG committed May 8, 2022
commit 7d7f3f235687686b68746726318e5d7b76fdc214
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