From 8acd2396d3ac7cdc42a62af81715ce2b73315790 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Mon, 27 Jan 2020 13:02:22 +0100 Subject: [PATCH] Fixed a crash --- server/src/channel/ClientChannelView.cpp | 9 ++++--- server/src/channel/ServerChannel.cpp | 26 +++++++++++++++++++ server/src/channel/ServerChannel.h | 1 + server/src/client/ConnectedClient.cpp | 4 ++- .../client/voice/VoiceClientConnection.cpp | 23 +++++++++------- .../src/client/voice/VoiceClientConnection.h | 2 +- .../client/voice/VoiceClientHandschake.cpp | 2 +- server/src/music/MusicPlaylist.h | 1 + 8 files changed, 53 insertions(+), 15 deletions(-) diff --git a/server/src/channel/ClientChannelView.cpp b/server/src/channel/ClientChannelView.cpp index 16f4067..e331cbf 100644 --- a/server/src/channel/ClientChannelView.cpp +++ b/server/src/channel/ClientChannelView.cpp @@ -171,10 +171,13 @@ std::deque> ClientChannelView::insert_channels(shared head = head->next; continue; }; - debugMessage(this->getServerId(), "{}[CHANNELS] Insert channel {} ({} => order {}) after {} ({})", + auto now_prv = this->find_channel(entry->previousChannelId()); + debugMessage(this->getServerId(), "{}[CHANNELS] Insert channel {} ({}) after {} ({}). Original view prv: {} ({}). Original prv: {} ({})", CLIENT_STR_LOG_PREFIX_(this->owner), - channel->channelId(), channel->name(), entry->previousChannelId(), - previous ? previous->channelId() : 0, previous_channel ? previous_channel->name() : "" + channel->channelId(), channel->name(), + entry->previousChannelId(), now_prv ? now_prv->channel()->name() : "", + remote_previous ? remote_previous->entry->channelId() : 0, remote_previous ? dynamic_pointer_cast(remote_previous->entry)->name() : "", + head->previous ? head->previous->entry->channelId() : 0, head->previous ? dynamic_pointer_cast(head->previous->entry)->name() : "" ); result.push_back(entry); diff --git a/server/src/channel/ServerChannel.cpp b/server/src/channel/ServerChannel.cpp index 9f4f202..56748e6 100644 --- a/server/src/channel/ServerChannel.cpp +++ b/server/src/channel/ServerChannel.cpp @@ -338,6 +338,31 @@ bool ServerChannelTree::buildChannelTreeFromTemp() { return true; } +inline void walk_tree(const std::shared_ptr& parent, std::shared_ptr head) { + auto parent_id = parent ? parent->entry->channelId() : 0; + std::shared_ptr previous{nullptr}; + while(head) { + head->entry->setParentChannelId(parent_id); + if(head->previous != previous) { + logCritical(0, "Detect broken channel tree!"); + } else if(previous) { + head->entry->setPreviousChannelId(previous->entry->channelId()); + } else { + head->entry->setPreviousChannelId(0); + } + + if(head->child_head) + walk_tree(head, head->child_head); + previous = head; + head = head->next; + } +} + +bool ServerChannelTree::updateOrderIds() { + walk_tree(nullptr, this->head); + return true; +} + inline ssize_t count_characters(const std::string& in) { size_t index = 0; @@ -478,6 +503,7 @@ void ServerChannelTree::loadChannelsFromDatabase() { logMessage(this->getServerId(), "Loaded {} saved channels. Assembling...", this->tmpChannelList.size()); this->initializeTempParents(); this->buildChannelTreeFromTemp(); + this->updateOrderIds(); this->validateChannelNames(); this->validateChannelIcons(); //this->printChannelTree(); diff --git a/server/src/channel/ServerChannel.h b/server/src/channel/ServerChannel.h index d994ccf..0d4d42d 100644 --- a/server/src/channel/ServerChannel.h +++ b/server/src/channel/ServerChannel.h @@ -62,6 +62,7 @@ namespace ts { bool initializeTempParents(); bool buildChannelTreeFromTemp(); + bool updateOrderIds(); bool validateChannelNames(); bool validateChannelIcons(); int loadChannelFromData(int argc, char** data, char** column); diff --git a/server/src/client/ConnectedClient.cpp b/server/src/client/ConnectedClient.cpp index 6a4a0fc..f5f6f7f 100644 --- a/server/src/client/ConnectedClient.cpp +++ b/server/src/client/ConnectedClient.cpp @@ -685,6 +685,7 @@ void ConnectedClient::sendChannelList(bool lock_channel_tree) { */ + /* std::deque> entry_channels{pop_view_entry(channels, this->currentChannel->channelId())}; while(entry_channels.front()) entry_channels.push_front(pop_view_entry(channels, entry_channels.front()->parentId())); entry_channels.pop_front(); @@ -693,8 +694,9 @@ void ConnectedClient::sendChannelList(bool lock_channel_tree) { send_channels(this, entry_channels.begin(), entry_channels.end(), true); this->notifyClientEnterView(_this.lock(), nullptr, "", this->currentChannel, ViewReasonId::VREASON_SYSTEM, nullptr, false); //Notify self after path is send - + */ send_channels(this, channels.begin(), channels.end(), false); + this->notifyClientEnterView(_this.lock(), nullptr, "", this->currentChannel, ViewReasonId::VREASON_SYSTEM, nullptr, false); //Notify self after path is send this->sendCommand(Command("channellistfinished")); } diff --git a/server/src/client/voice/VoiceClientConnection.cpp b/server/src/client/voice/VoiceClientConnection.cpp index 0552e64..7ab8adc 100644 --- a/server/src/client/voice/VoiceClientConnection.cpp +++ b/server/src/client/voice/VoiceClientConnection.cpp @@ -96,11 +96,12 @@ void VoiceClientConnection::handle_incoming_datagram(const pipes::buffer_view& b unique_lock queue_lock(fragment_buffer.buffer_lock); auto result = fragment_buffer.accept_index(packet_parser.packet_id()); if(result != 0) { /* packet index is ahead buffer index */ - debugMessage(this->client->getServerId(), "{} Dropping command packet because command assembly buffer has an {} ({}|{})", + debugMessage(this->client->getServerId(), "{} Dropping command packet because command assembly buffer has an {} ({}|{}|{})", CLIENT_STR_LOG_PREFIX_(this->client), result == -1 ? "underflow" : "overflow", fragment_buffer.capacity(), - fragment_buffer.current_index() + fragment_buffer.current_index(), + packet_parser.packet_id() ); if(result == -1) { /* underflow */ @@ -227,7 +228,8 @@ void VoiceClientConnection::execute_handle_command_packets(const std::chrono::sy //TODO: Remove the buffer_execute_lock and use the one within the this->client->handlePacketCommand method unique_lock buffer_execute_lock; pipes::buffer payload{}; - auto reexecute_handle = this->next_reassembled_command(buffer_execute_lock, payload); + uint16_t packet_id{}; + auto reexecute_handle = this->next_reassembled_command(buffer_execute_lock, payload, packet_id); if(!payload.empty()){ auto startTime = system_clock::now(); @@ -255,7 +257,7 @@ void VoiceClientConnection::execute_handle_command_packets(const std::chrono::sy } /* buffer_execute_lock: lock for in order execution */ -bool VoiceClientConnection::next_reassembled_command(unique_lock& buffer_execute_lock, pipes::buffer& result) { +bool VoiceClientConnection::next_reassembled_command(unique_lock& buffer_execute_lock, pipes::buffer& result, uint16_t& packet_id) { command_fragment_buffer_t* buffer{nullptr}; unique_lock buffer_lock; /* general buffer lock */ @@ -290,9 +292,11 @@ bool VoiceClientConnection::next_reassembled_command(unique_lockslot_value(0).packet_flags & PacketFlag::Fragmented) { - uint16_t sequence_length = 1; - size_t total_payload_length{0}; + auto& first_buffer = buffer->slot_value(0); + packet_id = first_buffer.packet_id; + if(first_buffer.packet_flags & PacketFlag::Fragmented) { + uint16_t sequence_length{1}; + size_t total_payload_length{first_buffer.payload_length}; do { if(sequence_length >= buffer->capacity()) { logError(this->client->getServerId(), "{} Command fragment buffer is full, and there is not fragmented packet end. Dropping full buffer which will probably cause a connection loss.", CLIENT_STR_LOG_PREFIX_(this->client)); @@ -320,7 +324,7 @@ bool VoiceClientConnection::next_reassembled_command(unique_lockslot_value(0).packet_flags; while(packet_count < sequence_length) { @@ -340,6 +344,7 @@ bool VoiceClientConnection::next_reassembled_command(unique_lockpop_front(); packet_flags = packet.packet_flags; @@ -415,7 +420,7 @@ bool VoiceClientConnection::prepare_packet_for_write(vector &resu string error = "success"; - if(packet->type().compressable() && !packet->memory_state.fragment_entry && false) { + if(packet->type().compressable() && !packet->memory_state.fragment_entry) { packet->enable_flag(PacketFlag::Compressed); if(!this->compress_handler.progressPacketOut(packet.get(), error)) { logError(this->getClient()->getServerId(), "{} Could not compress outgoing packet.\nThis could cause fatal failed for the client.\nError: {}", error); diff --git a/server/src/client/voice/VoiceClientConnection.h b/server/src/client/voice/VoiceClientConnection.h index 6761eef..09a39ba 100644 --- a/server/src/client/voice/VoiceClientConnection.h +++ b/server/src/client/voice/VoiceClientConnection.h @@ -102,7 +102,7 @@ namespace ts { //Handle stuff void execute_handle_command_packets(const std::chrono::system_clock::time_point& /* scheduled */); - bool next_reassembled_command(std::unique_lock &buffer_execute_lock /* packet channel execute lock */, pipes::buffer & /* buffer*/); + bool next_reassembled_command(std::unique_lock &buffer_execute_lock /* packet channel execute lock */, pipes::buffer & /* buffer*/, uint16_t& /* packet id */); /* ---------- Write declarations ---------- */ diff --git a/server/src/client/voice/VoiceClientHandschake.cpp b/server/src/client/voice/VoiceClientHandschake.cpp index 3919002..725d391 100644 --- a/server/src/client/voice/VoiceClientHandschake.cpp +++ b/server/src/client/voice/VoiceClientHandschake.cpp @@ -175,6 +175,6 @@ ts::command_result VoiceClient::handleCommandClientEk(Command& cmd) { this->connection->getCryptHandler()->setupSharedSecretNew(this->crypto.alpha, this->crypto.beta, (char*) private_key.data(), client_key.data()); this->connection->acknowledge_handler.reset(); this->crypto.protocol_encrypted = true; - this->sendAcknowledge(2); //Send the encrypted acknowledge (most the times the second packet; If not we're going into the resend loop) + this->sendAcknowledge(1); //Send the encrypted acknowledge (most the times the second packet; If not we're going into the resend loop) return ts::command_result{error::ok}; } \ No newline at end of file diff --git a/server/src/music/MusicPlaylist.h b/server/src/music/MusicPlaylist.h index 45b2e98..4083147 100644 --- a/server/src/music/MusicPlaylist.h +++ b/server/src/music/MusicPlaylist.h @@ -112,6 +112,7 @@ namespace ts { virtual void set_self_ref(const std::shared_ptr& /* playlist */); std::atomic current_id; + /* channel id's are here client database ids! */ std::shared_ptr _permissions; std::shared_ptr _properties; std::weak_ptr manager;