diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index ff53ee193..e305296c5 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -315,6 +315,8 @@ struct utp_socket_impl , m_stalled(false) , m_confirmed(false) { + TORRENT_ASSERT((m_recv_id == ((m_send_id + 1) & 0xffff)) + || (m_send_id == ((m_recv_id + 1) & 0xffff))); m_sm->inc_stats_counter(counters::num_utp_idle); TORRENT_ASSERT(m_userdata); for (int i = 0; i != num_delay_hist; ++i) @@ -2712,10 +2714,18 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size { INVARIANT_CHECK; - utp_header const* ph = reinterpret_cast(buf); - m_sm->inc_stats_counter(counters::utp_packets_in); + if (size < sizeof(utp_header)) + { + UTP_LOG("%8p: ERROR: incoming packet size too small:%d (ignored)\n" + , static_cast(this), size); + m_sm->inc_stats_counter(counters::utp_invalid_pkts_in); + return false; + } + + utp_header const* ph = reinterpret_cast(buf); + if (ph->get_version() != 1) { UTP_LOG("%8p: ERROR: incoming packet version:%d (ignored)\n" @@ -3105,7 +3115,14 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size UTP_LOGV("%8p: received ST_SYN state:%s seq_nr:%d ack_nr:%d\n" , static_cast(this), socket_state_names[m_state], m_seq_nr, m_ack_nr); #endif - TORRENT_ASSERT(m_send_id == ph->connection_id); + if (m_send_id != ph->connection_id) + { +#if TORRENT_UTP_LOG + UTP_LOGV("%8p: received invalid connection_id:%d expected: %d\n" + , static_cast(this), int(ph->connection_id), int(m_send_id)); +#endif + return false; + } TORRENT_ASSERT(m_recv_id == ((m_send_id + 1) & 0xffff)); defer_ack();