Browse Source

Fixup previous commits and allow IPv6 interface addresses too.

adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
5878d49f60
  1. 23
      src/base/bittorrent/session.cpp
  2. 10
      src/base/preferences.cpp
  3. 4
      src/base/preferences.h
  4. 58
      src/gui/advancedsettings.cpp
  5. 3
      src/gui/advancedsettings.h

23
src/base/bittorrent/session.cpp

@ -1738,9 +1738,21 @@ const QStringList Session::getListeningIPs()
const QString ifaceAddr = pref->getNetworkInterfaceAddress(); const QString ifaceAddr = pref->getNetworkInterfaceAddress();
const bool listenIPv6 = pref->getListenIPv6(); const bool listenIPv6 = pref->getListenIPv6();
//No interface name or address defined just use an empty list if (!ifaceAddr.isEmpty()) {
if (ifaceName.isEmpty() && 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()); IPs.append(QString());
return IPs; return IPs;
} }
@ -1769,12 +1781,15 @@ const QStringList Session::getListeningIPs()
// 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 (!ifaceAddr.isEmpty()) {
if (ipString != ifaceAddr) { if (ifaceAddr == ipString) {
continue; IPs.append(ipString);
break;
} }
} }
else {
IPs.append(ipString); IPs.append(ipString);
} }
}
// Make sure there is at least one IP // Make sure there is at least one IP
// At this point there was a valid network interface, with no suitable IP. // At this point there was a valid network interface, with no suitable IP.

10
src/base/preferences.cpp

