Browse Source

Fixes for ipv6

v0.7.4-release
Ryan 16 years ago
parent
commit
e30e4415ee
  1. 0
      http_simple.js
  2. 20
      src/net.cc
  3. 92
      test/test-tcp-pingpong.js
  4. 41
      test_client.js

0
test-http_simple.js → http_simple.js

20
src/net.cc

@ -42,14 +42,14 @@ 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_ADDRCONFIG /* ai_flags */ { AI_PASSIVE
/* ai_family */ , 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 */ { AI_ADDRCONFIG /* ai_flags */ { 0
/* ai_family */ , AF_UNSPEC /* ai_family */ , AF_UNSPEC
/* ai_socktype */ , SOCK_STREAM /* ai_socktype */ , SOCK_STREAM
, 0 , 0
@ -591,21 +591,21 @@ static void
SetRemoteAddress (Local<Object> connection_handle, struct sockaddr *addr) SetRemoteAddress (Local<Object> connection_handle, struct sockaddr *addr)
{ {
HandleScope scope; HandleScope scope;
char ip4[INET_ADDRSTRLEN], ip6[INET6_ADDRSTRLEN]; char ip[INET6_ADDRSTRLEN];
Local<String> remote_address; Local<String> remote_address;
if (addr->sa_family == AF_INET) { if (addr->sa_family == AF_INET) {
struct sockaddr_in *sa = reinterpret_cast<struct sockaddr_in*>(addr); struct sockaddr_in *sa = reinterpret_cast<struct sockaddr_in*>(addr);
inet_ntop(AF_INET, &(sa->sin_addr), ip4, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(sa->sin_addr), ip, INET6_ADDRSTRLEN);
remote_address = String::New(ip4); remote_address = String::New(ip);
} else if (addr->sa_family == AF_INET6) { } else if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sa6 = reinterpret_cast<struct sockaddr_in6*>(addr); struct sockaddr_in6 *sa6 = reinterpret_cast<struct sockaddr_in6*>(addr);
inet_ntop(AF_INET6, &(sa6->sin6_addr), ip6, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &(sa6->sin6_addr), ip, INET6_ADDRSTRLEN);
remote_address = String::New(ip6); remote_address = String::New(ip);
} else assert(0 && "received a bad sa_family");
} else {
assert(0 && "received a bad sa_family");
}
connection_handle->Set(REMOTE_ADDRESS_SYMBOL, remote_address); connection_handle->Set(REMOTE_ADDRESS_SYMBOL, remote_address);
} }

92
test/test-tcp-pingpong.js

@ -1,40 +1,44 @@
include("mjsunit.js"); include("mjsunit.js");
var port = 12123;
var N = 1000;
var count = 0;
var sent_final_ping = false; var tests_run = 0;
function Ponger (socket) { function pingPongTest (port, host, on_complete) {
socket.setEncoding("utf8"); var N = 1000;
socket.timeout = 0; var count = 0;
var sent_final_ping = false;
assertEquals("127.0.0.1", socket.remoteAddress);
var server = new node.tcp.Server(function (socket) {
socket.onReceive = function (data) { assertTrue(socket.remoteAddress !== null);
assertEquals("open", socket.readyState); assertTrue(socket.remoteAddress !== undefined);
assertTrue(count <= N); if (host === "127.0.0.1")
if (/PING/.exec(data)) { assertEquals(socket.remoteAddress, "127.0.0.1");
socket.send("PONG"); else if (host == null)
} assertEquals(socket.remoteAddress, "127.0.0.1");
};
socket.setEncoding("utf8");
socket.onEOF = function () { socket.timeout = 0;
assertEquals("writeOnly", socket.readyState);
socket.close(); socket.onReceive = function (data) {
}; assertEquals("open", socket.readyState);
assertTrue(count <= N);
socket.onDisconnect = function (had_error) { if (/PING/.exec(data)) {
assertFalse(had_error); socket.send("PONG");
assertEquals("closed", socket.readyState); }
socket.server.close(); };
};
} socket.onEOF = function () {
assertEquals("writeOnly", socket.readyState);
function onLoad () { socket.close();
var server = new node.tcp.Server(Ponger); };
server.listen(port);
socket.onDisconnect = function (had_error) {
assertFalse(had_error);
assertEquals("closed", socket.readyState);
socket.server.close();
};
});
server.listen(port, host);
var client = new node.tcp.Connection(); var client = new node.tcp.Connection();
assertEquals("closed", client.readyState); assertEquals("closed", client.readyState);
@ -65,15 +69,25 @@ function onLoad () {
client.close(); client.close();
} }
}; };
client.onEOF = function () { client.onDisconnect = function () {
assertEquals(N+1, count);
assertTrue(sent_final_ping);
if (on_complete) on_complete();
tests_run += 1;
}; };
assertEquals("closed", client.readyState); assertEquals("closed", client.readyState);
client.connect(port, "localhost"); client.connect(port, host);
}
function onLoad () {
/* All are run at once, so run on different ports */
pingPongTest(20989, "localhost");
pingPongTest(20988, null);
pingPongTest(20997, "::1");
} }
function onExit () { function onExit () {
assertEquals(N+1, count); assertEquals(3, tests_run);
assertTrue(sent_final_ping);
} }

41
test_client.js

@ -1,41 +0,0 @@
var c = new node.http.Client(8000, "127.0.0.1");
c.onError = function () {
puts("http client connection error.");
};
var req = c.get("/bytes/123", [["Accept", "*/*"]]);
req.finish(function (res) {
puts("response 1: " + res.statusCode.toString());
res.onBody = function (chunk) {
chunk = chunk.encodeUtf8();
puts("response 1 body <" + JSON.stringify(chunk) + ">");
return true;
};
res.onBodyComplete = function () {
puts("response 1 complete!");
return true;
};
});
setTimeout(function () {
var req2 = c.get("/something/else");
//node.debug("start req2");
req2.finish(function (res) {
puts("response 2: " + res.statusCode.toString());
res.onBody = function (chunk) {
chunk = chunk.encodeUtf8();
puts("response 2 body <" + JSON.stringify(chunk) + ">");
return true;
};
res.onBodyComplete = function () {
puts("response 2 complete!");
return true;
};
});
}, 2000);
Loading…
Cancel
Save