Browse Source

Clean up "network interfaces configuration" code

adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
0d3152e4b0
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 58
      src/base/bittorrent/session.cpp
  2. 2
      src/base/bittorrent/session.h

58
src/base/bittorrent/session.cpp

@ -1131,31 +1131,30 @@ void Session::configure(lt::settings_pack &settingsPack)
if (port > 0) // user specified port if (port > 0) // user specified port
settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0); settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0);
for (QString ip : getListeningIPs()) { for (const QString &ip : asConst(getListeningIPs())) {
lt::error_code ec;
std::string interfacesStr;
if (ip.isEmpty()) { if (ip.isEmpty()) {
ip = QLatin1String("0.0.0.0"); const QString anyIP = QHostAddress(QHostAddress::AnyIPv4).toString();
interfacesStr = std::string((QString("%1:%2").arg(ip).arg(port)).toLatin1().constData()); const std::string endpoint = anyIP.toStdString() + ':' + std::to_string(port);
LogMsg(tr("qBittorrent is trying to listen on any interface port: %1" settingsPack.set_str(lt::settings_pack::listen_interfaces, endpoint);
, "e.g: qBittorrent is trying to listen on any interface port: TCP/6881") LogMsg(tr("Trying to listen on IP: %1, port: %2"
.arg(QString::number(port)) , "e.g: Trying to listen on IP: 192.168.0.1, port: 6881")
.arg(anyIP, QString::number(port))
, Log::INFO); , Log::INFO);
settingsPack.set_str(lt::settings_pack::listen_interfaces, interfacesStr);
break; 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) { if (!ec) {
interfacesStr = std::string((addr.is_v6() ? QString("[%1]:%2") : QString("%1:%2")) const std::string endpoint = (addr.is_v6()
.arg(ip).arg(port).toLatin1().constData()); ? ('[' + addr.to_string() + ']')
LogMsg(tr("qBittorrent is trying to listen on interface %1 port: %2" : addr.to_string())
, "e.g: qBittorrent is trying to listen on interface 192.168.0.1 port: TCP/6881") + ':' + std::to_string(port);
.arg(ip).arg(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); , Log::INFO);
settingsPack.set_str(lt::settings_pack::listen_interfaces, interfacesStr);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
chosenIP = ip; chosenIP = ip;
#endif #endif
@ -1165,22 +1164,24 @@ void Session::configure(lt::settings_pack &settingsPack)
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// On Vista+ versions and after Qt 5.5 QNetworkInterface::name() returns // 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. // Libtorrent expects GUIDs for the 'outgoing_interfaces' setting.
if (!networkInterface().isEmpty()) { const QString netInterface = networkInterface();
const QString guid = convertIfaceNameToGuid(networkInterface()); if (!netInterface.isEmpty()) {
const QString guid = convertIfaceNameToGuid(netInterface);
if (!guid.isEmpty()) { if (!guid.isEmpty()) {
settingsPack.set_str(lt::settings_pack::outgoing_interfaces, guid.toStdString()); settingsPack.set_str(lt::settings_pack::outgoing_interfaces, guid.toStdString());
} }
else { else {
settingsPack.set_str(lt::settings_pack::outgoing_interfaces, chosenIP.toStdString()); 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); , Log::WARNING);
} }
} }
#else #else
settingsPack.set_str(lt::settings_pack::outgoing_interfaces, networkInterface().toStdString()); settingsPack.set_str(lt::settings_pack::outgoing_interfaces, networkInterface().toStdString());
#endif // Q_OS_WIN #endif // Q_OS_WIN
m_listenInterfaceChanged = false; m_listenInterfaceChanged = false;
} }
@ -2298,7 +2299,7 @@ void Session::networkConfigurationChange(const QNetworkConfiguration &cfg)
} }
} }
const QStringList Session::getListeningIPs() QStringList Session::getListeningIPs() const
{ {
QStringList IPs; QStringList IPs;
@ -4244,8 +4245,8 @@ void Session::handleListenSucceededAlert(const lt::listen_succeeded_alert *p)
#endif #endif
boost::system::error_code ec; boost::system::error_code ec;
LogMsg(tr("qBittorrent is successfully listening on interface %1 port: %2/%3" LogMsg(tr("Successfully listening on IP: %1, port: %2/%3"
, "e.g: qBittorrent is successfully listening on interface 192.168.0.1 port: TCP/6881") , "e.g: Successfully listening on IP: 192.168.0.1, port: TCP/6881")
#if (LIBTORRENT_VERSION_NUM < 10200) #if (LIBTORRENT_VERSION_NUM < 10200)
.arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())), Log::INFO); .arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())), Log::INFO);
#else #else
@ -4307,8 +4308,8 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p)
#endif #endif
boost::system::error_code ec; boost::system::error_code ec;
LogMsg(tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4." LogMsg(tr("Failed to listen on IP: %1, port: %2/%3. Reason: %4"
, "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use.") , "e.g: Failed to listen on IP: 192.168.0.1, port: TCP/6881. Reason: already in use")
#if (LIBTORRENT_VERSION_NUM < 10200) #if (LIBTORRENT_VERSION_NUM < 10200)
.arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port()) .arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())
, QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL); , 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) void Session::handleExternalIPAlert(const lt::external_ip_alert *p)
{ {
boost::system::error_code ec; 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) void Session::handleSessionStatsAlert(const lt::session_stats_alert *p)

2
src/base/bittorrent/session.h

@ -520,7 +520,7 @@ namespace BitTorrent
void adjustLimits(); void adjustLimits();
void applyBandwidthLimits(); void applyBandwidthLimits();
void processBannedIPs(lt::ip_filter &filter); void processBannedIPs(lt::ip_filter &filter);
const QStringList getListeningIPs(); QStringList getListeningIPs() const;
void configureListeningInterface(); void configureListeningInterface();
void enableTracker(bool enable); void enableTracker(bool enable);
void enableBandwidthScheduler(); void enableBandwidthScheduler();

Loading…
Cancel
Save