From 5517bef3cc71f1ac6191b89ae1194080385ddb18 Mon Sep 17 00:00:00 2001 From: stickz Date: Fri, 19 Jul 2024 21:03:56 -0400 Subject: [PATCH] libtorrent: Move resolver callback to connection manager (#40) We need to move the resolver callback up to the connection manager, so it's always accessible by UDNS. Otherwise, we crash. --- libtorrent/src/torrent/connection_manager.cc | 6 ++++++ libtorrent/src/torrent/connection_manager.h | 4 ++++ libtorrent/src/tracker/tracker_udp.cc | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/libtorrent/src/torrent/connection_manager.cc b/libtorrent/src/torrent/connection_manager.cc index 82d88308..43137203 100644 --- a/libtorrent/src/torrent/connection_manager.cc +++ b/libtorrent/src/torrent/connection_manager.cc @@ -85,6 +85,12 @@ ConnectionManager::start_udp_announce(uint64_t idx, const sockaddr* sa, int err) } } +void +ConnectionManager::add_resolver_callback(uint64_t idx) { + resolver_callback c = std::bind(&ConnectionManager::start_udp_announce, this, idx, std::placeholders::_1, std::placeholders::_2); + m_resolver_callback_list.push_back(c); +} + #define ASYNC_RESOLVER_IMPL UdnsAsyncResolver #else class StubAsyncResolver : public AsyncResolver { diff --git a/libtorrent/src/torrent/connection_manager.h b/libtorrent/src/torrent/connection_manager.h index ee64b29b..7efd3337 100644 --- a/libtorrent/src/torrent/connection_manager.h +++ b/libtorrent/src/torrent/connection_manager.h @@ -191,6 +191,9 @@ class LIBTORRENT_EXPORT ConnectionManager { void null_udp_tracker(uint64_t idx) { m_tracker_udp_list[idx] = NULL; } void add_udp_tracker(TrackerUdp* tracker) { m_tracker_udp_list.push_back(tracker); } uint64_t get_udp_tracker_count() { return m_tracker_udp_list.size(); } + + void add_resolver_callback(uint64_t idx); + resolver_callback* get_resolver_callback(uint64_t idx) { return &m_resolver_callback_list[idx]; } #endif // Legacy synchronous resolver interface. @@ -232,6 +235,7 @@ class LIBTORRENT_EXPORT ConnectionManager { #ifdef USE_UDNS std::vector m_tracker_udp_list; + std::vector m_resolver_callback_list; #endif std::unique_ptr m_async_resolver; diff --git a/libtorrent/src/tracker/tracker_udp.cc b/libtorrent/src/tracker/tracker_udp.cc index 5d611522..afe5c8ab 100644 --- a/libtorrent/src/tracker/tracker_udp.cc +++ b/libtorrent/src/tracker/tracker_udp.cc @@ -79,7 +79,7 @@ TrackerUdp::TrackerUdp(TrackerList* parent, rak::udp_tracker_info& info, int fla #ifdef USE_UDNS manager->connection_manager()->add_udp_tracker(this); m_vec_idx = manager->connection_manager()->get_udp_tracker_count() - 1; - m_resolver_callback = std::bind(&ConnectionManager::start_udp_announce, manager->connection_manager(), m_vec_idx, std::placeholders::_1, std::placeholders::_2); + manager->connection_manager()->add_resolver_callback(m_vec_idx); #else m_resolver_callback = std::bind(&TrackerUdp::start_announce, this, std::placeholders::_1, std::placeholders::_2); #endif @@ -111,7 +111,11 @@ TrackerUdp::send_state(int state) { m_resolver_query = manager->connection_manager()->async_resolver().enqueue( m_hostname.c_str(), AF_UNSPEC, +#ifdef USE_UDNS + manager->connection_manager()->get_resolver_callback(m_vec_idx) +#else &m_resolver_callback +#endif ); manager->connection_manager()->async_resolver().flush(); }