Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
net: properly export remoteAddress to user land
Browse files Browse the repository at this point in the history
Fixes failing test:
  test/simple/test-net-remote-address-port.js
  • Loading branch information
bnoordhuis committed Aug 11, 2011
1 parent f52a8db commit 79f064f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/net_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ Socket.prototype.connect = function() {
if (err) {
self.emit('error', err);
} else {
addressType = addressType || 4;

// node_net.cc handles null host names graciously but user land
// expects remoteAddress to have a meaningful value
ip = ip || (addressType === 4 ? '127.0.0.1' : '0:0:0:0:0:0:0:1');

timers.active(self);
self.type = addressType == 4 ? 'tcp4' : 'tcp6';
self.fd = socket(self.type);
Expand Down
13 changes: 12 additions & 1 deletion lib/net_uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ function initSocketHandle(self) {
if (self._handle) {
self._handle.socket = self;
self._handle.onread = onread;

var sockname = self._handle.getsockname();
self.remoteAddress = sockname.address;
self.remotePort = sockname.port;
// also export sockname.family?
}
}

Expand Down Expand Up @@ -464,7 +469,13 @@ Socket.prototype.connect = function(port /* [host], [cb] */) {
} else {
timers.active(self);

connect(self, ip || '127.0.0.1', port, ip ? addressType : 4);
addressType = addressType || 4;

// node_net.cc handles null host names graciously but user land
// expects remoteAddress to have a meaningful value
ip = ip || (addressType === 4 ? '127.0.0.1' : '0:0:0:0:0:0:0:1');

connect(self, ip, port, addressType);
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/simple/test-net-remote-address-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ var assert = require('assert');

var net = require('net');

var conns = 0;
var conns = 0, conns_closed = 0;

var server = net.createServer(function(socket) {
conns++;
assert.equal('127.0.0.1', socket.remoteAddress);
socket.on('end', function() {
server.close();
if (++conns_closed == 2) server.close();
});
});

Expand Down

0 comments on commit 79f064f

Please sign in to comment.