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: */
|
2014-04-14 05:21:26 +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 "UDPSocket.h"
|
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-04-14 05:21:26 +04:00
|
|
|
#include "mozilla/dom/ErrorEvent.h"
|
2018-11-04 22:14:08 +03:00
|
|
|
#include "mozilla/dom/network/UDPSocketChild.h"
|
2014-04-14 05:21:26 +04:00
|
|
|
#include "mozilla/dom/UDPMessageEvent.h"
|
|
|
|
#include "mozilla/dom/UDPSocketBinding.h"
|
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
|
|
|
#include "mozilla/net/DNS.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsINetAddr.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-03-09 05:25:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS(UDPSocket::ListenerProxy, nsIUDPSocketListener,
|
|
|
|
nsIUDPSocketInternal)
|
|
|
|
|
2014-04-14 05:21:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(UDPSocket)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(UDPSocket,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOpened)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mClosed)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(UDPSocket, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOpened)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mClosed)
|
|
|
|
tmp->CloseWithReason(NS_OK);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(UDPSocket, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(UDPSocket, DOMEventTargetHelper)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UDPSocket)
|
2014-04-14 05:21:26 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIUDPSocketListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIUDPSocketInternal)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<UDPSocket> UDPSocket::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const UDPOptions& aOptions,
|
|
|
|
ErrorResult& aRv) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> ownerWindow =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
2014-04-14 05:21:26 +04:00
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool addressReuse = aOptions.mAddressReuse;
|
|
|
|
bool loopback = aOptions.mLoopback;
|
|
|
|
|
|
|
|
nsCString remoteAddress;
|
|
|
|
if (aOptions.mRemoteAddress.WasPassed()) {
|
|
|
|
remoteAddress = NS_ConvertUTF16toUTF8(aOptions.mRemoteAddress.Value());
|
|
|
|
} else {
|
|
|
|
remoteAddress.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<uint16_t> remotePort;
|
|
|
|
if (aOptions.mRemotePort.WasPassed()) {
|
|
|
|
remotePort.SetValue(aOptions.mRemotePort.Value());
|
|
|
|
|
|
|
|
if (remotePort.Value() == 0) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString localAddress;
|
|
|
|
if (aOptions.mLocalAddress.WasPassed()) {
|
|
|
|
localAddress = aOptions.mLocalAddress.Value();
|
|
|
|
|
|
|
|
// check if localAddress is a valid IPv4/6 address
|
|
|
|
NS_ConvertUTF16toUTF8 address(localAddress);
|
|
|
|
PRNetAddr prAddr;
|
|
|
|
PRStatus status = PR_StringToNetAddr(address.BeginReading(), &prAddr);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(localAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<uint16_t> localPort;
|
|
|
|
if (aOptions.mLocalPort.WasPassed()) {
|
|
|
|
localPort.SetValue(aOptions.mLocalPort.Value());
|
|
|
|
|
|
|
|
if (localPort.Value() == 0) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UDPSocket> socket =
|
|
|
|
new UDPSocket(ownerWindow, remoteAddress, remotePort);
|
2014-04-14 05:21:26 +04:00
|
|
|
aRv = socket->Init(localAddress, localPort, addressReuse, loopback);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return socket.forget();
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
UDPSocket::UDPSocket(nsPIDOMWindowInner* aOwner,
|
2014-04-14 05:21:26 +04:00
|
|
|
const nsCString& aRemoteAddress,
|
|
|
|
const Nullable<uint16_t>& aRemotePort)
|
|
|
|
: DOMEventTargetHelper(aOwner),
|
|
|
|
mRemoteAddress(aRemoteAddress),
|
|
|
|
mRemotePort(aRemotePort),
|
2016-01-12 21:16:59 +03:00
|
|
|
mAddressReuse(false),
|
|
|
|
mLoopback(false),
|
2014-04-14 05:21:26 +04:00
|
|
|
mReadyState(SocketReadyState::Opening) {
|
|
|
|
MOZ_ASSERT(aOwner);
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* aDoc = aOwner->GetExtantDoc();
|
2014-04-14 05:21:26 +04:00
|
|
|
if (aDoc) {
|
|
|
|
aDoc->DisallowBFCaching();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocket::~UDPSocket() { CloseWithReason(NS_OK); }
|
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* UDPSocket::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return UDPSocket_Binding::Wrap(aCx, this, aGivenProto);
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSocket::DisconnectFromOwner() {
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
|
|
|
CloseWithReason(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> UDPSocket::Close() {
|
|
|
|
MOZ_ASSERT(mClosed);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mClosed;
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
if (mReadyState == SocketReadyState::Closed) {
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseWithReason(NS_OK);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSocket::CloseWithReason(nsresult aReason) {
|
|
|
|
if (mReadyState == SocketReadyState::Closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOpened) {
|
|
|
|
if (mReadyState == SocketReadyState::Opening) {
|
|
|
|
// reject openedPromise with AbortError if socket is closed without error
|
|
|
|
nsresult openFailedReason =
|
|
|
|
NS_FAILED(aReason) ? aReason : NS_ERROR_DOM_ABORT_ERR;
|
|
|
|
mOpened->MaybeReject(openFailedReason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadyState = SocketReadyState::Closed;
|
|
|
|
|
2015-03-09 05:25:20 +03:00
|
|
|
if (mListenerProxy) {
|
|
|
|
mListenerProxy->Disconnect();
|
|
|
|
mListenerProxy = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:21:26 +04:00
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Close();
|
|
|
|
mSocket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSocketChild) {
|
|
|
|
mSocketChild->Close();
|
|
|
|
mSocketChild = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mClosed) {
|
|
|
|
if (NS_SUCCEEDED(aReason)) {
|
2016-08-09 12:15:13 +03:00
|
|
|
mClosed->MaybeResolveWithUndefined();
|
2014-04-14 05:21:26 +04:00
|
|
|
} else {
|
|
|
|
mClosed->MaybeReject(aReason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mPendingMcastCommands.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSocket::JoinMulticastGroup(const nsAString& aMulticastGroupAddress,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (mReadyState == SocketReadyState::Closed) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReadyState == SocketReadyState::Opening) {
|
|
|
|
MulticastCommand joinCommand(MulticastCommand::Join,
|
|
|
|
aMulticastGroupAddress);
|
|
|
|
mPendingMcastCommands.AppendElement(joinCommand);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mSocket || mSocketChild);
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 address(aMulticastGroupAddress);
|
|
|
|
|
|
|
|
if (mSocket) {
|
|
|
|
MOZ_ASSERT(!mSocketChild);
|
|
|
|
|
|
|
|
aRv = mSocket->JoinMulticast(address, EmptyCString());
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(!aRv.Failed(), "JoinMulticast failed");
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mSocketChild);
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
mSocketChild->JoinMulticast(address, EmptyCString());
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSocket::LeaveMulticastGroup(const nsAString& aMulticastGroupAddress,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (mReadyState == SocketReadyState::Closed) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReadyState == SocketReadyState::Opening) {
|
|
|
|
MulticastCommand leaveCommand(MulticastCommand::Leave,
|
|
|
|
aMulticastGroupAddress);
|
|
|
|
mPendingMcastCommands.AppendElement(leaveCommand);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mSocket || mSocketChild);
|
|
|
|
|
|
|
|
nsCString address = NS_ConvertUTF16toUTF8(aMulticastGroupAddress);
|
|
|
|
if (mSocket) {
|
|
|
|
MOZ_ASSERT(!mSocketChild);
|
|
|
|
|
|
|
|
aRv = mSocket->LeaveMulticast(address, EmptyCString());
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(!aRv.Failed(), "mSocket->LeaveMulticast failed");
|
2014-04-14 05:21:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mSocketChild);
|
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
mSocketChild->LeaveMulticast(address, EmptyCString());
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UDPSocket::DoPendingMcastCommand() {
|
|
|
|
MOZ_ASSERT(mReadyState == SocketReadyState::Open,
|
|
|
|
"Multicast command can only be executed after socket opened");
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mPendingMcastCommands.Length(); ++i) {
|
|
|
|
MulticastCommand& command = mPendingMcastCommands[i];
|
|
|
|
ErrorResult rv;
|
|
|
|
|
|
|
|
switch (command.mCommand) {
|
|
|
|
case MulticastCommand::Join: {
|
|
|
|
JoinMulticastGroup(command.mAddress, rv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MulticastCommand::Leave: {
|
|
|
|
LeaveMulticastGroup(command.mAddress, rv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mPendingMcastCommands.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UDPSocket::Send(const StringOrBlobOrArrayBufferOrArrayBufferView& aData,
|
|
|
|
const Optional<nsAString>& aRemoteAddress,
|
|
|
|
const Optional<Nullable<uint16_t>>& aRemotePort,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (mReadyState != SocketReadyState::Open) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mSocket || mSocketChild);
|
|
|
|
|
|
|
|
// If the remote address and port were not specified in the constructor or as
|
|
|
|
// arguments, throw InvalidAccessError.
|
|
|
|
nsCString remoteAddress;
|
|
|
|
if (aRemoteAddress.WasPassed()) {
|
|
|
|
remoteAddress = NS_ConvertUTF16toUTF8(aRemoteAddress.Value());
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: Send to %s", __FUNCTION__, remoteAddress.get()));
|
2014-04-14 05:21:26 +04:00
|
|
|
} else if (!mRemoteAddress.IsVoid()) {
|
|
|
|
remoteAddress = mRemoteAddress;
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: Send to %s", __FUNCTION__, remoteAddress.get()));
|
2014-04-14 05:21:26 +04:00
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t remotePort;
|
|
|
|
if (aRemotePort.WasPassed() && !aRemotePort.Value().IsNull()) {
|
|
|
|
remotePort = aRemotePort.Value().Value();
|
|
|
|
} else if (!mRemotePort.IsNull()) {
|
|
|
|
remotePort = mRemotePort.Value();
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
if (aData.IsBlob()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
Blob& blob = aData.GetAsBlob();
|
2014-04-14 05:21:26 +04:00
|
|
|
|
2017-10-02 14:53:12 +03:00
|
|
|
blob.CreateInputStream(getter_AddRefs(stream), aRv);
|
2014-04-14 05:21:26 +04:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStringInputStream> strStream =
|
|
|
|
do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData.IsString()) {
|
|
|
|
NS_ConvertUTF16toUTF8 data(aData.GetAsString());
|
|
|
|
aRv = strStream->SetData(data.BeginReading(), data.Length());
|
|
|
|
} else if (aData.IsArrayBuffer()) {
|
|
|
|
const ArrayBuffer& data = aData.GetAsArrayBuffer();
|
2020-01-23 06:22:06 +03:00
|
|
|
data.ComputeState();
|
2014-04-14 05:21:26 +04:00
|
|
|
aRv = strStream->SetData(reinterpret_cast<const char*>(data.Data()),
|
|
|
|
data.Length());
|
|
|
|
} else {
|
|
|
|
const ArrayBufferView& data = aData.GetAsArrayBufferView();
|
2020-01-23 06:22:06 +03:00
|
|
|
data.ComputeState();
|
2014-04-14 05:21:26 +04:00
|
|
|
aRv = strStream->SetData(reinterpret_cast<const char*>(data.Data()),
|
|
|
|
data.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-14 03:50:11 +03:00
|
|
|
stream = strStream;
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mSocket) {
|
|
|
|
aRv = mSocket->SendBinaryStream(remoteAddress, remotePort, stream);
|
|
|
|
} else if (mSocketChild) {
|
|
|
|
aRv = mSocketChild->SendBinaryStream(remoteAddress, remotePort, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UDPSocket::InitLocal(const nsAString& aLocalAddress,
|
|
|
|
const uint16_t& aLocalPort) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUDPSocket> sock =
|
|
|
|
do_CreateInstance("@mozilla.org/network/udp-socket;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:35:00 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner(), &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull();
|
|
|
|
if (!principal) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:21:26 +04:00
|
|
|
if (aLocalAddress.IsEmpty()) {
|
2015-04-08 22:35:00 +03:00
|
|
|
rv = sock->Init(aLocalPort, /* loopback = */ false, principal,
|
|
|
|
mAddressReuse, /* optionalArgc = */ 1);
|
2014-04-14 05:21:26 +04:00
|
|
|
} else {
|
|
|
|
PRNetAddr prAddr;
|
|
|
|
PR_InitializeNetAddr(PR_IpAddrAny, aLocalPort, &prAddr);
|
|
|
|
PR_StringToNetAddr(NS_ConvertUTF16toUTF8(aLocalAddress).BeginReading(),
|
|
|
|
&prAddr);
|
2015-05-29 17:13:39 +03:00
|
|
|
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__,
|
|
|
|
NS_ConvertUTF16toUTF8(aLocalAddress).get(), aLocalPort));
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
mozilla::net::NetAddr addr;
|
|
|
|
PRNetAddrToNetAddr(&prAddr, &addr);
|
2015-04-08 22:35:00 +03:00
|
|
|
rv = sock->InitWithAddress(&addr, principal, mAddressReuse,
|
|
|
|
/* optionalArgc = */ 1);
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = sock->SetMulticastLoopback(mLoopback);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSocket = sock;
|
|
|
|
|
|
|
|
// Get real local address and local port
|
|
|
|
nsCOMPtr<nsINetAddr> localAddr;
|
|
|
|
rv = mSocket->GetLocalAddr(getter_AddRefs(localAddr));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString localAddress;
|
|
|
|
rv = localAddr->GetAddress(localAddress);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mLocalAddress = NS_ConvertUTF8toUTF16(localAddress);
|
|
|
|
|
|
|
|
uint16_t localPort;
|
|
|
|
rv = localAddr->GetPort(&localPort);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mLocalPort.SetValue(localPort);
|
|
|
|
|
2015-03-09 05:25:20 +03:00
|
|
|
mListenerProxy = new ListenerProxy(this);
|
|
|
|
|
|
|
|
rv = mSocket->AsyncListen(mListenerProxy);
|
2014-04-14 05:21:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadyState = SocketReadyState::Open;
|
|
|
|
rv = DoPendingMcastCommand();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-08-09 12:15:13 +03:00
|
|
|
mOpened->MaybeResolveWithUndefined();
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UDPSocket::InitRemote(const nsAString& aLocalAddress,
|
|
|
|
const uint16_t& aLocalPort) {
|
|
|
|
nsresult rv;
|
|
|
|
|
2019-05-09 09:55:05 +03:00
|
|
|
RefPtr<UDPSocketChild> sock = new UDPSocketChild();
|
2014-04-14 05:21:26 +04:00
|
|
|
|
2015-03-09 05:25:20 +03:00
|
|
|
mListenerProxy = new ListenerProxy(this);
|
|
|
|
|
2015-04-08 22:35:00 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> obj = do_QueryInterface(GetOwner(), &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = obj->PrincipalOrNull();
|
|
|
|
if (!principal) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-06-12 01:24:00 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target;
|
|
|
|
if (nsCOMPtr<nsIGlobalObject> global = GetOwnerGlobal()) {
|
|
|
|
target = global->EventTargetFor(TaskCategory::Other);
|
|
|
|
}
|
|
|
|
|
2015-03-09 05:25:20 +03:00
|
|
|
rv = sock->Bind(mListenerProxy, principal,
|
|
|
|
NS_ConvertUTF16toUTF8(aLocalAddress), aLocalPort,
|
|
|
|
mAddressReuse, mLoopback, 0, 0, target);
|
|
|
|
|
2014-04-14 05:21:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSocketChild = sock;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UDPSocket::Init(const nsString& aLocalAddress,
|
|
|
|
const Nullable<uint16_t>& aLocalPort,
|
|
|
|
const bool& aAddressReuse, const bool& aLoopback) {
|
|
|
|
MOZ_ASSERT(!mSocket && !mSocketChild);
|
|
|
|
|
|
|
|
mLocalAddress = aLocalAddress;
|
|
|
|
mLocalPort = aLocalPort;
|
|
|
|
mAddressReuse = aAddressReuse;
|
|
|
|
mLoopback = aLoopback;
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
|
|
|
|
mOpened = Promise::Create(global, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mClosed = Promise::Create(global, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class OpenSocketRunnable final : public Runnable {
|
2014-04-14 05:21:26 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit OpenSocketRunnable(UDPSocket* aSocket)
|
|
|
|
: mozilla::Runnable("OpenSocketRunnable"), mSocket(aSocket) {}
|
2014-04-14 05:21:26 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-04-14 05:21:26 +04:00
|
|
|
MOZ_ASSERT(mSocket);
|
|
|
|
|
|
|
|
if (mSocket->mReadyState != SocketReadyState::Opening) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t localPort = 0;
|
|
|
|
if (!mSocket->mLocalPort.IsNull()) {
|
|
|
|
localPort = mSocket->mLocalPort.Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2015-07-04 04:29:00 +03:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2014-04-14 05:21:26 +04:00
|
|
|
rv = mSocket->InitRemote(mSocket->mLocalAddress, localPort);
|
|
|
|
} else {
|
|
|
|
rv = mSocket->InitLocal(mSocket->mLocalAddress, localPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mSocket->CloseWithReason(NS_ERROR_DOM_NETWORK_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UDPSocket> mSocket;
|
2014-04-14 05:21:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new OpenSocketRunnable(this);
|
|
|
|
|
|
|
|
return NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSocket::HandleReceivedData(const nsACString& aRemoteAddress,
|
|
|
|
const uint16_t& aRemotePort,
|
2019-05-09 09:55:07 +03:00
|
|
|
const nsTArray<uint8_t>& aData) {
|
2014-04-14 05:21:26 +04:00
|
|
|
if (mReadyState != SocketReadyState::Open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:46:46 +03:00
|
|
|
if (NS_FAILED(CheckCurrentGlobalCorrectness())) {
|
2014-04-14 05:21:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:07 +03:00
|
|
|
if (NS_FAILED(DispatchReceivedData(aRemoteAddress, aRemotePort, aData))) {
|
2019-09-24 19:53:44 +03:00
|
|
|
CloseWithReason(NS_ERROR_UNEXPECTED);
|
2014-04-14 05:21:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult UDPSocket::DispatchReceivedData(const nsACString& aRemoteAddress,
|
|
|
|
const uint16_t& aRemotePort,
|
2019-05-09 09:55:07 +03:00
|
|
|
const nsTArray<uint8_t>& aData) {
|
2014-04-14 05:21:26 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
|
|
|
// Copy packet data to ArrayBuffer
|
2019-05-09 09:55:07 +03:00
|
|
|
JS::Rooted<JSObject*> arrayBuf(
|
|
|
|
cx, ArrayBuffer::Create(cx, aData.Length(), aData.Elements()));
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
if (NS_WARN_IF(!arrayBuf)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> jsData(cx, JS::ObjectValue(*arrayBuf));
|
|
|
|
|
|
|
|
// Create DOM event
|
|
|
|
RootedDictionary<UDPMessageEventInit> init(cx);
|
|
|
|
init.mRemoteAddress = NS_ConvertUTF8toUTF16(aRemoteAddress);
|
|
|
|
init.mRemotePort = aRemotePort;
|
|
|
|
init.mData = jsData;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UDPMessageEvent> udpEvent =
|
2014-04-14 05:21:26 +04:00
|
|
|
UDPMessageEvent::Constructor(this, NS_LITERAL_STRING("message"), init);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!udpEvent)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
udpEvent->SetTrusted(true);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, udpEvent);
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
return asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIUDPSocketListener
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::OnPacketReceived(nsIUDPSocket* aSocket, nsIUDPMessage* aMessage) {
|
|
|
|
// nsIUDPSocketListener callbacks should be invoked on main thread.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
|
|
|
|
// Create appropriate JS object for message
|
|
|
|
FallibleTArray<uint8_t>& buffer = aMessage->GetDataAsTArray();
|
|
|
|
|
|
|
|
nsCOMPtr<nsINetAddr> addr;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aMessage->GetFromAddr(getter_AddRefs(addr))))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString remoteAddress;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(addr->GetAddress(remoteAddress)))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t remotePort;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(addr->GetPort(&remotePort)))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:07 +03:00
|
|
|
HandleReceivedData(remoteAddress, remotePort, buffer);
|
2014-04-14 05:21:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::OnStopListening(nsIUDPSocket* aSocket, nsresult aStatus) {
|
|
|
|
// nsIUDPSocketListener callbacks should be invoked on main thread.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
|
|
|
|
CloseWithReason(aStatus);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIUDPSocketInternal
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::CallListenerError(const nsACString& aMessage,
|
|
|
|
const nsACString& aFilename,
|
|
|
|
uint32_t aLineNumber) {
|
|
|
|
CloseWithReason(NS_ERROR_DOM_NETWORK_ERR);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::CallListenerReceivedData(const nsACString& aRemoteAddress,
|
2019-05-09 09:55:07 +03:00
|
|
|
uint16_t aRemotePort,
|
|
|
|
const nsTArray<uint8_t>& aData) {
|
|
|
|
HandleReceivedData(aRemoteAddress, aRemotePort, aData);
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::CallListenerOpened() {
|
|
|
|
if (mReadyState != SocketReadyState::Opening) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mSocketChild);
|
|
|
|
|
|
|
|
// Get real local address and local port
|
2019-05-09 09:55:06 +03:00
|
|
|
mLocalAddress = NS_ConvertUTF8toUTF16(mSocketChild->LocalAddress());
|
2014-04-14 05:21:26 +04:00
|
|
|
|
2019-05-09 09:55:06 +03:00
|
|
|
mLocalPort.SetValue(mSocketChild->LocalPort());
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
mReadyState = SocketReadyState::Open;
|
|
|
|
nsresult rv = DoPendingMcastCommand();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
CloseWithReason(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-09 12:15:13 +03:00
|
|
|
mOpened->MaybeResolveWithUndefined();
|
2014-04-14 05:21:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::CallListenerConnected() {
|
|
|
|
// This shouldn't be called here.
|
|
|
|
MOZ_CRASH();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:21:26 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
UDPSocket::CallListenerClosed() {
|
|
|
|
CloseWithReason(NS_OK);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|