Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IceServer crash when client uses ICE renomination #1182

Merged
merged 9 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### NEXT

* CI: Use Node.js version 20 ([PR #1177](https://github.com/versatica/mediasoup/pull/1177)).
* Fix `IceServer` crash when client uses ICE renomination ([PR #1182](https://github.com/versatica/mediasoup/pull/1182)).


### 3.12.13
Expand Down
10 changes: 6 additions & 4 deletions worker/include/RTC/IceServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ namespace RTC
}
bool IsValidTuple(const RTC::TransportTuple* tuple) const;
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.
void ForceSelectedTuple(const RTC::TransportTuple* tuple);
/**
* This should be just called in 'connected' or completed' state and the
ibc marked this conversation as resolved.
Show resolved Hide resolved
* given tuple must be an already valid tuple.
*/
void MayForceSelectedTuple(const RTC::TransportTuple* tuple);

private:
void HandleTuple(
Expand All @@ -122,8 +124,8 @@ namespace RTC
std::string password;
std::string oldUsernameFragment;
std::string oldPassword;
uint32_t remoteNomination{ 0u };
IceState state{ IceState::NEW };
uint32_t remoteNomination{ 0u };
std::list<RTC::TransportTuple> tuples;
RTC::TransportTuple* selectedTuple{ nullptr };
};
Expand Down
106 changes: 79 additions & 27 deletions worker/src/RTC/IceServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,13 @@ namespace RTC

// Authenticate the response.
if (this->oldPassword.empty())
{
response->Authenticate(this->password);
}
else
{
response->Authenticate(this->oldPassword);
}

// Send back.
response->Serialize(StunSerializeBuffer);
Expand All @@ -233,7 +237,9 @@ namespace RTC
uint32_t nomination{ 0u };

if (packet->HasNomination())
{
nomination = packet->GetNomination();
}

// Handle the tuple.
HandleTuple(tuple, packet->HasUseCandidate(), packet->HasNomination(), nomination);
Expand Down Expand Up @@ -294,7 +300,9 @@ namespace RTC

// If not found, ignore.
if (!removedTuple)
{
return;
}

// Notify the listener.
this->listener->OnIceServerTupleRemoved(this, removedTuple);
Expand All @@ -319,24 +327,35 @@ namespace RTC
{
// Update state.
this->state = IceState::DISCONNECTED;

// Reset remote nomination.
this->remoteNomination = 0u;

// Notify the listener.
this->listener->OnIceServerDisconnected(this);
}
}
}

void IceServer::ForceSelectedTuple(const RTC::TransportTuple* tuple)
void IceServer::MayForceSelectedTuple(const RTC::TransportTuple* tuple)
{
MS_TRACE();

MS_ASSERT(
this->selectedTuple, "cannot force the selected tuple if there was not a selected tuple");
if (this->state != IceState::CONNECTED && this->state != IceState::COMPLETED)
{
MS_WARN_TAG(ice, "cannot force selected tuple if not in state 'connected' or 'completed'");

return;
}

auto* storedTuple = HasTuple(tuple);

MS_ASSERT(
storedTuple,
"cannot force the selected tuple if the given tuple was not already a valid tuple");
if (!storedTuple)
{
MS_WARN_TAG(ice, "cannot force selected tuple if the given tuple was not already a valid one");

return;
}

// Mark it as selected tuple.
SetSelectedTuple(storedTuple);
Expand All @@ -351,10 +370,6 @@ namespace RTC
{
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.
MS_ASSERT(!this->selectedTuple, "state is 'new' but there is selected tuple");

Expand All @@ -373,15 +388,22 @@ namespace RTC

// Mark it as selected tuple.
SetSelectedTuple(storedTuple);

// Update state.
this->state = IceState::CONNECTED;

// Notify the listener.
this->listener->OnIceServerConnected(this);
}
else
{
// Store the tuple.
auto* storedTuple = AddTuple(tuple);
auto* storedTuple = HasTuple(tuple);

// If a new tuple store it.
if (!storedTuple)
{
storedTuple = AddTuple(tuple);
}

if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
Expand All @@ -395,11 +417,16 @@ namespace RTC

// Mark it as selected tuple.
SetSelectedTuple(storedTuple);

// Update state.
this->state = IceState::COMPLETED;

// Update nomination.
if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination;
}

// Notify the listener.
this->listener->OnIceServerCompleted(this);
}
Expand All @@ -410,12 +437,6 @@ namespace RTC

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.
MS_ASSERT(!this->selectedTuple, "state is 'disconnected' but there is selected tuple");

Expand All @@ -434,15 +455,22 @@ namespace RTC

// Mark it as selected tuple.
SetSelectedTuple(storedTuple);

// Update state.
this->state = IceState::CONNECTED;

// Notify the listener.
this->listener->OnIceServerConnected(this);
}
else
{
// Store the tuple.
auto* storedTuple = AddTuple(tuple);
auto* storedTuple = HasTuple(tuple);

// If a new tuple store it.
if (!storedTuple)
{
storedTuple = AddTuple(tuple);
}

if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
Expand All @@ -456,11 +484,16 @@ namespace RTC

// Mark it as selected tuple.
SetSelectedTuple(storedTuple);

// Update state.
this->state = IceState::COMPLETED;

// Update nomination.
if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination;
}

