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