Browse Source

Fiddle with remoteAddress

v0.7.4-release
Ryan 16 years ago
parent
commit
dcf5e72036
  1. 50
      src/net.cc
  2. 3
      test/test-tcp-pingpong.js

50
src/net.cc

@ -41,15 +41,15 @@ using namespace node;
#define CLOSED_SYMBOL String::NewSymbol("closed")
static const struct addrinfo server_tcp_hints =
/* ai_flags */ { AI_PASSIVE
/* ai_family */ , AF_INET //AF_UNSPEC
/* ai_flags */ { AI_PASSIVE | AI_ADDRCONFIG
/* ai_family */ , AF_UNSPEC
/* ai_socktype */ , SOCK_STREAM
, 0
};
static const struct addrinfo client_tcp_hints =
/* ai_flags */ { 0
/* ai_family */ , AF_INET //AF_UNSPEC
/* ai_flags */ { AI_ADDRCONFIG
/* ai_family */ , AF_UNSPEC
/* ai_socktype */ , SOCK_STREAM
, 0
};
@ -522,6 +522,28 @@ Acceptor::Acceptor (Handle<Object> handle, Handle<Function> connection_handler,
server_.data = this;
}
static void
SetRemoteAddress (Local<Object> connection_handle, struct sockaddr *addr)
{
HandleScope scope;
char ip4[INET_ADDRSTRLEN], 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*
Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
{
@ -544,23 +566,7 @@ 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);
SetRemoteAddress(connection_handle, addr);
Connection *connection = NODE_UNWRAP(Connection, connection_handle);
if (!connection) return NULL;
@ -619,7 +625,7 @@ Acceptor::Listen (const Arguments& args)
}
// For servers call getaddrinfo inline. This is blocking but it shouldn't
// matter--ever. If someone actually complains then simply swap it out
// matter much. If someone actually complains then simply swap it out
// with a libeio call.
struct addrinfo *address = NULL;
int r = getaddrinfo(host, *port, &server_tcp_hints, &address);

3
test/test-tcp-pingpong.js

@ -10,6 +10,8 @@ function Ponger (socket) {
socket.setEncoding("utf8");
socket.timeout = 0;
assertEquals("127.0.0.1", socket.remoteAddress);
socket.onReceive = function (data) {
assertEquals("open", socket.readyState);
assertTrue(count <= N);
@ -24,7 +26,6 @@ function Ponger (socket) {
};
socket.onDisconnect = function (had_error) {
assertEquals("127.0.0.1", socket.remoteAddress);
assertFalse(had_error);
assertEquals("closed", socket.readyState);
socket.server.close();

Loading…
Cancel
Save