mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
Add connection.remoteAddress for server-side node.tcp.Connections.
This commit is contained in:
22
src/net.cc
22
src/net.cc
@@ -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;
|
||||
@@ -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<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;
|
||||
|
||||
|
||||
@@ -160,10 +160,7 @@ private:
|
||||
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_;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -428,6 +428,14 @@ server.listen(7000, "localhost");</pre>
|
||||
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>,
|
||||
|
||||
Reference in New Issue
Block a user