From eb575783a571efa596121087a078804c82e06574 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Thu, 14 Mar 2024 20:27:12 +0800 Subject: [PATCH] fix(ipv6): get ipv4/ipv6 from dbus(issues#I8ZOMD) --- src/frontend/netdetails/ipv4page.h | 4 +- src/frontend/netdetails/netdetail.cpp | 148 ++++++++++++++++++++++++-- src/frontend/netdetails/netdetail.h | 8 ++ 3 files changed, 152 insertions(+), 8 deletions(-) diff --git a/src/frontend/netdetails/ipv4page.h b/src/frontend/netdetails/ipv4page.h index 0a1fa101..9645da1e 100644 --- a/src/frontend/netdetails/ipv4page.h +++ b/src/frontend/netdetails/ipv4page.h @@ -52,6 +52,8 @@ public: } } + QString getNetMaskText(QString text); + bool checkIsChanged(const ConInfo info, KyConnectSetting &setting); bool checkDnsSettingsIsChanged(); @@ -94,7 +96,7 @@ private: void configSave(); bool getTextEditState(QString text); bool netMaskIsValide(QString text); - QString getNetMaskText(QString text); + bool checkConnectBtnIsEnabled(); void initConflictHintLable(); void initLoadingIcon(); diff --git a/src/frontend/netdetails/netdetail.cpp b/src/frontend/netdetails/netdetail.cpp index d628e9a7..7eae50f0 100644 --- a/src/frontend/netdetails/netdetail.cpp +++ b/src/frontend/netdetails/netdetail.cpp @@ -715,26 +715,33 @@ void NetDetail::getStaticIpInfo(ConInfo &conInfo, bool bActived) kyConnectResourse->getConnectionSetting(m_uuid,connetSetting); connetSetting.dumpInfo(); - conInfo.ipv4ConfigType = connetSetting.m_ipv4ConfigIpType; - conInfo.ipv6ConfigType = connetSetting.m_ipv6ConfigIpType; + //conInfo.ipv4ConfigType = connetSetting.m_ipv4ConfigIpType; + //conInfo.ipv6ConfigType = connetSetting.m_ipv6ConfigIpType; conInfo.isAutoConnect = connetSetting.m_isAutoConnect; conInfo.ipv4DnsList = connetSetting.m_ipv4Dns; conInfo.ipv6DnsList = connetSetting.m_ipv6Dns; + //openkylin从第三方库读取有问题 改为ipv4/ipv6信息直接通过dbus获取 + KyConnectItem* item = kyConnectResourse->getConnectionItemByUuidWithoutActivateChecking(m_uuid); + if (item == nullptr) { + conInfo.ipv4ConfigType = CONFIG_IP_DHCP; + conInfo.ipv6ConfigType = CONFIG_IP_DHCP; + } else { + getIpv4Ipv6Info(item->m_connectPath, conInfo); + } +#if 0 if (connetSetting.m_ipv4ConfigIpType == CONFIG_IP_MANUAL) { if (connetSetting.m_ipv4Address.size() > 0) { conInfo.strIPV4Address = connetSetting.m_ipv4Address.at(0).ip().toString(); conInfo.strIPV4NetMask = connetSetting.m_ipv4Address.at(0).netmask().toString(); conInfo.strIPV4GateWay = connetSetting.m_ipv4Address.at(0).gateway().toString(); } - #if 0 if (connetSetting.m_ipv4Dns.size() == 1) { conInfo.strIPV4FirDns = connetSetting.m_ipv4Dns.at(0).toString(); } else if (connetSetting.m_ipv4Dns.size() > 1) { conInfo.strIPV4FirDns = connetSetting.m_ipv4Dns.at(0).toString(); conInfo.strIPV4SecDns = connetSetting.m_ipv4Dns.at(1).toString(); } - #endif } if (connetSetting.m_ipv6ConfigIpType == CONFIG_IP_MANUAL) { @@ -743,16 +750,14 @@ void NetDetail::getStaticIpInfo(ConInfo &conInfo, bool bActived) conInfo.iIPV6Prefix = ipv6Page->getPerfixLength(connetSetting.m_ipv6Address.at(0).netmask().toString()); conInfo.strIPV6GateWay = connetSetting.m_ipv6Address.at(0).gateway().toString(); } -#if 0 if (connetSetting.m_ipv6Dns.size() == 1) { conInfo.strIPV6FirDns = connetSetting.m_ipv6Dns.at(0).toString(); } else if (connetSetting.m_ipv6Dns.size() > 1) { conInfo.strIPV6FirDns = connetSetting.m_ipv6Dns.at(0).toString(); conInfo.strIPV6SecDns = connetSetting.m_ipv6Dns.at(1).toString(); } -#endif } - +#endif if (!bActived) { conInfo.strDynamicIpv4 = conInfo.strIPV4Address.isEmpty() ? tr("Auto") : conInfo.strIPV4Address; conInfo.strDynamicIpv6 = conInfo.strIPV6Address.isEmpty() ? tr("Auto") : conInfo.strIPV6Address; @@ -1337,3 +1342,132 @@ void ThreadObject::checkIpv6ConflictThread(const QString &ipv6Address) Q_EMIT ipv6IsConflict(isConflict); } + +void NetDetail::getIpv4Ipv6Info(QString objPath, ConInfo &conInfo) +{ + QDBusInterface m_interface("org.freedesktop.NetworkManager", + objPath, + "org.freedesktop.NetworkManager.Settings.Connection", + QDBusConnection::systemBus()); + QDBusMessage result = m_interface.call("GetSettings"); + + if (result.arguments().isEmpty()) { return; } + const QDBusArgument &dbusArg1st = result.arguments().at( 0 ).value(); + QMap> map; + dbusArg1st >> map; + + for (QString key : map.keys() ) { + QMap innerMap = map.value(key); + if (key == "ipv4") { + for (QString innerKey : innerMap.keys()) { + if (innerKey == "address-data") { + //ipv4地址 ipv4子网掩码 + QMap ipv4Map = getAddressDataFromMap(innerMap, innerKey); + if (!ipv4Map.isEmpty()) { + conInfo.strIPV4Address = ipv4Map.value("address").toString(); + conInfo.strIPV4NetMask = ipv4Page->getNetMaskText(ipv4Map.value("prefix").toString()); + } + } else if (innerKey == "method") { + conInfo.ipv4ConfigType = getIpConfigTypeFromMap(innerMap, innerKey); + + } else if (innerKey == "dns") { + conInfo.ipv4DnsList = getIpv4DnsFromMap(innerMap, innerKey); + + } else if (innerKey == "gateway") { + conInfo.strIPV4GateWay = innerMap.value(innerKey).toString(); + } + } + } + if (key == "ipv6") { + for (QString innerKey : innerMap.keys()) { + if (innerKey == "address-data"){ + QMap ipv6Map = getAddressDataFromMap(innerMap, innerKey); + if (!ipv6Map.isEmpty()) { + conInfo.strIPV6Address = ipv6Map.value("address").toString(); + conInfo.iIPV6Prefix = ipv6Map.value("prefix").toString().toInt(); + } + + } else if (innerKey == "method") { + conInfo.ipv6ConfigType = getIpConfigTypeFromMap(innerMap, innerKey); + + } else if (innerKey == "dns") { + conInfo.ipv6DnsList = getIpv6DnsFromMap(innerMap, innerKey); + + } else if (innerKey == "gateway") { + conInfo.strIPV6GateWay = innerMap.value(innerKey).toString(); + } + } + } + } +} + +QMap NetDetail::getAddressDataFromMap(QMap &innerMap, QString innerKey) +{ + //get address-data: {'address', 'prefix'} + const QDBusArgument &dbusArg2nd = innerMap.value(innerKey).value(); + QVector> addressVector; + + dbusArg2nd.beginArray(); + while (!dbusArg2nd.atEnd()) { + QMap tempMap; + dbusArg2nd >> tempMap;// append map to a vector here if you want to keep it + addressVector.append(tempMap); + } + dbusArg2nd.endArray(); + + return addressVector.size() >= 1 ? addressVector.at(0) : QMap(); +} + +KyIpConfigType NetDetail::getIpConfigTypeFromMap(QMap &innerMap, QString innerKey) +{ + // get 'method' + QString strMethod = innerMap.value(innerKey).toString(); + if (strMethod == "auto") { + return CONFIG_IP_DHCP; + } else if (strMethod == "manual") { + return CONFIG_IP_MANUAL; + } else { + //TODO: match other types + return CONFIG_IP_DHCP; + } +} + +QList NetDetail::getIpv4DnsFromMap(QMap &innerMap, QString innerKey) +{ + // get ipv4 'dns' + const QDBusArgument &dbusArg2nd = innerMap.value(innerKey).value(); + QList addressVector; + + dbusArg2nd.beginArray(); + while (!dbusArg2nd.atEnd()) { + uint tmpVar; + dbusArg2nd >> tmpVar; + QString dnsi(inet_ntoa(*(struct in_addr *)&tmpVar)); + addressVector.append(QHostAddress(tmpVar)); + } + dbusArg2nd.endArray(); + + return addressVector; +} + +QList NetDetail::getIpv6DnsFromMap(QMap &innerMap, QString innerKey) +{ + // get ipv6 'dns' + const QDBusArgument &dbusArg2nd = innerMap.value(innerKey).value(); + QList addressVector; + + dbusArg2nd.beginArray(); + while (!dbusArg2nd.atEnd()) { + QByteArray temArray; + quint8 tmpVar[16]; + + dbusArg2nd >> temArray; + for (int i = 0; i< 16; ++i) { + tmpVar[i] = temArray[i]; + } + addressVector.append(QHostAddress(tmpVar)); + } + dbusArg2nd.endArray(); + + return addressVector; +} diff --git a/src/frontend/netdetails/netdetail.h b/src/frontend/netdetails/netdetail.h index 62877b9b..aebaa21f 100644 --- a/src/frontend/netdetails/netdetail.h +++ b/src/frontend/netdetails/netdetail.h @@ -52,6 +52,7 @@ #include "ktabbar.h" #include "networkmodeconfig.h" +#include using namespace kdk; #define TAB_WIDTH 60 @@ -141,9 +142,16 @@ private: void showDesktopNotify(const QString &message, QString soundName); void setNetdetailSomeEnable(bool on); + void startObjectThread(); void setNetTabToolTip(); + void getIpv4Ipv6Info(QString objPath, ConInfo &conInfo); + QMap getAddressDataFromMap(QMap &innerMap, QString innerKey); + KyIpConfigType getIpConfigTypeFromMap(QMap &innerMap, QString innerKey); + QList getIpv4DnsFromMap(QMap &innerMap, QString innerKey); + QList getIpv6DnsFromMap(QMap &innerMap, QString innerKey); + private: KyNetworkDeviceResourse *m_netDeviceResource = nullptr; KyConnectOperation* m_connectOperation = nullptr;