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:
@@ -257,13 +257,13 @@ void ConnSession::send_generic_result(uint64_t req_id, bool ok, uint32_t code,
|
||||
// ── Handlers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
void ConnSession::handle_client_hello(uint64_t req_id, const voicecat::v1::ClientHello& msg) {
|
||||
if (msg.proto_version() != 1) {
|
||||
if (msg.proto_version() != 2) { // protocol v2: 64-bit voice seq (docs/voice.md §2)
|
||||
send_disconnect_and_close(1, "unsupported protocol version");
|
||||
return;
|
||||
}
|
||||
auto env = make_env(req_id);
|
||||
auto* hello = env.mutable_server_hello();
|
||||
hello->set_proto_version(1);
|
||||
hello->set_proto_version(2);
|
||||
hello->set_server_name("VoiceCat Server");
|
||||
hello->set_server_version("0.1.0");
|
||||
if (allow_guests_) hello->add_auth_methods("guest");
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -49,10 +49,20 @@ class MediaRelay {
|
||||
private:
|
||||
void on_udp_frame(const uint8_t* data, size_t len, asio::ip::udp::endpoint sender);
|
||||
|
||||
// Count a dropped inbound voice frame (by reason) and emit a rate-limited summary
|
||||
// to stderr. Runs on the io thread, so plain counters are safe.
|
||||
void note_drop(const char* reason);
|
||||
|
||||
asio::io_context& io_;
|
||||
std::shared_ptr<SessionRegistry> registry_;
|
||||
voicecat::net::UdpMediaChannel udp_;
|
||||
|
||||
// Diagnostics: dropped-frame counters so a wedged media path is observable.
|
||||
uint64_t drop_no_endpoint_ = 0; // voice from an unmapped UDP endpoint
|
||||
uint64_t drop_no_crypto_ = 0; // session has no recv_crypto yet
|
||||
uint64_t drop_open_failed_ = 0; // AEAD auth failure or replay reject
|
||||
int64_t last_drop_log_ms_ = 0;
|
||||
|
||||
// Scratch buffer for re-encrypted payloads (size = max_frame + 16 MAC)
|
||||
static constexpr size_t kMaxPayload = 1500;
|
||||
std::vector<uint8_t> seal_buf_ = std::vector<uint8_t>(kMaxPayload + 16, uint8_t{0});
|
||||
|
||||
@@ -100,6 +100,22 @@ void SessionRegistry::unregister_session(uint64_t session_id) {
|
||||
std::unique_lock lk(mu_);
|
||||
sessions_.erase(session_id);
|
||||
session_permissions_.erase(session_id);
|
||||
|
||||
// Free the per-session UDP/media state too. These maps are keyed by
|
||||
// endpoint/token/ssrc (not session id), so scan-and-erase by value. Leaving them
|
||||
// behind leaks entries and lets a stale endpoint/token resolve toward a dead
|
||||
// session across reconnects (e.g. a wifi-handoff rebind from a new port).
|
||||
auto erase_by_value = [session_id](auto& map) {
|
||||
for (auto it = map.begin(); it != map.end();) {
|
||||
if (it->second == session_id)
|
||||
it = map.erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
};
|
||||
erase_by_value(udp_endpoints_);
|
||||
erase_by_value(udp_tokens_);
|
||||
erase_by_value(ssrc_to_session_);
|
||||
}
|
||||
|
||||
uint32_t SessionRegistry::add_user(uint64_t session_id, const voicecat::v1::User& user) {
|
||||
|
||||
Reference in New Issue
Block a user