提交 10bb33ec authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

C++ and classes cleanup.

Add IceServer::ForceSendingTuple() for bad clients (Firefox).
上级 ca4cf925
...@@ -17,6 +17,16 @@ namespace RTC ...@@ -17,6 +17,16 @@ namespace RTC
class DTLSTransport : class DTLSTransport :
public Timer::Listener public Timer::Listener
{ {
public:
enum class DtlsState
{
NEW = 1,
CONNECTING,
CONNECTED,
FAILED,
CLOSED
};
public: public:
enum class Role enum class Role
{ {
...@@ -63,15 +73,12 @@ namespace RTC ...@@ -63,15 +73,12 @@ namespace RTC
// and remote fingerprint verification). Outgoing media can now flow through. // and remote fingerprint verification). Outgoing media can now flow through.
// NOTE: The caller MUST NOT call any method during this callback. // NOTE: The caller MUST NOT call any method during this callback.
virtual void onDTLSConnected(DTLSTransport* dtlsTransport, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) = 0; virtual void onDTLSConnected(DTLSTransport* dtlsTransport, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) = 0;
// The DTLS connection has been closed due to receipt of a close_notify alert.
// NOTE: The caller MUST NOT call Close() during this callback.
virtual void onDTLSDisconnected(DTLSTransport* dtlsTransport) = 0;
// The DTLS connection has been closed as the result of an error (such as a // The DTLS connection has been closed as the result of an error (such as a
// DTLS alert or a failure to validate the remote fingerprint). // DTLS alert or a failure to validate the remote fingerprint).
// NOTE: The caller MUST NOT call Close() during this callback. // NOTE: The caller MUST NOT call Close() during this callback.
virtual void onDTLSFailed(DTLSTransport* dtlsTransport) = 0; virtual void onDTLSFailed(DTLSTransport* dtlsTransport) = 0;
// The DTLS connection has been closed intentionally via Close(). // The DTLS connection has been closed due to receipt of a close_notify alert.
// NOTE: The caller MUST NOT call any method during this callback. // NOTE: The caller MUST NOT call Close() during this callback.
virtual void onDTLSClosed(DTLSTransport* dtlsTransport) = 0; virtual void onDTLSClosed(DTLSTransport* dtlsTransport) = 0;
// Need to send DTLS data to the peer. // Need to send DTLS data to the peer.
// NOTE: The caller MUST NOT call Close() during this callback. // NOTE: The caller MUST NOT call Close() during this callback.
...@@ -114,10 +121,12 @@ namespace RTC ...@@ -114,10 +121,12 @@ namespace RTC
void Run(Role localRole); void Run(Role localRole);
void SetRemoteFingerprint(Fingerprint fingerprint); void SetRemoteFingerprint(Fingerprint fingerprint);
void ProcessDTLSData(const MS_BYTE* data, size_t len); void ProcessDTLSData(const MS_BYTE* data, size_t len);
DtlsState GetState();
Role GetLocalRole(); Role GetLocalRole();
void SendApplicationData(const MS_BYTE* data, size_t len); void SendApplicationData(const MS_BYTE* data, size_t len);
private: private:
bool IsRunning();
void Reset(); void Reset();
bool CheckStatus(int return_code); bool CheckStatus(int return_code);
void SendPendingOutgoingDTLSData(); void SendPendingOutgoingDTLSData();
...@@ -144,12 +153,11 @@ namespace RTC ...@@ -144,12 +153,11 @@ namespace RTC
BIO* sslBioToNetwork = nullptr; // The BIO in which ssl writes. BIO* sslBioToNetwork = nullptr; // The BIO in which ssl writes.
Timer* timer = nullptr; Timer* timer = nullptr;
// Others. // Others.
DtlsState state = DtlsState::NEW;
Role localRole = Role::NONE; Role localRole = Role::NONE;
Fingerprint remoteFingerprint = { FingerprintAlgorithm::NONE, "" }; Fingerprint remoteFingerprint = { FingerprintAlgorithm::NONE, "" };
bool running = false;
bool handshakeDone = false; bool handshakeDone = false;
bool handshakeDoneNow = false; bool handshakeDoneNow = false;
bool connected = false;
}; };
/* Inline static methods. */ /* Inline static methods. */
...@@ -195,6 +203,28 @@ namespace RTC ...@@ -195,6 +203,28 @@ namespace RTC
/* Inline instance methods. */ /* Inline instance methods. */
inline
bool DTLSTransport::IsRunning()
{
switch (this->state)
{
case DtlsState::NEW:
return false;
case DtlsState::CONNECTING:
case DtlsState::CONNECTED:
return true;
case DtlsState::FAILED:
case DtlsState::CLOSED:
return false;
}
}
inline
DTLSTransport::DtlsState DTLSTransport::GetState()
{
return this->state;
}
inline inline
DTLSTransport::Role DTLSTransport::GetLocalRole() DTLSTransport::Role DTLSTransport::GetLocalRole()
{ {
......
...@@ -11,6 +11,13 @@ namespace RTC ...@@ -11,6 +11,13 @@ namespace RTC
{ {
class IceServer class IceServer
{ {
public:
enum class IceComponent
{
RTP = 1,
RTCP = 2
};
public: public:
enum class IceState enum class IceState
{ {
...@@ -34,14 +41,18 @@ namespace RTC ...@@ -34,14 +41,18 @@ namespace RTC
}; };
public: public:
IceServer(Listener* listener, const std::string& usernameFragment, const std::string password); IceServer(Listener* listener, IceServer::IceComponent iceComponent, const std::string& usernameFragment, const std::string password);
void Close(); void Close();
void ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportTuple* tuple); void ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportTuple* tuple);
IceComponent GetComponent();
std::string& GetUsernameFragment(); std::string& GetUsernameFragment();
std::string& GetPassword(); std::string& GetPassword();
IceState GetState(); IceState GetState();
bool IsValidTuple(RTC::TransportTuple* tuple); bool IsValidTuple(RTC::TransportTuple* tuple);
// This should be just called in 'connected' or completed' state
// and the given tuple must be an already valid tuple.
void ForceSelectedTuple(RTC::TransportTuple* tuple);
private: private:
void HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate); void HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate);
...@@ -63,6 +74,7 @@ namespace RTC ...@@ -63,6 +74,7 @@ namespace RTC
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
// Others. // Others.
IceComponent iceComponent;
std::string usernameFragment; std::string usernameFragment;
std::string password; std::string password;
IceState state = IceState::NEW; IceState state = IceState::NEW;
...@@ -72,6 +84,12 @@ namespace RTC ...@@ -72,6 +84,12 @@ namespace RTC
/* Inline methods. */ /* Inline methods. */
inline
IceServer::IceComponent IceServer::GetComponent()
{
return this->iceComponent;
}
inline inline
std::string& IceServer::GetUsernameFragment() std::string& IceServer::GetUsernameFragment()
{ {
......
...@@ -24,23 +24,6 @@ namespace RTC ...@@ -24,23 +24,6 @@ namespace RTC
public RTC::IceServer::Listener, public RTC::IceServer::Listener,
public RTC::DTLSTransport::Listener public RTC::DTLSTransport::Listener
{ {
public:
enum class IceComponent
{
RTP = 1,
RTCP = 2
};
private:
enum class DtlsTransportState
{
NEW = 1,
CONNECTING,
CONNECTED,
CLOSED,
FAILED
};
public: public:
class Listener class Listener
{ {
...@@ -63,7 +46,7 @@ namespace RTC ...@@ -63,7 +46,7 @@ namespace RTC
Transport* CreateAssociatedTransport(unsigned int transportId); Transport* CreateAssociatedTransport(unsigned int transportId);
private: private:
void Terminate(); void ClosePorts();
bool IsAlive(); bool IsAlive();
void MayRunDTLSTransport(); void MayRunDTLSTransport();
...@@ -103,7 +86,6 @@ namespace RTC ...@@ -103,7 +86,6 @@ namespace RTC
public: public:
virtual void onDTLSConnecting(DTLSTransport* dtlsTransport) override; virtual void onDTLSConnecting(DTLSTransport* dtlsTransport) override;
virtual void onDTLSConnected(DTLSTransport* dtlsTransport, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) override; virtual void onDTLSConnected(DTLSTransport* dtlsTransport, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) override;
virtual void onDTLSDisconnected(DTLSTransport* dtlsTransport) override;
virtual void onDTLSFailed(DTLSTransport* dtlsTransport) override; virtual void onDTLSFailed(DTLSTransport* dtlsTransport) override;
virtual void onDTLSClosed(DTLSTransport* dtlsTransport) override; virtual void onDTLSClosed(DTLSTransport* dtlsTransport) override;
virtual void onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) override; virtual void onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) override;
...@@ -132,11 +114,9 @@ namespace RTC ...@@ -132,11 +114,9 @@ namespace RTC
// Others. // Others.
bool allocated = false; bool allocated = false;
// Others (ICE). // Others (ICE).
RTC::Transport::IceComponent iceComponent;
std::vector<IceCandidate> iceLocalCandidates; std::vector<IceCandidate> iceLocalCandidates;
RTC::TransportTuple* selectedTuple = nullptr; RTC::TransportTuple* selectedTuple = nullptr;
// Others (DTLS). // Others (DTLS).
DtlsTransportState dtlsState = DtlsTransportState::NEW;
bool remoteDtlsParametersGiven = false; bool remoteDtlsParametersGiven = false;
RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO; RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO;
}; };
...@@ -146,8 +126,8 @@ namespace RTC ...@@ -146,8 +126,8 @@ namespace RTC
inline inline
bool Transport::IsAlive() bool Transport::IsAlive()
{ {
if (this->dtlsState == DtlsTransportState::CLOSED || if (this->dtlsTransport->GetState() == DTLSTransport::DtlsState::FAILED ||
this->dtlsState == DtlsTransportState::FAILED) this->dtlsTransport->GetState() == DTLSTransport::DtlsState::CLOSED)
{ {
return false; return false;
} }
......
...@@ -542,7 +542,7 @@ namespace RTC ...@@ -542,7 +542,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (this->running) if (IsRunning())
{ {
// Send close alert to the peer. // Send close alert to the peer.
SSL_shutdown(this->ssl); SSL_shutdown(this->ssl);
...@@ -552,9 +552,6 @@ namespace RTC ...@@ -552,9 +552,6 @@ namespace RTC
// Close the DTLS timer. // Close the DTLS timer.
this->timer->Close(); this->timer->Close();
// Notify the listener.
this->listener->onDTLSClosed(this);
delete this; delete this;
} }
...@@ -564,9 +561,9 @@ namespace RTC ...@@ -564,9 +561,9 @@ namespace RTC
MS_DEBUG("[role:%s, running:%s, handshake done:%s, connected:%s]", MS_DEBUG("[role:%s, running:%s, handshake done:%s, connected:%s]",
(this->localRole == Role::SERVER ? "server" : (this->localRole == Role::CLIENT ? "client" : "none")), (this->localRole == Role::SERVER ? "server" : (this->localRole == Role::CLIENT ? "client" : "none")),
this->running ? "yes" : "no", IsRunning() ? "yes" : "no",
this->handshakeDone ? "yes" : "no", this->handshakeDone ? "yes" : "no",
this->connected ? "yes" : "no"); this->state == DtlsState::CONNECTED ? "yes" : "no");
} }
void DTLSTransport::Run(Role localRole) void DTLSTransport::Run(Role localRole)
...@@ -594,10 +591,9 @@ namespace RTC ...@@ -594,10 +591,9 @@ namespace RTC
// Update local role. // Update local role.
this->localRole = localRole; this->localRole = localRole;
// Set running flag.
this->running = true;
// Notify the listener. // Set state and notify the listener.
this->state = DtlsState::CONNECTING;
this->listener->onDTLSConnecting(this); this->listener->onDTLSConnecting(this);
switch (this->localRole) switch (this->localRole)
...@@ -632,7 +628,7 @@ namespace RTC ...@@ -632,7 +628,7 @@ namespace RTC
// The remote fingerpring may have been set after DTLS handshake was done, // The remote fingerpring may have been set after DTLS handshake was done,
// so we may need to process it now. // so we may need to process it now.
if (this->handshakeDone && !this->connected) if (this->handshakeDone && this->state != DtlsState::CONNECTED)
{ {
MS_DEBUG("handshake already done, processing it right now"); MS_DEBUG("handshake already done, processing it right now");
...@@ -647,7 +643,7 @@ namespace RTC ...@@ -647,7 +643,7 @@ namespace RTC
int written; int written;
int read; int read;
if (!this->running) if (!IsRunning())
{ {
MS_ERROR("cannot process data while not running"); MS_ERROR("cannot process data while not running");
...@@ -694,7 +690,7 @@ namespace RTC ...@@ -694,7 +690,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// We cannot send data to the peer if its remote fingerprint is not validated. // We cannot send data to the peer if its remote fingerprint is not validated.
if (!this->connected) if (this->state != DtlsState::CONNECTED)
{ {
MS_ERROR("cannot send application data while DTLS is not fully connected"); MS_ERROR("cannot send application data while DTLS is not fully connected");
...@@ -732,7 +728,7 @@ namespace RTC ...@@ -732,7 +728,7 @@ namespace RTC
int ret; int ret;
if (!this->running) if (!IsRunning())
return; return;
MS_DEBUG("resetting DTLS transport"); MS_DEBUG("resetting DTLS transport");
...@@ -746,10 +742,9 @@ namespace RTC ...@@ -746,10 +742,9 @@ namespace RTC
SSL_shutdown(this->ssl); SSL_shutdown(this->ssl);
this->localRole = Role::NONE; this->localRole = Role::NONE;
this->running = false; this->state = DtlsState::NEW;
this->handshakeDone = false; this->handshakeDone = false;
this->handshakeDoneNow = false; this->handshakeDoneNow = false;
this->connected = false;
// Reset SSL status. // Reset SSL status.
// NOTE: For this to properly work, SSL_shutdown() must be called before. // NOTE: For this to properly work, SSL_shutdown() must be called before.
...@@ -766,6 +761,7 @@ namespace RTC ...@@ -766,6 +761,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
int err; int err;
bool was_handshake_done = this->handshakeDone;
err = SSL_get_error(this->ssl, return_code); err = SSL_get_error(this->ssl, return_code);
switch (err) switch (err)
...@@ -805,28 +801,22 @@ namespace RTC ...@@ -805,28 +801,22 @@ namespace RTC
// Stop the timer. // Stop the timer.
this->timer->Stop(); this->timer->Stop();
// Process the handshake. // Process the handshake just once (ignore if DTLS renegotiation).
ProcessHandshake(); if (!was_handshake_done)
ProcessHandshake();
} }
// Check if the peer sent close alert or a fatal error happened. // Check if the peer sent close alert or a fatal error happened.
else if ((SSL_get_shutdown(this->ssl) & SSL_RECEIVED_SHUTDOWN) || err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) else if ((SSL_get_shutdown(this->ssl) & SSL_RECEIVED_SHUTDOWN) || err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL)
{ {
bool was_connected = this->connected; if (this->state == DtlsState::CONNECTED)
this->localRole = Role::NONE;
this->running = false;
this->handshakeDone = false;
this->handshakeDoneNow = false;
this->connected = false;
if (was_connected)
{ {
MS_DEBUG("disconnected"); MS_DEBUG("disconnected");
Reset(); Reset();
// Notify to the listener. // Set state and notify the listener.
this->listener->onDTLSDisconnected(this); this->state = DtlsState::CLOSED;
this->listener->onDTLSClosed(this);
} }
else else
{ {
...@@ -834,7 +824,8 @@ namespace RTC ...@@ -834,7 +824,8 @@ namespace RTC
Reset(); Reset();
// Notify to the listener. // Set state and notify the listener.
this->state = DtlsState::FAILED;
this->listener->onDTLSFailed(this); this->listener->onDTLSFailed(this);
} }
...@@ -902,7 +893,8 @@ namespace RTC ...@@ -902,7 +893,8 @@ namespace RTC
Reset(); Reset();
// Notify to the listener. // Set state and notify the listener.
this->state = DtlsState::FAILED;
this->listener->onDTLSFailed(this); this->listener->onDTLSFailed(this);
return false; return false;
...@@ -930,14 +922,13 @@ namespace RTC ...@@ -930,14 +922,13 @@ namespace RTC
{ {
Reset(); Reset();
// Notify to the listener. // Set state and notify the listener.
this->state = DtlsState::FAILED;
this->listener->onDTLSFailed(this); this->listener->onDTLSFailed(this);
return; return;
} }
this->connected = true;
// Get the negotiated SRTP profile. // Get the negotiated SRTP profile.
RTC::SRTPSession::SRTPProfile srtp_profile; RTC::SRTPSession::SRTPProfile srtp_profile;
srtp_profile = GetNegotiatedSRTPProfile(); srtp_profile = GetNegotiatedSRTPProfile();
...@@ -955,7 +946,8 @@ namespace RTC ...@@ -955,7 +946,8 @@ namespace RTC
Reset(); Reset();
// Notify to the listener. // Set state and notify the listener.
this->state = DtlsState::FAILED;
this->listener->onDTLSFailed(this); this->listener->onDTLSFailed(this);
} }
} }
...@@ -1074,7 +1066,8 @@ namespace RTC ...@@ -1074,7 +1066,8 @@ namespace RTC
std::memcpy(srtp_remote_master_key, srtp_remote_key, MS_SRTP_MASTER_KEY_LENGTH); std::memcpy(srtp_remote_master_key, srtp_remote_key, MS_SRTP_MASTER_KEY_LENGTH);
std::memcpy(srtp_remote_master_key + MS_SRTP_MASTER_KEY_LENGTH, srtp_remote_salt, MS_SRTP_MASTER_SALT_LENGTH); std::memcpy(srtp_remote_master_key + MS_SRTP_MASTER_KEY_LENGTH, srtp_remote_salt, MS_SRTP_MASTER_SALT_LENGTH);
// Notify the listener with the SRTP key material. // Set state and notify the listener.
this->state = DtlsState::CONNECTED;
this->listener->onDTLSConnected(this, srtp_profile, srtp_local_master_key, MS_SRTP_MASTER_LENGTH, srtp_remote_master_key, MS_SRTP_MASTER_LENGTH); this->listener->onDTLSConnected(this, srtp_profile, srtp_local_master_key, MS_SRTP_MASTER_LENGTH, srtp_remote_master_key, MS_SRTP_MASTER_LENGTH);
} }
......
...@@ -7,8 +7,9 @@ namespace RTC ...@@ -7,8 +7,9 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
IceServer::IceServer(Listener* listener, const std::string& usernameFragment, const std::string password) : IceServer::IceServer(Listener* listener, IceServer::IceComponent iceComponent, const std::string& usernameFragment, const std::string password) :
listener(listener), listener(listener),
iceComponent(iceComponent),
usernameFragment(usernameFragment), usernameFragment(usernameFragment),
password(password) password(password)
{ {
...@@ -190,6 +191,30 @@ namespace RTC ...@@ -190,6 +191,30 @@ namespace RTC
return false; return false;
} }
void IceServer::ForceSelectedTuple(RTC::TransportTuple* tuple)
{
MS_TRACE();
MS_ASSERT(this->state != IceState::NEW, "cannot force the selected tuple if not in 'connecting' or 'connected' state is 'new' but there is selected tuple");
// TODO: Remove as this assert is reduntant.
MS_ASSERT(this->selectedTuple != nullptr, "cannot force the selected tuple if there was not a selected tuple");
auto stored_tuple = HasTuple(tuple);
MS_ASSERT(stored_tuple, "cannot force the selected tuple if the given tuple was not already a valid tuple");
// If the given tuple was already the selected tuple do nothing.
if (stored_tuple == this->selectedTuple)
return;
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
}
void IceServer::HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate) void IceServer::HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate)
{ {
MS_TRACE(); MS_TRACE();
...@@ -314,6 +339,7 @@ namespace RTC ...@@ -314,6 +339,7 @@ namespace RTC
} }
} }
inline
RTC::TransportTuple* IceServer::AddTuple(RTC::TransportTuple* tuple) RTC::TransportTuple* IceServer::AddTuple(RTC::TransportTuple* tuple)
{ {
MS_TRACE(); MS_TRACE();
...@@ -332,6 +358,7 @@ namespace RTC ...@@ -332,6 +358,7 @@ namespace RTC
return stored_tuple; return stored_tuple;
} }
inline
RTC::TransportTuple* IceServer::HasTuple(RTC::TransportTuple* tuple) RTC::TransportTuple* IceServer::HasTuple(RTC::TransportTuple* tuple)
{ {
MS_TRACE(); MS_TRACE();
...@@ -357,6 +384,7 @@ namespace RTC ...@@ -357,6 +384,7 @@ namespace RTC
return nullptr; return nullptr;
} }
inline
void IceServer::SetSelectedTuple(RTC::TransportTuple* stored_tuple) void IceServer::SetSelectedTuple(RTC::TransportTuple* stored_tuple)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -50,10 +50,9 @@ namespace RTC ...@@ -50,10 +50,9 @@ namespace RTC
// RTP transport. // RTP transport.
if (!rtpTransport) if (!rtpTransport)
{ {
this->iceComponent = IceComponent::RTP;
// Create a ICE server. // Create a ICE server.
this->iceServer = new RTC::IceServer(this, this->iceServer = new RTC::IceServer(this,
IceServer::IceComponent::RTP,
Utils::Crypto::GetRandomString(16), Utils::Crypto::GetRandomString(16),
Utils::Crypto::GetRandomString(32)); Utils::Crypto::GetRandomString(32));
...@@ -66,10 +65,9 @@ namespace RTC ...@@ -66,10 +65,9 @@ namespace RTC
// RTCP transport associated to a given RTP transport. // RTCP transport associated to a given RTP transport.
else else
{ {
this->iceComponent = IceComponent::RTCP;
// Create a ICE server. // Create a ICE server.
this->iceServer = new RTC::IceServer(this, this->iceServer = new RTC::IceServer(this,
IceServer::IceComponent::RTCP,
rtpTransport->GetIceUsernameFragment(), rtpTransport->GetIceUsernameFragment(),
rtpTransport->GetIcePassword()); rtpTransport->GetIcePassword());
...@@ -84,7 +82,7 @@ namespace RTC ...@@ -84,7 +82,7 @@ namespace RTC
{ {
unsigned long priority; unsigned long priority;
if (this->iceComponent == IceComponent::RTP) if (this->iceServer->GetComponent() == IceServer::IceComponent::RTP)
priority = ICE_CANDIDATE_PRIORITY_IPV4_UDP_RTP; priority = ICE_CANDIDATE_PRIORITY_IPV4_UDP_RTP;
else else
priority = ICE_CANDIDATE_PRIORITY_IPV4_UDP_RTCP; priority = ICE_CANDIDATE_PRIORITY_IPV4_UDP_RTCP;
...@@ -109,7 +107,7 @@ namespace RTC ...@@ -109,7 +107,7 @@ namespace RTC
{ {
unsigned long priority; unsigned long priority;
if (this->iceComponent == IceComponent::RTP) if (this->iceServer->GetComponent() == IceServer::IceComponent::RTP)
priority = ICE_CANDIDATE_PRIORITY_IPV6_UDP_RTP; priority = ICE_CANDIDATE_PRIORITY_IPV6_UDP_RTP;
else else
priority = ICE_CANDIDATE_PRIORITY_IPV6_UDP_RTCP; priority = ICE_CANDIDATE_PRIORITY_IPV6_UDP_RTCP;
...@@ -134,7 +132,7 @@ namespace RTC ...@@ -134,7 +132,7 @@ namespace RTC
{ {
unsigned long priority; unsigned long priority;
if (this->iceComponent == IceComponent::RTP) if (this->iceServer->GetComponent() == IceServer::IceComponent::RTP)
priority = ICE_CANDIDATE_PRIORITY_IPV4_TCP_RTP; priority = ICE_CANDIDATE_PRIORITY_IPV4_TCP_RTP;
else else
priority = ICE_CANDIDATE_PRIORITY_IPV4_TCP_RTCP; priority = ICE_CANDIDATE_PRIORITY_IPV4_TCP_RTCP;
...@@ -159,7 +157,7 @@ namespace RTC ...@@ -159,7 +157,7 @@ namespace RTC
{ {
unsigned long priority; unsigned long priority;
if (this->iceComponent == IceComponent::RTP) if (this->iceServer->GetComponent() == IceServer::IceComponent::RTP)
priority = ICE_CANDIDATE_PRIORITY_IPV6_TCP_RTP; priority = ICE_CANDIDATE_PRIORITY_IPV6_TCP_RTP;
else else
priority = ICE_CANDIDATE_PRIORITY_IPV6_TCP_RTCP; priority = ICE_CANDIDATE_PRIORITY_IPV6_TCP_RTCP;
...@@ -203,16 +201,32 @@ namespace RTC ...@@ -203,16 +201,32 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Close all the servers. if (this->iceServer)
Terminate(); {
this->iceServer->Close();
this->iceServer = nullptr;
}
// And also close the DTLSTransport.
if (this->dtlsTransport) if (this->dtlsTransport)
{ {
this->dtlsTransport->Close(); this->dtlsTransport->Close();
this->dtlsTransport = nullptr; this->dtlsTransport = nullptr;
} }
ClosePorts();
// if (this->srtpRecvSession)
// {
// this->srtpRecvSession->Close();
// this->srtpRecvSession = nullptr;
// }
// if (this->srtpSendSession)
// {
// this->srtpSendSession->Close();
// this->srtpSendSession = nullptr;
// }
// If this was allocated (it did not throw in the constructor) notify the // If this was allocated (it did not throw in the constructor) notify the
// listener and delete it. // listener and delete it.
if (this->allocated) if (this->allocated)
...@@ -229,12 +243,12 @@ namespace RTC ...@@ -229,12 +243,12 @@ namespace RTC
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_iceComponent("iceComponent"); static const Json::StaticString k_iceComponent("iceComponent");
static const Json::StaticString v_RTP("RTP");
static const Json::StaticString v_RTCP("RTCP");
static const Json::StaticString k_iceLocalParameters("iceLocalParameters"); static const Json::StaticString k_iceLocalParameters("iceLocalParameters");
static const Json::StaticString k_usernameFragment("usernameFragment"); static const Json::StaticString k_usernameFragment("usernameFragment");
static const Json::StaticString k_password("password"); static const Json::StaticString k_password("password");
static const Json::StaticString k_iceLocalCandidates("iceLocalCandidates"); static const Json::StaticString k_iceLocalCandidates("iceLocalCandidates");
static const Json::StaticString v_RTP("RTP");
static const Json::StaticString v_RTCP("RTCP");
static const Json::StaticString k_dtlsLocalParameters("dtlsLocalParameters"); static const Json::StaticString k_dtlsLocalParameters("dtlsLocalParameters");
static const Json::StaticString k_fingerprints("fingerprints"); static const Json::StaticString k_fingerprints("fingerprints");
static const Json::StaticString k_role("role"); static const Json::StaticString k_role("role");
...@@ -251,7 +265,7 @@ namespace RTC ...@@ -251,7 +265,7 @@ namespace RTC
Json::Value data; Json::Value data;
// Add `iceComponent`. // Add `iceComponent`.
if (this->iceComponent == IceComponent::RTP) if (this->iceServer->GetComponent() == IceServer::IceComponent::RTP)
data[k_iceComponent] = v_RTP; data[k_iceComponent] = v_RTP;
else else
data[k_iceComponent] = v_RTCP; data[k_iceComponent] = v_RTCP;
...@@ -287,23 +301,23 @@ namespace RTC ...@@ -287,23 +301,23 @@ namespace RTC
} }
// Add `dtlsState`. // Add `dtlsState`.
switch (this->dtlsState) switch (this->dtlsTransport->GetState())
{ {
case DtlsTransportState::NEW: case DTLSTransport::DtlsState::NEW:
data[k_dtlsState] = v_new; data[k_dtlsState] = v_new;
break; break;
case DtlsTransportState::CONNECTING: case DTLSTransport::DtlsState::CONNECTING:
data[k_dtlsState] = v_connecting; data[k_dtlsState] = v_connecting;
break; break;
case DtlsTransportState::CONNECTED: case DTLSTransport::DtlsState::CONNECTED:
data[k_dtlsState] = v_connected; data[k_dtlsState] = v_connected;
break; break;
case DtlsTransportState::CLOSED: case DTLSTransport::DtlsState::FAILED:
data[k_dtlsState] = v_closed;
break;
case DtlsTransportState::FAILED:
data[k_dtlsState] = v_failed; data[k_dtlsState] = v_failed;
break; break;
case DTLSTransport::DtlsState::CLOSED:
data[k_dtlsState] = v_closed;
break;
} }
return data; return data;
...@@ -453,29 +467,11 @@ namespace RTC ...@@ -453,29 +467,11 @@ namespace RTC
} }
} }
// This closes all the server but not the DTLSTransport. // This closes all the UDP/TCP servers.
void Transport::Terminate() void Transport::ClosePorts()
{ {
MS_TRACE(); MS_TRACE();
if (this->iceServer)
{
this->iceServer->Close();
this->iceServer = nullptr;
}
// if (this->srtpRecvSession)
// {
// this->srtpRecvSession->Close();
// this->srtpRecvSession = nullptr;
// }
// if (this->srtpSendSession)
// {
// this->srtpSendSession->Close();
// this->srtpSendSession = nullptr;
// }
for (auto socket : this->udpSockets) for (auto socket : this->udpSockets)
socket->Close(); socket->Close();
this->udpSockets.clear(); this->udpSockets.clear();
...@@ -494,7 +490,7 @@ namespace RTC ...@@ -494,7 +490,7 @@ namespace RTC
if (!IsAlive()) if (!IsAlive())
MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a non alive Transport"); MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a non alive Transport");
if (this->iceComponent != IceComponent::RTP) if (this->iceServer->GetComponent() != IceServer::IceComponent::RTP)
MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a RTCP Transport"); MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a RTCP Transport");
return new RTC::Transport(this->listener, this->notifier, transportId, nullData, this); return new RTC::Transport(this->listener, this->notifier, transportId, nullData, this);
...@@ -586,9 +582,12 @@ namespace RTC ...@@ -586,9 +582,12 @@ namespace RTC
return; return;
} }
// Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple);
// Check that DTLS status is 'connecting' or 'connected'. // Check that DTLS status is 'connecting' or 'connected'.
if (this->dtlsState == DtlsTransportState::CONNECTING || if (this->dtlsTransport->GetState() == DTLSTransport::DtlsState::CONNECTING ||
this->dtlsState == DtlsTransportState::CONNECTED) this->dtlsTransport->GetState() == DTLSTransport::DtlsState::CONNECTED)
{ {
MS_DEBUG("DTLS data received, passing it to the DTLS transport"); MS_DEBUG("DTLS data received, passing it to the DTLS transport");
...@@ -596,7 +595,7 @@ namespace RTC ...@@ -596,7 +595,7 @@ namespace RTC
} }
else else
{ {
MS_DEBUG("DTLSTransport is not connecting or conneted, ignoring received DTLS data"); MS_DEBUG("DTLSTransport is not 'connecting' or 'conneted', ignoring received DTLS data");
return; return;
} }
...@@ -607,6 +606,16 @@ namespace RTC ...@@ -607,6 +606,16 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!this->iceServer->IsValidTuple(tuple))
{
MS_DEBUG("ignoring RTP data coming from an invalid tuple");
return;
}
// Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple);
// TODO // TODO
} }
...@@ -615,6 +624,16 @@ namespace RTC ...@@ -615,6 +624,16 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!this->iceServer->IsValidTuple(tuple))
{
MS_DEBUG("ignoring RTCP data coming from an invalid tuple");
return;
}
// Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple);
// TODO // TODO
} }
...@@ -776,8 +795,6 @@ namespace RTC ...@@ -776,8 +795,6 @@ namespace RTC
MS_DEBUG("DTLS connecting"); MS_DEBUG("DTLS connecting");
this->dtlsState = DtlsTransportState::CONNECTING;
// Notify. // Notify.
eventData[k_dtlsState] = v_connecting; eventData[k_dtlsState] = v_connecting;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
...@@ -794,8 +811,6 @@ namespace RTC ...@@ -794,8 +811,6 @@ namespace RTC
MS_DEBUG("DTLS connected"); MS_DEBUG("DTLS connected");
this->dtlsState = DtlsTransportState::CONNECTED;
// TODO // TODO
// SetLocalSRTPKey(srtp_profile, srtp_local_key, srtp_local_key_len); // SetLocalSRTPKey(srtp_profile, srtp_local_key, srtp_local_key_len);
// SetRemoteSRTPKey(srtp_profile, srtp_remote_key, srtp_remote_key_len); // SetRemoteSRTPKey(srtp_profile, srtp_remote_key, srtp_remote_key_len);
...@@ -805,29 +820,22 @@ namespace RTC ...@@ -805,29 +820,22 @@ namespace RTC
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSDisconnected(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState"); static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_closed("closed"); static const Json::StaticString v_closed("closed");
DtlsTransportState previousDtlsState = this->dtlsState;
Json::Value eventData; Json::Value eventData;
MS_DEBUG("DTLS remotely disconnected"); MS_DEBUG("DTLS remotely disconnected");
this->dtlsState = DtlsTransportState::CLOSED; ClosePorts();
// Call Terminate() so all the servers are closed.
Terminate();
// Notify. // Notify.
if (previousDtlsState != DtlsTransportState::CLOSED) eventData[k_dtlsState] = v_closed;
{ this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
eventData[k_dtlsState] = v_closed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
}
} }
void Transport::onDTLSFailed(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSFailed(RTC::DTLSTransport* dtlsTransport)
...@@ -841,41 +849,13 @@ namespace RTC ...@@ -841,41 +849,13 @@ namespace RTC
MS_DEBUG("DTLS failed"); MS_DEBUG("DTLS failed");
this->dtlsState = DtlsTransportState::FAILED; ClosePorts();
// Call Terminate() so all the servers are closed.
Terminate();
// Notify. // Notify.
eventData[k_dtlsState] = v_failed; eventData[k_dtlsState] = v_failed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport)
{
MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_closed("closed");
DtlsTransportState previousDtlsState = this->dtlsState;
Json::Value eventData;
MS_DEBUG("DTLS closed");
this->dtlsState = DtlsTransportState::CLOSED;
// NOTE: This is just called when calling Close() so don't call
// Terminate() here (not needed).
// Notify.
if (previousDtlsState != DtlsTransportState::CLOSED)
{
eventData[k_dtlsState] = v_closed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
}
}
void Transport::onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) void Transport::onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len)
{ {
MS_TRACE(); MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论