From e2e4a34429f7fbe1a850d62ac002a23f69955588 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 7 Apr 2008 01:39:29 +0000 Subject: [PATCH] removed exceptions from metadata extensions --- src/metadata_transfer.cpp | 54 +++++++++++++++++++++++++-------------- src/ut_metadata.cpp | 30 ++++++++++++---------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/metadata_transfer.cpp b/src/metadata_transfer.cpp index 303942231..f5dcab181 100644 --- a/src/metadata_transfer.cpp +++ b/src/metadata_transfer.cpp @@ -281,17 +281,14 @@ namespace libtorrent { namespace // called when the extension handshake from the other end is received virtual bool on_extension_handshake(entry const& h) { - entry const& messages = h["m"]; - if (entry const* index = messages.find_key("LT_metadata")) - { - m_message_index = int(index->integer()); - return true; - } - else - { - m_message_index = 0; - return false; - } + m_message_index = 0; + entry const* messages = h.find_key("m"); + if (!messages || messages->type() != entry::dictionary_t) return false; + + entry const* index = messages->find_key("LT_metadata"); + if (!index || index->type() != entry::int_t) return false; + m_message_index = int(index->integer()); + return true; } void write_metadata_request(std::pair req) @@ -376,7 +373,10 @@ namespace libtorrent { namespace if (m_message_index == 0) return false; if (length > 500 * 1024) - throw protocol_error("LT_metadata message larger than 500 kB"); + { + m_pc.disconnect("LT_metadata message larger than 500 kB"); + return true; + } if (body.left() < 1) return true; int type = detail::read_uint8(body.begin); @@ -392,7 +392,8 @@ namespace libtorrent { namespace if (length != 3) { // invalid metadata request - throw protocol_error("invalid metadata request"); + m_pc.disconnect("invalid metadata request"); + return true; } write_metadata(std::make_pair(start, size)); @@ -407,13 +408,25 @@ namespace libtorrent { namespace int data_size = length - 9; if (total_size > 500 * 1024) - throw protocol_error("metadata size larger than 500 kB"); + { + m_pc.disconnect("metadata size larger than 500 kB"); + return true; + } if (total_size <= 0) - throw protocol_error("invalid metadata size"); + { + m_pc.disconnect("invalid metadata size"); + return true; + } if (offset > total_size || offset < 0) - throw protocol_error("invalid metadata offset"); + { + m_pc.disconnect("invalid metadata offset"); + return true; + } if (offset + data_size > total_size) - throw protocol_error("invalid metadata message"); + { + m_pc.disconnect("invalid metadata message"); + return true; + } m_tp.metadata_progress(total_size , body.left() - m_metadata_progress); @@ -434,8 +447,11 @@ namespace libtorrent { namespace m_waiting_metadata_request = false; break; default: - throw protocol_error("unknown metadata extension message: " - + boost::lexical_cast(type)); + { + std::stringstream msg; + msg << "unknown metadata extension message: " << type; + m_pc.disconnect(msg.str().c_str()); + } } return true; } diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 34f39deda..4cefcdc7c 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -241,17 +241,13 @@ namespace libtorrent { namespace if (metadata_size && metadata_size->type() == entry::int_t) m_tp.metadata_size(metadata_size->integer()); - entry const& messages = h["m"]; - if (entry const* index = messages.find_key("ut_metadata")) - { - m_message_index = index->integer(); - return true; - } - else - { - m_message_index = 0; - return false; - } + entry const* messages = h.find_key("m"); + if (!messages || messages->type() != entry::dictionary_t) return false; + + entry const* index = messages->find_key("ut_metadata"); + if (!index || index->type() != entry::int_t) return false; + m_message_index = int(index->integer()); + return true; } void write_metadata_packet(int type, int piece) @@ -309,7 +305,10 @@ namespace libtorrent { namespace if (m_message_index == 0) return false; if (length > 17 * 1024) - throw protocol_error("ut_metadata message larger than 17 kB"); + { + m_pc.disconnect("ut_metadata message larger than 17 kB"); + return true; + } if (!m_pc.packet_finished()) return true; @@ -362,8 +361,11 @@ namespace libtorrent { namespace } break; default: - throw protocol_error("unknown metadata extension message: " - + boost::lexical_cast(type)); + { + std::stringstream msg; + msg << "unknown ut_metadata extension message: " << type; + m_pc.disconnect(msg.str().c_str()); + } } return true; }