Ignore:
Timestamp:
Sep 5, 2009, 2:57:17 AM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

network: Ported QHostInfo and QNetworkInterface to OS/2.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/network/kernel/qnetworkinterface_os2.cpp

    r158 r160  
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    44** Contact: Qt Software Information ([email protected])
     5
     6
    57**
    68** This file is part of the QtNetwork module of the Qt Toolkit.
     
    4749#ifndef QT_NO_NETWORKINTERFACE
    4850
    49 #define IP_MULTICAST    // make AIX happy and define IFF_MULTICAST
    50 
    5151#include <sys/types.h>
    5252#include <sys/socket.h>
    5353
    54 #ifdef Q_OS_SOLARIS
    55 # include <sys/sockio.h>
    56 #endif
     54#include <sys/sockio.h>
    5755#include <net/if.h>
    58 
    59 #ifndef QT_NO_GETIFADDRS
    60 # include <ifaddrs.h>
    61 #endif
    62 
    63 #ifdef QT_LINUXBASE
    64 #  include <arpa/inet.h>
    65 #  ifndef SIOCGIFBRDADDR
    66 #    define SIOCGIFBRDADDR 0x8919
    67 #  endif
    68 #endif // QT_LINUXBASE
    6956
    7057#include <qplatformdefs.h>
     
    8067    if (sa->sa_family == AF_INET)
    8168        address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));
    82 #ifndef QT_NO_IPV6
    83     else if (sa->sa_family == AF_INET6)
    84         address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr);
    85 #endif
    8669    return address;
    8770
     
    10588}
    10689
    107 #ifdef QT_NO_GETIFADDRS
    108 // getifaddrs not available
    109 
    11090static const int STORAGEBUFFER_GROWTH = 256;
    11191
     
    11393{
    11494    QSet<QByteArray> result;
    115 #ifdef QT_NO_IPV6IFNAME
    11695    QByteArray storageBuffer;
    11796    struct ifconf interfaceList;
     
    148127
    149128    return result;
    150 #else
    151     Q_UNUSED(socket);
    152 
    153     // use if_nameindex
    154     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 #endif
    161129}
    162130
     
    167135    int ifindex = 0;
    168136
    169 #ifndef QT_NO_IPV6IFNAME
    170     // Get the interface index
    171     ifindex = if_nametoindex(req.ifr_name);
    172 
    173     // find the interface data
    174     QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
    175     for ( ; if_it != interfaces.end(); ++if_it)
    176         if ((*if_it)->index == ifindex) {
    177             // existing interface
    178             iface = *if_it;
    179             break;
    180         }
    181 #else
    182137    // Search by name
    183138    QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
     
    188143            break;
    189144        }
    190 #endif
    191145
    192146    if (!iface) {
     
    274228}
    275229
    276 #else
    277 // use getifaddrs
    278 
    279 // platform-specific defs:
    280 # ifdef Q_OS_LINUX
    281 QT_BEGIN_INCLUDE_NAMESPACE
    282 #  include <features.h>
    283 QT_END_INCLUDE_NAMESPACE
    284 # endif
    285 
    286 # if defined(Q_OS_LINUX) &&  __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1
    287 #  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 index
    298         int ifindex = if_nametoindex(ptr->ifa_name);
    299 
    300         // on Linux we use AF_PACKET and sockaddr_ll to obtain hHwAddress
    301         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 already
    305                 if ( ptr->ifa_addr->sa_family == AF_PACKET
    306                         && (*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_NAMESPACE
    332 #  include <net/if_dl.h>
    333 QT_END_INCLUDE_NAMESPACE
    334 
    335 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
    336 {
    337     QList<QNetworkInterfacePrivate *> interfaces;
    338 
    339     // on NetBSD we use AF_LINK and sockaddr_dl
    340     // scan the list for that family
    341     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 version
    357 
    358 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
    359 {
    360     QList<QNetworkInterfacePrivate *> interfaces;
    361 
    362     // make sure there's one entry for each interface
    363     for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
    364         // Get the interface index
    365         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 already
    371                 break;
    372 
    373         if (if_it == interfaces.end()) {
    374             // none found, create
    375             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 # endif
    388 
    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;      // error
    397 
    398     ifaddrs *interfaceListing;
    399     if (getifaddrs(&interfaceListing) == -1) {
    400         // error
    401         ::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 index
    408         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 already
    414                 iface = *if_it;
    415                 break;
    416             }
    417         if (!iface) {
    418             // skip all non-IP interfaces
    419             continue;
    420         }
    421 
    422         QNetworkAddressEntry entry;
    423         entry.setIp(addressFromSockaddr(ptr->ifa_addr));
    424         if (entry.ip().isNull())
    425             // could not parse the address
    426             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 #endif
    440 
    441230QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan()
    442231{
Note: See TracChangeset for help on using the changeset viewer.