Bug 960397 - Part 2: Multicast option support for UDPSocket. r=mayhemer

This commit is contained in:
J. Ryan Stinnett
2014-04-29 21:35:00 +02:00
parent d41daa6f12
commit a14f9bcb63
5 changed files with 563 additions and 3 deletions

View File

@@ -10,11 +10,13 @@
#include "nsIOutputStream.h"
#include "nsIInputStream.h"
#include "nsINetAddr.h"
#include "nsITimer.h"
#include "mozilla/net/DNS.h"
#include "prerror.h"
#define REQUEST 0x68656c6f
#define RESPONSE 0x6f6c6568
#define MULTICAST_TIMEOUT 2000
#define EXPECT_SUCCESS(rv, ...) \
PR_BEGIN_MACRO \
@@ -44,6 +46,7 @@
enum TestPhase {
TEST_OUTPUT_STREAM,
TEST_SEND_API,
TEST_MULTICAST,
TEST_NONE
};
@@ -195,6 +198,8 @@ UDPServerListener::OnPacketReceived(nsIUDPSocket* socket, nsIUDPMessage* message
fail("Response written");
}
return NS_OK;
} else if (TEST_MULTICAST == phase && CheckMessageContent(message, REQUEST)) {
mResult = NS_OK;
} else if (TEST_SEND_API != phase || !CheckMessageContent(message, RESPONSE)) {
mResult = NS_ERROR_FAILURE;
}
@@ -211,6 +216,39 @@ UDPServerListener::OnStopListening(nsIUDPSocket*, nsresult)
return NS_OK;
}
/**
* Multicast timer callback: detects delivery failure
*/
class MulticastTimerCallback : public nsITimerCallback
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSITIMERCALLBACK
virtual ~MulticastTimerCallback();
nsresult mResult;
};
NS_IMPL_ISUPPORTS(MulticastTimerCallback, nsITimerCallback)
MulticastTimerCallback::~MulticastTimerCallback()
{
}
NS_IMETHODIMP
MulticastTimerCallback::Notify(nsITimer* timer)
{
if (TEST_MULTICAST != phase) {
return NS_OK;
}
// Multicast ping failed
printf("Multicast ping timeout expired\n");
mResult = NS_ERROR_FAILURE;
QuitPumpingEvents();
return NS_OK;
}
/**** Main ****/
int
main(int32_t argc, char *argv[])
@@ -230,8 +268,8 @@ main(int32_t argc, char *argv[])
// Create UDPServerListener to process UDP packets
nsRefPtr<UDPServerListener> serverListener = new UDPServerListener();
// Bind server socket to 127.0.0.1
rv = server->Init(0, true);
// Bind server socket to 0.0.0.0
rv = server->Init(0, false);
NS_ENSURE_SUCCESS(rv, -1);
int32_t serverPort;
server->GetPort(&serverPort);
@@ -239,7 +277,7 @@ main(int32_t argc, char *argv[])
// Bind clinet on arbitrary port
nsRefPtr<UDPClientListener> clientListener = new UDPClientListener();
client->Init(0, true);
client->Init(0, false);
client->AsyncListen(clientListener);
// Write data to server
@@ -262,6 +300,9 @@ main(int32_t argc, char *argv[])
mozilla::net::NetAddr clientAddr;
rv = client->GetAddress(&clientAddr);
NS_ENSURE_SUCCESS(rv, -1);
// The client address is 0.0.0.0, but Windows won't receive packets there, so
// use 127.0.0.1 explicitly
clientAddr.inet.ip = PR_htonl(127 << 24 | 1);
phase = TEST_SEND_API;
rv = server->SendWithAddress(&clientAddr, (uint8_t*)&data, sizeof(uint32_t), &count);
@@ -276,6 +317,143 @@ main(int32_t argc, char *argv[])
// Read response from server
NS_ENSURE_SUCCESS(clientListener->mResult, -1);
// Setup timer to detect multicast failure
nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
if (NS_WARN_IF(!timer)) {
return -1;
}
nsCOMPtr<MulticastTimerCallback> timerCb = new MulticastTimerCallback();
// The following multicast tests using multiple sockets require a firewall
// exception on Windows XP before they pass. For now, we'll skip them here.
// Later versions of Windows don't seem to have this issue.
#ifdef XP_WIN
OSVERSIONINFO OsVersion;
OsVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVersion);
if (OsVersion.dwMajorVersion == 5 && OsVersion.dwMinorVersion == 1) {
goto close;
}
#endif
// Join multicast group
printf("Joining multicast group\n");
phase = TEST_MULTICAST;
mozilla::net::NetAddr multicastAddr;
multicastAddr.inet.family = AF_INET;
multicastAddr.inet.ip = PR_htonl(224 << 24 | 255);
multicastAddr.inet.port = PR_htons(serverPort);
rv = server->JoinMulticastAddr(multicastAddr, nullptr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data, sizeof(uint32_t), &count);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
REQUIRE_EQUAL(count, sizeof(uint32_t), "Error");
passed("Multicast ping written by SendWithAddress");
// Wait for server to receive successfully
PumpEvents();
if (NS_WARN_IF(NS_FAILED(serverListener->mResult))) {
return -1;
}
if (NS_WARN_IF(NS_FAILED(timerCb->mResult))) {
return -1;
}
timer->Cancel();
passed("Server received ping successfully");
// Disable multicast loopback
printf("Disable multicast loopback\n");
client->SetMulticastLoopback(false);
server->SetMulticastLoopback(false);
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data, sizeof(uint32_t), &count);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
REQUIRE_EQUAL(count, sizeof(uint32_t), "Error");
passed("Multicast ping written by SendWithAddress");
// Wait for server to fail to receive
PumpEvents();
if (NS_WARN_IF(NS_SUCCEEDED(timerCb->mResult))) {
return -1;
}
timer->Cancel();
passed("Server failed to receive ping correctly");
// Reset state
client->SetMulticastLoopback(true);
server->SetMulticastLoopback(true);
// Change multicast interface
printf("Changing multicast interface\n");
mozilla::net::NetAddr loopbackAddr;
loopbackAddr.inet.family = AF_INET;
loopbackAddr.inet.ip = PR_htonl(INADDR_LOOPBACK);
client->SetMulticastInterfaceAddr(loopbackAddr);
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data, sizeof(uint32_t), &count);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
REQUIRE_EQUAL(count, sizeof(uint32_t), "Error");
passed("Multicast ping written by SendWithAddress");
// Wait for server to fail to receive
PumpEvents();
if (NS_WARN_IF(NS_SUCCEEDED(timerCb->mResult))) {
return -1;
}
timer->Cancel();
passed("Server failed to receive ping correctly");
// Reset state
mozilla::net::NetAddr anyAddr;
anyAddr.inet.family = AF_INET;
anyAddr.inet.ip = PR_htonl(INADDR_ANY);
client->SetMulticastInterfaceAddr(anyAddr);
// Leave multicast group
printf("Leave multicast group\n");
rv = server->LeaveMulticastAddr(multicastAddr, nullptr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data, sizeof(uint32_t), &count);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
REQUIRE_EQUAL(count, sizeof(uint32_t), "Error");
passed("Multicast ping written by SendWithAddress");
// Wait for server to fail to receive
PumpEvents();
if (NS_WARN_IF(NS_SUCCEEDED(timerCb->mResult))) {
return -1;
}
timer->Cancel();
passed("Server failed to receive ping correctly");
goto close;
close:
// Close server
printf("*** Attempting to close server ...\n");
server->Close();