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

Commit

Permalink
Add connection.remoteAddress for server-side node.tcp.Connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 5, 2009
1 parent 5170823 commit 5558bc4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h> /* inet_ntop */

using namespace v8;
using namespace node;
Expand All @@ -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")
Expand Down Expand Up @@ -522,6 +524,24 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
return NULL;
}

char ip4[INET_ADDRSTRLEN];
char ip6[INET6_ADDRSTRLEN];
Local<String> remote_address;
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sa = reinterpret_cast<struct sockaddr_in*>(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<struct sockaddr_in6*>(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;

Expand Down
5 changes: 1 addition & 4 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Acceptor*> (s->data);
Connection *connection = acceptor->OnConnection (addr, len);
if (connection)
return &connection->socket_;
else
return NULL;
return &connection->socket_;
}

oi_server server_;
Expand Down
1 change: 1 addition & 0 deletions test/test-pingpong.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 8 additions & 0 deletions website/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ <h3 id="tcp_connection"><code>node.tcp.Connection</code></h3>
assumed.
</dd>

<dt><code>connection.remoteAddress</code></dt>
<dd>
The string representation of the remote IP address. For example,
<code>"74.125.127.100"</code> or <code>"2001:4860:a005::68"</code>.

<p>This member is only present in server-side connections.</p>
</dd>

<dt><code>connection.readyState</code></dt>
<dd>
Either <code>"closed"</code>, <code>"open"</code>,
Expand Down

0 comments on commit 5558bc4

Please sign in to comment.