Changeset 160 for trunk/src/network/kernel/qnetworkinterface_os2.cpp
- Timestamp:
- Sep 5, 2009, 2:57:17 AM (16 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/network/kernel/qnetworkinterface_os2.cpp
r158 r160 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 4 ** Contact: Qt Software Information ([email protected]) 5 6 5 7 ** 6 8 ** This file is part of the QtNetwork module of the Qt Toolkit. … … 47 49 #ifndef QT_NO_NETWORKINTERFACE 48 50 49 #define IP_MULTICAST // make AIX happy and define IFF_MULTICAST50 51 51 #include <sys/types.h> 52 52 #include <sys/socket.h> 53 53 54 #ifdef Q_OS_SOLARIS 55 # include <sys/sockio.h> 56 #endif 54 #include <sys/sockio.h> 57 55 #include <net/if.h> 58 59 #ifndef QT_NO_GETIFADDRS60 # include <ifaddrs.h>61 #endif62 63 #ifdef QT_LINUXBASE64 # include <arpa/inet.h>65 # ifndef SIOCGIFBRDADDR66 # define SIOCGIFBRDADDR 0x891967 # endif68 #endif // QT_LINUXBASE69 56 70 57 #include <qplatformdefs.h> … … 80 67 if (sa->sa_family == AF_INET) 81 68 address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); 82 #ifndef QT_NO_IPV683 else if (sa->sa_family == AF_INET6)84 address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr);85 #endif86 69 return address; 87 70 … … 105 88 } 106 89 107 #ifdef QT_NO_GETIFADDRS108 // getifaddrs not available109 110 90 static const int STORAGEBUFFER_GROWTH = 256; 111 91 … … 113 93 { 114 94 QSet<QByteArray> result; 115 #ifdef QT_NO_IPV6IFNAME116 95 QByteArray storageBuffer; 117 96 struct ifconf interfaceList; … … 148 127 149 128 return result; 150 #else151 Q_UNUSED(socket);152 153 // use if_nameindex154 struct if_nameindex *interfaceList = ::if_nameindex();155 for (struct if_nameindex *ptr = interfaceList; ptr && ptr->if_name; ++ptr)156 result << ptr->if_name;157 158 if_freenameindex(interfaceList);159 return result;160 #endif161 129 } 162 130 … … 167 135 int ifindex = 0; 168 136 169 #ifndef QT_NO_IPV6IFNAME170 // Get the interface index171 ifindex = if_nametoindex(req.ifr_name);172 173 // find the interface data174 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();175 for ( ; if_it != interfaces.end(); ++if_it)176 if ((*if_it)->index == ifindex) {177 // existing interface178 iface = *if_it;179 break;180 }181 #else182 137 // Search by name 183 138 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); … … 188 143 break; 189 144 } 190 #endif191 145 192 146 if (!iface) { … … 274 228 } 275 229 276 #else277 // use getifaddrs278 279 // platform-specific defs:280 # ifdef Q_OS_LINUX281 QT_BEGIN_INCLUDE_NAMESPACE282 # include <features.h>283 QT_END_INCLUDE_NAMESPACE284 # endif285 286 # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1287 # include <netpacket/packet.h>288 289 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)290 {291 QList<QNetworkInterfacePrivate *> interfaces;292 293 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {294 if ( !ptr->ifa_addr )295 continue;296 297 // Get the interface index298 int ifindex = if_nametoindex(ptr->ifa_name);299 300 // on Linux we use AF_PACKET and sockaddr_ll to obtain hHwAddress301 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();302 for ( ; if_it != interfaces.end(); ++if_it)303 if ((*if_it)->index == ifindex) {304 // this one has been added already305 if ( ptr->ifa_addr->sa_family == AF_PACKET306 && (*if_it)->hardwareAddress.isEmpty()) {307 sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;308 (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);309 }310 break;311 }312 if ( if_it != interfaces.end() )313 continue;314 315 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;316 interfaces << iface;317 iface->index = ifindex;318 iface->name = QString::fromLatin1(ptr->ifa_name);319 iface->flags = convertFlags(ptr->ifa_flags);320 321 if ( ptr->ifa_addr->sa_family == AF_PACKET ) {322 sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;323 iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);324 }325 }326 327 return interfaces;328 }329 330 # elif defined(Q_OS_BSD4)331 QT_BEGIN_INCLUDE_NAMESPACE332 # include <net/if_dl.h>333 QT_END_INCLUDE_NAMESPACE334 335 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)336 {337 QList<QNetworkInterfacePrivate *> interfaces;338 339 // on NetBSD we use AF_LINK and sockaddr_dl340 // scan the list for that family341 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next)342 if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_LINK) {343 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;344 interfaces << iface;345 346 sockaddr_dl *sdl = (sockaddr_dl *)ptr->ifa_addr;347 iface->index = sdl->sdl_index;348 iface->name = QString::fromLatin1(ptr->ifa_name);349 iface->flags = convertFlags(ptr->ifa_flags);350 iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (uchar*)LLADDR(sdl));351 }352 353 return interfaces;354 }355 356 # else // Generic version357 358 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)359 {360 QList<QNetworkInterfacePrivate *> interfaces;361 362 // make sure there's one entry for each interface363 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {364 // Get the interface index365 int ifindex = if_nametoindex(ptr->ifa_name);366 367 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();368 for ( ; if_it != interfaces.end(); ++if_it)369 if ((*if_it)->index == ifindex)370 // this one has been added already371 break;372 373 if (if_it == interfaces.end()) {374 // none found, create375 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;376 interfaces << iface;377 378 iface->index = ifindex;379 iface->name = QString::fromLatin1(ptr->ifa_name);380 iface->flags = convertFlags(ptr->ifa_flags);381 }382 }383 384 return interfaces;385 }386 387 # endif388 389 390 static QList<QNetworkInterfacePrivate *> interfaceListing()391 {392 QList<QNetworkInterfacePrivate *> interfaces;393 394 int socket;395 if ((socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)396 return interfaces; // error397 398 ifaddrs *interfaceListing;399 if (getifaddrs(&interfaceListing) == -1) {400 // error401 ::close(socket);402 return interfaces;403 }404 405 interfaces = createInterfaces(interfaceListing);406 for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) {407 // Get the interface index408 int ifindex = if_nametoindex(ptr->ifa_name);409 QNetworkInterfacePrivate *iface = 0;410 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();411 for ( ; if_it != interfaces.end(); ++if_it)412 if ((*if_it)->index == ifindex) {413 // found this interface already414 iface = *if_it;415 break;416 }417 if (!iface) {418 // skip all non-IP interfaces419 continue;420 }421 422 QNetworkAddressEntry entry;423 entry.setIp(addressFromSockaddr(ptr->ifa_addr));424 if (entry.ip().isNull())425 // could not parse the address426 continue;427 428 entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask));429 if (iface->flags & QNetworkInterface::CanBroadcast)430 entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr));431 432 iface->addressEntries << entry;433 }434 435 freeifaddrs(interfaceListing);436 ::close(socket);437 return interfaces;438 }439 #endif440 441 230 QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan() 442 231 {
Note:
See TracChangeset
for help on using the changeset viewer.