1
1
/****************************************************************************
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)
6
7
** This file is part of the QtNetwork module of the Qt Toolkit.
8
9
** $QT_BEGIN_LICENSE:LGPL$
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
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.
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
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.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
38
38
** $QT_END_LICENSE$
40
40
****************************************************************************/
42
42
//#define QHOSTINFO_DEBUG
44
static const int RESOLVER_TIMEOUT = 2000;
46
44
#include "qplatformdefs.h"
48
46
#include "qhostinfo_p.h"
121
122
QHostInfo QHostInfoAgent::fromName(const QString &hostName)
123
124
QHostInfo results;
124
results.setHostName(hostName);
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)
153
153
#ifndef QT_NO_IPV6
154
154
sockaddr_in6 sa6;
181
181
results.setHostName(QString::fromLatin1(hbuf));
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);
186
186
results.setError(QHostInfo::HostNotFound);
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"));
206
// it's an IP reverse resolution
207
aceHostname = results.hostName().toLatin1();
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;
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);
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());
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"));
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"));
284
302
results.setError(QHostInfo::UnknownError);
285
303
results.setErrorString(tr("Unknown error"));
317
335
QString QHostInfo::localDomainName()
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)));
323
343
memset(state, 0, sizeof(*state));
324
344
local_res_ninit(state);
325
345
QString domainName = QUrl::fromAce(state->defdname);