2012-10-03 23:51:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#if !defined(__Userspace_os_Windows)
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2013-07-31 17:09:00 +00:00
|
|
|
// usrsctp.h expects to have errno definitions prior to its inclusion.
|
|
|
|
#include <errno.h>
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
#define SCTP_DEBUG 1
|
2013-07-30 14:25:31 +00:00
|
|
|
#define SCTP_STDINT_INCLUDE <stdint.h>
|
2013-07-02 19:56:04 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Disable "warning C4200: nonstandard extension used : zero-sized array in
|
|
|
|
// struct/union"
|
|
|
|
// ...which the third-party file usrsctp.h runs afoul of.
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4200)
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "usrsctp.h"
|
|
|
|
|
2013-07-02 19:56:04 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "DataChannelLog.h"
|
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "mozilla/Services.h"
|
2014-03-15 19:00:17 +00:00
|
|
|
#include "nsProxyRelease.h"
|
2014-02-01 13:09:00 +00:00
|
|
|
#include "nsThread.h"
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsNetCID.h"
|
2013-08-16 19:13:08 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2015-01-11 05:28:34 +00:00
|
|
|
#include "mozilla/unused.h"
|
2012-10-03 23:51:23 +00:00
|
|
|
#ifdef MOZ_PEERCONNECTION
|
|
|
|
#include "mtransport/runnable_utils.h"
|
|
|
|
#endif
|
2013-01-07 03:01:23 +00:00
|
|
|
|
|
|
|
#define DATACHANNEL_LOG(args) LOG(args)
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "DataChannel.h"
|
|
|
|
#include "DataChannelProtocol.h"
|
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
PRLogModuleInfo*
|
|
|
|
GetDataChannelLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("DataChannel");
|
|
|
|
return sLog;
|
|
|
|
}
|
2013-01-29 07:46:26 +00:00
|
|
|
|
|
|
|
PRLogModuleInfo*
|
|
|
|
GetSCTPLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("SCTP");
|
|
|
|
return sLog;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-03-27 23:46:50 +00:00
|
|
|
// Let us turn on and off important assertions in non-debug builds
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define ASSERT_WEBRTC(x) MOZ_ASSERT((x))
|
|
|
|
#elif defined(MOZ_WEBRTC_ASSERT_ALWAYS)
|
2013-03-28 00:23:37 +00:00
|
|
|
#define ASSERT_WEBRTC(x) do { if (!(x)) { MOZ_CRASH(); } } while (0)
|
2013-03-27 23:46:50 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
static bool sctp_initialized;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class DataChannelShutdown;
|
2013-08-16 19:13:08 +00:00
|
|
|
StaticRefPtr<DataChannelShutdown> gDataChannelShutdown;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
class DataChannelShutdown : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// This needs to be tied to some form object that is guaranteed to be
|
|
|
|
// around (singleton likely) unless we want to shutdown sctp whenever
|
|
|
|
// we're not using it (and in which case we'd keep a refcnt'd object
|
|
|
|
// ref'd by each DataChannelConnection to release the SCTP usrlib via
|
|
|
|
// sctp_finish)
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
DataChannelShutdown() {}
|
|
|
|
|
2012-12-14 04:30:11 +00:00
|
|
|
void Init()
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsresult rv = observerService->AddObserver(this,
|
2015-10-20 08:47:00 +00:00
|
|
|
"xpcom-will-shutdown",
|
2012-10-03 23:51:23 +00:00
|
|
|
false);
|
|
|
|
MOZ_ASSERT(rv == NS_OK);
|
|
|
|
(void) rv;
|
|
|
|
}
|
|
|
|
|
2014-06-24 16:36:44 +00:00
|
|
|
private:
|
2012-10-03 23:51:23 +00:00
|
|
|
virtual ~DataChannelShutdown()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService)
|
2015-10-20 08:47:00 +00:00
|
|
|
observerService->RemoveObserver(this, "xpcom-will-shutdown");
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 16:36:44 +00:00
|
|
|
public:
|
2012-10-03 23:51:23 +00:00
|
|
|
NS_IMETHODIMP Observe(nsISupports* aSubject, const char* aTopic,
|
2015-03-21 16:28:04 +00:00
|
|
|
const char16_t* aData) override {
|
2015-10-20 08:47:00 +00:00
|
|
|
if (strcmp(aTopic, "xpcom-will-shutdown") == 0) {
|
2012-10-03 23:51:23 +00:00
|
|
|
LOG(("Shutting down SCTP"));
|
|
|
|
if (sctp_initialized) {
|
|
|
|
usrsctp_finish();
|
|
|
|
sctp_initialized = false;
|
|
|
|
}
|
2012-12-14 04:30:11 +00:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv = observerService->RemoveObserver(this,
|
2015-10-20 08:47:00 +00:00
|
|
|
"xpcom-will-shutdown");
|
2012-12-14 04:30:11 +00:00
|
|
|
MOZ_ASSERT(rv == NS_OK);
|
|
|
|
(void) rv;
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannelShutdown> kungFuDeathGrip(this);
|
2012-12-14 04:30:11 +00:00
|
|
|
gDataChannelShutdown = nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(DataChannelShutdown, nsIObserver);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
BufferedMsg::BufferedMsg(struct sctp_sendv_spa &spa, const char *data,
|
2015-09-28 23:02:23 +00:00
|
|
|
size_t length) : mLength(length)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
mSpa = new sctp_sendv_spa;
|
|
|
|
*mSpa = spa;
|
|
|
|
char *tmp = new char[length]; // infallible malloc!
|
|
|
|
memcpy(tmp, data, length);
|
|
|
|
mData = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferedMsg::~BufferedMsg()
|
|
|
|
{
|
|
|
|
delete mSpa;
|
|
|
|
delete mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
receive_cb(struct socket* sock, union sctp_sockstore addr,
|
|
|
|
void *data, size_t datalen,
|
|
|
|
struct sctp_rcvinfo rcv, int flags, void *ulp_info)
|
|
|
|
{
|
|
|
|
DataChannelConnection *connection = static_cast<DataChannelConnection*>(ulp_info);
|
|
|
|
return connection->ReceiveCallback(sock, data, datalen, rcv, flags);
|
|
|
|
}
|
|
|
|
|
2013-01-29 07:46:26 +00:00
|
|
|
static void
|
|
|
|
debug_printf(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buffer[1024];
|
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
if (MOZ_LOG_TEST(GetSCTPLog(), LogLevel::Debug)) {
|
2013-01-29 07:46:26 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, ap) > 0) {
|
|
|
|
#else
|
|
|
|
if (vsnprintf(buffer, sizeof(buffer), format, ap) > 0) {
|
|
|
|
#endif
|
|
|
|
PR_LogPrint("%s", buffer);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
DataChannelConnection::DataChannelConnection(DataConnectionListener *listener) :
|
2013-02-12 21:32:43 +00:00
|
|
|
mLock("netwerk::sctp::DataChannelConnection")
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
mState = CLOSED;
|
|
|
|
mSocket = nullptr;
|
|
|
|
mMasterSocket = nullptr;
|
2014-07-30 19:52:05 +00:00
|
|
|
mListener = listener;
|
2012-10-03 23:51:23 +00:00
|
|
|
mLocalPort = 0;
|
|
|
|
mRemotePort = 0;
|
|
|
|
mDeferTimeout = 10;
|
|
|
|
mTimerRunning = false;
|
2013-04-01 01:09:26 +00:00
|
|
|
LOG(("Constructor DataChannelConnection=%p, listener=%p", this, mListener.get()));
|
2014-01-23 07:41:29 +00:00
|
|
|
mInternalIOThread = nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataChannelConnection::~DataChannelConnection()
|
|
|
|
{
|
2013-01-07 03:01:23 +00:00
|
|
|
LOG(("Deleting DataChannelConnection %p", (void *) this));
|
2012-12-14 04:30:11 +00:00
|
|
|
// This may die on the MainThread, or on the STS thread
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(mState == CLOSED);
|
2012-12-14 04:30:11 +00:00
|
|
|
MOZ_ASSERT(!mMasterSocket);
|
2013-01-07 03:01:23 +00:00
|
|
|
MOZ_ASSERT(mPending.GetSize() == 0);
|
2013-01-12 10:40:22 +00:00
|
|
|
|
2013-01-07 03:01:23 +00:00
|
|
|
// Already disconnected from sigslot/mTransportFlow
|
2013-01-12 10:40:22 +00:00
|
|
|
// TransportFlows must be released from the STS thread
|
2014-02-01 13:09:00 +00:00
|
|
|
if (!IsSTSThread()) {
|
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
|
|
|
if (mTransportFlow) {
|
|
|
|
ASSERT_WEBRTC(mSTS);
|
2014-03-15 19:00:17 +00:00
|
|
|
NS_ProxyRelease(mSTS, mTransportFlow);
|
2014-02-01 13:09:00 +00:00
|
|
|
}
|
2014-01-23 07:41:29 +00:00
|
|
|
|
2014-02-01 13:09:00 +00:00
|
|
|
if (mInternalIOThread) {
|
|
|
|
// Avoid spinning the event thread from here (which if we're mainthread
|
|
|
|
// is in the event loop already)
|
|
|
|
NS_DispatchToMainThread(WrapRunnable(nsCOMPtr<nsIThread>(mInternalIOThread),
|
|
|
|
&nsIThread::Shutdown),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// on STS, safe to call shutdown
|
|
|
|
if (mInternalIOThread) {
|
|
|
|
mInternalIOThread->Shutdown();
|
|
|
|
}
|
2014-01-23 07:41:29 +00:00
|
|
|
}
|
2012-12-14 04:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::Destroy()
|
|
|
|
{
|
2012-10-03 23:51:23 +00:00
|
|
|
// Though it's probably ok to do this and close the sockets;
|
|
|
|
// if we really want it to do true clean shutdowns it can
|
|
|
|
// create a dependant Internal object that would remain around
|
|
|
|
// until the network shut down the association or timed out.
|
2012-12-14 04:30:11 +00:00
|
|
|
LOG(("Destroying DataChannelConnection %p", (void *) this));
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
CloseAll();
|
2012-12-14 04:30:11 +00:00
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-06-02 13:48:17 +00:00
|
|
|
// If we had a pending reset, we aren't waiting for it - clear the list so
|
|
|
|
// we can deregister this DataChannelConnection without leaking.
|
|
|
|
ClearResets();
|
|
|
|
|
2014-03-15 19:15:17 +00:00
|
|
|
MOZ_ASSERT(mSTS);
|
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
|
|
|
// Finish Destroy on STS thread to avoid bug 876167 - once that's fixed,
|
|
|
|
// the usrsctp_close() calls can move back here (and just proxy the
|
|
|
|
// disconnect_all())
|
2015-10-18 05:24:48 +00:00
|
|
|
RUN_ON_THREAD(mSTS, WrapRunnable(RefPtr<DataChannelConnection>(this),
|
2014-03-15 19:15:17 +00:00
|
|
|
&DataChannelConnection::DestroyOnSTS,
|
|
|
|
mSocket, mMasterSocket),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
// These will be released on STS
|
2012-12-14 04:30:11 +00:00
|
|
|
mSocket = nullptr;
|
2013-04-01 01:09:26 +00:00
|
|
|
mMasterSocket = nullptr; // also a flag that we've Destroyed this connection
|
2012-12-14 04:30:11 +00:00
|
|
|
|
2014-03-15 19:15:17 +00:00
|
|
|
// Must do this in Destroy() since we may then delete this object
|
2013-04-01 01:09:24 +00:00
|
|
|
if (mUsingDtls) {
|
|
|
|
usrsctp_deregister_address(static_cast<void *>(this));
|
|
|
|
LOG(("Deregistered %p from the SCTP stack.", static_cast<void *>(this)));
|
|
|
|
}
|
2014-03-15 19:15:17 +00:00
|
|
|
|
2012-12-14 04:30:11 +00:00
|
|
|
// We can't get any more new callbacks from the SCTP library
|
|
|
|
// All existing callbacks have refs to DataChannelConnection
|
2013-01-07 03:01:23 +00:00
|
|
|
|
|
|
|
// nsDOMDataChannel objects have refs to DataChannels that have refs to us
|
2014-03-15 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataChannelConnection::DestroyOnSTS(struct socket *aMasterSocket,
|
|
|
|
struct socket *aSocket)
|
|
|
|
{
|
|
|
|
if (aSocket && aSocket != aMasterSocket)
|
|
|
|
usrsctp_close(aSocket);
|
|
|
|
if (aMasterSocket)
|
|
|
|
usrsctp_close(aMasterSocket);
|
|
|
|
|
|
|
|
disconnect_all();
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(DataChannelConnection,
|
|
|
|
nsITimerCallback)
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
DataChannelConnection::Init(unsigned short aPort, uint16_t aNumStreams, bool aUsingDtls)
|
|
|
|
{
|
|
|
|
struct sctp_initmsg initmsg;
|
|
|
|
struct sctp_udpencaps encaps;
|
|
|
|
struct sctp_assoc_value av;
|
|
|
|
struct sctp_event event;
|
|
|
|
socklen_t len;
|
|
|
|
|
|
|
|
uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
|
|
|
|
SCTP_PEER_ADDR_CHANGE,
|
|
|
|
SCTP_REMOTE_ERROR,
|
|
|
|
SCTP_SHUTDOWN_EVENT,
|
|
|
|
SCTP_ADAPTATION_INDICATION,
|
|
|
|
SCTP_SEND_FAILED_EVENT,
|
|
|
|
SCTP_STREAM_RESET_EVENT,
|
|
|
|
SCTP_STREAM_CHANGE_EVENT};
|
|
|
|
{
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// MutexAutoLock lock(mLock); Not needed since we're on mainthread always
|
|
|
|
if (!sctp_initialized) {
|
|
|
|
if (aUsingDtls) {
|
|
|
|
LOG(("sctp_init(DTLS)"));
|
|
|
|
#ifdef MOZ_PEERCONNECTION
|
2013-01-29 07:46:26 +00:00
|
|
|
usrsctp_init(0,
|
|
|
|
DataChannelConnection::SctpDtlsOutput,
|
|
|
|
debug_printf
|
|
|
|
);
|
2012-10-03 23:51:23 +00:00
|
|
|
#else
|
|
|
|
NS_ASSERTION(!aUsingDtls, "Trying to use SCTP/DTLS without mtransport");
|
|
|
|
#endif
|
|
|
|
} else {
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("sctp_init(%u)", aPort));
|
2013-01-29 07:46:26 +00:00
|
|
|
usrsctp_init(aPort,
|
|
|
|
nullptr,
|
|
|
|
debug_printf
|
|
|
|
);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
// Set logging to SCTP:LogLevel::Debug to get SCTP debugs
|
|
|
|
if (MOZ_LOG_TEST(GetSCTPLog(), LogLevel::Debug)) {
|
2013-01-29 07:46:26 +00:00
|
|
|
usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
|
|
|
|
}
|
2015-05-07 19:52:44 +00:00
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
usrsctp_sysctl_set_sctp_blackhole(2);
|
2013-04-01 01:09:24 +00:00
|
|
|
// ECN is currently not supported by the Firefox code
|
|
|
|
usrsctp_sysctl_set_sctp_ecn_enable(0);
|
2012-10-03 23:51:23 +00:00
|
|
|
sctp_initialized = true;
|
|
|
|
|
|
|
|
gDataChannelShutdown = new DataChannelShutdown();
|
|
|
|
gDataChannelShutdown->Init();
|
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:30:11 +00:00
|
|
|
|
2012-10-29 03:48:36 +00:00
|
|
|
// XXX FIX! make this a global we get once
|
|
|
|
// Find the STS thread
|
2012-12-14 04:30:11 +00:00
|
|
|
nsresult rv;
|
|
|
|
mSTS = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2012-10-29 03:48:36 +00:00
|
|
|
|
2012-12-14 04:30:11 +00:00
|
|
|
// Open sctp with a callback
|
2012-10-03 23:51:23 +00:00
|
|
|
if ((mMasterSocket = usrsctp_socket(
|
|
|
|
aUsingDtls ? AF_CONN : AF_INET,
|
|
|
|
SOCK_STREAM, IPPROTO_SCTP, receive_cb, nullptr, 0, this)) == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-05 16:17:07 +00:00
|
|
|
// Make non-blocking for bind/connect. SCTP over UDP defaults to non-blocking
|
|
|
|
// in associations for normal IO
|
|
|
|
if (usrsctp_set_non_blocking(mMasterSocket, 1) < 0) {
|
|
|
|
LOG(("Couldn't set non_blocking on SCTP socket"));
|
|
|
|
// We can't handle connect() safely if it will block, not that this will
|
|
|
|
// even happen.
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2012-11-01 00:31:39 +00:00
|
|
|
// Make sure when we close the socket, make sure it doesn't call us back again!
|
|
|
|
// This would cause it try to use an invalid DataChannelConnection pointer
|
|
|
|
struct linger l;
|
|
|
|
l.l_onoff = 1;
|
|
|
|
l.l_linger = 0;
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, SOL_SOCKET, SO_LINGER,
|
|
|
|
(const void *)&l, (socklen_t)sizeof(struct linger)) < 0) {
|
|
|
|
LOG(("Couldn't set SO_LINGER on SCTP socket"));
|
2013-03-05 16:17:07 +00:00
|
|
|
// unsafe to allow it to continue if this fails
|
|
|
|
goto error_cleanup;
|
2012-11-01 00:31:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 04:30:11 +00:00
|
|
|
// XXX Consider disabling this when we add proper SDP negotiation.
|
|
|
|
// We may want to leave enabled for supporting 'cloning' of SDP offers, which
|
|
|
|
// implies re-use of the same pseudo-port number, or forcing a renegotiation.
|
2013-03-05 16:17:07 +00:00
|
|
|
{
|
|
|
|
uint32_t on = 1;
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_REUSE_PORT,
|
|
|
|
(const void *)&on, (socklen_t)sizeof(on)) < 0) {
|
|
|
|
LOG(("Couldn't set SCTP_REUSE_PORT on SCTP socket"));
|
|
|
|
}
|
2014-03-14 09:10:11 +00:00
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_NODELAY,
|
|
|
|
(const void *)&on, (socklen_t)sizeof(on)) < 0) {
|
|
|
|
LOG(("Couldn't set SCTP_NODELAY on SCTP socket"));
|
|
|
|
}
|
2012-12-14 04:30:11 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
if (!aUsingDtls) {
|
|
|
|
memset(&encaps, 0, sizeof(encaps));
|
|
|
|
encaps.sue_address.ss_family = AF_INET;
|
|
|
|
encaps.sue_port = htons(aPort);
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT,
|
|
|
|
(const void*)&encaps,
|
|
|
|
(socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
|
|
|
|
LOG(("*** failed encaps errno %d", errno));
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
LOG(("SCTP encapsulation local port %d", aPort));
|
|
|
|
}
|
|
|
|
|
|
|
|
av.assoc_id = SCTP_ALL_ASSOC;
|
|
|
|
av.assoc_value = SCTP_ENABLE_RESET_STREAM_REQ | SCTP_ENABLE_CHANGE_ASSOC_REQ;
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, &av,
|
|
|
|
(socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
|
|
|
|
LOG(("*** failed enable stream reset errno %d", errno));
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable the events of interest. */
|
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
event.se_assoc_id = SCTP_ALL_ASSOC;
|
|
|
|
event.se_on = 1;
|
|
|
|
for (uint32_t i = 0; i < sizeof(event_types)/sizeof(event_types[0]); ++i) {
|
|
|
|
event.se_type = event_types[i];
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)) < 0) {
|
|
|
|
LOG(("*** failed setsockopt SCTP_EVENT errno %d", errno));
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update number of streams
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams.AppendElements(aNumStreams);
|
2012-10-03 23:51:23 +00:00
|
|
|
for (uint32_t i = 0; i < aNumStreams; ++i) {
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams[i] = nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
memset(&initmsg, 0, sizeof(initmsg));
|
|
|
|
len = sizeof(initmsg);
|
|
|
|
if (usrsctp_getsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, &len) < 0) {
|
|
|
|
LOG(("*** failed getsockopt SCTP_INITMSG"));
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
LOG(("Setting number of SCTP streams to %u, was %u/%u", aNumStreams,
|
|
|
|
initmsg.sinit_num_ostreams, initmsg.sinit_max_instreams));
|
|
|
|
initmsg.sinit_num_ostreams = aNumStreams;
|
|
|
|
initmsg.sinit_max_instreams = MAX_NUM_STREAMS;
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
|
|
|
|
(socklen_t)sizeof(initmsg)) < 0) {
|
|
|
|
LOG(("*** failed setsockopt SCTP_INITMSG, errno %d", errno));
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSocket = nullptr;
|
2013-04-01 01:09:24 +00:00
|
|
|
if (aUsingDtls) {
|
|
|
|
mUsingDtls = true;
|
|
|
|
usrsctp_register_address(static_cast<void *>(this));
|
|
|
|
LOG(("Registered %p within the SCTP stack.", static_cast<void *>(this)));
|
|
|
|
} else {
|
|
|
|
mUsingDtls = false;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error_cleanup:
|
|
|
|
usrsctp_close(mMasterSocket);
|
|
|
|
mMasterSocket = nullptr;
|
2013-04-01 01:09:24 +00:00
|
|
|
mUsingDtls = false;
|
2012-10-03 23:51:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::StartDefer()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!NS_IsMainThread()) {
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
|
|
|
DataChannelOnMessageAvailable::START_DEFER,
|
|
|
|
this, (DataChannel *) nullptr)));
|
2012-10-03 23:51:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
if (!mDeferredTimer) {
|
|
|
|
mDeferredTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
|
|
|
MOZ_ASSERT(mDeferredTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTimerRunning) {
|
|
|
|
rv = mDeferredTimer->InitWithCallback(this, mDeferTimeout,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2012-12-24 04:45:57 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(rv == NS_OK);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
mTimerRunning = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsITimerCallback
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DataChannelConnection::Notify(nsITimer *timer)
|
|
|
|
{
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
LOG(("%s: %p [%p] (%dms), sending deferred messages", __FUNCTION__, this, timer, mDeferTimeout));
|
|
|
|
|
|
|
|
if (timer == mDeferredTimer) {
|
|
|
|
if (SendDeferredMessages()) {
|
|
|
|
// Still blocked
|
|
|
|
// we don't need a lock, since this must be main thread...
|
|
|
|
nsresult rv = mDeferredTimer->InitWithCallback(this, mDeferTimeout,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("%s: cannot initialize open timer", __FUNCTION__));
|
|
|
|
// XXX and do....?
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mTimerRunning = true;
|
|
|
|
} else {
|
|
|
|
LOG(("Turned off deferred send timer"));
|
|
|
|
mTimerRunning = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_PEERCONNECTION
|
2013-04-01 01:09:26 +00:00
|
|
|
void
|
|
|
|
DataChannelConnection::SetEvenOdd()
|
|
|
|
{
|
|
|
|
ASSERT_WEBRTC(IsSTSThread());
|
|
|
|
|
|
|
|
TransportLayerDtls *dtls = static_cast<TransportLayerDtls *>(
|
|
|
|
mTransportFlow->GetLayer(TransportLayerDtls::ID()));
|
|
|
|
MOZ_ASSERT(dtls); // DTLS is mandatory
|
|
|
|
mAllocateEven = (dtls->role() == TransportLayerDtls::CLIENT);
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
bool
|
2013-04-01 01:09:26 +00:00
|
|
|
DataChannelConnection::ConnectViaTransportFlow(TransportFlow *aFlow, uint16_t localport, uint16_t remoteport)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2013-04-01 01:09:26 +00:00
|
|
|
LOG(("Connect DTLS local %u, remote %u", localport, remoteport));
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
NS_PRECONDITION(mMasterSocket, "SCTP wasn't initialized before ConnectViaTransportFlow!");
|
2012-10-03 23:51:23 +00:00
|
|
|
NS_ENSURE_TRUE(aFlow, false);
|
|
|
|
|
|
|
|
mTransportFlow = aFlow;
|
|
|
|
mLocalPort = localport;
|
|
|
|
mRemotePort = remoteport;
|
2013-03-05 16:17:07 +00:00
|
|
|
mState = CONNECTING;
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RUN_ON_THREAD(mSTS, WrapRunnable(RefPtr<DataChannelConnection>(this),
|
2013-04-01 01:09:26 +00:00
|
|
|
&DataChannelConnection::SetSignals),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::SetSignals()
|
|
|
|
{
|
|
|
|
ASSERT_WEBRTC(IsSTSThread());
|
|
|
|
ASSERT_WEBRTC(mTransportFlow);
|
|
|
|
LOG(("Setting transport signals, state: %d", mTransportFlow->state()));
|
|
|
|
mTransportFlow->SignalPacketReceived.connect(this, &DataChannelConnection::SctpDtlsInput);
|
|
|
|
// SignalStateChange() doesn't call you with the initial state
|
|
|
|
mTransportFlow->SignalStateChange.connect(this, &DataChannelConnection::CompleteConnect);
|
|
|
|
CompleteConnect(mTransportFlow, mTransportFlow->state());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::CompleteConnect(TransportFlow *flow, TransportLayer::State state)
|
|
|
|
{
|
|
|
|
LOG(("Data transport state: %d", state));
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
ASSERT_WEBRTC(IsSTSThread());
|
|
|
|
// We should abort connection on TS_ERROR.
|
|
|
|
// Note however that the association will also fail (perhaps with a delay) and
|
|
|
|
// notify us in that way
|
|
|
|
if (state != TransportLayer::TS_OPEN || !mMasterSocket)
|
|
|
|
return;
|
|
|
|
|
2013-03-05 16:17:07 +00:00
|
|
|
struct sockaddr_conn addr;
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sconn_family = AF_CONN;
|
|
|
|
#if defined(__Userspace_os_Darwin)
|
|
|
|
addr.sconn_len = sizeof(addr);
|
|
|
|
#endif
|
|
|
|
addr.sconn_port = htons(mLocalPort);
|
2013-04-01 01:09:24 +00:00
|
|
|
addr.sconn_addr = static_cast<void *>(this);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-03-05 16:17:07 +00:00
|
|
|
LOG(("Calling usrsctp_bind"));
|
|
|
|
int r = usrsctp_bind(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr),
|
|
|
|
sizeof(addr));
|
|
|
|
if (r < 0) {
|
|
|
|
LOG(("usrsctp_bind failed: %d", r));
|
|
|
|
} else {
|
|
|
|
// This is the remote addr
|
|
|
|
addr.sconn_port = htons(mRemotePort);
|
|
|
|
LOG(("Calling usrsctp_connect"));
|
|
|
|
r = usrsctp_connect(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr),
|
|
|
|
sizeof(addr));
|
2015-08-21 18:19:26 +00:00
|
|
|
if (r >= 0 || errno == EINPROGRESS) {
|
|
|
|
struct sctp_paddrparams paddrparams;
|
|
|
|
socklen_t opt_len;
|
|
|
|
|
|
|
|
memset(&paddrparams, 0, sizeof(struct sctp_paddrparams));
|
|
|
|
memcpy(&paddrparams.spp_address, &addr, sizeof(struct sockaddr_conn));
|
|
|
|
opt_len = (socklen_t)sizeof(struct sctp_paddrparams);
|
|
|
|
r = usrsctp_getsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS,
|
|
|
|
&paddrparams, &opt_len);
|
|
|
|
if (r < 0) {
|
|
|
|
LOG(("usrsctp_getsockopt failed: %d", r));
|
|
|
|
} else {
|
|
|
|
// draft-ietf-rtcweb-data-channel-13 section 5: max initial MTU IPV4 1200, IPV6 1280
|
|
|
|
paddrparams.spp_pathmtu = 1200; // safe for either
|
2015-08-26 14:05:39 +00:00
|
|
|
paddrparams.spp_flags &= ~SPP_PMTUD_ENABLE;
|
2015-08-21 18:19:26 +00:00
|
|
|
paddrparams.spp_flags |= SPP_PMTUD_DISABLE;
|
|
|
|
opt_len = (socklen_t)sizeof(struct sctp_paddrparams);
|
|
|
|
r = usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS,
|
|
|
|
&paddrparams, opt_len);
|
|
|
|
if (r < 0) {
|
|
|
|
LOG(("usrsctp_getsockopt failed: %d", r));
|
|
|
|
} else {
|
|
|
|
LOG(("usrsctp: PMTUD disabled, MTU set to %u", paddrparams.spp_pathmtu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 16:17:07 +00:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
// non-blocking
|
2013-04-01 01:09:26 +00:00
|
|
|
return;
|
2013-03-05 16:17:07 +00:00
|
|
|
} else {
|
|
|
|
LOG(("usrsctp_connect failed: %d", errno));
|
|
|
|
mState = CLOSED;
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-01 01:09:26 +00:00
|
|
|
// We set Even/Odd and fire ON_CONNECTION via SCTP_COMM_UP when we get that
|
|
|
|
// This also avoids issues with calling TransportFlow stuff on Mainthread
|
|
|
|
return;
|
2013-03-05 16:17:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Note: currently this doesn't actually notify the application
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
|
|
|
DataChannelOnMessageAvailable::ON_CONNECTION,
|
|
|
|
this)));
|
2013-04-01 01:09:26 +00:00
|
|
|
return;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
// Process any pending Opens
|
|
|
|
void
|
|
|
|
DataChannelConnection::ProcessQueuedOpens()
|
|
|
|
{
|
2013-04-17 03:58:09 +00:00
|
|
|
// The nsDeque holds channels with an AddRef applied. Another reference
|
|
|
|
// (may) be held by the DOMDataChannel, unless it's been GC'd. No other
|
|
|
|
// references should exist.
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
// Can't copy nsDeque's. Move into temp array since any that fail will
|
|
|
|
// go back to mPending
|
|
|
|
nsDeque temp;
|
|
|
|
DataChannel *temp_channel; // really already_AddRefed<>
|
|
|
|
while (nullptr != (temp_channel = static_cast<DataChannel *>(mPending.PopFront()))) {
|
|
|
|
temp.Push(static_cast<void *>(temp_channel));
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel;
|
2013-04-17 03:58:09 +00:00
|
|
|
// All these entries have an AddRef(); make that explicit now via the dont_AddRef()
|
2013-04-01 01:09:26 +00:00
|
|
|
while (nullptr != (channel = dont_AddRef(static_cast<DataChannel *>(temp.PopFront())))) {
|
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_FINISH_OPEN) {
|
|
|
|
LOG(("Processing queued open for %p (%u)", channel.get(), channel->mStream));
|
|
|
|
channel->mFlags &= ~DATA_CHANNEL_FLAGS_FINISH_OPEN;
|
2013-04-17 03:58:09 +00:00
|
|
|
// OpenFinish returns a reference itself, so we need to take it can Release it
|
|
|
|
channel = OpenFinish(channel.forget()); // may reset the flag and re-push
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(false, "How did a DataChannel get queued without the FINISH_OPEN flag?");
|
2013-04-01 01:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
void
|
2012-11-02 19:28:11 +00:00
|
|
|
DataChannelConnection::SctpDtlsInput(TransportFlow *flow,
|
|
|
|
const unsigned char *data, size_t len)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2015-06-03 22:25:57 +00:00
|
|
|
if (MOZ_LOG_TEST(GetSCTPLog(), LogLevel::Debug)) {
|
2013-01-29 07:46:26 +00:00
|
|
|
char *buf;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-11-25 19:02:47 +00:00
|
|
|
if ((buf = usrsctp_dumppacket((void *)data, len, SCTP_DUMP_INBOUND)) != nullptr) {
|
2013-01-29 07:46:26 +00:00
|
|
|
PR_LogPrint("%s", buf);
|
|
|
|
usrsctp_freedumpbuffer(buf);
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
// Pass the data to SCTP
|
|
|
|
usrsctp_conninput(static_cast<void *>(this), data, len, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-06-28 08:26:34 +00:00
|
|
|
DataChannelConnection::SendPacket(unsigned char data[], size_t len, bool release)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
//LOG(("%p: SCTP/DTLS sent %ld bytes", this, len));
|
2012-11-02 19:28:11 +00:00
|
|
|
int res = mTransportFlow->SendPacket(data, len) < 0 ? 1 : 0;
|
|
|
|
if (release)
|
2014-06-28 08:26:34 +00:00
|
|
|
delete [] data;
|
2012-11-02 19:28:11 +00:00
|
|
|
return res;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
int
|
|
|
|
DataChannelConnection::SctpDtlsOutput(void *addr, void *buffer, size_t length,
|
|
|
|
uint8_t tos, uint8_t set_df)
|
|
|
|
{
|
|
|
|
DataChannelConnection *peer = static_cast<DataChannelConnection *>(addr);
|
2012-10-29 03:48:36 +00:00
|
|
|
int res;
|
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
if (MOZ_LOG_TEST(GetSCTPLog(), LogLevel::Debug)) {
|
2013-01-29 07:46:26 +00:00
|
|
|
char *buf;
|
|
|
|
|
2013-11-25 19:02:47 +00:00
|
|
|
if ((buf = usrsctp_dumppacket(buffer, length, SCTP_DUMP_OUTBOUND)) != nullptr) {
|
2013-01-29 07:46:26 +00:00
|
|
|
PR_LogPrint("%s", buf);
|
|
|
|
usrsctp_freedumpbuffer(buf);
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 19:28:11 +00:00
|
|
|
// We're async proxying even if on the STSThread because this is called
|
|
|
|
// with internal SCTP locks held in some cases (such as in usrsctp_connect()).
|
|
|
|
// SCTP has an option for Apple, on IP connections only, to release at least
|
|
|
|
// one of the locks before calling a packet output routine; with changes to
|
|
|
|
// the underlying SCTP stack this might remove the need to use an async proxy.
|
|
|
|
if (0 /*peer->IsSTSThread()*/) {
|
|
|
|
res = peer->SendPacket(static_cast<unsigned char *>(buffer), length, false);
|
2012-10-29 03:48:36 +00:00
|
|
|
} else {
|
2012-11-02 19:28:11 +00:00
|
|
|
unsigned char *data = new unsigned char[length];
|
|
|
|
memcpy(data, buffer, length);
|
2014-05-13 15:01:15 +00:00
|
|
|
// Commented out since we have to Dispatch SendPacket to avoid deadlock"
|
|
|
|
// res = -1;
|
|
|
|
|
2012-10-29 03:48:36 +00:00
|
|
|
// XXX It might be worthwhile to add an assertion against the thread
|
|
|
|
// somehow getting into the DataChannel/SCTP code again, as
|
|
|
|
// DISPATCH_SYNC is not fully blocking. This may be tricky, as it
|
|
|
|
// needs to be a per-thread check, not a global.
|
2012-11-02 19:28:11 +00:00
|
|
|
peer->mSTS->Dispatch(WrapRunnable(
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannelConnection>(peer),
|
2012-12-14 04:30:11 +00:00
|
|
|
&DataChannelConnection::SendPacket, data, length, true),
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DISPATCH_NORMAL);
|
2012-11-02 19:28:11 +00:00
|
|
|
res = 0; // cheat! Packets can always be dropped later anyways
|
2012-10-29 03:48:36 +00:00
|
|
|
}
|
|
|
|
return res;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
#ifdef ALLOW_DIRECT_SCTP_LISTEN_CONNECT
|
2012-10-03 23:51:23 +00:00
|
|
|
// listen for incoming associations
|
|
|
|
// Blocks! - Don't call this from main thread!
|
2013-04-01 01:09:26 +00:00
|
|
|
|
|
|
|
#error This code will not work as-is since SetEvenOdd() runs on Mainthread
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
bool
|
|
|
|
DataChannelConnection::Listen(unsigned short port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t addr_len;
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(!NS_IsMainThread(), "Blocks, do not call from main thread!!!");
|
|
|
|
|
|
|
|
/* Acting as the 'server' */
|
|
|
|
memset((void *)&addr, 0, sizeof(addr));
|
|
|
|
#ifdef HAVE_SIN_LEN
|
|
|
|
addr.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Waiting for connections on port %u", ntohs(addr.sin_port)));
|
2012-10-03 23:51:23 +00:00
|
|
|
mState = CONNECTING;
|
|
|
|
if (usrsctp_bind(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr), sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
LOG(("***Failed userspace_bind"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (usrsctp_listen(mMasterSocket, 1) < 0) {
|
|
|
|
LOG(("***Failed userspace_listen"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Accepting connection"));
|
|
|
|
addr_len = 0;
|
|
|
|
if ((mSocket = usrsctp_accept(mMasterSocket, nullptr, &addr_len)) == nullptr) {
|
|
|
|
LOG(("***Failed accept"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mState = OPEN;
|
|
|
|
|
2012-11-01 00:31:39 +00:00
|
|
|
struct linger l;
|
|
|
|
l.l_onoff = 1;
|
|
|
|
l.l_linger = 0;
|
|
|
|
if (usrsctp_setsockopt(mSocket, SOL_SOCKET, SO_LINGER,
|
|
|
|
(const void *)&l, (socklen_t)sizeof(struct linger)) < 0) {
|
|
|
|
LOG(("Couldn't set SO_LINGER on SCTP socket"));
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
SetEvenOdd();
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
// Notify Connection open
|
|
|
|
// XXX We need to make sure connection sticks around until the message is delivered
|
|
|
|
LOG(("%s: sending ON_CONNECTION for %p", __FUNCTION__, this));
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2012-10-03 23:51:23 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CONNECTION,
|
2015-07-10 03:24:11 +00:00
|
|
|
this, (DataChannel *) nullptr)));
|
2012-10-03 23:51:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocks! - Don't call this from main thread!
|
|
|
|
bool
|
|
|
|
DataChannelConnection::Connect(const char *addr, unsigned short port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr4;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(!NS_IsMainThread(), "Blocks, do not call from main thread!!!");
|
|
|
|
|
|
|
|
/* Acting as the connector */
|
|
|
|
LOG(("Connecting to %s, port %u", addr, port));
|
|
|
|
memset((void *)&addr4, 0, sizeof(struct sockaddr_in));
|
|
|
|
memset((void *)&addr6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
#ifdef HAVE_SIN_LEN
|
|
|
|
addr4.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SIN6_LEN
|
|
|
|
addr6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
|
|
|
addr4.sin_family = AF_INET;
|
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
|
addr4.sin_port = htons(port);
|
|
|
|
addr6.sin6_port = htons(port);
|
|
|
|
mState = CONNECTING;
|
|
|
|
|
|
|
|
#if !defined(__Userspace_os_Windows)
|
|
|
|
if (inet_pton(AF_INET6, addr, &addr6.sin6_addr) == 1) {
|
|
|
|
if (usrsctp_connect(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr6), sizeof(struct sockaddr_in6)) < 0) {
|
|
|
|
LOG(("*** Failed userspace_connect"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (inet_pton(AF_INET, addr, &addr4.sin_addr) == 1) {
|
|
|
|
if (usrsctp_connect(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr4), sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
LOG(("*** Failed userspace_connect"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("*** Illegal destination address."));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
int sslen = sizeof(ss);
|
|
|
|
|
|
|
|
if (!WSAStringToAddressA(const_cast<char *>(addr), AF_INET6, nullptr, (struct sockaddr*)&ss, &sslen)) {
|
|
|
|
addr6.sin6_addr = (reinterpret_cast<struct sockaddr_in6 *>(&ss))->sin6_addr;
|
|
|
|
if (usrsctp_connect(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr6), sizeof(struct sockaddr_in6)) < 0) {
|
|
|
|
LOG(("*** Failed userspace_connect"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!WSAStringToAddressA(const_cast<char *>(addr), AF_INET, nullptr, (struct sockaddr*)&ss, &sslen)) {
|
|
|
|
addr4.sin_addr = (reinterpret_cast<struct sockaddr_in *>(&ss))->sin_addr;
|
|
|
|
if (usrsctp_connect(mMasterSocket, reinterpret_cast<struct sockaddr *>(&addr4), sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
LOG(("*** Failed userspace_connect"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("*** Illegal destination address."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mSocket = mMasterSocket;
|
|
|
|
|
|
|
|
LOG(("connect() succeeded! Entering connected mode"));
|
|
|
|
mState = OPEN;
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
SetEvenOdd();
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
// Notify Connection open
|
|
|
|
// XXX We need to make sure connection sticks around until the message is delivered
|
|
|
|
LOG(("%s: sending ON_CONNECTION for %p", __FUNCTION__, this));
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2012-10-03 23:51:23 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CONNECTION,
|
2015-07-10 03:24:11 +00:00
|
|
|
this, (DataChannel *) nullptr)));
|
2012-10-03 23:51:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-01 01:09:26 +00:00
|
|
|
#endif
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
DataChannel *
|
2013-10-25 20:08:18 +00:00
|
|
|
DataChannelConnection::FindChannelByStream(uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2013-10-25 20:08:18 +00:00
|
|
|
return mStreams.SafeElementAt(stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannelConnection::FindFreeStream()
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2013-04-17 12:30:19 +00:00
|
|
|
uint32_t i, j, limit;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
limit = mStreams.Length();
|
2012-10-03 23:51:23 +00:00
|
|
|
if (limit > MAX_NUM_STREAMS)
|
|
|
|
limit = MAX_NUM_STREAMS;
|
2013-04-01 01:09:25 +00:00
|
|
|
|
|
|
|
for (i = (mAllocateEven ? 0 : 1); i < limit; i += 2) {
|
|
|
|
if (!mStreams[i]) {
|
2012-11-02 19:28:13 +00:00
|
|
|
// Verify it's not still in the process of closing
|
2013-04-17 12:30:19 +00:00
|
|
|
for (j = 0; j < mStreamsResetting.Length(); ++j) {
|
2012-11-02 19:28:13 +00:00
|
|
|
if (mStreamsResetting[j] == i) {
|
2013-04-17 12:30:19 +00:00
|
|
|
break;
|
2012-11-02 19:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-19 11:10:29 +00:00
|
|
|
if (j == mStreamsResetting.Length())
|
2013-04-17 12:30:19 +00:00
|
|
|
break;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-17 12:30:19 +00:00
|
|
|
if (i >= limit) {
|
2012-10-03 23:51:23 +00:00
|
|
|
return INVALID_STREAM;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannelConnection::RequestMoreStreams(int32_t aNeeded)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
struct sctp_status status;
|
|
|
|
struct sctp_add_streams sas;
|
|
|
|
uint32_t outStreamsNeeded;
|
|
|
|
socklen_t len;
|
|
|
|
|
2014-10-28 15:06:00 +00:00
|
|
|
if (aNeeded + mStreams.Length() > MAX_NUM_STREAMS) {
|
2013-04-01 01:09:25 +00:00
|
|
|
aNeeded = MAX_NUM_STREAMS - mStreams.Length();
|
2014-10-28 15:06:00 +00:00
|
|
|
}
|
|
|
|
if (aNeeded <= 0) {
|
2012-10-03 23:51:23 +00:00
|
|
|
return false;
|
2014-10-28 15:06:00 +00:00
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
len = (socklen_t)sizeof(struct sctp_status);
|
|
|
|
if (usrsctp_getsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_STATUS, &status, &len) < 0) {
|
|
|
|
LOG(("***failed: getsockopt SCTP_STATUS"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
outStreamsNeeded = aNeeded; // number to add
|
|
|
|
|
2014-10-28 15:06:00 +00:00
|
|
|
// Note: if multiple channel opens happen when we don't have enough space,
|
|
|
|
// we'll call RequestMoreStreams() multiple times
|
|
|
|
memset(&sas, 0, sizeof(sas));
|
2012-10-03 23:51:23 +00:00
|
|
|
sas.sas_instrms = 0;
|
|
|
|
sas.sas_outstrms = (uint16_t)outStreamsNeeded; /* XXX error handling */
|
|
|
|
// Doesn't block, we get an event when it succeeds or fails
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_ADD_STREAMS, &sas,
|
|
|
|
(socklen_t) sizeof(struct sctp_add_streams)) < 0) {
|
2014-10-28 15:06:00 +00:00
|
|
|
if (errno == EALREADY) {
|
|
|
|
LOG(("Already have %u output streams", outStreamsNeeded));
|
2012-10-03 23:51:23 +00:00
|
|
|
return true;
|
2014-10-28 15:06:00 +00:00
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
LOG(("***failed: setsockopt ADD errno=%d", errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(("Requested %u more streams", outStreamsNeeded));
|
2014-10-28 15:06:00 +00:00
|
|
|
// We add to mStreams when we get a SCTP_STREAM_CHANGE_EVENT and the
|
|
|
|
// values are larger than mStreams.Length()
|
2012-10-03 23:51:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannelConnection::SendControlMessage(void *msg, uint32_t len, uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
struct sctp_sndinfo sndinfo;
|
|
|
|
|
|
|
|
// Note: Main-thread IO, but doesn't block
|
|
|
|
memset(&sndinfo, 0, sizeof(struct sctp_sndinfo));
|
2013-04-01 01:09:25 +00:00
|
|
|
sndinfo.snd_sid = stream;
|
2012-10-03 23:51:23 +00:00
|
|
|
sndinfo.snd_ppid = htonl(DATA_CHANNEL_PPID_CONTROL);
|
|
|
|
if (usrsctp_sendv(mSocket, msg, len, nullptr, 0,
|
|
|
|
&sndinfo, (socklen_t)sizeof(struct sctp_sndinfo),
|
|
|
|
SCTP_SENDV_SNDINFO, 0) < 0) {
|
|
|
|
//LOG(("***failed: sctp_sendv")); don't log because errno is a return!
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
int32_t
|
|
|
|
DataChannelConnection::SendOpenAckMessage(uint16_t stream)
|
|
|
|
{
|
|
|
|
struct rtcweb_datachannel_ack ack;
|
|
|
|
|
|
|
|
memset(&ack, 0, sizeof(struct rtcweb_datachannel_ack));
|
|
|
|
ack.msg_type = DATA_CHANNEL_ACK;
|
|
|
|
|
|
|
|
return SendControlMessage(&ack, sizeof(ack), stream);
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
int32_t
|
|
|
|
DataChannelConnection::SendOpenRequestMessage(const nsACString& label,
|
2013-04-01 01:09:26 +00:00
|
|
|
const nsACString& protocol,
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t stream, bool unordered,
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t prPolicy, uint32_t prValue)
|
|
|
|
{
|
2014-11-26 19:00:36 +00:00
|
|
|
const int label_len = label.Length(); // not including nul
|
|
|
|
const int proto_len = protocol.Length(); // not including nul
|
|
|
|
// careful - request struct include one char for the label
|
|
|
|
const int req_size = sizeof(struct rtcweb_datachannel_open_request) - 1 +
|
|
|
|
label_len + proto_len;
|
2012-10-03 23:51:23 +00:00
|
|
|
struct rtcweb_datachannel_open_request *req =
|
2014-11-26 19:00:36 +00:00
|
|
|
(struct rtcweb_datachannel_open_request*) moz_xmalloc(req_size);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2014-11-26 19:00:36 +00:00
|
|
|
memset(req, 0, req_size);
|
2012-10-03 23:51:23 +00:00
|
|
|
req->msg_type = DATA_CHANNEL_OPEN_REQUEST;
|
|
|
|
switch (prPolicy) {
|
|
|
|
case SCTP_PR_SCTP_NONE:
|
|
|
|
req->channel_type = DATA_CHANNEL_RELIABLE;
|
|
|
|
break;
|
|
|
|
case SCTP_PR_SCTP_TTL:
|
|
|
|
req->channel_type = DATA_CHANNEL_PARTIAL_RELIABLE_TIMED;
|
|
|
|
break;
|
|
|
|
case SCTP_PR_SCTP_RTX:
|
|
|
|
req->channel_type = DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// FIX! need to set errno! Or make all these SendXxxx() funcs return 0 or errno!
|
2015-02-19 04:51:06 +00:00
|
|
|
free(req);
|
2012-10-03 23:51:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (unordered) {
|
2013-04-01 01:09:26 +00:00
|
|
|
// Per the current types, all differ by 0x80 between ordered and unordered
|
|
|
|
req->channel_type |= 0x80; // NOTE: be careful if new types are added in the future
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
2013-04-01 01:09:26 +00:00
|
|
|
|
2013-04-09 04:55:16 +00:00
|
|
|
req->reliability_param = htonl(prValue);
|
2012-10-03 23:51:23 +00:00
|
|
|
req->priority = htons(0); /* XXX: add support */
|
2013-04-09 04:51:04 +00:00
|
|
|
req->label_length = htons(label_len);
|
|
|
|
req->protocol_length = htons(proto_len);
|
2013-04-01 01:09:26 +00:00
|
|
|
memcpy(&req->label[0], PromiseFlatCString(label).get(), label_len);
|
2013-04-09 04:51:04 +00:00
|
|
|
memcpy(&req->label[label_len], PromiseFlatCString(protocol).get(), proto_len);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2014-11-26 19:00:36 +00:00
|
|
|
int32_t result = SendControlMessage(req, req_size, stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2015-02-19 04:51:06 +00:00
|
|
|
free(req);
|
2012-10-03 23:51:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX This should use a separate thread (outbound queue) which should
|
|
|
|
// select() to know when to *try* to send data to the socket again.
|
|
|
|
// Alternatively, it can use a timeout, but that's guaranteed to be wrong
|
|
|
|
// (just not sure in what direction). We could re-implement NSPR's
|
|
|
|
// PR_POLL_WRITE/etc handling... with a lot of work.
|
|
|
|
|
2013-01-12 10:40:22 +00:00
|
|
|
// Better yet, use the SCTP stack's notifications on buffer state to avoid
|
|
|
|
// filling the SCTP's buffers.
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
// returns if we're still blocked or not
|
|
|
|
bool
|
|
|
|
DataChannelConnection::SendDeferredMessages()
|
|
|
|
{
|
|
|
|
uint32_t i;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel; // we may null out the refs to this
|
2012-10-03 23:51:23 +00:00
|
|
|
bool still_blocked = false;
|
|
|
|
bool sent = false;
|
|
|
|
|
|
|
|
// This may block while something is modifying channels, but should not block for IO
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// XXX For total fairness, on a still_blocked we'd start next time at the
|
|
|
|
// same index. Sorry, not going to bother for now.
|
2013-04-01 01:09:25 +00:00
|
|
|
for (i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
channel = mStreams[i];
|
2012-10-03 23:51:23 +00:00
|
|
|
if (!channel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Only one of these should be set....
|
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_SEND_REQ) {
|
2013-04-01 01:09:26 +00:00
|
|
|
if (SendOpenRequestMessage(channel->mLabel, channel->mProtocol,
|
|
|
|
channel->mStream,
|
2013-04-01 01:09:26 +00:00
|
|
|
channel->mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED,
|
2012-10-03 23:51:23 +00:00
|
|
|
channel->mPrPolicy, channel->mPrValue)) {
|
|
|
|
channel->mFlags &= ~DATA_CHANNEL_FLAGS_SEND_REQ;
|
2014-10-28 15:06:00 +00:00
|
|
|
|
|
|
|
channel->mState = OPEN;
|
|
|
|
channel->mReady = true;
|
|
|
|
LOG(("%s: sending ON_CHANNEL_OPEN for %p", __FUNCTION__, channel.get()));
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2014-10-28 15:06:00 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_OPEN, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2012-10-03 23:51:23 +00:00
|
|
|
sent = true;
|
|
|
|
} else {
|
2012-11-06 18:18:39 +00:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2012-10-03 23:51:23 +00:00
|
|
|
still_blocked = true;
|
|
|
|
} else {
|
|
|
|
// Close the channel, inform the user
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams[channel->mStream] = nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
channel->mState = CLOSED;
|
|
|
|
// Don't need to reset; we didn't open it
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2012-10-03 23:51:23 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_CLOSED, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (still_blocked)
|
|
|
|
break;
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_SEND_ACK) {
|
|
|
|
if (SendOpenAckMessage(channel->mStream)) {
|
|
|
|
channel->mFlags &= ~DATA_CHANNEL_FLAGS_SEND_ACK;
|
|
|
|
sent = true;
|
|
|
|
} else {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
still_blocked = true;
|
|
|
|
} else {
|
|
|
|
// Close the channel, inform the user
|
|
|
|
CloseInt(channel);
|
|
|
|
// XXX send error via DataChannelOnMessageAvailable (bug 843625)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (still_blocked)
|
|
|
|
break;
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_SEND_DATA) {
|
|
|
|
bool failed_send = false;
|
|
|
|
int32_t result;
|
|
|
|
|
|
|
|
if (channel->mState == CLOSED || channel->mState == CLOSING) {
|
|
|
|
channel->mBufferedData.Clear();
|
|
|
|
}
|
2015-09-28 23:02:23 +00:00
|
|
|
|
|
|
|
uint32_t buffered_amount = channel->GetBufferedAmount();
|
|
|
|
uint32_t threshold = channel->GetBufferedAmountLowThreshold();
|
|
|
|
bool was_over_threshold = buffered_amount >= threshold;
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
while (!channel->mBufferedData.IsEmpty() &&
|
|
|
|
!failed_send) {
|
|
|
|
struct sctp_sendv_spa *spa = channel->mBufferedData[0]->mSpa;
|
|
|
|
const char *data = channel->mBufferedData[0]->mData;
|
2015-09-28 23:02:23 +00:00
|
|
|
size_t len = channel->mBufferedData[0]->mLength;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// SCTP will return EMSGSIZE if the message is bigger than the buffer
|
|
|
|
// size (or EAGAIN if there isn't space)
|
|
|
|
if ((result = usrsctp_sendv(mSocket, data, len,
|
|
|
|
nullptr, 0,
|
|
|
|
(void *)spa, (socklen_t)sizeof(struct sctp_sendv_spa),
|
|
|
|
SCTP_SENDV_SPA,
|
2015-03-27 17:34:45 +00:00
|
|
|
0)) < 0) {
|
2012-11-06 18:18:39 +00:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2012-10-03 23:51:23 +00:00
|
|
|
// leave queued for resend
|
|
|
|
failed_send = true;
|
|
|
|
LOG(("queue full again when resending %d bytes (%d)", len, result));
|
|
|
|
} else {
|
|
|
|
LOG(("error %d re-sending string", errno));
|
|
|
|
failed_send = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("Resent buffer of %d bytes (%d)", len, result));
|
|
|
|
sent = true;
|
2015-09-28 23:02:23 +00:00
|
|
|
// In theory this could underflow if >4GB was buffered and re
|
|
|
|
// truncated in GetBufferedAmount(), but this won't cause any problems.
|
|
|
|
buffered_amount -= channel->mBufferedData[0]->mLength;
|
2012-10-03 23:51:23 +00:00
|
|
|
channel->mBufferedData.RemoveElementAt(0);
|
2015-09-28 23:02:23 +00:00
|
|
|
// can never fire with default threshold of 0
|
|
|
|
if (was_over_threshold && buffered_amount < threshold) {
|
|
|
|
LOG(("%s: sending BUFFER_LOW_THRESHOLD for %s/%s: %u", __FUNCTION__,
|
|
|
|
channel->mLabel.get(), channel->mProtocol.get(), channel->mStream));
|
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
|
|
|
DataChannelOnMessageAvailable::BUFFER_LOW_THRESHOLD,
|
|
|
|
this, channel)));
|
|
|
|
was_over_threshold = false;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (channel->mBufferedData.IsEmpty())
|
|
|
|
channel->mFlags &= ~DATA_CHANNEL_FLAGS_SEND_DATA;
|
|
|
|
else
|
|
|
|
still_blocked = true;
|
|
|
|
}
|
|
|
|
if (still_blocked)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!still_blocked) {
|
|
|
|
// mDeferTimeout becomes an estimate of how long we need to wait next time we block
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// adjust time? More time for next wait if we didn't send anything, less if did
|
|
|
|
// Pretty crude, but better than nothing; just to keep CPU use down
|
|
|
|
if (!sent && mDeferTimeout < 50)
|
|
|
|
mDeferTimeout++;
|
|
|
|
else if (sent && mDeferTimeout > 10)
|
|
|
|
mDeferTimeout--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleOpenRequestMessage(const struct rtcweb_datachannel_open_request *req,
|
|
|
|
size_t length,
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel;
|
2012-10-03 23:51:23 +00:00
|
|
|
uint32_t prValue;
|
|
|
|
uint16_t prPolicy;
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2013-04-09 04:51:04 +00:00
|
|
|
if (length != (sizeof(*req) - 1) + ntohs(req->label_length) + ntohs(req->protocol_length)) {
|
2013-10-25 20:08:18 +00:00
|
|
|
LOG(("%s: Inconsistent length: %u, should be %u", __FUNCTION__, length,
|
2013-04-09 04:51:04 +00:00
|
|
|
(sizeof(*req) - 1) + ntohs(req->label_length) + ntohs(req->protocol_length)));
|
|
|
|
if (length < (sizeof(*req) - 1) + ntohs(req->label_length) + ntohs(req->protocol_length))
|
2013-04-01 01:09:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
LOG(("%s: length %u, sizeof(*req) = %u", __FUNCTION__, length, sizeof(*req)));
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
switch (req->channel_type) {
|
|
|
|
case DATA_CHANNEL_RELIABLE:
|
2013-04-01 01:09:26 +00:00
|
|
|
case DATA_CHANNEL_RELIABLE_UNORDERED:
|
2012-10-03 23:51:23 +00:00
|
|
|
prPolicy = SCTP_PR_SCTP_NONE;
|
|
|
|
break;
|
|
|
|
case DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT:
|
2013-04-01 01:09:26 +00:00
|
|
|
case DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT_UNORDERED:
|
2012-10-03 23:51:23 +00:00
|
|
|
prPolicy = SCTP_PR_SCTP_RTX;
|
|
|
|
break;
|
|
|
|
case DATA_CHANNEL_PARTIAL_RELIABLE_TIMED:
|
2013-04-01 01:09:26 +00:00
|
|
|
case DATA_CHANNEL_PARTIAL_RELIABLE_TIMED_UNORDERED:
|
2012-10-03 23:51:23 +00:00
|
|
|
prPolicy = SCTP_PR_SCTP_TTL;
|
|
|
|
break;
|
|
|
|
default:
|
2014-10-28 15:06:00 +00:00
|
|
|
LOG(("Unknown channel type", req->channel_type));
|
2012-10-03 23:51:23 +00:00
|
|
|
/* XXX error handling */
|
|
|
|
return;
|
|
|
|
}
|
2013-04-09 04:55:16 +00:00
|
|
|
prValue = ntohl(req->reliability_param);
|
2013-04-01 01:09:26 +00:00
|
|
|
flags = (req->channel_type & 0x80) ? DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED : 0;
|
2013-04-01 01:09:26 +00:00
|
|
|
|
|
|
|
if ((channel = FindChannelByStream(stream))) {
|
|
|
|
if (!(channel->mFlags & DATA_CHANNEL_FLAGS_EXTERNAL_NEGOTIATED)) {
|
|
|
|
LOG(("ERROR: HandleOpenRequestMessage: channel for stream %u is in state %d instead of CLOSED.",
|
|
|
|
stream, channel->mState));
|
|
|
|
/* XXX: some error handling */
|
|
|
|
} else {
|
|
|
|
LOG(("Open for externally negotiated channel %u", stream));
|
|
|
|
// XXX should also check protocol, maybe label
|
|
|
|
if (prPolicy != channel->mPrPolicy ||
|
|
|
|
prValue != channel->mPrValue ||
|
2013-04-01 01:09:26 +00:00
|
|
|
flags != (channel->mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED))
|
2013-04-01 01:09:26 +00:00
|
|
|
{
|
|
|
|
LOG(("WARNING: external negotiation mismatch with OpenRequest:"
|
|
|
|
"channel %u, policy %u/%u, value %u/%u, flags %x/%x",
|
|
|
|
stream, prPolicy, channel->mPrPolicy,
|
|
|
|
prValue, channel->mPrValue, flags, channel->mFlags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-10-28 15:06:00 +00:00
|
|
|
if (stream >= mStreams.Length()) {
|
|
|
|
LOG(("%s: stream %u out of bounds (%u)", __FUNCTION__, stream, mStreams.Length()));
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 01:09:26 +00:00
|
|
|
|
2013-04-09 04:51:04 +00:00
|
|
|
nsCString label(nsDependentCSubstring(&req->label[0], ntohs(req->label_length)));
|
|
|
|
nsCString protocol(nsDependentCSubstring(&req->label[ntohs(req->label_length)],
|
|
|
|
ntohs(req->protocol_length)));
|
2013-04-01 01:09:26 +00:00
|
|
|
|
2012-11-02 19:28:13 +00:00
|
|
|
channel = new DataChannel(this,
|
2013-04-01 01:09:25 +00:00
|
|
|
stream,
|
2012-10-03 23:51:23 +00:00
|
|
|
DataChannel::CONNECTING,
|
|
|
|
label,
|
2013-04-01 01:09:26 +00:00
|
|
|
protocol,
|
2012-10-03 23:51:23 +00:00
|
|
|
prPolicy, prValue,
|
|
|
|
flags,
|
|
|
|
nullptr, nullptr);
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams[stream] = channel;
|
2013-03-27 23:46:48 +00:00
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
channel->mState = DataChannel::WAITING_TO_OPEN;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2014-10-28 15:06:00 +00:00
|
|
|
LOG(("%s: sending ON_CHANNEL_CREATED for %s/%s: %u (state %u)", __FUNCTION__,
|
|
|
|
channel->mLabel.get(), channel->mProtocol.get(), stream, channel->mState));
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_CREATED,
|
2015-07-10 03:24:11 +00:00
|
|
|
this, channel)));
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("%s: deferring sending ON_CHANNEL_OPEN for %p", __FUNCTION__, channel.get()));
|
2013-04-01 01:09:25 +00:00
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
if (!SendOpenAckMessage(stream)) {
|
|
|
|
// XXX Only on EAGAIN!? And if not, then close the channel??
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_SEND_ACK;
|
|
|
|
StartDefer();
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
// Now process any queued data messages for the channel (which will
|
|
|
|
// themselves likely get queued until we leave WAITING_TO_OPEN, plus any
|
|
|
|
// more that come in before that happens)
|
|
|
|
DeliverQueuedData(stream);
|
|
|
|
}
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
// NOTE: the updated spec from the IETF says we should set in-order until we receive an ACK.
|
|
|
|
// That would make this code moot. Keep it for now for backwards compatibility.
|
2013-04-01 01:09:25 +00:00
|
|
|
void
|
|
|
|
DataChannelConnection::DeliverQueuedData(uint16_t stream)
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < mQueuedData.Length()) {
|
|
|
|
// Careful! we may modify the array length from within the loop!
|
|
|
|
if (mQueuedData[i]->mStream == stream) {
|
|
|
|
LOG(("Delivering queued data for stream %u, length %u",
|
2015-09-28 23:02:23 +00:00
|
|
|
stream, (unsigned int) mQueuedData[i]->mLength));
|
2013-04-01 01:09:25 +00:00
|
|
|
// Deliver the queued data
|
|
|
|
HandleDataMessage(mQueuedData[i]->mPpid,
|
|
|
|
mQueuedData[i]->mData, mQueuedData[i]->mLength,
|
|
|
|
mQueuedData[i]->mStream);
|
|
|
|
mQueuedData.RemoveElementAt(i);
|
|
|
|
continue; // don't bump index since we removed the element
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
void
|
|
|
|
DataChannelConnection::HandleOpenAckMessage(const struct rtcweb_datachannel_ack *ack,
|
|
|
|
size_t length, uint16_t stream)
|
|
|
|
{
|
|
|
|
DataChannel *channel;
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
channel = FindChannelByStream(stream);
|
|
|
|
NS_ENSURE_TRUE_VOID(channel);
|
|
|
|
|
|
|
|
LOG(("OpenAck received for stream %u, waiting=%d", stream,
|
|
|
|
(channel->mFlags & DATA_CHANNEL_FLAGS_WAITING_ACK) ? 1 : 0));
|
|
|
|
|
|
|
|
channel->mFlags &= ~DATA_CHANNEL_FLAGS_WAITING_ACK;
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
void
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannelConnection::HandleUnknownMessage(uint32_t ppid, size_t length, uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
/* XXX: Send an error message? */
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("unknown DataChannel message received: %u, len %ld on stream %lu", ppid, length, stream));
|
2012-10-03 23:51:23 +00:00
|
|
|
// XXX Log to JS error console if possible
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleDataMessage(uint32_t ppid,
|
|
|
|
const void *data, size_t length,
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
DataChannel *channel;
|
|
|
|
const char *buffer = (const char *) data;
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
channel = FindChannelByStream(stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// XXX A closed channel may trip this... check
|
2013-10-25 20:08:18 +00:00
|
|
|
// NOTE: the updated spec from the IETF says we should set in-order until we receive an ACK.
|
|
|
|
// That would make this code moot. Keep it for now for backwards compatibility.
|
2013-04-01 01:09:25 +00:00
|
|
|
if (!channel) {
|
|
|
|
// In the updated 0-RTT open case, the sender can send data immediately
|
|
|
|
// after Open, and doesn't set the in-order bit (since we don't have a
|
|
|
|
// response or ack). Also, with external negotiation, data can come in
|
|
|
|
// before we're told about the external negotiation. We need to buffer
|
|
|
|
// data until either a) Open comes in, if the ordering get messed up,
|
|
|
|
// or b) the app tells us this channel was externally negotiated. When
|
|
|
|
// these occur, we deliver the data.
|
|
|
|
|
|
|
|
// Since this is rare and non-performance, keep a single list of queued
|
|
|
|
// data messages to deliver once the channel opens.
|
|
|
|
LOG(("Queuing data for stream %u, length %u", stream, length));
|
2014-06-08 13:53:13 +00:00
|
|
|
// Copies data
|
2013-04-01 01:09:25 +00:00
|
|
|
mQueuedData.AppendElement(new QueuedDataMessage(stream, ppid, data, length));
|
|
|
|
return;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// XXX should this be a simple if, no warnings/debugbreaks?
|
2012-12-24 04:45:57 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(channel->mState != CLOSED);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
{
|
2014-06-08 13:53:13 +00:00
|
|
|
nsAutoCString recvData(buffer, length); // copies (<64) or allocates
|
2013-04-12 14:08:49 +00:00
|
|
|
bool is_binary = true;
|
|
|
|
|
|
|
|
if (ppid == DATA_CHANNEL_PPID_DOMSTRING ||
|
|
|
|
ppid == DATA_CHANNEL_PPID_DOMSTRING_LAST) {
|
|
|
|
is_binary = false;
|
|
|
|
}
|
|
|
|
if (is_binary != channel->mIsRecvBinary && !channel->mRecvBuffer.IsEmpty()) {
|
|
|
|
NS_WARNING("DataChannel message aborted by fragment type change!");
|
|
|
|
channel->mRecvBuffer.Truncate(0);
|
|
|
|
}
|
|
|
|
channel->mIsRecvBinary = is_binary;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
switch (ppid) {
|
|
|
|
case DATA_CHANNEL_PPID_DOMSTRING:
|
2013-04-12 14:08:49 +00:00
|
|
|
case DATA_CHANNEL_PPID_BINARY:
|
|
|
|
channel->mRecvBuffer += recvData;
|
|
|
|
LOG(("DataChannel: Partial %s message of length %lu (total %u) on channel id %u",
|
|
|
|
is_binary ? "binary" : "string", length, channel->mRecvBuffer.Length(),
|
|
|
|
channel->mStream));
|
|
|
|
return; // Not ready to notify application
|
|
|
|
|
|
|
|
case DATA_CHANNEL_PPID_DOMSTRING_LAST:
|
|
|
|
LOG(("DataChannel: String message received of length %lu on channel %u",
|
|
|
|
length, channel->mStream));
|
|
|
|
if (!channel->mRecvBuffer.IsEmpty()) {
|
|
|
|
channel->mRecvBuffer += recvData;
|
|
|
|
LOG(("%s: sending ON_DATA (string fragmented) for %p", __FUNCTION__, channel));
|
|
|
|
channel->SendOrQueue(new DataChannelOnMessageAvailable(
|
|
|
|
DataChannelOnMessageAvailable::ON_DATA, this,
|
|
|
|
channel, channel->mRecvBuffer, -1));
|
|
|
|
channel->mRecvBuffer.Truncate(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else send using recvData normally
|
2012-10-03 23:51:23 +00:00
|
|
|
length = -1; // Flag for DOMString
|
|
|
|
|
|
|
|
// WebSockets checks IsUTF8() here; we can try to deliver it
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_CHANNEL_PPID_BINARY_LAST:
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("DataChannel: Received binary message of length %lu on channel id %u",
|
|
|
|
length, channel->mStream));
|
2013-04-12 14:08:49 +00:00
|
|
|
if (!channel->mRecvBuffer.IsEmpty()) {
|
|
|
|
channel->mRecvBuffer += recvData;
|
2012-10-03 23:51:23 +00:00
|
|
|
LOG(("%s: sending ON_DATA (binary fragmented) for %p", __FUNCTION__, channel));
|
2012-11-02 19:28:13 +00:00
|
|
|
channel->SendOrQueue(new DataChannelOnMessageAvailable(
|
|
|
|
DataChannelOnMessageAvailable::ON_DATA, this,
|
2013-04-12 14:08:49 +00:00
|
|
|
channel, channel->mRecvBuffer,
|
|
|
|
channel->mRecvBuffer.Length()));
|
|
|
|
channel->mRecvBuffer.Truncate(0);
|
2012-10-03 23:51:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else send using recvData normally
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown data PPID");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Notify onmessage */
|
|
|
|
LOG(("%s: sending ON_DATA for %p", __FUNCTION__, channel));
|
2012-11-02 19:28:13 +00:00
|
|
|
channel->SendOrQueue(new DataChannelOnMessageAvailable(
|
|
|
|
DataChannelOnMessageAvailable::ON_DATA, this,
|
|
|
|
channel, recvData, length));
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called with mLock locked!
|
|
|
|
void
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannelConnection::HandleMessage(const void *buffer, size_t length, uint32_t ppid, uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
const struct rtcweb_datachannel_open_request *req;
|
2013-10-25 20:08:18 +00:00
|
|
|
const struct rtcweb_datachannel_ack *ack;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
switch (ppid) {
|
|
|
|
case DATA_CHANNEL_PPID_CONTROL:
|
2013-04-01 01:09:25 +00:00
|
|
|
req = static_cast<const struct rtcweb_datachannel_open_request *>(buffer);
|
2013-10-25 20:08:18 +00:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE_VOID(length >= sizeof(*ack)); // smallest message
|
2013-04-01 01:09:25 +00:00
|
|
|
switch (req->msg_type) {
|
2012-10-03 23:51:23 +00:00
|
|
|
case DATA_CHANNEL_OPEN_REQUEST:
|
2013-10-25 20:08:18 +00:00
|
|
|
// structure includes a possibly-unused char label[1] (in a packed structure)
|
|
|
|
NS_ENSURE_TRUE_VOID(length >= sizeof(*req) - 1);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
HandleOpenRequestMessage(req, length, stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
2013-10-25 20:08:18 +00:00
|
|
|
case DATA_CHANNEL_ACK:
|
|
|
|
// >= sizeof(*ack) checked above
|
|
|
|
|
|
|
|
ack = static_cast<const struct rtcweb_datachannel_ack *>(buffer);
|
|
|
|
HandleOpenAckMessage(ack, length, stream);
|
|
|
|
break;
|
2012-10-03 23:51:23 +00:00
|
|
|
default:
|
2013-04-01 01:09:25 +00:00
|
|
|
HandleUnknownMessage(ppid, length, stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DATA_CHANNEL_PPID_DOMSTRING:
|
2013-04-12 14:08:49 +00:00
|
|
|
case DATA_CHANNEL_PPID_DOMSTRING_LAST:
|
2012-10-03 23:51:23 +00:00
|
|
|
case DATA_CHANNEL_PPID_BINARY:
|
|
|
|
case DATA_CHANNEL_PPID_BINARY_LAST:
|
2013-04-01 01:09:25 +00:00
|
|
|
HandleDataMessage(ppid, buffer, length, stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(("Message of length %lu, PPID %u on stream %u received.",
|
2013-04-01 01:09:25 +00:00
|
|
|
length, ppid, stream));
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleAssociationChangeEvent(const struct sctp_assoc_change *sac)
|
|
|
|
{
|
|
|
|
uint32_t i, n;
|
|
|
|
|
|
|
|
switch (sac->sac_state) {
|
|
|
|
case SCTP_COMM_UP:
|
|
|
|
LOG(("Association change: SCTP_COMM_UP"));
|
2013-03-05 16:17:07 +00:00
|
|
|
if (mState == CONNECTING) {
|
|
|
|
mSocket = mMasterSocket;
|
|
|
|
mState = OPEN;
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
SetEvenOdd();
|
|
|
|
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-03-05 16:17:07 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CONNECTION,
|
2015-07-10 03:24:11 +00:00
|
|
|
this)));
|
2013-03-05 16:17:07 +00:00
|
|
|
LOG(("DTLS connect() succeeded! Entering connected mode"));
|
2013-04-01 01:09:26 +00:00
|
|
|
|
|
|
|
// Open any streams pending...
|
|
|
|
ProcessQueuedOpens();
|
|
|
|
|
2013-03-05 16:17:07 +00:00
|
|
|
} else if (mState == OPEN) {
|
|
|
|
LOG(("DataConnection Already OPEN"));
|
|
|
|
} else {
|
|
|
|
LOG(("Unexpected state: %d", mState));
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
case SCTP_COMM_LOST:
|
|
|
|
LOG(("Association change: SCTP_COMM_LOST"));
|
2013-06-02 13:48:17 +00:00
|
|
|
// This association is toast, so also close all the channels -- from mainthread!
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-03-05 16:17:07 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_DISCONNECTED,
|
2015-07-10 03:24:11 +00:00
|
|
|
this)));
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
case SCTP_RESTART:
|
|
|
|
LOG(("Association change: SCTP_RESTART"));
|
|
|
|
break;
|
|
|
|
case SCTP_SHUTDOWN_COMP:
|
|
|
|
LOG(("Association change: SCTP_SHUTDOWN_COMP"));
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-03-05 16:17:07 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_DISCONNECTED,
|
2015-07-10 03:24:11 +00:00
|
|
|
this)));
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
case SCTP_CANT_STR_ASSOC:
|
|
|
|
LOG(("Association change: SCTP_CANT_STR_ASSOC"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(("Association change: UNKNOWN"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LOG(("Association change: streams (in/out) = (%u/%u)",
|
|
|
|
sac->sac_inbound_streams, sac->sac_outbound_streams));
|
|
|
|
|
2012-10-07 05:34:30 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(sac);
|
2012-10-03 23:51:23 +00:00
|
|
|
n = sac->sac_length - sizeof(*sac);
|
|
|
|
if (((sac->sac_state == SCTP_COMM_UP) ||
|
|
|
|
(sac->sac_state == SCTP_RESTART)) && (n > 0)) {
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
switch (sac->sac_info[i]) {
|
|
|
|
case SCTP_ASSOC_SUPPORTS_PR:
|
|
|
|
LOG(("Supports: PR"));
|
|
|
|
break;
|
|
|
|
case SCTP_ASSOC_SUPPORTS_AUTH:
|
|
|
|
LOG(("Supports: AUTH"));
|
|
|
|
break;
|
|
|
|
case SCTP_ASSOC_SUPPORTS_ASCONF:
|
|
|
|
LOG(("Supports: ASCONF"));
|
|
|
|
break;
|
|
|
|
case SCTP_ASSOC_SUPPORTS_MULTIBUF:
|
|
|
|
LOG(("Supports: MULTIBUF"));
|
|
|
|
break;
|
|
|
|
case SCTP_ASSOC_SUPPORTS_RE_CONFIG:
|
|
|
|
LOG(("Supports: RE-CONFIG"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(("Supports: UNKNOWN(0x%02x)", sac->sac_info[i]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (((sac->sac_state == SCTP_COMM_LOST) ||
|
|
|
|
(sac->sac_state == SCTP_CANT_STR_ASSOC)) && (n > 0)) {
|
|
|
|
LOG(("Association: ABORT ="));
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
LOG((" 0x%02x", sac->sac_info[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((sac->sac_state == SCTP_CANT_STR_ASSOC) ||
|
|
|
|
(sac->sac_state == SCTP_SHUTDOWN_COMP) ||
|
|
|
|
(sac->sac_state == SCTP_COMM_LOST)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandlePeerAddressChangeEvent(const struct sctp_paddr_change *spc)
|
|
|
|
{
|
|
|
|
const char *addr = "";
|
2014-07-08 22:44:52 +00:00
|
|
|
#if !defined(__Userspace_os_Windows)
|
|
|
|
char addr_buf[INET6_ADDRSTRLEN];
|
2012-10-03 23:51:23 +00:00
|
|
|
struct sockaddr_in *sin;
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (spc->spc_aaddr.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
#if !defined(__Userspace_os_Windows)
|
2014-07-08 22:44:52 +00:00
|
|
|
sin = (struct sockaddr_in *)&spc->spc_aaddr;
|
2012-10-03 23:51:23 +00:00
|
|
|
addr = inet_ntop(AF_INET, &sin->sin_addr, addr_buf, INET6_ADDRSTRLEN);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
#if !defined(__Userspace_os_Windows)
|
2014-07-08 22:44:52 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
|
2012-10-03 23:51:23 +00:00
|
|
|
addr = inet_ntop(AF_INET6, &sin6->sin6_addr, addr_buf, INET6_ADDRSTRLEN);
|
|
|
|
#endif
|
2014-07-08 22:44:52 +00:00
|
|
|
break;
|
2012-10-03 23:51:23 +00:00
|
|
|
case AF_CONN:
|
|
|
|
addr = "DTLS connection";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LOG(("Peer address %s is now ", addr));
|
|
|
|
switch (spc->spc_state) {
|
|
|
|
case SCTP_ADDR_AVAILABLE:
|
|
|
|
LOG(("SCTP_ADDR_AVAILABLE"));
|
|
|
|
break;
|
|
|
|
case SCTP_ADDR_UNREACHABLE:
|
|
|
|
LOG(("SCTP_ADDR_UNREACHABLE"));
|
|
|
|
break;
|
|
|
|
case SCTP_ADDR_REMOVED:
|
|
|
|
LOG(("SCTP_ADDR_REMOVED"));
|
|
|
|
break;
|
|
|
|
case SCTP_ADDR_ADDED:
|
|
|
|
LOG(("SCTP_ADDR_ADDED"));
|
|
|
|
break;
|
|
|
|
case SCTP_ADDR_MADE_PRIM:
|
|
|
|
LOG(("SCTP_ADDR_MADE_PRIM"));
|
|
|
|
break;
|
|
|
|
case SCTP_ADDR_CONFIRMED:
|
|
|
|
LOG(("SCTP_ADDR_CONFIRMED"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(("UNKNOWN"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LOG((" (error = 0x%08x).\n", spc->spc_error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleRemoteErrorEvent(const struct sctp_remote_error *sre)
|
|
|
|
{
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
n = sre->sre_length - sizeof(struct sctp_remote_error);
|
|
|
|
LOG(("Remote Error (error = 0x%04x): ", sre->sre_error));
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
LOG((" 0x%02x", sre-> sre_data[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleShutdownEvent(const struct sctp_shutdown_event *sse)
|
|
|
|
{
|
|
|
|
LOG(("Shutdown event."));
|
|
|
|
/* XXX: notify all channels. */
|
|
|
|
// Attempts to actually send anything will fail
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleAdaptationIndication(const struct sctp_adaptation_event *sai)
|
|
|
|
{
|
|
|
|
LOG(("Adaptation indication: %x.", sai-> sai_adaptation_ind));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleSendFailedEvent(const struct sctp_send_failed_event *ssfe)
|
|
|
|
{
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
if (ssfe->ssfe_flags & SCTP_DATA_UNSENT) {
|
|
|
|
LOG(("Unsent "));
|
|
|
|
}
|
|
|
|
if (ssfe->ssfe_flags & SCTP_DATA_SENT) {
|
|
|
|
LOG(("Sent "));
|
|
|
|
}
|
|
|
|
if (ssfe->ssfe_flags & ~(SCTP_DATA_SENT | SCTP_DATA_UNSENT)) {
|
|
|
|
LOG(("(flags = %x) ", ssfe->ssfe_flags));
|
|
|
|
}
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("message with PPID = %u, SID = %d, flags: 0x%04x due to error = 0x%08x",
|
2012-10-03 23:51:23 +00:00
|
|
|
ntohl(ssfe->ssfe_info.snd_ppid), ssfe->ssfe_info.snd_sid,
|
|
|
|
ssfe->ssfe_info.snd_flags, ssfe->ssfe_error));
|
|
|
|
n = ssfe->ssfe_length - sizeof(struct sctp_send_failed_event);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
LOG((" 0x%02x", ssfe->ssfe_data[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-02 13:48:17 +00:00
|
|
|
void
|
|
|
|
DataChannelConnection::ClearResets()
|
|
|
|
{
|
|
|
|
// Clear all pending resets
|
|
|
|
if (!mStreamsResetting.IsEmpty()) {
|
|
|
|
LOG(("Clearing resets for %d streams", mStreamsResetting.Length()));
|
|
|
|
}
|
|
|
|
|
2013-07-02 07:20:18 +00:00
|
|
|
for (uint32_t i = 0; i < mStreamsResetting.Length(); ++i) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel;
|
2013-06-02 13:48:17 +00:00
|
|
|
channel = FindChannelByStream(mStreamsResetting[i]);
|
|
|
|
if (channel) {
|
|
|
|
LOG(("Forgetting channel %u (%p) with pending reset",channel->mStream, channel.get()));
|
|
|
|
mStreams[channel->mStream] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mStreamsResetting.Clear();
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
void
|
2013-10-25 20:08:18 +00:00
|
|
|
DataChannelConnection::ResetOutgoingStream(uint16_t stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Connection %p: Resetting outgoing stream %u",
|
2013-10-25 20:08:18 +00:00
|
|
|
(void *) this, stream));
|
2012-10-03 23:51:23 +00:00
|
|
|
// Rarely has more than a couple items and only for a short time
|
|
|
|
for (i = 0; i < mStreamsResetting.Length(); ++i) {
|
2013-10-25 20:08:18 +00:00
|
|
|
if (mStreamsResetting[i] == stream) {
|
2012-10-03 23:51:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 20:08:18 +00:00
|
|
|
mStreamsResetting.AppendElement(stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::SendOutgoingStreamReset()
|
|
|
|
{
|
|
|
|
struct sctp_reset_streams *srs;
|
|
|
|
uint32_t i;
|
|
|
|
size_t len;
|
|
|
|
|
2012-12-14 04:30:11 +00:00
|
|
|
LOG(("Connection %p: Sending outgoing stream reset for %d streams",
|
|
|
|
(void *) this, mStreamsResetting.Length()));
|
2012-10-03 23:51:23 +00:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2012-12-14 04:30:11 +00:00
|
|
|
if (mStreamsResetting.IsEmpty()) {
|
|
|
|
LOG(("No streams to reset"));
|
2012-10-03 23:51:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
len = sizeof(sctp_assoc_t) + (2 + mStreamsResetting.Length()) * sizeof(uint16_t);
|
|
|
|
srs = static_cast<struct sctp_reset_streams *> (moz_xmalloc(len)); // infallible malloc
|
|
|
|
memset(srs, 0, len);
|
|
|
|
srs->srs_flags = SCTP_STREAM_RESET_OUTGOING;
|
|
|
|
srs->srs_number_streams = mStreamsResetting.Length();
|
|
|
|
for (i = 0; i < mStreamsResetting.Length(); ++i) {
|
|
|
|
srs->srs_stream_list[i] = mStreamsResetting[i];
|
|
|
|
}
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_RESET_STREAMS, srs, (socklen_t)len) < 0) {
|
|
|
|
LOG(("***failed: setsockopt RESET, errno %d", errno));
|
2013-06-02 13:48:17 +00:00
|
|
|
// if errno == EALREADY, this is normal - we can't send another reset
|
|
|
|
// with one pending.
|
|
|
|
// When we get an incoming reset (which may be a response to our
|
|
|
|
// outstanding one), see if we have any pending outgoing resets and
|
|
|
|
// send them
|
2012-10-03 23:51:23 +00:00
|
|
|
} else {
|
|
|
|
mStreamsResetting.Clear();
|
|
|
|
}
|
2015-02-19 04:51:06 +00:00
|
|
|
free(srs);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleStreamResetEvent(const struct sctp_stream_reset_event *strrst)
|
|
|
|
{
|
|
|
|
uint32_t n, i;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel; // since we may null out the ref to the channel
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
if (!(strrst->strreset_flags & SCTP_STREAM_RESET_DENIED) &&
|
|
|
|
!(strrst->strreset_flags & SCTP_STREAM_RESET_FAILED)) {
|
|
|
|
n = (strrst->strreset_length - sizeof(struct sctp_stream_reset_event)) / sizeof(uint16_t);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (strrst->strreset_flags & SCTP_STREAM_RESET_INCOMING_SSN) {
|
2013-04-01 01:09:25 +00:00
|
|
|
channel = FindChannelByStream(strrst->strreset_stream_list[i]);
|
2012-11-02 19:28:13 +00:00
|
|
|
if (channel) {
|
2012-12-14 04:30:11 +00:00
|
|
|
// The other side closed the channel
|
|
|
|
// We could be in three states:
|
|
|
|
// 1. Normal state (input and output streams (OPEN)
|
|
|
|
// Notify application, send a RESET in response on our
|
|
|
|
// outbound channel. Go to CLOSED
|
|
|
|
// 2. We sent our own reset (CLOSING); either they crossed on the
|
|
|
|
// wire, or this is a response to our Reset.
|
|
|
|
// Go to CLOSED
|
2014-10-28 15:06:00 +00:00
|
|
|
// 3. We've sent a open but haven't gotten a response yet (CONNECTING)
|
2012-12-14 04:30:11 +00:00
|
|
|
// I believe this is impossible, as we don't have an input stream yet.
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Incoming: Channel %u closed, state %d",
|
|
|
|
channel->mStream, channel->mState));
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(channel->mState == DataChannel::OPEN ||
|
|
|
|
channel->mState == DataChannel::CLOSING ||
|
2014-10-28 15:06:00 +00:00
|
|
|
channel->mState == DataChannel::CONNECTING ||
|
2013-03-27 23:46:50 +00:00
|
|
|
channel->mState == DataChannel::WAITING_TO_OPEN);
|
2013-01-07 03:01:23 +00:00
|
|
|
if (channel->mState == DataChannel::OPEN ||
|
|
|
|
channel->mState == DataChannel::WAITING_TO_OPEN) {
|
2015-01-28 22:05:56 +00:00
|
|
|
// Mark the stream for reset (the reset is sent below)
|
2013-04-01 01:09:25 +00:00
|
|
|
ResetOutgoingStream(channel->mStream);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2015-01-28 22:05:56 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_CLOSED, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams[channel->mStream] = nullptr;
|
2012-12-14 04:30:11 +00:00
|
|
|
|
|
|
|
LOG(("Disconnected DataChannel %p from connection %p",
|
|
|
|
(void *) channel.get(), (void *) channel->mConnection.get()));
|
|
|
|
channel->Destroy();
|
|
|
|
// At this point when we leave here, the object is a zombie held alive only by the DOM object
|
|
|
|
} else {
|
|
|
|
LOG(("Can't find incoming channel %d",i));
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-02 13:48:17 +00:00
|
|
|
|
2015-01-28 22:05:56 +00:00
|
|
|
// Process any pending resets now:
|
2013-06-02 13:48:17 +00:00
|
|
|
if (!mStreamsResetting.IsEmpty()) {
|
|
|
|
LOG(("Sending %d pending resets", mStreamsResetting.Length()));
|
|
|
|
SendOutgoingStreamReset();
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleStreamChangeEvent(const struct sctp_stream_change_event *strchg)
|
|
|
|
{
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t stream;
|
2012-10-03 23:51:23 +00:00
|
|
|
uint32_t i;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
if (strchg->strchange_flags == SCTP_STREAM_CHANGE_DENIED) {
|
|
|
|
LOG(("*** Failed increasing number of streams from %u (%u/%u)",
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams.Length(),
|
2012-10-03 23:51:23 +00:00
|
|
|
strchg->strchange_instrms,
|
|
|
|
strchg->strchange_outstrms));
|
|
|
|
// XXX FIX! notify pending opens of failure
|
|
|
|
return;
|
|
|
|
} else {
|
2013-04-01 01:09:25 +00:00
|
|
|
if (strchg->strchange_instrms > mStreams.Length()) {
|
2014-10-28 15:06:00 +00:00
|
|
|
LOG(("Other side increased streams from %u to %u",
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams.Length(), strchg->strchange_instrms));
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
2014-10-28 15:06:00 +00:00
|
|
|
if (strchg->strchange_outstrms > mStreams.Length() ||
|
|
|
|
strchg->strchange_instrms > mStreams.Length()) {
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t old_len = mStreams.Length();
|
2014-10-28 15:06:00 +00:00
|
|
|
uint16_t new_len = std::max(strchg->strchange_outstrms,
|
|
|
|
strchg->strchange_instrms);
|
2012-10-03 23:51:23 +00:00
|
|
|
LOG(("Increasing number of streams from %u to %u - adding %u (in: %u)",
|
2014-10-28 15:06:00 +00:00
|
|
|
old_len, new_len, new_len - old_len,
|
2012-10-03 23:51:23 +00:00
|
|
|
strchg->strchange_instrms));
|
|
|
|
// make sure both are the same length
|
2014-10-28 15:06:00 +00:00
|
|
|
mStreams.AppendElements(new_len - old_len);
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("New length = %d (was %d)", mStreams.Length(), old_len));
|
2014-10-28 15:06:00 +00:00
|
|
|
for (size_t i = old_len; i < mStreams.Length(); ++i) {
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams[i] = nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
// Re-process any channels waiting for streams.
|
|
|
|
// Linear search, but we don't increase channels often and
|
|
|
|
// the array would only get long in case of an app error normally
|
|
|
|
|
|
|
|
// Make sure we request enough streams if there's a big jump in streams
|
|
|
|
// Could make a more complex API for OpenXxxFinish() and avoid this loop
|
|
|
|
int32_t num_needed = mPending.GetSize();
|
|
|
|
LOG(("%d of %d new streams already needed", num_needed,
|
2014-10-28 15:06:00 +00:00
|
|
|
new_len - old_len));
|
|
|
|
num_needed -= (new_len - old_len); // number we added
|
2012-10-03 23:51:23 +00:00
|
|
|
if (num_needed > 0) {
|
|
|
|
if (num_needed < 16)
|
|
|
|
num_needed = 16;
|
|
|
|
LOG(("Not enough new streams, asking for %d more", num_needed));
|
2013-04-01 01:09:25 +00:00
|
|
|
RequestMoreStreams(num_needed);
|
2014-10-28 15:06:00 +00:00
|
|
|
} else if (strchg->strchange_outstrms < strchg->strchange_instrms) {
|
|
|
|
LOG(("Requesting %d output streams to match partner",
|
|
|
|
strchg->strchange_instrms - strchg->strchange_outstrms));
|
|
|
|
RequestMoreStreams(strchg->strchange_instrms - strchg->strchange_outstrms);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
ProcessQueuedOpens();
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
// else probably not a change in # of streams
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
for (i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
channel = mStreams[i];
|
2012-10-03 23:51:23 +00:00
|
|
|
if (!channel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((channel->mState == CONNECTING) &&
|
2013-04-01 01:09:25 +00:00
|
|
|
(channel->mStream == INVALID_STREAM)) {
|
2012-10-03 23:51:23 +00:00
|
|
|
if ((strchg->strchange_flags & SCTP_STREAM_CHANGE_DENIED) ||
|
|
|
|
(strchg->strchange_flags & SCTP_STREAM_CHANGE_FAILED)) {
|
|
|
|
/* XXX: Signal to the other end. */
|
|
|
|
channel->mState = CLOSED;
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-03-27 23:46:48 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_CLOSED, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2013-03-27 23:46:48 +00:00
|
|
|
// maybe fire onError (bug 843625)
|
2012-10-03 23:51:23 +00:00
|
|
|
} else {
|
2013-04-01 01:09:25 +00:00
|
|
|
stream = FindFreeStream();
|
|
|
|
if (stream != INVALID_STREAM) {
|
|
|
|
channel->mStream = stream;
|
|
|
|
mStreams[stream] = channel;
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_SEND_REQ;
|
|
|
|
/// XXX fix
|
2012-10-03 23:51:23 +00:00
|
|
|
StartDefer();
|
|
|
|
} else {
|
|
|
|
/* We will not find more ... */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Called with mLock locked!
|
|
|
|
void
|
|
|
|
DataChannelConnection::HandleNotification(const union sctp_notification *notif, size_t n)
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
if (notif->sn_header.sn_length != (uint32_t)n) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (notif->sn_header.sn_type) {
|
|
|
|
case SCTP_ASSOC_CHANGE:
|
|
|
|
HandleAssociationChangeEvent(&(notif->sn_assoc_change));
|
|
|
|
break;
|
|
|
|
case SCTP_PEER_ADDR_CHANGE:
|
|
|
|
HandlePeerAddressChangeEvent(&(notif->sn_paddr_change));
|
|
|
|
break;
|
|
|
|
case SCTP_REMOTE_ERROR:
|
|
|
|
HandleRemoteErrorEvent(&(notif->sn_remote_error));
|
|
|
|
break;
|
|
|
|
case SCTP_SHUTDOWN_EVENT:
|
|
|
|
HandleShutdownEvent(&(notif->sn_shutdown_event));
|
|
|
|
break;
|
|
|
|
case SCTP_ADAPTATION_INDICATION:
|
|
|
|
HandleAdaptationIndication(&(notif->sn_adaptation_event));
|
|
|
|
break;
|
|
|
|
case SCTP_PARTIAL_DELIVERY_EVENT:
|
|
|
|
LOG(("SCTP_PARTIAL_DELIVERY_EVENT"));
|
|
|
|
break;
|
|
|
|
case SCTP_AUTHENTICATION_EVENT:
|
|
|
|
LOG(("SCTP_AUTHENTICATION_EVENT"));
|
|
|
|
break;
|
|
|
|
case SCTP_SENDER_DRY_EVENT:
|
|
|
|
//LOG(("SCTP_SENDER_DRY_EVENT"));
|
|
|
|
break;
|
|
|
|
case SCTP_NOTIFICATIONS_STOPPED_EVENT:
|
|
|
|
LOG(("SCTP_NOTIFICATIONS_STOPPED_EVENT"));
|
|
|
|
break;
|
|
|
|
case SCTP_SEND_FAILED_EVENT:
|
|
|
|
HandleSendFailedEvent(&(notif->sn_send_failed_event));
|
|
|
|
break;
|
|
|
|
case SCTP_STREAM_RESET_EVENT:
|
|
|
|
HandleStreamResetEvent(&(notif->sn_strreset_event));
|
|
|
|
break;
|
|
|
|
case SCTP_ASSOC_RESET_EVENT:
|
|
|
|
LOG(("SCTP_ASSOC_RESET_EVENT"));
|
|
|
|
break;
|
|
|
|
case SCTP_STREAM_CHANGE_EVENT:
|
|
|
|
HandleStreamChangeEvent(&(notif->sn_strchange_event));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(("unknown SCTP event: %u", (uint32_t)notif->sn_header.sn_type));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
DataChannelConnection::ReceiveCallback(struct socket* sock, void *data, size_t datalen,
|
|
|
|
struct sctp_rcvinfo rcv, int32_t flags)
|
|
|
|
{
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(!NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
usrsctp_close(sock); // SCTP has finished shutting down
|
|
|
|
} else {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (flags & MSG_NOTIFICATION) {
|
|
|
|
HandleNotification(static_cast<union sctp_notification *>(data), datalen);
|
|
|
|
} else {
|
|
|
|
HandleMessage(data, datalen, ntohl(rcv.rcv_ppid), rcv.rcv_sid);
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 13:53:13 +00:00
|
|
|
// sctp allocates 'data' with malloc(), and expects the receiver to free
|
|
|
|
// it (presumably with free).
|
|
|
|
// XXX future optimization: try to deliver messages without an internal
|
|
|
|
// alloc/copy, and if so delay the free until later.
|
|
|
|
free(data);
|
2012-10-03 23:51:23 +00:00
|
|
|
// usrsctp defines the callback as returning an int, but doesn't use it
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:28:13 +00:00
|
|
|
already_AddRefed<DataChannel>
|
2013-04-01 01:09:26 +00:00
|
|
|
DataChannelConnection::Open(const nsACString& label, const nsACString& protocol,
|
|
|
|
Type type, bool inOrder,
|
2012-10-03 23:51:23 +00:00
|
|
|
uint32_t prValue, DataChannelListener *aListener,
|
2013-04-01 01:09:26 +00:00
|
|
|
nsISupports *aContext, bool aExternalNegotiated,
|
|
|
|
uint16_t aStream)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2013-04-01 01:09:26 +00:00
|
|
|
// aStream == INVALID_STREAM to have the protocol allocate
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t prPolicy = SCTP_PR_SCTP_NONE;
|
|
|
|
uint32_t flags;
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
LOG(("DC Open: label %s/%s, type %u, inorder %d, prValue %u, listener %p, context %p, external: %s, stream %u",
|
|
|
|
PromiseFlatCString(label).get(), PromiseFlatCString(protocol).get(),
|
|
|
|
type, inOrder, prValue, aListener, aContext,
|
|
|
|
aExternalNegotiated ? "true" : "false", aStream));
|
2012-10-03 23:51:23 +00:00
|
|
|
switch (type) {
|
|
|
|
case DATA_CHANNEL_RELIABLE:
|
|
|
|
prPolicy = SCTP_PR_SCTP_NONE;
|
|
|
|
break;
|
|
|
|
case DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT:
|
|
|
|
prPolicy = SCTP_PR_SCTP_RTX;
|
|
|
|
break;
|
|
|
|
case DATA_CHANNEL_PARTIAL_RELIABLE_TIMED:
|
|
|
|
prPolicy = SCTP_PR_SCTP_TTL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((prPolicy == SCTP_PR_SCTP_NONE) && (prValue != 0)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-04-17 19:48:35 +00:00
|
|
|
// Don't look past currently-negotiated streams
|
|
|
|
if (aStream != INVALID_STREAM && aStream < mStreams.Length() && mStreams[aStream]) {
|
2013-04-01 01:09:26 +00:00
|
|
|
LOG(("ERROR: external negotiation of already-open channel %u", aStream));
|
|
|
|
// XXX How do we indicate this up to the application? Probably the
|
|
|
|
// caller's job, but we may need to return an error code.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
flags = !inOrder ? DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED : 0;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel(new DataChannel(this,
|
2013-04-01 01:09:26 +00:00
|
|
|
aStream,
|
2012-11-02 19:28:13 +00:00
|
|
|
DataChannel::CONNECTING,
|
2013-04-01 01:09:26 +00:00
|
|
|
label, protocol,
|
|
|
|
type, prValue,
|
2012-11-02 19:28:13 +00:00
|
|
|
flags,
|
|
|
|
aListener, aContext));
|
2013-04-01 01:09:26 +00:00
|
|
|
if (aExternalNegotiated) {
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_EXTERNAL_NEGOTIATED;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
MutexAutoLock lock(mLock); // OpenFinish assumes this
|
2012-11-02 19:28:13 +00:00
|
|
|
return OpenFinish(channel.forget());
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Separate routine so we can also call it to finish up from pending opens
|
2012-11-02 19:28:13 +00:00
|
|
|
already_AddRefed<DataChannel>
|
2014-03-15 19:00:17 +00:00
|
|
|
DataChannelConnection::OpenFinish(already_AddRefed<DataChannel>&& aChannel)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel(aChannel); // takes the reference passed in
|
2013-04-17 03:58:09 +00:00
|
|
|
// Normally 1 reference if called from ::Open(), or 2 if called from
|
|
|
|
// ProcessQueuedOpens() unless the DOMDataChannel was gc'd
|
2013-04-01 01:09:26 +00:00
|
|
|
uint16_t stream = channel->mStream;
|
2013-04-17 19:48:35 +00:00
|
|
|
bool queue = false;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-17 19:48:35 +00:00
|
|
|
// Cases we care about:
|
|
|
|
// Pre-negotiated:
|
|
|
|
// Not Open:
|
|
|
|
// Doesn't fit:
|
|
|
|
// -> change initial ask or renegotiate after open
|
|
|
|
// -> queue open
|
|
|
|
// Open:
|
|
|
|
// Doesn't fit:
|
|
|
|
// -> RequestMoreStreams && queue
|
|
|
|
// Does fit:
|
|
|
|
// -> open
|
|
|
|
// Not negotiated:
|
|
|
|
// Not Open:
|
|
|
|
// -> queue open
|
|
|
|
// Open:
|
|
|
|
// -> Try to get a stream
|
|
|
|
// Doesn't fit:
|
|
|
|
// -> RequestMoreStreams && queue
|
|
|
|
// Does fit:
|
|
|
|
// -> open
|
|
|
|
// So the Open cases are basically the same
|
|
|
|
// Not Open cases are simply queue for non-negotiated, and
|
|
|
|
// either change the initial ask or possibly renegotiate after open.
|
|
|
|
|
|
|
|
if (mState == OPEN) {
|
|
|
|
if (stream == INVALID_STREAM) {
|
2013-04-01 01:09:26 +00:00
|
|
|
stream = FindFreeStream(); // may be INVALID_STREAM if we need more
|
|
|
|
}
|
2013-04-17 19:48:35 +00:00
|
|
|
if (stream == INVALID_STREAM || stream >= mStreams.Length()) {
|
2013-06-02 13:48:17 +00:00
|
|
|
// RequestMoreStreams() limits to MAX_NUM_STREAMS -- allocate extra streams
|
2013-04-17 19:48:35 +00:00
|
|
|
// to avoid going back immediately for more if the ask to N, N+1, etc
|
|
|
|
int32_t more_needed = (stream == INVALID_STREAM) ? 16 :
|
|
|
|
(stream-((int32_t)mStreams.Length())) + 16;
|
|
|
|
if (!RequestMoreStreams(more_needed)) {
|
|
|
|
// Something bad happened... we're done
|
|
|
|
goto request_error_cleanup;
|
|
|
|
}
|
|
|
|
queue = true;
|
2013-04-01 01:09:26 +00:00
|
|
|
}
|
2013-04-01 01:09:26 +00:00
|
|
|
} else {
|
2013-04-17 19:48:35 +00:00
|
|
|
// not OPEN
|
|
|
|
if (stream != INVALID_STREAM && stream >= mStreams.Length() &&
|
|
|
|
mState == CLOSED) {
|
|
|
|
// Update number of streams for init message
|
|
|
|
struct sctp_initmsg initmsg;
|
|
|
|
socklen_t len = sizeof(initmsg);
|
|
|
|
int32_t total_needed = stream+16;
|
|
|
|
|
|
|
|
memset(&initmsg, 0, sizeof(initmsg));
|
|
|
|
if (usrsctp_getsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, &len) < 0) {
|
|
|
|
LOG(("*** failed getsockopt SCTP_INITMSG"));
|
|
|
|
goto request_error_cleanup;
|
|
|
|
}
|
|
|
|
LOG(("Setting number of SCTP streams to %u, was %u/%u", total_needed,
|
|
|
|
initmsg.sinit_num_ostreams, initmsg.sinit_max_instreams));
|
|
|
|
initmsg.sinit_num_ostreams = total_needed;
|
|
|
|
initmsg.sinit_max_instreams = MAX_NUM_STREAMS;
|
|
|
|
if (usrsctp_setsockopt(mMasterSocket, IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
|
|
|
|
(socklen_t)sizeof(initmsg)) < 0) {
|
|
|
|
LOG(("*** failed setsockopt SCTP_INITMSG, errno %d", errno));
|
|
|
|
goto request_error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t old_len = mStreams.Length();
|
|
|
|
mStreams.AppendElements(total_needed - old_len);
|
|
|
|
for (int32_t i = old_len; i < total_needed; ++i) {
|
|
|
|
mStreams[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else if state is CONNECTING, we'll just re-negotiate when OpenFinish
|
|
|
|
// is called, if needed
|
|
|
|
queue = true;
|
|
|
|
}
|
|
|
|
if (queue) {
|
|
|
|
LOG(("Queuing channel %p (%u) to finish open", channel.get(), stream));
|
|
|
|
// Also serves to mark we told the app
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_FINISH_OPEN;
|
2014-12-25 20:18:38 +00:00
|
|
|
// we need a ref for the nsDeQue and one to return
|
|
|
|
DataChannel* rawChannel = channel;
|
|
|
|
rawChannel->AddRef();
|
|
|
|
mPending.Push(rawChannel);
|
2013-04-17 19:48:35 +00:00
|
|
|
return channel.forget();
|
2013-04-01 01:09:26 +00:00
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-17 19:48:35 +00:00
|
|
|
MOZ_ASSERT(stream != INVALID_STREAM);
|
|
|
|
// just allocated (& OPEN), or externally negotiated
|
|
|
|
mStreams[stream] = channel; // holds a reference
|
|
|
|
channel->mStream = stream;
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
#ifdef TEST_QUEUED_DATA
|
|
|
|
// It's painful to write a test for this...
|
|
|
|
channel->mState = OPEN;
|
|
|
|
channel->mReady = true;
|
2013-04-12 14:08:49 +00:00
|
|
|
SendMsgInternal(channel, "Help me!", 8, DATA_CHANNEL_PPID_DOMSTRING_LAST);
|
2013-04-01 01:09:25 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED) {
|
|
|
|
// Don't send unordered until this gets cleared
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_WAITING_ACK;
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
if (!(channel->mFlags & DATA_CHANNEL_FLAGS_EXTERNAL_NEGOTIATED)) {
|
|
|
|
if (!SendOpenRequestMessage(channel->mLabel, channel->mProtocol,
|
|
|
|
stream,
|
2013-04-01 01:09:26 +00:00
|
|
|
!!(channel->mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED),
|
2013-04-01 01:09:26 +00:00
|
|
|
channel->mPrPolicy, channel->mPrValue)) {
|
|
|
|
LOG(("SendOpenRequest failed, errno = %d", errno));
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_SEND_REQ;
|
|
|
|
StartDefer();
|
|
|
|
|
|
|
|
return channel.forget();
|
|
|
|
} else {
|
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_FINISH_OPEN) {
|
|
|
|
// We already returned the channel to the app.
|
|
|
|
NS_ERROR("Failed to send open request");
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-04-01 01:09:26 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_CLOSED, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2013-04-01 01:09:26 +00:00
|
|
|
}
|
|
|
|
// If we haven't returned the channel yet, it will get destroyed when we exit
|
|
|
|
// this function.
|
|
|
|
mStreams[stream] = nullptr;
|
|
|
|
channel->mStream = INVALID_STREAM;
|
|
|
|
// we'll be destroying the channel
|
|
|
|
channel->mState = CLOSED;
|
|
|
|
return nullptr;
|
2013-03-27 23:46:48 +00:00
|
|
|
}
|
2013-04-01 01:09:26 +00:00
|
|
|
/* NOTREACHED */
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-01 01:09:26 +00:00
|
|
|
// Either externally negotiated or we sent Open
|
|
|
|
channel->mState = OPEN;
|
|
|
|
channel->mReady = true;
|
|
|
|
// FIX? Move into DOMDataChannel? I don't think we can send it yet here
|
|
|
|
LOG(("%s: sending ON_CHANNEL_OPEN for %p", __FUNCTION__, channel.get()));
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-04-01 01:09:26 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_OPEN, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2013-04-01 01:09:26 +00:00
|
|
|
|
2012-11-02 19:28:13 +00:00
|
|
|
return channel.forget();
|
2013-04-17 19:48:35 +00:00
|
|
|
|
|
|
|
request_error_cleanup:
|
|
|
|
channel->mState = CLOSED;
|
|
|
|
if (channel->mFlags & DATA_CHANNEL_FLAGS_FINISH_OPEN) {
|
|
|
|
// We already returned the channel to the app.
|
|
|
|
NS_ERROR("Failed to request more streams");
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2013-04-17 19:48:35 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_CLOSED, this,
|
2015-07-10 03:24:11 +00:00
|
|
|
channel)));
|
2013-04-17 19:48:35 +00:00
|
|
|
return channel.forget();
|
|
|
|
}
|
|
|
|
// we'll be destroying the channel, but it never really got set up
|
|
|
|
// Alternative would be to RUN_ON_THREAD(channel.forget(),::Destroy,...) and
|
|
|
|
// Dispatch it to ourselves
|
|
|
|
return nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
DataChannelConnection::SendMsgInternal(DataChannel *channel, const char *data,
|
2015-09-28 23:02:23 +00:00
|
|
|
size_t length, uint32_t ppid)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
uint16_t flags;
|
|
|
|
struct sctp_sendv_spa spa;
|
|
|
|
int32_t result;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(channel->mState == OPEN || channel->mState == CONNECTING, 0);
|
|
|
|
NS_WARN_IF_FALSE(length > 0, "Length is 0?!");
|
|
|
|
|
2013-10-25 20:08:18 +00:00
|
|
|
// To avoid problems where an in-order OPEN is lost and an
|
2012-10-03 23:51:23 +00:00
|
|
|
// out-of-order data message "beats" it, require data to be in-order
|
|
|
|
// until we get an ACK.
|
2013-10-25 20:08:18 +00:00
|
|
|
if ((channel->mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED) &&
|
|
|
|
!(channel->mFlags & DATA_CHANNEL_FLAGS_WAITING_ACK)) {
|
|
|
|
flags = SCTP_UNORDERED;
|
|
|
|
} else {
|
|
|
|
flags = 0;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
2013-10-25 20:08:18 +00:00
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
spa.sendv_sndinfo.snd_ppid = htonl(ppid);
|
2013-04-01 01:09:25 +00:00
|
|
|
spa.sendv_sndinfo.snd_sid = channel->mStream;
|
2012-10-03 23:51:23 +00:00
|
|
|
spa.sendv_sndinfo.snd_flags = flags;
|
|
|
|
spa.sendv_sndinfo.snd_context = 0;
|
|
|
|
spa.sendv_sndinfo.snd_assoc_id = 0;
|
2013-02-06 16:04:34 +00:00
|
|
|
spa.sendv_flags = SCTP_SEND_SNDINFO_VALID;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-02-06 16:04:34 +00:00
|
|
|
if (channel->mPrPolicy != SCTP_PR_SCTP_NONE) {
|
|
|
|
spa.sendv_prinfo.pr_policy = channel->mPrPolicy;
|
|
|
|
spa.sendv_prinfo.pr_value = channel->mPrValue;
|
|
|
|
spa.sendv_flags |= SCTP_SEND_PRINFO_VALID;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// Note: Main-thread IO, but doesn't block!
|
|
|
|
// XXX FIX! to deal with heavy overruns of JS trying to pass data in
|
|
|
|
// (more than the buffersize) queue data onto another thread to do the
|
|
|
|
// actual sends. See netwerk/protocol/websocket/WebSocketChannel.cpp
|
|
|
|
|
|
|
|
// SCTP will return EMSGSIZE if the message is bigger than the buffer
|
|
|
|
// size (or EAGAIN if there isn't space)
|
|
|
|
if (channel->mBufferedData.IsEmpty()) {
|
|
|
|
result = usrsctp_sendv(mSocket, data, length,
|
|
|
|
nullptr, 0,
|
|
|
|
(void *)&spa, (socklen_t)sizeof(struct sctp_sendv_spa),
|
2013-02-06 16:04:34 +00:00
|
|
|
SCTP_SENDV_SPA, 0);
|
2012-10-03 23:51:23 +00:00
|
|
|
LOG(("Sent buffer (len=%u), result=%d", length, result));
|
|
|
|
} else {
|
|
|
|
// Fake EAGAIN if we're already buffering data
|
|
|
|
result = -1;
|
|
|
|
errno = EAGAIN;
|
|
|
|
}
|
|
|
|
if (result < 0) {
|
2012-11-06 18:18:39 +00:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2012-10-03 23:51:23 +00:00
|
|
|
// queue data for resend! And queue any further data for the stream until it is...
|
|
|
|
BufferedMsg *buffered = new BufferedMsg(spa, data, length); // infallible malloc
|
|
|
|
channel->mBufferedData.AppendElement(buffered); // owned by mBufferedData array
|
|
|
|
channel->mFlags |= DATA_CHANNEL_FLAGS_SEND_DATA;
|
|
|
|
LOG(("Queued %u buffers (len=%u)", channel->mBufferedData.Length(), length));
|
|
|
|
StartDefer();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
LOG(("error %d sending string", errno));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles fragmenting binary messages
|
|
|
|
int32_t
|
|
|
|
DataChannelConnection::SendBinary(DataChannel *channel, const char *data,
|
2015-09-28 23:02:23 +00:00
|
|
|
size_t len,
|
2013-04-12 14:08:49 +00:00
|
|
|
uint32_t ppid_partial, uint32_t ppid_final)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
// Since there's a limit on network buffer size and no limits on message
|
|
|
|
// size, and we don't want to use EOR mode (multiple writes for a
|
|
|
|
// message, but all other streams are blocked until you finish sending
|
|
|
|
// this message), we need to add application-level fragmentation of large
|
|
|
|
// messages. On a reliable channel, these can be simply rebuilt into a
|
|
|
|
// large message. On an unreliable channel, we can't and don't know how
|
|
|
|
// long to wait, and there are no retransmissions, and no easy way to
|
|
|
|
// tell the user "this part is missing", so on unreliable channels we
|
|
|
|
// need to return an error if sending more bytes than the network buffers
|
|
|
|
// can hold, and perhaps a lower number.
|
|
|
|
|
|
|
|
// We *really* don't want to do this from main thread! - and SendMsgInternal
|
|
|
|
// avoids blocking.
|
2013-04-01 01:09:25 +00:00
|
|
|
// This MUST be reliable and in-order for the reassembly to work
|
2012-10-03 23:51:23 +00:00
|
|
|
if (len > DATA_CHANNEL_MAX_BINARY_FRAGMENT &&
|
2013-04-01 01:09:25 +00:00
|
|
|
channel->mPrPolicy == DATA_CHANNEL_RELIABLE &&
|
2013-04-01 01:09:26 +00:00
|
|
|
!(channel->mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED)) {
|
2012-10-03 23:51:23 +00:00
|
|
|
int32_t sent=0;
|
|
|
|
uint32_t origlen = len;
|
|
|
|
LOG(("Sending binary message length %u in chunks", len));
|
|
|
|
// XXX check flags for out-of-order, or force in-order for large binary messages
|
|
|
|
while (len > 0) {
|
2015-09-28 23:02:23 +00:00
|
|
|
size_t sendlen = PR_MIN(len, DATA_CHANNEL_MAX_BINARY_FRAGMENT);
|
2012-10-03 23:51:23 +00:00
|
|
|
uint32_t ppid;
|
|
|
|
len -= sendlen;
|
2013-04-12 14:08:49 +00:00
|
|
|
ppid = len > 0 ? ppid_partial : ppid_final;
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Send chunk of %u bytes, ppid %u", sendlen, ppid));
|
2012-10-03 23:51:23 +00:00
|
|
|
// Note that these might end up being deferred and queued.
|
|
|
|
sent += SendMsgInternal(channel, data, sendlen, ppid);
|
|
|
|
data += sendlen;
|
|
|
|
}
|
2013-02-12 21:38:22 +00:00
|
|
|
LOG(("Sent %d buffers for %u bytes, %d sent immediately, %d buffers queued",
|
2012-10-03 23:51:23 +00:00
|
|
|
(origlen+DATA_CHANNEL_MAX_BINARY_FRAGMENT-1)/DATA_CHANNEL_MAX_BINARY_FRAGMENT,
|
|
|
|
origlen, sent,
|
|
|
|
channel->mBufferedData.Length()));
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
NS_WARN_IF_FALSE(len <= DATA_CHANNEL_MAX_BINARY_FRAGMENT,
|
|
|
|
"Sending too-large data on unreliable channel!");
|
|
|
|
|
2013-04-12 14:08:49 +00:00
|
|
|
// This will fail if the message is too large (default 256K)
|
|
|
|
return SendMsgInternal(channel, data, len, ppid_final);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 07:41:29 +00:00
|
|
|
class ReadBlobRunnable : public nsRunnable {
|
|
|
|
public:
|
|
|
|
ReadBlobRunnable(DataChannelConnection* aConnection, uint16_t aStream,
|
|
|
|
nsIInputStream* aBlob) :
|
|
|
|
mConnection(aConnection),
|
|
|
|
mStream(aStream),
|
|
|
|
mBlob(aBlob)
|
2015-01-11 05:28:34 +00:00
|
|
|
{}
|
2014-01-23 07:41:29 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP Run() {
|
2014-02-01 13:09:00 +00:00
|
|
|
// ReadBlob() is responsible to releasing the reference
|
|
|
|
DataChannelConnection *self = mConnection;
|
|
|
|
self->ReadBlob(mConnection.forget(), mStream, mBlob);
|
2014-01-23 07:41:29 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-02-01 13:09:00 +00:00
|
|
|
// Make sure the Connection doesn't die while there are jobs outstanding.
|
|
|
|
// Let it die (if released by PeerConnectionImpl while we're running)
|
|
|
|
// when we send our runnable back to MainThread. Then ~DataChannelConnection
|
|
|
|
// can send the IOThread to MainThread to die in a runnable, avoiding
|
|
|
|
// unsafe event loop recursion. Evil.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannelConnection> mConnection;
|
2014-01-23 07:41:29 +00:00
|
|
|
uint16_t mStream;
|
|
|
|
// Use RefCount for preventing the object is deleted when SendBlob returns.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsIInputStream> mBlob;
|
2014-01-23 07:41:29 +00:00
|
|
|
};
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
int32_t
|
|
|
|
DataChannelConnection::SendBlob(uint16_t stream, nsIInputStream *aBlob)
|
|
|
|
{
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannel *channel = mStreams[stream];
|
2012-10-03 23:51:23 +00:00
|
|
|
NS_ENSURE_TRUE(channel, 0);
|
|
|
|
// Spawn a thread to send the data
|
2014-01-23 07:41:29 +00:00
|
|
|
if (!mInternalIOThread) {
|
|
|
|
nsresult res = NS_NewThread(getter_AddRefs(mInternalIOThread));
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2015-07-10 03:24:11 +00:00
|
|
|
mInternalIOThread->Dispatch(do_AddRef(new ReadBlobRunnable(this, stream, aBlob)), NS_DISPATCH_NORMAL);
|
2014-01-23 07:41:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2015-01-11 05:28:34 +00:00
|
|
|
class DataChannelBlobSendRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DataChannelBlobSendRunnable(already_AddRefed<DataChannelConnection>& aConnection,
|
|
|
|
uint16_t aStream)
|
|
|
|
: mConnection(aConnection)
|
|
|
|
, mStream(aStream) {}
|
|
|
|
|
|
|
|
~DataChannelBlobSendRunnable()
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread() && mConnection) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
// explicitly leak the connection if destroyed off mainthread
|
|
|
|
unused << mConnection.forget().take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
|
|
|
|
|
|
|
mConnection->SendBinaryMsg(mStream, mData);
|
|
|
|
mConnection = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// explicitly public so we can avoid allocating twice and copying
|
|
|
|
nsCString mData;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Note: we can be destroyed off the target thread, so be careful not to let this
|
|
|
|
// get Released()ed on the temp thread!
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannelConnection> mConnection;
|
2015-01-11 05:28:34 +00:00
|
|
|
uint16_t mStream;
|
|
|
|
};
|
|
|
|
|
2014-02-01 13:09:00 +00:00
|
|
|
void
|
|
|
|
DataChannelConnection::ReadBlob(already_AddRefed<DataChannelConnection> aThis,
|
|
|
|
uint16_t aStream, nsIInputStream* aBlob)
|
2014-01-23 07:41:29 +00:00
|
|
|
{
|
2014-02-01 13:09:00 +00:00
|
|
|
// NOTE: 'aThis' has been forgotten by the caller to avoid releasing
|
|
|
|
// it off mainthread; if PeerConnectionImpl has released then we want
|
|
|
|
// ~DataChannelConnection() to run on MainThread
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
// XXX to do this safely, we must enqueue these atomically onto the
|
2015-01-11 05:28:34 +00:00
|
|
|
// output socket. We need a sender thread(s?) to enqueue data into the
|
2012-10-03 23:51:23 +00:00
|
|
|
// socket and to avoid main-thread IO that might block. Even on a
|
|
|
|
// background thread, we may not want to block on one stream's data.
|
|
|
|
// I.e. run non-blocking and service multiple channels.
|
|
|
|
|
|
|
|
// For now as a hack, send as a single blast of queued packets which may
|
|
|
|
// be deferred until buffer space is available.
|
|
|
|
uint64_t len;
|
2014-03-23 18:52:02 +00:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
|
2015-01-11 05:28:34 +00:00
|
|
|
// Must not let Dispatching it cause the DataChannelConnection to get
|
2015-10-18 05:24:48 +00:00
|
|
|
// released on the wrong thread. Using WrapRunnable(RefPtr<DataChannelConnection>(aThis),...
|
2015-01-11 05:28:34 +00:00
|
|
|
// will occasionally cause aThis to get released on this thread. Also, an explicit Runnable
|
|
|
|
// lets us avoid copying the blob data an extra time.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannelBlobSendRunnable> runnable = new DataChannelBlobSendRunnable(aThis,
|
2015-01-11 05:28:34 +00:00
|
|
|
aStream);
|
|
|
|
// avoid copying the blob data by passing the mData from the runnable
|
2014-03-23 18:52:02 +00:00
|
|
|
if (NS_FAILED(aBlob->Available(&len)) ||
|
2015-01-11 05:28:34 +00:00
|
|
|
NS_FAILED(NS_ReadInputStreamToString(aBlob, runnable->mData, len))) {
|
2014-02-01 13:09:00 +00:00
|
|
|
// Bug 966602: Doesn't return an error to the caller via onerror.
|
2014-03-23 18:52:02 +00:00
|
|
|
// We must release DataChannelConnection on MainThread to avoid issues (bug 876167)
|
2015-01-11 05:28:34 +00:00
|
|
|
// aThis is now owned by the runnable; release it there
|
|
|
|
NS_ProxyRelease(mainThread, runnable);
|
2014-02-01 13:09:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
aBlob->Close();
|
2015-01-11 05:28:34 +00:00
|
|
|
NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 15:41:35 +00:00
|
|
|
void
|
|
|
|
DataChannelConnection::GetStreamIds(std::vector<uint16_t>* aStreamList)
|
|
|
|
{
|
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
if (mStreams[i]) {
|
|
|
|
aStreamList->push_back(mStreams[i]->mStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
int32_t
|
|
|
|
DataChannelConnection::SendMsgCommon(uint16_t stream, const nsACString &aMsg,
|
|
|
|
bool isBinary)
|
|
|
|
{
|
2013-03-27 23:46:50 +00:00
|
|
|
ASSERT_WEBRTC(NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
// We really could allow this from other threads, so long as we deal with
|
|
|
|
// asynchronosity issues with channels closing, in particular access to
|
2013-04-01 01:09:25 +00:00
|
|
|
// mStreams, and issues with the association closing (access to mSocket).
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
const char *data = aMsg.BeginReading();
|
|
|
|
uint32_t len = aMsg.Length();
|
|
|
|
DataChannel *channel;
|
|
|
|
|
2013-03-27 23:46:48 +00:00
|
|
|
LOG(("Sending %sto stream %u: %u bytes", isBinary ? "binary " : "", stream, len));
|
2012-10-03 23:51:23 +00:00
|
|
|
// XXX if we want more efficiency, translate flags once at open time
|
2013-04-01 01:09:25 +00:00
|
|
|
channel = mStreams[stream];
|
2012-10-03 23:51:23 +00:00
|
|
|
NS_ENSURE_TRUE(channel, 0);
|
|
|
|
|
|
|
|
if (isBinary)
|
2013-04-12 14:08:49 +00:00
|
|
|
return SendBinary(channel, data, len,
|
|
|
|
DATA_CHANNEL_PPID_BINARY, DATA_CHANNEL_PPID_BINARY_LAST);
|
|
|
|
return SendBinary(channel, data, len,
|
|
|
|
DATA_CHANNEL_PPID_DOMSTRING, DATA_CHANNEL_PPID_DOMSTRING_LAST);
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-17 10:26:40 +00:00
|
|
|
DataChannelConnection::Close(DataChannel *aChannel)
|
2013-03-27 23:46:48 +00:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
CloseInt(aChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// So we can call Close() with the lock already held
|
|
|
|
// Called from someone who holds a ref via ::Close(), or from ~DataChannel
|
|
|
|
void
|
|
|
|
DataChannelConnection::CloseInt(DataChannel *aChannel)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
2013-01-17 10:26:40 +00:00
|
|
|
MOZ_ASSERT(aChannel);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel(aChannel); // make sure it doesn't go away on us
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-03-27 23:46:48 +00:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Connection %p/Channel %p: Closing stream %u",
|
|
|
|
channel->mConnection.get(), channel.get(), channel->mStream));
|
2013-03-27 23:46:48 +00:00
|
|
|
// re-test since it may have closed before the lock was grabbed
|
|
|
|
if (aChannel->mState == CLOSED || aChannel->mState == CLOSING) {
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Channel already closing/closed (%u)", aChannel->mState));
|
2013-06-02 13:48:17 +00:00
|
|
|
if (mState == CLOSED && channel->mStream != INVALID_STREAM) {
|
|
|
|
// called from CloseAll()
|
|
|
|
// we're not going to hang around waiting any more
|
|
|
|
mStreams[channel->mStream] = nullptr;
|
|
|
|
}
|
2013-01-17 10:26:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-03-27 23:46:48 +00:00
|
|
|
aChannel->mBufferedData.Clear();
|
2013-04-01 01:09:25 +00:00
|
|
|
if (channel->mStream != INVALID_STREAM) {
|
|
|
|
ResetOutgoingStream(channel->mStream);
|
2013-03-25 22:33:32 +00:00
|
|
|
if (mState == CLOSED) { // called from CloseAll()
|
|
|
|
// Let resets accumulate then send all at once in CloseAll()
|
|
|
|
// we're not going to hang around waiting
|
2013-04-01 01:09:25 +00:00
|
|
|
mStreams[channel->mStream] = nullptr;
|
2013-03-25 22:33:32 +00:00
|
|
|
} else {
|
|
|
|
SendOutgoingStreamReset();
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
2013-03-27 23:46:48 +00:00
|
|
|
aChannel->mState = CLOSING;
|
2013-03-25 22:33:32 +00:00
|
|
|
if (mState == CLOSED) {
|
|
|
|
// we're not going to hang around waiting
|
|
|
|
channel->Destroy();
|
|
|
|
}
|
|
|
|
// At this point when we leave here, the object is a zombie held alive only by the DOM object
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataChannelConnection::CloseAll()
|
|
|
|
{
|
2013-06-02 13:48:17 +00:00
|
|
|
LOG(("Closing all channels (connection %p)", (void*) this));
|
2012-10-03 23:51:23 +00:00
|
|
|
// Don't need to lock here
|
|
|
|
|
|
|
|
// Make sure no more channels will be opened
|
2013-04-01 01:09:26 +00:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mState = CLOSED;
|
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// Close current channels
|
2012-12-14 04:30:11 +00:00
|
|
|
// If there are runnables, they hold a strong ref and keep the channel
|
|
|
|
// and/or connection alive (even if in a CLOSED state)
|
2013-03-25 22:33:32 +00:00
|
|
|
bool closed_some = false;
|
2013-04-01 01:09:25 +00:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
if (mStreams[i]) {
|
|
|
|
mStreams[i]->Close();
|
2013-03-25 22:33:32 +00:00
|
|
|
closed_some = true;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up any pending opens for channels
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataChannel> channel;
|
2013-01-07 03:01:23 +00:00
|
|
|
while (nullptr != (channel = dont_AddRef(static_cast<DataChannel *>(mPending.PopFront())))) {
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("closing pending channel %p, stream %u", channel.get(), channel->mStream));
|
2012-11-02 19:28:13 +00:00
|
|
|
channel->Close(); // also releases the ref on each iteration
|
2013-03-25 22:33:32 +00:00
|
|
|
closed_some = true;
|
|
|
|
}
|
|
|
|
// It's more efficient to let the Resets queue in shutdown and then
|
|
|
|
// SendOutgoingStreamReset() here.
|
|
|
|
if (closed_some) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
SendOutgoingStreamReset();
|
2013-01-07 03:01:23 +00:00
|
|
|
}
|
2012-11-02 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataChannel::~DataChannel()
|
2012-12-14 04:30:11 +00:00
|
|
|
{
|
2013-03-27 23:46:48 +00:00
|
|
|
// NS_ASSERTION since this is more "I think I caught all the cases that
|
|
|
|
// can cause this" than a true kill-the-program assertion. If this is
|
|
|
|
// wrong, nothing bad happens. A worst it's a leak.
|
|
|
|
NS_ASSERTION(mState == CLOSED || mState == CLOSING, "unexpected state in ~DataChannel");
|
2013-01-17 10:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannel::Close()
|
|
|
|
{
|
|
|
|
ENSURE_DATACONNECTION;
|
|
|
|
mConnection->Close(this);
|
2012-12-14 04:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used when disconnecting from the DataChannelConnection
|
|
|
|
void
|
|
|
|
DataChannel::Destroy()
|
2012-11-02 19:28:13 +00:00
|
|
|
{
|
2013-01-07 03:01:23 +00:00
|
|
|
ENSURE_DATACONNECTION;
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
LOG(("Destroying Data channel %u", mStream));
|
|
|
|
MOZ_ASSERT_IF(mStream != INVALID_STREAM,
|
|
|
|
!mConnection->FindChannelByStream(mStream));
|
|
|
|
mStream = INVALID_STREAM;
|
2012-12-14 04:30:11 +00:00
|
|
|
mState = CLOSED;
|
|
|
|
mConnection = nullptr;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataChannel::SetListener(DataChannelListener *aListener, nsISupports *aContext)
|
|
|
|
{
|
2013-02-12 21:32:43 +00:00
|
|
|
MutexAutoLock mLock(mListenerLock);
|
2012-10-03 23:51:23 +00:00
|
|
|
mContext = aContext;
|
|
|
|
mListener = aListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
// May be called from another (i.e. Main) thread!
|
|
|
|
void
|
|
|
|
DataChannel::AppReady()
|
|
|
|
{
|
2013-01-07 03:01:23 +00:00
|
|
|
ENSURE_DATACONNECTION;
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
MutexAutoLock lock(mConnection->mLock);
|
|
|
|
|
|
|
|
mReady = true;
|
|
|
|
if (mState == WAITING_TO_OPEN) {
|
|
|
|
mState = OPEN;
|
2015-07-10 03:24:11 +00:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new DataChannelOnMessageAvailable(
|
2012-10-03 23:51:23 +00:00
|
|
|
DataChannelOnMessageAvailable::ON_CHANNEL_OPEN, mConnection,
|
2015-07-10 03:24:11 +00:00
|
|
|
this)));
|
2012-10-03 23:51:23 +00:00
|
|
|
for (uint32_t i = 0; i < mQueuedMessages.Length(); ++i) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = mQueuedMessages[i];
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mQueuedMessages.IsEmpty(), "Shouldn't have queued messages if not WAITING_TO_OPEN");
|
|
|
|
}
|
|
|
|
mQueuedMessages.Clear();
|
|
|
|
mQueuedMessages.Compact();
|
|
|
|
// We never use it again... We could even allocate the array in the odd
|
|
|
|
// cases we need it.
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
DataChannel::GetBufferedAmount()
|
|
|
|
{
|
2015-09-28 23:02:23 +00:00
|
|
|
size_t buffered = 0;
|
|
|
|
for (auto& buffer : mBufferedData) {
|
|
|
|
buffered += buffer->mLength;
|
|
|
|
}
|
|
|
|
// XXX Note: per Michael Tuexen, there's no way to currently get the buffered
|
|
|
|
// amount from the SCTP stack for a single stream. It is on their to-do
|
|
|
|
// list, and once we import a stack with support for that, we'll need to
|
|
|
|
// add it to what we buffer. Also we'll need to ask for notification of a per-
|
|
|
|
// stream buffer-low event and merge that into the handling of buffer-low
|
|
|
|
// (the equivalent to TCP_NOTSENT_LOWAT on TCP sockets)
|
|
|
|
|
|
|
|
if (buffered > UINT32_MAX) { // paranoia - >4GB buffered is very very unlikely
|
|
|
|
buffered = UINT32_MAX;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
return buffered;
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:02:23 +00:00
|
|
|
uint32_t
|
|
|
|
DataChannel::GetBufferedAmountLowThreshold()
|
|
|
|
{
|
|
|
|
return mBufferedThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never fire immediately, as it's defined to fire on transitions, not state
|
|
|
|
void
|
|
|
|
DataChannel::SetBufferedAmountLowThreshold(uint32_t aThreshold)
|
|
|
|
{
|
|
|
|
mBufferedThreshold = aThreshold;
|
|
|
|
}
|
|
|
|
|
2013-01-17 10:26:40 +00:00
|
|
|
// Called with mLock locked!
|
|
|
|
void
|
|
|
|
DataChannel::SendOrQueue(DataChannelOnMessageAvailable *aMessage)
|
|
|
|
{
|
|
|
|
if (!mReady &&
|
|
|
|
(mState == CONNECTING || mState == WAITING_TO_OPEN)) {
|
|
|
|
mQueuedMessages.AppendElement(aMessage);
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(aMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
} // namespace mozilla
|