Skip to content

Commit

Permalink
Metrics: fixed payload length validation (#69).
Browse files Browse the repository at this point in the history
Also added ENABLE_AEAD_API_PREVIEW.
  • Loading branch information
maxsharabayko authored Dec 23, 2022
1 parent abc18d0 commit 0bc23d9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
6 changes: 6 additions & 0 deletions xtransmit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ if (ENABLE_BONDING OR ENABLE_EXPERIMENTAL_BONDING)
)
endif()

if (ENABLE_AEAD_API_PREVIEW)
target_compile_options(srt-xtransmit
PRIVATE "-DENABLE_AEAD_API_PREVIEW"
)
endif()

if (ENABLE_CXX17)
set(REQUIRE_CXX_VER 17)
if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU|Intel")
Expand Down
24 changes: 14 additions & 10 deletions xtransmit/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ void write_sysclock_timestamp(vector<char>& payload)
*(reinterpret_cast<int64_t*>(payload.data() + SYS_TIMESTAMP_BYTE_OFFSET)) = elapsed_us.count();
}

system_clock::time_point read_sysclock_timestamp(const vector<char>& payload)
system_clock::time_point read_sysclock_timestamp(const const_buffer& payload)
{
const int64_t elapsed_us = *(reinterpret_cast<const int64_t*>(payload.data() + SYS_TIMESTAMP_BYTE_OFFSET));
const int8_t* ptr = reinterpret_cast<const int8_t*>(payload.data());
const int64_t elapsed_us = *(reinterpret_cast<const int64_t*>(ptr + SYS_TIMESTAMP_BYTE_OFFSET));

// auto in_time_t = std::chrono::system_clock::to_time_t(system_clock::time_point() + microseconds(elapsed_us));
// std::stringstream ss;
Expand All @@ -79,9 +80,10 @@ void write_steadyclock_timestamp(vector<char>& payload)
*(reinterpret_cast<int64_t*>(payload.data() + STD_TIMESTAMP_BYTE_OFFSET)) = elapsed_us.count();
}

steady_clock::time_point read_stdclock_timestamp(const vector<char>& payload)
steady_clock::time_point read_stdclock_timestamp(const const_buffer& payload)
{
const int64_t elapsed_us = *(reinterpret_cast<const int64_t*>(payload.data() + STD_TIMESTAMP_BYTE_OFFSET));
const int8_t* ptr = reinterpret_cast<const int8_t*>(payload.data());
const int64_t elapsed_us = *(reinterpret_cast<const int64_t*>(ptr + STD_TIMESTAMP_BYTE_OFFSET));
const auto std_timestamp = steady_clock::time_point() + microseconds(elapsed_us);
return std_timestamp;
}
Expand All @@ -92,9 +94,10 @@ void write_packet_seqno(vector<char>& payload, uint64_t seqno)
*ptr = seqno;
}

uint64_t read_packet_seqno(const vector<char>& payload)
uint64_t read_packet_seqno(const const_buffer& payload)
{
const uint64_t seqno = *reinterpret_cast<const uint64_t*>(payload.data() + PKT_SEQNO_BYTE_OFFSET);
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(payload.data());
const uint64_t seqno = *reinterpret_cast<const uint64_t*>(ptr + PKT_SEQNO_BYTE_OFFSET);
return seqno;
}

Expand All @@ -104,9 +107,10 @@ void write_packet_length(vector<char>& payload, uint64_t length)
*ptr = length;
}

uint64_t read_packet_length(const vector<char>& payload)
uint64_t read_packet_length(const const_buffer& payload)
{
const uint64_t length = *reinterpret_cast<const uint64_t*>(payload.data() + PKT_LENGTH_BYTE_OFFSET);
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(payload.data());
const uint64_t length = *reinterpret_cast<const uint64_t*>(ptr + PKT_LENGTH_BYTE_OFFSET);
return length;
}

Expand All @@ -125,7 +129,7 @@ void write_packet_checksum(vector<char>& payload)
md5_finish(&s, (md5_byte_t*) (payload.data() + PKT_MD5_BYTE_OFFSET));
}

