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

ICE renomination support (#756)

上级 55c6015e
......@@ -61,15 +61,15 @@ namespace RTC
{
return this->selectedTuple;
}
void SetUsernameFragment(const std::string& usernameFragment)
void RestartIce(const std::string& usernameFragment, const std::string& password)
{
this->oldUsernameFragment = this->usernameFragment;
this->usernameFragment = usernameFragment;
}
void SetPassword(const std::string& password)
{
this->oldPassword = this->password;
this->password = password;
this->remoteNomination = 0u;
}
bool IsValidTuple(const RTC::TransportTuple* tuple) const;
void RemoveTuple(RTC::TransportTuple* tuple);
......@@ -78,7 +78,8 @@ namespace RTC
void ForceSelectedTuple(const RTC::TransportTuple* tuple);
private:
void HandleTuple(RTC::TransportTuple* tuple, bool hasUseCandidate);
void HandleTuple(
RTC::TransportTuple* tuple, bool hasUseCandidate, bool hasNomination, uint32_t nomination);
/**
* Store the given tuple and return its stored address.
*/
......@@ -101,6 +102,7 @@ namespace RTC
std::string password;
std::string oldUsernameFragment;
std::string oldPassword;
uint32_t remoteNomination{ 0u };
IceState state{ IceState::NEW };
std::list<RTC::TransportTuple> tuples;
RTC::TransportTuple* selectedTuple{ nullptr };
......
......@@ -41,7 +41,8 @@ namespace RTC
ALTERNATE_SERVER = 0x8023,
FINGERPRINT = 0x8028,
ICE_CONTROLLED = 0x8029,
ICE_CONTROLLING = 0x802A
ICE_CONTROLLING = 0x802A,
NOMINATION = 0xC001
};
// Authentication result.
......@@ -114,6 +115,10 @@ namespace RTC
{
this->hasUseCandidate = true;
}
void SetNomination(uint32_t nomination)
{
this->nomination = nomination;
}
void SetXorMappedAddress(const struct sockaddr* xorMappedAddress)
{
this->xorMappedAddress = xorMappedAddress;
......@@ -150,6 +155,18 @@ namespace RTC
{
return this->hasUseCandidate;
}
void SetHasNomination()
{
this->hasNomination = true;
}
bool HasNomination() const
{
return this->hasNomination;
}
uint32_t GetNomination() const
{
return this->nomination;
}
uint16_t GetErrorCode() const
{
return this->errorCode;
......@@ -177,10 +194,12 @@ namespace RTC
uint8_t* data{ nullptr }; // Pointer to binary data.
size_t size{ 0u }; // The full message size (including header).
// STUN attributes.
std::string username; // Less than 513 bytes.
uint32_t priority{ 0u }; // 4 bytes unsigned integer.
uint64_t iceControlling{ 0u }; // 8 bytes unsigned integer.
uint64_t iceControlled{ 0u }; // 8 bytes unsigned integer.
std::string username; // Less than 513 bytes.
uint32_t priority{ 0u }; // 4 bytes unsigned integer.
uint64_t iceControlling{ 0u }; // 8 bytes unsigned integer.
uint64_t iceControlled{ 0u }; // 8 bytes unsigned integer.
bool hasNomination{ false };
uint32_t nomination{ 0u }; // 4 bytes unsigned integer.
bool hasUseCandidate{ false }; // 0 bytes.
const uint8_t* messageIntegrity{ nullptr }; // 20 bytes.
bool hasFingerprint{ false }; // 4 bytes.
......
......@@ -198,8 +198,13 @@ namespace RTC
delete response;
uint32_t nomination{ 0u };
if (packet->HasNomination())
nomination = packet->GetNomination();
// Handle the tuple.
HandleTuple(tuple, packet->HasUseCandidate());
HandleTuple(tuple, packet->HasUseCandidate(), packet->HasNomination(), nomination);
break;
}
......@@ -301,7 +306,8 @@ namespace RTC
SetSelectedTuple(storedTuple);
}
void IceServer::HandleTuple(RTC::TransportTuple* tuple, bool hasUseCandidate)
void IceServer::HandleTuple(
RTC::TransportTuple* tuple, bool hasUseCandidate, bool hasNomination, uint32_t nomination)
{
MS_TRACE();
......@@ -316,9 +322,15 @@ namespace RTC
// There shouldn't be a selected tuple.
MS_ASSERT(!this->selectedTuple, "state is 'new' but there is selected tuple");
if (!hasUseCandidate)
if (!hasUseCandidate && !hasNomination)
{
MS_DEBUG_TAG(ice, "transition from state 'new' to 'connected'");
MS_DEBUG_TAG(
ice,
"transition from state 'new' to 'connected' [hasUseCandidate:%s, hasNomination:%s, nomination:%" PRIu32
"]",
hasUseCandidate ? "true" : "false",
hasNomination ? "true" : "false",
nomination);
// Store the tuple.
auto* storedTuple = AddTuple(tuple);
......@@ -332,17 +344,29 @@ namespace RTC
}
else
{
MS_DEBUG_TAG(ice, "transition from state 'new' to 'completed'");
// Store the tuple.
auto* storedTuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);
// Update state.
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->OnIceServerCompleted(this);
if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
MS_DEBUG_TAG(
ice,
"transition from state 'new' to 'completed' [hasUseCandidate:%s, hasNomination:%s, nomination:%" PRIu32
"]",
hasUseCandidate ? "true" : "false",
hasNomination ? "true" : "false",
nomination);
// 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);
}
}
break;
......@@ -359,9 +383,15 @@ namespace RTC
// There shouldn't be a selected tuple.
MS_ASSERT(!this->selectedTuple, "state is 'disconnected' but there is selected tuple");
if (!hasUseCandidate)
if (!hasUseCandidate && !hasNomination)
{
MS_DEBUG_TAG(ice, "transition from state 'disconnected' to 'connected'");
MS_DEBUG_TAG(
ice,
"transition from state 'disconnected' to 'connected' [hasUseCandidate:%s, hasNomination:%s, nomination:%" PRIu32
"]",
hasUseCandidate ? "true" : "false",
hasNomination ? "true" : "false",
nomination);
// Store the tuple.
auto* storedTuple = AddTuple(tuple);
......@@ -375,17 +405,29 @@ namespace RTC
}
else
{
MS_DEBUG_TAG(ice, "transition from state 'disconnected' to 'completed'");
// Store the tuple.
auto* storedTuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);
// Update state.
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->OnIceServerCompleted(this);
if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
MS_DEBUG_TAG(
ice,
"transition from state 'disconnected' to 'completed' [hasUseCandidate:%s, hasNomination:%s, nomination:%" PRIu32
"]",
hasUseCandidate ? "true" : "false",
hasNomination ? "true" : "false",
nomination);
// 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);
}
}
break;
......@@ -399,7 +441,7 @@ namespace RTC
// There should be a selected tuple.
MS_ASSERT(this->selectedTuple, "state is 'connected' but there is not selected tuple");
if (!hasUseCandidate)
if (!hasUseCandidate && !hasNomination)
{
// If a new tuple store it.
if (!HasTuple(tuple))
......@@ -407,7 +449,13 @@ namespace RTC
}
else
{
MS_DEBUG_TAG(ice, "transition from state 'connected' to 'completed'");
MS_DEBUG_TAG(
ice,
"transition from state 'connected' to 'completed' [hasUseCandidate:%s, hasNomination:%s, nomination:%" PRIu32
"]",
hasUseCandidate ? "true" : "false",
hasNomination ? "true" : "false",
nomination);
auto* storedTuple = HasTuple(tuple);
......@@ -415,12 +463,18 @@ namespace RTC
if (!storedTuple)
storedTuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);
// Update state.
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->OnIceServerCompleted(this);
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);
}
}
break;
......@@ -434,7 +488,7 @@ namespace RTC
// There should be a selected tuple.
MS_ASSERT(this->selectedTuple, "state is 'completed' but there is not selected tuple");
if (!hasUseCandidate)
if (!hasUseCandidate && !hasNomination)
{
// If a new tuple store it.
if (!HasTuple(tuple))
......@@ -448,8 +502,14 @@ namespace RTC
if (!storedTuple)
storedTuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);
if ((hasNomination && nomination > this->remoteNomination) || !hasNomination)
{
// Mark it as selected tuple.
SetSelectedTuple(storedTuple);
// Update nomination.
if (hasNomination && nomination > this->remoteNomination)
this->remoteNomination = nomination;
}
}
break;
......
......@@ -209,6 +209,23 @@ namespace RTC
break;
}
case Attribute::NOMINATION:
{
// Ensure attribute length is 4 bytes.
if (attrLength != 4)
{
MS_WARN_TAG(ice, "attribute NOMINATION must be 4 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetHasNomination();
packet->SetNomination(Utils::Byte::Get4Bytes(attrValuePos, 0));
break;
}
case Attribute::MESSAGE_INTEGRITY:
{
// Ensure attribute length is 20 bytes.
......
......@@ -585,8 +585,7 @@ namespace RTC
std::string usernameFragment = Utils::Crypto::GetRandomString(16);
std::string password = Utils::Crypto::GetRandomString(32);
this->iceServer->SetUsernameFragment(usernameFragment);
this->iceServer->SetPassword(password);
this->iceServer->RestartIce(usernameFragment, password);
MS_DEBUG_DEV(
"WebRtcTransport ICE usernameFragment and password changed [id:%s]", this->id.c_str());
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论