author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
//#define QHOSTINFO_DEBUG |
|
43 |
||
44 |
#include "qplatformdefs.h" |
|
45 |
||
46 |
#include "qhostinfo_p.h" |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
47 |
#include "private/qnativesocketengine_p.h" |
0 | 48 |
#include "qiodevice.h" |
49 |
#include <qbytearray.h> |
|
50 |
#include <qlibrary.h> |
|
51 |
#include <qurl.h> |
|
52 |
#include <qfile.h> |
|
53 |
#include <private/qmutexpool_p.h> |
|
54 |
#include <private/qnet_unix_p.h> |
|
55 |
||
56 |
#include <sys/types.h> |
|
57 |
#include <netdb.h> |
|
58 |
#include <arpa/inet.h> |
|
59 |
#if defined(Q_OS_VXWORKS) |
|
60 |
# include <hostLib.h> |
|
61 |
#else |
|
62 |
# include <resolv.h> |
|
63 |
#endif |
|
64 |
||
65 |
#if defined (QT_NO_GETADDRINFO) |
|
66 |
#include <qmutex.h> |
|
67 |
QT_BEGIN_NAMESPACE |
|
68 |
Q_GLOBAL_STATIC(QMutex, getHostByNameMutex) |
|
69 |
QT_END_NAMESPACE |
|
70 |
#endif |
|
71 |
||
72 |
QT_BEGIN_NAMESPACE |
|
73 |
||
74 |
// Almost always the same. If not, specify in qplatformdefs.h. |
|
75 |
#if !defined(QT_SOCKOPTLEN_T) |
|
76 |
# define QT_SOCKOPTLEN_T QT_SOCKLEN_T |
|
77 |
#endif |
|
78 |
||
79 |
// HP-UXi has a bug in getaddrinfo(3) that makes it thread-unsafe |
|
80 |
// with this flag. So disable it in that platform. |
|
81 |
#if defined(AI_ADDRCONFIG) && !defined(Q_OS_HPUX) |
|
82 |
# define Q_ADDRCONFIG AI_ADDRCONFIG |
|
83 |
#endif |
|
84 |
||
85 |
typedef struct __res_state *res_state_ptr; |
|
86 |
||
87 |
typedef int (*res_init_proto)(void); |
|
88 |
static res_init_proto local_res_init = 0; |
|
89 |
typedef int (*res_ninit_proto)(res_state_ptr); |
|
90 |
static res_ninit_proto local_res_ninit = 0; |
|
91 |
typedef void (*res_nclose_proto)(res_state_ptr); |
|
92 |
static res_nclose_proto local_res_nclose = 0; |
|
93 |
static res_state_ptr local_res = 0; |
|
94 |
||
95 |
static void resolveLibrary() |
|
96 |
{ |
|
97 |
#ifndef QT_NO_LIBRARY |
|
98 |
QLibrary lib(QLatin1String("resolv")); |
|
99 |
if (!lib.load()) |
|
100 |
return; |
|
101 |
||
102 |
local_res_init = res_init_proto(lib.resolve("__res_init")); |
|
103 |
if (!local_res_init) |
|
104 |
local_res_init = res_init_proto(lib.resolve("res_init")); |
|
105 |
||
106 |
local_res_ninit = res_ninit_proto(lib.resolve("__res_ninit")); |
|
107 |
if (!local_res_ninit) |
|
108 |
local_res_ninit = res_ninit_proto(lib.resolve("res_ninit")); |
|
109 |
||
110 |
if (!local_res_ninit) { |
|
111 |
// if we can't get a thread-safe context, we have to use the global _res state |
|
112 |
local_res = res_state_ptr(lib.resolve("_res")); |
|
113 |
} else { |
|
114 |
local_res_nclose = res_nclose_proto(lib.resolve("res_nclose")); |
|
115 |
if (!local_res_nclose) |
|
116 |
local_res_nclose = res_nclose_proto(lib.resolve("__res_nclose")); |
|
117 |
if (!local_res_nclose) |
|
118 |
local_res_ninit = 0; |
|
119 |
} |
|
120 |
#endif |
|
121 |
} |
|
122 |
||
123 |
QHostInfo QHostInfoAgent::fromName(const QString &hostName) |
|
124 |
{ |
|
125 |
QHostInfo results; |
|
126 |
||
127 |
#if defined(QHOSTINFO_DEBUG) |
|
128 |
qDebug("QHostInfoAgent::fromName(%s) looking up...", |
|
129 |
hostName.toLatin1().constData()); |
|
130 |
#endif |
|
131 |
||
132 |
// Load res_init on demand. |
|
133 |
static volatile bool triedResolve = false; |
|
134 |
if (!triedResolve) { |
|
135 |
#ifndef QT_NO_THREAD |
|
136 |
QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_init)); |
|
137 |
#endif |
|
138 |
if (!triedResolve) { |
|
139 |
resolveLibrary(); |
|
140 |
triedResolve = true; |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
// If res_init is available, poll it. |
|
145 |
if (local_res_init) |
|
146 |
local_res_init(); |
|
147 |
||
148 |
QHostAddress address; |
|
149 |
if (address.setAddress(hostName)) { |
|
150 |
// Reverse lookup |
|
151 |
// Reverse lookups using getnameinfo are broken on darwin, use gethostbyaddr instead. |
|
152 |
#if !defined (QT_NO_GETADDRINFO) && !defined (Q_OS_DARWIN) && !defined (Q_OS_SYMBIAN) |
|
153 |
sockaddr_in sa4; |
|
154 |
#ifndef QT_NO_IPV6 |
|
155 |
sockaddr_in6 sa6; |
|
156 |
#endif |
|
157 |
sockaddr *sa = 0; |
|
158 |
QT_SOCKLEN_T saSize = 0; |
|
159 |
if (address.protocol() == QAbstractSocket::IPv4Protocol) { |
|
160 |
sa = (sockaddr *)&sa4; |
|
161 |
saSize = sizeof(sa4); |
|
162 |
memset(&sa4, 0, sizeof(sa4)); |
|
163 |
sa4.sin_family = AF_INET; |
|
164 |
sa4.sin_addr.s_addr = htonl(address.toIPv4Address()); |
|
165 |
} |
|
166 |
#ifndef QT_NO_IPV6 |
|
167 |
else { |
|
168 |
sa = (sockaddr *)&sa6; |
|
169 |
saSize = sizeof(sa6); |
|
170 |
memset(&sa6, 0, sizeof(sa6)); |
|
171 |
sa6.sin6_family = AF_INET6; |
|
172 |
memcpy(sa6.sin6_addr.s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.s6_addr)); |
|
173 |
} |
|
174 |
#endif |
|
175 |
||
176 |
char hbuf[NI_MAXHOST]; |
|
177 |
if (sa && getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0) |
|
178 |
results.setHostName(QString::fromLatin1(hbuf)); |
|
179 |
#else |
|
180 |
in_addr_t inetaddr = qt_safe_inet_addr(hostName.toLatin1().constData()); |
|
181 |
struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET); |
|
182 |
if (ent) |
|
183 |
results.setHostName(QString::fromLatin1(ent->h_name)); |
|
184 |
#endif |
|
185 |
||
186 |
if (results.hostName().isEmpty()) |
|
187 |
results.setHostName(address.toString()); |
|
188 |
results.setAddresses(QList<QHostAddress>() << address); |
|
189 |
return results; |
|
190 |
} |
|
191 |
||
192 |
// IDN support |
|
193 |
QByteArray aceHostname = QUrl::toAce(hostName); |
|
194 |
results.setHostName(hostName); |
|
195 |
if (aceHostname.isEmpty()) { |
|
196 |
results.setError(QHostInfo::HostNotFound); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
197 |
results.setErrorString(hostName.isEmpty() ? |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
198 |
QCoreApplication::translate("QHostInfoAgent", "No host name given") : |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
199 |
QCoreApplication::translate("QHostInfoAgent", "Invalid hostname")); |
0 | 200 |
return results; |
201 |
} |
|
202 |
||
203 |
#if !defined (QT_NO_GETADDRINFO) |
|
204 |
// Call getaddrinfo, and place all IPv4 addresses at the start and |
|
205 |
// the IPv6 addresses at the end of the address list in results. |
|
206 |
addrinfo *res = 0; |
|
207 |
struct addrinfo hints; |
|
208 |
memset(&hints, 0, sizeof(hints)); |
|
209 |
hints.ai_family = PF_UNSPEC; |
|
210 |
#ifdef Q_ADDRCONFIG |
|
211 |
hints.ai_flags = Q_ADDRCONFIG; |
|
212 |
#endif |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
213 |
#ifdef Q_OS_SYMBIAN |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
214 |
# ifdef QHOSTINFO_DEBUG |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
215 |
qDebug() << "Setting flags: 'hints.ai_flags &= AI_V4MAPPED | AI_ALL'"; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
216 |
# endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
217 |
#endif |
0 | 218 |
|
219 |
int result = getaddrinfo(aceHostname.constData(), 0, &hints, &res); |
|
220 |
# ifdef Q_ADDRCONFIG |
|
221 |
if (result == EAI_BADFLAGS) { |
|
222 |
// if the lookup failed with AI_ADDRCONFIG set, try again without it |
|
223 |
hints.ai_flags = 0; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
224 |
#ifdef Q_OS_SYMBIAN |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
225 |
# ifdef QHOSTINFO_DEBUG |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
226 |
qDebug() << "Setting flags: 'hints.ai_flags &= AI_V4MAPPED | AI_ALL'"; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
227 |
# endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
228 |
hints.ai_flags &= AI_V4MAPPED | AI_ALL; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
229 |
#endif |
0 | 230 |
result = getaddrinfo(aceHostname.constData(), 0, &hints, &res); |
231 |
} |
|
232 |
# endif |
|
233 |
||
234 |
if (result == 0) { |
|
235 |
addrinfo *node = res; |
|
236 |
QList<QHostAddress> addresses; |
|
237 |
while (node) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
238 |
#ifdef QHOSTINFO_DEBUG |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
239 |
qDebug() << "getaddrinfo node: flags:" << node->ai_flags << "family:" << node->ai_family << "ai_socktype:" << node->ai_socktype << "ai_protocol:" << node->ai_protocol << "ai_addrlen:" << node->ai_addrlen; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
240 |
#endif |
0 | 241 |
if (node->ai_family == AF_INET) { |
242 |
QHostAddress addr; |
|
243 |
addr.setAddress(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr)); |
|
244 |
if (!addresses.contains(addr)) |
|
245 |
addresses.append(addr); |
|
246 |
} |
|
247 |
#ifndef QT_NO_IPV6 |
|
248 |
else if (node->ai_family == AF_INET6) { |
|
249 |
QHostAddress addr; |
|
250 |
addr.setAddress(((sockaddr_in6 *) node->ai_addr)->sin6_addr.s6_addr); |
|
251 |
if (!addresses.contains(addr)) |
|
252 |
addresses.append(addr); |
|
253 |
} |
|
254 |
#endif |
|
255 |
node = node->ai_next; |
|
256 |
} |
|
257 |
if (addresses.isEmpty() && node == 0) { |
|
258 |
// Reached the end of the list, but no addresses were found; this |
|
259 |
// means the list contains one or more unknown address types. |
|
260 |
results.setError(QHostInfo::UnknownError); |
|
261 |
results.setErrorString(tr("Unknown address type")); |
|
262 |
} |
|
263 |
||
264 |
results.setAddresses(addresses); |
|
265 |
freeaddrinfo(res); |
|
266 |
} else if (result == EAI_NONAME |
|
267 |
|| result == EAI_FAIL |
|
268 |
#ifdef EAI_NODATA |
|
269 |
// EAI_NODATA is deprecated in RFC 3493 |
|
270 |
|| result == EAI_NODATA |
|
271 |
#endif |
|
272 |
) { |
|
273 |
results.setError(QHostInfo::HostNotFound); |
|
274 |
results.setErrorString(tr("Host not found")); |
|
275 |
} else { |
|
276 |
results.setError(QHostInfo::UnknownError); |
|
277 |
results.setErrorString(QString::fromLocal8Bit(gai_strerror(result))); |
|
278 |
} |
|
279 |
||
280 |
#else |
|
281 |
// Fall back to gethostbyname for platforms that don't define |
|
282 |
// getaddrinfo. gethostbyname does not support IPv6, and it's not |
|
283 |
// reentrant on all platforms. For now this is okay since we only |
|
284 |
// use one QHostInfoAgent, but if more agents are introduced, locking |
|
285 |
// must be provided. |
|
286 |
QMutexLocker locker(::getHostByNameMutex()); |
|
287 |
hostent *result = gethostbyname(aceHostname.constData()); |
|
288 |
if (result) { |
|
289 |
if (result->h_addrtype == AF_INET) { |
|
290 |
QList<QHostAddress> addresses; |
|
291 |
for (char **p = result->h_addr_list; *p != 0; p++) { |
|
292 |
QHostAddress addr; |
|
293 |
addr.setAddress(ntohl(*((quint32 *)*p))); |
|
294 |
if (!addresses.contains(addr)) |
|
295 |
addresses.prepend(addr); |
|
296 |
} |
|
297 |
results.setAddresses(addresses); |
|
298 |
} else { |
|
299 |
results.setError(QHostInfo::UnknownError); |
|
300 |
results.setErrorString(tr("Unknown address type")); |
|
301 |
} |
|
302 |
#if !defined(Q_OS_VXWORKS) |
|
303 |
} else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA |
|
304 |
|| h_errno == NO_ADDRESS) { |
|
305 |
results.setError(QHostInfo::HostNotFound); |
|
306 |
results.setErrorString(tr("Host not found")); |
|
307 |
#endif |
|
308 |
} else { |
|
309 |
results.setError(QHostInfo::UnknownError); |
|
310 |
results.setErrorString(tr("Unknown error")); |
|
311 |
} |
|
312 |
#endif // !defined (QT_NO_GETADDRINFO) |
|
313 |
||
314 |
#if defined(QHOSTINFO_DEBUG) |
|
315 |
if (results.error() != QHostInfo::NoError) { |
|
316 |
qDebug("QHostInfoAgent::fromName(): error #%d %s", |
|
317 |
h_errno, results.errorString().toLatin1().constData()); |
|
318 |
} else { |
|
319 |
QString tmp; |
|
320 |
QList<QHostAddress> addresses = results.addresses(); |
|
321 |
for (int i = 0; i < addresses.count(); ++i) { |
|
322 |
if (i != 0) tmp += ", "; |
|
323 |
tmp += addresses.at(i).toString(); |
|
324 |
} |
|
325 |
qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}", |
|
326 |
addresses.count(), hostName.toLatin1().constData(), |
|
327 |
tmp.toLatin1().constData()); |
|
328 |
} |
|
329 |
#endif |
|
330 |
return results; |
|
331 |
} |
|
332 |
||
333 |
QString QHostInfo::localHostName() |
|
334 |
{ |
|
335 |
char hostName[512]; |
|
336 |
if (gethostname(hostName, sizeof(hostName)) == -1) |
|
337 |
return QString(); |
|
338 |
hostName[sizeof(hostName) - 1] = '\0'; |
|
339 |
return QString::fromLocal8Bit(hostName); |
|
340 |
} |
|
341 |
||
342 |
QString QHostInfo::localDomainName() |
|
343 |
{ |
|
344 |
#if !defined(Q_OS_VXWORKS) |
|
345 |
resolveLibrary(); |
|
346 |
if (local_res_ninit) { |
|
347 |
// using thread-safe version |
|
348 |
res_state_ptr state = res_state_ptr(qMalloc(sizeof(*state))); |
|
349 |
Q_CHECK_PTR(state); |
|
350 |
memset(state, 0, sizeof(*state)); |
|
351 |
local_res_ninit(state); |
|
352 |
QString domainName = QUrl::fromAce(state->defdname); |
|
353 |
if (domainName.isEmpty()) |
|
354 |
domainName = QUrl::fromAce(state->dnsrch[0]); |
|
355 |
local_res_nclose(state); |
|
356 |
qFree(state); |
|
357 |
||
358 |
return domainName; |
|
359 |
} |
|
360 |
||
361 |
if (local_res_init && local_res) { |
|
362 |
// using thread-unsafe version |
|
363 |
||
364 |
#if defined(QT_NO_GETADDRINFO) |
|
365 |
// We have to call res_init to be sure that _res was initialized |
|
366 |
// So, for systems without getaddrinfo (which is thread-safe), we lock the mutex too |
|
367 |
QMutexLocker locker(::getHostByNameMutex()); |
|
368 |
#endif |
|
369 |
local_res_init(); |
|
370 |
QString domainName = QUrl::fromAce(local_res->defdname); |
|
371 |
if (domainName.isEmpty()) |
|
372 |
domainName = QUrl::fromAce(local_res->dnsrch[0]); |
|
373 |
return domainName; |
|
374 |
} |
|
375 |
#endif |
|
376 |
// nothing worked, try doing it by ourselves: |
|
377 |
QFile resolvconf; |
|
378 |
#if defined(_PATH_RESCONF) |
|
379 |
resolvconf.setFileName(QFile::decodeName(_PATH_RESCONF)); |
|
380 |
#else |
|
381 |
resolvconf.setFileName(QLatin1String("/etc/resolv.conf")); |
|
382 |
#endif |
|
383 |
if (!resolvconf.open(QIODevice::ReadOnly)) |
|
384 |
return QString(); // failure |
|
385 |
||
386 |
QString domainName; |
|
387 |
while (!resolvconf.atEnd()) { |
|
388 |
QByteArray line = resolvconf.readLine().trimmed(); |
|
389 |
if (line.startsWith("domain ")) |
|
390 |
return QUrl::fromAce(line.mid(sizeof "domain " - 1).trimmed()); |
|
391 |
||
392 |
// in case there's no "domain" line, fall back to the first "search" entry |
|
393 |
if (domainName.isEmpty() && line.startsWith("search ")) { |
|
394 |
QByteArray searchDomain = line.mid(sizeof "search " - 1).trimmed(); |
|
395 |
int pos = searchDomain.indexOf(' '); |
|
396 |
if (pos != -1) |
|
397 |
searchDomain.truncate(pos); |
|
398 |
domainName = QUrl::fromAce(searchDomain); |
|
399 |
} |
|
400 |
} |
|
401 |
||
402 |
// return the fallen-back-to searched domain |
|
403 |
return domainName; |
|
404 |
} |
|
405 |
||
406 |
QT_END_NAMESPACE |