source: trunk/src/network/kernel/qhostinfo_os2.cpp@ 568

Last change on this file since 568 was 568, checked in by Dmitry A. Kuminov, 15 years ago

network: Adopted to 4.6.1 changes.

File size: 7.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** Copyright (C) 2009 netlabs.org. OS/2 parts.
8**
9** This file is part of the QtNetwork module of the Qt Toolkit.
10**
11** $QT_BEGIN_LICENSE:LGPL$
12** Commercial Usage
13** Licensees holding valid Qt Commercial licenses may use this file in
14** accordance with the Qt Commercial License Agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Nokia.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 2.1 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 2.1 requirements
24** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** In addition, as a special exception, Nokia gives you certain additional
27** rights. These rights are described in the Nokia Qt LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** GNU General Public License Usage
31** Alternatively, this file may be used under the terms of the GNU
32** General Public License version 3.0 as published by the Free Software
33** Foundation and appearing in the file LICENSE.GPL included in the
34** packaging of this file. Please review the following information to
35** ensure the GNU General Public License version 3.0 requirements will be
36** met: http://www.gnu.org/copyleft/gpl.html.
37**
38** If you have questions regarding the use of this file, please contact
39** Nokia at [email protected].
40** $QT_END_LICENSE$
41**
42****************************************************************************/
43
44//#define QHOSTINFO_DEBUG
45
46static const int RESOLVER_TIMEOUT = 2000;
47
48#include "qt_os2.h"
49#include "qplatformdefs.h"
50
51#include "qhostinfo_p.h"
52#include "qiodevice.h"
53#include <qbytearray.h>
54#include <qlibrary.h>
55#include <qurl.h>
56#include <qfile.h>
57
58extern "C" {
59#include <sys/types.h>
60#include <netdb.h>
61#include <arpa/inet.h>
62#include <resolv.h>
63}
64
65#include <qmutex.h>
66QT_BEGIN_NAMESPACE
67Q_GLOBAL_STATIC(QMutex, getHostByNameMutex)
68QT_END_NAMESPACE
69
70QT_BEGIN_NAMESPACE
71
72QHostInfo QHostInfoAgent::fromName(const QString &hostName)
73{
74 QHostInfo results;
75 results.setHostName(hostName);
76
77#if defined(QHOSTINFO_DEBUG)
78 qDebug("QHostInfoAgent::fromName(%s) looking up...",
79 hostName.toLatin1().constData());
80#endif
81
82 QHostAddress address;
83 if (address.setAddress(hostName)) {
84 // Reverse lookup
85 in_addr_t inetaddr = inet_addr(hostName.toLatin1().constData());
86 struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET);
87 if (!ent) {
88 results.setError(QHostInfo::HostNotFound);
89 results.setErrorString(tr("Host not found"));
90 return results;
91 }
92 results.setHostName(QString::fromLatin1(ent->h_name));
93 }
94
95 // Fall back to gethostbyname for platforms that don't define
96 // getaddrinfo. gethostbyname does not support IPv6, and it's not
97 // reentrant on all platforms. For now this is okay since we only
98 // use one QHostInfoAgent, but if more agents are introduced, locking
99 // must be provided.
100 QMutexLocker locker(::getHostByNameMutex());
101 hostent *result = gethostbyname(hostName.toLatin1().constData());
102 if (result) {
103 if (result->h_addrtype == AF_INET) {
104 QList<QHostAddress> addresses;
105 for (char **p = result->h_addr_list; *p != 0; p++) {
106 QHostAddress addr;
107 addr.setAddress(ntohl(*((quint32 *)*p)));
108 if (!addresses.contains(addr))
109 addresses.prepend(addr);
110 }
111 results.setAddresses(addresses);
112 } else {
113 results.setError(QHostInfo::UnknownError);
114 results.setErrorString(tr("Unknown address type"));
115 }
116 } else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA
117 || h_errno == NO_ADDRESS) {
118 results.setError(QHostInfo::HostNotFound);
119 results.setErrorString(tr("Host not found"));
120 } else {
121 results.setError(QHostInfo::UnknownError);
122 results.setErrorString(tr("Unknown error"));
123 }
124
125#if defined(QHOSTINFO_DEBUG)
126 if (results.error() != QHostInfo::NoError) {
127 qDebug("QHostInfoAgent::fromName(): error #%d %s",
128 h_errno, results.errorString().toLatin1().constData());
129 } else {
130 QString tmp;
131 QList<QHostAddress> addresses = results.addresses();
132 for (int i = 0; i < addresses.count(); ++i) {
133 if (i != 0) tmp += ", ";
134 tmp += addresses.at(i).toString();
135 }
136 qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}",
137 addresses.count(), hostName.toLatin1().constData(),
138 tmp.toLatin1().constData());
139 }
140#endif
141 return results;
142}
143
144QString QHostInfo::localHostName()
145{
146 char hostName[512];
147 if (gethostname(hostName, sizeof(hostName)) == -1)
148 return QString();
149 hostName[sizeof(hostName) - 1] = '\0';
150 return QString::fromLocal8Bit(hostName);
151}
152
153QString QHostInfo::localDomainName()
154{
155 // We have to call res_init to be sure that _res was initialized
156 // So, for systems without getaddrinfo (which is thread-safe), we lock the mutex too
157 {
158 QMutexLocker locker(::getHostByNameMutex());
159 if ((_res.options & RES_INIT) || res_init() == 0) {
160 QString domainName = QUrl::fromAce(_res.defdname);
161 if (domainName.isEmpty())
162 domainName = QUrl::fromAce(_res.dnsrch[0]);
163 return domainName;
164 }
165 }
166
167 // nothing worked, try doing it by ourselves:
168 QFile resolvconf;
169 QByteArray etc = qgetenv("ETC");
170 if (etc.isEmpty()) {
171 etc = "x:\\MPTN\\ETC";
172 ULONG boot = 0;
173 DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, (PVOID)boot, sizeof(boot));
174 etc[0] = (char)(boot + 'A' - 1);
175 }
176 // currently, resolv takes precedence over resolv2 assuming that resolv
177 // is created for PPP connections and therefore should override resolv2
178 // which is created for permanent LAN connections.
179 resolvconf.setFileName(QFile::decodeName(etc) + QLatin1String("\\resolv"));
180 if (!resolvconf.exists())
181 resolvconf.setFileName(QFile::decodeName(etc)+ QLatin1String("\\resolv2"));
182
183 if (!resolvconf.open(QIODevice::ReadOnly))
184 return QString(); // failure
185
186 QString domainName;
187 while (!resolvconf.atEnd()) {
188 QByteArray line = resolvconf.readLine().trimmed();
189 if (line.startsWith("domain "))
190 return QUrl::fromAce(line.mid(sizeof "domain " - 1).trimmed());
191
192 // in case there's no "domain" line, fall back to the first "search" entry
193 if (domainName.isEmpty() && line.startsWith("search ")) {
194 QByteArray searchDomain = line.mid(sizeof "search " - 1).trimmed();
195 int pos = searchDomain.indexOf(' ');
196 if (pos != -1)
197 searchDomain.truncate(pos);
198 domainName = QUrl::fromAce(searchDomain);
199 }
200 }
201
202 // return the fallen-back-to searched domain
203 return domainName;
204}
205
206QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.