1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company 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 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <winsock2.h>
43
44 #include "qhostinfo_p.h"
45 #include "private/qnativesocketengine_p.h"
46 #include <ws2tcpip.h>
47 #include <private/qsystemlibrary_p.h>
48 #include <qurl.h>
49
50 QT_BEGIN_NAMESPACE
51
52 //#define QHOSTINFO_DEBUG
53
54 // Older SDKs do not include the addrinfo struct declaration, so we
55 // include a copy of it here.
56 struct qt_addrinfo
57 {
58 int ai_flags;
59 int ai_family;
60 int ai_socktype;
61 int ai_protocol;
62 size_t ai_addrlen;
63 char *ai_canonname;
64 sockaddr *ai_addr;
65 qt_addrinfo *ai_next;
66 };
67
68 //###
69 #define QT_SOCKLEN_T int
70 #ifndef NI_MAXHOST // already defined to 1025 in ws2tcpip.h?
71 #define NI_MAXHOST 1024
72 #endif
73
74 typedef int (__stdcall *getnameinfoProto)(const sockaddr *, QT_SOCKLEN_T, const char *, DWORD, const char *, DWORD, int);
75 typedef int (__stdcall *getaddrinfoProto)(const char *, const char *, const qt_addrinfo *, qt_addrinfo **);
76 typedef int (__stdcall *freeaddrinfoProto)(qt_addrinfo *);
77 static getnameinfoProto local_getnameinfo = 0;
78 static getaddrinfoProto local_getaddrinfo = 0;
79 static freeaddrinfoProto local_freeaddrinfo = 0;
80
resolveLibrary()81 static void resolveLibrary()
82 {
83 // Attempt to resolve getaddrinfo(); without it we'll have to fall
84 // back to gethostbyname(), which has no IPv6 support.
85 static bool triedResolve = false;
86 if (triedResolve)
87 return;
88
89 #if !defined(Q_OS_WINCE)
90 QSystemLibrary ws2lib(QLatin1String("ws2_32"));
91 #else
92 QSystemLibrary ws2lib(QLatin1String("ws2"));
93 #endif
94 if (ws2lib.load()) {
95 local_getaddrinfo = (getaddrinfoProto)ws2lib.resolve("getaddrinfo");
96 local_freeaddrinfo = (freeaddrinfoProto)ws2lib.resolve("freeaddrinfo");
97 local_getnameinfo = (getnameinfoProto)ws2lib.resolve("getnameinfo");
98 }
99
100 triedResolve = true;
101 }
102
103 #if defined(Q_OS_WINCE)
104 #include <qmutex.h>
Q_GLOBAL_STATIC(QMutex,qPrivCEMutex)105 Q_GLOBAL_STATIC(QMutex, qPrivCEMutex)
106 #endif
107
108 static void translateWSAError(int error, QHostInfo *results)
109 {
110 switch (error) {
111 case WSAHOST_NOT_FOUND: //authoritative not found
112 case WSATRY_AGAIN: //non authoritative not found
113 case WSANO_DATA: //valid name, no associated address
114 results->setError(QHostInfo::HostNotFound);
115 results->setErrorString(QHostInfoAgent::tr("Host not found"));
116 return;
117 default:
118 results->setError(QHostInfo::UnknownError);
119 results->setErrorString(QHostInfoAgent::tr("Unknown error (%1)").arg(error));
120 return;
121 }
122 }
123
fromName(const QString & hostName)124 QHostInfo QHostInfoAgent::fromName(const QString &hostName)
125 {
126 resolveLibrary();
127
128 #if defined(Q_OS_WINCE)
129 QMutexLocker locker(qPrivCEMutex());
130 #endif
131
132 QWindowsSockInit winSock;
133
134 QHostInfo results;
135
136 #if defined(QHOSTINFO_DEBUG)
137 qDebug("QHostInfoAgent::fromName(%p): looking up \"%s\" (IPv6 support is %s)",
138 this, hostName.toLatin1().constData(),
139 (local_getaddrinfo && local_freeaddrinfo) ? "enabled" : "disabled");
140 #endif
141
142 QHostAddress address;
143 if (address.setAddress(hostName)) {
144 // Reverse lookup
145 if (local_getnameinfo) {
146 sockaddr_in sa4;
147 qt_sockaddr_in6 sa6;
148 sockaddr *sa;
149 QT_SOCKLEN_T saSize;
150 if (address.protocol() == QAbstractSocket::IPv4Protocol) {
151 sa = (sockaddr *)&sa4;
152 saSize = sizeof(sa4);
153 memset(&sa4, 0, sizeof(sa4));
154 sa4.sin_family = AF_INET;
155 sa4.sin_addr.s_addr = htonl(address.toIPv4Address());
156 } else {
157 sa = (sockaddr *)&sa6;
158 saSize = sizeof(sa6);
159 memset(&sa6, 0, sizeof(sa6));
160 sa6.sin6_family = AF_INET6;
161 memcpy(sa6.sin6_addr.qt_s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.qt_s6_addr));
162 }
163
164 char hbuf[NI_MAXHOST];
165 if (local_getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0)
166 results.setHostName(QString::fromLatin1(hbuf));
167 } else {
168 unsigned long addr = inet_addr(hostName.toLatin1().constData());
169 struct hostent *ent = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
170 if (ent)
171 results.setHostName(QString::fromLatin1(ent->h_name));
172 }
173
174 if (results.hostName().isEmpty())
175 results.setHostName(address.toString());
176 results.setAddresses(QList<QHostAddress>() << address);
177 return results;
178 }
179
180 // IDN support
181 QByteArray aceHostname = QUrl::toAce(hostName);
182 results.setHostName(hostName);
183 if (aceHostname.isEmpty()) {
184 results.setError(QHostInfo::HostNotFound);
185 results.setErrorString(hostName.isEmpty() ? tr("No host name given") : tr("Invalid hostname"));
186 return results;
187 }
188
189 if (local_getaddrinfo && local_freeaddrinfo) {
190 // Call getaddrinfo, and place all IPv4 addresses at the start
191 // and the IPv6 addresses at the end of the address list in
192 // results.
193 qt_addrinfo *res;
194 int err = local_getaddrinfo(aceHostname.constData(), 0, 0, &res);
195 if (err == 0) {
196 QList<QHostAddress> addresses;
197 for (qt_addrinfo *p = res; p != 0; p = p->ai_next) {
198 switch (p->ai_family) {
199 case AF_INET: {
200 QHostAddress addr;
201 addr.setAddress(ntohl(((sockaddr_in *) p->ai_addr)->sin_addr.s_addr));
202 if (!addresses.contains(addr))
203 addresses.append(addr);
204 }
205 break;
206 case AF_INET6: {
207 QHostAddress addr;
208 addr.setAddress(((qt_sockaddr_in6 *) p->ai_addr)->sin6_addr.qt_s6_addr);
209 if (!addresses.contains(addr))
210 addresses.append(addr);
211 }
212 break;
213 default:
214 results.setError(QHostInfo::UnknownError);
215 results.setErrorString(tr("Unknown address type"));
216 }
217 }
218 results.setAddresses(addresses);
219 local_freeaddrinfo(res);
220 } else {
221 translateWSAError(WSAGetLastError(), &results);
222 }
223 } else {
224 // Fall back to gethostbyname, which only supports IPv4.
225 hostent *ent = gethostbyname(aceHostname.constData());
226 if (ent) {
227 char **p;
228 QList<QHostAddress> addresses;
229 switch (ent->h_addrtype) {
230 case AF_INET:
231 for (p = ent->h_addr_list; *p != 0; p++) {
232 long *ip4Addr = (long *) *p;
233 QHostAddress temp;
234 temp.setAddress(ntohl(*ip4Addr));
235 addresses << temp;
236 }
237 break;
238 default:
239 results.setError(QHostInfo::UnknownError);
240 results.setErrorString(tr("Unknown address type"));
241 break;
242 }
243 results.setAddresses(addresses);
244 } else {
245 translateWSAError(WSAGetLastError(), &results);
246 }
247 }
248
249 #if defined(QHOSTINFO_DEBUG)
250 if (results.error() != QHostInfo::NoError) {
251 qDebug("QHostInfoAgent::run(%p): error (%s)",
252 this, results.errorString().toLatin1().constData());
253 } else {
254 QString tmp;
255 QList<QHostAddress> addresses = results.addresses();
256 for (int i = 0; i < addresses.count(); ++i) {
257 if (i != 0) tmp += ", ";
258 tmp += addresses.at(i).toString();
259 }
260 qDebug("QHostInfoAgent::run(%p): found %i entries: {%s}",
261 this, addresses.count(), tmp.toLatin1().constData());
262 }
263 #endif
264 return results;
265 }
266
localHostName()267 QString QHostInfo::localHostName()
268 {
269 QWindowsSockInit winSock;
270
271 char hostName[512];
272 if (gethostname(hostName, sizeof(hostName)) == -1)
273 return QString();
274 hostName[sizeof(hostName) - 1] = '\0';
275 return QString::fromLocal8Bit(hostName);
276 }
277
278 // QString QHostInfo::localDomainName() defined in qnetworkinterface_win.cpp
279
280 QT_END_NAMESPACE
281