bool validate_packet_checksum(const vector<char>& payload)
bool validate_packet_checksum(const const_buffer& payload)
{
#if SRT_VERSION_VALUE >= SRT_MAKE_VERSION(1, 5, 0)
using namespace srt;
Expand All @@ -141,7 +145,7 @@ bool validate_packet_checksum(const vector<char>& payload)
array<md5_byte_t, PKT_MD5_BYTE_LEN> result;
md5_finish(&s, result.data());

const md5_byte_t* ptr = (md5_byte_t*) (payload.data() + PKT_MD5_BYTE_OFFSET);
const md5_byte_t* ptr = reinterpret_cast<const md5_byte_t*>(payload.data()) + PKT_MD5_BYTE_OFFSET;
const int cmpres = std::memcmp(ptr, result.data(), result.size());

return cmpres == 0;
Expand Down
15 changes: 8 additions & 7 deletions xtransmit/metrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// submodules
#include "spdlog/spdlog.h"

#include "buffer.hpp"
#include "metrics_latency.hpp" // Transmission Delay
#include "metrics_jitter.hpp" // Interarrival Jitter (RFC 3550)
#include "metrics_delay_factor.hpp" // Time-Stamped Delay Factor (TS-DF) (EBU TECH 3337)
Expand All @@ -25,18 +26,18 @@ namespace metrics
//#define LOG_SC_METRICS "METRIC "

void write_sysclock_timestamp(vector<char>& payload);
system_clock::time_point read_sysclock_timestamp(const vector<char>& payload);
system_clock::time_point read_sysclock_timestamp(const const_buffer& payload);
void write_steadyclock_timestamp(vector<char>& payload);
steady_clock::time_point read_stdclock_timestamp(const vector<char>& payload);
steady_clock::time_point read_stdclock_timestamp(const const_buffer& payload);
void write_packet_seqno(vector<char>& payload, uint64_t seqno);
uint64_t read_packet_seqno(const vector<char>& payload);
uint64_t read_packet_seqno(const const_buffer& payload);
void write_packet_length(vector<char>& payload, uint64_t length);
uint64_t read_packet_length(const vector<char>& payload);
uint64_t read_packet_length(const const_buffer& payload);
void write_packet_checksum(vector<char>& payload);
/// @brief Check if the MD5 checksum of the packet is correct.
/// @param payload the payload
/// @return true if the checksum is correct, false otherwise.
bool validate_packet_checksum(const vector<char>& payload);
bool validate_packet_checksum(const const_buffer& payload);

class generator
{
Expand Down Expand Up @@ -71,7 +72,7 @@ namespace metrics
validator() {}

public:
inline void validate_packet(const vector<char>& payload)
inline void validate_packet(const const_buffer& payload)
{
const auto sys_time_now = system_clock::now();
const auto std_time_now = steady_clock::now();
Expand All @@ -82,7 +83,7 @@ namespace metrics
const uint64_t pktlength = read_packet_length(payload);
const bool checksum_match = validate_packet_checksum(payload);

m_integrity.submit_sample(pktseqno, payload.size() == pktlength, checksum_match);
m_integrity.submit_sample(pktseqno, pktlength, payload.size(), checksum_match);
if (!checksum_match)
{
// Do not calculate other metrics, packet payload is corrupted,
Expand Down
9 changes: 6 additions & 3 deletions xtransmit/metrics_integrity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ class integrity
public:
/// Submit new sample for integrity update.
/// @param [in] pkt_seqno newly arrived packet sequence number
/// @param [in] is_correct_length true if the packet length is correct
/// @param [in] expected_len expected payload length
/// @param [in] actual_len actual payload length
/// @param [in] is_valid_checksum true if the checksum is correct
void submit_sample(const uint64_t pkt_seqno, const bool is_correct_length, const bool is_valid_checksum)
void submit_sample(const uint64_t pkt_seqno, const uint64_t expected_len, const uint64_t actual_len, const bool is_valid_checksum)
{
const bool is_correct_length = expected_len == actual_len;
if (!is_correct_length)
{
++m_stats.pkts_wrong_len;
spdlog::warn("[METRICS] Incorrect length of packet seqno {}.", pkt_seqno);
spdlog::warn("[METRICS] Incorrect length of packet seqno {}. Expected {}, got {}.",
pkt_seqno, expected_len, actual_len);
}

if (!is_valid_checksum)
Expand Down
2 changes: 1 addition & 1 deletion xtransmit/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void run_pipe(shared_sock src, const config& cfg, const atomic_bool& force_break
if (cfg.enable_metrics)
{
lock_guard<mutex> lck(metrics_mtx);
validator.validate_packet(buffer);
validator.validate_packet(const_buffer(buffer.data(), bytes));
}

if (cfg.send_reply)
Expand Down

0 comments on commit 0bc23d9

Please sign in to comment.