Merge pull request #124 from mammonsama666/0427-dev

fix(configuration): It's not permitted to save configuration when only set ipv6 address.
This commit is contained in:
chenlelin 2021-04-29 10:54:21 +08:00 committed by GitHub
commit d554e18ece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 62 deletions

View File

@ -166,6 +166,7 @@ void ConfForm::setProp(QString connName, QString uuidName, QString v4method, QSt
ui->leName->setText(connName); ui->leName->setText(connName);
lastConnName = connName; lastConnName = connName;
lastIpv4 = v4addr; lastIpv4 = v4addr;
lastIpv6 = v6addr;
netUuid = uuidName; netUuid = uuidName;
qDebug() << Q_FUNC_INFO << __LINE__ << connName << uuidName; qDebug() << Q_FUNC_INFO << __LINE__ << connName << uuidName;
@ -175,7 +176,7 @@ void ConfForm::setProp(QString connName, QString uuidName, QString v4method, QSt
isActWifi = isWiFi; isActWifi = isWiFi;
} }
if (v4method == "auto" || v4method == "") { if ((v4method == "auto" || v4method == "") && (v6method == "auto" || v6method == "")) {
ui->cbType->setCurrentIndex(0); ui->cbType->setCurrentIndex(0);
cbTypeChanged(0); cbTypeChanged(0);
} else { } else {
@ -245,10 +246,12 @@ void ConfForm::on_btnCreate_clicked()
mIfname = kylindbus.multiWiredIfName.at(0); mIfname = kylindbus.multiWiredIfName.at(0);
} }
if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { if (ui->cbType->currentIndex() == 1) {
//在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突 //在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突
if (check_ip_conflict(mIfname)) { if (!ui->leAddr->text().isEmpty()|| !ui->leAddr_ipv6->text().isEmpty()) {
return; if (check_ip_conflict(mIfname)) {
return;
}
} }
} }
QString name = ui->leName->text().trimmed(); QString name = ui->leName->text().trimmed();
@ -256,8 +259,16 @@ void ConfForm::on_btnCreate_clicked()
if(ui->cbType->currentIndex() == 0){ if(ui->cbType->currentIndex() == 0){
cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet"; cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet";
}else{ }else{
cmdStr = "nmcli connection add con-name '" + name + "' ifname '" + mIfname + "' type ethernet ipv4.method manual ipv4.address " if (ui->leAddr->text().isEmpty()) { //只配置了ipv6地址
+ ui->leAddr->text() + "/" + mask.toUtf8().data(); 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()){ if(!ui->leGateway->text().isEmpty()){
cmdStr += " ipv4.gateway " + ui->leGateway->text(); cmdStr += " ipv4.gateway " + ui->leGateway->text();
} }
@ -281,13 +292,13 @@ void ConfForm::on_btnCreate_clicked()
kylindbus.showDesktopNotify(txt); kylindbus.showDesktopNotify(txt);
} }
if (!ui->leAddr_ipv6->text().isEmpty()) { // if (!ui->leAddr_ipv6->text().isEmpty()) {
QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method manual ipv6.addresses " + ui->leAddr_ipv6->text(); // QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method manual ipv6.addresses " + ui->leAddr_ipv6->text();
Utils::m_system(cmdStr.toUtf8().data()); // Utils::m_system(cmdStr.toUtf8().data());
} else { // } else {
QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method auto"; // QString cmdStr = "nmcli connection modify '" + name + "' ipv6.method auto";
Utils::m_system(cmdStr.toUtf8().data()); // Utils::m_system(cmdStr.toUtf8().data());
} // }
onConfformHide(); onConfformHide();
} }
@ -307,10 +318,12 @@ void ConfForm::on_btnSave_clicked()
kylindbus.showDesktopNotify(notifyTxt); kylindbus.showDesktopNotify(notifyTxt);
return; return;
} }
if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { if (ui->cbType->currentIndex() == 1) {
//在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突 //在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突
if (check_ip_conflict(mWifiIfname)) { if ((!ui->leAddr->text().isEmpty() && (ui->leAddr->text() != lastIpv4)) || (!ui->leAddr_ipv6->text().isEmpty() && (ui->leAddr_ipv6->text() != lastIpv6))) {
return; if (check_ip_conflict(mWifiIfname)) {
return;
}
} }
} }
@ -358,10 +371,12 @@ void ConfForm::on_btnSave_clicked()
int pos = str.indexOf(regExpTest); int pos = str.indexOf(regExpTest);
newUuid = str.mid(pos,36); //36是uuid的长度 newUuid = str.mid(pos,36); //36是uuid的长度
if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { if (ui->cbType->currentIndex() == 1) {
//在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突 //在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突
if (check_ip_conflict(mIfname)) { if ((!ui->leAddr->text().isEmpty() && (ui->leAddr->text() != lastIpv4)) || (!ui->leAddr_ipv6->text().isEmpty() && (ui->leAddr_ipv6->text() != lastIpv6))) {
return; if (check_ip_conflict(mIfname)) {
return;
}
} }
} }
@ -372,10 +387,12 @@ void ConfForm::on_btnSave_clicked()
this->isCreateNewNet = false; this->isCreateNewNet = false;
newUuid = "--"; newUuid = "--";
if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { if (ui->cbType->currentIndex() == 1) {
//在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突 //在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突
if (check_ip_conflict(mIfname)) { if ((!ui->leAddr->text().isEmpty() && (ui->leAddr->text() != lastIpv4)) || (!ui->leAddr_ipv6->text().isEmpty() && (ui->leAddr_ipv6->text() != lastIpv6))) {
return; if (check_ip_conflict(mIfname)) {
return;
}
} }
} }
@ -383,17 +400,6 @@ void ConfForm::on_btnSave_clicked()
this->saveNetworkConfiguration(); 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")); QString txt(tr("New network settings already finished"));
kylindbus.showDesktopNotify(txt); kylindbus.showDesktopNotify(txt);
} }
@ -427,14 +433,33 @@ void ConfForm::saveNetworkConfiguration()
qDebug() << Q_FUNC_INFO << __LINE__ << name << newUuid << ui->leAddr->text() << mask << ui->leGateway->text(); 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(name.toUtf8().data());
kylin_network_set_automethod(netUuid.toUtf8().data()); kylin_network_set_automethod(netUuid.toUtf8().data());
kylin_network_set_ipv6_automethod(netUuid.toUtf8().data());
} }
else { else {
if (newUuid != "--" && newUuid != "" && !newUuid.isEmpty()) { if (newUuid != "--" && newUuid != "" && !newUuid.isEmpty()) {
qDebug() << Q_FUNC_INFO << __LINE__ << name << newUuid << ui->leAddr->text() << mask << ui->leGateway->text() << dnss; 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 { } else {
qDebug() << Q_FUNC_INFO << __LINE__ << name << netUuid << ui->leAddr->text() << mask << ui->leGateway->text() << dnss; 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; FILE *fp;
char ret[10], arp_all[1024]; char ret[10], arp_all[1024];
QString arp_all_cmd = "arping -c 3 -f -I " + ifname + " -D " + ui->leAddr->text(); if (!ui->leAddr_ipv6->text().isEmpty() && ui->leAddr_ipv6->text() != "") {
fp = popen(arp_all_cmd.toUtf8().data(),"r"); //ipv4地址不为空需要验证是否冲突
if(!fp) QString arp_all_cmd = "arping -c 3 -f -I " + ifname + " -D " + ui->leAddr->text();
return false; fp = popen(arp_all_cmd.toUtf8().data(),"r");
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(!fp) if(!fp)
return false; return false;
fgets(ret,sizeof(ret),fp); fread(arp_all, 1, sizeof(arp_all), fp);
pclose(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 ) { fp = popen(arp_result.toUtf8().data(),"r");
// if (!strcmp(ret,"0")) { if(!fp)
// //printf("正常连接"); return false;
// return false; fgets(ret,sizeof(ret),fp);
// } else { pclose(fp);
if (strcmp(ret,"0")) {
//printf("ipv4地址冲突"); ret[strlen(ret)-1]=0;
QString strInfo = tr("IPV4 address conflict, Please change IP");
// QString buffer = "notify-send -i network-offline " + strInfo; if ( ret != NULL ) {
showNotify(strInfo); // if (!strcmp(ret,"0")) {
return true; // //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 (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(); this->setBtnEnableFalse();
return; return;
} }

View File

@ -96,7 +96,7 @@ private:
bool isActConf; //是否对已经连接的网络进行的更改 bool isActConf; //是否对已经连接的网络进行的更改
bool isCreateNewNet = false; //是否是创建的新网络 bool isCreateNewNet = false; //是否是创建的新网络
bool isShowSaveBtn = true; //是否显示保存按钮,即是否是编辑网络界面 bool isShowSaveBtn = true; //是否显示保存按钮,即是否是编辑网络界面
QString lastConnName, lastIpv4, netUuid, newUuid; QString lastConnName, lastIpv4, netUuid, newUuid, lastIpv6;
bool isActWifi; //是否是wifi网络 bool isActWifi; //是否是wifi网络
bool canCheckIpConflict = true; //当前是否可以执行IP冲突的检测 bool canCheckIpConflict = true; //当前是否可以执行IP冲突的检测
bool isIpv6Conflict = false; //ipv6地址是否冲突 bool isIpv6Conflict = false; //ipv6地址是否冲突

View File

@ -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");} 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 //设置手动分配ip
void kylin_network_set_manualmethod(char *con_name,char *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");} 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 // 设置手动分配all
void kylin_network_set_manualall(char *con_name, char *addr, char *mask, char *gateway, char *dns) void kylin_network_set_manualall(char *con_name, char *addr, char *mask, char *gateway, char *dns)
{ {

View File

@ -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_automethod(char *con_name);
void kylin_network_set_ipv6_automethod(char *con_name);
/* /*
* Set up manual IP assignment. * Set up manual IP assignment.
* @con_name is the connection name. * @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_manualmethod(char *con_name,char *ip);
void kylin_network_set_ipv6_manualmethod(char *con_name,char *ip);
/* /*
* Set up manual all prop. * Set up manual all prop.
*/ */

View File

@ -259,7 +259,7 @@ void OneLancForm::setLanInfo(QString str1, QString str2, QString str3, QString s
if (str1 == "" || str1 == "auto") { if (str1 == "" || str1 == "auto") {
str1 = tr("No Configuration"); str1 = tr("No Configuration");
str2 = tr("No Configuration"); // str2 = tr("No Configuration");
} }
if (str4 == "--" || str4 == "") { if (str4 == "--" || str4 == "") {