diff --git a/src/net.cc b/src/net.cc index c42d6ddd33f..04d335ea76b 100644 --- a/src/net.cc +++ b/src/net.cc @@ -9,6 +9,7 @@ #include #include #include +#include /* inet_ntop */ using namespace v8; using namespace node; @@ -28,9 +29,10 @@ using namespace node; #define ENCODING_SYMBOL String::NewSymbol("encoding") #define TIMEOUT_SYMBOL String::NewSymbol("timeout") #define SERVER_SYMBOL String::NewSymbol("server") +#define REMOTE_ADDRESS_SYMBOL String::NewSymbol("remoteAddress") #define PROTOCOL_SYMBOL String::NewSymbol("protocol") -#define CONNECTION_HANDLER_SYMBOL String::NewSymbol("connection_handler") +#define CONNECTION_HANDLER_SYMBOL String::NewSymbol("connectionHandler") #define READY_STATE_SYMBOL String::NewSymbol("readyState") #define OPEN_SYMBOL String::NewSymbol("open") @@ -522,6 +524,24 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len) return NULL; } + char ip4[INET_ADDRSTRLEN]; + char ip6[INET6_ADDRSTRLEN]; + Local remote_address; + if (addr->sa_family == AF_INET) { + struct sockaddr_in *sa = reinterpret_cast(addr); + inet_ntop(AF_INET, &(sa->sin_addr), ip4, INET_ADDRSTRLEN); + remote_address = String::New(ip4); + + } else if (addr->sa_family == AF_INET6) { + struct sockaddr_in6 *sa6 = reinterpret_cast(addr); + inet_ntop(AF_INET6, &(sa6->sin6_addr), ip6, INET6_ADDRSTRLEN); + remote_address = String::New(ip6); + + } else { + assert(0 && "received a bad sa_family"); + } + connection_handle->Set(REMOTE_ADDRESS_SYMBOL, remote_address); + Connection *connection = NODE_UNWRAP(Connection, connection_handle); if (!connection) return NULL; diff --git a/src/net.h b/src/net.h index a0f7980b44b..40e8e6a9ce1 100644 --- a/src/net.h +++ b/src/net.h @@ -160,10 +160,7 @@ class Acceptor : public ObjectWrap { static oi_socket* on_connection (oi_server *s, struct sockaddr *addr, socklen_t len) { Acceptor *acceptor = static_cast (s->data); Connection *connection = acceptor->OnConnection (addr, len); - if (connection) - return &connection->socket_; - else - return NULL; + return &connection->socket_; } oi_server server_; diff --git a/test/test-pingpong.js b/test/test-pingpong.js index 619670d8eca..5eff9bb3cdd 100644 --- a/test/test-pingpong.js +++ b/test/test-pingpong.js @@ -27,6 +27,7 @@ function Ponger (socket) { }; socket.onDisconnect = function (had_error) { + assertEquals("127.0.0.1", socket.remoteAddress); assertFalse(had_error); assertEquals("closed", socket.readyState); puts("ponger: onDisconnect"); diff --git a/website/api.html b/website/api.html index eb4941bc7c0..7b7da4cd850 100644 --- a/website/api.html +++ b/website/api.html @@ -428,6 +428,14 @@

node.tcp.Connection

assumed. +
connection.remoteAddress
+
+ The string representation of the remote IP address. For example, + "74.125.127.100" or "2001:4860:a005::68". + +

This member is only present in server-side connections.

+
+
connection.readyState
Either "closed", "open",