fix: optimize network switching detection - #601
Conversation
Reviewer's GuideRefactors InternetChecker to avoid oscillating primary NICs by probing each candidate interface’s real connectivity (DNS + TCP from that interface) before switching, and switches primary routing via per-device route metrics instead of toggling never-default and relying on NetworkManager primary-connection changes. Sequence diagram for optimized NIC switching in InternetCheckersequenceDiagram
actor Caller
participant InternetChecker
participant NetworkManager
participant Device
Caller->>InternetChecker: switchInternetAccess(checkPrimaryConnection)
InternetChecker->>NetworkManager: primaryConnection()
InternetChecker->>NetworkManager: networkInterfaces()
InternetChecker->>InternetChecker: build checkedDevices (wired + wireless)
alt checkedDevices is empty
InternetChecker-->>Caller: switchFailed()
else checkedDevices not empty
alt checkPrimaryConnection && primaryDevice exists
InternetChecker->>InternetChecker: checkInterfaceOnline(primaryDevice)
alt primaryDevice online
InternetChecker-->>Caller: switchSuccess()
else primaryDevice offline
loop for each device in checkedDevices
InternetChecker->>InternetChecker: checkInterfaceOnline(device)
alt device online
InternetChecker->>InternetChecker: setPrimaryDevice(device, devices)
InternetChecker-->>Caller: switchSuccess()
%% break
end
end
InternetChecker-->>Caller: switchFailed()
end
else no primary check or no primaryDevice
loop for each device in checkedDevices
InternetChecker->>InternetChecker: checkInterfaceOnline(device)
alt device online
InternetChecker->>InternetChecker: setPrimaryDevice(device, devices)
InternetChecker-->>Caller: switchSuccess()
%% break
end
end
InternetChecker-->>Caller: switchFailed()
end
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
|
||
| using namespace network::systemservice; | ||
|
|
||
| // 注册 NMVariantMapMap (QMap<QString,QVariantMap>) 到 D-Bus 元类型系统, |
There was a problem hiding this comment.
注册 NMVariantMapMap的代码没看到,代码与注释不一致?
When network connectivity is lost, automatically switch to another NIC that has internet access. Previous approach: After detecting network unreachable, sequentially switch the primary link to the next NIC, then check if the switched NIC has connectivity. If still unreachable, continue switching. This causes NICs to keep switching back and forth. Improved approach: After detecting network unreachable, individually check each alternative NIC to see if it can access the internet. Only switch the primary link to the target NIC after confirming it is online; otherwise, do not switch. fix: 优化网络切换检测功能 在检测到网络不通的情况下,自动切换到其他可以上网的网卡 之前方案:在检测到网络不通后,依次将主链接切换到下一个网卡,再判断切换后的网卡网络是否通,如果还是不通,继续切换,这样就会导致网卡在不停切换 修改方案:在检测到网络不通后,依次针对其他的网卡单独检测这个网卡是否可以上网,只有在检测到可以上网后,才将主链接切换到当前网卡,否则不予切换 Log: 优化网卡自动切换功能 Influence: 切换主链接 Bug: https://pms.uniontech.com/bug-view-372223.html
deepin pr auto review★ 总体评分:95分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 // 优化 parseDnsResponse 中的边界检查
bool InternetChecker::parseDnsResponse(const unsigned char *buf, int len, unsigned short txId, in_addr &outIp)
{
// ... 前置代码 ...
// 跳过 Question Section:每个问题包含域名 + QTYPE(2) + QCLASS(2)
for (int i = 0; i < qnum; i++) {
bool compressed = false;
// 跳过域名:逐标签跳过,遇到压缩指针(高两 bit 为 11)则跳过 2 字节
while (p < len && buf[p] != 0) {
if ((buf[p] & 0xC0) == 0xC0) {
if (p + 2 > len)
break;
p += 2;
compressed = true;
break;
}
// 增加边界检查,防止 p 溢出
if (p + buf[p] + 1 >= len) {
p = len; // 强制退出
break;
}
p += buf[p] + 1;
}
// ... 后续代码 ...
}
// ... 后续 Answer Section 解析逻辑 ...
} |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: caixr23, ut003640 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/force |
|
/forcemerge |
When network connectivity is lost, automatically switch to another NIC that has internet access.
Previous approach: After detecting network unreachable, sequentially switch the primary link to the next NIC, then check if the switched NIC has connectivity. If still unreachable, continue switching. This causes NICs to keep switching back and forth.
Improved approach: After detecting network unreachable, individually check each alternative NIC to see if it can access the internet. Only switch the primary link to the target NIC after confirming it is online; otherwise, do not switch.
fix: 优化网络切换检测功能
在检测到网络不通的情况下,自动切换到其他可以上网的网卡
之前方案:在检测到网络不通后,依次将主链接切换到下一个网卡,再判断切换后的网卡网络是否通,如果还是不通,继续切换,这样就会导致网卡在不停切换 修改方案:在检测到网络不通后,依次针对其他的网卡单独检测这个网卡是否可以上网,只有在检测到可以上网后,才将主链接切换到当前网卡,否则不予切换
Log: 优化网卡自动切换功能
Influence: 切换主链接
Bug: https://pms.uniontech.com/bug-view-372223.html
Summary by Sourcery
Optimize automatic NIC switching so the primary link only moves to interfaces that are confirmed to have internet connectivity, reducing unnecessary flapping between devices.
Bug Fixes:
Enhancements: