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