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

Commit

Permalink
net-uv: correctly set socket.remoteAddress and -port
Browse files Browse the repository at this point in the history
closes #1345
  • Loading branch information
piscisaureus committed Sep 5, 2011
1 parent ae2ac4f commit e20d0c1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/net_uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
}

Expand Down Expand Up @@ -689,6 +682,7 @@ Server.prototype.address = function() {
function onconnection(clientHandle) {
var handle = this;
var self = handle.socket;
var peername;

debug("onconnection");

Expand All @@ -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++;
Expand Down
38 changes: 38 additions & 0 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}


Expand Down
2 changes: 2 additions & 0 deletions test/simple/test-net-remote-address-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit e20d0c1

Please sign in to comment.