[network/libktorrent] src/tracker: ABI/API break: Use QByteArrayView for udp tracker socket functions
Jack Hill <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 21bf78c34b96bfd0fd4be72c6467d764e0e5c4df by Jack Hill.
Committed on 14/08/2026 at 20:44.
Pushed by jackh into branch 'master'.
ABI/API break: Use QByteArrayView for udp tracker socket functions
No downstream changes required.
M +35 -31 src/tracker/udptracker.cpp
M +2 -2 src/tracker/udptracker.h
M +16 -11 src/tracker/udptrackersocket.cpp
M +11 -12 src/tracker/udptrackersocket.h
https://invent.kde.org/network/libktorrent/-/commit/21bf78c34b96bfd0fd4be72c6467d764e0e5c4df
diff --git a/src/tracker/udptracker.cpp b/src/tracker/udptracker.cpp
index 34dd872e..a7df355b 100644
--- a/src/tracker/udptracker.cpp
+++ b/src/tracker/udptracker.cpp
@@ -4,9 +4,13 @@
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "udptracker.h"
+
+#include <array>
+#include <cstddef>
+#include <cstdlib>
+
#include "udptrackersocket.h"
#include <KLocalizedString>
-#include <cstdlib>
#include <interfaces/torrentinterface.h>
#include <net/addressresolver.h>
#include <peer/peermanager.h>
@@ -115,9 +119,9 @@ void UDPTracker::connectReceived(Int32 tid, Int64 cid)
}
}
-void UDPTracker::announceReceived(Int32 tid, const bt::Uint8 *buf, bt::Uint32 size)
+void UDPTracker::announceReceived(Int32 tid, QByteArrayView buf)
{
- if (tid != transaction_id || size < 20) {
+ if (tid != transaction_id || buf.size() < 20) {
return;
}
@@ -131,11 +135,11 @@ void UDPTracker::announceReceived(Int32 tid, const bt::Uint8 *buf, bt::Uint32 si
24 + 6 * n 16-bit integer TCP port
20 + 6 * N
*/
- interval = ReadInt32(buf, 8);
- leechers = ReadInt32(buf, 12);
- seeders = ReadInt32(buf, 16);
+ interval = ReadInt32(buf.data(), 8);
+ leechers = ReadInt32(buf.data(), 12);
+ seeders = ReadInt32(buf.data(), 16);
- const auto ip_list = QByteArrayView{buf, size}.sliced(20);
+ const auto ip_list = buf.sliced(20);
const auto num_peers = ip_list.size() / 6;
for (Uint32 i = 0; i < num_peers; ++i) {
addPeer(net::Address::fromCompactIPv4(ip_list.sliced(i * 6, 6)), false);
@@ -206,7 +210,7 @@ void UDPTracker::scrape()
}
}
-void UDPTracker::scrapeReceived(Int32 tid, const Uint8 *buf, Uint32 size)
+void UDPTracker::scrapeReceived(Int32 tid, QByteArrayView buf)
{
/*
0 32-bit integer action 2
@@ -216,13 +220,13 @@ void UDPTracker::scrapeReceived(Int32 tid, const Uint8 *buf, Uint32 size)
16 + 12 * n 32-bit integer leechers
8 + 12 * N
*/
- if (tid != scrape_transaction_id || size < 20) {
+ if (tid != scrape_transaction_id || buf.size() < 20) {
return;
}
- seeders = ReadInt32(buf, 8);
- total_downloaded = ReadInt32(buf, 12);
- leechers = ReadInt32(buf, 16);
+ seeders = ReadInt32(buf.data(), 8);
+ total_downloaded = ReadInt32(buf.data(), 12);
+ leechers = ReadInt32(buf.data(), 16);
Out(SYS_TRK | LOG_DEBUG) << "Scrape : leechers = " << leechers << ", seeders = " << seeders << ", downloaded = " << total_downloaded << endl;
}
@@ -268,20 +272,20 @@ void UDPTracker::sendAnnounce()
const Uint32 ip_addr = cip.isNull() ? 0 : QHostAddress{cip}.toIPv4Address();
const Int32 num_want = ev != STOPPED ? 100 : 0;
- Uint8 buf[98];
- WriteInt64(buf, 0, connection_id);
- WriteInt32(buf, 8, UDPTrackerSocket::ANNOUNCE);
- WriteInt32(buf, 12, transaction_id);
- memcpy(buf + 16, info_hash.getData(), 20);
- memcpy(buf + 36, peer_id.data(), 20);
- WriteInt64(buf, 56, bytesDownloaded());
- WriteInt64(buf, 64, bytes_left);
- WriteInt64(buf, 72, bytesUploaded());
- WriteInt32(buf, 80, ev);
- WriteUint32(buf, 84, ip_addr);
- WriteUint32(buf, 88, key);
- WriteInt32(buf, 92, num_want);
- WriteUint16(buf, 96, port);
+ std::array<std::byte, 98> buf;
+ WriteInt64(buf.data(), 0, connection_id);
+ WriteInt32(buf.data(), 8, UDPTrackerSocket::ANNOUNCE);
+ WriteInt32(buf.data(), 12, transaction_id);
+ memcpy(buf.data() + 16, info_hash.getData(), 20);
+ memcpy(buf.data() + 36, peer_id.data(), 20);
+ WriteInt64(buf.data(), 56, bytesDownloaded());
+ WriteInt64(buf.data(), 64, bytes_left);
+ WriteInt64(buf.data(), 72, bytesUploaded());
+ WriteInt32(buf.data(), 80, ev);
+ WriteUint32(buf.data(), 84, ip_addr);
+ WriteUint32(buf.data(), 88, key);
+ WriteInt32(buf.data(), 92, num_want);
+ WriteUint16(buf.data(), 96, port);
socket->sendAnnounce(transaction_id, buf, address);
}
@@ -299,11 +303,11 @@ void UDPTracker::sendScrape()
scrape_transaction_id = socket->newTransactionID();
const SHA1Hash &info_hash = tds->infoHash();
- Uint8 buf[36];
- WriteInt64(buf, 0, connection_id);
- WriteInt32(buf, 8, UDPTrackerSocket::SCRAPE);
- WriteInt32(buf, 12, scrape_transaction_id);
- memcpy(buf + 16, info_hash.getData(), 20);
+ std::array<std::byte, 36> buf;
+ WriteInt64(buf.data(), 0, connection_id);
+ WriteInt32(buf.data(), 8, UDPTrackerSocket::SCRAPE);
+ WriteInt32(buf.data(), 12, scrape_transaction_id);
+ memcpy(buf.data() + 16, info_hash.getData(), 20);
socket->sendScrape(scrape_transaction_id, buf, address);
}
diff --git a/src/tracker/udptracker.h b/src/tracker/udptracker.h
index e1fce3dd..ca040c36 100644
--- a/src/tracker/udptracker.h
+++ b/src/tracker/udptracker.h
@@ -48,8 +48,8 @@ public:
private Q_SLOTS:
void onConnTimeout();
void connectReceived(Int32 tid, Int64 connection_id);
- void announceReceived(Int32 tid, const Uint8 *buf, Uint32 size);
- void scrapeReceived(Int32 tid, const Uint8 *buf, Uint32 size);
+ void announceReceived(Int32 tid, QByteArrayView buf);
+ void scrapeReceived(Int32 tid, QByteArrayView buf);
void onError(Int32 tid, const QString &error_string);
void onResolverResults(net::AddressResolver *ar);
void manualUpdate() override;
diff --git a/src/tracker/udptrackersocket.cpp b/src/tracker/udptrackersocket.cpp
index a270a329..6a8fcff5 100644
--- a/src/tracker/udptrackersocket.cpp
+++ b/src/tracker/udptrackersocket.cpp
@@ -9,9 +9,12 @@
#include <array>
#include <cstddef>
-#include <KLocalizedString>
#include <QHostAddress>
#include <QRandomGenerator>
+#include <QtAssert>
+
+#include <KLocalizedString>
+
#include <net/portlist.h>
#include <net/serversocket.h>
#include <net/socket.h>
@@ -138,15 +141,17 @@ void UDPTrackerSocket::sendConnect(Int32 tid, const net::Address &addr)
d->transactions.insert(tid, CONNECT);
}
-void UDPTrackerSocket::sendAnnounce(Int32 tid, const Uint8 *data, const net::Address &addr)
+void UDPTrackerSocket::sendAnnounce(Int32 tid, QByteArrayView data, const net::Address &addr)
{
- d->send(QByteArrayView{data, 98}, addr);
+ Q_ASSERT(data.size() == 98);
+ d->send(data, addr);
d->transactions.insert(tid, ANNOUNCE);
}
-void UDPTrackerSocket::sendScrape(Int32 tid, const bt::Uint8 *data, const net::Address &addr)
+void UDPTrackerSocket::sendScrape(Int32 tid, QByteArrayView data, const net::Address &addr)
{
- d->send(QByteArrayView{data, 36}, addr);
+ Q_ASSERT(data.size() == 36);
+ d->send(data, addr);
d->transactions.insert(tid, SCRAPE);
}
@@ -155,7 +160,7 @@ void UDPTrackerSocket::cancelTransaction(Int32 tid)
d->transactions.remove(tid);
}
-void UDPTrackerSocket::handleConnect(const bt::Buffer &buf)
+void UDPTrackerSocket::handleConnect(QByteArrayView buf)
{
if (buf.size() < 16) {
return;
@@ -181,7 +186,7 @@ void UDPTrackerSocket::handleConnect(const bt::Buffer &buf)
Q_EMIT connectReceived(tid, ReadInt64(buf.data(), 8));
}
-void UDPTrackerSocket::handleAnnounce(const bt::Buffer &buf)
+void UDPTrackerSocket::handleAnnounce(QByteArrayView buf)
{
if (buf.size() < 20) {
return;
@@ -204,10 +209,10 @@ void UDPTrackerSocket::handleAnnounce(const bt::Buffer &buf)
// everything ok, emit signal
d->transactions.erase(i);
- Q_EMIT announceReceived(tid, buf.data(), buf.size());
+ Q_EMIT announceReceived(tid, buf);
}
-void UDPTrackerSocket::handleError(const bt::Buffer &buf)
+void UDPTrackerSocket::handleError(QByteArrayView buf)
{
if (buf.size() < 8) {
return;
@@ -232,7 +237,7 @@ void UDPTrackerSocket::handleError(const bt::Buffer &buf)
Q_EMIT error(tid, msg);
}
-void UDPTrackerSocket::handleScrape(const bt::Buffer &buf)
+void UDPTrackerSocket::handleScrape(QByteArrayView buf)
{
if (buf.size() < 20) {
return;
@@ -255,7 +260,7 @@ void UDPTrackerSocket::handleScrape(const bt::Buffer &buf)
// everything ok, emit signal
d->transactions.erase(i);
- Q_EMIT scrapeReceived(tid, buf.data(), buf.size());
+ Q_EMIT scrapeReceived(tid, buf);
}
Int32 UDPTrackerSocket::newTransactionID()
diff --git a/src/tracker/udptrackersocket.h b/src/tracker/udptrackersocket.h
index d97775fb..622f1e66 100644
--- a/src/tracker/udptrackersocket.h
+++ b/src/tracker/udptrackersocket.h
@@ -7,6 +7,7 @@
#define BTUDPTRACKERSOCKET_H
#include <QByteArray>
+#include <QByteArrayView>
#include <QObject>
#include <ktorrent_export.h>
#include <util/bufferpool.h>
@@ -76,20 +77,20 @@ public:
* signal will be emitted, classes recieving this signal should check if
* the transaction_id is the same.
* \param tid The transaction_id
- * \param data The data to send (connect input structure, in UDP Tracker specifaction)
+ * \param data The data to send (announce input structure, in UDP Tracker specification)
* \param addr The address to send to
*/
- void sendAnnounce(Int32 tid, const Uint8 *data, const net::Address &addr);
+ void sendAnnounce(Int32 tid, QByteArrayView data, const net::Address &addr);
/*!
* Send a scrape message. As a response to this, the scrapeReceived
* signal will be emitted, classes recieving this signal should check if
* the transaction_id is the same.
* \param tid The transaction_id
- * \param data The data to send (connect input structure, in UDP Tracker specifaction)
+ * \param data The data to send (scrape input structure, in UDP Tracker specification)
* \param addr The address to send to
*/
- void sendScrape(Int32 tid, const Uint8 *data, const net::Address &addr);
+ void sendScrape(Int32 tid, QByteArrayView data, const net::Address &addr);
/*!
* If a transaction times out, this should be used to cancel it.
@@ -124,17 +125,15 @@ Q_SIGNALS:
* Emitted when an announce message is received.
* \param tid The transaction_id
* \param buf The data
- * \param size The data size
*/
- void announceReceived(Int32 tid, const Uint8 *buf, Uint32 size);
+ void announceReceived(Int32 tid, QByteArrayView buf);
/*!
* Emitted when a scrape message is received.
* \param tid The transaction_id
* \param buf The data
- * \param size The data size
*/
- void scrapeReceived(Int32 tid, const Uint8 *buf, Uint32 size);
+ void scrapeReceived(Int32 tid, QByteArrayView buf);
/*!
* Signal emitted, when an error occurs during a transaction.
@@ -144,10 +143,10 @@ Q_SIGNALS:
void error(Int32 tid, const QString &error_string);
private:
- void handleConnect(const bt::Buffer &buf);
- void handleAnnounce(const bt::Buffer &buf);
- void handleError(const bt::Buffer &buf);
- void handleScrape(const bt::Buffer &buf);
+ void handleConnect(QByteArrayView buf);
+ void handleAnnounce(QByteArrayView buf);
+ void handleError(QByteArrayView buf);
+ void handleScrape(QByteArrayView buf);
private:
class Private;