Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/kernel/qnetworkinterface.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | - | |||||||
2 | - | |||||||
3 | - | |||||||
4 | - | |||||||
5 | - | |||||||
6 | - | |||||||
7 | static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list) | - | ||||||
8 | { | - | ||||||
9 | QList<QNetworkInterfacePrivate *>::Iterator it = list.begin(); | - | ||||||
10 | const QList<QNetworkInterfacePrivate *>::Iterator end = list.end(); | - | ||||||
11 | for ( ; it != end; ++it) { | - | ||||||
12 | QList<QNetworkAddressEntry>::Iterator addr_it = (*it)->addressEntries.begin(); | - | ||||||
13 | const QList<QNetworkAddressEntry>::Iterator addr_end = (*it)->addressEntries.end(); | - | ||||||
14 | for ( ; addr_it != addr_end; ++addr_it) { | - | ||||||
15 | if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol) | - | ||||||
16 | continue; | - | ||||||
17 | - | |||||||
18 | if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) { | - | ||||||
19 | QHostAddress bcast = addr_it->ip(); | - | ||||||
20 | bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address()); | - | ||||||
21 | addr_it->setBroadcast(bcast); | - | ||||||
22 | } | - | ||||||
23 | } | - | ||||||
24 | } | - | ||||||
25 | - | |||||||
26 | return list; | - | ||||||
27 | } | - | ||||||
28 | - | |||||||
29 | namespace { namespace Q_QGS_manager { typedef QNetworkInterfaceManager Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized) guard.store(QtGlobalStatic::Destroyed); } }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ())) : value () { guard.store(QtGlobalStatic::Initialized); } } holder; return &holder.value; } } } static QGlobalStatic<QNetworkInterfaceManager, Q_QGS_manager::innerFunction, Q_QGS_manager::guard> manager; | - | ||||||
30 | - | |||||||
31 | QNetworkInterfaceManager::QNetworkInterfaceManager() | - | ||||||
32 | { | - | ||||||
33 | } | - | ||||||
34 | - | |||||||
35 | QNetworkInterfaceManager::~QNetworkInterfaceManager() | - | ||||||
36 | { | - | ||||||
37 | } | - | ||||||
38 | - | |||||||
39 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name) | - | ||||||
40 | { | - | ||||||
41 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces(); | - | ||||||
42 | QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin(); | - | ||||||
43 | - | |||||||
44 | bool ok; | - | ||||||
45 | uint index = name.toUInt(&ok); | - | ||||||
46 | - | |||||||
47 | for ( ; it != interfaceList.constEnd(); ++it) { | - | ||||||
48 | if (ok && (*it)->index == int(index)) | - | ||||||
49 | return *it; | - | ||||||
50 | else if ((*it)->name == name) | - | ||||||
51 | return *it; | - | ||||||
52 | } | - | ||||||
53 | - | |||||||
54 | return empty; | - | ||||||
55 | } | - | ||||||
56 | - | |||||||
57 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index) | - | ||||||
58 | { | - | ||||||
59 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces(); | - | ||||||
60 | QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin(); | - | ||||||
61 | for ( ; it != interfaceList.constEnd(); ++it) | - | ||||||
62 | if ((*it)->index == index) | - | ||||||
63 | return *it; | - | ||||||
64 | - | |||||||
65 | return empty; | - | ||||||
66 | } | - | ||||||
67 | - | |||||||
68 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces() | - | ||||||
69 | { | - | ||||||
70 | const QList<QNetworkInterfacePrivate *> list = postProcess(scan()); | - | ||||||
71 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > result; | - | ||||||
72 | result.reserve(list.size()); | - | ||||||
73 | - | |||||||
74 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(list)>::type> _container_((list)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1)for (QNetworkInterfacePrivate *ptr = *_container_.i; _container_.control; _container_.control = 0: list) | - | ||||||
75 | result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr); executed 612 times by 19 tests: result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr); Executed by:
| 612 | ||||||
76 | - | |||||||
77 | return executed 204 times by 19 tests: result;return result; Executed by:
executed 204 times by 19 tests: return result; Executed by:
| 204 | ||||||
78 | } | - | ||||||
79 | - | |||||||
80 | QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data) | - | ||||||
81 | { | - | ||||||
82 | const int outLen = qMax(len * 2 + (len - 1) * 1, 0); | - | ||||||
83 | QString result(outLen, Qt::Uninitialized); | - | ||||||
84 | QChar *out = result.data(); | - | ||||||
85 | for (int i = 0; i < len; ++i) { | - | ||||||
86 | if (i) | - | ||||||
87 | *out++ = QLatin1Char(':'); | - | ||||||
88 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] / 16)); | - | ||||||
89 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] % 16)); | - | ||||||
90 | } | - | ||||||
91 | return result; | - | ||||||
92 | } | - | ||||||
93 | QNetworkAddressEntry::QNetworkAddressEntry() | - | ||||||
94 | : d(new QNetworkAddressEntryPrivate) | - | ||||||
95 | { | - | ||||||
96 | } | - | ||||||
97 | - | |||||||
98 | - | |||||||
99 | - | |||||||
100 | - | |||||||
101 | - | |||||||
102 | QNetworkAddressEntry::QNetworkAddressEntry(const QNetworkAddressEntry &other) | - | ||||||
103 | : d(new QNetworkAddressEntryPrivate(*other.d.data())) | - | ||||||
104 | { | - | ||||||
105 | } | - | ||||||
106 | - | |||||||
107 | - | |||||||
108 | - | |||||||
109 | - | |||||||
110 | QNetworkAddressEntry &QNetworkAddressEntry::operator=(const QNetworkAddressEntry &other) | - | ||||||
111 | { | - | ||||||
112 | *d.data() = *other.d.data(); | - | ||||||
113 | return *this; | - | ||||||
114 | } | - | ||||||
115 | QNetworkAddressEntry::~QNetworkAddressEntry() | - | ||||||
116 | { | - | ||||||
117 | } | - | ||||||
118 | - | |||||||
119 | - | |||||||
120 | - | |||||||
121 | - | |||||||
122 | - | |||||||
123 | bool QNetworkAddressEntry::operator==(const QNetworkAddressEntry &other) const | - | ||||||
124 | { | - | ||||||
125 | if (d == other.d) return true; | - | ||||||
126 | if (!d || !other.d) return false; | - | ||||||
127 | return d->address == other.d->address && | - | ||||||
128 | d->netmask == other.d->netmask && | - | ||||||
129 | d->broadcast == other.d->broadcast; | - | ||||||
130 | } | - | ||||||
131 | QHostAddress QNetworkAddressEntry::ip() const | - | ||||||
132 | { | - | ||||||
133 | return d->address; | - | ||||||
134 | } | - | ||||||
135 | - | |||||||
136 | - | |||||||
137 | - | |||||||
138 | - | |||||||
139 | - | |||||||
140 | void QNetworkAddressEntry::setIp(const QHostAddress &newIp) | - | ||||||
141 | { | - | ||||||
142 | d->address = newIp; | - | ||||||
143 | } | - | ||||||
144 | QHostAddress QNetworkAddressEntry::netmask() const | - | ||||||
145 | { | - | ||||||
146 | return d->netmask; | - | ||||||
147 | } | - | ||||||
148 | void QNetworkAddressEntry::setNetmask(const QHostAddress &newNetmask) | - | ||||||
149 | { | - | ||||||
150 | if (newNetmask.protocol() != ip().protocol()) { | - | ||||||
151 | d->netmask = QNetmaskAddress(); | - | ||||||
152 | return; | - | ||||||
153 | } | - | ||||||
154 | - | |||||||
155 | d->netmask.setAddress(newNetmask); | - | ||||||
156 | } | - | ||||||
157 | int QNetworkAddressEntry::prefixLength() const | - | ||||||
158 | { | - | ||||||
159 | return d->netmask.prefixLength(); | - | ||||||
160 | } | - | ||||||
161 | void QNetworkAddressEntry::setPrefixLength(int length) | - | ||||||
162 | { | - | ||||||
163 | d->netmask.setPrefixLength(d->address.protocol(), length); | - | ||||||
164 | } | - | ||||||
165 | QHostAddress QNetworkAddressEntry::broadcast() const | - | ||||||
166 | { | - | ||||||
167 | return d->broadcast; | - | ||||||
168 | } | - | ||||||
169 | - | |||||||
170 | - | |||||||
171 | - | |||||||
172 | - | |||||||
173 | - | |||||||
174 | void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast) | - | ||||||
175 | { | - | ||||||
176 | d->broadcast = newBroadcast; | - | ||||||
177 | } | - | ||||||
178 | QNetworkInterface::QNetworkInterface() | - | ||||||
179 | : d(0) | - | ||||||
180 | { | - | ||||||
181 | } | - | ||||||
182 | - | |||||||
183 | - | |||||||
184 | - | |||||||
185 | - | |||||||
186 | QNetworkInterface::~QNetworkInterface() | - | ||||||
187 | { | - | ||||||
188 | } | - | ||||||
189 | - | |||||||
190 | - | |||||||
191 | - | |||||||
192 | - | |||||||
193 | - | |||||||
194 | QNetworkInterface::QNetworkInterface(const QNetworkInterface &other) | - | ||||||
195 | : d(other.d) | - | ||||||
196 | { | - | ||||||
197 | } | - | ||||||
198 | - | |||||||
199 | - | |||||||
200 | - | |||||||
201 | - | |||||||
202 | - | |||||||
203 | QNetworkInterface &QNetworkInterface::operator=(const QNetworkInterface &other) | - | ||||||
204 | { | - | ||||||
205 | d = other.d; | - | ||||||
206 | return *this; | - | ||||||
207 | } | - | ||||||
208 | bool QNetworkInterface::isValid() const | - | ||||||
209 | { | - | ||||||
210 | return !name().isEmpty(); | - | ||||||
211 | } | - | ||||||
212 | int QNetworkInterface::index() const | - | ||||||
213 | { | - | ||||||
214 | return d ? d->index : 0; | - | ||||||
215 | } | - | ||||||
216 | - | |||||||
217 | - | |||||||
218 | - | |||||||
219 | - | |||||||
220 | - | |||||||
221 | - | |||||||
222 | - | |||||||
223 | QString QNetworkInterface::name() const | - | ||||||
224 | { | - | ||||||
225 | return d ? d->name : QString(); | - | ||||||
226 | } | - | ||||||
227 | QString QNetworkInterface::humanReadableName() const | - | ||||||
228 | { | - | ||||||
229 | return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString(); | - | ||||||
230 | } | - | ||||||
231 | - | |||||||
232 | - | |||||||
233 | - | |||||||
234 | - | |||||||
235 | QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const | - | ||||||
236 | { | - | ||||||
237 | return d ? d->flags : InterfaceFlags(0); | - | ||||||
238 | } | - | ||||||
239 | QString QNetworkInterface::hardwareAddress() const | - | ||||||
240 | { | - | ||||||
241 | return d ? d->hardwareAddress : QString(); | - | ||||||
242 | } | - | ||||||
243 | QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const | - | ||||||
244 | { | - | ||||||
245 | return d ? d->addressEntries : QList<QNetworkAddressEntry>(); | - | ||||||
246 | } | - | ||||||
247 | int QNetworkInterface::interfaceIndexFromName(const QString &name) | - | ||||||
248 | { | - | ||||||
249 | if (name.isEmpty()
| 27-1556 | ||||||
250 | return executed 1556 times by 15 tests: 0;return 0; Executed by:
executed 1556 times by 15 tests: return 0; Executed by:
| 1556 | ||||||
251 | - | |||||||
252 | bool ok; | - | ||||||
253 | uint id = name.toUInt(&ok); | - | ||||||
254 | if (!ok
| 0-27 | ||||||
255 | id = QNetworkInterfaceManager::interfaceIndexFromName(name); executed 27 times by 3 tests: id = QNetworkInterfaceManager::interfaceIndexFromName(name); Executed by:
| 27 | ||||||
256 | return executed 27 times by 3 tests: int(id);return int(id); Executed by:
executed 27 times by 3 tests: return int(id); Executed by:
| 27 | ||||||
257 | } | - | ||||||
258 | QNetworkInterface QNetworkInterface::interfaceFromName(const QString &name) | - | ||||||
259 | { | - | ||||||
260 | QNetworkInterface result; | - | ||||||
261 | result.d = manager()->interfaceFromName(name); | - | ||||||
262 | return result; | - | ||||||
263 | } | - | ||||||
264 | QNetworkInterface QNetworkInterface::interfaceFromIndex(int index) | - | ||||||
265 | { | - | ||||||
266 | QNetworkInterface result; | - | ||||||
267 | result.d = manager()->interfaceFromIndex(index); | - | ||||||
268 | return result; | - | ||||||
269 | } | - | ||||||
270 | QString QNetworkInterface::interfaceNameFromIndex(int index) | - | ||||||
271 | { | - | ||||||
272 | if (!index
| 0-33 | ||||||
273 | return never executed: QString();return QString(); never executed: return QString(); | 0 | ||||||
274 | return executed 33 times by 3 tests: QNetworkInterfaceManager::interfaceNameFromIndex(index);return QNetworkInterfaceManager::interfaceNameFromIndex(index); Executed by:
executed 33 times by 3 tests: return QNetworkInterfaceManager::interfaceNameFromIndex(index); Executed by:
| 33 | ||||||
275 | } | - | ||||||
276 | - | |||||||
277 | - | |||||||
278 | - | |||||||
279 | - | |||||||
280 | - | |||||||
281 | QList<QNetworkInterface> QNetworkInterface::allInterfaces() | - | ||||||
282 | { | - | ||||||
283 | const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); | - | ||||||
284 | QList<QNetworkInterface> result; | - | ||||||
285 | result.reserve(privs.size()); | - | ||||||
286 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(privs)>::type> _container_((privs)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1)for (const QSharedDataPointer<QNetworkInterfacePrivate>auto &p = *_container_.i; _container_.control; _container_.control = 0: privs) { | - | ||||||
287 | QNetworkInterface item; | - | ||||||
288 | item.d = p; | - | ||||||
289 | result << item; | - | ||||||
290 | } executed 507 times by 19 tests: end of block Executed by:
| 507 | ||||||
291 | - | |||||||
292 | return executed 169 times by 19 tests: result;return result; Executed by:
executed 169 times by 19 tests: return result; Executed by:
| 169 | ||||||
293 | } | - | ||||||
294 | - | |||||||
295 | - | |||||||
296 | - | |||||||
297 | - | |||||||
298 | - | |||||||
299 | - | |||||||
300 | - | |||||||
301 | QList<QHostAddress> QNetworkInterface::allAddresses() | - | ||||||
302 | { | - | ||||||
303 | const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); | - | ||||||
304 | QList<QHostAddress> result; | - | ||||||
for (QForeachContainer<typename QtPrivate::remove_reference<decltype(privs)>::type> _container_((privs)); _container_.control && _container_.i != _container_.e; | ||||||||
305 | ++_container_.i, _container_.control ^= 1)for (const QSharedDataPointer<QNetworkInterfacePrivate>auto &p = *_container_.i; _container_.control; _container_.control = 0: privs) { | - | ||||||
306 | for (QForeachContainer<typename QtPrivate::remove_reference<decltypeconst QNetworkAddressEntry &entry : qAsConst(p->addressEntries)>::type> _container_((p->addressEntries)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1) for (const QNetworkAddressEntry &entry = *_container_.i; _container_.control; _container_.control = 0))) | - | ||||||
307 | result += entry.ip(); executed 24 times by 2 tests: result += entry.ip(); Executed by:
| 24 | ||||||
308 | } executed 12 times by 2 tests: end of block Executed by:
| 12 | ||||||
309 | - | |||||||
310 | return executed 4 times by 2 tests: result;return result; Executed by:
executed 4 times by 2 tests: return result; Executed by:
| 4 | ||||||
311 | } | - | ||||||
312 | - | |||||||
313 | - | |||||||
314 | static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags) | - | ||||||
315 | { | - | ||||||
316 | if (flags & QNetworkInterface::IsUp) | - | ||||||
317 | debug << "IsUp "; | - | ||||||
318 | if (flags & QNetworkInterface::IsRunning) | - | ||||||
319 | debug << "IsRunning "; | - | ||||||
320 | if (flags & QNetworkInterface::CanBroadcast) | - | ||||||
321 | debug << "CanBroadcast "; | - | ||||||
322 | if (flags & QNetworkInterface::IsLoopBack) | - | ||||||
323 | debug << "IsLoopBack "; | - | ||||||
324 | if (flags & QNetworkInterface::IsPointToPoint) | - | ||||||
325 | debug << "IsPointToPoint "; | - | ||||||
326 | if (flags & QNetworkInterface::CanMulticast) | - | ||||||
327 | debug << "CanMulticast "; | - | ||||||
328 | return debug; | - | ||||||
329 | } | - | ||||||
330 | - | |||||||
331 | static inline QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry) | - | ||||||
332 | { | - | ||||||
333 | debug << "(address = " << entry.ip(); | - | ||||||
334 | if (!entry.netmask().isNull()) | - | ||||||
335 | debug << ", netmask = " << entry.netmask(); | - | ||||||
336 | if (!entry.broadcast().isNull()) | - | ||||||
337 | debug << ", broadcast = " << entry.broadcast(); | - | ||||||
338 | debug << ')'; | - | ||||||
339 | return debug; | - | ||||||
340 | } | - | ||||||
341 | - | |||||||
342 | QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface) | - | ||||||
343 | { | - | ||||||
344 | QDebugStateSaver saver(debug); | - | ||||||
345 | debug.resetFormat().nospace(); | - | ||||||
346 | debug << "QNetworkInterface(name = " << networkInterface.name() | - | ||||||
347 | << ", hardware address = " << networkInterface.hardwareAddress() | - | ||||||
348 | << ", flags = "; | - | ||||||
349 | flagsDebug(debug, networkInterface.flags()); | - | ||||||
350 | debug << ", entries = " << networkInterface.addressEntries() | - | ||||||
351 | << ")\n"; | - | ||||||
352 | return debug; | - | ||||||
353 | } | - | ||||||
354 | - | |||||||
355 | - | |||||||
356 | - | |||||||
Switch to Source code | Preprocessed file |