diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 677a88881..68d427d7b 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1738,9 +1738,21 @@ const QStringList Session::getListeningIPs() const QString ifaceAddr = pref->getNetworkInterfaceAddress(); const bool listenIPv6 = pref->getListenIPv6(); - //No interface name or address defined just use an empty list - if (ifaceName.isEmpty() && ifaceAddr.isEmpty()) { - IPs.append(QString()); + if (!ifaceAddr.isEmpty()) { + QHostAddress addr(ifaceAddr); + if (addr.isNull()) { + logger->addMessage(tr("Configured network interface address %1 isn't valid.", "Configured network interface address 124.5.1568.1 isn't valid.").arg(ifaceAddr), Log::CRITICAL); + IPs.append("127.0.0.1"); // Force listening to localhost and avoid accidental connection that will expose user data. + return IPs; + } + } + + if (ifaceName.isEmpty()) { + if (!ifaceAddr.isEmpty()) + IPs.append(ifaceAddr); + else + IPs.append(QString()); + return IPs; } @@ -1767,13 +1779,16 @@ const QStringList Session::getListeningIPs() || (listenIPv6 && (protocol == QAbstractSocket::IPv4Protocol))) continue; - //If an iface address has been defined only allow ip's that match it to go through + // If an iface address has been defined only allow ip's that match it to go through if (!ifaceAddr.isEmpty()) { - if (ipString != ifaceAddr) { - continue; + if (ifaceAddr == ipString) { + IPs.append(ipString); + break; } } - IPs.append(ipString); + else { + IPs.append(ipString); + } } // Make sure there is at least one IP diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index e1351164b..4de87be34 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -1339,6 +1339,11 @@ QString Preferences::getNetworkInterfaceName() const return value("Preferences/Connection/InterfaceName").toString(); } +void Preferences::setNetworkInterfaceName(const QString& iface) +{ + setValue("Preferences/Connection/InterfaceName", iface); +} + void Preferences::setNetworkInterfaceAddress(const QString& addr) { setValue("Preferences/Connection/InterfaceAddress", addr); @@ -1349,11 +1354,6 @@ QString Preferences::getNetworkInterfaceAddress() const return value("Preferences/Connection/InterfaceAddress").toString(); } -void Preferences::setNetworkInterfaceName(const QString& iface) -{ - setValue("Preferences/Connection/InterfaceName", iface); -} - bool Preferences::getListenIPv6() const { return value("Preferences/Connection/InterfaceListenIPv6", false).toBool(); diff --git a/src/base/preferences.h b/src/base/preferences.h index a6355a941..cc5948b43 100644 --- a/src/base/preferences.h +++ b/src/base/preferences.h @@ -361,10 +361,10 @@ public: void setMaxHalfOpenConnections(int value); QString getNetworkInterface() const; void setNetworkInterface(const QString& iface); - QString getNetworkInterfaceAddress() const; - void setNetworkInterfaceAddress(const QString& addr); QString getNetworkInterfaceName() const; void setNetworkInterfaceName(const QString& iface); + QString getNetworkInterfaceAddress() const; + void setNetworkInterfaceAddress(const QString& addr); bool getListenIPv6() const; void setListenIPv6(bool enable); QString getNetworkAddress() const; diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index f62967167..cf8b287d1 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -106,7 +106,7 @@ AdvancedSettings::AdvancedSettings(QWidget *parent) setEditTriggers(QAbstractItemView::NoEditTriggers); // Signals connect(&spin_cache, SIGNAL(valueChanged(int)), SLOT(updateCacheSpinSuffix(int))); - connect(&combo_iface, SIGNAL(currentIndexChanged(int)), SLOT(updateInterfaceAddressCombo(int))); + connect(&combo_iface, SIGNAL(currentIndexChanged(int)), SLOT(updateInterfaceAddressCombo())); // Load settings loadAdvancedSettings(); resizeColumnToContents(0); @@ -156,11 +156,7 @@ void AdvancedSettings::saveAdvancedSettings() } else { QHostAddress ifaceAddr(combo_iface_address.currentText().trimmed()); - if (ifaceAddr.isNull()) { - pref->setNetworkInterfaceAddress(QString::null); - } else { - pref->setNetworkInterfaceAddress(ifaceAddr.toString()); - } + ifaceAddr.isNull() ? pref->setNetworkInterfaceAddress(QString::null) : pref->setNetworkInterfaceAddress(ifaceAddr.toString()); } // Network Announce address QHostAddress networkAddr(txt_network_address.text().trimmed()); @@ -197,41 +193,39 @@ void AdvancedSettings::updateCacheSpinSuffix(int value) spin_cache.setSuffix(tr(" MiB")); } -void AdvancedSettings::updateInterfaceAddressCombo(int) { - //Try to get the currently selected interface name - QString ifaceName; - if (combo_iface.currentIndex() == 0) { - ifaceName = QString(); - } - else { - ifaceName = combo_iface.itemData(combo_iface.currentIndex()).toString(); - } - const QNetworkInterface iface = QNetworkInterface::interfaceFromName(ifaceName); +void AdvancedSettings::updateInterfaceAddressCombo() +{ + // Try to get the currently selected interface name + const QString ifaceName = combo_iface.itemData(combo_iface.currentIndex()).toString(); // Empty string for the first element + const QString currentAddress = Preferences::instance()->getNetworkInterfaceAddress(); //Clear all items and reinsert them, default to all combo_iface_address.clear(); - combo_iface_address.addItem(tr("All Addresses")); + combo_iface_address.addItem(tr("All addresses")); combo_iface_address.setCurrentIndex(0); - if (!iface.isValid()) { - return; - } - //Found a valid interface, try to get the addresses - const QList addresses = iface.addressEntries(); - const Preferences* const pref = Preferences::instance(); - const QString currentAddress = pref->getNetworkInterfaceAddress(); - foreach (const QNetworkAddressEntry &entry, addresses) { - QHostAddress ip = entry.ip(); - QString ipString = ip.toString(); - QAbstractSocket::NetworkLayerProtocol protocol = ip.protocol(); + auto populateCombo = [this, ¤tAddress](const QString &ip, const QAbstractSocket::NetworkLayerProtocol &protocol) + { Q_ASSERT(protocol == QAbstractSocket::IPv4Protocol || protocol == QAbstractSocket::IPv6Protocol); //Only take ipv4 for now? - if (protocol != QAbstractSocket::IPv4Protocol) - continue; - combo_iface_address.addItem( ipString ); + if (protocol != QAbstractSocket::IPv4Protocol && protocol != QAbstractSocket::IPv6Protocol) + return; + combo_iface_address.addItem(ip); //Try to select the last added one - if (ipString == currentAddress) { + if (ip == currentAddress) combo_iface_address.setCurrentIndex(combo_iface_address.count() - 1); + }; + + if (ifaceName.isEmpty()) { + foreach (const QHostAddress &ip, QNetworkInterface::allAddresses()) + populateCombo(ip.toString(), ip.protocol()); + } + else { + const QNetworkInterface iface = QNetworkInterface::interfaceFromName(ifaceName); + const QList addresses = iface.addressEntries(); + foreach (const QNetworkAddressEntry &entry, addresses) { + const QHostAddress ip = entry.ip(); + populateCombo(ip.toString(), ip.protocol()); } } } @@ -339,7 +333,7 @@ void AdvancedSettings::loadAdvancedSettings() } addRow(NETWORK_IFACE, tr("Network Interface (requires restart)"), &combo_iface); // Network interface address - updateInterfaceAddressCombo(combo_iface.currentIndex()); + updateInterfaceAddressCombo(); addRow(NETWORK_IFACE_ADDRESS, tr("Optional IP Address to bind to (requires restart)"), &combo_iface_address); // Listen on IPv6 address cb_listen_ipv6.setChecked(pref->getListenIPv6()); diff --git a/src/gui/advancedsettings.h b/src/gui/advancedsettings.h index ef61d52f1..9e1c49cf6 100644 --- a/src/gui/advancedsettings.h +++ b/src/gui/advancedsettings.h @@ -52,7 +52,8 @@ signals: private slots: void updateCacheSpinSuffix(int value); - void updateInterfaceAddressCombo(int index); + void updateInterfaceAddressCombo(); + private: void loadAdvancedSettings(); template void addRow(int row, const QString &rowText, T* widget);