Unverified 提交 b66673c6 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Fix IceServer crash when client uses ICE renomination (#1182)

上级 80d668f6
# Changelog # Changelog
### Next
* Fix `IceServer` crash when client uses ICE renomination ([PR #1182](https://github.com/versatica/mediasoup/pull/1182)).
### 3.12.15 ### 3.12.15
* Fix NPM "postinstall" task in Windows ([PR #1187](https://github.com/versatica/mediasoup/pull/1187)). * Fix NPM "postinstall" task in Windows ([PR #1187](https://github.com/versatica/mediasoup/pull/1187)).
......
...@@ -93,9 +93,11 @@ namespace RTC ...@@ -93,9 +93,11 @@ namespace RTC
} }
bool IsValidTuple(const RTC::TransportTuple* tuple) const; bool IsValidTuple(const RTC::TransportTuple* tuple) const;
void RemoveTuple(RTC::TransportTuple* tuple); void RemoveTuple(RTC::TransportTuple* tuple);
// This should be just called in 'connected' or completed' state /**
// and the given tuple must be an already valid tuple. * This should be just called in 'connected' or 'completed' state and the
void ForceSelectedTuple(const RTC::TransportTuple* tuple); * given tuple must be an already valid tuple.
*/
void MayForceSelectedTuple(const RTC::TransportTuple* tuple);
private: private:
void HandleTuple( void HandleTuple(
...@@ -122,8 +124,8 @@ namespace RTC ...@@ -122,8 +124,8 @@ namespace RTC
std::string password; std::string password;
std::string oldUsernameFragment; std::string oldUsernameFragment;
std::string oldPassword; std::string oldPassword;
uint32_t remoteNomination{ 0u };
IceState state{ IceState::NEW }; IceState state{ IceState::NEW };
uint32_t remoteNomination{ 0u };
std::list<RTC::TransportTuple> tuples; std::list<RTC::TransportTuple> tuples;
RTC::TransportTuple* selectedTuple{ nullptr }; RTC::TransportTuple* selectedTuple{ nullptr };
}; };
......
...@@ -220,9 +220,13 @@ namespace RTC ...@@ -220,9 +220,13 @@ namespace RTC
// Authenticate the response. // Authenticate the response.
if (this->oldPassword.empty()) if (this->oldPassword.empty())
{
response->Authenticate(this->password); response->Authenticate(this->password);
}
else else
{
response->Authenticate(this->oldPassword); response->Authenticate(this->oldPassword);
}
// Send back. // Send back.
response->Serialize(StunSerializeBuffer); response->Serialize(StunSerializeBuffer);
...@@ -233,7 +237,9 @@ namespace RTC ...@@ -233,7 +237,9 @@ namespace RTC
uint32_t nomination{ 0u }; uint32_t nomination{ 0u };
if (packet->HasNomination()) if (packet->HasNomination())
{
nomination = packet->GetNomination(); nomination = packet->GetNomination();
}
// Handle the tuple. // Handle the tuple.
HandleTuple(tuple, packet->HasUseCandidate(), packet->HasNomination(), nomination); HandleTuple(tuple, packet->HasUseCandidate(), packet->HasNomination(), nomination);
...@@ -294,7 +300,9 @@ namespace RTC ...@@ -294,7 +300,9 @@ namespace RTC
// If not found, ignore. // If not found, ignore.
if (!removedTuple) if (!removedTuple)
{
return; return;
}
// Notify the listener. // Notify the listener.
this->listener->OnIceServerTupleRemoved(this, removedTuple); this->listener->OnIceServerTupleRemoved(this, removedTuple);
...@@ -319,24 +327,35 @@ namespace RTC ...@@ -319,24 +327,35 @@ namespace RTC
{ {
// Update state. // Update state.
this->state = IceState::DISCONNECTED; this->state = IceState::DISCONNECTED;
// Reset remote nomination.
this->remoteNomination = 0u;
// Notify the listener. // Notify the listener.
this->listener->OnIceServerDisconnected(this); this->listener->OnIceServerDisconnected(this);
} }
} }
} }
void IceServer::ForceSelectedTuple(const RTC::TransportTuple* tuple) void IceServer::MayForceSelectedTuple(const RTC::TransportTuple* tuple)
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT( if (this->state != IceState::CONNECTED && this->state != IceState::COMPLETED)
this->selectedTuple, "cannot force the selected tuple if there was not a selected tuple"); {
MS_WARN_TAG(ice, "cannot force selected tuple if not in state 'connected' or 'completed'");
return;
}
auto* storedTuple = HasTuple(tuple); auto* storedTuple = HasTuple(tuple);
MS_ASSERT( if (!storedTuple)
storedTuple, {
"cannot force the selected tuple if the given tuple was not already a valid tuple"); MS_WARN_TAG(ice, "cannot force selected tuple if the given tuple was not already a valid one");
return;
}
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
...@@ -351,10 +370,6 @@ namespace RTC ...@@ -351,10 +370,6 @@ namespace RTC
{ {
case IceState::NEW: case IceState::NEW:
{ {
// There should be no tuples.
MS_ASSERT(
this->tuples.empty(), "state is 'new' but there are %zu tuples", this->tuples.size());
// There shouldn't be a selected tuple. // There shouldn't be a selected tuple.
MS_ASSERT(!this->selectedTuple, "state is 'new' but there is selected tuple"); MS_ASSERT(!this->selectedTuple, "state is 'new' but there is selected tuple");
...@@ -373,8 +388,10 @@ namespace RTC ...@@ -373,8 +388,10 @@ namespace RTC
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
// Update state. // Update state.
this->state = IceState::CONNECTED; this->state = IceState::CONNECTED;
// Notify the listener. // Notify the listener.
this->listener->OnIceServerConnected(this); this->listener->OnIceServerConnected(this);
} }
...@@ -395,11 +412,16 @@ namespace RTC ...@@ -395,11 +412,16 @@ namespace RTC
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
// Update state. // Update state.
this->state = IceState::COMPLETED; this->state = IceState::COMPLETED;
// Update nomination. // Update nomination.
if (hasNomination && nomination > this->remoteNomination) if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination; this->remoteNomination = nomination;
}
// Notify the listener. // Notify the listener.
this->listener->OnIceServerCompleted(this); this->listener->OnIceServerCompleted(this);
} }
...@@ -410,12 +432,6 @@ namespace RTC ...@@ -410,12 +432,6 @@ namespace RTC
case IceState::DISCONNECTED: case IceState::DISCONNECTED:
{ {
// There should be no tuples.
MS_ASSERT(
this->tuples.empty(),
"state is 'disconnected' but there are %zu tuples",
this->tuples.size());
// There shouldn't be a selected tuple. // There shouldn't be a selected tuple.
MS_ASSERT(!this->selectedTuple, "state is 'disconnected' but there is selected tuple"); MS_ASSERT(!this->selectedTuple, "state is 'disconnected' but there is selected tuple");
...@@ -434,8 +450,10 @@ namespace RTC ...@@ -434,8 +450,10 @@ namespace RTC
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
// Update state. // Update state.
this->state = IceState::CONNECTED; this->state = IceState::CONNECTED;
// Notify the listener. // Notify the listener.
this->listener->OnIceServerConnected(this); this->listener->OnIceServerConnected(this);
} }
...@@ -456,11 +474,16 @@ namespace RTC ...@@ -456,11 +474,16 @@ namespace RTC
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
// Update state. // Update state.
this->state = IceState::COMPLETED; this->state = IceState::COMPLETED;
// Update nomination. // Update nomination.
if (hasNomination && nomination > this->remoteNomination) if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination; this->remoteNomination = nomination;
}
// Notify the listener. // Notify the listener.
this->listener->OnIceServerCompleted(this); this->listener->OnIceServerCompleted(this);
} }
...@@ -479,8 +502,7 @@ namespace RTC ...@@ -479,8 +502,7 @@ namespace RTC
if (!hasUseCandidate && !hasNomination) if (!hasUseCandidate && !hasNomination)
{ {
// If a new tuple store it. // Store the tuple.
if (!HasTuple(tuple))
AddTuple(tuple); AddTuple(tuple);
} }
else else
...@@ -493,21 +515,23 @@ namespace RTC ...@@ -493,21 +515,23 @@ namespace RTC
hasNomination ? "true" : "false", hasNomination ? "true" : "false",
nomination); nomination);
auto* storedTuple = HasTuple(tuple); // Store the tuple.
auto* storedTuple = AddTuple(tuple);
// If a new tuple store it.
if (!storedTuple)
storedTuple = AddTuple(tuple);
if ((hasNomination && nomination > this->remoteNomination) || !hasNomination) if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{ {
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
// Update state. // Update state.
this->state = IceState::COMPLETED; this->state = IceState::COMPLETED;
// Update nomination. // Update nomination.
if (hasNomination && nomination > this->remoteNomination) if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination; this->remoteNomination = nomination;
}
// Notify the listener. // Notify the listener.
this->listener->OnIceServerCompleted(this); this->listener->OnIceServerCompleted(this);
} }
...@@ -526,27 +550,26 @@ namespace RTC ...@@ -526,27 +550,26 @@ namespace RTC
if (!hasUseCandidate && !hasNomination) if (!hasUseCandidate && !hasNomination)
{ {
// If a new tuple store it. // Store the tuple.
if (!HasTuple(tuple))
AddTuple(tuple); AddTuple(tuple);
} }
else else
{ {
auto* storedTuple = HasTuple(tuple); // Store the tuple.
auto* storedTuple = AddTuple(tuple);
// If a new tuple store it.
if (!storedTuple)
storedTuple = AddTuple(tuple);
if ((hasNomination && nomination > this->remoteNomination) || !hasNomination) if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{ {
// Mark it as selected tuple. // Mark it as selected tuple.
SetSelectedTuple(storedTuple); SetSelectedTuple(storedTuple);
// Update nomination. // Update nomination.
if (hasNomination && nomination > this->remoteNomination) if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination; this->remoteNomination = nomination;
} }
} }
}
break; break;
} }
...@@ -557,15 +580,26 @@ namespace RTC ...@@ -557,15 +580,26 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
auto* storedTuple = HasTuple(tuple);
if (storedTuple)
{
MS_DEBUG_DEV('tuple already exists');
return storedTuple;
}
// Add the new tuple at the beginning of the list. // Add the new tuple at the beginning of the list.
this->tuples.push_front(*tuple); this->tuples.push_front(*tuple);
auto* storedTuple = std::addressof(*this->tuples.begin()); storedTuple = std::addressof(*this->tuples.begin());
// If it is UDP then we must store the remote address (until now it is // If it is UDP then we must store the remote address (until now it is
// just a pointer that will be freed soon). // just a pointer that will be freed soon).
if (storedTuple->GetProtocol() == TransportTuple::Protocol::UDP) if (storedTuple->GetProtocol() == TransportTuple::Protocol::UDP)
{
storedTuple->StoreUdpRemoteAddress(); storedTuple->StoreUdpRemoteAddress();
}
// Notify the listener. // Notify the listener.
this->listener->OnIceServerTupleAdded(this, storedTuple); this->listener->OnIceServerTupleAdded(this, storedTuple);
...@@ -614,14 +648,11 @@ namespace RTC ...@@ -614,14 +648,11 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// If there is no selected tuple yet then we know that the tuples list // Check the current selected tuple (if any).
// is empty. if (this->selectedTuple && this->selectedTuple->Compare(tuple))
if (!this->selectedTuple) {
return nullptr;
// Check the current selected tuple.
if (this->selectedTuple->Compare(tuple))
return this->selectedTuple; return this->selectedTuple;
}
// Otherwise check other stored tuples. // Otherwise check other stored tuples.
for (const auto& it : this->tuples) for (const auto& it : this->tuples)
...@@ -629,8 +660,10 @@ namespace RTC ...@@ -629,8 +660,10 @@ namespace RTC
auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it)); auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it));
if (storedTuple->Compare(tuple)) if (storedTuple->Compare(tuple))
{
return storedTuple; return storedTuple;
} }
}
return nullptr; return nullptr;
} }
...@@ -641,7 +674,9 @@ namespace RTC ...@@ -641,7 +674,9 @@ namespace RTC
// If already the selected tuple do nothing. // If already the selected tuple do nothing.
if (storedTuple == this->selectedTuple) if (storedTuple == this->selectedTuple)
{
return; return;
}
this->selectedTuple = storedTuple; this->selectedTuple = storedTuple;
......
...@@ -1058,7 +1058,7 @@ namespace RTC ...@@ -1058,7 +1058,7 @@ namespace RTC
} }
// Trick for clients performing aggressive ICE regardless we are ICE-Lite. // Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple); this->iceServer->MayForceSelectedTuple(tuple);
// Check that DTLS status is 'connecting' or 'connected'. // Check that DTLS status is 'connecting' or 'connected'.
if ( if (
...@@ -1142,7 +1142,7 @@ namespace RTC ...@@ -1142,7 +1142,7 @@ namespace RTC
} }
// Trick for clients performing aggressive ICE regardless we are ICE-Lite. // Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple); this->iceServer->MayForceSelectedTuple(tuple);
// Pass the packet to the parent transport. // Pass the packet to the parent transport.
RTC::Transport::ReceiveRtpPacket(packet); RTC::Transport::ReceiveRtpPacket(packet);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论