@ -1339,6 +1339,11 @@ QString Preferences::getNetworkInterfaceName() const
return value("Preferences/Connection/InterfaceName").toString(); return value("Preferences/Connection/InterfaceName").toString();
} }
void Preferences::setNetworkInterfaceName(const QString& iface)
{
setValue("Preferences/Connection/InterfaceName", iface);
}
void Preferences::setNetworkInterfaceAddress(const QString& addr) void Preferences::setNetworkInterfaceAddress(const QString& addr)
{ {
setValue("Preferences/Connection/InterfaceAddress", addr); setValue("Preferences/Connection/InterfaceAddress", addr);
@ -1349,11 +1354,6 @@ QString Preferences::getNetworkInterfaceAddress() const
return value("Preferences/Connection/InterfaceAddress").toString(); return value("Preferences/Connection/InterfaceAddress").toString();
} }
void Preferences::setNetworkInterfaceName(const QString& iface)
{
setValue("Preferences/Connection/InterfaceName", iface);
}
bool Preferences::getListenIPv6() const bool Preferences::getListenIPv6() const
{ {
return value("Preferences/Connection/InterfaceListenIPv6", false).toBool(); return value("Preferences/Connection/InterfaceListenIPv6", false).toBool();

4
src/base/preferences.h

@ -361,10 +361,10 @@ public:
void setMaxHalfOpenConnections(int value); void setMaxHalfOpenConnections(int value);
QString getNetworkInterface() const; QString getNetworkInterface() const;
void setNetworkInterface(const QString& iface); void setNetworkInterface(const QString& iface);
QString getNetworkInterfaceAddress() const;
void setNetworkInterfaceAddress(const QString& addr);
QString getNetworkInterfaceName() const; QString getNetworkInterfaceName() const;
void setNetworkInterfaceName(const QString& iface); void setNetworkInterfaceName(const QString& iface);
QString getNetworkInterfaceAddress() const;
void setNetworkInterfaceAddress(const QString& addr);
bool getListenIPv6() const; bool getListenIPv6() const;
void setListenIPv6(bool enable); void setListenIPv6(bool enable);
QString getNetworkAddress() const; QString getNetworkAddress() const;

58
src/gui/advancedsettings.cpp

@ -106,7 +106,7 @@ AdvancedSettings::AdvancedSettings(QWidget *parent)
setEditTriggers(QAbstractItemView::NoEditTriggers); setEditTriggers(QAbstractItemView::NoEditTriggers);
// Signals // Signals
connect(&spin_cache, SIGNAL(valueChanged(int)), SLOT(updateCacheSpinSuffix(int))); 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 // Load settings
loadAdvancedSettings(); loadAdvancedSettings();
resizeColumnToContents(0); resizeColumnToContents(0);
@ -156,11 +156,7 @@ void AdvancedSettings::saveAdvancedSettings()
} }
else { else {
QHostAddress ifaceAddr(combo_iface_address.currentText().trimmed()); QHostAddress ifaceAddr(combo_iface_address.currentText().trimmed());
if (ifaceAddr.isNull()) { ifaceAddr.isNull() ? pref->setNetworkInterfaceAddress(QString::null) : pref->setNetworkInterfaceAddress(ifaceAddr.toString());
pref->setNetworkInterfaceAddress(QString::null);
} else {
pref->setNetworkInterfaceAddress(ifaceAddr.toString());
}
} }
// Network Announce address // Network Announce address
QHostAddress networkAddr(txt_network_address.text().trimmed()); QHostAddress networkAddr(txt_network_address.text().trimmed());
@ -197,41 +193,39 @@ void AdvancedSettings::updateCacheSpinSuffix(int value)
spin_cache.setSuffix(tr(" MiB")); spin_cache.setSuffix(tr(" MiB"));
} }
void AdvancedSettings::updateInterfaceAddressCombo(int) { void AdvancedSettings::updateInterfaceAddressCombo()
{
// Try to get the currently selected interface name // Try to get the currently selected interface name
QString ifaceName; const QString ifaceName = combo_iface.itemData(combo_iface.currentIndex()).toString(); // Empty string for the first element
if (combo_iface.currentIndex() == 0) { const QString currentAddress = Preferences::instance()->getNetworkInterfaceAddress();
ifaceName = QString();
}
else {
ifaceName = combo_iface.itemData(combo_iface.currentIndex()).toString();
}
const QNetworkInterface iface = QNetworkInterface::interfaceFromName(ifaceName);
//Clear all items and reinsert them, default to all //Clear all items and reinsert them, default to all
combo_iface_address.clear(); combo_iface_address.clear();
combo_iface_address.addItem(tr("All Addresses")); combo_iface_address.addItem(tr("All addresses"));
combo_iface_address.setCurrentIndex(0); combo_iface_address.setCurrentIndex(0);
if (!iface.isValid()) {
return;
}
//Found a valid interface, try to get the addresses
const QList<QNetworkAddressEntry> addresses = iface.addressEntries();
const Preferences* const pref = Preferences::instance();
const QString currentAddress = pref->getNetworkInterfaceAddress();
foreach (const QNetworkAddressEntry &entry, addresses) { auto populateCombo = [this, &currentAddress](const QString &ip, const QAbstractSocket::NetworkLayerProtocol &protocol)
QHostAddress ip = entry.ip(); {
QString ipString = ip.toString();
QAbstractSocket::NetworkLayerProtocol protocol = ip.protocol();
Q_ASSERT(protocol == QAbstractSocket::IPv4Protocol || protocol == QAbstractSocket::IPv6Protocol); Q_ASSERT(protocol == QAbstractSocket::IPv4Protocol || protocol == QAbstractSocket::IPv6Protocol);
//Only take ipv4 for now? //Only take ipv4 for now?
if (protocol != QAbstractSocket::IPv4Protocol) if (protocol != QAbstractSocket::IPv4Protocol && protocol != QAbstractSocket::IPv6Protocol)
continue; return;
combo_iface_address.addItem( ipString ); combo_iface_address.addItem(ip);
//Try to select the last added one //Try to select the last added one
if (ipString == currentAddress) { if (ip == currentAddress)
combo_iface_address.setCurrentIndex(combo_iface_address.count() - 1); 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<QNetworkAddressEntry> 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); addRow(NETWORK_IFACE, tr("Network Interface (requires restart)"), &combo_iface);
// Network interface address // Network interface address
updateInterfaceAddressCombo(combo_iface.currentIndex()); updateInterfaceAddressCombo();
addRow(NETWORK_IFACE_ADDRESS, tr("Optional IP Address to bind to (requires restart)"), &combo_iface_address); addRow(NETWORK_IFACE_ADDRESS, tr("Optional IP Address to bind to (requires restart)"), &combo_iface_address);
// Listen on IPv6 address // Listen on IPv6 address
cb_listen_ipv6.setChecked(pref->getListenIPv6()); cb_listen_ipv6.setChecked(pref->getListenIPv6());

3
src/gui/advancedsettings.h

@ -52,7 +52,8 @@ signals:
private slots: private slots:
void updateCacheSpinSuffix(int value); void updateCacheSpinSuffix(int value);
void updateInterfaceAddressCombo(int index); void updateInterfaceAddressCombo();
private: private:
void loadAdvancedSettings(); void loadAdvancedSettings();
template <typename T> void addRow(int row, const QString &rowText, T* widget); template <typename T> void addRow(int row, const QString &rowText, T* widget);

Loading…
Cancel
Save