Browse Source

Fiddle with remoteAddress

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

50
src/net.cc

@ -41,15 +41,15 @@ using namespace node;
#define CLOSED_SYMBOL String::NewSymbol("closed") #define CLOSED_SYMBOL String::NewSymbol("closed")
static const struct addrinfo server_tcp_hints = static const struct addrinfo server_tcp_hints =
/* ai_flags */ { AI_PASSIVE /* ai_flags */ { AI_PASSIVE | AI_ADDRCONFIG
/* ai_family */ , AF_INET //AF_UNSPEC /* ai_family */ , AF_UNSPEC
/* ai_socktype */ , SOCK_STREAM /* ai_socktype */ , SOCK_STREAM
, 0 , 0
}; };
static const struct addrinfo client_tcp_hints = static const struct addrinfo client_tcp_hints =
/* ai_flags */ { 0 /* ai_flags */ { AI_ADDRCONFIG
/* ai_family */ , AF_INET //AF_UNSPEC /* ai_family */ , AF_UNSPEC
/* ai_socktype */ , SOCK_STREAM /* ai_socktype */ , SOCK_STREAM
, 0 , 0
}; };
@ -522,6 +522,28 @@ Acceptor::Acceptor (Handle<Object> handle, Handle<Function> connection_handler,
server_.data = this; 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* Connection*
Acceptor::OnConnection (struct sockaddr *addr, socklen_t len) Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
{ {
@ -544,23 +566,7 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
return NULL; return NULL;
} }
char ip4[INET_ADDRSTRLEN]; SetRemoteAddress(connection_handle, addr);
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); Connection *connection = NODE_UNWRAP(Connection, connection_handle);
if (!connection) return NULL; 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 // 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. // with a libeio call.
struct addrinfo *address = NULL; struct addrinfo *address = NULL;
int r = getaddrinfo(host, *port, &server_tcp_hints, &address); int r = getaddrinfo(host, *port, &server_tcp_hints, &address);

5
test/test-tcp-pingpong.js

@ -10,6 +10,8 @@ function Ponger (socket) {
socket.setEncoding("utf8"); socket.setEncoding("utf8");
socket.timeout = 0; socket.timeout = 0;
assertEquals("127.0.0.1", socket.remoteAddress);
socket.onReceive = function (data) { socket.onReceive = function (data) {
assertEquals("open", socket.readyState); assertEquals("open", socket.readyState);
assertTrue(count <= N); assertTrue(count <= N);
@ -24,14 +26,13 @@ function Ponger (socket) {
}; };
socket.onDisconnect = function (had_error) { socket.onDisconnect = function (had_error) {
assertEquals("127.0.0.1", socket.remoteAddress);
assertFalse(had_error); assertFalse(had_error);
assertEquals("closed", socket.readyState); assertEquals("closed", socket.readyState);
socket.server.close(); socket.server.close();
}; };
} }
function onLoad() { function onLoad () {
var server = new node.tcp.Server(Ponger); var server = new node.tcp.Server(Ponger);
server.listen(port); server.listen(port);

Loading…
Cancel
Save