提交 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
};
// Send Channel request
this._channel.request('transport.start', data)
.then(() =>
return this._channel.request('transport.start', data)
.then((data) =>
{
logger.debug('"transport.start" request succeeded');
return data;
})
.catch((error) =>
{
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)
type : 'sha-256',
hash : mediasoup.extra.dtlsFingerprintToSDP(transport.dtlsLocalFingerprints['sha-256'])
};
// SDP offer is always 'a=setup:actpass'
am.setup = 'passive';
am.type = om.type;
am.rtcpMux = 'rtcp-mux';
......@@ -159,6 +160,22 @@ app.put('/test-transport', function(req)
am.direction = om.direction === 'recvonly' ? 'sendonly' : 'sendrecv';
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);
});
......
......@@ -95,15 +95,15 @@ namespace RTC
DTLSTransport(Listener* listener);
virtual ~DTLSTransport();
void Start(Role role);
void SetRemoteFingerprint(Fingerprint fingerprint);
void Reset();
void Close();
void Reset();
void Dump();
void Run(Role localRole);
void SetRemoteFingerprint(Fingerprint fingerprint);
void ProcessDTLSData(const MS_BYTE* data, size_t len);
bool IsStarted();
bool IsRunning();
bool IsConnected();
void SendApplicationData(const MS_BYTE* data, size_t len);
void Dump();
private:
bool CheckStatus(int return_code);
......@@ -133,7 +133,7 @@ namespace RTC
// Others.
Role localRole = Role::NONE;
Fingerprint remoteFingerprint = { FingerprintAlgorithm::NONE, "" };
bool started = false;
bool running = false;
bool handshakeDone = false;
bool handshakeDoneNow = false;
bool connected = false;
......@@ -186,15 +186,15 @@ namespace RTC
/* Inline instance methods. */
inline
bool DTLSTransport::IsConnected()
bool DTLSTransport::IsRunning()
{
return this->connected;
return this->running;
}
inline
bool DTLSTransport::IsStarted()
bool DTLSTransport::IsConnected()
{
return this->started;
return this->connected;
}
}
......
......@@ -125,7 +125,7 @@ namespace RTC
bool icePairedWithUseCandidate = false;
// Others (DTLS).
bool started = false;
RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::CLIENT;
RTC::DTLSTransport::Role dtlsLocalRole = RTC::DTLSTransport::Role::AUTO;
};
/* Inline methods. */
......
......@@ -538,51 +538,26 @@ namespace RTC
}
}
// TODO: en realidad esto debería juntarse con el SetRemoteFingerprint.
void DTLSTransport::Start(Role role)
void DTLSTransport::Close()
{
MS_TRACE();
if (this->started)
if (this->checkingStatus)
{
this->doClose = true;
return;
}
this->started = true;
this->localRole = role;
switch (this->localRole)
if (this->running)
{
case Role::SERVER:
SSL_set_accept_state(this->ssl);
SSL_do_handshake(this->ssl);
break;
case Role::CLIENT:
SSL_set_connect_state(this->ssl);
SSL_do_handshake(this->ssl);
SendPendingOutgoingDTLSData();
SetTimeout();
break;
case Role::NONE:
MS_ABORT("no valid DTLS role given");
break;
// Send close alert to the peer.
SSL_shutdown(this->ssl);
SendPendingOutgoingDTLSData();
}
}
// TODO: replace with start()
void DTLSTransport::SetRemoteFingerprint(Fingerprint fingerprint)
{
MS_TRACE();
MS_ASSERT(fingerprint.algorithm != FingerprintAlgorithm::NONE, "no fingerprint algorithm provided");
this->remoteFingerprint = fingerprint;
this->timer->Close();
// 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();
}
delete this;
}
void DTLSTransport::Reset()
......@@ -593,7 +568,7 @@ namespace RTC
this->doReset = false;
if (!this->started)
if (!this->running)
return;
if (this->checkingStatus)
......@@ -612,7 +587,7 @@ namespace RTC
SendPendingOutgoingDTLSData();
this->localRole = Role::NONE;
this->started = false;
this->running = false;
this->handshakeDone = false;
this->handshakeDoneNow = false;
this->connected = false;
......@@ -627,26 +602,79 @@ namespace RTC
LOG_OPENSSL_ERROR("SSL_clear() failed");
}
void DTLSTransport::Close()
void DTLSTransport::Dump()
{
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)
{
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)
{
this->doClose = true;
MS_DEBUG("same local DTLS role provided, doing nothing");
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.
SSL_shutdown(this->ssl);
SendPendingOutgoingDTLSData();
MS_DEBUG("resetting due to local DTLS role change");
Reset();
}
this->timer->Close();
// Update local role.
this->localRole = localRole;
// Set running flag.
this->running = true;
delete this;
switch (this->localRole)
{
case Role::CLIENT:
SSL_set_connect_state(this->ssl);
SSL_do_handshake(this->ssl);
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;
}
}
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,
// 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)
......@@ -656,9 +684,10 @@ namespace RTC
int written;
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;
}
......@@ -699,7 +728,7 @@ namespace RTC
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;
}
......@@ -726,17 +755,6 @@ namespace RTC
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
bool DTLSTransport::CheckStatus(int return_code)
{
......@@ -794,7 +812,7 @@ namespace RTC
bool was_connected = this->connected;
this->localRole = Role::NONE;
this->started = false;
this->running = false;
this->handshakeDone = false;
this->handshakeDoneNow = false;
this->connected = false;
......@@ -904,6 +922,7 @@ namespace RTC
if (this->remoteFingerprint.algorithm == FingerprintAlgorithm::NONE)
{
MS_DEBUG("remote fingerprint not yet set, waiting for it");
return;
}
......@@ -915,6 +934,7 @@ namespace RTC
// Reset DTLS.
Reset();
return;
}
......@@ -1070,7 +1090,7 @@ namespace RTC
srtp_local_salt = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH;
srtp_remote_salt = srtp_local_salt + MS_SRTP_MASTER_SALT_LENGTH;
break;
case Role::NONE:
default:
MS_ABORT("no DTLS role set");
break;
}
......@@ -1102,7 +1122,6 @@ namespace RTC
{
MS_DEBUG("role: %s | action: %s", role, SSL_state_string_long(this->ssl));
}
else if (where & SSL_CB_ALERT)
{
const char* alert_type;
......@@ -1121,7 +1140,6 @@ namespace RTC
else
MS_DEBUG("DTLS %s alert: %s", alert_type, SSL_alert_desc_string_long(ret));
}
else if (where & SSL_CB_EXIT)
{
if (ret == 0)
......@@ -1129,7 +1147,6 @@ namespace RTC
else if (ret < 0)
MS_DEBUG("role: %s | waiting for: %s", role, SSL_state_string_long(this->ssl));
}
else if (where & SSL_CB_HANDSHAKE_START)
{
MS_DEBUG("DTLS handshake start");
......@@ -1138,6 +1155,7 @@ namespace RTC
else if (where & SSL_CB_HANDSHAKE_DONE)
{
MS_DEBUG("DTLS handshake done");
this->handshakeDoneNow = true;
}
......
......@@ -304,9 +304,8 @@ namespace RTC
static const Json::StaticString k_algorithm("algorithm");
static const Json::StaticString k_value("value");
RTC::DTLSTransport::FingerprintAlgorithm fingerprint_algorithm;
std::string fingerprint_value;
RTC::DTLSTransport::Role role = RTC::DTLSTransport::Role::AUTO; // Default value.
RTC::DTLSTransport::Fingerprint remoteFingerprint;
RTC::DTLSTransport::Role remoteRole = RTC::DTLSTransport::Role::AUTO; // Default value if missing.
// Ensure this method is not called twice.
if (this->started)
......@@ -337,9 +336,9 @@ namespace RTC
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");
......@@ -347,21 +346,52 @@ namespace RTC
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())
{
role = RTC::DTLSTransport::GetRole(request->data[k_role].asString());
remoteRole = 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");
request->Reject(500, "invalid .role");
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;
}
......@@ -390,7 +420,33 @@ namespace RTC
{
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
......@@ -423,10 +479,18 @@ namespace RTC
return;
}
// TODO: remove
MS_DEBUG("received DTLS data");
if (this->dtlsTransport->IsRunning())
{
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
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论