当前位置: 首页 > news >正文

Qt之QNetworkInterface

简介

用于表示网络接口(即网卡)信息

常用接口

static QList<QNetworkInterface> allInterfaces();
static QList<QHostAddress> allAddresses();
QList<QNetworkAddressEntry> addressEntries() const;

接口类型

用枚举InterfaceType表示

enum InterfaceType {
        Loopback = 1,
        Virtual,
        Ethernet,
        Slip,
        CanBus,
        Ppp,
        Fddi,
        Wifi,
        Ieee80211 = Wifi,   // alias
        Phonet,
        Ieee802154,
        SixLoWPAN,  // 6LoWPAN, but we can't start with a digit
        Ieee80216,
        Ieee1394,

        Unknown = 0
    };

接口状态

用枚举InterfaceFlag表示

enum InterfaceFlag {
        IsUp = 0x1,
        IsRunning = 0x2,
        CanBroadcast = 0x4,
        IsLoopBack = 0x8,
        IsPointToPoint = 0x10,
        CanMulticast = 0x20
    };

结构

QNetworkInterface
- QSharedDataPointer<QNetworkInterfacePrivate> d
QNetworkInterfacePrivate
+ int index
+ int mtu
+ QNetworkInterface::InterfaceFlags flags
+ QNetworkInterface::InterfaceType type
+ QString name
+ QString friendlyName
+ QString hardwareAddress
+ QList<QNetworkAddressEntry> addressEntries
QHostAddress
- QExplicitlySharedDataPointer<QHostAddressPrivate> d
QHostAddressPrivate
+ QString scopeId
+ quint32 a
+ qint8 protocol
QNetworkAddressEntryPrivate
+ QHostAddress address
+ QHostAddress broadcast
+ QDeadlineTimer preferredLifetime
+ QDeadlineTimer validityLifetime
+ QNetmask netmask
+ bool lifetimeKnown
+ QNetworkAddressEntry::DnsEligibilityStatus dnsEligibility
QNetworkAddressEntry
QSharedData

QNetworkAddressEntry :用来表示网络地址实体,即包含地址,广播地址以及掩码
QHostAddress:包含地址以及协议
QHostAddressPrivate成员包含:

QString scopeId;

union {
    Q_IPV6ADDR a6; // IPv6 address
    struct { quint64 c[2]; } a6_64;
    struct { quint32 c[4]; } a6_32;
};
quint32 a;    // IPv4 address
qint8 protocol;

QNetworkInterfaceManager

网络接口管理器,获取电脑上所有可用的网络接口
定义有全局静态变量

Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager)

其结构为

QNetworkInterfaceManager
+ QSharedDataPointer<QNetworkInterfacePrivate> empty
+static uint interfaceIndexFromName(const QString &name)
+static QString interfaceNameFromIndex(uint index)
+QList~QSharedDataPointer~QNetworkInterfacePrivate~ ~ allInterfaces()
-QList~QNetworkInterfacePrivate *~ scan()

allInterfaces:获取所有的网络接口,先通过scan得到所有网络接口,winrt,win,unix不同平整对scan有不同实现,然后调用 postProcess作后处理

QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
{
    const QList<QNetworkInterfacePrivate *> list = postProcess(scan());
    QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;
    result.reserve(list.size());

    for (QNetworkInterfacePrivate *ptr : list) {
        if ((ptr->flags & QNetworkInterface::IsUp) == 0) {
            // if the network interface isn't UP, the addresses are ineligible for DNS
            for (auto &addr : ptr->addressEntries)
                addr.setDnsEligibility(QNetworkAddressEntry::DnsIneligible);
        }

        result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
    }

    return result;
}

static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list)
{
    // Some platforms report a netmask but don't report a broadcast address
    // Go through all available addresses and calculate the broadcast address
    // from the IP and the netmask
    //
    // This is an IPv4-only thing -- IPv6 has no concept of broadcasts
    // The math is:
    //    broadcast = IP | ~netmask

    for (QNetworkInterfacePrivate *interface : list) {
        for (QNetworkAddressEntry &address : interface->addressEntries) {
            if (address.ip().protocol() != QAbstractSocket::IPv4Protocol)
                continue;

            if (!address.netmask().isNull() && address.broadcast().isNull()) {
                QHostAddress bcast = address.ip();
                bcast = QHostAddress(bcast.toIPv4Address() | ~address.netmask().toIPv4Address());
                address.setBroadcast(bcast);
            }
        }
    }

    return list;
}
http://www.dtcms.com/a/112886.html

相关文章:

  • 低代码开发平台:飞帆中的控件中转区
  • AI Agent设计模式三:Routing
  • 智能合约的法律挑战与解决之道:技术与法律的交融
  • 《P1072 [NOIP 2009 提高组] Hankson 的趣味题》
  • 小米BE3600路由器信息
  • 【愚公系列】《高效使用DeepSeek》053-工艺参数调优
  • [ctfshow web入门] web5
  • MySQL表结构导出(Excel)
  • 状态模式~
  • 【蓝桥杯】十五届省赛B组c++
  • 神经网络入门:生动解读机器学习的“神经元”
  • C++ KMP算法
  • Leetcode - 双周赛153
  • 失眠睡不着运动锻炼贴士
  • Mac强制解锁APP或文件夹
  • Java的Selenium常用的元素操作API
  • 【图像处理基石】什么是AWB?
  • 扩展库Scrapy:Python网络爬虫的利器
  • 【Rust学习】Rust数据类型,函数,条件语句,循环
  • 实战打靶集锦-38-inclusiveness
  • pyTorch框架使用CNN进行手写数字识别
  • AI比人脑更强,因为被植入思维模型【43】蝴蝶效应思维模型
  • 多模态智能体框架MM-StoryAgent:跨模态叙事视频生成的技术突破
  • 九、重学C++—类和函数
  • QGIS中第三方POI坐标偏移的快速校正-百度POI
  • C#编程基础知识点介绍
  • 亚马逊系统异常48小时:这3类商品退货政策有变
  • 开源 LLM 应用开发平台 Dify 全栈部署指南(Docker Compose 方案)
  • SpringWebFlux路由函数:RouterFunction与HandlerFunction
  • 简单多状态dp问题 + 总结(一)