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

Last change on this file since 561 was 160, checked in by Dmitry A. Kuminov, 16 years ago

network: Ported QHostInfo and QNetworkInterface to OS/2.

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