From 0d3152e4b0478b30d93162ac775b2a18572f576c Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 16 Oct 2019 23:38:51 +0800 Subject: [PATCH 1/3] Clean up "network interfaces configuration" code --- src/base/bittorrent/session.cpp | 58 +++++++++++++++++---------------- src/base/bittorrent/session.h | 2 +- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index bbd355ebf..6c31313c2 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1131,31 +1131,30 @@ void Session::configure(lt::settings_pack &settingsPack) if (port > 0) // user specified port settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0); - for (QString ip : getListeningIPs()) { - lt::error_code ec; - std::string interfacesStr; - + for (const QString &ip : asConst(getListeningIPs())) { if (ip.isEmpty()) { - ip = QLatin1String("0.0.0.0"); - interfacesStr = std::string((QString("%1:%2").arg(ip).arg(port)).toLatin1().constData()); - LogMsg(tr("qBittorrent is trying to listen on any interface port: %1" - , "e.g: qBittorrent is trying to listen on any interface port: TCP/6881") - .arg(QString::number(port)) + const QString anyIP = QHostAddress(QHostAddress::AnyIPv4).toString(); + const std::string endpoint = anyIP.toStdString() + ':' + std::to_string(port); + settingsPack.set_str(lt::settings_pack::listen_interfaces, endpoint); + LogMsg(tr("Trying to listen on IP: %1, port: %2" + , "e.g: Trying to listen on IP: 192.168.0.1, port: 6881") + .arg(anyIP, QString::number(port)) , Log::INFO); - - settingsPack.set_str(lt::settings_pack::listen_interfaces, interfacesStr); break; } - const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec); + lt::error_code ec; + const lt::address addr = lt::address::from_string(ip.toStdString(), ec); if (!ec) { - interfacesStr = std::string((addr.is_v6() ? QString("[%1]:%2") : QString("%1:%2")) - .arg(ip).arg(port).toLatin1().constData()); - LogMsg(tr("qBittorrent is trying to listen on interface %1 port: %2" - , "e.g: qBittorrent is trying to listen on interface 192.168.0.1 port: TCP/6881") - .arg(ip).arg(port) + const std::string endpoint = (addr.is_v6() + ? ('[' + addr.to_string() + ']') + : addr.to_string()) + + ':' + std::to_string(port); + settingsPack.set_str(lt::settings_pack::listen_interfaces, endpoint); + LogMsg(tr("Trying to listen on IP: %1, port: %2" + , "e.g: Trying to listen on IP: 192.168.0.1, port: 6881") + .arg(ip, QString::number(port)) , Log::INFO); - settingsPack.set_str(lt::settings_pack::listen_interfaces, interfacesStr); #ifdef Q_OS_WIN chosenIP = ip; #endif @@ -1165,22 +1164,24 @@ void Session::configure(lt::settings_pack &settingsPack) #ifdef Q_OS_WIN // On Vista+ versions and after Qt 5.5 QNetworkInterface::name() returns - // the interface's Luid and not the GUID. + // the interface's LUID and not the GUID. // Libtorrent expects GUIDs for the 'outgoing_interfaces' setting. - if (!networkInterface().isEmpty()) { - const QString guid = convertIfaceNameToGuid(networkInterface()); + const QString netInterface = networkInterface(); + if (!netInterface.isEmpty()) { + const QString guid = convertIfaceNameToGuid(netInterface); if (!guid.isEmpty()) { settingsPack.set_str(lt::settings_pack::outgoing_interfaces, guid.toStdString()); } else { settingsPack.set_str(lt::settings_pack::outgoing_interfaces, chosenIP.toStdString()); - LogMsg(tr("Could not get GUID of configured network interface. Binding to IP %1").arg(chosenIP) + LogMsg(tr("Could not get GUID of configured network interface. Binding to IP: %1").arg(chosenIP) , Log::WARNING); } } #else settingsPack.set_str(lt::settings_pack::outgoing_interfaces, networkInterface().toStdString()); #endif // Q_OS_WIN + m_listenInterfaceChanged = false; } @@ -2298,7 +2299,7 @@ void Session::networkConfigurationChange(const QNetworkConfiguration &cfg) } } -const QStringList Session::getListeningIPs() +QStringList Session::getListeningIPs() const { QStringList IPs; @@ -4244,8 +4245,8 @@ void Session::handleListenSucceededAlert(const lt::listen_succeeded_alert *p) #endif boost::system::error_code ec; - LogMsg(tr("qBittorrent is successfully listening on interface %1 port: %2/%3" - , "e.g: qBittorrent is successfully listening on interface 192.168.0.1 port: TCP/6881") + LogMsg(tr("Successfully listening on IP: %1, port: %2/%3" + , "e.g: Successfully listening on IP: 192.168.0.1, port: TCP/6881") #if (LIBTORRENT_VERSION_NUM < 10200) .arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())), Log::INFO); #else @@ -4307,8 +4308,8 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p) #endif boost::system::error_code ec; - LogMsg(tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4." - , "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use.") + LogMsg(tr("Failed to listen on IP: %1, port: %2/%3. Reason: %4" + , "e.g: Failed to listen on IP: 192.168.0.1, port: TCP/6881. Reason: already in use") #if (LIBTORRENT_VERSION_NUM < 10200) .arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port()) , QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL); @@ -4321,7 +4322,8 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p) void Session::handleExternalIPAlert(const lt::external_ip_alert *p) { boost::system::error_code ec; - LogMsg(tr("External IP: %1", "e.g. External IP: 192.168.0.1").arg(p->external_address.to_string(ec).c_str()), Log::INFO); + LogMsg(tr("Detected external IP: %1", "e.g. Detected external IP: 1.1.1.1") + .arg(p->external_address.to_string(ec).c_str()), Log::INFO); } void Session::handleSessionStatsAlert(const lt::session_stats_alert *p) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 4209be95f..6f647d895 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -520,7 +520,7 @@ namespace BitTorrent void adjustLimits(); void applyBandwidthLimits(); void processBannedIPs(lt::ip_filter &filter); - const QStringList getListeningIPs(); + QStringList getListeningIPs() const; void configureListeningInterface(); void enableTracker(bool enable); void enableBandwidthScheduler(); From cbe78baff1cb748f907b9f7f96731c5248c57667 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 17 Oct 2019 00:29:26 +0800 Subject: [PATCH 2/3] Migrate to lt::error_code --- src/base/bittorrent/private/filterparserthread.cpp | 4 +++- src/base/bittorrent/session.cpp | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/base/bittorrent/private/filterparserthread.cpp b/src/base/bittorrent/private/filterparserthread.cpp index ba275384c..c10d829a7 100644 --- a/src/base/bittorrent/private/filterparserthread.cpp +++ b/src/base/bittorrent/private/filterparserthread.cpp @@ -30,6 +30,8 @@ #include +#include + #include #include @@ -89,7 +91,7 @@ namespace bool parseIPAddress(const char *data, lt::address &address) { IPv4Parser parser; - boost::system::error_code ec; + lt::error_code ec; if (parser.tryParse(data)) address = lt::address_v4(parser.parsed()); diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 6c31313c2..5b1c1f18d 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1012,7 +1012,7 @@ void Session::processBannedIPs(lt::ip_filter &filter) { // First, import current filter for (const QString &ip : asConst(m_bannedIPs.value())) { - boost::system::error_code ec; + lt::error_code ec; const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec); Q_ASSERT(!ec); if (!ec) @@ -1612,7 +1612,7 @@ void Session::banIP(const QString &ip) QStringList bannedIPs = m_bannedIPs; if (!bannedIPs.contains(ip)) { lt::ip_filter filter = m_nativeSession->get_ip_filter(); - boost::system::error_code ec; + lt::error_code ec; const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec); Q_ASSERT(!ec); if (ec) return; @@ -4132,7 +4132,7 @@ void Session::handlePortmapAlert(const lt::portmap_alert *p) void Session::handlePeerBlockedAlert(const lt::peer_blocked_alert *p) { - boost::system::error_code ec; + lt::error_code ec; #if LIBTORRENT_VERSION_NUM < 10200 const std::string ip = p->ip.to_string(ec); #else @@ -4166,7 +4166,7 @@ void Session::handlePeerBlockedAlert(const lt::peer_blocked_alert *p) void Session::handlePeerBanAlert(const lt::peer_ban_alert *p) { - boost::system::error_code ec; + lt::error_code ec; #if (LIBTORRENT_VERSION_NUM < 10200) const std::string ip = p->ip.address().to_string(ec); #else @@ -4244,7 +4244,7 @@ void Session::handleListenSucceededAlert(const lt::listen_succeeded_alert *p) } #endif - boost::system::error_code ec; + lt::error_code ec; LogMsg(tr("Successfully listening on IP: %1, port: %2/%3" , "e.g: Successfully listening on IP: 192.168.0.1, port: TCP/6881") #if (LIBTORRENT_VERSION_NUM < 10200) @@ -4307,7 +4307,7 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p) } #endif - boost::system::error_code ec; + lt::error_code ec; LogMsg(tr("Failed to listen on IP: %1, port: %2/%3. Reason: %4" , "e.g: Failed to listen on IP: 192.168.0.1, port: TCP/6881. Reason: already in use") #if (LIBTORRENT_VERSION_NUM < 10200) @@ -4321,7 +4321,7 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p) void Session::handleExternalIPAlert(const lt::external_ip_alert *p) { - boost::system::error_code ec; + lt::error_code ec; LogMsg(tr("Detected external IP: %1", "e.g. Detected external IP: 1.1.1.1") .arg(p->external_address.to_string(ec).c_str()), Log::INFO); } From 49266cb9e4a9ac1fd1e043db154e93a0f471e249 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 17 Oct 2019 12:09:25 +0800 Subject: [PATCH 3/3] Replace boost::int64_t by std::int64_t --- src/base/bittorrent/torrenthandle.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 5ad0565ab..497c31322 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1071,7 +1071,11 @@ qulonglong TorrentHandle::eta() const QVector TorrentHandle::filesProgress() const { +#if (LIBTORRENT_VERSION_NUM < 10200) std::vector fp; +#else + std::vector fp; +#endif m_nativeHandle.file_progress(fp, lt::torrent_handle::piece_granularity); const int count = static_cast(fp.size()); @@ -1267,15 +1271,25 @@ int TorrentHandle::maxSeedingTime() const qreal TorrentHandle::realRatio() const { +#if (LIBTORRENT_VERSION_NUM < 10200) const boost::int64_t upload = m_nativeStatus.all_time_upload; // special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent - const boost::int64_t download = (m_nativeStatus.all_time_download < m_nativeStatus.total_done * 0.01) ? m_nativeStatus.total_done : m_nativeStatus.all_time_download; + const boost::int64_t download = (m_nativeStatus.all_time_download < (m_nativeStatus.total_done * 0.01)) + ? m_nativeStatus.total_done + : m_nativeStatus.all_time_download; +#else + const int64_t upload = m_nativeStatus.all_time_upload; + // special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent + const int64_t download = (m_nativeStatus.all_time_download < (m_nativeStatus.total_done * 0.01)) + ? m_nativeStatus.total_done + : m_nativeStatus.all_time_download; +#endif if (download == 0) - return (upload == 0) ? 0.0 : MAX_RATIO; + return (upload == 0) ? 0 : MAX_RATIO; const qreal ratio = upload / static_cast(download); - Q_ASSERT(ratio >= 0.0); + Q_ASSERT(ratio >= 0); return (ratio > MAX_RATIO) ? MAX_RATIO : ratio; }