Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -6,16 +6,20 @@
#include "enablesqlmanager.h"

#include <QStringList>
#include <QMap>

Check warning on line 9 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 9 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 10 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 10 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QProcess>

Check warning on line 11 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 11 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusInterface>

Check warning on line 12 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusInterface> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 12 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDBusInterface> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusReply>

Check warning on line 13 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 13 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusConnection>

Check warning on line 14 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusConnection> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDBusConnection> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusObjectPath>

Check warning on line 15 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusObjectPath> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDBusObjectPath> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QCryptographicHash>

Check warning on line 16 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCryptographicHash> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QCryptographicHash> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegularExpression>

Check warning on line 17 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>

Check warning on line 18 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>

#define LEAST_NUM 10
#define REG_ADDRESS "^[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}$"
Expand Down Expand Up @@ -140,88 +144,101 @@

bool EnableUtils::ioctlOperateNetworkLogicalName(const QString &logicalName, bool enable)
{
if (logicalName.startsWith("wlan") || logicalName.startsWith("wlp")) { // Wireless LAN
// 第一步:获取 wiphy 编号
QProcess iwProcess;
iwProcess.start("iw", QStringList() << "dev" << logicalName << "info");
iwProcess.waitForFinished();
QString iwOutput = QString::fromUtf8(iwProcess.readAllStandardOutput());

// 解析 wiphy 编号
QRegularExpression wiphyRe("wiphy\\s+(\\d+)");
QRegularExpressionMatch wiphyMatch = wiphyRe.match(iwOutput);
if (!wiphyMatch.hasMatch()) {
qCritical() << "Failed to get wiphy number for interface: " << logicalName;
return false;
}
QString phyNum = wiphyMatch.captured(1);

// 第二步:获取 rfkill 设备编号
QProcess rfkillListProcess;
rfkillListProcess.start("rfkill", QStringList() << "list");
rfkillListProcess.waitForFinished();
QString rfkillOutput = QString::fromUtf8(rfkillListProcess.readAllStandardOutput());

// 查找对应的 rfkill 编号
QRegularExpression rfkillRe("^(\\d+):.*\\n.*\\n.*phy" + phyNum);
QRegularExpressionMatch rfkillMatch = rfkillRe.match(rfkillOutput);
QString rfkillId;
if (rfkillMatch.hasMatch()) {
rfkillId = rfkillMatch.captured(1);
}
// 方案一:通过NetworkManager D-Bus设置DeviceEnabled属性
if (enableNetworkByDBus(logicalName, enable))
return true;

// 第三步:执行 rfkill block/unblock
if (!rfkillId.isEmpty()) {
QProcess rfkillBlockProcess;
rfkillBlockProcess.start("rfkill", QStringList() << (enable ? "unblock" : "block") << rfkillId);
rfkillBlockProcess.waitForFinished();
int ret = rfkillBlockProcess.exitCode();
if (ret != 0) {
qCritical() << "Failed to block/unblock wifi: error code: " << ret;
}
}
// 方案二:方案一失败,回退到ioctl方式
qWarning() << "Fallback to ioctl method for" << logicalName;
return enableNetworkByIoctl(logicalName, enable);
}

// 第四步:执行 ifconfig up/down
QProcess ifconfigProcess;
ifconfigProcess.start("/sbin/ifconfig", QStringList() << logicalName << (enable ? "up" : "down"));
ifconfigProcess.waitForFinished();
int ret = ifconfigProcess.exitCode();
if (ret != 0) {
qCritical() << "Failed to up/down network: " << logicalName << enable << " error code: " << ret;
return false;
}
} else {
// 1. 通过ioctl禁用
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return false;

struct ifreq ifr;
strncpy(ifr.ifr_name, logicalName.toStdString().c_str(),IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
short flag;
if (enable) {
flag = IFF_UP | IFF_PROMISC;
} else {
flag = ~(IFF_UP | IFF_PROMISC);
}
// 先获取标识
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
close(fd);
return false;
}
// 获取后重新设置标识
if (enable) {
ifr.ifr_ifru.ifru_flags |= flag;
} else {
ifr.ifr_ifru.ifru_flags &= flag;
}
// 方案一:通过NetworkManager D-Bus设置DeviceEnabled属性
bool EnableUtils::enableNetworkByDBus(const QString &logicalName, bool enable)
{
// 1. 通过 system dbus 获取 devicePath
QDBusInterface nmInterface("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
QDBusConnection::systemBus());
if (!nmInterface.isValid()) {
qCritical() << "Failed to connect to NetworkManager:" << nmInterface.lastError().message();
return false;
}

if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
close(fd);
return false;
}
QDBusReply<QDBusObjectPath> reply = nmInterface.call("GetDeviceByIpIface", logicalName);
if (!reply.isValid()) {
qCritical() << "Failed to GetDeviceByIpIface for" << logicalName << ":" << reply.error().message();
return false;
}

QString devicePath = reply.value().path();
if (devicePath.isEmpty()) {
qCritical() << "Got empty devicePath for interface:" << logicalName;
return false;
}

// 2. 通过 system dbus 设置 DeviceEnabled 属性
QDBusInterface deviceInterface("org.freedesktop.NetworkManager",
devicePath,
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus());
if (!deviceInterface.isValid()) {
qCritical() << "Failed to connect to device properties:" << deviceInterface.lastError().message();
return false;
}

QDBusReply<void> setReply = deviceInterface.call("Set",
"org.freedesktop.NetworkManager.Device",
"DeviceEnabled",
QVariant::fromValue(QDBusVariant(enable)));
if (!setReply.isValid()) {
qCritical() << "Failed to set DeviceEnabled for" << logicalName << ":" << setReply.error().message();
return false;
}

return true;
}

