提交 82752177 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

RtpPacket and StunMessage:

- Rename GetRaw() to GetData() and GetLength() to GetSize().
- Make Serialize() use an externally provided buffer.
上级 5b886c86
......@@ -35,6 +35,9 @@ namespace RTC
virtual void onIceDisconnected(IceServer* iceServer) = 0;
};
private:
static uint8_t stunSerializeBuffer[];
public:
IceServer(Listener* listener, const std::string& usernameFragment, const std::string& password);
......
......@@ -35,7 +35,7 @@ namespace RTC
struct ExtensionHeader
{
uint16_t id;
uint16_t length; // Size of value in multiples of 4 bytes.
uint16_t length; // Size of value in multiples of 4 bytes.
uint8_t* value;
};
......@@ -44,12 +44,12 @@ namespace RTC
static RtpPacket* Parse(const uint8_t* data, size_t len);
public:
RtpPacket(Header* header, ExtensionHeader* extensionHeader, const uint8_t* payload, size_t payloadLen, uint8_t payloadPadding, const uint8_t* raw, size_t length);
RtpPacket(Header* header, ExtensionHeader* extensionHeader, const uint8_t* payload, size_t payloadLen, uint8_t payloadPadding, size_t size);
~RtpPacket();
void Dump();
const uint8_t* GetRaw();
size_t GetLength();
const uint8_t* GetData();
size_t GetSize();
uint8_t GetPayloadType();
void SetPayloadType(uint8_t payload_type);
bool HasMarker();
......@@ -63,7 +63,7 @@ namespace RTC
size_t GetExtensionHeaderLength();
uint8_t* GetPayload();
size_t GetPayloadLength();
void Serialize();
void Serialize(uint8_t* buffer);
RtpPacket* Clone(uint8_t* buffer);
private:
......@@ -74,10 +74,7 @@ namespace RTC
uint8_t* payload = nullptr;
size_t payloadLength = 0;
uint8_t payloadPadding = 0;
uint8_t* raw = nullptr; // Allocated when Serialize().
size_t length = 0;
// Others.
bool isSerialized = false;
size_t size = 0; // Full size of the packet in bytes.
};
/* Inline static methods. */
......@@ -101,16 +98,15 @@ namespace RTC
/* Inline instance methods. */
inline
const uint8_t* RtpPacket::GetRaw()
const uint8_t* RtpPacket::GetData()
{
// Return pointer to the serialized data or to the original raw data.
return this->raw ? this->raw : (const uint8_t*)this->header;
return (const uint8_t*)this->header;
}
inline
size_t RtpPacket::GetLength()
size_t RtpPacket::GetSize()
{
return this->length;
return this->size;
}
inline
......
......@@ -60,15 +60,14 @@ namespace RTC
static const uint8_t magicCookie[];
public:
StunMessage(Class klass, Method method, const uint8_t* transactionId, const uint8_t* raw, size_t length);
StunMessage(Class klass, Method method, const uint8_t* transactionId, const uint8_t* data, size_t size);
~StunMessage();
void Dump();
Class GetClass();
Method GetMethod();
const uint8_t* GetRaw();
void SetLength(size_t length);
size_t GetLength();
const uint8_t* GetData();
size_t GetSize();
void SetUsername(const char* username, size_t len);
void SetPriority(const uint32_t priority);
void SetIceControlling(const uint64_t iceControlling);
......@@ -90,27 +89,25 @@ namespace RTC
StunMessage* CreateSuccessResponse();
StunMessage* CreateErrorResponse(uint16_t errorCode);
void Authenticate(const std::string &password);
void Serialize();
void Serialize(uint8_t* buffer);
private:
// Passed by argument.
Class klass; // 2 bytes.
Method method; // 2 bytes.
const uint8_t* transactionId = nullptr; // 12 bytes.
uint8_t* raw = nullptr; // Allocated when Serialize().
size_t length = 0; // The full message size (including header).
// Others.
bool isSerialized = false;
Class klass; // 2 bytes.
Method method; // 2 bytes.
const uint8_t* transactionId = nullptr; // 12 bytes.
uint8_t* data = nullptr; // Pointer to binary data.
size_t size = 0; // The full message size (including header).
// STUN attributes.
std::string username; // Less than 513 bytes.
uint32_t priority = 0; // 4 bytes unsigned integer.
uint64_t iceControlling = 0; // 8 bytes unsigned integer.
uint64_t iceControlled = 0; // 8 bytes unsigned integer.
bool hasUseCandidate = false; // 0 bytes.
const uint8_t* messageIntegrity = nullptr; // 20 bytes.
bool hasFingerprint = false; // 4 bytes.
const struct sockaddr* xorMappedAddress = nullptr; // 8 or 20 bytes.
uint16_t errorCode = 0; // 4 bytes (no reason phrase).
std::string username; // Less than 513 bytes.
uint32_t priority = 0; // 4 bytes unsigned integer.
uint64_t iceControlling = 0; // 8 bytes unsigned integer.
uint64_t iceControlled = 0; // 8 bytes unsigned integer.
bool hasUseCandidate = false; // 0 bytes.
const uint8_t* messageIntegrity = nullptr; // 20 bytes.
bool hasFingerprint = false; // 4 bytes.
const struct sockaddr* xorMappedAddress = nullptr; // 8 or 20 bytes.
uint16_t errorCode = 0; // 4 bytes (no reason phrase).
std::string password;
};
......@@ -145,21 +142,15 @@ namespace RTC
}
inline
const uint8_t* StunMessage::GetRaw()
const uint8_t* StunMessage::GetData()
{
return this->raw;
return this->data;
}
inline
void StunMessage::SetLength(size_t length)
size_t StunMessage::GetSize()
{
this->length = length;
}
inline
size_t StunMessage::GetLength()
{
return this->length;
return this->size;
}
inline
......
......@@ -4,8 +4,14 @@
#include "RTC/IceServer.h"
#include "Logger.h"
#define MS_STUN_SERIALIZE_BUFFER_SIZE 65536
namespace RTC
{
/* Class variables. */
uint8_t IceServer::stunSerializeBuffer[MS_STUN_SERIALIZE_BUFFER_SIZE];
/* Instance methods. */
IceServer::IceServer(Listener* listener, const std::string& usernameFragment, const std::string& password) :
......@@ -38,7 +44,7 @@ namespace RTC
// Reply 400.
RTC::StunMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
}
......@@ -59,7 +65,7 @@ namespace RTC
// Reply 400.
RTC::StunMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
}
......@@ -82,7 +88,7 @@ namespace RTC
// Reply 400.
RTC::StunMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
......@@ -101,7 +107,7 @@ namespace RTC
// Reply 401.
RTC::StunMessage* response = msg->CreateErrorResponse(401);
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
......@@ -114,7 +120,7 @@ namespace RTC
// Reply 400.
RTC::StunMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
......@@ -129,7 +135,7 @@ namespace RTC
// Reply 487 (Role Conflict).
RTC::StunMessage* response = msg->CreateErrorResponse(487);
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
......@@ -148,7 +154,7 @@ namespace RTC
response->Authenticate(this->password);
// Send back.
response->Serialize();
response->Serialize(IceServer::stunSerializeBuffer);
this->listener->onOutgoingStunMessage(this, response, tuple);
delete response;
......
......@@ -108,34 +108,30 @@ namespace RTC
if (payloadLength == 0)
payload = nullptr;
MS_ASSERT(len == sizeof(Header) + csrc_list_size + (extensionHeader ? 4 + extension_value_size : 0) + payloadLength + (size_t)payloadPadding, "packet's computed length does not match received length");
MS_ASSERT(len == sizeof(Header) + csrc_list_size + (extensionHeader ? 4 + extension_value_size : 0) + payloadLength + (size_t)payloadPadding, "packet's computed size does not match received size");
return new RtpPacket(header, extensionHeader, payload, payloadLength, payloadPadding, data, len);
return new RtpPacket(header, extensionHeader, payload, payloadLength, payloadPadding, len);
}
/* Instance methods. */
RtpPacket::RtpPacket(Header* header, ExtensionHeader* extensionHeader, const uint8_t* payload, size_t payloadLength, uint8_t payloadPadding, const uint8_t* raw, size_t length) :
RtpPacket::RtpPacket(Header* header, ExtensionHeader* extensionHeader, const uint8_t* payload, size_t payloadLength, uint8_t payloadPadding, size_t size) :
header(header),
extensionHeader(extensionHeader),
payload((uint8_t*)payload),
payloadLength(payloadLength),
payloadPadding(payloadPadding),
raw((uint8_t*)raw),
length(length)
size(size)
{
MS_TRACE();
if (this->header->csrc_count)
this->csrcList = (uint8_t*)raw + sizeof(Header);
this->csrcList = (uint8_t*)header + sizeof(Header);
}
RtpPacket::~RtpPacket()
{
MS_TRACE();
if (this->isSerialized)
delete this->raw;
}
void RtpPacket::Dump()
......@@ -164,83 +160,73 @@ namespace RTC
#endif
}
void RtpPacket::Serialize()
void RtpPacket::Serialize(uint8_t* buffer)
{
MS_TRACE();
if (this->isSerialized)
delete this->raw;
// Set this->isSerialized so the destructor will free the buffer.
this->isSerialized = true;
// Some useful variables.
size_t extension_value_size = 0;
// First calculate the total required size for the entire message.
this->length = sizeof(Header); // Minimum header.
this->size = sizeof(Header); // Minimum header.
if (this->csrcList)
this->length += this->header->csrc_count * sizeof(header->ssrc);
this->size += this->header->csrc_count * sizeof(header->ssrc);
if (this->extensionHeader)
{
extension_value_size = (size_t)(ntohs(this->extensionHeader->length) * 4);
this->length += 4 + extension_value_size;
this->size += 4 + extension_value_size;
}
this->length += this->payloadLength;
this->size += this->payloadLength;
this->length += (size_t)this->payloadPadding;
// Allocate it.
this->raw = new uint8_t[this->length];
this->size += (size_t)this->payloadPadding;
// Add minimum header.
std::memcpy(this->raw, this->header, sizeof(Header));
std::memcpy(buffer, this->header, sizeof(Header));
// Update the header pointer.
this->header = (Header*)(this->raw);
this->header = (Header*)buffer;
size_t pos = sizeof(Header);
// Add CSRC list.
if (this->csrcList)
{
std::memcpy(this->raw + pos, this->csrcList, this->header->csrc_count * sizeof(this->header->ssrc));
std::memcpy(buffer + pos, this->csrcList, this->header->csrc_count * sizeof(this->header->ssrc));
// Update the pointer.
this->csrcList = this->raw + pos;
this->csrcList = buffer + pos;
pos += this->header->csrc_count * sizeof(this->header->ssrc);
}
// Add extension header.
if (this->extensionHeader)
{
std::memcpy(this->raw + pos, this->extensionHeader, 4 + extension_value_size);
std::memcpy(buffer + pos, this->extensionHeader, 4 + extension_value_size);
// Update the header extension pointer.
this->extensionHeader = (ExtensionHeader*)(this->raw + pos);
this->extensionHeader = (ExtensionHeader*)(buffer + pos);
pos += 4 + extension_value_size;
}
// Add payload.
if (this->payload)
{
std::memcpy(this->raw + pos, this->payload, this->payloadLength);
std::memcpy(buffer + pos, this->payload, this->payloadLength);
// Update the payload pointer.
this->payload = this->raw + pos;
this->payload = buffer + pos;
pos += this->payloadLength;
}
// Add payload padding.
if (this->payloadPadding)
{
this->raw[pos + (size_t)this->payloadPadding - 1] = this->payloadPadding;
buffer[pos + (size_t)this->payloadPadding - 1] = this->payloadPadding;
pos += (size_t)this->payloadPadding;
}
MS_ASSERT(pos == this->length, "pos != this->length");
MS_ASSERT(pos == this->size, "pos != this->size");
}
RtpPacket* RtpPacket::Clone(uint8_t* buffer)
......@@ -248,7 +234,7 @@ namespace RTC
MS_TRACE();
// Copy the full packet into the given buffer.
std::memcpy(buffer, GetRaw(), GetLength());
std::memcpy(buffer, GetData(), GetSize());
// Set header pointer pointing to the given buffer.
Header* header = (Header*)buffer;
......@@ -285,9 +271,9 @@ namespace RTC
pos += (size_t)this->payloadPadding;
}
MS_ASSERT(pos == this->length, "pos != this->length");
MS_ASSERT(pos == this->size, "pos != this->size");
// Create the new RtpPacket instance and return it.
return new RtpPacket(header, extensionHeader, payload, this->payloadLength, this->payloadPadding, buffer, this->length);
return new RtpPacket(header, extensionHeader, payload, this->payloadLength, this->payloadPadding, this->size);
}
}
......@@ -246,7 +246,7 @@ namespace RTC
event_data[k_class] = "RtpReceiver";
this->notifier->EmitWithBinary(this->rtpReceiverId, "rtpraw", event_data, packet->GetRaw(), packet->GetLength());
this->notifier->EmitWithBinary(this->rtpReceiverId, "rtpraw", event_data, packet->GetData(), packet->GetSize());
}
// Emit "rtpobject" is enabled.
......
......@@ -322,7 +322,7 @@ namespace RTC
auto first_packet = first_buffer_item.packet;
// Store points to the store used by the first packet.
store = (uint8_t*)first_packet->GetRaw();
store = (uint8_t*)first_packet->GetData();
// Free the first packet.
delete first_packet;
// Remove the first element in the list.
......
......@@ -4,8 +4,8 @@
#include "RTC/StunMessage.h"
#include "Utils.h"
#include "Logger.h"
#include <cstdio> // std::snprintf()
#include <cstring> // std::memcmp(), std::memcpy()
#include <cstdio> // std::snprintf()
#include <cstring> // std::memcmp(), std::memcpy()
namespace RTC
{
......@@ -88,8 +88,8 @@ namespace RTC
// Flags (positions) for special MESSAGE-INTEGRITY and FINGERPRINT attributes.
bool has_message_integrity = false;
bool has_fingerprint = false;
size_t fingerprint_attr_pos; // Will point to the beginning of the attribute.
uint32_t fingerprint; // Holds the value of the FINGERPRINT attribute.
size_t fingerprint_attr_pos; // Will point to the beginning of the attribute.
uint32_t fingerprint; // Holds the value of the FINGERPRINT attribute.
// Ensure there are at least 4 remaining bytes (attribute with 0 length).
while (pos + 4 <= len)
......@@ -225,12 +225,12 @@ namespace RTC
/* Instance methods. */
StunMessage::StunMessage(Class klass, Method method, const uint8_t* transactionId, const uint8_t* raw, size_t length) :
StunMessage::StunMessage(Class klass, Method method, const uint8_t* transactionId, const uint8_t* data, size_t size) :
klass(klass),
method(method),
transactionId(transactionId),
raw((uint8_t*)raw),
length(length)
data((uint8_t*)data),
size(size)
{
MS_TRACE();
}
......@@ -238,16 +238,16 @@ namespace RTC
StunMessage::~StunMessage()
{
MS_TRACE();
if (this->isSerialized)
delete this->raw;
}
void StunMessage::Dump()
{
#ifdef MS_LOG_DEV
MS_TRACE();
MS_DEBUG_DEV("<StunMessage>");
std::string klass;
switch (this->klass)
{
......@@ -261,7 +261,7 @@ namespace RTC
else
// This prints the unknown method number. Example: TURN Allocate => 0x003.
MS_DEBUG_DEV(" %s with unknown method %#.3x", klass.c_str(), (uint16_t)this->method);
MS_DEBUG_DEV(" length (with header): %zu bytes", this->length);
MS_DEBUG_DEV(" size: %zu bytes", this->size);
char transaction_id[25];
for (int i=0; i<12; i++)
{
......@@ -302,7 +302,10 @@ namespace RTC
}
if (this->hasFingerprint)
MS_DEBUG_DEV(" fingerprint");
MS_DEBUG_DEV("</StunMessage>");
#endif
}
StunMessage::Authentication StunMessage::CheckAuthentication(const std::string &local_username, const std::string &local_password)
......@@ -343,11 +346,11 @@ namespace RTC
// If there is FINGERPRINT it must be discarded for MESSAGE-INTEGRITY calculation,
// so the header length field must be modified (and later restored).
if (this->hasFingerprint)
// Set the length: full length - header length (20) - FINGERPRINT length (8).
Utils::Byte::Set2Bytes(this->raw, 2, (uint16_t)(this->length - 20 - 8));
// Set the header length field: full size - header length (20) - FINGERPRINT length (8).
Utils::Byte::Set2Bytes(this->data, 2, (uint16_t)(this->size - 20 - 8));
// Calculate the HMAC-SHA1 of the message according to MESSAGE-INTEGRITY rules.
const uint8_t* computed_message_integrity = Utils::Crypto::GetHMAC_SHA1(local_password, this->raw, (this->messageIntegrity - 4) - this->raw);
const uint8_t* computed_message_integrity = Utils::Crypto::GetHMAC_SHA1(local_password, this->data, (this->messageIntegrity - 4) - this->data);
Authentication result;
......@@ -359,7 +362,7 @@ namespace RTC
// Restore the header length field.
if (this->hasFingerprint)
Utils::Byte::Set2Bytes(this->raw, 2, (uint16_t)(this->length - 20));
Utils::Byte::Set2Bytes(this->data, 2, (uint16_t)(this->size - 20));
return result;
}
......@@ -399,44 +402,41 @@ namespace RTC
this->password = password;
}
void StunMessage::Serialize()
void StunMessage::Serialize(uint8_t* buffer)
{
MS_TRACE();
if (this->isSerialized)
delete this->raw;
// Set this->isSerialized so the destructor will free the buffer.
this->isSerialized = true;
// Some useful variables.
uint16_t username_padded_len = 0;
uint16_t xor_mapped_address_padded_len = 0;
bool add_xor_mapped_address = (this->xorMappedAddress && this->method == StunMessage::Method::Binding && this->klass == Class::SuccessResponse);
bool add_error_code = (this->errorCode && this->klass == Class::ErrorResponse);
bool add_message_integrity = (this->klass != Class::ErrorResponse && !this->password.empty());
bool add_fingerprint = true; // Do always.
bool add_fingerprint = true; // Do always.
// Update data pointer.
this->data = buffer;
// First calculate the total required size for the entire message.
this->length = 20; // Header.
this->size = 20; // Header.
if (!this->username.empty())
{
username_padded_len = Utils::Byte::PadTo4Bytes((uint16_t)this->username.length());
this->length += 4 + username_padded_len;
this->size += 4 + username_padded_len;
}
if (this->priority)
this->length += 4 + 4;
this->size += 4 + 4;
if (this->iceControlling)
this->length += 4 + 8;
this->size += 4 + 8;
if (this->iceControlled)
this->length += 4 + 8;
this->size += 4 + 8;
if (this->hasUseCandidate)
this->length += 4;
this->size += 4;
if (add_xor_mapped_address)
{
......@@ -445,14 +445,14 @@ namespace RTC
case AF_INET:
{
xor_mapped_address_padded_len = 8;
this->length += 4 + 8;
this->size += 4 + 8;
break;
}
case AF_INET6:
{
xor_mapped_address_padded_len = 20;
this->length += 4 + 20;
this->size += 4 + 20;
break;
}
......@@ -466,16 +466,13 @@ namespace RTC
}
if (add_error_code)
this->length += 4 + 4;
this->size += 4 + 4;
if (add_message_integrity)
this->length += 4 + 20;
this->size += 4 + 20;
if (add_fingerprint)
this->length += 4 + 4;
// Allocate it.
this->raw = new uint8_t[this->length];
this->size += 4 + 4;
// Merge class and method fields into type.
uint16_t type_field = ((uint16_t)this->method & 0x0f80) << 2;
......@@ -485,19 +482,19 @@ namespace RTC
type_field |= ((uint16_t)this->klass & 0x01) << 4;
// Set type field.
Utils::Byte::Set2Bytes(this->raw, 0, type_field);
Utils::Byte::Set2Bytes(buffer, 0, type_field);
// Set length field.
Utils::Byte::Set2Bytes(this->raw, 2, (uint16_t)this->length - 20);
Utils::Byte::Set2Bytes(buffer, 2, (uint16_t)this->size - 20);
// Set magic cookie.
std::memcpy(this->raw + 4, StunMessage::magicCookie, 4);
std::memcpy(buffer + 4, StunMessage::magicCookie, 4);
// Set TransactionId field.
std::memcpy(this->raw + 8, this->transactionId, 12);
std::memcpy(buffer + 8, this->transactionId, 12);
// Update the transaction ID pointer.
this->transactionId = this->raw + 8;
this->transactionId = buffer + 8;
// Add atributes.
size_t pos = 20;
......@@ -505,53 +502,53 @@ namespace RTC
// Add USERNAME.
if (username_padded_len)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::Username);
Utils::Byte::Set2Bytes(this->raw, pos + 2, (uint16_t)this->username.length());
std::memcpy(this->raw + pos + 4, this->username.c_str(), this->username.length());
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::Username);
Utils::Byte::Set2Bytes(buffer, pos + 2, (uint16_t)this->username.length());
std::memcpy(buffer + pos + 4, this->username.c_str(), this->username.length());
pos += 4 + username_padded_len;
}
// Add PRIORITY.
if (this->priority)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::Priority);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 4);
Utils::Byte::Set4Bytes(this->raw, pos + 4, this->priority);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::Priority);
Utils::Byte::Set2Bytes(buffer, pos + 2, 4);
Utils::Byte::Set4Bytes(buffer, pos + 4, this->priority);
pos += 4 + 4;
}
// Add ICE-CONTROLLING.
if (this->iceControlling)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::IceControlling);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 8);
Utils::Byte::Set8Bytes(this->raw, pos + 4, this->iceControlling);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::IceControlling);
Utils::Byte::Set2Bytes(buffer, pos + 2, 8);
Utils::Byte::Set8Bytes(buffer, pos + 4, this->iceControlling);
pos += 4 + 8;
}
// Add ICE-CONTROLLED.
if (this->iceControlled)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::IceControlled);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 8);
Utils::Byte::Set8Bytes(this->raw, pos + 4, this->iceControlled);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::IceControlled);
Utils::Byte::Set2Bytes(buffer, pos + 2, 8);
Utils::Byte::Set8Bytes(buffer, pos + 4, this->iceControlled);
pos += 4 + 8;
}
// Add USE-CANDIDATE.
if (this->hasUseCandidate)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::UseCandidate);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 0);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::UseCandidate);
Utils::Byte::Set2Bytes(buffer, pos + 2, 0);
pos += 4;
}
// Add XOR-MAPPED-ADDRESS
if (add_xor_mapped_address)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::XorMappedAddress);
Utils::Byte::Set2Bytes(this->raw, pos + 2, xor_mapped_address_padded_len);
uint8_t* attr_value = this->raw + pos + 4;
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::XorMappedAddress);
Utils::Byte::Set2Bytes(buffer, pos + 2, xor_mapped_address_padded_len);
uint8_t* attr_value = buffer + pos + 4;
switch (this->xorMappedAddress->sa_family)
{
case AF_INET:
......@@ -614,13 +611,13 @@ namespace RTC
// Add ERROR-CODE.
if (add_error_code)
{
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::ErrorCode);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 4);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::ErrorCode);
Utils::Byte::Set2Bytes(buffer, pos + 2, 4);
uint8_t code_class = (uint8_t)(this->errorCode / 100);
uint8_t code_number = (uint8_t)this->errorCode - (code_class * 100);
Utils::Byte::Set2Bytes(this->raw, pos + 4, 0);
Utils::Byte::Set1Byte(this->raw, pos + 6, code_class);
Utils::Byte::Set1Byte(this->raw, pos + 7, code_number);
Utils::Byte::Set2Bytes(buffer, pos + 4, 0);
Utils::Byte::Set1Byte(buffer, pos + 6, code_class);
Utils::Byte::Set1Byte(buffer, pos + 7, code_number);
pos += 4 + 4;
}
......@@ -629,22 +626,22 @@ namespace RTC
{
// Ignore FINGERPRINT.
if (add_fingerprint)
Utils::Byte::Set2Bytes(this->raw, 2, (uint16_t)(this->length - 20 - 8));
Utils::Byte::Set2Bytes(buffer, 2, (uint16_t)(this->size - 20 - 8));
// Calculate the HMAC-SHA1 of the message according to MESSAGE-INTEGRITY rules.
const uint8_t* computed_message_integrity = Utils::Crypto::GetHMAC_SHA1(this->password, this->raw, pos);
const uint8_t* computed_message_integrity = Utils::Crypto::GetHMAC_SHA1(this->password, buffer, pos);
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::MessageIntegrity);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 20);
std::memcpy(this->raw + pos + 4, computed_message_integrity, 20);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::MessageIntegrity);
Utils::Byte::Set2Bytes(buffer, pos + 2, 20);
std::memcpy(buffer + pos + 4, computed_message_integrity, 20);
// Update the pointer.
this->messageIntegrity = this->raw + pos + 4;
this->messageIntegrity = buffer + pos + 4;
pos += 4 + 20;
// Restore length field.
if (add_fingerprint)
Utils::Byte::Set2Bytes(this->raw, 2, (uint16_t)(this->length - 20));
Utils::Byte::Set2Bytes(buffer, 2, (uint16_t)(this->size - 20));
}
else
{
......@@ -657,11 +654,11 @@ namespace RTC
{
// Compute the CRC32 of the message up to (but excluding) the FINGERPRINT
// attribute and XOR it with 0x5354554e.
uint32_t computed_fingerprint = Utils::Crypto::GetCRC32(this->raw, pos) ^ 0x5354554e;
uint32_t computed_fingerprint = Utils::Crypto::GetCRC32(buffer, pos) ^ 0x5354554e;
Utils::Byte::Set2Bytes(this->raw, pos, (uint16_t)Attribute::Fingerprint);
Utils::Byte::Set2Bytes(this->raw, pos + 2, 4);
Utils::Byte::Set4Bytes(this->raw, pos + 4, computed_fingerprint);
Utils::Byte::Set2Bytes(buffer, pos, (uint16_t)Attribute::Fingerprint);
Utils::Byte::Set2Bytes(buffer, pos + 2, 4);
Utils::Byte::Set4Bytes(buffer, pos + 4, computed_fingerprint);
pos += 4 + 4;
// Set flag.
this->hasFingerprint = true;
......@@ -671,6 +668,6 @@ namespace RTC
this->hasFingerprint = false;
}
MS_ASSERT(pos == this->length, "pos != this->length");
MS_ASSERT(pos == this->size, "pos != this->size");
}
}
......@@ -505,8 +505,8 @@ namespace RTC
return;
}
const uint8_t* data = packet->GetRaw();
size_t len = packet->GetLength();
const uint8_t* data = packet->GetData();
size_t len = packet->GetSize();
if (!this->srtpSendSession->EncryptRtp(&data, &len))
return;
......@@ -857,7 +857,7 @@ namespace RTC
MS_TRACE();
// Send the STUN response over the same transport tuple.
tuple->Send(msg->GetRaw(), msg->GetLength());
tuple->Send(msg->GetData(), msg->GetSize());
}
void Transport::onIceSelectedTuple(RTC::IceServer* iceServer, RTC::TransportTuple* tuple)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论