Skip to content

Commit

Permalink
refactor: change sets of strings to sets of snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 22, 2024
1 parent c8781e8 commit 02868c5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,13 @@ class DPP_EXPORT discord_voice_client : public websocket_client
* @brief The list of users that have E2EE potentially enabled for
* DAVE protocol.
*/
std::set<std::string> dave_mls_user_list;
std::set<dpp::snowflake> dave_mls_user_list;

/**
* @brief The list of users that have left the voice channel but
* not yet removed from MLS group.
*/
std::set<std::string> dave_mls_pending_remove_list;
std::set<dpp::snowflake> dave_mls_pending_remove_list;

/**
* @brief File descriptor for UDP connection
Expand Down
12 changes: 6 additions & 6 deletions src/dpp/dave/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ catch (const std::exception& e) {
return;
}

std::optional<std::vector<uint8_t>> session::process_proposals(std::vector<uint8_t> proposals, std::set<std::string> const& recognised_user_ids) noexcept
std::optional<std::vector<uint8_t>> session::process_proposals(std::vector<uint8_t> proposals, std::set<dpp::snowflake> const& recognised_user_ids) noexcept
try {
if (!pending_group_state && !current_state) {
creator.log(dpp::ll_debug, "Cannot process proposals without any pending or established MLS group state");
Expand Down Expand Up @@ -238,9 +238,9 @@ catch (const std::exception& e) {
return std::nullopt;
}

bool session::is_recognized_user_id(const ::mlspp::Credential& cred, std::set<std::string> const& recognised_user_ids) const
bool session::is_recognized_user_id(const ::mlspp::Credential& cred, std::set<dpp::snowflake> const& recognised_user_ids) const
{
std::string uid = user_credential_to_string(cred, session_protocol_version);
dpp::snowflake uid(user_credential_to_string(cred, session_protocol_version));
if (uid.empty()) {
creator.log(dpp::ll_warning, "Attempted to verify credential of unexpected type");
return false;
Expand All @@ -254,7 +254,7 @@ bool session::is_recognized_user_id(const ::mlspp::Credential& cred, std::set<st
return true;
}

bool session::validate_proposal_message(::mlspp::AuthenticatedContent const& message, ::mlspp::State const& target_state, std::set<std::string> const& recognised_user_ids) const {
bool session::validate_proposal_message(::mlspp::AuthenticatedContent const& message, ::mlspp::State const& target_state, std::set<dpp::snowflake> const& recognised_user_ids) const {
if (message.wire_format != ::mlspp::WireFormat::mls_public_message) {
creator.log(dpp::ll_warning, "MLS proposal message must be PublicMessage");
TRACK_MLS_ERROR("Invalid proposal wire format");
Expand Down Expand Up @@ -357,7 +357,7 @@ catch (const std::exception& e) {
return failed_t{};
}

std::optional<roster_map> session::process_welcome(std::vector<uint8_t> welcome, std::set<std::string> const& recognised_user_ids) noexcept
std::optional<roster_map> session::process_welcome(std::vector<uint8_t> welcome, std::set<dpp::snowflake> const& recognised_user_ids) noexcept
try {
if (!has_cryptographic_state_for_welcome()) {
creator.log(dpp::ll_warning, "Missing local crypto state necessary to process MLS welcome");
Expand Down Expand Up @@ -461,7 +461,7 @@ bool session::has_cryptographic_state_for_welcome() const noexcept
return join_key_package && join_init_private_key && signature_private_key && hpke_private_key;
}

bool session::verify_welcome_state(::mlspp::State const& state, std::set<std::string> const& recognised_user_ids) const
bool session::verify_welcome_state(::mlspp::State const& state, std::set<dpp::snowflake> const& recognised_user_ids) const
{
if (!mls_external_sender) {
creator.log(dpp::ll_warning, "Cannot verify MLS welcome without an external sender");
Expand Down
10 changes: 5 additions & 5 deletions src/dpp/dave/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class session { // NOLINT
* @param recognised_user_ids list of recognised user IDs
* @return optional vector to send in reply as commit welcome
*/
std::optional<std::vector<uint8_t>> process_proposals(std::vector<uint8_t> proposals, std::set<std::string> const& recognised_user_ids) noexcept;
std::optional<std::vector<uint8_t>> process_proposals(std::vector<uint8_t> proposals, std::set<dpp::snowflake> const& recognised_user_ids) noexcept;

/**
* @brief Process commit message from discord websocket
Expand All @@ -146,7 +146,7 @@ class session { // NOLINT
* @param recognised_user_ids Recognised user ID list
* @return roster list of people in the vc
*/
std::optional<roster_map> process_welcome(std::vector<uint8_t> welcome, std::set<std::string> const& recognised_user_ids) noexcept;
std::optional<roster_map> process_welcome(std::vector<uint8_t> welcome, std::set<dpp::snowflake> const& recognised_user_ids) noexcept;

/**
* @brief Get the bot user's key package for sending to websocket
Expand Down Expand Up @@ -206,7 +206,7 @@ class session { // NOLINT
* @param recognised_user_ids list of recognised user IDs
* @return
*/
[[nodiscard]] bool is_recognized_user_id(const ::mlspp::Credential& cred, std::set<std::string> const& recognised_user_ids) const;
[[nodiscard]] bool is_recognized_user_id(const ::mlspp::Credential& cred, std::set<dpp::snowflake> const& recognised_user_ids) const;

