Skip to content

Commit

Permalink
libtorrent: Move resolver callback to connection manager (#40)
Browse files Browse the repository at this point in the history
We need to move the resolver callback up to the connection manager, so it's always accessible by UDNS. Otherwise, we crash.
  • Loading branch information
stickz authored Jul 20, 2024
1 parent 80f01db commit 5517bef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions libtorrent/src/torrent/connection_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions libtorrent/src/torrent/connection_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -232,6 +235,7 @@ class LIBTORRENT_EXPORT ConnectionManager {

#ifdef USE_UDNS
std::vector<TrackerUdp*> m_tracker_udp_list;
std::vector<resolver_callback> m_resolver_callback_list;
#endif

std::unique_ptr<AsyncResolver> m_async_resolver;
Expand Down
6 changes: 5 additions & 1 deletion libtorrent/src/tracker/tracker_udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 5517bef

Please sign in to comment.