2015-01-22 03:26:00 +03:00
|
|
|
/* -*- mode: c++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-10-03 00:04:58 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
|
|
|
|
// Some of this code is cut-and-pasted from nICEr. Copyright is:
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright (c) 2007, Adobe Systems, Incorporated
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of Adobe Systems, Network Resonance nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-07-11 07:03:12 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2013-05-21 06:11:01 +04:00
|
|
|
#include "logging.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "nss.h"
|
|
|
|
#include "pk11pub.h"
|
2014-01-11 03:57:15 +04:00
|
|
|
#include "plbase64.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "ScopedNSSTypes.h"
|
2015-01-22 03:26:00 +03:00
|
|
|
#include "runnable_utils.h"
|
2015-05-22 21:11:37 +03:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// nICEr includes
|
|
|
|
extern "C" {
|
|
|
|
#include "nr_api.h"
|
|
|
|
#include "registry.h"
|
|
|
|
#include "async_timer.h"
|
|
|
|
#include "r_crc32.h"
|
2013-04-07 04:58:58 +04:00
|
|
|
#include "r_memory.h"
|
2014-01-23 23:00:32 +04:00
|
|
|
#include "ice_reg.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "ice_util.h"
|
|
|
|
#include "transport_addr.h"
|
|
|
|
#include "nr_crypto.h"
|
|
|
|
#include "nr_socket.h"
|
|
|
|
#include "nr_socket_local.h"
|
2015-01-22 03:26:00 +03:00
|
|
|
#include "nr_proxy_tunnel.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "stun_client_ctx.h"
|
2014-06-23 02:11:48 +04:00
|
|
|
#include "stun_reg.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "stun_server_ctx.h"
|
|
|
|
#include "ice_codeword.h"
|
|
|
|
#include "ice_ctx.h"
|
|
|
|
#include "ice_candidate.h"
|
|
|
|
#include "ice_handler.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local includes
|
|
|
|
#include "nricectx.h"
|
|
|
|
#include "nricemediastream.h"
|
2013-01-05 23:40:34 +04:00
|
|
|
#include "nr_socket_prsock.h"
|
2013-08-28 11:27:14 +04:00
|
|
|
#include "nrinterfaceprioritizer.h"
|
2013-10-29 22:53:44 +04:00
|
|
|
#include "rlogringbuffer.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-05-20 06:16:38 +04:00
|
|
|
TimeStamp nr_socket_short_term_violation_time() {
|
|
|
|
return NrSocketBase::short_term_violation_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp nr_socket_long_term_violation_time() {
|
|
|
|
return NrSocketBase::long_term_violation_time();
|
|
|
|
}
|
|
|
|
|
2012-11-16 05:50:26 +04:00
|
|
|
MOZ_MTLOG_MODULE("mtransport")
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2014-01-27 20:02:51 +04:00
|
|
|
const char kNrIceTransportUdp[] = "udp";
|
|
|
|
const char kNrIceTransportTcp[] = "tcp";
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
// Implement NSPR-based crypto algorithms
|
|
|
|
static int nr_crypto_nss_random_bytes(UCHAR *buf, int len) {
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
if (!slot)
|
|
|
|
return R_INTERNAL;
|
|
|
|
|
|
|
|
SECStatus rv = PK11_GenerateRandomOnSlot(slot, buf, len);
|
|
|
|
if (rv != SECSuccess)
|
|
|
|
return R_INTERNAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_crypto_nss_hmac(UCHAR *key, int keyl, UCHAR *buf, int bufl,
|
|
|
|
UCHAR *result) {
|
|
|
|
CK_MECHANISM_TYPE mech = CKM_SHA_1_HMAC;
|
|
|
|
PK11SlotInfo *slot = 0;
|
|
|
|
MOZ_ASSERT(keyl > 0);
|
|
|
|
SECItem keyi = { siBuffer, key, static_cast<unsigned int>(keyl)};
|
|
|
|
PK11SymKey *skey = 0;
|
|
|
|
PK11Context *hmac_ctx = 0;
|
|
|
|
SECStatus status;
|
|
|
|
unsigned int hmac_len;
|
|
|
|
SECItem param = { siBuffer, nullptr, 0 };
|
|
|
|
int err = R_INTERNAL;
|
|
|
|
|
|
|
|
slot = PK11_GetInternalKeySlot();
|
|
|
|
if (!slot)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
skey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap,
|
|
|
|
CKA_SIGN, &keyi, nullptr);
|
|
|
|
if (!skey)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
|
|
|
|
hmac_ctx = PK11_CreateContextBySymKey(mech, CKA_SIGN,
|
|
|
|
skey, ¶m);
|
2014-04-02 01:30:58 +04:00
|
|
|
if (!hmac_ctx)
|
|
|
|
goto abort;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
status = PK11_DigestBegin(hmac_ctx);
|
|
|
|
if (status != SECSuccess)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
status = PK11_DigestOp(hmac_ctx, buf, bufl);
|
|
|
|
if (status != SECSuccess)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
status = PK11_DigestFinal(hmac_ctx, result, &hmac_len, 20);
|
|
|
|
if (status != SECSuccess)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
MOZ_ASSERT(hmac_len == 20);
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
abort:
|
2012-10-25 20:32:24 +04:00
|
|
|
if(hmac_ctx) PK11_DestroyContext(hmac_ctx, PR_TRUE);
|
2012-10-03 00:04:58 +04:00
|
|
|
if (skey) PK11_FreeSymKey(skey);
|
|
|
|
if (slot) PK11_FreeSlot(slot);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-04-07 04:58:58 +04:00
|
|
|
static int nr_crypto_nss_md5(UCHAR *buf, int bufl, UCHAR *result) {
|
|
|
|
int err = R_INTERNAL;
|
|
|
|
SECStatus rv;
|
|
|
|
|
|
|
|
const SECHashObject *ho = HASH_GetHashObject(HASH_AlgMD5);
|
|
|
|
MOZ_ASSERT(ho);
|
|
|
|
if (!ho)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
MOZ_ASSERT(ho->length == 16);
|
|
|
|
|
|
|
|
rv = HASH_HashBuf(ho->type, result, buf, bufl);
|
|
|
|
if (rv != SECSuccess)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
abort:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
static nr_ice_crypto_vtbl nr_ice_crypto_nss_vtbl = {
|
|
|
|
nr_crypto_nss_random_bytes,
|
2013-04-07 04:58:58 +04:00
|
|
|
nr_crypto_nss_hmac,
|
|
|
|
nr_crypto_nss_md5
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
2014-04-23 12:15:25 +04:00
|
|
|
nsresult NrIceStunServer::ToNicerStunStruct(nr_ice_stun_server *server) const {
|
2013-02-21 20:52:30 +04:00
|
|
|
int r;
|
|
|
|
|
2013-04-07 04:58:58 +04:00
|
|
|
memset(server, 0, sizeof(nr_ice_stun_server));
|
2014-04-23 12:15:25 +04:00
|
|
|
if (transport_ == kNrIceTransportUdp) {
|
|
|
|
server->transport = IPPROTO_UDP;
|
|
|
|
} else if (transport_ == kNrIceTransportTcp) {
|
|
|
|
server->transport = IPPROTO_TCP;
|
2013-12-06 22:20:19 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-04-07 04:58:58 +04:00
|
|
|
|
2013-02-21 20:52:30 +04:00
|
|
|
if (has_addr_) {
|
2013-12-06 22:20:19 +04:00
|
|
|
r = nr_praddr_to_transport_addr(&addr_, &server->u.addr,
|
2014-04-23 12:15:25 +04:00
|
|
|
server->transport, 0);
|
2013-02-21 20:52:30 +04:00
|
|
|
if (r) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
server->type=NR_ICE_STUN_SERVER_TYPE_ADDR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(sizeof(server->u.dnsname.host) > host_.size());
|
|
|
|
PL_strncpyz(server->u.dnsname.host, host_.c_str(),
|
|
|
|
sizeof(server->u.dnsname.host));
|
|
|
|
server->u.dnsname.port = port_;
|
|
|
|
server->type=NR_ICE_STUN_SERVER_TYPE_DNSNAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2013-04-07 04:58:58 +04:00
|
|
|
|
|
|
|
nsresult NrIceTurnServer::ToNicerTurnStruct(nr_ice_turn_server *server) const {
|
|
|
|
memset(server, 0, sizeof(nr_ice_turn_server));
|
|
|
|
|
2014-04-23 12:15:25 +04:00
|
|
|
nsresult rv = ToNicerStunStruct(&server->turn_server);
|
2013-04-07 04:58:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (username_.empty())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
if (password_.empty())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
if (!(server->username=r_strdup(username_.c_str())))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// TODO(ekr@rtfm.com): handle non-ASCII passwords somehow?
|
|
|
|
// STUN requires they be SASLpreped, but we don't know if
|
|
|
|
// they are at this point.
|
|
|
|
|
|
|
|
// C++03 23.2.4, Paragraph 1 stipulates that the elements
|
|
|
|
// in std::vector must be contiguous, and can therefore be
|
|
|
|
// used as input to functions expecting C arrays.
|
|
|
|
int r = r_data_create(&server->password,
|
|
|
|
const_cast<UCHAR *>(&password_[0]),
|
|
|
|
password_.size());
|
|
|
|
if (r) {
|
|
|
|
RFREE(server->username);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Handler callbacks
|
|
|
|
int NrIceCtx::select_pair(void *obj,nr_ice_media_stream *stream,
|
|
|
|
int component_id, nr_ice_cand_pair **potentials,
|
|
|
|
int potential_ct) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_DEBUG, "select pair called: potential_ct = "
|
2013-04-07 04:58:58 +04:00
|
|
|
<< potential_ct);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrIceCtx::stream_ready(void *obj, nr_ice_media_stream *stream) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_DEBUG, "stream_ready called");
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2013-09-19 20:17:52 +04:00
|
|
|
// Get the ICE ctx.
|
2012-10-03 00:04:58 +04:00
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(obj);
|
|
|
|
|
|
|
|
RefPtr<NrIceMediaStream> s = ctx->FindStream(stream);
|
|
|
|
|
|
|
|
// Streams which do not exist should never be ready.
|
|
|
|
MOZ_ASSERT(s);
|
|
|
|
|
|
|
|
s->Ready();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrIceCtx::stream_failed(void *obj, nr_ice_media_stream *stream) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_DEBUG, "stream_failed called");
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Get the ICE ctx
|
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(obj);
|
|
|
|
RefPtr<NrIceMediaStream> s = ctx->FindStream(stream);
|
|
|
|
|
|
|
|
// Streams which do not exist should never fail.
|
|
|
|
MOZ_ASSERT(s);
|
|
|
|
|
2013-11-14 02:53:30 +04:00
|
|
|
ctx->SetConnectionState(ICE_CTX_FAILED);
|
2012-10-03 00:04:58 +04:00
|
|
|
s -> SignalFailed(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-06 22:34:55 +04:00
|
|
|
int NrIceCtx::ice_checking(void *obj, nr_ice_peer_ctx *pctx) {
|
|
|
|
MOZ_MTLOG(ML_DEBUG, "ice_checking called");
|
|
|
|
|
|
|
|
// Get the ICE ctx
|
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(obj);
|
|
|
|
|
|
|
|
ctx->SetConnectionState(ICE_CTX_CHECKING);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
int NrIceCtx::ice_completed(void *obj, nr_ice_peer_ctx *pctx) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_DEBUG, "ice_completed called");
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Get the ICE ctx
|
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(obj);
|
|
|
|
|
2013-12-17 23:57:13 +04:00
|
|
|
// This is called even on failed contexts.
|
|
|
|
if (ctx->connection_state() != ICE_CTX_FAILED) {
|
|
|
|
ctx->SetConnectionState(ICE_CTX_OPEN);
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrIceCtx::msg_recvd(void *obj, nr_ice_peer_ctx *pctx,
|
|
|
|
nr_ice_media_stream *stream, int component_id,
|
|
|
|
UCHAR *msg, int len) {
|
|
|
|
// Get the ICE ctx
|
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(obj);
|
|
|
|
RefPtr<NrIceMediaStream> s = ctx->FindStream(stream);
|
|
|
|
|
|
|
|
// Streams which do not exist should never have packets.
|
|
|
|
MOZ_ASSERT(s);
|
|
|
|
|
|
|
|
s->SignalPacketReceived(s, component_id, msg, len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:17:52 +04:00
|
|
|
void NrIceCtx::trickle_cb(void *arg, nr_ice_ctx *ice_ctx,
|
|
|
|
nr_ice_media_stream *stream,
|
|
|
|
int component_id,
|
|
|
|
nr_ice_candidate *candidate) {
|
|
|
|
// Get the ICE ctx
|
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(arg);
|
|
|
|
RefPtr<NrIceMediaStream> s = ctx->FindStream(stream);
|
|
|
|
|
2014-12-11 02:53:54 +03:00
|
|
|
if (!s) {
|
|
|
|
// This stream has been removed because it is inactive
|
|
|
|
return;
|
|
|
|
}
|
2013-09-19 20:17:52 +04:00
|
|
|
|
|
|
|
// Format the candidate.
|
|
|
|
char candidate_str[NR_ICE_MAX_ATTRIBUTE_SIZE];
|
|
|
|
int r = nr_ice_format_candidate_attribute(candidate, candidate_str,
|
|
|
|
sizeof(candidate_str));
|
|
|
|
MOZ_ASSERT(!r);
|
|
|
|
if (r)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MOZ_MTLOG(ML_INFO, "NrIceCtx(" << ctx->name_ << "): trickling candidate "
|
|
|
|
<< candidate_str);
|
|
|
|
|
|
|
|
s->SignalCandidate(s, candidate_str);
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
RefPtr<NrIceCtx> NrIceCtx::Create(const std::string& name,
|
2013-01-05 23:40:34 +04:00
|
|
|
bool offerer,
|
2014-06-23 02:11:48 +04:00
|
|
|
bool set_interface_priorities,
|
2015-06-19 09:02:33 +03:00
|
|
|
bool allow_loopback,
|
2015-04-22 23:02:50 +03:00
|
|
|
bool tcp_enabled,
|
2015-07-30 17:11:52 +03:00
|
|
|
bool allow_link_local,
|
|
|
|
Policy policy) {
|
2013-10-29 22:53:44 +04:00
|
|
|
|
2015-07-30 17:11:52 +03:00
|
|
|
RefPtr<NrIceCtx> ctx = new NrIceCtx(name, offerer, policy);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2013-10-29 22:53:44 +04:00
|
|
|
// Initialize the crypto callbacks and logging stuff
|
2012-10-03 00:04:58 +04:00
|
|
|
if (!initialized) {
|
|
|
|
NR_reg_init(NR_REG_MODE_LOCAL);
|
2013-10-29 22:53:44 +04:00
|
|
|
RLogRingBuffer::CreateInstance();
|
2012-10-03 00:04:58 +04:00
|
|
|
nr_crypto_vtbl = &nr_ice_crypto_nss_vtbl;
|
|
|
|
initialized = true;
|
|
|
|
|
2013-08-31 18:35:38 +04:00
|
|
|
// Set the priorites for candidate type preferences.
|
|
|
|
// These numbers come from RFC 5245 S. 4.1.2.2
|
2014-01-23 23:00:32 +04:00
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_SRV_RFLX, 100);
|
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_PEER_RFLX, 110);
|
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_HOST, 126);
|
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_RELAYED, 5);
|
2014-04-23 12:15:25 +04:00
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_SRV_RFLX_TCP, 99);
|
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_PEER_RFLX_TCP, 109);
|
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_HOST_TCP, 125);
|
2014-01-23 23:00:32 +04:00
|
|
|
NR_reg_set_uchar((char *)NR_ICE_REG_PREF_TYPE_RELAYED_TCP, 0);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
if (set_interface_priorities) {
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.rl0", 255);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.wi0", 254);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.lo0", 253);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.en1", 252);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.en0", 251);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.eth0", 252);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.eth1", 251);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.eth2", 249);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.ppp", 250);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.ppp0", 249);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.en2", 248);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.en3", 247);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.em0", 251);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.em1", 252);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet0", 240);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet1", 241);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet3", 239);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet4", 238);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet5", 237);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet6", 236);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet7", 235);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet8", 234);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.virbr0", 233);
|
|
|
|
NR_reg_set_uchar((char *)"ice.pref.interface.wlan0", 232);
|
|
|
|
}
|
|
|
|
|
2015-05-22 21:11:37 +03:00
|
|
|
int32_t stun_client_maximum_transmits = 7;
|
|
|
|
int32_t ice_trickle_grace_period = 5000;
|
2014-04-23 12:15:25 +04:00
|
|
|
int32_t ice_tcp_so_sock_count = 3;
|
2014-06-12 18:25:00 +04:00
|
|
|
int32_t ice_tcp_listen_backlog = 10;
|
2015-05-22 21:11:37 +03:00
|
|
|
#ifndef MOZILLA_XPCOMRT_API
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIPrefService> prefs =
|
|
|
|
do_GetService("@mozilla.org/preferences-service;1", &res);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs);
|
|
|
|
if (branch) {
|
|
|
|
branch->GetIntPref(
|
|
|
|
"media.peerconnection.ice.stun_client_maximum_transmits",
|
|
|
|
&stun_client_maximum_transmits);
|
|
|
|
branch->GetIntPref(
|
|
|
|
"media.peerconnection.ice.trickle_grace_period",
|
|
|
|
&ice_trickle_grace_period);
|
2014-04-23 12:15:25 +04:00
|
|
|
branch->GetIntPref(
|
|
|
|
"media.peerconnection.ice.tcp_so_sock_count",
|
|
|
|
&ice_tcp_so_sock_count);
|
2014-06-12 18:25:00 +04:00
|
|
|
branch->GetIntPref(
|
|
|
|
"media.peerconnection.ice.tcp_listen_backlog",
|
|
|
|
&ice_tcp_listen_backlog);
|
2015-05-22 21:11:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
NR_reg_set_uint4((char *)"stun.client.maximum_transmits",
|
|
|
|
stun_client_maximum_transmits);
|
|
|
|
NR_reg_set_uint4((char *)NR_ICE_REG_TRICKLE_GRACE_PERIOD,
|
|
|
|
ice_trickle_grace_period);
|
2014-04-23 12:15:25 +04:00
|
|
|
NR_reg_set_int4((char *)NR_ICE_REG_ICE_TCP_SO_SOCK_COUNT,
|
|
|
|
ice_tcp_so_sock_count);
|
2014-06-12 18:25:00 +04:00
|
|
|
NR_reg_set_int4((char *)NR_ICE_REG_ICE_TCP_SO_SOCK_COUNT,
|
|
|
|
ice_tcp_so_sock_count);
|
|
|
|
NR_reg_set_int4((char *)NR_ICE_REG_ICE_TCP_LISTEN_BACKLOG,
|
|
|
|
ice_tcp_listen_backlog);
|
2014-06-23 02:11:48 +04:00
|
|
|
|
2015-06-19 09:02:33 +03:00
|
|
|
NR_reg_set_char((char *)NR_ICE_REG_ICE_TCP_DISABLE, !tcp_enabled);
|
|
|
|
|
2014-06-23 02:11:48 +04:00
|
|
|
if (allow_loopback) {
|
|
|
|
NR_reg_set_char((char *)NR_STUN_REG_PREF_ALLOW_LOOPBACK_ADDRS, 1);
|
|
|
|
}
|
2015-04-22 23:02:50 +03:00
|
|
|
|
|
|
|
if (allow_link_local) {
|
|
|
|
NR_reg_set_char((char *)NR_STUN_REG_PREF_ALLOW_LINK_LOCAL_ADDRS, 1);
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the ICE context
|
|
|
|
int r;
|
|
|
|
|
|
|
|
UINT4 flags = offerer ? NR_ICE_CTX_FLAGS_OFFERER:
|
|
|
|
NR_ICE_CTX_FLAGS_ANSWERER;
|
|
|
|
flags |= NR_ICE_CTX_FLAGS_AGGRESSIVE_NOMINATION;
|
|
|
|
|
|
|
|
r = nr_ice_ctx_create(const_cast<char *>(name.c_str()), flags,
|
|
|
|
&ctx->ctx_);
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't create ICE ctx for '" << name << "'");
|
2012-10-03 00:04:58 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-28 11:27:14 +04:00
|
|
|
#ifdef USE_INTERFACE_PRIORITIZER
|
|
|
|
nr_interface_prioritizer *prioritizer = CreateInterfacePrioritizer();
|
|
|
|
if (!prioritizer) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_MTLOG(LogLevel::Error, "Couldn't create interface prioritizer.");
|
2013-08-28 11:27:14 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = nr_ice_ctx_set_interface_prioritizer(ctx->ctx_, prioritizer);
|
|
|
|
if (r) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_MTLOG(LogLevel::Error, "Couldn't set interface prioritizer.");
|
2013-08-28 11:27:14 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif // USE_INTERFACE_PRIORITIZER
|
|
|
|
|
2013-09-19 20:17:52 +04:00
|
|
|
if (ctx->generating_trickle()) {
|
|
|
|
r = nr_ice_ctx_set_trickle_cb(ctx->ctx_, &NrIceCtx::trickle_cb, ctx);
|
|
|
|
if (r) {
|
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set trickle cb for '" << name << "'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Create the handler objects
|
|
|
|
ctx->ice_handler_vtbl_ = new nr_ice_handler_vtbl();
|
|
|
|
ctx->ice_handler_vtbl_->select_pair = &NrIceCtx::select_pair;
|
|
|
|
ctx->ice_handler_vtbl_->stream_ready = &NrIceCtx::stream_ready;
|
|
|
|
ctx->ice_handler_vtbl_->stream_failed = &NrIceCtx::stream_failed;
|
|
|
|
ctx->ice_handler_vtbl_->ice_completed = &NrIceCtx::ice_completed;
|
|
|
|
ctx->ice_handler_vtbl_->msg_recvd = &NrIceCtx::msg_recvd;
|
2014-08-06 22:34:55 +04:00
|
|
|
ctx->ice_handler_vtbl_->ice_checking = &NrIceCtx::ice_checking;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
ctx->ice_handler_ = new nr_ice_handler();
|
|
|
|
ctx->ice_handler_->vtbl = ctx->ice_handler_vtbl_;
|
|
|
|
ctx->ice_handler_->obj = ctx;
|
|
|
|
|
|
|
|
// Create the peer ctx. Because we do not support parallel forking, we
|
|
|
|
// only have one peer ctx.
|
|
|
|
std::string peer_name = name + ":default";
|
|
|
|
r = nr_ice_peer_ctx_create(ctx->ctx_, ctx->ice_handler_,
|
|
|
|
const_cast<char *>(peer_name.c_str()),
|
|
|
|
&ctx->peer_);
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't create ICE peer ctx for '" << name << "'");
|
2012-10-03 00:04:58 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
ctx->sts_target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
if (!NS_SUCCEEDED(rv))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2014-06-23 02:11:48 +04:00
|
|
|
// ONLY USE THIS FOR TESTING. Will cause totally unpredictable and possibly very
|
|
|
|
// bad effects if ICE is still live.
|
|
|
|
void NrIceCtx::internal_DeinitializeGlobal() {
|
|
|
|
NR_reg_del((char *)"stun");
|
|
|
|
NR_reg_del((char *)"ice");
|
|
|
|
RLogRingBuffer::DestroyInstance();
|
|
|
|
nr_crypto_vtbl = nullptr;
|
|
|
|
initialized = false;
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
NrIceCtx::~NrIceCtx() {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_DEBUG, "Destroying ICE ctx '" << name_ <<"'");
|
2012-10-03 00:04:58 +04:00
|
|
|
nr_ice_peer_ctx_destroy(&peer_);
|
|
|
|
nr_ice_ctx_destroy(&ctx_);
|
|
|
|
delete ice_handler_vtbl_;
|
|
|
|
delete ice_handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<NrIceMediaStream>
|
|
|
|
NrIceCtx::CreateStream(const std::string& name, int components) {
|
2015-03-24 02:56:08 +03:00
|
|
|
return NrIceMediaStream::Create(this, name, components);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NrIceCtx::SetStream(size_t index, NrIceMediaStream* stream) {
|
|
|
|
if (index >= streams_.size()) {
|
|
|
|
streams_.resize(index + 1);
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2015-03-31 01:22:28 +03:00
|
|
|
RefPtr<NrIceMediaStream> oldStream(streams_[index]);
|
2015-03-24 02:56:08 +03:00
|
|
|
streams_[index] = stream;
|
2015-03-31 01:22:28 +03:00
|
|
|
|
|
|
|
if (oldStream) {
|
|
|
|
oldStream->Close();
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
2014-11-20 03:16:29 +03:00
|
|
|
std::string NrIceCtx::ufrag() const {
|
|
|
|
return ctx_->ufrag;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NrIceCtx::pwd() const {
|
|
|
|
return ctx_->pwd;
|
|
|
|
}
|
|
|
|
|
2012-11-20 18:12:52 +04:00
|
|
|
void NrIceCtx::destroy_peer_ctx() {
|
|
|
|
nr_ice_peer_ctx_destroy(&peer_);
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2012-12-03 01:49:56 +04:00
|
|
|
nsresult NrIceCtx::SetControlling(Controlling controlling) {
|
|
|
|
peer_->controlling = (controlling == ICE_CONTROLLING)? 1 : 0;
|
|
|
|
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_DEBUG, "ICE ctx " << name_ << " setting controlling to" <<
|
2012-12-03 01:49:56 +04:00
|
|
|
controlling);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-16 10:32:00 +04:00
|
|
|
NrIceCtx::Controlling NrIceCtx::GetControlling() {
|
|
|
|
return (peer_->controlling) ? ICE_CONTROLLING : ICE_CONTROLLED;
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:11:52 +03:00
|
|
|
nsresult NrIceCtx::SetPolicy(Policy policy) {
|
|
|
|
policy_ = policy;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-05 23:40:34 +04:00
|
|
|
nsresult NrIceCtx::SetStunServers(const std::vector<NrIceStunServer>&
|
|
|
|
stun_servers) {
|
|
|
|
if (stun_servers.empty())
|
|
|
|
return NS_OK;
|
|
|
|
|
2014-07-11 07:03:12 +04:00
|
|
|
auto servers = MakeUnique<nr_ice_stun_server[]>(stun_servers.size());
|
2013-01-05 23:40:34 +04:00
|
|
|
|
|
|
|
for (size_t i=0; i < stun_servers.size(); ++i) {
|
2013-04-07 04:58:58 +04:00
|
|
|
nsresult rv = stun_servers[i].ToNicerStunStruct(&servers[i]);
|
2013-02-21 20:52:30 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set STUN server for '" << name_ << "'");
|
2013-01-05 23:40:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 07:03:12 +04:00
|
|
|
int r = nr_ice_ctx_set_stun_servers(ctx_, servers.get(), stun_servers.size());
|
2013-01-05 23:40:34 +04:00
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set STUN server for '" << name_ << "'");
|
2013-01-05 23:40:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-07 04:58:58 +04:00
|
|
|
// TODO(ekr@rtfm.com): This is just SetStunServers with s/Stun/Turn
|
|
|
|
// Could we do a template or something?
|
|
|
|
nsresult NrIceCtx::SetTurnServers(const std::vector<NrIceTurnServer>&
|
|
|
|
turn_servers) {
|
|
|
|
if (turn_servers.empty())
|
|
|
|
return NS_OK;
|
|
|
|
|
2014-07-11 07:03:12 +04:00
|
|
|
auto servers = MakeUnique<nr_ice_turn_server[]>(turn_servers.size());
|
2013-04-07 04:58:58 +04:00
|
|
|
|
|
|
|
for (size_t i=0; i < turn_servers.size(); ++i) {
|
|
|
|
nsresult rv = turn_servers[i].ToNicerTurnStruct(&servers[i]);
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set TURN server for '" << name_ << "'");
|
2013-04-07 04:58:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 07:03:12 +04:00
|
|
|
int r = nr_ice_ctx_set_turn_servers(ctx_, servers.get(), turn_servers.size());
|
2013-04-07 04:58:58 +04:00
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set TURN server for '" << name_ << "'");
|
2013-04-07 04:58:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ekr@rtfm.com): This leaks the username/password. Need to free that.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-21 20:52:30 +04:00
|
|
|
nsresult NrIceCtx::SetResolver(nr_resolver *resolver) {
|
|
|
|
int r = nr_ice_ctx_set_resolver(ctx_, resolver);
|
|
|
|
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set resolver for '" << name_ << "'");
|
2013-02-21 20:52:30 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:26:00 +03:00
|
|
|
nsresult NrIceCtx::SetProxyServer(const NrIceProxyServer& proxy_server) {
|
|
|
|
int r,_status;
|
|
|
|
nr_proxy_tunnel_config *config = nullptr;
|
|
|
|
nr_socket_wrapper_factory *wrapper = nullptr;
|
|
|
|
|
|
|
|
if ((r = nr_proxy_tunnel_config_create(&config))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r = nr_proxy_tunnel_config_set_proxy(config,
|
|
|
|
proxy_server.host().c_str(),
|
|
|
|
proxy_server.port()))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r = nr_proxy_tunnel_config_set_resolver(config, ctx_->resolver))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r = nr_socket_wrapper_factory_proxy_tunnel_create(config, &wrapper))) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_MTLOG(LogLevel::Error, "Couldn't create proxy tunnel wrapper.");
|
2015-01-22 03:26:00 +03:00
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nr_ice_ctx will own the wrapper after this call
|
|
|
|
if ((r = nr_ice_ctx_set_turn_tcp_socket_wrapper(ctx_, wrapper))) {
|
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't set proxy for '" << name_ << "': " << r);
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
nr_proxy_tunnel_config_destroy(&config);
|
|
|
|
if (_status) {
|
|
|
|
nr_socket_wrapper_factory_destroy(&wrapper);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
nsresult NrIceCtx::StartGathering() {
|
2015-01-22 03:26:00 +03:00
|
|
|
ASSERT_ON_THREAD(sts_target_);
|
2015-03-31 02:32:51 +03:00
|
|
|
SetGatheringState(ICE_CTX_GATHER_STARTED);
|
2014-12-11 02:53:54 +03:00
|
|
|
// This might start gathering for the first time, or again after
|
|
|
|
// renegotiation, or might do nothing at all if gathering has already
|
|
|
|
// finished.
|
|
|
|
int r = nr_ice_gather(ctx_, &NrIceCtx::gather_cb, this);
|
|
|
|
|
2015-03-31 02:32:51 +03:00
|
|
|
if (!r) {
|
|
|
|
SetGatheringState(ICE_CTX_GATHER_COMPLETE);
|
|
|
|
} else if (r != R_WOULDBLOCK) {
|
2014-12-11 02:53:54 +03:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't gather ICE candidates for '"
|
|
|
|
<< name_ << "', error=" << r);
|
2013-11-14 02:53:30 +04:00
|
|
|
SetConnectionState(ICE_CTX_FAILED);
|
2013-03-04 11:10:30 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<NrIceMediaStream> NrIceCtx::FindStream(
|
|
|
|
nr_ice_media_stream *stream) {
|
|
|
|
for (size_t i=0; i<streams_.size(); ++i) {
|
2014-12-11 02:53:54 +03:00
|
|
|
if (streams_[i] && (streams_[i]->stream() == stream)) {
|
2012-10-03 00:04:58 +04:00
|
|
|
return streams_[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> NrIceCtx::GetGlobalAttributes() {
|
|
|
|
char **attrs = 0;
|
|
|
|
int attrct;
|
|
|
|
int r;
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
|
|
|
r = nr_ice_get_global_attributes(ctx_, &attrs, &attrct);
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't get ufrag and password for '"
|
|
|
|
<< name_ << "'");
|
2012-10-03 00:04:58 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<attrct; i++) {
|
|
|
|
ret.push_back(std::string(attrs[i]));
|
|
|
|
RFREE(attrs[i]);
|
|
|
|
}
|
|
|
|
RFREE(attrs);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NrIceCtx::ParseGlobalAttributes(std::vector<std::string> attrs) {
|
|
|
|
std::vector<char *> attrs_in;
|
|
|
|
|
|
|
|
for (size_t i=0; i<attrs.size(); ++i) {
|
|
|
|
attrs_in.push_back(const_cast<char *>(attrs[i].c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = nr_ice_peer_ctx_parse_global_attributes(peer_,
|
|
|
|
attrs_in.size() ?
|
|
|
|
&attrs_in[0] : nullptr,
|
|
|
|
attrs_in.size());
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't parse global attributes for "
|
|
|
|
<< name_ << "'");
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NrIceCtx::StartChecks() {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r=nr_ice_peer_ctx_pair_candidates(peer_);
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't pair candidates on "
|
|
|
|
<< name_ << "'");
|
2013-11-14 02:53:30 +04:00
|
|
|
SetConnectionState(ICE_CTX_FAILED);
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = nr_ice_peer_ctx_start_checks2(peer_,1);
|
|
|
|
if (r) {
|
|
|
|
if (r == R_NOT_FOUND) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't start peer checks on "
|
|
|
|
<< name_ << "' assuming trickle ICE");
|
2012-10-03 00:04:58 +04:00
|
|
|
} else {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't start peer checks on "
|
|
|
|
<< name_ << "'");
|
2013-11-14 02:53:30 +04:00
|
|
|
SetConnectionState(ICE_CTX_FAILED);
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-11 02:53:54 +03:00
|
|
|
void NrIceCtx::gather_cb(NR_SOCKET s, int h, void *arg) {
|
2012-10-03 00:04:58 +04:00
|
|
|
NrIceCtx *ctx = static_cast<NrIceCtx *>(arg);
|
|
|
|
|
2013-11-14 02:53:30 +04:00
|
|
|
ctx->SetGatheringState(ICE_CTX_GATHER_COMPLETE);
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NrIceCtx::Finalize() {
|
|
|
|
int r = nr_ice_ctx_finalize(ctx_, peer_);
|
|
|
|
|
|
|
|
if (r) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't finalize "
|
2012-10-03 00:04:58 +04:00
|
|
|
<< name_ << "'");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-14 02:53:30 +04:00
|
|
|
void NrIceCtx::SetConnectionState(ConnectionState state) {
|
|
|
|
if (state == connection_state_)
|
2013-03-04 11:10:30 +04:00
|
|
|
return;
|
|
|
|
|
2013-11-20 04:06:08 +04:00
|
|
|
MOZ_MTLOG(ML_INFO, "NrIceCtx(" << name_ << "): state " <<
|
2013-11-14 02:53:30 +04:00
|
|
|
connection_state_ << "->" << state);
|
|
|
|
connection_state_ = state;
|
|
|
|
|
2014-06-13 04:22:00 +04:00
|
|
|
if (connection_state_ == ICE_CTX_FAILED) {
|
|
|
|
MOZ_MTLOG(ML_INFO, "NrIceCtx(" << name_ << "): dumping r_log ringbuffer... ");
|
|
|
|
std::deque<std::string> logs;
|
|
|
|
RLogRingBuffer::GetInstance()->GetAny(0, &logs);
|
|
|
|
for (auto l = logs.begin(); l != logs.end(); ++l) {
|
|
|
|
MOZ_MTLOG(ML_INFO, *l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 02:53:30 +04:00
|
|
|
SignalConnectionStateChange(this, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrIceCtx::SetGatheringState(GatheringState state) {
|
|
|
|
if (state == gathering_state_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MOZ_MTLOG(ML_DEBUG, "NrIceCtx(" << name_ << "): gathering state " <<
|
|
|
|
gathering_state_ << "->" << state);
|
|
|
|
gathering_state_ = state;
|
|
|
|
|
|
|
|
SignalGatheringStateChange(this, state);
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
} // close namespace
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Reimplement nr_ice_compute_codeword to avoid copyright issues
|
|
|
|
void nr_ice_compute_codeword(char *buf, int len,char *codeword) {
|
|
|
|
UINT4 c;
|
|
|
|
|
|
|
|
r_crc32(buf,len,&c);
|
|
|
|
|
2014-01-11 03:57:15 +04:00
|
|
|
PL_Base64Encode(reinterpret_cast<char*>(&c), 3, codeword);
|
|
|
|
codeword[4] = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|