~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/network/kernel/qhostinfo_unix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/****************************************************************************
2
2
**
3
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** All rights reserved.
4
5
** Contact: Nokia Corporation (qt-info@nokia.com)
5
6
**
6
7
** This file is part of the QtNetwork module of the Qt Toolkit.
7
8
**
8
9
** $QT_BEGIN_LICENSE:LGPL$
9
 
** Commercial Usage
10
 
** Licensees holding valid Qt Commercial licenses may use this file in
11
 
** accordance with the Qt Commercial License Agreement provided with the
12
 
** Software or, alternatively, in accordance with the terms contained in
13
 
** a written agreement between you and Nokia.
 
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.
14
15
**
15
16
** GNU Lesser General Public License Usage
16
17
** Alternatively, this file may be used under the terms of the GNU Lesser
20
21
** ensure the GNU Lesser General Public License version 2.1 requirements
21
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22
23
**
23
 
** In addition, as a special exception, Nokia gives you certain
24
 
** additional rights. These rights are described in the Nokia Qt LGPL
25
 
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26
 
** package.
27
 
**
28
 
** GNU General Public License Usage
29
 
** Alternatively, this file may be used under the terms of the GNU
30
 
** General Public License version 3.0 as published by the Free Software
31
 
** Foundation and appearing in the file LICENSE.GPL included in the
32
 
** packaging of this file.  Please review the following information to
33
 
** ensure the GNU General Public License version 3.0 requirements will be
34
 
** met: http://www.gnu.org/copyleft/gpl.html.
35
 
**
36
 
** If you are unsure which license is appropriate for your use, please
37
 
** contact the sales department at http://www.qtsoftware.com/contact.
 
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
38
** $QT_END_LICENSE$
39
39
**
40
40
****************************************************************************/
41
41
 
42
42
//#define QHOSTINFO_DEBUG
43
43
 
44
 
static const int RESOLVER_TIMEOUT = 2000;
45
 
 
46
44
#include "qplatformdefs.h"
47
45
 
48
46
#include "qhostinfo_p.h"
52
50
#include <qurl.h>
53
51
#include <qfile.h>
54
52
#include <private/qmutexpool_p.h>
 
53
#include <private/qnet_unix_p.h>
55
54
 
56
 
extern "C" {
57
55
#include <sys/types.h>
58
56
#include <netdb.h>
59
57
#include <arpa/inet.h>
60
 
#include <resolv.h>
61
 
}
 
58
#if defined(Q_OS_VXWORKS)
 
59
#  include <hostLib.h>
 
60
#else
 
61
#  include <resolv.h>
 
62
#endif
62
63
 
