From e20d0c1cd09000169e9023a12a1d5427f8341245 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Mon, 5 Sep 2011 02:09:24 +0200 Subject: [PATCH] net-uv: correctly set socket.remoteAddress and -port closes #1345 --- lib/net_uv.js | 26 ++++++++++---- src/tcp_wrap.cc | 38 +++++++++++++++++++++ test/simple/test-net-remote-address-port.js | 2 ++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/lib/net_uv.js b/lib/net_uv.js index 532a231327c..bda6ad84500 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -69,13 +69,6 @@ function initSocketHandle(self) { if (self._handle) { self._handle.socket = self; self._handle.onread = onread; - - if (self._handle.getsockname) { - var sockname = self._handle.getsockname(); - self.remoteAddress = sockname.address; - self.remotePort = sockname.port; - } - // also export sockname.family? } } @@ -689,6 +682,7 @@ Server.prototype.address = function() { function onconnection(clientHandle) { var handle = this; var self = handle.socket; + var peername; debug("onconnection"); @@ -702,11 +696,29 @@ function onconnection(clientHandle) { return; } + // Todo: implement this for unix sockets + if (clientHandle.getpeername) { + peername = clientHandle.getpeername(); + if (!peername.address || !peername.port) { + var err = errnoException(errno, 'accept'); + clientHandle.close(); + set.emit('error', err); + return; + } + } + var socket = new Socket({ handle: clientHandle, allowHalfOpen: self.allowHalfOpen }); socket.readable = socket.writable = true; + + if (peername) { + socket.remoteAddress = peername.address; + socket.remotePort = peername.port; + // TODO: set family as well + } + socket.resume(); self.connections++; diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index ccd3b138f1f..773ce91024e 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -82,6 +82,7 @@ class TCPWrap : public StreamWrap { NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6); NODE_SET_PROTOTYPE_METHOD(t, "connect6", Connect6); NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName); + NODE_SET_PROTOTYPE_METHOD(t, "getpeername", GetPeerName); tcpConstructor = Persistent::New(t->GetFunction()); @@ -153,7 +154,44 @@ class TCPWrap : public StreamWrap { } return scope.Close(sockname); + } + + + static Handle GetPeerName(const Arguments& args) { + HandleScope scope; + struct sockaddr address; + int family; + int port; + char ip[INET6_ADDRSTRLEN]; + + UNWRAP + + int addrlen = sizeof(address); + int r = uv_tcp_getpeername(&wrap->handle_, + reinterpret_cast(&address), + &addrlen); + Local sockname = Object::New(); + if (r != 0) { + SetErrno(uv_last_error(uv_default_loop()).code); + } else { + family = address.sa_family; + if (family == AF_INET) { + struct sockaddr_in* addrin = (struct sockaddr_in*)&address; + uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, INET6_ADDRSTRLEN); + port = ntohs(addrin->sin_port); + } else if (family == AF_INET6) { + struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)&address; + uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, INET6_ADDRSTRLEN); + port = ntohs(addrin6->sin6_port); + } + + sockname->Set(port_symbol, Integer::New(port)); + sockname->Set(family_symbol, Integer::New(family)); + sockname->Set(address_symbol, String::New(ip)); + } + + return scope.Close(sockname); } diff --git a/test/simple/test-net-remote-address-port.js b/test/simple/test-net-remote-address-port.js index 95cfc04d79e..103e1db2b32 100644 --- a/test/simple/test-net-remote-address-port.js +++ b/test/simple/test-net-remote-address-port.js @@ -8,6 +8,8 @@ var conns = 0, conns_closed = 0; var server = net.createServer(function(socket) { conns++; assert.equal('127.0.0.1', socket.remoteAddress); + assert.ok(socket.remotePort); + assert.notEqual(socket.remotePort, common.PORT); socket.on('end', function() { if (++conns_closed == 2) server.close(); });