2013-11-29 20:13:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-05-10 16:00:28 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-11-29 20:13:44 +04:00
|
|
|
#include "nsIServiceManager.h"
|
2013-05-10 16:00:28 +04:00
|
|
|
#include "UDPSocketParent.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIUDPSocket.h"
|
|
|
|
#include "nsINetAddr.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-05-06 14:32:25 +04:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2013-05-10 16:00:28 +04:00
|
|
|
#include "mozilla/net/DNS.h"
|
2014-05-06 14:32:25 +04:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
|
|
|
#include "mozilla/net/PNeckoParent.h"
|
2015-04-08 22:35:00 +03:00
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2015-05-29 17:14:14 +03:00
|
|
|
#include "mozilla/ipc/PBackgroundParent.h"
|
2016-01-22 10:47:01 +03:00
|
|
|
#include "mtransport/runnable_utils.h"
|
2013-05-10 16:00:28 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
NS_IMPL_ISUPPORTS(UDPSocketParent, nsIUDPSocketListener)
|
|
|
|
|
2015-05-29 17:14:14 +03:00
|
|
|
UDPSocketParent::UDPSocketParent(PBackgroundParent* aManager)
|
|
|
|
: mBackgroundManager(aManager)
|
|
|
|
, mIPCOpen(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocketParent::UDPSocketParent(PNeckoParent* aManager)
|
|
|
|
: mBackgroundManager(nullptr)
|
|
|
|
, mIPCOpen(true)
|
2014-08-23 03:16:31 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
UDPSocketParent::~UDPSocketParent()
|
2014-08-27 03:33:42 +04:00
|
|
|
{
|
2014-08-22 21:15:00 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
bool
|
2015-04-08 22:35:00 +03:00
|
|
|
UDPSocketParent::Init(const IPC::Principal& aPrincipal,
|
|
|
|
const nsACString& aFilter)
|
2014-08-27 03:33:42 +04:00
|
|
|
{
|
2015-05-29 17:14:14 +03:00
|
|
|
MOZ_ASSERT_IF(mBackgroundManager, !aPrincipal);
|
|
|
|
// will be used once we move all UDPSocket to PBackground, or
|
|
|
|
// if we add in Principal checking for mtransport
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mBackgroundManager;
|
2016-01-22 10:47:01 +03:00
|
|
|
|
2015-04-08 22:35:00 +03:00
|
|
|
mPrincipal = aPrincipal;
|
|
|
|
if (net::UsingNeckoIPCSecurity() &&
|
|
|
|
mPrincipal &&
|
|
|
|
!ContentParent::IgnoreIPCPrincipal()) {
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
services::GetPermissionManager();
|
|
|
|
if (!permMgr) {
|
|
|
|
NS_WARNING("No PermissionManager available!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestExactPermissionFromPrincipal(mPrincipal, "udp-socket",
|
|
|
|
&permission);
|
|
|
|
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
if (!aFilter.IsEmpty()) {
|
|
|
|
nsAutoCString contractId(NS_NETWORK_UDP_SOCKET_FILTER_HANDLER_PREFIX);
|
|
|
|
contractId.Append(aFilter);
|
2016-02-11 12:18:46 +03:00
|
|
|
nsCOMPtr<nsISocketFilterHandler> filterHandler =
|
2014-05-06 14:32:25 +04:00
|
|
|
do_GetService(contractId.get());
|
|
|
|
if (filterHandler) {
|
|
|
|
nsresult rv = filterHandler->NewFilter(getter_AddRefs(mFilter));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf_stderr("Cannot create filter that content specified. "
|
2015-01-07 06:20:46 +03:00
|
|
|
"filter name: %s, error code: %u.", aFilter.BeginReading(), static_cast<uint32_t>(rv));
|
2014-05-06 14:32:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf_stderr("Content doesn't have a valid filter. "
|
|
|
|
"filter name: %s.", aFilter.BeginReading());
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-27 03:33:42 +04:00
|
|
|
}
|
2015-04-08 22:35:00 +03:00
|
|
|
// We don't have browser actors in xpcshell, and hence can't run automated
|
|
|
|
// tests without this loophole.
|
2015-05-29 17:14:14 +03:00
|
|
|
if (net::UsingNeckoIPCSecurity() && !mFilter &&
|
2015-04-08 22:35:00 +03:00
|
|
|
(!mPrincipal || ContentParent::IgnoreIPCPrincipal())) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-06 14:32:25 +04:00
|
|
|
return true;
|
2014-08-27 03:33:42 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
// PUDPSocketParent methods
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-06 14:32:25 +04:00
|
|
|
UDPSocketParent::RecvBind(const UDPAddressInfo& aAddressInfo,
|
2016-03-02 08:46:50 +03:00
|
|
|
const bool& aAddressReuse, const bool& aLoopback,
|
2016-04-09 02:51:21 +03:00
|
|
|
const uint32_t& recvBufferSize,
|
|
|
|
const uint32_t& sendBufferSize)
|
2013-05-10 16:00:28 +04:00
|
|
|
{
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__, aAddressInfo.addr().get(), aAddressInfo.port()));
|
|
|
|
|
2016-04-09 02:51:21 +03:00
|
|
|
if (NS_FAILED(BindInternal(aAddressInfo.addr(), aAddressInfo.port(),
|
|
|
|
aAddressReuse, aLoopback, recvBufferSize,
|
|
|
|
sendBufferSize))) {
|
2014-05-06 14:32:25 +04:00
|
|
|
FireInternalError(__LINE__);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
nsCOMPtr<nsINetAddr> localAddr;
|
|
|
|
mSocket->GetLocalAddr(getter_AddRefs(localAddr));
|
|
|
|
|
|
|
|
nsCString addr;
|
|
|
|
if (NS_FAILED(localAddr->GetAddress(addr))) {
|
|
|
|
FireInternalError(__LINE__);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t port;
|
|
|
|
if (NS_FAILED(localAddr->GetPort(&port))) {
|
|
|
|
FireInternalError(__LINE__);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: SendCallbackOpened: %s:%u", __FUNCTION__, addr.get(), port));
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << SendCallbackOpened(UDPAddressInfo(addr, port));
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
UDPSocketParent::BindInternal(const nsCString& aHost, const uint16_t& aPort,
|
2016-03-02 08:46:50 +03:00
|
|
|
const bool& aAddressReuse, const bool& aLoopback,
|
2016-04-09 02:51:21 +03:00
|
|
|
const uint32_t& recvBufferSize,
|
|
|
|
const uint32_t& sendBufferSize)
|
2013-05-10 16:00:28 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
UDPSOCKET_LOG(("%s: [this=%p] %s:%u addressReuse: %d loopback: %d recvBufferSize: %"
|
|
|
|
PRIu32 ", sendBufferSize: %" PRIu32,
|
2016-04-09 02:51:21 +03:00
|
|
|
__FUNCTION__, this, nsCString(aHost).get(), aPort,
|
|
|
|
aAddressReuse, aLoopback, recvBufferSize, sendBufferSize));
|
2015-05-29 17:13:39 +03:00
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
nsCOMPtr<nsIUDPSocket> sock =
|
|
|
|
do_CreateInstance("@mozilla.org/network/udp-socket;1", &rv);
|
2014-05-06 14:32:25 +04:00
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aHost.IsEmpty()) {
|
2015-04-08 22:35:00 +03:00
|
|
|
rv = sock->Init(aPort, false, mPrincipal, aAddressReuse,
|
|
|
|
/* optional_argc = */ 1);
|
2013-05-10 16:00:28 +04:00
|
|
|
} else {
|
|
|
|
PRNetAddr prAddr;
|
|
|
|
PR_InitializeNetAddr(PR_IpAddrAny, aPort, &prAddr);
|
|
|
|
PRStatus status = PR_StringToNetAddr(aHost.BeginReading(), &prAddr);
|
|
|
|
if (status != PR_SUCCESS) {
|
2014-05-06 14:32:25 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::net::NetAddr addr;
|
|
|
|
PRNetAddrToNetAddr(&prAddr, &addr);
|
2015-04-08 22:35:00 +03:00
|
|
|
rv = sock->InitWithAddress(&addr, mPrincipal, aAddressReuse,
|
|
|
|
/* optional_argc = */ 1);
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
nsCOMPtr<nsINetAddr> laddr;
|
|
|
|
rv = sock->GetLocalAddr(getter_AddRefs(laddr));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
uint16_t family;
|
|
|
|
rv = laddr->GetFamily(&family);
|
2014-05-06 14:32:25 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-01-22 10:47:01 +03:00
|
|
|
if (family == nsINetAddr::FAMILY_INET) {
|
|
|
|
rv = sock->SetMulticastLoopback(aLoopback);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2016-04-09 02:51:21 +03:00
|
|
|
// TODO: once bug 1252759 is fixed query buffer first and only increase
|
2016-03-02 08:46:50 +03:00
|
|
|
if (recvBufferSize != 0) {
|
|
|
|
rv = sock->SetRecvBufferSize(recvBufferSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
UDPSOCKET_LOG(("%s: [this=%p] %s:%u failed to set recv buffer size to: %" PRIu32, __FUNCTION__, this, nsCString(aHost).get(), aPort, recvBufferSize));
|
2016-03-02 08:46:50 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 02:51:21 +03:00
|
|
|
if (sendBufferSize != 0) {
|
|
|
|
rv = sock->SetSendBufferSize(sendBufferSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
UDPSOCKET_LOG(("%s: [this=%p] %s:%u failed to set send buffer size to: %" PRIu32, __FUNCTION__, this, nsCString(aHost).get(), aPort, sendBufferSize));
|
2016-04-09 02:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 14:32:25 +04:00
|
|
|
|
|
|
|
// register listener
|
|
|
|
rv = sock->AsyncListen(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mSocket = sock;
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
|
|
|
|
static nsCOMPtr<nsIEventTarget> GetSTSThread()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> sts_thread;
|
|
|
|
|
|
|
|
sts_thread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
return sts_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckSTSThread()
|
|
|
|
{
|
2016-08-30 17:44:52 +03:00
|
|
|
DebugOnly<nsCOMPtr<nsIEventTarget>> sts_thread = GetSTSThread();
|
2016-01-22 10:47:01 +03:00
|
|
|
|
2016-08-30 17:44:52 +03:00
|
|
|
ASSERT_ON_THREAD(sts_thread.value);
|
2016-01-22 10:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Proxy the Connect() request to the STS thread, since it may block and
|
|
|
|
// should be done there.
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-01-22 10:47:01 +03:00
|
|
|
UDPSocketParent::RecvConnect(const UDPAddressInfo& aAddressInfo)
|
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target = GetCurrentThreadEventTarget();
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused <<
|
|
|
|
NS_WARN_IF(NS_FAILED(GetSTSThread()->Dispatch(WrapRunnable(
|
2017-02-23 16:55:35 +03:00
|
|
|
RefPtr<UDPSocketParent>(this),
|
2016-09-02 10:12:24 +03:00
|
|
|
&UDPSocketParent::DoConnect,
|
|
|
|
mSocket,
|
2017-06-01 23:42:05 +03:00
|
|
|
target,
|
2016-09-02 10:12:24 +03:00
|
|
|
aAddressInfo),
|
|
|
|
NS_DISPATCH_NORMAL)));
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-01-22 10:47:01 +03:00
|
|
|
}
|
|
|
|
|
2016-01-22 11:56:04 +03:00
|
|
|
void
|
|
|
|
UDPSocketParent::DoSendConnectResponse(const UDPAddressInfo& aAddressInfo)
|
|
|
|
{
|
|
|
|
// can't use directly with WrapRunnable due to warnings
|
|
|
|
mozilla::Unused << SendCallbackConnected(aAddressInfo);
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
void
|
|
|
|
UDPSocketParent::SendConnectResponse(nsIEventTarget *aThread,
|
|
|
|
const UDPAddressInfo& aAddressInfo)
|
|
|
|
{
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused <<
|
|
|
|
NS_WARN_IF(NS_FAILED(aThread->Dispatch(WrapRunnable(
|
2017-02-23 16:55:35 +03:00
|
|
|
RefPtr<UDPSocketParent>(this),
|
2016-09-02 10:12:24 +03:00
|
|
|
&UDPSocketParent::DoSendConnectResponse,
|
|
|
|
aAddressInfo),
|
|
|
|
NS_DISPATCH_NORMAL)));
|
2016-01-22 10:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs on STS thread
|
|
|
|
void
|
|
|
|
UDPSocketParent::DoConnect(nsCOMPtr<nsIUDPSocket>& aSocket,
|
|
|
|
nsCOMPtr<nsIEventTarget>& aReturnThread,
|
|
|
|
const UDPAddressInfo& aAddressInfo)
|
|
|
|
{
|
|
|
|
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__, aAddressInfo.addr().get(), aAddressInfo.port()));
|
|
|
|
if (NS_FAILED(ConnectInternal(aAddressInfo.addr(), aAddressInfo.port()))) {
|
|
|
|
SendInternalError(aReturnThread, __LINE__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CheckSTSThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsINetAddr> localAddr;
|
|
|
|
aSocket->GetLocalAddr(getter_AddRefs(localAddr));
|
|
|
|
|
|
|
|
nsCString addr;
|
|
|
|
if (NS_FAILED(localAddr->GetAddress(addr))) {
|
|
|
|
SendInternalError(aReturnThread, __LINE__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t port;
|
|
|
|
if (NS_FAILED(localAddr->GetPort(&port))) {
|
|
|
|
SendInternalError(aReturnThread, __LINE__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSOCKET_LOG(("%s: SendConnectResponse: %s:%u", __FUNCTION__, addr.get(), port));
|
|
|
|
SendConnectResponse(aReturnThread, UDPAddressInfo(addr, port));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
UDPSocketParent::ConnectInternal(const nsCString& aHost, const uint16_t& aPort)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__, nsCString(aHost).get(), aPort));
|
2017-02-11 03:39:51 +03:00
|
|
|
|
|
|
|
if (!mSocket) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
PRNetAddr prAddr;
|
2017-01-24 00:40:29 +03:00
|
|
|
memset(&prAddr, 0, sizeof(prAddr));
|
2016-01-22 10:47:01 +03:00
|
|
|
PR_InitializeNetAddr(PR_IpAddrAny, aPort, &prAddr);
|
|
|
|
PRStatus status = PR_StringToNetAddr(aHost.BeginReading(), &prAddr);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::net::NetAddr addr;
|
|
|
|
PRNetAddrToNetAddr(&prAddr, &addr);
|
|
|
|
|
|
|
|
rv = mSocket->Connect(&addr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-06 14:32:25 +04:00
|
|
|
UDPSocketParent::RecvOutgoingData(const UDPData& aData,
|
|
|
|
const UDPSocketAddr& aAddr)
|
|
|
|
{
|
2017-02-11 03:39:51 +03:00
|
|
|
if (!mSocket) {
|
|
|
|
NS_WARNING("sending socket is closed");
|
|
|
|
FireInternalError(__LINE__);
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (mFilter) {
|
|
|
|
if (aAddr.type() != UDPSocketAddr::TNetAddr) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO, Packet filter doesn't support input stream yet.
|
|
|
|
if (aData.type() != UDPData::TArrayOfuint8_t) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool allowed;
|
|
|
|
const InfallibleTArray<uint8_t>& data(aData.get_ArrayOfuint8_t());
|
|
|
|
rv = mFilter->FilterPacket(&aAddr.get_NetAddr(), data.Elements(),
|
2016-02-11 12:18:46 +03:00
|
|
|
data.Length(), nsISocketFilter::SF_OUTGOING,
|
2014-05-06 14:32:25 +04:00
|
|
|
&allowed);
|
|
|
|
|
|
|
|
// Sending unallowed data, kill content.
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !allowed) {
|
2016-11-19 03:25:34 +03:00
|
|
|
return IPC_FAIL(this, "Content tried to send non STUN packet");
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
switch(aData.type()) {
|
|
|
|
case UDPData::TArrayOfuint8_t:
|
|
|
|
Send(aData.get_ArrayOfuint8_t(), aAddr);
|
|
|
|
break;
|
2017-03-09 16:09:54 +03:00
|
|
|
case UDPData::TIPCStream:
|
|
|
|
Send(aData.get_IPCStream(), aAddr);
|
2014-05-06 14:32:25 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid data type!");
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
void
|
|
|
|
UDPSocketParent::Send(const InfallibleTArray<uint8_t>& aData,
|
|
|
|
const UDPSocketAddr& aAddr)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
uint32_t count;
|
|
|
|
switch(aAddr.type()) {
|
|
|
|
case UDPSocketAddr::TUDPAddressInfo: {
|
|
|
|
const UDPAddressInfo& addrInfo(aAddr.get_UDPAddressInfo());
|
|
|
|
rv = mSocket->Send(addrInfo.addr(), addrInfo.port(),
|
|
|
|
aData.Elements(), aData.Length(), &count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UDPSocketAddr::TNetAddr: {
|
|
|
|
const NetAddr& addr(aAddr.get_NetAddr());
|
|
|
|
rv = mSocket->SendWithAddress(&addr, aData.Elements(),
|
|
|
|
aData.Length(), &count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid address type!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || count == 0) {
|
|
|
|
FireInternalError(__LINE__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-09 16:09:54 +03:00
|
|
|
UDPSocketParent::Send(const IPCStream& aStream,
|
2014-05-06 14:32:25 +04:00
|
|
|
const UDPSocketAddr& aAddr)
|
|
|
|
{
|
2017-03-09 16:09:54 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aStream);
|
2014-05-06 14:32:25 +04:00
|
|
|
|
|
|
|
if (NS_WARN_IF(!stream)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
switch(aAddr.type()) {
|
|
|
|
case UDPSocketAddr::TUDPAddressInfo: {
|
|
|
|
const UDPAddressInfo& addrInfo(aAddr.get_UDPAddressInfo());
|
|
|
|
rv = mSocket->SendBinaryStream(addrInfo.addr(), addrInfo.port(), stream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UDPSocketAddr::TNetAddr: {
|
|
|
|
const NetAddr& addr(aAddr.get_NetAddr());
|
|
|
|
rv = mSocket->SendBinaryStreamWithAddress(&addr, stream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid address type!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FireInternalError(__LINE__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-06 14:32:25 +04:00
|
|
|
UDPSocketParent::RecvJoinMulticast(const nsCString& aMulticastAddress,
|
|
|
|
const nsCString& aInterface)
|
2013-05-10 16:00:28 +04:00
|
|
|
{
|
2014-05-06 14:32:25 +04:00
|
|
|
nsresult rv = mSocket->JoinMulticast(aMulticastAddress, aInterface);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
FireInternalError(__LINE__);
|
|
|
|
}
|
2013-11-29 20:13:44 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2014-05-06 14:32:25 +04:00
|
|
|
UDPSocketParent::RecvLeaveMulticast(const nsCString& aMulticastAddress,
|
|
|
|
const nsCString& aInterface)
|
2013-05-10 16:00:28 +04:00
|
|
|
{
|
2014-05-06 14:32:25 +04:00
|
|
|
nsresult rv = mSocket->LeaveMulticast(aMulticastAddress, aInterface);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
FireInternalError(__LINE__);
|
|
|
|
}
|
2013-11-29 20:13:44 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2013-05-10 16:00:28 +04:00
|
|
|
UDPSocketParent::RecvClose()
|
|
|
|
{
|
2014-05-06 14:32:25 +04:00
|
|
|
if (!mSocket) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
nsresult rv = mSocket->Close();
|
|
|
|
mSocket = nullptr;
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << NS_WARN_IF(NS_FAILED(rv));
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2013-05-10 16:00:28 +04:00
|
|
|
UDPSocketParent::RecvRequestDelete()
|
|
|
|
{
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << Send__delete__(this);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UDPSocketParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIPCOpen);
|
|
|
|
mIPCOpen = false;
|
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Close();
|
|
|
|
}
|
|
|
|
mSocket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIUDPSocketListener
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocketParent::OnPacketReceived(nsIUDPSocket* aSocket, nsIUDPMessage* aMessage)
|
|
|
|
{
|
|
|
|
// receiving packet from remote host, forward the message content to child process
|
|
|
|
if (!mIPCOpen) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t port;
|
|
|
|
nsCString ip;
|
|
|
|
nsCOMPtr<nsINetAddr> fromAddr;
|
|
|
|
aMessage->GetFromAddr(getter_AddRefs(fromAddr));
|
|
|
|
fromAddr->GetPort(&port);
|
|
|
|
fromAddr->GetAddress(ip);
|
|
|
|
|
|
|
|
nsCString data;
|
|
|
|
aMessage->GetData(data);
|
|
|
|
|
|
|
|
const char* buffer = data.get();
|
|
|
|
uint32_t len = data.Length();
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %s:%u, length %u", __FUNCTION__, ip.get(), port, len));
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
if (mFilter) {
|
|
|
|
bool allowed;
|
|
|
|
mozilla::net::NetAddr addr;
|
|
|
|
fromAddr->GetNetAddr(&addr);
|
|
|
|
nsresult rv = mFilter->FilterPacket(&addr,
|
|
|
|
(const uint8_t*)buffer, len,
|
2016-02-11 12:18:46 +03:00
|
|
|
nsISocketFilter::SF_INCOMING,
|
2014-05-06 14:32:25 +04:00
|
|
|
&allowed);
|
|
|
|
// Receiving unallowed data, drop.
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !allowed) {
|
2015-05-29 17:13:39 +03:00
|
|
|
if (!allowed) {
|
|
|
|
UDPSOCKET_LOG(("%s: not allowed", __FUNCTION__));
|
|
|
|
}
|
2014-05-06 14:32:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2013-11-29 20:13:44 +04:00
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
FallibleTArray<uint8_t> fallibleArray;
|
2015-06-10 03:27:35 +03:00
|
|
|
if (!fallibleArray.InsertElementsAt(0, buffer, len, fallible)) {
|
2014-05-06 14:32:25 +04:00
|
|
|
FireInternalError(__LINE__);
|
2013-05-10 16:00:28 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
InfallibleTArray<uint8_t> infallibleArray;
|
|
|
|
infallibleArray.SwapElements(fallibleArray);
|
|
|
|
|
|
|
|
// compose callback
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << SendCallbackReceivedData(UDPAddressInfo(ip, port), infallibleArray);
|
2013-05-10 16:00:28 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocketParent::OnStopListening(nsIUDPSocket* aSocket, nsresult aStatus)
|
|
|
|
{
|
|
|
|
// underlying socket is dead, send state update to child process
|
|
|
|
if (mIPCOpen) {
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << SendCallbackClosed();
|
2013-05-10 16:00:28 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
void
|
|
|
|
UDPSocketParent::FireInternalError(uint32_t aLineNo)
|
|
|
|
{
|
|
|
|
if (!mIPCOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << SendCallbackError(NS_LITERAL_CSTRING("Internal error"),
|
2014-05-06 14:32:25 +04:00
|
|
|
NS_LITERAL_CSTRING(__FILE__), aLineNo);
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
void
|
|
|
|
UDPSocketParent::SendInternalError(nsIEventTarget *aThread,
|
|
|
|
uint32_t aLineNo)
|
|
|
|
{
|
|
|
|
UDPSOCKET_LOG(("SendInternalError: %u", aLineNo));
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused <<
|
|
|
|
NS_WARN_IF(NS_FAILED(aThread->Dispatch(WrapRunnable(
|
2017-02-23 16:55:35 +03:00
|
|
|
RefPtr<UDPSocketParent>(this),
|
2016-09-02 10:12:24 +03:00
|
|
|
&UDPSocketParent::FireInternalError,
|
|
|
|
aLineNo),
|
|
|
|
NS_DISPATCH_NORMAL)));
|
2016-01-22 10:47:01 +03:00
|
|
|
}
|
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|