This is a recommit of

https://webrtc.googlesource.com/src.git/+/26246cac660a95f439b7d1c593edec2929806d3f
that was reverted due to compile error on windows.

Changes since last is an addition of a cast to uint16_t in stun.cc:1018.

---

Add RelayPortFactoryInterface that allows for custom relay (e.g turn) ports

This patch adds a RelayPortFactoryInterface that allows
for custom relay ports. The factor is added as optional argument
to BasicPortAlloctor. If none is provided a default implementation
that mimics existing behavior is created.

The patch also adds 2 stun functions, namely to copy a
StunAttribute and to remove StunAttribute's from a StunMessage.

Bug: webrtc:8640
Change-Id: If23638317130060286f576c94401de55c60a1821
Reviewed-on: https://webrtc-review.googlesource.com/34181
Reviewed-by: Guido Urdaneta <guidou@webrtc.org>
Reviewed-by: Peter Thatcher <pthatcher@webrtc.org>
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21345}
This commit is contained in:
Jonas Oreland
2017-12-18 12:10:43 +01:00
committed by Commit Bot
parent 56adc122cf
commit 202994ca64
13 changed files with 456 additions and 40 deletions

View File

@ -92,6 +92,26 @@ void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) {
attrs_.push_back(std::move(attr));
}
std::unique_ptr<StunAttribute> StunMessage::RemoveAttribute(int type) {
std::unique_ptr<StunAttribute> attribute;
for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) {
if ((* it)->type() == type) {
attribute = std::move(* it);
attrs_.erase(std::next(it).base());
break;
}
}
if (attribute) {
attribute->SetOwner(nullptr);
size_t attr_length = attribute->length();
if (attr_length % 4 != 0) {
attr_length += (4 - (attr_length % 4));
}
length_ -= static_cast<uint16_t>(attr_length + 4);
}
return attribute;
}
const StunAddressAttribute* StunMessage::GetAddress(int type) const {
switch (type) {
case STUN_ATTR_MAPPED_ADDRESS: {
@ -984,6 +1004,35 @@ bool ComputeStunCredentialHash(const std::string& username,
return true;
}
std::unique_ptr<StunAttribute> CopyStunAttribute(
const StunAttribute& attribute,
rtc::ByteBufferWriter* tmp_buffer_ptr) {
ByteBufferWriter tmpBuffer;
if (tmp_buffer_ptr == nullptr) {
tmp_buffer_ptr = &tmpBuffer;
}
std::unique_ptr<StunAttribute> copy(
StunAttribute::Create(attribute.value_type(),
attribute.type(),
static_cast<uint16_t>(attribute.length()),
nullptr));
if (!copy) {
return nullptr;
}
tmp_buffer_ptr->Clear();
if (!attribute.Write(tmp_buffer_ptr)) {
return nullptr;
}
rtc::ByteBufferReader reader(*tmp_buffer_ptr);
if (!copy->Read(&reader)) {
return nullptr;
}
return copy;
}
StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const {
switch (type) {
case STUN_ATTR_LIFETIME: