From 79f064f565abdbdb09b8f394250e02cf2e2ff729 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 11 Aug 2011 17:41:30 +0200 Subject: [PATCH] net: properly export remoteAddress to user land Fixes failing test: test/simple/test-net-remote-address-port.js --- lib/net_legacy.js | 6 ++++++ lib/net_uv.js | 13 ++++++++++++- test/simple/test-net-remote-address-port.js | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/net_legacy.js b/lib/net_legacy.js index 4221aa1146..2abe2cde7a 100644 --- a/lib/net_legacy.js +++ b/lib/net_legacy.js @@ -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); diff --git a/lib/net_uv.js b/lib/net_uv.js index c89c263bfa..e990cb9aa9 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -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? } } @@ -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); } }); diff --git a/test/simple/test-net-remote-address-port.js b/test/simple/test-net-remote-address-port.js index 8eaa516f37..95cfc04d79 100644 --- a/test/simple/test-net-remote-address-port.js +++ b/test/simple/test-net-remote-address-port.js @@ -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(); }); });