From 549119459ab6f7d3cd6425edcbed198eee62f76c Mon Sep 17 00:00:00 2001 From: Aaron Rainbolt Date: Wed, 1 Nov 2023 23:35:33 -0500 Subject: [PATCH] Mostly implement IPv4 settings --- CMakeLists.txt | 4 + connectionsettingsengine.cpp | 98 +++++++++++++++++++- connectionsettingsengine.h | 12 +++ ethernetsettings.cpp | 11 ++- ethernetsettings.h | 2 + ipv4settingstab.cpp | 109 ++++++++++++++++++++++ ipv4settingstab.h | 27 ++++++ ipv4settingstab.ui | 171 +++++++++++++++++++++++++++++++++++ qvarianthelper.h | 9 ++ 9 files changed, 439 insertions(+), 4 deletions(-) create mode 100644 ipv4settingstab.cpp create mode 100644 ipv4settingstab.h create mode 100644 ipv4settingstab.ui create mode 100644 qvarianthelper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a78d63e..af34113 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,10 @@ set(PROJECT_SOURCES security802_1xtab.h security802_1xtab.cpp security802_1xtab.ui + ipv4settingstab.h + ipv4settingstab.cpp + ipv4settingstab.ui + qvarianthelper.h ${TS_FILES} ) diff --git a/connectionsettingsengine.cpp b/connectionsettingsengine.cpp index 2f7863c..985616f 100644 --- a/connectionsettingsengine.cpp +++ b/connectionsettingsengine.cpp @@ -18,6 +18,15 @@ QString ConnectionSettingsEngine::targetConnUuidStr = QString(); * clonedMacAddress: QString * mtu: int * + * All things IPv4: + * + * !ipv4Method: ConnectionSettingsEngine::Ipv4Method + * !ipv4DnsServers: QString + * !ipv4SearchDomains: QString + * !ipv4DhcpClientId: QString + * !ipv4AddressList: QList + * !ipv4Required: bool + * * All things 802.1x: * * 802.1xEnabled: bool @@ -77,6 +86,7 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr NetworkManager::Security8021xSetting::Ptr connSecurity802_1xSetting = connSettings->setting(NetworkManager::Setting::Security8021x).dynamicCast(); NetworkManager::WiredSetting::Ptr connWiredSetting = connSettings->setting(NetworkManager::Setting::Wired).dynamicCast(); NetworkManager::WirelessSetting::Ptr connWirelessSetting = connSettings->setting(NetworkManager::Setting::Wireless).dynamicCast(); + NetworkManager::Ipv4Setting::Ptr connIpv4Setting = connSettings->setting(NetworkManager::Setting::Ipv4).dynamicCast(); QVariantMap result; result.insert("connName", connSettings->id()); result.insert("autoconnectEnabled", connSettings->autoconnect()); @@ -125,6 +135,39 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr result.insert("device", connSettings->interfaceName()); + if (!connIpv4Setting.isNull()) { + switch (connIpv4Setting->method()) { + case NetworkManager::Ipv4Setting::Automatic: + if (connIpv4Setting->ignoreAutoDns()) { + result.insert("ipv4Method", ConnectionSettingsEngine::Ipv4AutomaticAddressOnly); + } else { + result.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Automatic); + } + break; + case NetworkManager::Ipv4Setting::LinkLocal: + result.insert("ipv4Method", ConnectionSettingsEngine::Ipv4LinkLocal); + break; + case NetworkManager::Ipv4Setting::Manual: + result.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Manual); + break; + case NetworkManager::Ipv4Setting::Shared: + result.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Shared); + break; + case NetworkManager::Ipv4Setting::Disabled: + result.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Disabled); + break; + } + QStringList dnsStringList; + for (const QHostAddress &addr : connIpv4Setting->dns()) { + dnsStringList.append(addr.toString()); + } + result.insert("ipv4DnsServers", dnsStringList.join(",")); + result.insert("ipv4SearchDomains", connIpv4Setting->dnsSearch().join(",")); + result.insert("ipv4DhcpClientId", connIpv4Setting->dhcpClientId()); + result.insert("ipv4AddressList", QVariant::fromValue(connIpv4Setting->addresses())); + result.insert("ipv4Required", !connIpv4Setting->mayFail()); + } + if (!connSecurity802_1xSetting.isNull()) { QList eapMethods = connSecurity802_1xSetting->eapMethods(); if (eapMethods.length() != 0) { @@ -331,7 +374,7 @@ void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVa NetworkManager::WiredSetting::Ptr connWiredSetting = connSettings->setting(NetworkManager::Setting::Wired).dynamicCast(); NetworkManager::WirelessSetting::Ptr connWirelessSetting = connSettings->setting(NetworkManager::Setting::Wireless).dynamicCast(); NetworkManager::Security8021xSetting secSetting; - + NetworkManager::Ipv4Setting::Ptr connIpv4Setting = connSettings->setting(NetworkManager::Setting::Ipv4).dynamicCast(); if (settings["connName"].isValid()) { connSettings->setId(settings["connName"].toString()); } @@ -375,6 +418,59 @@ void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVa connSettings->setInterfaceName(settings["device"].toString()); } + if (settings["ipv4Method"].isValid()) { + switch (settings["ipv4Method"].toInt()) { + case ConnectionSettingsEngine::Ipv4Automatic: + connIpv4Setting->setMethod(NetworkManager::Ipv4Setting::Automatic); + connIpv4Setting->setIgnoreAutoDns(false); + break; + case ConnectionSettingsEngine::Ipv4AutomaticAddressOnly: + connIpv4Setting->setMethod(NetworkManager::Ipv4Setting::Automatic); + connIpv4Setting->setIgnoreAutoDns(true); + break; + case ConnectionSettingsEngine::Ipv4LinkLocal: + connIpv4Setting->setMethod(NetworkManager::Ipv4Setting::LinkLocal); + break; + case ConnectionSettingsEngine::Ipv4Manual: + connIpv4Setting->setMethod(NetworkManager::Ipv4Setting::Manual); + break; + case ConnectionSettingsEngine::Ipv4Shared: + connIpv4Setting->setMethod(NetworkManager::Ipv4Setting::Shared); + break; + case ConnectionSettingsEngine::Ipv4Disabled: + connIpv4Setting->setMethod(NetworkManager::Ipv4Setting::Disabled); + break; + } + } + + if (settings["ipv4DnsServers"].isValid()) { + QStringList dnsStringList = settings["ipv4DnsServers"].toString().split(','); + QList dnsAddrList; + for (const QString &dnsStr : dnsStringList) { + QHostAddress dnsAddr(dnsStr); + if (!dnsAddr.isNull()) { + dnsAddrList.append(dnsAddr); + } + } + connIpv4Setting->setDns(dnsAddrList); + } + + if (settings["ipv4SearchDomains"].isValid()) { + connIpv4Setting->setDnsSearch(settings["ipv4SearchDomains"].toString().split(',')); + } + + if (settings["ipv4DhcpClientId"].isValid()) { + connIpv4Setting->setDhcpClientId(settings["ipv4DhcpClientId"].toString()); + } + + if (settings["ipv4AddressList"].isValid()) { + connIpv4Setting->setAddresses(settings["ipv4AddressList"].value>()); + } + + if (settings["ipv4Required"].isValid()) { + connIpv4Setting->setMayFail(!settings["ipv4Required"].toBool()); + } + if (settings["802.1xEnabled"].isValid() && settings["802.1xEnabled"].toBool()) { QList eapMethodsList; switch (settings["802.1xAuthMode"].toInt()) { diff --git a/connectionsettingsengine.h b/connectionsettingsengine.h index 0b967ff..6a17bff 100644 --- a/connectionsettingsengine.h +++ b/connectionsettingsengine.h @@ -1,6 +1,7 @@ #ifndef CONNECTIONSETTINGSENGINE_H #define CONNECTIONSETTINGSENGINE_H +#include "qvarianthelper.h" #include #include #include @@ -10,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -27,6 +30,15 @@ public: MeteredNo }; + enum Ipv4Method { + Ipv4Automatic, + Ipv4AutomaticAddressOnly, + Ipv4LinkLocal, + Ipv4Manual, + Ipv4Shared, + Ipv4Disabled + }; + enum LinkSpeed { SpeedUnknown, Speed10Mbps, diff --git a/ethernetsettings.cpp b/ethernetsettings.cpp index e5835f0..7a2cf79 100644 --- a/ethernetsettings.cpp +++ b/ethernetsettings.cpp @@ -13,12 +13,15 @@ EthernetSettings::EthernetSettings(QString connUuidStr, QWidget *parent) : generalSettingsTab = new GeneralSettingsTab(); ethernetSettingsTab = new EthernetSettingsTab(); security802_1xSettingsTab = new Security802_1xTab(); - ui->tabWidget->addTab(generalSettingsTab, "General"); - ui->tabWidget->addTab(ethernetSettingsTab, "Ethernet"); - ui->tabWidget->addTab(security802_1xSettingsTab, "802.1x Security"); + ipv4SettingsTab = new Ipv4SettingsTab(); + ui->tabWidget->addTab(generalSettingsTab, tr("General")); + ui->tabWidget->addTab(ethernetSettingsTab, tr("Ethernet")); + ui->tabWidget->addTab(security802_1xSettingsTab, tr("802.1x Security")); + ui->tabWidget->addTab(ipv4SettingsTab, tr("IPv4")); generalSettingsTab->loadSettings(connSettings); ethernetSettingsTab->loadSettings(connSettings); security802_1xSettingsTab->loadSettings(connSettings); + ipv4SettingsTab->loadSettings(connSettings); connect(ui->cancelButton, &QPushButton::clicked, this, &EthernetSettings::onCancelButtonClicked); connect(ui->saveButton, &QPushButton::clicked, this, &EthernetSettings::onSaveButtonClicked); } @@ -29,6 +32,7 @@ EthernetSettings::~EthernetSettings() delete generalSettingsTab; delete ethernetSettingsTab; delete security802_1xSettingsTab; + delete ipv4SettingsTab; } void EthernetSettings::onCancelButtonClicked() @@ -41,6 +45,7 @@ void EthernetSettings::onSaveButtonClicked() QVariantMap settings = generalSettingsTab->readSettings(); settings.insert(ethernetSettingsTab->readSettings()); settings.insert(security802_1xSettingsTab->readSettings()); + settings.insert(ipv4SettingsTab->readSettings()); settings.insert("connName", ui->connectionNameLineEdit->text()); ConnectionSettingsEngine::modifyConnectionSettings(targetConnUuidStr, settings); this->done(0); diff --git a/ethernetsettings.h b/ethernetsettings.h index ad642e5..8912a22 100644 --- a/ethernetsettings.h +++ b/ethernetsettings.h @@ -5,6 +5,7 @@ #include "generalsettingstab.h" #include "ethernetsettingstab.h" #include "security802_1xtab.h" +#include "ipv4settingstab.h" #include @@ -31,6 +32,7 @@ private: GeneralSettingsTab *generalSettingsTab; EthernetSettingsTab *ethernetSettingsTab; Security802_1xTab *security802_1xSettingsTab; + Ipv4SettingsTab *ipv4SettingsTab; }; #endif // ETHERNETSETTINGS_H diff --git a/ipv4settingstab.cpp b/ipv4settingstab.cpp new file mode 100644 index 0000000..0d920ae --- /dev/null +++ b/ipv4settingstab.cpp @@ -0,0 +1,109 @@ +#include "ipv4settingstab.h" +#include "ui_ipv4settingstab.h" + +Ipv4SettingsTab::Ipv4SettingsTab(QWidget *parent) : + QWidget(parent), + ui(new Ui::Ipv4SettingsTab) +{ + ui->setupUi(this); + ui->methodComboBox->addItems(QStringList() << tr("Automatic") << tr("Automatic (address-only)") << tr("Link-Local") << tr("Manual") << tr("Shared to other computers") << tr("Disabled")); +} + +Ipv4SettingsTab::~Ipv4SettingsTab() +{ + delete ui; +} + +QVariantMap Ipv4SettingsTab::readSettings() +{ + QVariantMap output; + switch (ui->methodComboBox->currentIndex()) { + case 0: // Automatic + output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Automatic); + break; + case 1: // Automatic (address-only) + output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4AutomaticAddressOnly); + break; + case 2: // Link-local + output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4LinkLocal); + break; + case 3: // Manual + output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Manual); + break; + case 4: // Shared + output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Shared); + break; + case 5: // Disabled + output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Disabled); + break; + } + output.insert("ipv4DnsServers", ui->dnsServersLineEdit->text()); + output.insert("ipv4SearchDomains", ui->searchDomainsLineEdit->text()); + output.insert("ipv4DhcpClientId", ui->dhcpClientIdLineEdit->text()); + QList ipAddrList; + if (ui->methodComboBox->currentIndex() == 3) { // Manual + for (int i = 0, rowCount = ui->manualIpv4ConfigurationTable->rowCount(); i < rowCount; i++) { + NetworkManager::IpAddress ipAddr; + ipAddr.setIp(QHostAddress(ui->manualIpv4ConfigurationTable->item(i, 0)->text())); + ipAddr.setNetmask(QHostAddress(ui->manualIpv4ConfigurationTable->item(i, 1)->text())); + ipAddr.setGateway(QHostAddress(ui->manualIpv4ConfigurationTable->item(i, 2)->text())); + ipAddrList.append(ipAddr); + } + } + output.insert("ipv4AddressList", QVariant::fromValue(ipAddrList)); + output.insert("ipv4Required", ui->ipv4RequiredCheckBox->isChecked()); + return output; +} + +void Ipv4SettingsTab::loadSettings(QVariantMap settings) +{ + if (settings["ipv4Method"].isValid()) { + switch (settings["ipv4Method"].toInt()) { + case ConnectionSettingsEngine::Ipv4Automatic: + ui->methodComboBox->setCurrentIndex(0); + break; + case ConnectionSettingsEngine::Ipv4AutomaticAddressOnly: + ui->methodComboBox->setCurrentIndex(1); + break; + case ConnectionSettingsEngine::Ipv4LinkLocal: + ui->methodComboBox->setCurrentIndex(2); + break; + case ConnectionSettingsEngine::Ipv4Manual: + ui->methodComboBox->setCurrentIndex(3); + break; + case ConnectionSettingsEngine::Ipv4Shared: + ui->methodComboBox->setCurrentIndex(4); + break; + case ConnectionSettingsEngine::Ipv4Disabled: + ui->methodComboBox->setCurrentIndex(5); + break; + } + } + + if (settings["ipv4DnsServers"].isValid()) { + ui->dnsServersLineEdit->setText(settings["ipv4DnsServers"].toString()); + } + + if (settings["ipv4SearchDomains"].isValid()) { + ui->searchDomainsLineEdit->setText(settings["ipv4SearchDomains"].toString()); + } + + if (settings["ipv4DhcpClientId"].isValid()) { + ui->dhcpClientIdLineEdit->setText(settings["ipv4DhcpClientId"].toString()); + } + + if (settings["ipv4AddressList"].isValid()) { + QList ipAddrList = settings["ipv4AddressList"].value>(); + for (int i = 0; i < ipAddrList.length(); i++) { + int newRow = ui->manualIpv4ConfigurationTable->rowCount(); + ui->manualIpv4ConfigurationTable->insertRow(newRow); + ui->manualIpv4ConfigurationTable->setItem(newRow, 0, new QTableWidgetItem(ipAddrList[i].ip().toString())); + ui->manualIpv4ConfigurationTable->setItem(newRow, 1, new QTableWidgetItem(ipAddrList[i].netmask().toString())); + ui->manualIpv4ConfigurationTable->setItem(newRow, 2, new QTableWidgetItem(ipAddrList[i].gateway().toString())); + } + } + + if (settings["ipv4Required"].isValid()) { + ui->ipv4RequiredCheckBox->setChecked(settings["ipv4Required"].toBool()); + } +} diff --git a/ipv4settingstab.h b/ipv4settingstab.h new file mode 100644 index 0000000..5e77d39 --- /dev/null +++ b/ipv4settingstab.h @@ -0,0 +1,27 @@ +#ifndef IPV4SETTINGSTAB_H +#define IPV4SETTINGSTAB_H + +#include "qvarianthelper.h" +#include "connectionsettingsengine.h" +#include + +namespace Ui { +class Ipv4SettingsTab; +} + +class Ipv4SettingsTab : public QWidget +{ + Q_OBJECT + +public: + explicit Ipv4SettingsTab(QWidget *parent = nullptr); + ~Ipv4SettingsTab(); + + QVariantMap readSettings(); + void loadSettings(QVariantMap settings); + +private: + Ui::Ipv4SettingsTab *ui; +}; + +#endif // IPV4SETTINGSTAB_H diff --git a/ipv4settingstab.ui b/ipv4settingstab.ui new file mode 100644 index 0000000..dc6259e --- /dev/null +++ b/ipv4settingstab.ui @@ -0,0 +1,171 @@ + + + Ipv4SettingsTab + + + + 0 + 0 + 540 + 479 + + + + Form + + + + + + + + + + + + 0 + 0 + + + + Edit... + + + + + + + + + Method: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + DNS servers: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + DHCP client ID: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + 125 + + + + Address + + + + + Netmask + + + + + Gateway + + + + + + + + + + Add + + + + + + + Remove + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + + + + + + 0 + 0 + + + + Edit... + + + + + + + + + Search domains: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + IPv4 is required for this connection + + + + + + + + diff --git a/qvarianthelper.h b/qvarianthelper.h new file mode 100644 index 0000000..c6461bb --- /dev/null +++ b/qvarianthelper.h @@ -0,0 +1,9 @@ +#ifndef QVARIANTHELPER_H +#define QVARIANTHELPER_H + +#include +#include + +Q_DECLARE_METATYPE(QList) + +#endif // QVARIANTHELPER_H