fix(media): stop permanent voice loss after bad-network blip (protocol v2)

A bad UDP packet on a flaky link could permanently wedge the voice path,
unrecoverable even across app restarts. Three defects:

1. Anti-replay window was advanced from the UNAUTHENTICATED header seq
   before the AEAD tag was checked, and not rolled back on failure. One
   corrupted/forged frame shoved recv_highest_ far ahead, after which every
   legitimate frame was rejected as "too old" forever. Reorder to
   replay-check -> authenticate -> update (RFC 3711 3.3); the window now
   moves only after a successful tag check.

2. The wire seq was only the low 16 bits of the nonce counter (zero-extended
   on receive). After 65,536 frames the nonce desynced and all frames failed
   auth. Widen the voice frame seq u16 -> u64 (header 14 -> 20 bytes). The
   core owns all UDP framing, so Swift/C# clients need only a rebuild. This
   is a versioned wire change: VOICECAT_PROTOCOL_VERSION 1 -> 2, handshake
   rejects on mismatch.

3. Server leaked per-session UDP state on disconnect; unregister_session now
   frees udp_endpoints_/udp_tokens_/ssrc_to_session_.

Also add rate-limited dropped-frame logging to MediaRelay so a wedged media
path is observable. New regression tests in test_media_aead.cpp cover the
poison (fails on old code) and the 16-bit wrap. ctest --preset dev
-E external_pcm: 22/22 pass (external_pcm aborts on a pre-existing CoreAudio
shutdown race, unrelated).
This commit is contained in:
2026-06-21 17:45:28 +02:00
parent 5be6d8430d
commit 6071c8e238
17 changed files with 299 additions and 86 deletions

View File

@@ -3,7 +3,9 @@
#ifdef VOICECAT_HAS_NET
#include <array>
#include <chrono>
#include <cstdio>
#include <string_view>
#include "conn_session.h"
#include "crypto/crypto.h"
@@ -35,6 +37,24 @@ uint16_t MediaRelay::media_port() const {
return static_cast<uint16_t>(udp_.local_endpoint().port());
}
void MediaRelay::note_drop(const char* reason) {
if (std::string_view(reason) == "unmapped-endpoint") ++drop_no_endpoint_;
else if (std::string_view(reason) == "no-recv-crypto") ++drop_no_crypto_;
else ++drop_open_failed_;
// Rate-limit the summary to at most once every 5s so a flood can't spam the log.
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
if (now_ms - last_drop_log_ms_ < 5000) return;
last_drop_log_ms_ = now_ms;
std::fprintf(stderr,
"[media] dropped frames — unmapped-endpoint=%llu no-recv-crypto=%llu "
"open-failed(auth/replay)=%llu\n",
static_cast<unsigned long long>(drop_no_endpoint_),
static_cast<unsigned long long>(drop_no_crypto_),
static_cast<unsigned long long>(drop_open_failed_));
}
void MediaRelay::on_udp_frame(const uint8_t* data, size_t len,
asio::ip::udp::endpoint sender) {
if (len < 1) return;
@@ -59,10 +79,10 @@ void MediaRelay::on_udp_frame(const uint8_t* data, size_t len,
// Resolve sender session.
auto sender_session = registry_->find_by_udp_endpoint(sender);
if (!sender_session) return;
if (!sender_session) { note_drop("unmapped-endpoint"); return; }
auto* recv_crypto = sender_session->recv_crypto();
if (!recv_crypto) return;
if (!recv_crypto) { note_drop("no-recv-crypto"); return; }
// AAD = 14-byte header (authenticated, not encrypted).
const uint8_t* aad = data;
@@ -73,7 +93,7 @@ void MediaRelay::on_udp_frame(const uint8_t* data, size_t len,
long plain_len = recv_crypto->open(sealed, sealed_len, aad, voicecat::net::kVoiceHeaderSize,
plain_buf_.data(), plain_buf_.size());
if (plain_len < 0) return; // auth failure or replay
if (plain_len < 0) { note_drop("open-failed"); return; } // auth failure or replay
// Parse the voice frame header to find the source ssrc/channel.
voicecat::net::VoiceFrame hdr{};
@@ -111,8 +131,14 @@ void MediaRelay::on_udp_frame(const uint8_t* data, size_t len,
// so the header (which is the authenticated AAD) carries the matching counter.
std::memcpy(seal_buf_.data(), data, voicecat::net::kVoiceHeaderSize);
const uint64_t send_ctr = send_crypto->peek_send_counter();
seal_buf_[8] = static_cast<uint8_t>((send_ctr >> 8) & 0xFF);
seal_buf_[9] = static_cast<uint8_t>(send_ctr & 0xFF);
seal_buf_[8] = static_cast<uint8_t>((send_ctr >> 56) & 0xFF);
seal_buf_[9] = static_cast<uint8_t>((send_ctr >> 48) & 0xFF);
seal_buf_[10] = static_cast<uint8_t>((send_ctr >> 40) & 0xFF);
seal_buf_[11] = static_cast<uint8_t>((send_ctr >> 32) & 0xFF);
seal_buf_[12] = static_cast<uint8_t>((send_ctr >> 24) & 0xFF);
seal_buf_[13] = static_cast<uint8_t>((send_ctr >> 16) & 0xFF);
seal_buf_[14] = static_cast<uint8_t>((send_ctr >> 8) & 0xFF);
seal_buf_[15] = static_cast<uint8_t>(send_ctr & 0xFF);
uint8_t* out_payload = seal_buf_.data() + voicecat::net::kVoiceHeaderSize;
long sealed_out = send_crypto->seal(