From 8ca602150d4fdce6dcc63fa13e22aeaac5b927cc Mon Sep 17 00:00:00 2001 From: sfan5 Date: Thu, 23 Jul 2020 19:47:58 +0200 Subject: Replace std::list in networking code (#10215) --- src/network/connectionthreads.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'src/network/connectionthreads.cpp') diff --git a/src/network/connectionthreads.cpp b/src/network/connectionthreads.cpp index 9a6617a1c..28ed798d9 100644 --- a/src/network/connectionthreads.cpp +++ b/src/network/connectionthreads.cpp @@ -144,7 +144,7 @@ void ConnectionSendThread::Trigger() bool ConnectionSendThread::packetsQueued() { - std::list peerIds = m_connection->getPeerIDs(); + std::vector peerIds = m_connection->getPeerIDs(); if (!m_outgoing_queue.empty() && !peerIds.empty()) return true; @@ -171,8 +171,8 @@ bool ConnectionSendThread::packetsQueued() void ConnectionSendThread::runTimeouts(float dtime) { - std::list timeouted_peers; - std::list peerIds = m_connection->getPeerIDs(); + std::vector timeouted_peers; + std::vector peerIds = m_connection->getPeerIDs(); for (session_t &peerId : peerIds) { PeerHelper peer = m_connection->getPeerNoEx(peerId); @@ -548,7 +548,7 @@ void ConnectionSendThread::disconnect() // Send to all - std::list peerids = m_connection->getPeerIDs(); + std::vector peerids = m_connection->getPeerIDs(); for (session_t peerid : peerids) { sendAsPacket(peerid, 0, data, false); @@ -620,7 +620,7 @@ void ConnectionSendThread::sendReliable(ConnectionCommand &c) void ConnectionSendThread::sendToAll(u8 channelnum, const SharedBuffer &data) { - std::list peerids = m_connection->getPeerIDs(); + std::vector peerids = m_connection->getPeerIDs(); for (session_t peerid : peerids) { send(peerid, channelnum, data); @@ -629,7 +629,7 @@ void ConnectionSendThread::sendToAll(u8 channelnum, const SharedBuffer &data void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c) { - std::list peerids = m_connection->getPeerIDs(); + std::vector peerids = m_connection->getPeerIDs(); for (session_t peerid : peerids) { PeerHelper peer = m_connection->getPeerNoEx(peerid); @@ -643,8 +643,8 @@ void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c) void ConnectionSendThread::sendPackets(float dtime) { - std::list peerIds = m_connection->getPeerIDs(); - std::list pendingDisconnect; + std::vector peerIds = m_connection->getPeerIDs(); + std::vector pendingDisconnect; std::map pending_unreliable; const unsigned int peer_packet_quota = m_iteration_packets_avaialble @@ -843,13 +843,11 @@ void *ConnectionReceiveThread::run() if (debug_print_timer > 20.0) { debug_print_timer -= 20.0; - std::list peerids = m_connection->getPeerIDs(); + std::vector peerids = m_connection->getPeerIDs(); - for (std::list::iterator i = peerids.begin(); - i != peerids.end(); - i++) + for (auto id : peerids) { - PeerHelper peer = m_connection->getPeerNoEx(*i); + PeerHelper peer = m_connection->getPeerNoEx(id); if (!peer) continue; @@ -1039,7 +1037,7 @@ void ConnectionReceiveThread::receive(SharedBuffer &packetdata, bool ConnectionReceiveThread::getFromBuffers(session_t &peer_id, SharedBuffer &dst) { - std::list peerids = m_connection->getPeerIDs(); + std::vector peerids = m_connection->getPeerIDs(); for (session_t peerid : peerids) { PeerHelper peer = m_connection->getPeerNoEx(peerid); -- cgit v1.2.3 From 0abb3e89fa6298041faa7e46d437e5a81f71cdd3 Mon Sep 17 00:00:00 2001 From: red-001 Date: Mon, 2 Nov 2020 21:21:03 +0000 Subject: Block attempts to connect to the client (#10589) A Minetest peer initiates a connection by sending a packet with an invalid peer_id, for whatever reason the code for doing this ran on both the client and the server meaning you could connect to a client if you knew what the address:port tuple it was listening on. --- src/network/connection.cpp | 2 +- src/network/connection.h | 5 +++++ src/network/connectionthreads.cpp | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src/network/connectionthreads.cpp') diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 1875d1461..0ba8c36b2 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -1566,7 +1566,7 @@ void Connection::sendAck(session_t peer_id, u8 channelnum, u16 seqnum) UDPPeer* Connection::createServerPeer(Address& address) { - if (getPeerNoEx(PEER_ID_SERVER) != 0) + if (ConnectedToServer()) { throw ConnectionException("Already connected to a server"); } diff --git a/src/network/connection.h b/src/network/connection.h index 2dc6d4397..24cd4fe4a 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -809,6 +809,11 @@ protected: void putEvent(ConnectionEvent &e); void TriggerSend(); + + bool ConnectedToServer() + { + return getPeerNoEx(PEER_ID_SERVER) != nullptr; + } private: MutexedQueue m_event_queue; diff --git a/src/network/connectionthreads.cpp b/src/network/connectionthreads.cpp index 28ed798d9..7b62bc792 100644 --- a/src/network/connectionthreads.cpp +++ b/src/network/connectionthreads.cpp @@ -956,8 +956,11 @@ void ConnectionReceiveThread::receive(SharedBuffer &packetdata, // command was sent reliably. } - /* The peer was not found in our lists. Add it. */ if (peer_id == PEER_ID_INEXISTENT) { + /* Ignore it if we are a client */ + if (m_connection->ConnectedToServer()) + return; + /* The peer was not found in our lists. Add it. */ peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0); } -- cgit v1.2.3