From 89213bd948a786d33651bdf19de163e5557eec1b Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Tue, 27 Apr 2021 16:52:04 +0800 Subject: [PATCH] fix(configuration): It's not permitted to save configuration when only set ipv6 address. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 修复仅配置ipv6地址时无法保存的问题 Log: 修复仅配置ipv6地址时无法保存的问题 Bug: http://172.17.66.192/biz/bug-view-49079.html --- src/confform.cpp | 154 +++++++++++++++++++++------------- src/confform.h | 2 +- src/kylin-network-interface.c | 20 +++++ src/kylin-network-interface.h | 3 + src/onelancform.cpp | 2 +- 5 files changed, 119 insertions(+), 62 deletions(-) diff --git a/src/confform.cpp b/src/confform.cpp index d04b3d15..9b00eb34 100644 --- a/src/confform.cpp +++ b/src/confform.cpp @@ -166,6 +166,7 @@ void ConfForm::setProp(QString connName, QString uuidName, QString v4method, QSt ui->leName->setText(connName); lastConnName = connName; lastIpv4 = v4addr; + lastIpv6 = v6addr; netUuid = uuidName; qDebug() << Q_FUNC_INFO << __LINE__ << connName << uuidName; @@ -175,7 +176,7 @@ void ConfForm::setProp(QString connName, QString uuidName, QString v4method, QSt isActWifi = isWiFi; } - if (v4method == "auto" || v4method == "") { + if ((v4method == "auto" || v4method == "") && (v6method == "auto" || v6method == "")) { ui->cbType->setCurrentIndex(0); cbTypeChanged(0); } else { @@ -245,10 +246,12 @@ void ConfForm::on_btnCreate_clicked() mIfname = kylindbus.multiWiredIfName.at(0); } - if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { + if (ui->cbType->currentIndex() == 1) { //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 - if (check_ip_conflict(mIfname)) { - return; + if (!ui->leAddr->text().isEmpty()|| !ui->leAddr_ipv6->text().isEmpty()) { + if (check_ip_conflict(mIfname)) { + return; + } } } QString name = ui->leName->text().trimmed(); @@ -256,8 +259,16 @@ void ConfForm::on_btnCreate_clicked() if(ui->cbType->currentIndex() == 0){ cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet"; }else{ - cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet ipv4.method manual ipv4.address " - + ui->leAddr->text() + "/" + mask.toUtf8().data(); + if (ui->leAddr->text().isEmpty()) { //只配置了ipv6地址 + cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet ipv4.method auto ipv6.method manual ipv6.address " + + ui->leAddr_ipv6->text(); + } else if (ui->leAddr_ipv6->text().isEmpty()) { //只配置了ipv4地址 + cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet ipv6.method auto ipv4.method manual ipv4.address " + + ui->leAddr->text() + "/" + mask.toUtf8().data(); + } else { + cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet ipv6.method manual ipv6.address " + ui->leAddr_ipv6->text() + + " ipv4.method manual ipv4.address " + ui->leAddr->text() + "/" + mask.toUtf8().data(); + } if(!ui->leGateway->text().isEmpty()){ cmdStr += " ipv4.gateway " + ui->leGateway->text(); } @@ -281,13 +292,13 @@ void ConfForm::on_btnCreate_clicked() kylindbus.showDesktopNotify(txt); } - if (!ui->leAddr_ipv6->text().isEmpty()) { - QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method manual ipv6.addresses " + ui->leAddr_ipv6->text(); - Utils::m_system(cmdStr.toUtf8().data()); - } else { - QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method auto"; - Utils::m_system(cmdStr.toUtf8().data()); - } +// if (!ui->leAddr_ipv6->text().isEmpty()) { +// QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method manual ipv6.addresses " + ui->leAddr_ipv6->text(); +// Utils::m_system(cmdStr.toUtf8().data()); +// } else { +// QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method auto"; +// Utils::m_system(cmdStr.toUtf8().data()); +// } onConfformHide(); } @@ -307,10 +318,12 @@ void ConfForm::on_btnSave_clicked() kylindbus.showDesktopNotify(notifyTxt); return; } - if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { + if (ui->cbType->currentIndex() == 1) { //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 - if (check_ip_conflict(mWifiIfname)) { - return; + if ((!ui->leAddr->text().isEmpty() && (ui->leAddr->text() != lastIpv4)) || (!ui->leAddr_ipv6->text().isEmpty() && (ui->leAddr_ipv6->text() != lastIpv6))) { + if (check_ip_conflict(mWifiIfname)) { + return; + } } } @@ -358,10 +371,12 @@ void ConfForm::on_btnSave_clicked() int pos = str.indexOf(regExpTest); newUuid = str.mid(pos,36); //36是uuid的长度 - if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { + if (ui->cbType->currentIndex() == 1) { //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 - if (check_ip_conflict(mIfname)) { - return; + if ((!ui->leAddr->text().isEmpty() && (ui->leAddr->text() != lastIpv4)) || (!ui->leAddr_ipv6->text().isEmpty() && (ui->leAddr_ipv6->text() != lastIpv6))) { + if (check_ip_conflict(mIfname)) { + return; + } } } @@ -372,10 +387,12 @@ void ConfForm::on_btnSave_clicked() this->isCreateNewNet = false; newUuid = "--"; - if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { + if (ui->cbType->currentIndex() == 1) { //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 - if (check_ip_conflict(mIfname)) { - return; + if ((!ui->leAddr->text().isEmpty() && (ui->leAddr->text() != lastIpv4)) || (!ui->leAddr_ipv6->text().isEmpty() && (ui->leAddr_ipv6->text() != lastIpv6))) { + if (check_ip_conflict(mIfname)) { + return; + } } } @@ -383,17 +400,6 @@ void ConfForm::on_btnSave_clicked() this->saveNetworkConfiguration(); } } - - if (ui->cbType->currentIndex() == 1) { - //对于已保存连接修改ipv6地址,使用UUID区分各网络配置(排除名称含空格或特殊字符的干扰) - if (!ui->leAddr_ipv6->text().isEmpty()) { - QString cmdStr = "nmcli connection modify '" + netUuid + "' ipv6.method manual ipv6.addresses " + ui->leAddr_ipv6->text(); - Utils::m_system(cmdStr.toUtf8().data()); - } else { - QString cmdStr = "nmcli connection modify '" + netUuid + "' ipv6.method auto"; - Utils::m_system(cmdStr.toUtf8().data()); - } - } QString txt(tr("New network settings already finished")); kylindbus.showDesktopNotify(txt); } @@ -427,14 +433,33 @@ void ConfForm::saveNetworkConfiguration() qDebug() << Q_FUNC_INFO << __LINE__ << name << newUuid << ui->leAddr->text() << mask << ui->leGateway->text(); //kylin_network_set_automethod(name.toUtf8().data()); kylin_network_set_automethod(netUuid.toUtf8().data()); + kylin_network_set_ipv6_automethod(netUuid.toUtf8().data()); } else { if (newUuid != "--" && newUuid != "" && !newUuid.isEmpty()) { qDebug() << Q_FUNC_INFO << __LINE__ << name << newUuid << ui->leAddr->text() << mask << ui->leGateway->text() << dnss; - kylin_network_set_manualall(newUuid.toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + if (!ui->leAddr->text().isEmpty()) { + kylin_network_set_manualall(newUuid.toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + } else { + kylin_network_set_automethod(newUuid.toUtf8().data()); + } + if (!ui->leAddr_ipv6->text().isEmpty()) { + kylin_network_set_ipv6_manualmethod(newUuid.toUtf8().data(), ui->leAddr_ipv6->text().toUtf8().data()); + } else { + kylin_network_set_ipv6_automethod(newUuid.toUtf8().data()); + } } else { qDebug() << Q_FUNC_INFO << __LINE__ << name << netUuid << ui->leAddr->text() << mask << ui->leGateway->text() << dnss; - kylin_network_set_manualall(netUuid.toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + if (!ui->leAddr->text().isEmpty()) { + kylin_network_set_manualall(netUuid.toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + } else { + kylin_network_set_automethod(netUuid.toUtf8().data()); + } + if (!ui->leAddr_ipv6->text().isEmpty()) { + kylin_network_set_ipv6_manualmethod(netUuid.toUtf8().data(), ui->leAddr_ipv6->text().toUtf8().data()); + } else { + kylin_network_set_ipv6_automethod(netUuid.toUtf8().data()); + } } } @@ -475,35 +500,38 @@ bool ConfForm::check_ip_conflict(QString ifname) FILE *fp; char ret[10], arp_all[1024]; - QString arp_all_cmd = "arping -c 3 -f -I " + ifname + " -D " + ui->leAddr->text(); - fp = popen(arp_all_cmd.toUtf8().data(),"r"); - if(!fp) - return false; - fread(arp_all, 1, sizeof(arp_all), fp); - pclose(fp); - - if (strstr(arp_all, "Received") && strstr(arp_all, " response(s)")) { - QString arp_result = "arping -c 1 -f -I " + ifname + " -D " + ui->leAddr->text() + " | awk '{print $2}' | sed -n '3p'"; - - fp = popen(arp_result.toUtf8().data(),"r"); + if (!ui->leAddr_ipv6->text().isEmpty() && ui->leAddr_ipv6->text() != "") { + //ipv4地址不为空,需要验证是否冲突 + QString arp_all_cmd = "arping -c 3 -f -I " + ifname + " -D " + ui->leAddr->text(); + fp = popen(arp_all_cmd.toUtf8().data(),"r"); if(!fp) return false; - fgets(ret,sizeof(ret),fp); + fread(arp_all, 1, sizeof(arp_all), fp); pclose(fp); - ret[strlen(ret)-1]=0; + if (strstr(arp_all, "Received") && strstr(arp_all, " response(s)")) { + QString arp_result = "arping -c 1 -f -I " + ifname + " -D " + ui->leAddr->text() + " | awk '{print $2}' | sed -n '3p'"; - if ( ret != NULL ) { -// if (!strcmp(ret,"0")) { -// //printf("正常连接"); -// return false; -// } else { - if (strcmp(ret,"0")) { - //printf("ipv4地址冲突"); - QString strInfo = tr("IPV4 address conflict, Please change IP"); -// QString buffer = "notify-send -i network-offline " + strInfo; - showNotify(strInfo); - return true; + fp = popen(arp_result.toUtf8().data(),"r"); + if(!fp) + return false; + fgets(ret,sizeof(ret),fp); + pclose(fp); + + ret[strlen(ret)-1]=0; + + if ( ret != NULL ) { + // if (!strcmp(ret,"0")) { + // //printf("正常连接"); + // return false; + // } else { + if (strcmp(ret,"0")) { + //printf("ipv4地址冲突"); + QString strInfo = tr("IPV4 address conflict, Please change IP"); + // QString buffer = "notify-send -i network-offline " + strInfo; + showNotify(strInfo); + return true; + } } } } @@ -684,7 +712,13 @@ void ConfForm::setEnableOfBtn() } if (ui->cbType->currentIndex() == 1) { - if (!this->getTextEditState(ui->leAddr->text()) ) { + if (ui->leAddr->text().isEmpty() && ui->leAddr_ipv6->text().isEmpty()) { + //当ipv4和ipv6地址均未设置时,禁止保存 + this->setBtnEnableFalse(); + return; + } + + if (!ui->leAddr->text().isEmpty() && !this->getTextEditState(ui->leAddr->text()) ) { this->setBtnEnableFalse(); return; } diff --git a/src/confform.h b/src/confform.h index db6220b4..d16a8bd7 100644 --- a/src/confform.h +++ b/src/confform.h @@ -96,7 +96,7 @@ private: bool isActConf; //是否对已经连接的网络进行的更改 bool isCreateNewNet = false; //是否是创建的新网络 bool isShowSaveBtn = true; //是否显示保存按钮,即是否是编辑网络界面 - QString lastConnName, lastIpv4, netUuid, newUuid; + QString lastConnName, lastIpv4, netUuid, newUuid, lastIpv6; bool isActWifi; //是否是wifi网络 bool canCheckIpConflict = true; //当前是否可以执行IP冲突的检测 bool isIpv6Conflict = false; //ipv6地址是否冲突 diff --git a/src/kylin-network-interface.c b/src/kylin-network-interface.c index bb241029..231d3c52 100644 --- a/src/kylin-network-interface.c +++ b/src/kylin-network-interface.c @@ -367,6 +367,16 @@ void kylin_network_set_automethod(char *con_name) if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_automethod' failed");} } +//设置动态分配ipv6地址 +void kylin_network_set_ipv6_automethod(char *con_name) +{ + char str[256]; + char *automethod="auto"; + sprintf(str,"nmcli connection modify '%s' ipv6.method %s ipv6.address ''",con_name,automethod); + int status = system(str); + if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_ipv6_automethod' failed");} +} + //设置手动分配ip void kylin_network_set_manualmethod(char *con_name,char *ip) { @@ -377,6 +387,16 @@ void kylin_network_set_manualmethod(char *con_name,char *ip) if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_manualmethod' failed");} } +//设置手动分配ipv6地址 +void kylin_network_set_ipv6_manualmethod(char *con_name,char *ip) +{ + char str[256]; + char *method="manual"; + sprintf(str,"nmcli connection modify '%s' ipv6.method %s ipv6.address '%s'",con_name,method,ip); + int status = system(str); + if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_ipv6_manualmethod' failed");} +} + // 设置手动分配all void kylin_network_set_manualall(char *con_name, char *addr, char *mask, char *gateway, char *dns) { diff --git a/src/kylin-network-interface.h b/src/kylin-network-interface.h index 1b44dab3..2a15931a 100644 --- a/src/kylin-network-interface.h +++ b/src/kylin-network-interface.h @@ -101,6 +101,7 @@ void kylin_network_del_ethernet_con(char *con_name); */ void kylin_network_set_automethod(char *con_name); +void kylin_network_set_ipv6_automethod(char *con_name); /* * Set up manual IP assignment. * @con_name is the connection name. @@ -108,6 +109,8 @@ void kylin_network_set_automethod(char *con_name); */ void kylin_network_set_manualmethod(char *con_name,char *ip); +void kylin_network_set_ipv6_manualmethod(char *con_name,char *ip); + /* * Set up manual all prop. */ diff --git a/src/onelancform.cpp b/src/onelancform.cpp index e17ff38c..9e366f68 100644 --- a/src/onelancform.cpp +++ b/src/onelancform.cpp @@ -259,7 +259,7 @@ void OneLancForm::setLanInfo(QString str1, QString str2, QString str3, QString s if (str1 == "" || str1 == "auto") { str1 = tr("No Configuration"); - str2 = tr("No Configuration"); +// str2 = tr("No Configuration"); } if (str4 == "--" || str4 == "") {