util/src/network/kernel/qhostinfo_win.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #if defined Q_CC_MSVC && _MSC_VER <=1300
       
    43 //VC.net 2002 support for templates doesn't match some PSDK requirements
       
    44 #define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
       
    45 #endif
       
    46 
       
    47 #include <winsock2.h>
       
    48 
       
    49 #include "qhostinfo_p.h"
       
    50 #include "private/qnativesocketengine_p.h"
       
    51 #include <ws2tcpip.h>
       
    52 #include <qlibrary.h>
       
    53 #include <qtimer.h>
       
    54 #include <qmutex.h>
       
    55 #include <qurl.h>
       
    56 #include <private/qmutexpool_p.h>
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 //#define QHOSTINFO_DEBUG
       
    61 
       
    62 // Older SDKs do not include the addrinfo struct declaration, so we
       
    63 // include a copy of it here.
       
    64 struct qt_addrinfo
       
    65 {
       
    66     int ai_flags;
       
    67     int ai_family;
       
    68     int ai_socktype;
       
    69     int ai_protocol;
       
    70     size_t ai_addrlen;
       
    71     char *ai_canonname;
       
    72     sockaddr *ai_addr;
       
    73     qt_addrinfo *ai_next;
       
    74 };
       
    75 
       
    76 //###
       
    77 #define QT_SOCKLEN_T int
       
    78 #ifndef NI_MAXHOST // already defined to 1025 in ws2tcpip.h?
       
    79 #define NI_MAXHOST 1024
       
    80 #endif
       
    81 
       
    82 typedef int (__stdcall *getnameinfoProto)(const sockaddr *, QT_SOCKLEN_T, const char *, DWORD, const char *, DWORD, int);
       
    83 typedef int (__stdcall *getaddrinfoProto)(const char *, const char *, const qt_addrinfo *, qt_addrinfo **);
       
    84 typedef int (__stdcall *freeaddrinfoProto)(qt_addrinfo *);
       
    85 static getnameinfoProto local_getnameinfo = 0;
       
    86 static getaddrinfoProto local_getaddrinfo = 0;
       
    87 static freeaddrinfoProto local_freeaddrinfo = 0;
       
    88 
       
    89 static void resolveLibrary()
       
    90 {
       
    91     // Attempt to resolve getaddrinfo(); without it we'll have to fall
       
    92     // back to gethostbyname(), which has no IPv6 support.
       
    93 #if !defined(Q_OS_WINCE)
       
    94     local_getaddrinfo = (getaddrinfoProto) QLibrary::resolve(QLatin1String("ws2_32.dll"), "getaddrinfo");
       
    95     local_freeaddrinfo = (freeaddrinfoProto) QLibrary::resolve(QLatin1String("ws2_32.dll"), "freeaddrinfo");
       
    96     local_getnameinfo = (getnameinfoProto) QLibrary::resolve(QLatin1String("ws2_32.dll"), "getnameinfo");
       
    97 #else
       
    98     local_getaddrinfo = (getaddrinfoProto) QLibrary::resolve(QLatin1String("ws2.dll"), "getaddrinfo");
       
    99     local_freeaddrinfo = (freeaddrinfoProto) QLibrary::resolve(QLatin1String("ws2.dll"), "freeaddrinfo");
       
   100     local_getnameinfo = (getnameinfoProto) QLibrary::resolve(QLatin1String("ws2.dll"), "getnameinfo");
       
   101 #endif
       
   102 }
       
   103 
       
   104 #if defined(Q_OS_WINCE)
       
   105 #include <qmutex.h>
       
   106 QMutex qPrivCEMutex;
       
   107 #endif
       
   108 
       
   109 QHostInfo QHostInfoAgent::fromName(const QString &hostName)
       
   110 {
       
   111 #if defined(Q_OS_WINCE)
       
   112     QMutexLocker locker(&qPrivCEMutex);
       
   113 #endif
       
   114     QWindowsSockInit winSock;
       
   115 
       
   116     // Load res_init on demand.
       
   117     static volatile bool triedResolve = false;
       
   118     if (!triedResolve) {
       
   119 #ifndef QT_NO_THREAD
       
   120         QMutexLocker locker(QMutexPool::globalInstanceGet(&local_getaddrinfo));
       
   121 #endif
       
   122         if (!triedResolve) {
       
   123             resolveLibrary();
       
   124             triedResolve = true;
       
   125         }
       
   126     }
       
   127 
       
   128     QHostInfo results;
       
   129 
       
   130 #if defined(QHOSTINFO_DEBUG)
       
   131     qDebug("QHostInfoAgent::fromName(%p): looking up \"%s\" (IPv6 support is %s)",
       
   132            this, hostName.toLatin1().constData(),
       
   133            (local_getaddrinfo && local_freeaddrinfo) ? "enabled" : "disabled");
       
   134 #endif
       
   135 
       
   136     QHostAddress address;
       
   137     if (address.setAddress(hostName)) {
       
   138         // Reverse lookup
       
   139         if (local_getnameinfo) {
       
   140             sockaddr_in sa4;
       
   141             qt_sockaddr_in6 sa6;
       
   142             sockaddr *sa;
       
   143             QT_SOCKLEN_T saSize;
       
   144             if (address.protocol() == QAbstractSocket::IPv4Protocol) {
       
   145                 sa = (sockaddr *)&sa4;
       
   146                 saSize = sizeof(sa4);
       
   147                 memset(&sa4, 0, sizeof(sa4));
       
   148                 sa4.sin_family = AF_INET;
       
   149                 sa4.sin_addr.s_addr = htonl(address.toIPv4Address());
       
   150             } else {
       
   151                 sa = (sockaddr *)&sa6;
       
   152                 saSize = sizeof(sa6);
       
   153                 memset(&sa6, 0, sizeof(sa6));
       
   154                 sa6.sin6_family = AF_INET6;
       
   155                 memcpy(sa6.sin6_addr.qt_s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.qt_s6_addr));
       
   156             }
       
   157 
       
   158             char hbuf[NI_MAXHOST];
       
   159             if (local_getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0)
       
   160                 results.setHostName(QString::fromLatin1(hbuf));
       
   161         } else {
       
   162             unsigned long addr = inet_addr(hostName.toLatin1().constData());
       
   163             struct hostent *ent = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
       
   164             if (ent)
       
   165                 results.setHostName(QString::fromLatin1(ent->h_name));
       
   166         }
       
   167 
       
   168         if (results.hostName().isEmpty())
       
   169             results.setHostName(address.toString());
       
   170         results.setAddresses(QList<QHostAddress>() << address);
       
   171         return results;
       
   172     }
       
   173 
       
   174     // IDN support
       
   175     QByteArray aceHostname = QUrl::toAce(hostName);
       
   176     results.setHostName(hostName);
       
   177     if (aceHostname.isEmpty()) {
       
   178         results.setError(QHostInfo::HostNotFound);
       
   179         results.setErrorString(hostName.isEmpty() ? tr("No host name given") : tr("Invalid hostname"));
       
   180         return results;
       
   181     }
       
   182 
       
   183     if (local_getaddrinfo && local_freeaddrinfo) {
       
   184         // Call getaddrinfo, and place all IPv4 addresses at the start
       
   185         // and the IPv6 addresses at the end of the address list in
       
   186         // results.
       
   187         qt_addrinfo *res;
       
   188         int err = local_getaddrinfo(aceHostname.constData(), 0, 0, &res);
       
   189         if (err == 0) {
       
   190             QList<QHostAddress> addresses;
       
   191             for (qt_addrinfo *p = res; p != 0; p = p->ai_next) {
       
   192                 switch (p->ai_family) {
       
   193                 case AF_INET: {
       
   194                     QHostAddress addr;
       
   195                     addr.setAddress(ntohl(((sockaddr_in *) p->ai_addr)->sin_addr.s_addr));
       
   196                     if (!addresses.contains(addr))
       
   197                         addresses.append(addr);
       
   198                 }
       
   199                     break;
       
   200                 case AF_INET6: {
       
   201                     QHostAddress addr;
       
   202                     addr.setAddress(((qt_sockaddr_in6 *) p->ai_addr)->sin6_addr.qt_s6_addr);
       
   203                     if (!addresses.contains(addr))
       
   204                         addresses.append(addr);
       
   205                 }
       
   206                     break;
       
   207                 default:
       
   208                     results.setError(QHostInfo::UnknownError);
       
   209                     results.setErrorString(tr("Unknown address type"));
       
   210                 }
       
   211             }
       
   212             results.setAddresses(addresses);
       
   213             local_freeaddrinfo(res);
       
   214         } else if (WSAGetLastError() == WSAHOST_NOT_FOUND || WSAGetLastError() == WSANO_DATA) {
       
   215             results.setError(QHostInfo::HostNotFound);
       
   216             results.setErrorString(tr("Host not found"));
       
   217         } else {
       
   218             results.setError(QHostInfo::UnknownError);
       
   219             results.setErrorString(tr("Unknown error"));
       
   220         }
       
   221     } else {
       
   222         // Fall back to gethostbyname, which only supports IPv4.
       
   223         hostent *ent = gethostbyname(aceHostname.constData());
       
   224         if (ent) {
       
   225             char **p;
       
   226             QList<QHostAddress> addresses;
       
   227             switch (ent->h_addrtype) {
       
   228             case AF_INET:
       
   229                 for (p = ent->h_addr_list; *p != 0; p++) {
       
   230                     long *ip4Addr = (long *) *p;
       
   231                     QHostAddress temp;
       
   232                     temp.setAddress(ntohl(*ip4Addr));
       
   233                     addresses << temp;
       
   234                 }
       
   235                 break;
       
   236             default:
       
   237                 results.setError(QHostInfo::UnknownError);
       
   238                 results.setErrorString(tr("Unknown address type"));
       
   239                 break;
       
   240             }
       
   241             results.setAddresses(addresses);
       
   242         } else if (WSAGetLastError() == 11001) {
       
   243             results.setErrorString(tr("Host not found"));
       
   244             results.setError(QHostInfo::HostNotFound);
       
   245         } else {
       
   246             results.setErrorString(tr("Unknown error"));
       
   247             results.setError(QHostInfo::UnknownError);
       
   248         }
       
   249     }
       
   250 
       
   251 #if defined(QHOSTINFO_DEBUG)
       
   252     if (results.error() != QHostInfo::NoError) {
       
   253         qDebug("QHostInfoAgent::run(%p): error (%s)",
       
   254                this, results.errorString().toLatin1().constData());
       
   255     } else {
       
   256         QString tmp;
       
   257         QList<QHostAddress> addresses = results.addresses();
       
   258         for (int i = 0; i < addresses.count(); ++i) {
       
   259             if (i != 0) tmp += ", ";
       
   260             tmp += addresses.at(i).toString();
       
   261         }
       
   262         qDebug("QHostInfoAgent::run(%p): found %i entries: {%s}",
       
   263                this, addresses.count(), tmp.toLatin1().constData());
       
   264     }
       
   265 #endif
       
   266     return results;
       
   267 }
       
   268 
       
   269 QString QHostInfo::localHostName()
       
   270 {
       
   271     QWindowsSockInit winSock;
       
   272 
       
   273     char hostName[512];
       
   274     if (gethostname(hostName, sizeof(hostName)) == -1)
       
   275         return QString();
       
   276     hostName[sizeof(hostName) - 1] = '\0';
       
   277     return QString::fromLocal8Bit(hostName);
       
   278 }
       
   279 
       
   280 // QString QHostInfo::localDomainName() defined in qnetworkinterface_win.cpp
       
   281 
       
   282 QT_END_NAMESPACE