1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the network module of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "qplatformdefs.h"
31
#include "qhostinfo_p.h"
32
#include "qiodevice.h"
33
#include <qbytearray.h>
39
#if defined (QT_NO_GETADDRINFO)
41
Q_GLOBAL_STATIC(QMutex, getHostByNameMutex)
44
//#define QHOSTINFO_DEBUG
46
QHostInfo QHostInfoAgent::fromName(const QString &hostName)
49
results.setHostName(hostName);
51
#if defined(QHOSTINFO_DEBUG)
52
qDebug("QHostInfoAgent::fromName(%s) looking up...",
53
hostName.toLatin1().constData());
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.
60
struct addrinfo hints;
61
memset(&hints, 0, sizeof(hints));
62
hints.ai_family = PF_UNSPEC;
64
int result = getaddrinfo(hostName.toLatin1().constData(), 0, &hints, &res);
67
QList<QHostAddress> addresses;
69
if (node->ai_family == AF_INET) {
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) {
76
addr.setAddress(((sockaddr_in6 *) node->ai_addr)->sin6_addr.s6_addr);
77
if (!addresses.contains(addr))
78
addresses.append(addr);
80
results.setError(QHostInfo::UnknownError);
81
results.setErrorString(tr("Unknown address type"));
86
results.setAddresses(addresses);
88
} else if (result == EAI_NONAME
90
// EAI_NODATA is deprecated in RFC 3493
91
|| result == EAI_NODATA
94
results.setError(QHostInfo::HostNotFound);
95
results.setErrorString(tr("Host not found"));
97
results.setError(QHostInfo::UnknownError);
98
results.setErrorString(QString::fromLocal8Bit(gai_strerror(result)));
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
107
QMutexLocker locker(::getHostByNameMutex());
108
hostent *result = gethostbyname(hostName.toLatin1().constData());
110
if (result->h_addrtype == AF_INET) {
111
QList<QHostAddress> addresses;
112
for (char **p = result->h_addr_list; *p != 0; p++) {
114
addr.setAddress(ntohl(*((quint32 *)*p)));
115
if (!addresses.contains(addr))
116
addresses.prepend(addr);
118
results.setAddresses(addresses);
120
results.setError(QHostInfo::UnknownError);
121
results.setErrorString(tr("Unknown address type"));
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"));
128
results.setError(QHostInfo::UnknownError);
129
results.setErrorString(tr("Unknown error"));
131
#endif // !defined (QT_NO_GETADDRINFO)
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());
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();
144
qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}",
145
addresses.count(), hostName.toLatin1().constData(),
146
tmp.toLatin1().constData());
152
QString QHostInfo::localHostName()
155
if (gethostname(hostName, sizeof(hostName)) == -1)
157
hostName[sizeof(hostName) - 1] = '\0';
158
return QString::fromLocal8Bit(hostName);