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

« back to all changes in this revision

Viewing changes to src/network/qhostinfo_unix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the network module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qplatformdefs.h"
 
30
 
 
31
#include "qhostinfo_p.h"
 
32
#include "qiodevice.h"
 
33
#include <qbytearray.h>
 
34
 
 
35
extern "C" {
 
36
#include <netdb.h>
 
37
}
 
38
 
 
39
#if defined (QT_NO_GETADDRINFO)
 
40
#include <qmutex.h>
 
41
Q_GLOBAL_STATIC(QMutex, getHostByNameMutex)
 
42
#endif
 
43
 
 
44
//#define QHOSTINFO_DEBUG
 
45
 
 
46
QHostInfo QHostInfoAgent::fromName(const QString &hostName)
 
47
{
 
48
    QHostInfo results;
 
49
    results.setHostName(hostName);
 
50
 
 
51
#if defined(QHOSTINFO_DEBUG)
 
52
    qDebug("QHostInfoAgent::fromName(%s) looking up...",
 
53
           hostName.toLatin1().constData());
 
54
#endif
 
55
 
 
56
#if !defined (QT_NO_GETADDRINFO)
 
57
    // Call getaddrinfo, and place all IPv4 addresses at the start and
 
58
    // the IPv6 addresses at the end of the address list in results.
 
59
    addrinfo *res = 0;
 
60
    struct addrinfo hints;
 
61
    memset(&hints, 0, sizeof(hints));
 
62
    hints.ai_family = PF_UNSPEC;
 
63
 
 
64
    int result = getaddrinfo(hostName.toLatin1().constData(), 0, &hints, &res);
 
65
    if (result == 0) {
 
66
        addrinfo *node = res;
 
67
        QList<QHostAddress> addresses;
 
68
        while (node) {
 
69
            if (node->ai_family == AF_INET) {
 
70
                QHostAddress addr;
 
71
                addr.setAddress(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr));
 
72
                if (!addresses.contains(addr))
 
73
                    addresses.prepend(addr);
 
74
            } else if (node->ai_family == AF_INET6) {
 
75
                QHostAddress addr;
 
76
                addr.setAddress(((sockaddr_in6 *) node->ai_addr)->sin6_addr.s6_addr);
 
77
                if (!addresses.contains(addr))
 
78
                    addresses.append(addr);
 
79
            } else {
 
80
                results.setError(QHostInfo::UnknownError);
 
81
                results.setErrorString(tr("Unknown address type"));
 
82
                break;
 
83
            }
 
84
            node = node->ai_next;
 
85
        }
 
86
        results.setAddresses(addresses);
 
87
        freeaddrinfo(res);
 
88
    } else if (result == EAI_NONAME
 
89
#ifdef EAI_NODATA
 
90
               // EAI_NODATA is deprecated in RFC 3493
 
91
               || result == EAI_NODATA
 
92
#endif
 
93
               ) {
 
94
        results.setError(QHostInfo::HostNotFound);
 
95
        results.setErrorString(tr("Host not found"));
 
96
    } else {
 
97
        results.setError(QHostInfo::UnknownError);
 
98
        results.setErrorString(QString::fromLocal8Bit(gai_strerror(result)));
 
99
    }
 
100
 
 
101
#else
 
102
    // Fall back to gethostbyname for platforms that don't define
 
103
    // getaddrinfo. gethostbyname does not support IPv6, and it's not
 
104
    // reentrant on all platforms. For now this is okay since we only
 
105
    // use one QHostInfoAgent, but if more agents are introduced, locking
 
106
    // must be provided.
 
107
    QMutexLocker locker(::getHostByNameMutex());
 
108
    hostent *result = gethostbyname(hostName.toLatin1().constData());
 
109
    if (result) {
 
110
        if (result->h_addrtype == AF_INET) {
 
111
            QList<QHostAddress> addresses;
 
112
            for (char **p = result->h_addr_list; *p != 0; p++) {
 
113
                QHostAddress addr;
 
114
                addr.setAddress(ntohl(*((quint32 *)*p)));
 
115
                if (!addresses.contains(addr))
 
116
                    addresses.prepend(addr);
 
117
            }
 
118
            results.setAddresses(addresses);
 
119
        } else {
 
120
            results.setError(QHostInfo::UnknownError);
 
121
            results.setErrorString(tr("Unknown address type"));
 
122
        }
 
123
    } else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA
 
124
               || h_errno == NO_ADDRESS) {
 
125
        results.setError(QHostInfo::HostNotFound);
 
126
        results.setErrorString(tr("Host not found"));
 
127
    } else {
 
128
        results.setError(QHostInfo::UnknownError);
 
129
        results.setErrorString(tr("Unknown error"));
 
130
    }
 
131
#endif //  !defined (QT_NO_GETADDRINFO)
 
132
 
 
133
#if defined(QHOSTINFO_DEBUG)
 
134
    if (results.error() != QHostInfo::NoError) {
 
135
        qDebug("QHostInfoAgent::fromName(): error #%d %s",
 
136
               h_errno, results.errorString().toLatin1().constData());
 
137
    } else {
 
138
        QString tmp;
 
139
        QList<QHostAddress> addresses = results.addresses();
 
140
        for (int i = 0; i < addresses.count(); ++i) {
 
141
            if (i != 0) tmp += ", ";
 
142
            tmp += addresses.at(i).toString();
 
143
        }
 
144
        qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}",
 
145
               addresses.count(), hostName.toLatin1().constData(),
 
146
               tmp.toLatin1().constData());
 
147
    }
 
148
#endif
 
149
    return results;
 
150
}
 
151
 
 
152
QString QHostInfo::localHostName()
 
153
{
 
154
    char hostName[512];
 
155
    if (gethostname(hostName, sizeof(hostName)) == -1)
 
156
        return QString();
 
157
    hostName[sizeof(hostName) - 1] = '\0';
 
158
    return QString::fromLocal8Bit(hostName);
 
159
}