| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/kernel/qnetworkinterface_unix.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
| 5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 6 | ** | - | ||||||||||||
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
| 8 | ** | - | ||||||||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
| 10 | ** Commercial License Usage | - | ||||||||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 17 | ** | - | ||||||||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** GNU General Public License Usage | - | ||||||||||||
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 36 | ** | - | ||||||||||||
| 37 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 38 | ** | - | ||||||||||||
| 39 | ****************************************************************************/ | - | ||||||||||||
| 40 | - | |||||||||||||
| 41 | #include "qset.h" | - | ||||||||||||
| 42 | #include "qnetworkinterface.h" | - | ||||||||||||
| 43 | #include "qnetworkinterface_p.h" | - | ||||||||||||
| 44 | #include "qalgorithms.h" | - | ||||||||||||
| 45 | #include "private/qnet_unix_p.h" | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | #ifndef QT_NO_NETWORKINTERFACE | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | #define IP_MULTICAST // make AIX happy and define IFF_MULTICAST | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | #include <sys/types.h> | - | ||||||||||||
| 52 | #include <sys/socket.h> | - | ||||||||||||
| 53 | - | |||||||||||||
| 54 | #ifdef Q_OS_SOLARIS | - | ||||||||||||
| 55 | # include <sys/sockio.h> | - | ||||||||||||
| 56 | #endif | - | ||||||||||||
| 57 | #include <net/if.h> | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | #if defined(QT_LINUXBASE) | - | ||||||||||||
| 60 | # define QT_NO_GETIFADDRS | - | ||||||||||||
| 61 | #endif | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | #ifdef Q_OS_ANDROID | - | ||||||||||||
| 64 | // android lacks if_nameindex | - | ||||||||||||
| 65 | # define QT_NO_IPV6IFNAME | - | ||||||||||||
| 66 | #endif | - | ||||||||||||
| 67 | - | |||||||||||||
| 68 | #ifdef Q_OS_HAIKU | - | ||||||||||||
| 69 | # include <sys/sockio.h> | - | ||||||||||||
| 70 | # define IFF_RUNNING 0x0001 | - | ||||||||||||
| 71 | #endif | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | #ifndef QT_NO_GETIFADDRS | - | ||||||||||||
| 74 | # include <ifaddrs.h> | - | ||||||||||||
| 75 | #endif | - | ||||||||||||
| 76 | - | |||||||||||||
| 77 | #ifdef QT_LINUXBASE | - | ||||||||||||
| 78 | # include <arpa/inet.h> | - | ||||||||||||
| 79 | # ifndef SIOCGIFBRDADDR | - | ||||||||||||
| 80 | # define SIOCGIFBRDADDR 0x8919 | - | ||||||||||||
| 81 | # endif | - | ||||||||||||
| 82 | #endif // QT_LINUXBASE | - | ||||||||||||
| 83 | - | |||||||||||||
| 84 | #include <qplatformdefs.h> | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | static QHostAddress addressFromSockaddr(sockaddr *sa, int ifindex = 0, const QString &ifname = QString()) | - | ||||||||||||
| 89 | { | - | ||||||||||||
| 90 | QHostAddress address; | - | ||||||||||||
| 91 | if (!sa)
| 408-3468 | ||||||||||||
| 92 | return address; executed 408 times by 19 tests: return address;Executed by:
| 408 | ||||||||||||
| 93 | - | |||||||||||||
| 94 | if (sa->sa_family == AF_INET)
| 1632-1836 | ||||||||||||
| 95 | address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); executed 1632 times by 19 tests: address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));Executed by:
| 1632 | ||||||||||||
| 96 | else if (sa->sa_family == AF_INET6) {
| 612-1224 | ||||||||||||
| 97 | address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr); | - | ||||||||||||
| 98 | int scope = ((sockaddr_in6 *)sa)->sin6_scope_id; | - | ||||||||||||
| 99 | if (scope && scope == ifindex) {
| 0-816 | ||||||||||||
| 100 | // this is the most likely scenario: | - | ||||||||||||
| 101 | // a scope ID in a socket is that of the interface this address came from | - | ||||||||||||
| 102 | address.setScopeId(ifname); | - | ||||||||||||
| 103 | } else if (scope) { executed 408 times by 19 tests: end of blockExecuted by:
| 0-816 | ||||||||||||
| #ifndef QT_NO_IPV6IFNAME executed 408 times by 19 tests: end of blockExecuted by:
| ||||||||||||||
| char scopeid[IFNAMSIZ]; executed 408 times by 19 tests: end of blockExecuted by:
| ||||||||||||||
| if (::if_indextoname(scope, scopeid)) executed 408 times by 19 tests: end of blockExecuted by:
executed 408 times by 19 tests: end of blockExecuted by:
| ||||||||||||||
| 104 | address.setScopeId(QLatin1String(scopeid)); | - | ||||||||||||
| } else | ||||||||||||||
| #endifaddress.setScopeId(QStringQNetworkInterfaceManager::number(uintinterfaceNameFromIndex(scope)));)); | ||||||||||||||
| 105 | } never executed: end of block | 0 | ||||||||||||
| 106 | } executed 1224 times by 19 tests: end of blockExecuted by:
| 1224 | ||||||||||||
| 107 | return address; executed 3468 times by 19 tests: return address;Executed by:
| 3468 | ||||||||||||
| 108 | - | |||||||||||||
| 109 | } | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags) | - | ||||||||||||
| 112 | { | - | ||||||||||||
| 113 | QNetworkInterface::InterfaceFlags flags = 0; | - | ||||||||||||
| 114 | flags |= (rawFlags & IFF_UP) ? QNetworkInterface::IsUp : QNetworkInterface::InterfaceFlag(0); | - | ||||||||||||
| 115 | flags |= (rawFlags & IFF_RUNNING) ? QNetworkInterface::IsRunning : QNetworkInterface::InterfaceFlag(0); | - | ||||||||||||
| 116 | flags |= (rawFlags & IFF_BROADCAST) ? QNetworkInterface::CanBroadcast : QNetworkInterface::InterfaceFlag(0); | - | ||||||||||||
| 117 | flags |= (rawFlags & IFF_LOOPBACK) ? QNetworkInterface::IsLoopBack : QNetworkInterface::InterfaceFlag(0); | - | ||||||||||||
| 118 | #ifdef IFF_POINTOPOINT //cygwin doesn't define IFF_POINTOPOINT | - | ||||||||||||
| 119 | flags |= (rawFlags & IFF_POINTOPOINT) ? QNetworkInterface::IsPointToPoint : QNetworkInterface::InterfaceFlag(0); | - | ||||||||||||
| 120 | #endif | - | ||||||||||||
| 121 | - | |||||||||||||
| 122 | #ifdef IFF_MULTICAST | - | ||||||||||||
| 123 | flags |= (rawFlags & IFF_MULTICAST) ? QNetworkInterface::CanMulticast : QNetworkInterface::InterfaceFlag(0); | - | ||||||||||||
| 124 | #endif | - | ||||||||||||
| 125 | return flags; | - | ||||||||||||
| 126 | } | - | ||||||||||||
| 127 | - | |||||||||||||
| 128 | uint QNetworkInterfaceManager::interfaceIndexFromName(const QString &name) | - | ||||||||||||
| 129 | { | - | ||||||||||||
| 130 | #ifndef QT_NO_IPV6IFNAME | - | ||||||||||||
| 131 | return ::if_nametoindex(name.toLatin1()); executed 27 times by 3 tests: return ::if_nametoindex(name.toLatin1());Executed by:
| 27 | ||||||||||||
| 132 | #elif defined(SIOCGIFINDEX) | - | ||||||||||||
| 133 | struct ifreq req; | - | ||||||||||||
| 134 | int socket = qt_safe_socket(AF_INET, SOCK_STREAM, 0); | - | ||||||||||||
| 135 | if (socket < 0) | - | ||||||||||||
| 136 | return 0; | - | ||||||||||||
| 137 | - | |||||||||||||
| 138 | QByteArray name8bit = name.toLatin1(); | - | ||||||||||||
| 139 | memset(&req, 0, sizeof(ifreq)); | - | ||||||||||||
| 140 | memcpy(req.ifr_name, name8bit, qMin<int>(name8bit.length() + 1, sizeof(req.ifr_name) - 1)); | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | uint id = 0; | - | ||||||||||||
| 143 | if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0) | - | ||||||||||||
| 144 | id = req.ifr_ifindex; | - | ||||||||||||
| 145 | qt_safe_close(socket); | - | ||||||||||||
| 146 | return id; | - | ||||||||||||
| 147 | #else | - | ||||||||||||
| 148 | return 0; | - | ||||||||||||
| 149 | #endif | - | ||||||||||||
| 150 | } | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | QString QNetworkInterfaceManager::interfaceNameFromIndex(uint index) | - | ||||||||||||
| 153 | { | - | ||||||||||||
| 154 | #ifndef QT_NO_IPV6IFNAME | - | ||||||||||||
| 155 | char buf[IF_NAMESIZE]; | - | ||||||||||||
| 156 | if (::if_indextoname(index, buf))
| 0-33 | ||||||||||||
| 157 | return QString::fromLatin1(buf); executed 33 times by 3 tests: return QString::fromLatin1(buf);Executed by:
| 33 | ||||||||||||
| 158 | #elif defined(SIOCGIFNAME) | - | ||||||||||||
| 159 | struct ifreq req; | - | ||||||||||||
| 160 | int socket = qt_safe_socket(AF_INET, SOCK_STREAM, 0); | - | ||||||||||||
| 161 | if (socket >= 0) { | - | ||||||||||||
| 162 | memset(&req, 0, sizeof(ifreq)); | - | ||||||||||||
| 163 | req.ifr_ifindex = index; | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) { | - | ||||||||||||
| 166 | qt_safe_close(socket); | - | ||||||||||||
| 167 | return QString::fromLatin1(req.ifr_name); | - | ||||||||||||
| 168 | } | - | ||||||||||||
| 169 | qt_safe_close(socket); | - | ||||||||||||
| 170 | } | - | ||||||||||||
| 171 | #endif | - | ||||||||||||
| 172 | return QString::number(uint(index)); never executed: return QString::number(uint(index)); | 0 | ||||||||||||
| 173 | } | - | ||||||||||||
| 174 | - | |||||||||||||
| 175 | #ifdef QT_NO_GETIFADDRS | - | ||||||||||||
| 176 | // getifaddrs not available | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | static QSet<QByteArray> interfaceNames(int socket) | - | ||||||||||||
| 179 | { | - | ||||||||||||
| 180 | QSet<QByteArray> result; | - | ||||||||||||
| 181 | #ifdef QT_NO_IPV6IFNAME | - | ||||||||||||
| 182 | QByteArray storageBuffer; | - | ||||||||||||
| 183 | struct ifconf interfaceList; | - | ||||||||||||
| 184 | static const int STORAGEBUFFER_GROWTH = 256; | - | ||||||||||||
| 185 | - | |||||||||||||
| 186 | forever { | - | ||||||||||||
| 187 | // grow the storage buffer | - | ||||||||||||
| 188 | storageBuffer.resize(storageBuffer.size() + STORAGEBUFFER_GROWTH); | - | ||||||||||||
| 189 | interfaceList.ifc_buf = storageBuffer.data(); | - | ||||||||||||
| 190 | interfaceList.ifc_len = storageBuffer.size(); | - | ||||||||||||
| 191 | - | |||||||||||||
| 192 | // get the interface list | - | ||||||||||||
| 193 | if (qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) { | - | ||||||||||||
| 194 | if (int(interfaceList.ifc_len + sizeof(ifreq) + 64) < storageBuffer.size()) { | - | ||||||||||||
| 195 | // if the buffer was big enough, break | - | ||||||||||||
| 196 | storageBuffer.resize(interfaceList.ifc_len); | - | ||||||||||||
| 197 | break; | - | ||||||||||||
| 198 | } | - | ||||||||||||
| 199 | } else { | - | ||||||||||||
| 200 | // internal error | - | ||||||||||||
| 201 | return result; | - | ||||||||||||
| 202 | } | - | ||||||||||||
| 203 | if (storageBuffer.size() > 100000) { | - | ||||||||||||
| 204 | // out of space | - | ||||||||||||
| 205 | return result; | - | ||||||||||||
| 206 | } | - | ||||||||||||
| 207 | } | - | ||||||||||||
| 208 | - | |||||||||||||
| 209 | int interfaceCount = interfaceList.ifc_len / sizeof(ifreq); | - | ||||||||||||
| 210 | for (int i = 0; i < interfaceCount; ++i) { | - | ||||||||||||
| 211 | QByteArray name = QByteArray(interfaceList.ifc_req[i].ifr_name); | - | ||||||||||||
| 212 | if (!name.isEmpty()) | - | ||||||||||||
| 213 | result << name; | - | ||||||||||||
| 214 | } | - | ||||||||||||
| 215 | - | |||||||||||||
| 216 | return result; | - | ||||||||||||
| 217 | #else | - | ||||||||||||
| 218 | Q_UNUSED(socket); | - | ||||||||||||
| 219 | - | |||||||||||||
| 220 | // use if_nameindex | - | ||||||||||||
| 221 | struct if_nameindex *interfaceList = ::if_nameindex(); | - | ||||||||||||
| 222 | for (struct if_nameindex *ptr = interfaceList; ptr && ptr->if_name; ++ptr) | - | ||||||||||||
| 223 | result << ptr->if_name; | - | ||||||||||||
| 224 | - | |||||||||||||
| 225 | if_freenameindex(interfaceList); | - | ||||||||||||
| 226 | return result; | - | ||||||||||||
| 227 | #endif | - | ||||||||||||
| 228 | } | - | ||||||||||||
| 229 | - | |||||||||||||
| 230 | static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfacePrivate *> &interfaces, | - | ||||||||||||
| 231 | struct ifreq &req) | - | ||||||||||||
| 232 | { | - | ||||||||||||
| 233 | QNetworkInterfacePrivate *iface = 0; | - | ||||||||||||
| 234 | int ifindex = 0; | - | ||||||||||||
| 235 | - | |||||||||||||
| 236 | #if !defined(QT_NO_IPV6IFNAME) || defined(SIOCGIFINDEX) | - | ||||||||||||
| 237 | // Get the interface index | - | ||||||||||||
| 238 | # ifdef SIOCGIFINDEX | - | ||||||||||||
| 239 | if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0) | - | ||||||||||||
| 240 | # if defined(Q_OS_HAIKU) | - | ||||||||||||
| 241 | ifindex = req.ifr_index; | - | ||||||||||||
| 242 | # else | - | ||||||||||||
| 243 | ifindex = req.ifr_ifindex; | - | ||||||||||||
| 244 | # endif | - | ||||||||||||
| 245 | # else | - | ||||||||||||
| 246 | ifindex = if_nametoindex(req.ifr_name); | - | ||||||||||||
| 247 | # endif | - | ||||||||||||
| 248 | - | |||||||||||||
| 249 | // find the interface data | - | ||||||||||||
| 250 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - | ||||||||||||
| 251 | for ( ; if_it != interfaces.end(); ++if_it) | - | ||||||||||||
| 252 | if ((*if_it)->index == ifindex) { | - | ||||||||||||
| 253 | // existing interface | - | ||||||||||||
| 254 | iface = *if_it; | - | ||||||||||||
| 255 | break; | - | ||||||||||||
| 256 | } | - | ||||||||||||
| 257 | #else | - | ||||||||||||
| 258 | // Search by name | - | ||||||||||||
| 259 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - | ||||||||||||
| 260 | for ( ; if_it != interfaces.end(); ++if_it) | - | ||||||||||||
| 261 | if ((*if_it)->name == QLatin1String(req.ifr_name)) { | - | ||||||||||||
| 262 | // existing interface | - | ||||||||||||
| 263 | iface = *if_it; | - | ||||||||||||
| 264 | break; | - | ||||||||||||
| 265 | } | - | ||||||||||||
| 266 | #endif | - | ||||||||||||
| 267 | - | |||||||||||||
| 268 | if (!iface) { | - | ||||||||||||
| 269 | // new interface, create data: | - | ||||||||||||
| 270 | iface = new QNetworkInterfacePrivate; | - | ||||||||||||
| 271 | iface->index = ifindex; | - | ||||||||||||
| 272 | interfaces << iface; | - | ||||||||||||
| 273 | } | - | ||||||||||||
| 274 | - | |||||||||||||
| 275 | return iface; | - | ||||||||||||
| 276 | } | - | ||||||||||||
| 277 | - | |||||||||||||
| 278 | static QList<QNetworkInterfacePrivate *> interfaceListing() | - | ||||||||||||
| 279 | { | - | ||||||||||||
| 280 | QList<QNetworkInterfacePrivate *> interfaces; | - | ||||||||||||
| 281 | - | |||||||||||||
| 282 | int socket; | - | ||||||||||||
| 283 | if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) | - | ||||||||||||
| 284 | return interfaces; // error | - | ||||||||||||
| 285 | - | |||||||||||||
| 286 | QSet<QByteArray> names = interfaceNames(socket); | - | ||||||||||||
| 287 | QSet<QByteArray>::ConstIterator it = names.constBegin(); | - | ||||||||||||
| 288 | for ( ; it != names.constEnd(); ++it) { | - | ||||||||||||
| 289 | ifreq req; | - | ||||||||||||
| 290 | memset(&req, 0, sizeof(ifreq)); | - | ||||||||||||
| 291 | memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1, sizeof(req.ifr_name) - 1)); | - | ||||||||||||
| 292 | - | |||||||||||||
| 293 | QNetworkInterfacePrivate *iface = findInterface(socket, interfaces, req); | - | ||||||||||||
| 294 | - | |||||||||||||
| 295 | #ifdef SIOCGIFNAME | - | ||||||||||||
| 296 | // Get the canonical name | - | ||||||||||||
| 297 | QByteArray oldName = req.ifr_name; | - | ||||||||||||
| 298 | if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) { | - | ||||||||||||
| 299 | iface->name = QString::fromLatin1(req.ifr_name); | - | ||||||||||||
| 300 | - | |||||||||||||
| 301 | // reset the name: | - | ||||||||||||
| 302 | memcpy(req.ifr_name, oldName, qMin<int>(oldName.length() + 1, sizeof(req.ifr_name) - 1)); | - | ||||||||||||
| 303 | } else | - | ||||||||||||
| 304 | #endif | - | ||||||||||||
| 305 | { | - | ||||||||||||
| 306 | // use this name anyways | - | ||||||||||||
| 307 | iface->name = QString::fromLatin1(req.ifr_name); | - | ||||||||||||
| 308 | } | - | ||||||||||||
| 309 | - | |||||||||||||
| 310 | // Get interface flags | - | ||||||||||||
| 311 | if (qt_safe_ioctl(socket, SIOCGIFFLAGS, &req) >= 0) { | - | ||||||||||||
| 312 | iface->flags = convertFlags(req.ifr_flags); | - | ||||||||||||
| 313 | } | - | ||||||||||||
| 314 | - | |||||||||||||
| 315 | #ifdef SIOCGIFHWADDR | - | ||||||||||||
| 316 | // Get the HW address | - | ||||||||||||
| 317 | if (qt_safe_ioctl(socket, SIOCGIFHWADDR, &req) >= 0) { | - | ||||||||||||
| 318 | uchar *addr = (uchar *)req.ifr_addr.sa_data; | - | ||||||||||||
| 319 | iface->hardwareAddress = iface->makeHwAddress(6, addr); | - | ||||||||||||
| 320 | } | - | ||||||||||||
| 321 | #endif | - | ||||||||||||
| 322 | - | |||||||||||||
| 323 | // Get the address of the interface | - | ||||||||||||
| 324 | QNetworkAddressEntry entry; | - | ||||||||||||
| 325 | if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) { | - | ||||||||||||
| 326 | sockaddr *sa = &req.ifr_addr; | - | ||||||||||||
| 327 | entry.setIp(addressFromSockaddr(sa)); | - | ||||||||||||
| 328 | - | |||||||||||||
| 329 | // Get the interface broadcast address | - | ||||||||||||
| 330 | if (iface->flags & QNetworkInterface::CanBroadcast) { | - | ||||||||||||
| 331 | if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) { | - | ||||||||||||
| 332 | sockaddr *sa = &req.ifr_addr; | - | ||||||||||||
| 333 | if (sa->sa_family == AF_INET) | - | ||||||||||||
| 334 | entry.setBroadcast(addressFromSockaddr(sa)); | - | ||||||||||||
| 335 | } | - | ||||||||||||
| 336 | } | - | ||||||||||||
| 337 | - | |||||||||||||
| 338 | // Get the interface netmask | - | ||||||||||||
| 339 | if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) { | - | ||||||||||||
| 340 | sockaddr *sa = &req.ifr_addr; | - | ||||||||||||
| 341 | entry.setNetmask(addressFromSockaddr(sa)); | - | ||||||||||||
| 342 | } | - | ||||||||||||
| 343 | - | |||||||||||||
| 344 | iface->addressEntries << entry; | - | ||||||||||||
| 345 | } | - | ||||||||||||
| 346 | } | - | ||||||||||||
| 347 | - | |||||||||||||
| 348 | ::close(socket); | - | ||||||||||||
| 349 | return interfaces; | - | ||||||||||||
| 350 | } | - | ||||||||||||
| 351 | - | |||||||||||||
| 352 | #else | - | ||||||||||||
| 353 | // use getifaddrs | - | ||||||||||||
| 354 | - | |||||||||||||
| 355 | // platform-specific defs: | - | ||||||||||||
| 356 | # ifdef Q_OS_LINUX | - | ||||||||||||
| 357 | QT_BEGIN_INCLUDE_NAMESPACE | - | ||||||||||||
| 358 | # include <features.h> | - | ||||||||||||
| 359 | QT_END_INCLUDE_NAMESPACE | - | ||||||||||||
| 360 | # endif | - | ||||||||||||
| 361 | - | |||||||||||||
| 362 | # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1 && !defined(QT_LINUXBASE) | - | ||||||||||||
| 363 | # include <netpacket/packet.h> | - | ||||||||||||
| 364 | - | |||||||||||||
| 365 | static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList) | - | ||||||||||||
| 366 | { | - | ||||||||||||
| 367 | QList<QNetworkInterfacePrivate *> interfaces; | - | ||||||||||||
| 368 | QSet<QString> seenInterfaces; | - | ||||||||||||
| 369 | QVarLengthArray<int, 16> seenIndexes; // faster than QSet<int> | - | ||||||||||||
| 370 | - | |||||||||||||
| 371 | // On Linux, glibc, uClibc and MUSL obtain the address listing via two | - | ||||||||||||
| 372 | // netlink calls: first an RTM_GETLINK to obtain the interface listing, | - | ||||||||||||
| 373 | // then one RTM_GETADDR to get all the addresses (uClibc implementation is | - | ||||||||||||
| 374 | // copied from glibc; Bionic currently doesn't support getifaddrs). They | - | ||||||||||||
| 375 | // synthesize AF_PACKET addresses from the RTM_GETLINK responses, which | - | ||||||||||||
| 376 | // means by construction they currently show up first in the interface | - | ||||||||||||
| 377 | // listing. | - | ||||||||||||
| 378 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) { | - | ||||||||||||
| 379 | if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_PACKET) { | - | ||||||||||||
| 380 | sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr; | - | ||||||||||||
| 381 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - | ||||||||||||
| 382 | interfaces << iface; | - | ||||||||||||
| 383 | iface->index = sll->sll_ifindex; | - | ||||||||||||
| 384 | iface->name = QString::fromLatin1(ptr->ifa_name); | - | ||||||||||||
| 385 | iface->flags = convertFlags(ptr->ifa_flags); | - | ||||||||||||
| 386 | iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr); | - | ||||||||||||
| 387 | - | |||||||||||||
| 388 | Q_ASSERT(!seenIndexes.contains(iface->index)); | - | ||||||||||||
| 389 | seenIndexes.append(iface->index); | - | ||||||||||||
| 390 | seenInterfaces.insert(iface->name); | - | ||||||||||||
| 391 | } | - | ||||||||||||
| 392 | } | - | ||||||||||||
| 393 | - | |||||||||||||
| 394 | // see if we missed anything: | - | ||||||||||||
| 395 | // - virtual interfaces with no HW address have no AF_PACKET | - | ||||||||||||
| 396 | // - interface labels have no AF_PACKET, but shouldn't be shown as a new interface | - | ||||||||||||
| 397 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) { | - | ||||||||||||
| 398 | if (!ptr->ifa_addr || ptr->ifa_addr->sa_family != AF_PACKET) { | - | ||||||||||||
| 399 | QString name = QString::fromLatin1(ptr->ifa_name); | - | ||||||||||||
| 400 | if (seenInterfaces.contains(name)) | - | ||||||||||||
| 401 | continue; | - | ||||||||||||
| 402 | - | |||||||||||||
| 403 | int ifindex = if_nametoindex(ptr->ifa_name); | - | ||||||||||||
| 404 | if (seenIndexes.contains(ifindex)) | - | ||||||||||||
| 405 | continue; | - | ||||||||||||
| 406 | - | |||||||||||||
| 407 | seenInterfaces.insert(name); | - | ||||||||||||
| 408 | seenIndexes.append(ifindex); | - | ||||||||||||
| 409 | - | |||||||||||||
| 410 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - | ||||||||||||
| 411 | interfaces << iface; | - | ||||||||||||
| 412 | iface->name = name; | - | ||||||||||||
| 413 | iface->flags = convertFlags(ptr->ifa_flags); | - | ||||||||||||
| 414 | iface->index = ifindex; | - | ||||||||||||
| 415 | } | - | ||||||||||||
| 416 | } | - | ||||||||||||
| 417 | - | |||||||||||||
| 418 | return interfaces; | - | ||||||||||||
| 419 | } | - | ||||||||||||
| 420 | - | |||||||||||||
| 421 | # elif defined(Q_OS_BSD4) | - | ||||||||||||
| 422 | QT_BEGIN_INCLUDE_NAMESPACE | - | ||||||||||||
| 423 | # include <net/if_dl.h> | - | ||||||||||||
| 424 | QT_END_INCLUDE_NAMESPACE | - | ||||||||||||
| 425 | - | |||||||||||||
| 426 | static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList) | - | ||||||||||||
| 427 | { | - | ||||||||||||
| 428 | QList<QNetworkInterfacePrivate *> interfaces; | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | // on NetBSD we use AF_LINK and sockaddr_dl | - | ||||||||||||
| 431 | // scan the list for that family | - | ||||||||||||
| 432 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) | - | ||||||||||||
| 433 | if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_LINK) { | - | ||||||||||||
| 434 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - | ||||||||||||
| 435 | interfaces << iface; | - | ||||||||||||
| 436 | - | |||||||||||||
| 437 | sockaddr_dl *sdl = (sockaddr_dl *)ptr->ifa_addr; | - | ||||||||||||
| 438 | iface->index = sdl->sdl_index; | - | ||||||||||||
| 439 | iface->name = QString::fromLatin1(ptr->ifa_name); | - | ||||||||||||
| 440 | iface->flags = convertFlags(ptr->ifa_flags); | - | ||||||||||||
| 441 | iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (uchar*)LLADDR(sdl)); | - | ||||||||||||
| 442 | } | - | ||||||||||||
| 443 | - | |||||||||||||
| 444 | return interfaces; | - | ||||||||||||
| 445 | } | - | ||||||||||||
| 446 | - | |||||||||||||
| 447 | # else // Generic version | - | ||||||||||||
| 448 | - | |||||||||||||
| 449 | static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList) | - | ||||||||||||
| 450 | { | - | ||||||||||||
| 451 | QList<QNetworkInterfacePrivate *> interfaces; | - | ||||||||||||
| 452 | - | |||||||||||||
| 453 | // make sure there's one entry for each interface | - | ||||||||||||
| 454 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) { | - | ||||||||||||
| 455 | // Get the interface index | - | ||||||||||||
| 456 | int ifindex = if_nametoindex(ptr->ifa_name); | - | ||||||||||||
| 457 | - | |||||||||||||
| 458 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - | ||||||||||||
| 459 | for ( ; if_it != interfaces.end(); ++if_it) | - | ||||||||||||
| 460 | if ((*if_it)->index == ifindex) | - | ||||||||||||
| 461 | // this one has been added already | - | ||||||||||||
| 462 | break; | - | ||||||||||||
| 463 | - | |||||||||||||
| 464 | if (if_it == interfaces.end()) { | - | ||||||||||||
| 465 | // none found, create | - | ||||||||||||
| 466 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - | ||||||||||||
| 467 | interfaces << iface; | - | ||||||||||||
| 468 | - | |||||||||||||
| 469 | iface->index = ifindex; | - | ||||||||||||
| 470 | iface->name = QString::fromLatin1(ptr->ifa_name); | - | ||||||||||||
| 471 | iface->flags = convertFlags(ptr->ifa_flags); | - | ||||||||||||
| 472 | } | - | ||||||||||||
| 473 | } | - | ||||||||||||
| 474 | - | |||||||||||||
| 475 | return interfaces; | - | ||||||||||||
| 476 | } | - | ||||||||||||
| 477 | - | |||||||||||||
| 478 | # endif | - | ||||||||||||
| 479 | - | |||||||||||||
| 480 | - | |||||||||||||
| 481 | static QList<QNetworkInterfacePrivate *> interfaceListing() | - | ||||||||||||
| 482 | { | - | ||||||||||||
| 483 | QList<QNetworkInterfacePrivate *> interfaces; | - | ||||||||||||
| 484 | - | |||||||||||||
| 485 | ifaddrs *interfaceListing; | - | ||||||||||||
| 486 | if (getifaddrs(&interfaceListing) == -1) { | - | ||||||||||||
| 487 | // error | - | ||||||||||||
| 488 | return interfaces; | - | ||||||||||||
| 489 | } | - | ||||||||||||
| 490 | - | |||||||||||||
| 491 | interfaces = createInterfaces(interfaceListing); | - | ||||||||||||
| 492 | for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) { | - | ||||||||||||
| 493 | // Find the interface | - | ||||||||||||
| 494 | QString name = QString::fromLatin1(ptr->ifa_name); | - | ||||||||||||
| 495 | QNetworkInterfacePrivate *iface = 0; | - | ||||||||||||
| 496 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - | ||||||||||||
| 497 | for ( ; if_it != interfaces.end(); ++if_it) | - | ||||||||||||
| 498 | if ((*if_it)->name == name) { | - | ||||||||||||
| 499 | // found this interface already | - | ||||||||||||
| 500 | iface = *if_it; | - | ||||||||||||
| 501 | break; | - | ||||||||||||
| 502 | } | - | ||||||||||||
| 503 | - | |||||||||||||
| 504 | if (!iface) { | - | ||||||||||||
| 505 | // it may be an interface label, search by interface index | - | ||||||||||||
| 506 | int ifindex = if_nametoindex(ptr->ifa_name); | - | ||||||||||||
| 507 | for (if_it = interfaces.begin(); if_it != interfaces.end(); ++if_it) | - | ||||||||||||
| 508 | if ((*if_it)->index == ifindex) { | - | ||||||||||||
| 509 | // found this interface already | - | ||||||||||||
| 510 | iface = *if_it; | - | ||||||||||||
| 511 | break; | - | ||||||||||||
| 512 | } | - | ||||||||||||
| 513 | } | - | ||||||||||||
| 514 | - | |||||||||||||
| 515 | if (!iface) { | - | ||||||||||||
| 516 | // skip all non-IP interfaces | - | ||||||||||||
| 517 | continue; | - | ||||||||||||
| 518 | } | - | ||||||||||||
| 519 | - | |||||||||||||
| 520 | QNetworkAddressEntry entry; | - | ||||||||||||
| 521 | entry.setIp(addressFromSockaddr(ptr->ifa_addr, iface->index, iface->name)); | - | ||||||||||||
| 522 | if (entry.ip().isNull()) | - | ||||||||||||
| 523 | // could not parse the address | - | ||||||||||||
| 524 | continue; | - | ||||||||||||
| 525 | - | |||||||||||||
| 526 | entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask, iface->index, iface->name)); | - | ||||||||||||
| 527 | if (iface->flags & QNetworkInterface::CanBroadcast) | - | ||||||||||||
| 528 | entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr, iface->index, iface->name)); | - | ||||||||||||
| 529 | - | |||||||||||||
| 530 | iface->addressEntries << entry; | - | ||||||||||||
| 531 | } | - | ||||||||||||
| 532 | - | |||||||||||||
| 533 | freeifaddrs(interfaceListing); | - | ||||||||||||
| 534 | return interfaces; | - | ||||||||||||
| 535 | } | - | ||||||||||||
| 536 | #endif | - | ||||||||||||
| 537 | - | |||||||||||||
| 538 | QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan() | - | ||||||||||||
| 539 | { | - | ||||||||||||
| 540 | return interfaceListing(); | - | ||||||||||||
| 541 | } | - | ||||||||||||
| 542 | - | |||||||||||||
| 543 | QT_END_NAMESPACE | - | ||||||||||||
| 544 | - | |||||||||||||
| 545 | #endif // QT_NO_NETWORKINTERFACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |