From bcd93da465428b472d23010fe03ed923d97ff825 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 4 Feb 2009 05:51:00 +0000 Subject: [PATCH] fixed encryption asserts --- include/libtorrent/bt_peer_connection.hpp | 2 +- src/bt_peer_connection.cpp | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index f1d252b83..81a645cd6 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -287,7 +287,7 @@ public: m_RC4_handler->encrypt(buffer, size); #ifdef TORRENT_DEBUG m_encrypted_bytes += size; - TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size() + size); + TORRENT_ASSERT(m_encrypted_bytes == send_buffer_size() + size); #endif } #endif diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 4fdb91653..d7b04c773 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -443,6 +443,8 @@ namespace libtorrent int pad_size = rand() % 512; + TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes); + // synchash,skeyhash,vc,crypto_provide,len(pad),pad,len(ia) buffer::interval send_buf = allocate_send_buffer(20 + 20 + 8 + 4 + 2 + pad_size + 2); @@ -506,7 +508,7 @@ namespace libtorrent const int packet_size = 20 + 20 + 8 + 4 + 2 + pad_size + 2; TORRENT_ASSERT(send_buffer_size() - packet_size == m_encrypted_bytes); m_encrypted_bytes += packet_size; - TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size()); + TORRENT_ASSERT(m_encrypted_bytes == send_buffer_size()); #endif TORRENT_ASSERT(send_buf.begin == send_buf.end); @@ -525,6 +527,8 @@ namespace libtorrent int pad_size =rand() % 512; + TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes); + const int buf_size = 8 + 4 + 2 + pad_size; buffer::interval send_buf = allocate_send_buffer(buf_size); if (send_buf.begin == 0) return; // out of memory @@ -636,7 +640,6 @@ namespace libtorrent m_RC4_handler->encrypt(const_cast(buf), size); #ifdef TORRENT_DEBUG m_encrypted_bytes += size; - TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size() + size); #endif } @@ -680,6 +683,7 @@ namespace libtorrent void bt_peer_connection::setup_send() { encrypt_pending_buffer(); + TORRENT_ASSERT(!m_encrypted || !m_rc4_encrypted || m_encrypted_bytes == send_buffer_size()); peer_connection::setup_send(); } @@ -2777,9 +2781,16 @@ namespace libtorrent #ifdef TORRENT_DEBUG if (m_encrypted_bytes > 0) { - m_encrypted_bytes -= bytes_transferred; + if (m_rc4_encrypted) + { + m_encrypted_bytes -= bytes_transferred; + TORRENT_ASSERT(m_encrypted_bytes >= 0); + } + else + { + m_encrypted_bytes -= (std::min)(int(bytes_transferred), m_encrypted_bytes); + } TORRENT_ASSERT(m_encrypted_bytes >= 0); - TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size()); } #endif