2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
33 #include "base/bittorrent/ltqbitarray.h"
34 #include "base/net/geoipmanager.h"
35 #include "base/unicodestrings.h"
36 #include "base/utils/bytearray.h"
37 #include "peeraddress.h"
39 using namespace BitTorrent
;
41 PeerInfo::PeerInfo(const lt::peer_info
&nativeInfo
, const QBitArray
&allPieces
)
42 : m_nativeInfo(nativeInfo
)
43 , m_relevance(calcRelevance(allPieces
))
48 bool PeerInfo::fromDHT() const
50 return static_cast<bool>(m_nativeInfo
.source
& lt::peer_info::dht
);
53 bool PeerInfo::fromPeX() const
55 return static_cast<bool>(m_nativeInfo
.source
& lt::peer_info::pex
);
58 bool PeerInfo::fromLSD() const
60 return static_cast<bool>(m_nativeInfo
.source
& lt::peer_info::lsd
);
63 QString
PeerInfo::country() const
65 if (m_country
.isEmpty())
66 m_country
= Net::GeoIPManager::instance()->lookup(address().ip
);
70 bool PeerInfo::isInteresting() const
72 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::interesting
);
75 bool PeerInfo::isChocked() const
77 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::choked
);
80 bool PeerInfo::isRemoteInterested() const
82 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::remote_interested
);
85 bool PeerInfo::isRemoteChocked() const
87 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::remote_choked
);
90 bool PeerInfo::isSupportsExtensions() const
92 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::supports_extensions
);
95 bool PeerInfo::isLocalConnection() const
97 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::local_connection
);
100 bool PeerInfo::isHandshake() const
102 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::handshake
);
105 bool PeerInfo::isConnecting() const
107 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::connecting
);
110 bool PeerInfo::isOnParole() const
112 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::on_parole
);
115 bool PeerInfo::isSeed() const
117 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::seed
);
120 bool PeerInfo::optimisticUnchoke() const
122 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::optimistic_unchoke
);
125 bool PeerInfo::isSnubbed() const
127 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::snubbed
);
130 bool PeerInfo::isUploadOnly() const
132 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::upload_only
);
135 bool PeerInfo::isEndgameMode() const
137 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::endgame_mode
);
140 bool PeerInfo::isHolepunched() const
142 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::holepunched
);
145 bool PeerInfo::useI2PSocket() const
147 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::i2p_socket
);
150 bool PeerInfo::useUTPSocket() const
152 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::utp_socket
);
155 bool PeerInfo::useSSLSocket() const
157 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::ssl_socket
);
160 bool PeerInfo::isRC4Encrypted() const
162 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::rc4_encrypted
);
165 bool PeerInfo::isPlaintextEncrypted() const
167 return static_cast<bool>(m_nativeInfo
.flags
& lt::peer_info::plaintext_encrypted
);
170 PeerAddress
PeerInfo::address() const
175 // fast path for platforms which boost.asio internal struct maps to `sockaddr`
176 return {QHostAddress(m_nativeInfo
.ip
.data()), m_nativeInfo
.ip
.port()};
177 // slow path for the others
178 //return {QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string()))
179 // , m_nativeInfo.ip.port()};
182 QString
PeerInfo::I2PAddress() const
187 #if defined(QBT_USES_LIBTORRENT2) && TORRENT_USE_I2P
188 if (m_I2PAddress
.isEmpty())
190 const lt::sha256_hash destHash
= m_nativeInfo
.i2p_destination();
191 const QByteArray base32Dest
= Utils::ByteArray::toBase32({destHash
.data(), destHash
.size()}).replace('=', "").toLower();
192 m_I2PAddress
= QString::fromLatin1(base32Dest
) + u
".b32.i2p";
199 QString
PeerInfo::client() const
201 auto client
= QString::fromStdString(m_nativeInfo
.client
).simplified();
203 // remove non-printable characters
204 erase_if(client
, [](const QChar
&c
) { return !c
.isPrint(); });
209 QString
PeerInfo::peerIdClient() const
211 // when peer ID is not known yet it contains only zero bytes,
212 // do not create string in such case, return empty string instead
213 if (m_nativeInfo
.pid
.is_all_zeros())
218 // interesting part of a typical peer ID is first 8 chars
219 for (int i
= 0; i
< 8; ++i
)
221 const std::uint8_t c
= m_nativeInfo
.pid
[i
];
223 // ensure that the peer ID slice consists only of printable ASCII characters,
224 // this should filter out most of the improper IDs
225 if ((c
< 32) || (c
> 126))
226 return tr("Unknown");
228 result
+= QChar::fromLatin1(c
);
234 qreal
PeerInfo::progress() const
236 return m_nativeInfo
.progress
;
239 int PeerInfo::payloadUpSpeed() const
241 return m_nativeInfo
.payload_up_speed
;
244 int PeerInfo::payloadDownSpeed() const
246 return m_nativeInfo
.payload_down_speed
;
249 qlonglong
PeerInfo::totalUpload() const
251 return m_nativeInfo
.total_upload
;
254 qlonglong
PeerInfo::totalDownload() const
256 return m_nativeInfo
.total_download
;
259 QBitArray
PeerInfo::pieces() const
261 return LT::toQBitArray(m_nativeInfo
.pieces
);
264 QString
PeerInfo::connectionType() const
266 if (m_nativeInfo
.flags
& lt::peer_info::utp_socket
)
269 return (m_nativeInfo
.connection_type
== lt::peer_info::standard_bittorrent
)
274 qreal
PeerInfo::calcRelevance(const QBitArray
&allPieces
) const
276 const int localMissing
= allPieces
.count(false);
277 if (localMissing
<= 0)
280 const QBitArray peerPieces
= pieces();
281 const int remoteHaves
= (peerPieces
& (~allPieces
)).count(true);
282 return static_cast<qreal
>(remoteHaves
) / localMissing
;
285 qreal
PeerInfo::relevance() const
290 void PeerInfo::determineFlags()
292 const auto updateFlags
= [this](const QChar specifier
, const QString
&explanation
)
294 m_flags
+= (specifier
+ u
' ');
295 m_flagsDescription
+= u
"%1 = %2\n"_s
.arg(specifier
, explanation
);
300 if (isRemoteChocked())
302 // d = Your client wants to download, but peer doesn't want to send (interested and choked)
303 updateFlags(u
'd', tr("Interested (local) and choked (peer)"));
307 // D = Currently downloading (interested and not choked)
308 updateFlags(u
'D', tr("Interested (local) and unchoked (peer)"));
312 if (isRemoteInterested())
316 // u = Peer wants your client to upload, but your client doesn't want to (interested and choked)
317 updateFlags(u
'u', tr("Interested (peer) and choked (local)"));
321 // U = Currently uploading (interested and not choked)
322 updateFlags(u
'U', tr("Interested (peer) and unchoked (local)"));
326 // K = Peer is unchoking your client, but your client is not interested
327 if (!isRemoteChocked() && !isInteresting())
328 updateFlags(u
'K', tr("Not interested (local) and unchoked (peer)"));
330 // ? = Your client unchoked the peer but the peer is not interested
331 if (!isChocked() && !isRemoteInterested())
332 updateFlags(u
'?', tr("Not interested (peer) and unchoked (local)"));
334 // O = Optimistic unchoke
335 if (optimisticUnchoke())
336 updateFlags(u
'O', tr("Optimistic unchoke"));
338 // S = Peer is snubbed
340 updateFlags(u
'S', tr("Peer snubbed"));
342 // I = Peer is an incoming connection
343 if (!isLocalConnection())
344 updateFlags(u
'I', tr("Incoming connection"));
346 // H = Peer was obtained through DHT
348 updateFlags(u
'H', tr("Peer from DHT"));
350 // X = Peer was included in peerlists obtained through Peer Exchange (PEX)
352 updateFlags(u
'X', tr("Peer from PEX"));
356 updateFlags(u
'L', tr("Peer from LSD"));
358 // E = Peer is using Protocol Encryption (all traffic)
359 if (isRC4Encrypted())
360 updateFlags(u
'E', tr("Encrypted traffic"));
362 // e = Peer is using Protocol Encryption (handshake)
363 if (isPlaintextEncrypted())
364 updateFlags(u
'e', tr("Encrypted handshake"));
366 // P = Peer is using uTorrent uTP
368 updateFlags(u
'P', C_UTP
);
370 // h = Peer is using NAT hole punching
372 updateFlags(u
'h', tr("Peer is using NAT hole punching"));
375 m_flagsDescription
.chop(1);
378 QString
PeerInfo::flags() const
383 QString
PeerInfo::flagsDescription() const
385 return m_flagsDescription
;
388 int PeerInfo::downloadingPieceIndex() const
390 return static_cast<int>(m_nativeInfo
.downloading_piece_index
);