// 方案二:通过ioctl控制网卡
bool EnableUtils::enableNetworkByIoctl(const QString &logicalName, bool enable)
{
// 安全校验:网卡名长度合法性
if (logicalName.isEmpty() || logicalName.length() >= IFNAMSIZ) {
qCritical() << "Invalid logicalName length for ioctl";
return false;
}

// 通过ioctl设置网卡启用/禁用
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return false;

struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));

std::string nameStr = logicalName.toStdString();
strncpy(ifr.ifr_name, nameStr.c_str(),IFNAMSIZ - 1);
ifr.ifr_name[IFNAMSIZ - 1] = '\0';

// 先获取标识
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
close(fd);
return false;
}

if (enable) {
ifr.ifr_flags |= IFF_UP;
} else {
ifr.ifr_flags &= ~IFF_UP;
}
// 设置新标识
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
close(fd);
return false;
}

close(fd);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -25,13 +25,29 @@ class EnableUtils
static void disableInDevice();

/**
* @brief ioctlOperateNetworkLogicalName 通过ioctl设置网卡是否可用
* @param logicalName
* @param enable
* @return
* @brief ioctlOperateNetworkLogicalName 设置网卡是否可用,优先使用D-Bus方案,失败回退到ioctl方案
* @param logicalName 网卡逻辑名称
* @param enable true启用,false禁用
* @return 成功返回true,失败返回false
*/
static bool ioctlOperateNetworkLogicalName(const QString &logicalName, bool enable);

/**
* @brief enableNetworkByDBus 方案一:通过NetworkManager D-Bus设置DeviceEnabled属性
* @param logicalName 网卡逻辑名称
* @param enable true启用,false禁用
* @return 成功返回true,失败返回false
*/
static bool enableNetworkByDBus(const QString &logicalName, bool enable);

/**
* @brief enableNetworkByIoctl 方案二:通过ioctl控制网卡是否可用
* @param logicalName 网卡逻辑名称
* @param enable true启用,false禁用
* @return 成功返回true,失败返回false
*/
static bool enableNetworkByIoctl(const QString &logicalName, bool enable);

/**
* @brief getMapInfo 解析usb信息
* @param item
Expand Down
Loading