// Notify the listener.
this->listener->OnIceServerCompleted(this);
}
Expand All @@ -481,7 +514,9 @@ namespace RTC
{
// If a new tuple store it.
if (!HasTuple(tuple))
{
AddTuple(tuple);
}
}
else
{
Expand All @@ -497,17 +532,24 @@ namespace RTC

// If a new tuple store it.
if (!storedTuple)
{
storedTuple = AddTuple(tuple);
}

if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);

// Update state.
this->state = IceState::COMPLETED;

// Update nomination.
if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination;
}

// Notify the listener.
this->listener->OnIceServerCompleted(this);
}
Expand All @@ -528,23 +570,30 @@ namespace RTC
{
// If a new tuple store it.
if (!HasTuple(tuple))
{
AddTuple(tuple);
}
}
else
{
auto* storedTuple = HasTuple(tuple);

// If a new tuple store it.
if (!storedTuple)
{
storedTuple = AddTuple(tuple);
}

if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);

// Update nomination.
if (hasNomination && nomination > this->remoteNomination)
{
this->remoteNomination = nomination;
}
}
}

Expand All @@ -565,7 +614,9 @@ namespace RTC
// If it is UDP then we must store the remote address (until now it is
// just a pointer that will be freed soon).
if (storedTuple->GetProtocol() == TransportTuple::Protocol::UDP)
{
storedTuple->StoreUdpRemoteAddress();
}

// Notify the listener.
this->listener->OnIceServerTupleAdded(this, storedTuple);
Expand Down Expand Up @@ -614,22 +665,21 @@ namespace RTC
{
MS_TRACE();

// If there is no selected tuple yet then we know that the tuples list
// is empty.
if (!this->selectedTuple)
return nullptr;

// Check the current selected tuple.
if (this->selectedTuple->Compare(tuple))
// Check the current selected tuple (if any).
if (this->selectedTuple && this->selectedTuple->Compare(tuple))
{
return this->selectedTuple;
}

// Otherwise check other stored tuples.
for (const auto& it : this->tuples)
{
auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it));

if (storedTuple->Compare(tuple))
{
return storedTuple;
}
}

return nullptr;
Expand All @@ -641,7 +691,9 @@ namespace RTC

// If already the selected tuple do nothing.
if (storedTuple == this->selectedTuple)
{
return;
}

this->selectedTuple = storedTuple;

Expand Down
14 changes: 12 additions & 2 deletions worker/src/RTC/WebRtcTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,12 @@ namespace RTC
}

// Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple);
if (
this->iceServer->GetState() == RTC::IceServer::IceState::CONNECTED ||
this->iceServer->GetState() == RTC::IceServer::IceState::COMPLETED)
{
this->iceServer->MayForceSelectedTuple(tuple);
}
ibc marked this conversation as resolved.
Show resolved Hide resolved

// Check that DTLS status is 'connecting' or 'connected'.
if (
Expand Down Expand Up @@ -1142,7 +1147,12 @@ namespace RTC
}

// Trick for clients performing aggressive ICE regardless we are ICE-Lite.
this->iceServer->ForceSelectedTuple(tuple);
if (
this->iceServer->GetState() == RTC::IceServer::IceState::CONNECTED ||
this->iceServer->GetState() == RTC::IceServer::IceState::COMPLETED)
{
this->iceServer->MayForceSelectedTuple(tuple);
}
ibc marked this conversation as resolved.
Show resolved Hide resolved

// Pass the packet to the parent transport.
RTC::Transport::ReceiveRtpPacket(packet);
Expand Down
Loading