/**
* @brief Validate proposals message
Expand All @@ -215,15 +215,15 @@ class session { // NOLINT
* @param recognised_user_ids recognised list of user IDs
* @return true if validated
*/
[[nodiscard]] bool validate_proposal_message(::mlspp::AuthenticatedContent const& message, ::mlspp::State const& target_state, std::set<std::string> const& recognised_user_ids) const;
[[nodiscard]] bool validate_proposal_message(::mlspp::AuthenticatedContent const& message, ::mlspp::State const& target_state, std::set<dpp::snowflake> const& recognised_user_ids) const;

/**
* @brief Verify that welcome state is valid
* @param state current state
* @param recognised_user_ids list of recognised user IDs
* @return
*/
[[nodiscard]] bool verify_welcome_state(::mlspp::State const& state, std::set<std::string> const& recognised_user_ids) const;
[[nodiscard]] bool verify_welcome_state(::mlspp::State const& state, std::set<dpp::snowflake> const& recognised_user_ids) const;

/**
* @brief Check if can process a commit now
Expand Down
24 changes: 11 additions & 13 deletions src/dpp/voice/enabled/handle_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ void discord_voice_client::update_ratchets(bool force) {
*/
log(ll_debug, "Updating MLS ratchets for " + std::to_string(dave_mls_user_list.size() + 1) + " user(s)");
for (const auto& user : dave_mls_user_list) {
dpp::snowflake u{user};
if (u == creator->me.id) {
if (user == creator->me.id) {
continue;
}
decryptor_list::iterator decryptor;
/* New user join/old user leave - insert new ratchets if they don't exist */
decryptor = mls_state->decryptors.find(u);
decryptor = mls_state->decryptors.find(user.str());
if (decryptor == mls_state->decryptors.end()) {
log(ll_debug, "Inserting decryptor key ratchet for NEW user: " + user + ", protocol version: " + std::to_string(mls_state->dave_session->get_protocol_version()));
auto [iter, inserted] = mls_state->decryptors.emplace(u, std::make_unique<dpp::dave::decryptor>(*creator));
log(ll_debug, "Inserting decryptor key ratchet for NEW user: " + user.str() + ", protocol version: " + std::to_string(mls_state->dave_session->get_protocol_version()));
auto [iter, inserted] = mls_state->decryptors.emplace(user.str(), std::make_unique<dpp::dave::decryptor>(*creator));
decryptor = iter;
}
decryptor->second->transition_to_key_ratchet(mls_state->dave_session->get_key_ratchet(user), RATCHET_EXPIRY);
Expand All @@ -72,7 +71,7 @@ void discord_voice_client::update_ratchets(bool force) {
if (mls_state->encryptor) {
/* Updating key rachet should always be done on execute transition. Generally after group member add/remove. */
log(ll_debug, "Setting key ratchet for sending audio...");
mls_state->encryptor->set_key_ratchet(mls_state->dave_session->get_key_ratchet(creator->me.id.str()));
mls_state->encryptor->set_key_ratchet(mls_state->dave_session->get_key_ratchet(creator->me.id));
}

/**
Expand Down Expand Up @@ -146,7 +145,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
log(ll_debug, "voice_client_dave_mls_welcome with transition id " + std::to_string(this->mls_state->transition_id));

/* We should always recognize our own selves, but do we? */
dave_mls_user_list.insert(this->creator->me.id.str());
dave_mls_user_list.insert(this->creator->me.id);

auto r = mls_state->dave_session->process_welcome(dave_header.get_data(), dave_mls_user_list);

Expand Down Expand Up @@ -222,7 +221,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod

/* Remove this user from pending remove list if exist */
for (const auto &user : joining_dave_users) {
dave_mls_pending_remove_list.erase(user);
dave_mls_pending_remove_list.erase(dpp::snowflake(user));
}

log(ll_debug, "New of clients in voice channel: " + std::to_string(joining_dave_users.size()) + " total is " + std::to_string(dave_mls_user_list.size()));
Expand Down Expand Up @@ -298,7 +297,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
}

/* Mark this user for remove on immediate upgrade */
dave_mls_pending_remove_list.insert(u_id.str());
dave_mls_pending_remove_list.insert(u_id);

if (!creator->on_voice_client_disconnect.empty()) {
voice_client_disconnect_t vcd(nullptr, data);
Expand Down Expand Up @@ -630,12 +629,11 @@ void discord_voice_client::process_mls_group_rosters(const dave::roster_map &rma
}

dpp::snowflake u_id(k);
auto u_id_str = u_id.str();

log(ll_debug, "Removed user from MLS Group: " + u_id_str);
log(ll_debug, "Removed user from MLS Group: " + u_id.str());

dave_mls_user_list.erase(u_id_str);
dave_mls_pending_remove_list.erase(u_id_str);
dave_mls_user_list.erase(u_id);
dave_mls_pending_remove_list.erase(u_id);

/* Remove this user's key ratchet */
mls_state->decryptors.erase(u_id);
Expand Down

0 comments on commit 02868c5

Please sign in to comment.