2015-05-03 22:32:37 +03: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/. */
|
|
|
|
|
|
|
|
#include "UDPSocketChild.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-03-09 16:09:54 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2013-05-10 16:00:28 +04:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
2017-03-09 16:09:54 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2015-04-08 22:35:00 +03:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
2015-05-29 17:14:14 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2013-05-10 16:00:28 +04:00
|
|
|
|
|
|
|
using mozilla::net::gNeckoChild;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
NS_IMPL_ISUPPORTS(UDPSocketChildBase, nsISupports)
|
2013-05-10 16:00:28 +04:00
|
|
|
|
|
|
|
UDPSocketChildBase::UDPSocketChildBase() : mIPCOpen(false) {}
|
|
|
|
|
|
|
|
UDPSocketChildBase::~UDPSocketChildBase() {}
|
2014-03-08 10:57:15 +04:00
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
void UDPSocketChildBase::ReleaseIPDLReference() {
|
|
|
|
MOZ_ASSERT(mIPCOpen);
|
|
|
|
mIPCOpen = false;
|
2014-10-31 00:12:09 +03:00
|
|
|
mSocket = nullptr;
|
2013-05-10 16:00:28 +04:00
|
|
|
this->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSocketChildBase::AddIPDLReference() {
|
|
|
|
MOZ_ASSERT(!mIPCOpen);
|
|
|
|
mIPCOpen = true;
|
|
|
|
this->AddRef();
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) UDPSocketChild::Release(void) {
|
2013-05-10 16:00:28 +04:00
|
|
|
nsrefcnt refcnt = UDPSocketChildBase::Release();
|
|
|
|
if (refcnt == 1 && mIPCOpen) {
|
|
|
|
PUDPSocketChild::SendRequestDelete();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return refcnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocketChild::UDPSocketChild() : mBackgroundManager(nullptr), mLocalPort(0) {}
|
|
|
|
|
|
|
|
UDPSocketChild::~UDPSocketChild() {}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
nsresult UDPSocketChild::SetBackgroundSpinsEvents() {
|
2015-05-29 17:14:14 +03:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
|
2017-10-24 13:02:39 +03:00
|
|
|
mBackgroundManager = BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!mBackgroundManager)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-29 17:14:14 +03:00
|
|
|
}
|
2017-10-24 13:02:39 +03:00
|
|
|
|
2015-05-29 17:14:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
nsresult UDPSocketChild::Bind(nsIUDPSocketInternal* aSocket,
|
|
|
|
nsIPrincipal* aPrincipal, const nsACString& aHost,
|
|
|
|
uint16_t aPort, bool aAddressReuse,
|
|
|
|
bool aLoopback, uint32_t recvBufferSize,
|
|
|
|
uint32_t sendBufferSize,
|
|
|
|
nsIEventTarget* aMainThreadEventTarget) {
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(
|
|
|
|
("%s: %s:%u", __FUNCTION__, PromiseFlatCString(aHost).get(), aPort));
|
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
NS_ENSURE_ARG(aSocket);
|
|
|
|
|
2019-05-02 20:29:46 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (aMainThreadEventTarget) {
|
|
|
|
gNeckoChild->SetEventTargetForActor(this, aMainThreadEventTarget);
|
|
|
|
}
|
|
|
|
if (!gNeckoChild->SendPUDPSocketConstructor(
|
|
|
|
this, IPC::Principal(aPrincipal), mFilterName)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!mBackgroundManager) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2015-05-29 17:14:14 +03:00
|
|
|
// If we want to support a passed-in principal here we'd need to
|
|
|
|
// convert it to a PrincipalInfo
|
|
|
|
MOZ_ASSERT(!aPrincipal);
|
2019-05-02 20:29:46 +03:00
|
|
|
if (!mBackgroundManager->SendPUDPSocketConstructor(this, Nothing(),
|
|
|
|
mFilterName)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2017-06-12 01:24:00 +03:00
|
|
|
}
|
2015-05-29 17:14:14 +03:00
|
|
|
}
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2019-05-02 20:29:46 +03:00
|
|
|
mSocket = aSocket;
|
|
|
|
AddIPDLReference();
|
|
|
|
|
2016-04-09 02:51:21 +03:00
|
|
|
SendBind(UDPAddressInfo(nsCString(aHost), aPort), aAddressReuse, aLoopback,
|
|
|
|
recvBufferSize, sendBufferSize);
|
2013-05-10 16:00:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
void UDPSocketChild::Connect(nsIUDPSocketInternal* aSocket,
|
|
|
|
const nsACString& aHost, uint16_t aPort) {
|
2016-01-22 10:47:01 +03:00
|
|
|
UDPSOCKET_LOG(
|
|
|
|
("%s: %s:%u", __FUNCTION__, PromiseFlatCString(aHost).get(), aPort));
|
|
|
|
|
|
|
|
mSocket = aSocket;
|
|
|
|
|
|
|
|
SendConnect(UDPAddressInfo(nsCString(aHost), aPort));
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
void UDPSocketChild::Close() { SendClose(); }
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
nsresult UDPSocketChild::SendWithAddress(const NetAddr* aAddr,
|
|
|
|
const uint8_t* aData,
|
|
|
|
uint32_t aByteLength) {
|
2013-05-10 16:00:28 +04:00
|
|
|
NS_ENSURE_ARG(aAddr);
|
|
|
|
NS_ENSURE_ARG(aData);
|
|
|
|
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %u bytes", __FUNCTION__, aByteLength));
|
2014-05-06 14:32:25 +04:00
|
|
|
return SendDataInternal(UDPSocketAddr(*aAddr), aData, aByteLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UDPSocketChild::SendDataInternal(const UDPSocketAddr& aAddr,
|
|
|
|
const uint8_t* aData,
|
|
|
|
const uint32_t aByteLength) {
|
|
|
|
NS_ENSURE_ARG(aData);
|
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
FallibleTArray<uint8_t> fallibleArray;
|
2015-06-10 03:27:35 +03:00
|
|
|
if (!fallibleArray.InsertElementsAt(0, aData, aByteLength, fallible)) {
|
2013-05-10 16:00:28 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2019-07-10 06:27:30 +03:00
|
|
|
nsTArray<uint8_t> array;
|
2013-05-10 16:00:28 +04:00
|
|
|
array.SwapElements(fallibleArray);
|
2014-05-06 14:32:25 +04:00
|
|
|
|
|
|
|
SendOutgoingData(array, aAddr);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
nsresult UDPSocketChild::SendBinaryStream(const nsACString& aHost,
|
|
|
|
uint16_t aPort,
|
|
|
|
nsIInputStream* aStream) {
|
2014-05-06 14:32:25 +04:00
|
|
|
NS_ENSURE_ARG(aStream);
|
|
|
|
|
2017-03-09 16:09:54 +03:00
|
|
|
mozilla::ipc::AutoIPCStream autoStream;
|
|
|
|
autoStream.Serialize(aStream, static_cast<mozilla::dom::ContentChild*>(
|
|
|
|
gNeckoChild->Manager()));
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(
|
|
|
|
("%s: %s:%u", __FUNCTION__, PromiseFlatCString(aHost).get(), aPort));
|
2019-02-22 21:46:14 +03:00
|
|
|
SendOutgoingData(UDPData(autoStream.TakeValue()),
|
2017-03-09 16:09:54 +03:00
|
|
|
UDPSocketAddr(UDPAddressInfo(nsCString(aHost), aPort)));
|
2013-05-10 16:00:28 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
void UDPSocketChild::JoinMulticast(const nsACString& aMulticastAddress,
|
|
|
|
const nsACString& aInterface) {
|
2014-05-06 14:32:25 +04:00
|
|
|
SendJoinMulticast(nsCString(aMulticastAddress), nsCString(aInterface));
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
void UDPSocketChild::LeaveMulticast(const nsACString& aMulticastAddress,
|
|
|
|
const nsACString& aInterface) {
|
2014-05-06 14:32:25 +04:00
|
|
|
SendLeaveMulticast(nsCString(aMulticastAddress), nsCString(aInterface));
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
nsresult UDPSocketChild::SetFilterName(const nsACString& aFilterName) {
|
2013-11-29 20:13:44 +04:00
|
|
|
if (!mFilterName.IsEmpty()) {
|
|
|
|
// filter name can only be set once.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mFilterName = aFilterName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-10 16:00:28 +04:00
|
|
|
// PUDPSocketChild Methods
|
2014-05-06 14:32:25 +04:00
|
|
|
mozilla::ipc::IPCResult UDPSocketChild::RecvCallbackOpened(
|
|
|
|
const UDPAddressInfo& aAddressInfo) {
|
|
|
|
mLocalAddress = aAddressInfo.addr();
|
|
|
|
mLocalPort = aAddressInfo.port();
|
2013-05-10 16:00:28 +04:00
|
|
|
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__, mLocalAddress.get(), mLocalPort));
|
2014-05-06 14:32:25 +04:00
|
|
|
nsresult rv = mSocket->CallListenerOpened();
|
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();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
// PUDPSocketChild Methods
|
|
|
|
mozilla::ipc::IPCResult UDPSocketChild::RecvCallbackConnected(
|
|
|
|
const UDPAddressInfo& aAddressInfo) {
|
|
|
|
mLocalAddress = aAddressInfo.addr();
|
|
|
|
mLocalPort = aAddressInfo.port();
|
|
|
|
|
|
|
|
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__, mLocalAddress.get(), mLocalPort));
|
|
|
|
nsresult rv = mSocket->CallListenerConnected();
|
|
|
|
mozilla::Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-01-22 10:47:01 +03:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
mozilla::ipc::IPCResult UDPSocketChild::RecvCallbackClosed() {
|
|
|
|
nsresult rv = mSocket->CallListenerClosed();
|
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();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult UDPSocketChild::RecvCallbackReceivedData(
|
2019-07-10 06:27:30 +03:00
|
|
|
const UDPAddressInfo& aAddressInfo, nsTArray<uint8_t>&& aData) {
|
2017-07-26 23:03:57 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %s:%u length %zu", __FUNCTION__,
|
2015-05-29 17:13:39 +03:00
|
|
|
aAddressInfo.addr().get(), aAddressInfo.port(),
|
|
|
|
aData.Length()));
|
2019-05-09 09:55:07 +03:00
|
|
|
nsresult rv = mSocket->CallListenerReceivedData(aAddressInfo.addr(),
|
|
|
|
aAddressInfo.port(), aData);
|
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();
|
2014-05-06 14:32:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult UDPSocketChild::RecvCallbackError(
|
|
|
|
const nsCString& aMessage, const nsCString& aFilename,
|
|
|
|
const uint32_t& aLineNumber) {
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %s:%s:%u", __FUNCTION__, aMessage.get(), aFilename.get(),
|
|
|
|
aLineNumber));
|
2014-05-06 14:32:25 +04:00
|
|
|
nsresult rv = mSocket->CallListenerError(aMessage, aFilename, aLineNumber);
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << NS_WARN_IF(NS_FAILED(rv));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|