提交 6d8802c5 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

DTLS working at 75%.

上级 d318e260
...@@ -150,14 +150,18 @@ class Transport extends EventEmitter ...@@ -150,14 +150,18 @@ class Transport extends EventEmitter
}; };
// Send Channel request // Send Channel request
this._channel.request('transport.start', data) return this._channel.request('transport.start', data)
.then(() => .then((data) =>
{ {
logger.debug('"transport.start" request succeeded'); logger.debug('"transport.start" request succeeded');
return data;
}) })
.catch((error) => .catch((error) =>
{ {
logger.error('"transport.start" request failed [status:%s, error:"%s"]', error.status, error.message); logger.error('"transport.start" request failed [status:%s, error:"%s"]', error.status, error.message);
throw error;
}); });
} }
......
...@@ -149,6 +149,7 @@ app.put('/test-transport', function(req) ...@@ -149,6 +149,7 @@ app.put('/test-transport', function(req)
type : 'sha-256', type : 'sha-256',
hash : mediasoup.extra.dtlsFingerprintToSDP(transport.dtlsLocalFingerprints['sha-256']) hash : mediasoup.extra.dtlsFingerprintToSDP(transport.dtlsLocalFingerprints['sha-256'])
}; };
// SDP offer is always 'a=setup:actpass'
am.setup = 'passive'; am.setup = 'passive';
am.type = om.type; am.type = om.type;
am.rtcpMux = 'rtcp-mux'; am.rtcpMux = 'rtcp-mux';
...@@ -159,6 +160,22 @@ app.put('/test-transport', function(req) ...@@ -159,6 +160,22 @@ app.put('/test-transport', function(req)
am.direction = om.direction === 'recvonly' ? 'sendonly' : 'sendrecv'; am.direction = om.direction === 'recvonly' ? 'sendonly' : 'sendrecv';
am.payloads = om.payloads; am.payloads = om.payloads;
// Update the transport with the remote DTLS parameters
setTimeout(() =>
{
transport.start(
{
role : 'client',
fingerprint :
{
algorithm : om.fingerprint.type,
value : mediasoup.extra.dtlsFingerprintFromSDP(om.fingerprint.hash)
}
})
.then((data) => debug('transport.start() success [data:%o]', data))
.catch((error) => debugerror('transport.start() failed: %o', error));
}, 0);
answer.media.push(am); answer.media.push(am);
}); });
......
...@@ -95,15 +95,15 @@ namespace RTC ...@@ -95,15 +95,15 @@ namespace RTC
DTLSTransport(Listener* listener); DTLSTransport(Listener* listener);
virtual ~DTLSTransport(); virtual ~DTLSTransport();
void Start(Role role);
void SetRemoteFingerprint(Fingerprint fingerprint);
void Reset();
void Close(); void Close();
void Reset();
void Dump();
void Run(Role localRole);
void SetRemoteFingerprint(Fingerprint fingerprint);
void ProcessDTLSData(const MS_BYTE* data, size_t len); void ProcessDTLSData(const MS_BYTE* data, size_t len);
bool IsStarted(); bool IsRunning();
bool IsConnected(); bool IsConnected();
void SendApplicationData(const MS_BYTE* data, size_t len); void SendApplicationData(const MS_BYTE* data, size_t len);
void Dump();
private: private:
bool CheckStatus(int return_code); bool CheckStatus(int return_code);
...@@ -133,7 +133,7 @@ namespace RTC ...@@ -133,7 +133,7 @@ namespace RTC
// Others. // Others.
Role localRole = Role::NONE; Role localRole = Role::NONE;
Fingerprint remoteFingerprint = { FingerprintAlgorithm::NONE, "" }; Fingerprint remoteFingerprint = { FingerprintAlgorithm::NONE, "" };
bool started = false; bool running = false;
bool handshakeDone = false; bool handshakeDone = false;
bool handshakeDoneNow = false; bool handshakeDoneNow = false;
bool connected = false; bool connected = false;
...@@ -186,15 +186,15 @@ namespace RTC ...@@ -186,15 +186,15 @@ namespace RTC
/* Inline instance methods. */ /* Inline instance methods. */
inline inline
bool DTLSTransport::IsConnected() bool DTLSTransport::IsRunning()
{ {
return this->connected; return this->running;
} }
inline inline
bool DTLSTransport::IsStarted() bool DTLSTransport::IsConnected()
{ {
return this->started; return this->connected;
} }
} }
......
...@@ -125,7 +125,7 @@ namespace RTC ...@@ -125,7 +125,7 @@ namespace RTC
bool icePairedWithUseCandidate = false; bool icePairedWithUseCandidate = false;
// Others (DTLS). // Others (DTLS).
bool started = false; bool started = false;
RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::CLIENT; RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO;
}; };
/* Inline methods. */ /* Inline methods. */
......
...@@ -538,51 +538,26 @@ namespace RTC ...@@ -538,51 +538,26 @@ namespace RTC
} }
} }
// TODO: en realidad esto debería juntarse con el SetRemoteFingerprint. void DTLSTransport::Close()
void DTLSTransport::Start(Role role)
{ {
MS_TRACE(); MS_TRACE();
if (this->started) if (this->checkingStatus)
{
this->doClose = true;
return; return;
}
this->started = true; if (this->running)
this->localRole = role;
switch (this->localRole)
{ {
case Role::SERVER: // Send close alert to the peer.
SSL_set_accept_state(this->ssl); SSL_shutdown(this->ssl);
SSL_do_handshake(this->ssl);
break;
case Role::CLIENT:
SSL_set_connect_state(this->ssl);
SSL_do_handshake(this->ssl);
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
SetTimeout();
break;
case Role::NONE:
MS_ABORT("no valid DTLS role given");
break;
}
} }
// TODO: replace with start() this->timer->Close();
void DTLSTransport::SetRemoteFingerprint(Fingerprint fingerprint)
{
MS_TRACE();
MS_ASSERT(fingerprint.algorithm != FingerprintAlgorithm::NONE, "no fingerprint algorithm provided");
this->remoteFingerprint = fingerprint;
// The remote fingerpring may have been set after DTLS handshake was done, delete this;
// so we may need to process it now.
if (this->handshakeDone && !this->connected)
{
MS_DEBUG("handshake already done, processing it right now");
ProcessHandshake();
}
} }
void DTLSTransport::Reset() void DTLSTransport::Reset()
...@@ -593,7 +568,7 @@ namespace RTC ...@@ -593,7 +568,7 @@ namespace RTC
this->doReset = false; this->doReset = false;
if (!this->started) if (!this->running)
return; return;
if (this->checkingStatus) if (this->checkingStatus)
...@@ -612,7 +587,7 @@ namespace RTC ...@@ -612,7 +587,7 @@ namespace RTC
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
this->localRole = Role::NONE; this->localRole = Role::NONE;
this->started = false; this->running = false;
this->handshakeDone = false; this->handshakeDone = false;
this->handshakeDoneNow = false; this->handshakeDoneNow = false;
this->connected = false; this->connected = false;
...@@ -627,26 +602,79 @@ namespace RTC ...@@ -627,26 +602,79 @@ namespace RTC
LOG_OPENSSL_ERROR("SSL_clear() failed"); LOG_OPENSSL_ERROR("SSL_clear() failed");
} }
void DTLSTransport::Close() void DTLSTransport::Dump()
{ {
MS_TRACE(); MS_TRACE();
if (this->checkingStatus) MS_DEBUG("[role: %s | running: %s | handshake done: %s | connected: %s]",
(this->localRole == Role::SERVER ? "server" : (this->localRole == Role::CLIENT ? "client" : "none")),
this->running ? "yes" : "no",
this->handshakeDone ? "yes" : "no",
this->connected ? "yes" : "no");
}
void DTLSTransport::Run(Role localRole)
{ {
this->doClose = true; MS_TRACE();
MS_ASSERT(localRole == Role::CLIENT || localRole == Role::SERVER, "local DTLS role must be 'client' or 'server'");
Role previousLocalRole = this->localRole;
if (localRole == previousLocalRole)
{
MS_DEBUG("same local DTLS role provided, doing nothing");
return; return;
} }
if (this->started) // If the previous local DTLS role was 'client' or 'server' do reset.
if (previousLocalRole == Role::CLIENT || previousLocalRole == Role::SERVER)
{ {
// Send close alert to the peer. MS_DEBUG("resetting due to local DTLS role change");
SSL_shutdown(this->ssl);
Reset();
}
// Update local role.
this->localRole = localRole;
// Set running flag.
this->running = true;
switch (this->localRole)
{
case Role::CLIENT:
SSL_set_connect_state(this->ssl);
SSL_do_handshake(this->ssl);
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
SetTimeout();
break;
case Role::SERVER:
SSL_set_accept_state(this->ssl);
SSL_do_handshake(this->ssl);
break;
default:
MS_ABORT("invalid local DTLS role");
break;
}
} }
this->timer->Close(); void DTLSTransport::SetRemoteFingerprint(Fingerprint fingerprint)
{
MS_TRACE();
delete this; MS_ASSERT(fingerprint.algorithm != FingerprintAlgorithm::NONE, "no fingerprint algorithm provided");
this->remoteFingerprint = fingerprint;
// The remote fingerpring may have been set after DTLS handshake was done,
// so we may need to process it now.
if (this->handshakeDone && !this->connected)
{
MS_DEBUG("handshake already done, processing it right now");
ProcessHandshake();
}
} }
void DTLSTransport::ProcessDTLSData(const MS_BYTE* data, size_t len) void DTLSTransport::ProcessDTLSData(const MS_BYTE* data, size_t len)
...@@ -656,9 +684,10 @@ namespace RTC ...@@ -656,9 +684,10 @@ namespace RTC
int written; int written;
int read; int read;
if (!this->started) if (!this->running)
{ {
MS_ERROR("cannot process data while not started"); MS_ERROR("cannot process data while not running");
return; return;
} }
...@@ -699,7 +728,7 @@ namespace RTC ...@@ -699,7 +728,7 @@ namespace RTC
if (!this->connected) if (!this->connected)
{ {
MS_ERROR("cannot send application data while DTLS is not connected"); MS_ERROR("cannot send application data while DTLS is not fully connected");
return; return;
} }
...@@ -726,17 +755,6 @@ namespace RTC ...@@ -726,17 +755,6 @@ namespace RTC
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
} }
void DTLSTransport::Dump()
{
MS_TRACE();
MS_DEBUG("[role: %s | started: %s | handshake done: %s | connected: %s]",
(this->localRole == Role::SERVER ? "server" : (this->localRole == Role::CLIENT ? "client" : "none")),
this->started ? "yes" : "no",
this->handshakeDone ? "yes" : "no",
this->connected ? "yes" : "no");
}
inline inline
bool DTLSTransport::CheckStatus(int return_code) bool DTLSTransport::CheckStatus(int return_code)
{ {
...@@ -794,7 +812,7 @@ namespace RTC ...@@ -794,7 +812,7 @@ namespace RTC
bool was_connected = this->connected; bool was_connected = this->connected;
this->localRole = Role::NONE; this->localRole = Role::NONE;
this->started = false; this->running = false;
this->handshakeDone = false; this->handshakeDone = false;
this->handshakeDoneNow = false; this->handshakeDoneNow = false;
this->connected = false; this->connected = false;
...@@ -904,6 +922,7 @@ namespace RTC ...@@ -904,6 +922,7 @@ namespace RTC
if (this->remoteFingerprint.algorithm == FingerprintAlgorithm::NONE) if (this->remoteFingerprint.algorithm == FingerprintAlgorithm::NONE)
{ {
MS_DEBUG("remote fingerprint not yet set, waiting for it"); MS_DEBUG("remote fingerprint not yet set, waiting for it");
return; return;
} }
...@@ -915,6 +934,7 @@ namespace RTC ...@@ -915,6 +934,7 @@ namespace RTC
// Reset DTLS. // Reset DTLS.
Reset(); Reset();
return; return;
} }
...@@ -1070,7 +1090,7 @@ namespace RTC ...@@ -1070,7 +1090,7 @@ namespace RTC
srtp_local_salt = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH; srtp_local_salt = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH;
srtp_remote_salt = srtp_local_salt + MS_SRTP_MASTER_SALT_LENGTH; srtp_remote_salt = srtp_local_salt + MS_SRTP_MASTER_SALT_LENGTH;
break; break;
case Role::NONE: default:
MS_ABORT("no DTLS role set"); MS_ABORT("no DTLS role set");
break; break;
} }
...@@ -1102,7 +1122,6 @@ namespace RTC ...@@ -1102,7 +1122,6 @@ namespace RTC
{ {
MS_DEBUG("role: %s | action: %s", role, SSL_state_string_long(this->ssl)); MS_DEBUG("role: %s | action: %s", role, SSL_state_string_long(this->ssl));
} }
else if (where & SSL_CB_ALERT) else if (where & SSL_CB_ALERT)
{ {
const char* alert_type; const char* alert_type;
...@@ -1121,7 +1140,6 @@ namespace RTC ...@@ -1121,7 +1140,6 @@ namespace RTC
else else
MS_DEBUG("DTLS %s alert: %s", alert_type, SSL_alert_desc_string_long(ret)); MS_DEBUG("DTLS %s alert: %s", alert_type, SSL_alert_desc_string_long(ret));
} }
else if (where & SSL_CB_EXIT) else if (where & SSL_CB_EXIT)
{ {
if (ret == 0) if (ret == 0)
...@@ -1129,7 +1147,6 @@ namespace RTC ...@@ -1129,7 +1147,6 @@ namespace RTC
else if (ret < 0) else if (ret < 0)
MS_DEBUG("role: %s | waiting for: %s", role, SSL_state_string_long(this->ssl)); MS_DEBUG("role: %s | waiting for: %s", role, SSL_state_string_long(this->ssl));
} }
else if (where & SSL_CB_HANDSHAKE_START) else if (where & SSL_CB_HANDSHAKE_START)
{ {
MS_DEBUG("DTLS handshake start"); MS_DEBUG("DTLS handshake start");
...@@ -1138,6 +1155,7 @@ namespace RTC ...@@ -1138,6 +1155,7 @@ namespace RTC
else if (where & SSL_CB_HANDSHAKE_DONE) else if (where & SSL_CB_HANDSHAKE_DONE)
{ {
MS_DEBUG("DTLS handshake done"); MS_DEBUG("DTLS handshake done");
this->handshakeDoneNow = true; this->handshakeDoneNow = true;
} }
......
...@@ -304,9 +304,8 @@ namespace RTC ...@@ -304,9 +304,8 @@ namespace RTC
static const Json::StaticString k_algorithm("algorithm"); static const Json::StaticString k_algorithm("algorithm");
static const Json::StaticString k_value("value"); static const Json::StaticString k_value("value");
RTC::DTLSTransport::FingerprintAlgorithm fingerprint_algorithm; RTC::DTLSTransport::Fingerprint remoteFingerprint;
std::string fingerprint_value; RTC::DTLSTransport::Role remoteRole = RTC::DTLSTransport::Role::AUTO; // Default value if missing.
RTC::DTLSTransport::Role role = RTC::DTLSTransport::Role::AUTO; // Default value.
// Ensure this method is not called twice. // Ensure this method is not called twice.
if (this->started) if (this->started)
...@@ -337,9 +336,9 @@ namespace RTC ...@@ -337,9 +336,9 @@ namespace RTC
return; return;
} }
fingerprint_algorithm = RTC::DTLSTransport::GetFingerprintAlgorithm(request->data[k_fingerprint][k_algorithm].asString()); remoteFingerprint.algorithm = RTC::DTLSTransport::GetFingerprintAlgorithm(request->data[k_fingerprint][k_algorithm].asString());
if (fingerprint_algorithm == RTC::DTLSTransport::FingerprintAlgorithm::NONE) if (remoteFingerprint.algorithm == RTC::DTLSTransport::FingerprintAlgorithm::NONE)
{ {
MS_ERROR("unsupported .fingerprint.algorithm"); MS_ERROR("unsupported .fingerprint.algorithm");
...@@ -347,21 +346,52 @@ namespace RTC ...@@ -347,21 +346,52 @@ namespace RTC
return; return;
} }
fingerprint_value = request->data[k_fingerprint][k_value].asString(); remoteFingerprint.value = request->data[k_fingerprint][k_value].asString();
if (request->data[k_role].isString()) if (request->data[k_role].isString())
{ remoteRole = RTC::DTLSTransport::GetRole(request->data[k_role].asString());
role = RTC::DTLSTransport::GetRole(request->data[k_role].asString());
if (role == RTC::DTLSTransport::Role::NONE) // Set local DTLS role.
switch (remoteRole)
{ {
case RTC::DTLSTransport::Role::CLIENT:
this->dtlsLocalRole = RTC::DTLSTransport::Role::SERVER;
break;
case RTC::DTLSTransport::Role::SERVER:
this->dtlsLocalRole = RTC::DTLSTransport::Role::CLIENT;
break;
case RTC::DTLSTransport::Role::AUTO:
this->dtlsLocalRole = RTC::DTLSTransport::Role::CLIENT;
break;
case RTC::DTLSTransport::Role::NONE:
MS_ERROR("invalid .role"); MS_ERROR("invalid .role");
request->Reject(500, "invalid .role"); request->Reject(500, "invalid .role");
return; return;
} }
// TODO: Provide the local DTLS role in the response so the JS can expose it.
Json::Value data;
switch (this->dtlsLocalRole)
{
case RTC::DTLSTransport::Role::CLIENT:
data["localDtlsRole"] = "client";
break;
case RTC::DTLSTransport::Role::SERVER:
data["localDtlsRole"] = "server";
break;
default:
MS_ABORT("KAKAKAKAKAKA"); // TODO jeje
} }
request->Accept(data);
// Pass the remote fingerprint to the DTLS transport.
this->dtlsTransport->SetRemoteFingerprint(remoteFingerprint);
// Run the DTLS transport if ready.
RunDTLSTransportIfReady();
break; break;
} }
...@@ -390,7 +420,33 @@ namespace RTC ...@@ -390,7 +420,33 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// TODO switch (this->dtlsLocalRole)
{
// If 'client' then wait for the first Binding with USE-CANDIDATE.
// If 'auto' then behave as 'client' since we are always ICE controlled.
case RTC::DTLSTransport::Role::CLIENT:
case RTC::DTLSTransport::Role::AUTO:
if (this->icePairedWithUseCandidate)
{
MS_DEBUG("running DTLS transport in 'client' role");
this->dtlsTransport->Run(RTC::DTLSTransport::Role::CLIENT);
}
break;
// If 'server' then run the DTLS handler upon first valid ICE pair.
case RTC::DTLSTransport::Role::SERVER:
if (this->icePaired)
{
MS_DEBUG("running DTLS transport in 'server' role");
this->dtlsTransport->Run(RTC::DTLSTransport::Role::SERVER);
}
break;
case RTC::DTLSTransport::Role::NONE:
MS_ABORT("local DTLS role cannot be none");
}
} }
inline inline
...@@ -423,10 +479,18 @@ namespace RTC ...@@ -423,10 +479,18 @@ namespace RTC
return; return;
} }
// TODO: remove if (this->dtlsTransport->IsRunning())
MS_DEBUG("received DTLS data"); {
MS_DEBUG("DTLS data received, passing it to the DTLS transport");
// TODO this->dtlsTransport->ProcessDTLSData(data, len);
}
else
{
MS_DEBUG("DTLSTransport is not running, ignoring received DTLS data");
return;
}
} }
inline inline
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论