1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QNETWORKINTERFACEPRIVATE_H |
5 | #define QNETWORKINTERFACEPRIVATE_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
19 | #include <QtNetwork/qnetworkinterface.h> |
20 | #include <QtCore/qatomic.h> |
21 | #include <QtCore/qdeadlinetimer.h> |
22 | #include <QtCore/qlist.h> |
23 | #include <QtCore/qstring.h> |
24 | #include <QtNetwork/qhostaddress.h> |
25 | #include <QtNetwork/qabstractsocket.h> |
26 | #include <private/qhostaddress_p.h> |
27 | |
28 | #ifndef QT_NO_NETWORKINTERFACE |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class QNetworkAddressEntryPrivate |
33 | { |
34 | public: |
35 | QHostAddress address; |
36 | QHostAddress broadcast; |
37 | QDeadlineTimer preferredLifetime = QDeadlineTimer::Forever; |
38 | QDeadlineTimer validityLifetime = QDeadlineTimer::Forever; |
39 | |
40 | QNetmask netmask; |
41 | bool lifetimeKnown = false; |
42 | QNetworkAddressEntry::DnsEligibilityStatus dnsEligibility = QNetworkAddressEntry::DnsEligibilityUnknown; |
43 | }; |
44 | |
45 | class QNetworkInterfacePrivate: public QSharedData |
46 | { |
47 | public: |
48 | QNetworkInterfacePrivate() : index(0) |
49 | { } |
50 | ~QNetworkInterfacePrivate() |
51 | { } |
52 | |
53 | int index; // interface index, if know |
54 | int mtu = 0; |
55 | QNetworkInterface::InterfaceFlags flags; |
56 | QNetworkInterface::InterfaceType type = QNetworkInterface::Unknown; |
57 | |
58 | QString name; |
59 | QString friendlyName; |
60 | QString hardwareAddress; |
61 | |
62 | QList<QNetworkAddressEntry> addressEntries; |
63 | |
64 | static QString makeHwAddress(int len, uchar *data); |
65 | static void calculateDnsEligibility(QNetworkAddressEntry *entry, bool isTemporary, |
66 | bool isDeprecated) |
67 | { |
68 | // this implements an algorithm that yields the same results as Windows |
69 | // produces, for the same input (as far as I can test) |
70 | if (isTemporary || isDeprecated) { |
71 | entry->setDnsEligibility(QNetworkAddressEntry::DnsIneligible); |
72 | } else { |
73 | AddressClassification cl = QHostAddressPrivate::classify(address: entry->ip()); |
74 | if (cl == LoopbackAddress || cl == LinkLocalAddress) |
75 | entry->setDnsEligibility(QNetworkAddressEntry::DnsIneligible); |
76 | else |
77 | entry->setDnsEligibility(QNetworkAddressEntry::DnsEligible); |
78 | } |
79 | } |
80 | |
81 | private: |
82 | // disallow copying -- avoid detaching |
83 | QNetworkInterfacePrivate &operator=(const QNetworkInterfacePrivate &other); |
84 | QNetworkInterfacePrivate(const QNetworkInterfacePrivate &other); |
85 | }; |
86 | |
87 | class QNetworkInterfaceManager |
88 | { |
89 | public: |
90 | QNetworkInterfaceManager(); |
91 | ~QNetworkInterfaceManager(); |
92 | |
93 | QSharedDataPointer<QNetworkInterfacePrivate> interfaceFromName(const QString &name); |
94 | QSharedDataPointer<QNetworkInterfacePrivate> interfaceFromIndex(int index); |
95 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > allInterfaces(); |
96 | |
97 | static uint interfaceIndexFromName(const QString &name); |
98 | static QString interfaceNameFromIndex(uint index); |
99 | |
100 | // convenience: |
101 | QSharedDataPointer<QNetworkInterfacePrivate> empty; |
102 | |
103 | private: |
104 | QList<QNetworkInterfacePrivate *> scan(); |
105 | }; |
106 | |
107 | |
108 | QT_END_NAMESPACE |
109 | |
110 | #endif // QT_NO_NETWORKINTERFACE |
111 | |
112 | #endif |
113 |