63
64
#if defined (QT_NO_GETADDRINFO)
64
65
#include <qmutex.h>
121
122
QHostInfo QHostInfoAgent::fromName(const QString &hostName)
122
123
{
123
124
    QHostInfo results;
124
 
    results.setHostName(hostName);
125
125
 
126
126
#if defined(QHOSTINFO_DEBUG)
127
127
    qDebug("QHostInfoAgent::fromName(%s) looking up...",
148
148
    if (address.setAddress(hostName)) {
149
149
        // Reverse lookup
150
150
// Reverse lookups using getnameinfo are broken on darwin, use gethostbyaddr instead.
151
 
#if !defined (QT_NO_GETADDRINFO) && !defined (Q_OS_DARWIN)
 
151
#if !defined (QT_NO_GETADDRINFO) && !defined (Q_OS_DARWIN) && !defined (Q_OS_SYMBIAN)
152
152
        sockaddr_in sa4;
153
153
#ifndef QT_NO_IPV6
154
154
        sockaddr_in6 sa6;
180
180
        }
181
181
        results.setHostName(QString::fromLatin1(hbuf));
182
182
#else
183
 
        in_addr_t inetaddr = inet_addr(hostName.toLatin1().constData());
 
183
        in_addr_t inetaddr = qt_safe_inet_addr(hostName.toLatin1().constData());
184
184
        struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET);
185
185
        if (!ent) {
186
186
            results.setError(QHostInfo::HostNotFound);
191
191
#endif
192
192
    }
193
193
 
 
194
    // IDN support
 
195
    QByteArray aceHostname;
 
196
    if (results.hostName().isEmpty()) {
 
197
        // it's a hostname resolution
 
198
        aceHostname = QUrl::toAce(hostName);
 
199
        results.setHostName(hostName);
 
200
        if (aceHostname.isEmpty()) {
 
201
            results.setError(QHostInfo::HostNotFound);
 
202
            results.setErrorString(hostName.isEmpty() ? QObject::tr("No host name given") : QObject::tr("Invalid hostname"));
 
203
            return results;
 
204
        }
 
205
    } else {
 
206
        // it's an IP reverse resolution
 
207
        aceHostname = results.hostName().toLatin1();
 
208
    }
 
209
 
194
210
#if !defined (QT_NO_GETADDRINFO)
195
211
    // Call getaddrinfo, and place all IPv4 addresses at the start and
196
212
    // the IPv6 addresses at the end of the address list in results.
202
218
    hints.ai_flags = Q_ADDRCONFIG;
203
219
#endif
204
220
 
205
 
    int result = getaddrinfo(hostName.toLatin1().constData(), 0, &hints, &res);
 
221
    int result = getaddrinfo(aceHostname.constData(), 0, &hints, &res);
206
222
# ifdef Q_ADDRCONFIG
207
223
    if (result == EAI_BADFLAGS) {
208
224
        // if the lookup failed with AI_ADDRCONFIG set, try again without it
209
225
        hints.ai_flags = 0;
210
 
        result = getaddrinfo(hostName.toLatin1().constData(), 0, &hints, &res);
 
226
        result = getaddrinfo(aceHostname.constData(), 0, &hints, &res);
211
227
    }
212
228
# endif
213
229
 
261
277
    // use one QHostInfoAgent, but if more agents are introduced, locking
262
278
    // must be provided.
263
279
    QMutexLocker locker(::getHostByNameMutex());
264
 
    hostent *result = gethostbyname(hostName.toLatin1().constData());
 
280
    hostent *result = gethostbyname(aceHostname.constData());
265
281
    if (result) {
266
282
        if (result->h_addrtype == AF_INET) {
267
283
            QList<QHostAddress> addresses;
276
292
            results.setError(QHostInfo::UnknownError);
277
293
            results.setErrorString(tr("Unknown address type"));
278
294
        }
 
295
#if !defined(Q_OS_VXWORKS)
279
296
    } else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA
280
297
               || h_errno == NO_ADDRESS) {
281
298
        results.setError(QHostInfo::HostNotFound);
282
299
        results.setErrorString(tr("Host not found"));
 
300
#endif
283
301
    } else {
284
302
        results.setError(QHostInfo::UnknownError);
285
303
        results.setErrorString(tr("Unknown error"));
316
334
 
317
335
QString QHostInfo::localDomainName()
318
336
{
 
337
#if !defined(Q_OS_VXWORKS)
319
338
    resolveLibrary();
320
339
    if (local_res_ninit) {
321
340
        // using thread-safe version
322
341
        res_state_ptr state = res_state_ptr(qMalloc(sizeof(*state)));
 
342
        Q_CHECK_PTR(state);
323
343
        memset(state, 0, sizeof(*state));
324
344
        local_res_ninit(state);
325
345
        QString domainName = QUrl::fromAce(state->defdname);
345
365
            domainName = QUrl::fromAce(local_res->dnsrch[0]);
346
366
        return domainName;
347
367
    }
348
 
 
 
368
#endif
349
369
    // nothing worked, try doing it by ourselves:
350
370
    QFile resolvconf;
351
371
#if defined(_PATH_RESCONF)