feat(M1): TCP/TLS control plane -- auth, channels, ephemeral text
Implements the full M1 milestone. Two clients authenticate over TLS 1.3
(guest + Argon2id password) and exchange channel + private text messages
through a real server. All five ctest --preset m1-dev tests pass in ~1 s.
Key components added:
- vcpkg baseline + m1-dev preset (protobuf/mbedTLS/libsodium/asio/sqlite3)
- FrameCodec feed+emit, encode/decode_envelope, protobuf codegen
- TcpServerConn with blocking TLS handshake thread + tls_read_loop
- TlsContext (mbedTLS 1.3, ECDSA-P256 self-signed cert, TOFU on client)
- WorkerPool (3 threads, used for Argon2id)
- Database: SQLite + libsodium Argon2id, account lifecycle, bootstrap admin
- ServerIdentityManager: Ed25519 key + cert generate/persist/fingerprint
- ConnSession state machine: WaitingHello -> WaitingAuth -> Authenticated
- SessionRegistry: channel tree, user map, text routing, broadcast
- vc_client full M1 C ABI: connect/TLS/handshake/auth/text/disconnect
- voicecat-admin CLI: account add/reset/del/list
- test_m1_integration: M1 exit criterion, verified green
Bug fixed: double-framing in ConnSession::send_envelope -- encode_envelope
was adding the [4-byte len] prefix, then TcpServerConn::send_frame added
a second one, causing the client to parse [len][proto] as protobuf (silent
failure). Fixed by serializing raw protobuf bytes in send_envelope and
letting send_frame apply the single length prefix.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-15 23:48:44 +02:00
|
|
|
#include "session_registry.h"
|
|
|
|
|
|
|
|
|
|
#ifdef VOICECAT_HAS_NET
|
|
|
|
|
|
feat(M2): UDP voice/media plane -- SFU relay, Opus, AEAD, jitter buffer
Adds the full voice pipeline: 14-byte binary frame header, ChaCha20-Poly1305
AEAD keyed from the TLS exporter, libopus encode/decode with FEC/PLC/DTX,
an adaptive per-ssrc jitter buffer, a miniaudio capture/playback engine, an
APM passthrough stub, and the UdpBinding/StreamAnnounce signaling chain
wired through ConnSession/SessionRegistry into a new server-side SFU
(MediaRelay) that decrypts and re-encrypts frames per channel member.
Exit criterion verified: test_m2_voice — two headless clients relay 50
encrypted Opus frames through the server; ctest --preset m1-dev is 9/9
green. Also corrects protocol.md's UdpBinding diagram, which described the
UDP-side binding packet as AEAD-sealed when it is in fact a plaintext
bootstrap frame (separate from the TCP/TLS UdpBinding ack).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-16 01:31:14 +02:00
|
|
|
#include <atomic>
|
feat(M1): TCP/TLS control plane -- auth, channels, ephemeral text
Implements the full M1 milestone. Two clients authenticate over TLS 1.3
(guest + Argon2id password) and exchange channel + private text messages
through a real server. All five ctest --preset m1-dev tests pass in ~1 s.
Key components added:
- vcpkg baseline + m1-dev preset (protobuf/mbedTLS/libsodium/asio/sqlite3)
- FrameCodec feed+emit, encode/decode_envelope, protobuf codegen
- TcpServerConn with blocking TLS handshake thread + tls_read_loop
- TlsContext (mbedTLS 1.3, ECDSA-P256 self-signed cert, TOFU on client)
- WorkerPool (3 threads, used for Argon2id)
- Database: SQLite + libsodium Argon2id, account lifecycle, bootstrap admin
- ServerIdentityManager: Ed25519 key + cert generate/persist/fingerprint
- ConnSession state machine: WaitingHello -> WaitingAuth -> Authenticated
- SessionRegistry: channel tree, user map, text routing, broadcast
- vc_client full M1 C ABI: connect/TLS/handshake/auth/text/disconnect
- voicecat-admin CLI: account add/reset/del/list
- test_m1_integration: M1 exit criterion, verified green
Bug fixed: double-framing in ConnSession::send_envelope -- encode_envelope
was adding the [4-byte len] prefix, then TcpServerConn::send_frame added
a second one, causing the client to parse [len][proto] as protobuf (silent
failure). Fixed by serializing raw protobuf bytes in send_envelope and
letting send_frame apply the single length prefix.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-15 23:48:44 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <shared_mutex>
|
|
|
|
|
|
|
|
|
|
#include "conn_session.h"
|
|
|
|
|
|
|
|
|
|
namespace voicecat::server {
|
|
|
|
|
|
|
|
|
|
void SessionRegistry::init_default_channels() {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
ChannelEntry lobby;
|
|
|
|
|
lobby.proto.set_id(1);
|
|
|
|
|
lobby.proto.set_name("Lobby");
|
|
|
|
|
lobby.proto.set_type(voicecat::v1::CHANNEL_PERMANENT);
|
|
|
|
|
lobby.proto.set_order(0);
|
|
|
|
|
channels_[1] = std::move(lobby);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t SessionRegistry::register_session(std::weak_ptr<ConnSession> session) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
uint64_t id = next_session_id_++;
|
|
|
|
|
sessions_[id] = std::move(session);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SessionRegistry::unregister_session(uint64_t session_id) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
sessions_.erase(session_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t SessionRegistry::add_user(uint64_t session_id, const voicecat::v1::User& user) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
uint32_t uid = next_user_id_++;
|
|
|
|
|
UserEntry entry;
|
|
|
|
|
entry.proto = user;
|
|
|
|
|
entry.proto.set_id(uid);
|
|
|
|
|
entry.proto.set_channel_id(1); // start in Lobby
|
|
|
|
|
entry.session_id = session_id;
|
|
|
|
|
users_[uid] = std::move(entry);
|
|
|
|
|
return uid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SessionRegistry::remove_user(uint32_t user_id) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
users_.erase(user_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SessionRegistry::set_user_channel(uint32_t user_id, uint32_t channel_id) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
auto ch_it = channels_.find(channel_id);
|
|
|
|
|
if (ch_it == channels_.end()) return false;
|
|
|
|
|
auto user_it = users_.find(user_id);
|
|
|
|
|
if (user_it == users_.end()) return false;
|
|
|
|
|
user_it->second.proto.set_channel_id(channel_id);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<voicecat::v1::Channel> SessionRegistry::channel_snapshot() const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
std::vector<voicecat::v1::Channel> result;
|
|
|
|
|
result.reserve(channels_.size());
|
|
|
|
|
for (auto& [id, entry] : channels_) result.push_back(entry.proto);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<voicecat::v1::User> SessionRegistry::user_snapshot() const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
std::vector<voicecat::v1::User> result;
|
|
|
|
|
result.reserve(users_.size());
|
|
|
|
|
for (auto& [id, entry] : users_) result.push_back(entry.proto);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<ConnSession>> SessionRegistry::resolve_text_targets(
|
|
|
|
|
uint64_t sender_session_id, voicecat::v1::TextScope scope, uint32_t target_id) const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
std::vector<std::shared_ptr<ConnSession>> targets;
|
|
|
|
|
|
|
|
|
|
if (scope == voicecat::v1::TEXT_CHANNEL) {
|
|
|
|
|
// Find channel_id of the target, then all users in that channel
|
|
|
|
|
for (auto& [uid, entry] : users_) {
|
|
|
|
|
if (entry.proto.channel_id() != target_id) continue;
|
|
|
|
|
if (entry.session_id == sender_session_id) continue;
|
|
|
|
|
auto sit = sessions_.find(entry.session_id);
|
|
|
|
|
if (sit == sessions_.end()) continue;
|
|
|
|
|
if (auto sess = sit->second.lock()) targets.push_back(sess);
|
|
|
|
|
}
|
|
|
|
|
} else if (scope == voicecat::v1::TEXT_PRIVATE) {
|
|
|
|
|
// target_id is user_id
|
|
|
|
|
auto user_it = users_.find(target_id);
|
|
|
|
|
if (user_it != users_.end()) {
|
|
|
|
|
auto sit = sessions_.find(user_it->second.session_id);
|
|
|
|
|
if (sit != sessions_.end()) {
|
|
|
|
|
if (auto sess = sit->second.lock()) targets.push_back(sess);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return targets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SessionRegistry::broadcast(const voicecat::v1::Envelope& env,
|
|
|
|
|
uint64_t exclude_session_id) const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
for (auto& [sid, weak] : sessions_) {
|
|
|
|
|
if (sid == exclude_session_id) continue;
|
|
|
|
|
if (auto sess = weak.lock()) sess->send_envelope(env);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
feat(M2): UDP voice/media plane -- SFU relay, Opus, AEAD, jitter buffer
Adds the full voice pipeline: 14-byte binary frame header, ChaCha20-Poly1305
AEAD keyed from the TLS exporter, libopus encode/decode with FEC/PLC/DTX,
an adaptive per-ssrc jitter buffer, a miniaudio capture/playback engine, an
APM passthrough stub, and the UdpBinding/StreamAnnounce signaling chain
wired through ConnSession/SessionRegistry into a new server-side SFU
(MediaRelay) that decrypts and re-encrypts frames per channel member.
Exit criterion verified: test_m2_voice — two headless clients relay 50
encrypted Opus frames through the server; ctest --preset m1-dev is 9/9
green. Also corrects protocol.md's UdpBinding diagram, which described the
UDP-side binding packet as AEAD-sealed when it is in fact a plaintext
bootstrap frame (separate from the TCP/TLS UdpBinding ack).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-16 01:31:14 +02:00
|
|
|
// ── M2: UDP / media ──────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
void SessionRegistry::register_udp_token(const std::array<uint8_t, 16>& token,
|
|
|
|
|
uint64_t session_id) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
udp_tokens_[token] = session_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<ConnSession> SessionRegistry::find_by_udp_token(
|
|
|
|
|
const std::array<uint8_t, 16>& token) const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
auto it = udp_tokens_.find(token);
|
|
|
|
|
if (it == udp_tokens_.end()) return nullptr;
|
|
|
|
|
auto sit = sessions_.find(it->second);
|
|
|
|
|
if (sit == sessions_.end()) return nullptr;
|
|
|
|
|
return sit->second.lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SessionRegistry::register_udp_endpoint(asio::ip::udp::endpoint ep,
|
|
|
|
|
uint64_t session_id) {
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
udp_endpoints_[ep] = session_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<ConnSession> SessionRegistry::find_by_udp_endpoint(
|
|
|
|
|
const asio::ip::udp::endpoint& ep) const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
auto it = udp_endpoints_.find(ep);
|
|
|
|
|
if (it == udp_endpoints_.end()) return nullptr;
|
|
|
|
|
auto sit = sessions_.find(it->second);
|
|
|
|
|
if (sit == sessions_.end()) return nullptr;
|
|
|
|
|
return sit->second.lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t SessionRegistry::assign_ssrc(uint64_t session_id) {
|
|
|
|
|
uint32_t ssrc = next_ssrc_.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
|
std::unique_lock lk(mu_);
|
|
|
|
|
ssrc_to_session_[ssrc] = session_id;
|
|
|
|
|
return ssrc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<ConnSession>> SessionRegistry::find_channel_sessions(
|
|
|
|
|
uint32_t channel_id, uint64_t exclude_session_id) const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
std::vector<std::shared_ptr<ConnSession>> result;
|
|
|
|
|
for (auto& [uid, entry] : users_) {
|
|
|
|
|
if (entry.proto.channel_id() != channel_id) continue;
|
|
|
|
|
if (entry.session_id == exclude_session_id) continue;
|
|
|
|
|
auto sit = sessions_.find(entry.session_id);
|
|
|
|
|
if (sit == sessions_.end()) continue;
|
|
|
|
|
if (auto sess = sit->second.lock()) result.push_back(sess);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t SessionRegistry::user_channel(uint32_t user_id) const {
|
|
|
|
|
std::shared_lock lk(mu_);
|
|
|
|
|
auto it = users_.find(user_id);
|
|
|
|
|
return (it == users_.end()) ? 0 : it->second.proto.channel_id();
|
|
|
|
|
}
|
|
|
|
|
|
feat(M1): TCP/TLS control plane -- auth, channels, ephemeral text
Implements the full M1 milestone. Two clients authenticate over TLS 1.3
(guest + Argon2id password) and exchange channel + private text messages
through a real server. All five ctest --preset m1-dev tests pass in ~1 s.
Key components added:
- vcpkg baseline + m1-dev preset (protobuf/mbedTLS/libsodium/asio/sqlite3)
- FrameCodec feed+emit, encode/decode_envelope, protobuf codegen
- TcpServerConn with blocking TLS handshake thread + tls_read_loop
- TlsContext (mbedTLS 1.3, ECDSA-P256 self-signed cert, TOFU on client)
- WorkerPool (3 threads, used for Argon2id)
- Database: SQLite + libsodium Argon2id, account lifecycle, bootstrap admin
- ServerIdentityManager: Ed25519 key + cert generate/persist/fingerprint
- ConnSession state machine: WaitingHello -> WaitingAuth -> Authenticated
- SessionRegistry: channel tree, user map, text routing, broadcast
- vc_client full M1 C ABI: connect/TLS/handshake/auth/text/disconnect
- voicecat-admin CLI: account add/reset/del/list
- test_m1_integration: M1 exit criterion, verified green
Bug fixed: double-framing in ConnSession::send_envelope -- encode_envelope
was adding the [4-byte len] prefix, then TcpServerConn::send_frame added
a second one, causing the client to parse [len][proto] as protobuf (silent
failure). Fixed by serializing raw protobuf bytes in send_envelope and
letting send_frame apply the single length prefix.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-15 23:48:44 +02:00
|
|
|
} // namespace voicecat::server
|
|
|
|
|
|
|
|
|
|
#endif // VOICECAT_HAS_NET
|