From 137baa6c04170cef7e525d123f1092833f8f3a7c Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 17 Mar 2018 12:35:36 +0100 Subject: [PATCH 1/2] expose post_dht_stats() to python binding --- ChangeLog | 1 + bindings/python/src/session.cpp | 1 + bindings/python/test.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+) diff --git a/ChangeLog b/ChangeLog index b1e5fa93e..94679ba1d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ + * expose post_dht_stats() to python binding * fix backwards compatibility to downloads without partfiles * improve part-file related error messages * fix reporting &redundant= in tracker announces diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index 513e0c1e3..3d4b46645 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -900,6 +900,7 @@ void bind_session() .def("outgoing_ports", &outgoing_ports) #endif .def("post_torrent_updates", allow_threads(<::session::post_torrent_updates), arg("flags") = 0xffffffff) + .def("post_dht_stats", allow_threads(<::session::post_dht_stats)) .def("post_session_stats", allow_threads(<::session::post_session_stats)) .def("is_listening", allow_threads(<::session::is_listening)) .def("listen_port", allow_threads(<::session::listen_port)) diff --git a/bindings/python/test.py b/bindings/python/test.py index 2c42984ba..9cdf146a3 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -383,6 +383,21 @@ class test_session(unittest.TestCase): self.assertTrue(isinstance(a.values, dict)) self.assertTrue(len(a.values) > 0) + def test_post_dht_stats(self): + s = lt.session({'alert_mask': lt.alert.category_t.stats_notification, 'enable_dht': False}) + s.post_dht_stats() + alerts = [] + # first the stats headers log line. but not if logging is disabled + time.sleep(1) + alerts = s.pop_alerts() + a = alerts.pop(0) + while not isinstance(a, lt.dht_stats_alert): + a = alerts.pop(0 +) + self.assertTrue(isinstance(a, lt.dht_stats_alert)) + self.assertTrue(isinstance(a.active_requests, list)) + self.assertTrue(isinstance(a.routing_table, list)) + def test_unknown_settings(self): try: s = lt.session({'unexpected-key-name': 42}) From 09bf97b79efe6647c1b6aeff2a6e3013852391c6 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 5 Feb 2018 01:29:14 +0100 Subject: [PATCH 2/2] minor fixes in utp_socket_impl --- src/utp_stream.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) 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();