diff --git a/src/net.cc b/src/net.cc index 0e88abf741..4f5cadf322 100644 --- a/src/net.cc +++ b/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 handle, Handle connection_handler, server_.data = this; } +static void +SetRemoteAddress (Local connection_handle, struct sockaddr *addr) +{ + HandleScope scope; + char ip4[INET_ADDRSTRLEN], 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* 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 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); + 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); diff --git a/test/test-tcp-pingpong.js b/test/test-tcp-pingpong.js index 3f35b4cd4f..aa7570d086 100644 --- a/test/test-tcp-pingpong.js +++ b/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,14 +26,13 @@ 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(); }; } -function onLoad() { +function onLoad () { var server = new node.tcp.Server(Ponger); server.listen(port);