1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #1638, #307, merge john, add NACK suport, remove debug code, verbose log

This commit is contained in:
winlin 2020-03-14 22:51:35 +08:00
commit 60c8b37f05
10 changed files with 421 additions and 134 deletions

View file

@ -12,23 +12,6 @@ using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_kernel_utility.hpp>
static string dump_string_hex(const std::string& str, const int& max_len = 128)
{
char buf[1024*16];
int len = 0;
for (int i = 0; i < str.size() && i < max_len; ++i) {
int nb = snprintf(buf + len, sizeof(buf) - len - 1, "%02X ", (uint8_t)str[i]);
if (nb <= 0)
break;
len += nb;
}
buf[len] = '\0';
return string(buf, len);
}
static srs_error_t hmac_encode(const std::string& algo, const char* key, const int& key_length,
const char* input, const int input_length, char* output, unsigned int& output_length)
{
@ -95,16 +78,11 @@ srs_error_t SrsStunPacket::decode(const char* buf, const int nb_buf)
return srs_error_wrap(err, "invalid stun packet, size=%d", stream->size());
}
srs_trace("stun packet, nb_buf=%d", nb_buf);
message_type = stream->read_2bytes();
uint16_t message_len = stream->read_2bytes();
string magic_cookie = stream->read_string(4);
transcation_id = stream->read_string(12);
srs_trace("message_type=%u, message_len=%u, magic_cookie=%s, transcation_id=%s",
message_type, message_len, magic_cookie.c_str(), transcation_id.c_str());
if (nb_buf != 20 + message_len) {
return srs_error_wrap(err, "invalid stun packet, message_len=%d, nb_buf=%d", message_len, nb_buf);
}
@ -113,8 +91,6 @@ srs_error_t SrsStunPacket::decode(const char* buf, const int nb_buf)
uint16_t type = stream->read_2bytes();
uint16_t len = stream->read_2bytes();
srs_trace("type=%u, len=%u", type, len);
if (stream->left() < len) {
return srs_error_wrap(err, "invalid stun packet");
}
@ -124,17 +100,15 @@ srs_error_t SrsStunPacket::decode(const char* buf, const int nb_buf)
if (len % 4 != 0) {
stream->read_string(4 - (len % 4));
}
//srs_trace("val=%s", val.c_str());
switch (type) {
// FIXME: enum
case 6: {
case Username: {
username = val;
size_t p = val.find(":");
if (p != string::npos) {
local_ufrag = val.substr(0, p);
remote_ufrag = val.substr(p + 1);
srs_trace("stun packet local_ufrag=%s, remote_ufrag=%s", local_ufrag.c_str(), remote_ufrag.c_str());
srs_verbose("stun packet local_ufrag=%s, remote_ufrag=%s", local_ufrag.c_str(), remote_ufrag.c_str());
}
break;
}
@ -168,7 +142,7 @@ srs_error_t SrsStunPacket::encode_binding_response(const string& pwd, SrsBuffer*
stream->write_2bytes(BindingResponse);
stream->write_2bytes(property_username.size() + mapped_address.size());
stream->write_4bytes(0x2112A442);
stream->write_4bytes(kStunMagicCookie);
stream->write_string(transcation_id);
stream->write_string(property_username);
stream->write_string(mapped_address);
@ -226,22 +200,12 @@ string SrsStunPacket::encode_mapped_address()
SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf));
SrsAutoFree(SrsBuffer, stream);
uint32_t magic_cookie = 0x2112A442;
#if 1
stream->write_2bytes(XorMappedAddress);
stream->write_2bytes(8);
stream->write_1bytes(0); // ignore this bytes
stream->write_1bytes(1); // ipv4 family
stream->write_2bytes(mapped_port ^ (magic_cookie >> 16));
stream->write_4bytes(mapped_address ^ magic_cookie);
#else
stream->write_2bytes(MappedAddress);
stream->write_2bytes(8);
stream->write_1bytes(0); // ignore this bytes
stream->write_1bytes(1); // ipv4 family
stream->write_2bytes(mapped_port);
stream->write_4bytes(mapped_address);
#endif
stream->write_2bytes(mapped_port ^ (kStunMagicCookie >> 16));
stream->write_4bytes(mapped_address ^ kStunMagicCookie);
return string(stream->data(), stream->pos());
}

View file

@ -31,6 +31,10 @@
class SrsBuffer;
// @see: https://tools.ietf.org/html/rfc5389
// The magic cookie field MUST contain the fixed value 0x2112A442 in network byte order
const uint32_t kStunMagicCookie = 0x2112A442;
enum SrsStunMessageType
{
// see @ https://tools.ietf.org/html/rfc3489#section-11.1