Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

net-uv: correctly set socket.remoteAddress and -port

closes #1345
  • Loading branch information...
piscisaureus committed Sep 5, 2011
1 parent ae2ac4f commit e20d0c1cd09000169e9023a12a1d5427f8341245
Showing with 59 additions and 7 deletions.
  1. +19 −7 lib/net_uv.js
  2. +38 −0 src/tcp_wrap.cc
  3. +2 −0 test/simple/test-net-remote-address-port.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++;
@@ -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<Function>::New(t->GetFunction());

@@ -153,7 +154,44 @@ class TCPWrap : public StreamWrap {
}

return scope.Close(sockname);
}


static Handle<Value> 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<sockaddr*>(&address),
&addrlen);

Local<Object> 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);
}


@@ -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();
});

0 comments on commit e20d0c1

Please sign in to comment.
You can’t perform that action at this time.