Skip to content

Commit

Permalink
libtorrent: Add m_tracker_udp_list (#39)
Browse files Browse the repository at this point in the history
We can't send UDP tracker object references through std:bind() because it copies them, resulting in memory address errors. So we need to store them higher up in the connection manager, where they are always valid.
  • Loading branch information
stickz authored Jul 19, 2024
1 parent 1281df8 commit 80f01db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
6 changes: 3 additions & 3 deletions libtorrent/src/torrent/connection_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class UdnsAsyncResolver : public AsyncResolver {
};

void
ConnectionManager::start_udp_announce(TrackerUdp *tracker, const sockaddr* sa, int err) {
if (tracker != NULL) {
tracker->start_announce(sa, err);
ConnectionManager::start_udp_announce(uint64_t idx, const sockaddr* sa, int err) {
if (m_tracker_udp_list[idx] != NULL) {
m_tracker_udp_list[idx]->start_announce(sa, err);
}
}

Expand Down
9 changes: 8 additions & 1 deletion libtorrent/src/torrent/connection_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ class LIBTORRENT_EXPORT ConnectionManager {
void cancel_async_resolve(void *query);

#ifdef USE_UDNS
void start_udp_announce(TrackerUdp *tracker, const sockaddr* sa, int err);
void start_udp_announce(uint64_t idx, const sockaddr* sa, int err);
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(); }
#endif

// Legacy synchronous resolver interface.
Expand Down Expand Up @@ -227,6 +230,10 @@ class LIBTORRENT_EXPORT ConnectionManager {
slot_resolver_type m_slot_resolver;
slot_throttle_type m_slot_address_throttle;

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

std::unique_ptr<AsyncResolver> m_async_resolver;
};

Expand Down
7 changes: 6 additions & 1 deletion libtorrent/src/tracker/tracker_udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ TrackerUdp::TrackerUdp(TrackerList* parent, rak::udp_tracker_info& info, int fla
m_taskTimeout.slot() = std::bind(&TrackerUdp::receive_timeout, this);

#ifdef USE_UDNS
m_resolver_callback = std::bind(&ConnectionManager::start_udp_announce, manager->connection_manager(), this, std::placeholders::_1, std::placeholders::_2);
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);
#else
m_resolver_callback = std::bind(&TrackerUdp::start_announce, this, std::placeholders::_1, std::placeholders::_2);
#endif
Expand All @@ -86,6 +88,9 @@ TrackerUdp::TrackerUdp(TrackerList* parent, rak::udp_tracker_info& info, int fla
}

TrackerUdp::~TrackerUdp() {
#ifdef USE_UDNS
manager->connection_manager()->null_udp_tracker(m_vec_idx);
#endif
close_directly();
}

Expand Down
4 changes: 4 additions & 0 deletions libtorrent/src/tracker/tracker_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class TrackerUdp : public SocketDatagram, public Tracker {
uint32_t m_action;
uint64_t m_connectionId;
uint32_t m_transactionId;

#ifdef USE_UDNS
uint64_t m_vec_idx;
#endif

ReadBuffer* m_readBuffer;
WriteBuffer* m_writeBuffer;
Expand Down

0 comments on commit 80f01db

Please sign in to comment.