0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
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 |
#include "qnetworkinterface.h"
|
|
43 |
#include "qnetworkinterface_p.h"
|
|
44 |
|
|
45 |
#ifndef QT_NO_NETWORKINTERFACE
|
|
46 |
|
|
47 |
#include "qnetworkinterface_win_p.h"
|
|
48 |
#include <qhostinfo.h>
|
|
49 |
#include <qhash.h>
|
|
50 |
#include <qurl.h>
|
|
51 |
|
|
52 |
QT_BEGIN_NAMESPACE
|
|
53 |
|
|
54 |
typedef DWORD (WINAPI *PtrGetAdaptersInfo)(PIP_ADAPTER_INFO, PULONG);
|
|
55 |
static PtrGetAdaptersInfo ptrGetAdaptersInfo = 0;
|
|
56 |
typedef ULONG (WINAPI *PtrGetAdaptersAddresses)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
|
|
57 |
static PtrGetAdaptersAddresses ptrGetAdaptersAddresses = 0;
|
|
58 |
typedef DWORD (WINAPI *PtrGetNetworkParams)(PFIXED_INFO, PULONG);
|
|
59 |
static PtrGetNetworkParams ptrGetNetworkParams = 0;
|
|
60 |
|
|
61 |
static void resolveLibs()
|
|
62 |
{
|
|
63 |
// try to find the functions we need from Iphlpapi.dll
|
|
64 |
static bool done = false;
|
|
65 |
|
|
66 |
if (!done) {
|
|
67 |
done = true;
|
|
68 |
|
|
69 |
HINSTANCE iphlpapiHnd = LoadLibrary(L"iphlpapi");
|
|
70 |
if (iphlpapiHnd == NULL)
|
|
71 |
return;
|
|
72 |
|
|
73 |
#if defined(Q_OS_WINCE)
|
|
74 |
ptrGetAdaptersInfo = (PtrGetAdaptersInfo)GetProcAddress(iphlpapiHnd, L"GetAdaptersInfo");
|
|
75 |
ptrGetAdaptersAddresses = (PtrGetAdaptersAddresses)GetProcAddress(iphlpapiHnd, L"GetAdaptersAddresses");
|
|
76 |
ptrGetNetworkParams = (PtrGetNetworkParams)GetProcAddress(iphlpapiHnd, L"GetNetworkParams");
|
|
77 |
#else
|
|
78 |
ptrGetAdaptersInfo = (PtrGetAdaptersInfo)GetProcAddress(iphlpapiHnd, "GetAdaptersInfo");
|
|
79 |
ptrGetAdaptersAddresses = (PtrGetAdaptersAddresses)GetProcAddress(iphlpapiHnd, "GetAdaptersAddresses");
|
|
80 |
ptrGetNetworkParams = (PtrGetNetworkParams)GetProcAddress(iphlpapiHnd, "GetNetworkParams");
|
|
81 |
#endif
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
static QHostAddress addressFromSockaddr(sockaddr *sa)
|
|
86 |
{
|
|
87 |
QHostAddress address;
|
|
88 |
if (!sa)
|
|
89 |
return address;
|
|
90 |
|
|
91 |
if (sa->sa_family == AF_INET)
|
|
92 |
address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));
|
|
93 |
else if (sa->sa_family == AF_INET6)
|
|
94 |
address.setAddress(((qt_sockaddr_in6 *)sa)->sin6_addr.qt_s6_addr);
|
|
95 |
else
|
|
96 |
qWarning("Got unknown socket family %d", sa->sa_family);
|
|
97 |
return address;
|
|
98 |
|
|
99 |
}
|
|
100 |
|
|
101 |
static QHash<QHostAddress, QHostAddress> ipv4Netmasks()
|
|
102 |
{
|
|
103 |
//Retrieve all the IPV4 addresses & netmasks
|
|
104 |
IP_ADAPTER_INFO staticBuf[2]; // 2 is arbitrary
|
|
105 |
PIP_ADAPTER_INFO pAdapter = staticBuf;
|
|
106 |
ULONG bufSize = sizeof staticBuf;
|
|
107 |
QHash<QHostAddress, QHostAddress> ipv4netmasks;
|
|
108 |
|
|
109 |
DWORD retval = ptrGetAdaptersInfo(pAdapter, &bufSize);
|
|
110 |
if (retval == ERROR_BUFFER_OVERFLOW) {
|
|
111 |
// need more memory
|
|
112 |
pAdapter = (IP_ADAPTER_INFO *)qMalloc(bufSize);
|
|
113 |
if (!pAdapter)
|
|
114 |
return ipv4netmasks;
|
|
115 |
// try again
|
|
116 |
if (ptrGetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) {
|
|
117 |
qFree(pAdapter);
|
|
118 |
return ipv4netmasks;
|
|
119 |
}
|
|
120 |
} else if (retval != ERROR_SUCCESS) {
|
|
121 |
// error
|
|
122 |
return ipv4netmasks;
|
|
123 |
}
|
|
124 |
|
|
125 |
// iterate over the list and add the entries to our listing
|
|
126 |
for (PIP_ADAPTER_INFO ptr = pAdapter; ptr; ptr = ptr->Next) {
|
|
127 |
for (PIP_ADDR_STRING addr = &ptr->IpAddressList; addr; addr = addr->Next) {
|
|
128 |
QHostAddress address(QLatin1String(addr->IpAddress.String));
|
|
129 |
QHostAddress mask(QLatin1String(addr->IpMask.String));
|
|
130 |
ipv4netmasks[address] = mask;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
if (pAdapter != staticBuf)
|
|
134 |
qFree(pAdapter);
|
|
135 |
|
|
136 |
return ipv4netmasks;
|
|
137 |
|
|
138 |
}
|
|
139 |
|
|
140 |
static QList<QNetworkInterfacePrivate *> interfaceListingWinXP()
|
|
141 |
{
|
|
142 |
QList<QNetworkInterfacePrivate *> interfaces;
|
|
143 |
IP_ADAPTER_ADDRESSES staticBuf[2]; // 2 is arbitrary
|
|
144 |
PIP_ADAPTER_ADDRESSES pAdapter = staticBuf;
|
|
145 |
ULONG bufSize = sizeof staticBuf;
|
|
146 |
|
|
147 |
const QHash<QHostAddress, QHostAddress> &ipv4netmasks = ipv4Netmasks();
|
|
148 |
ULONG flags = GAA_FLAG_INCLUDE_ALL_INTERFACES |
|
|
149 |
GAA_FLAG_INCLUDE_PREFIX |
|
|
150 |
GAA_FLAG_SKIP_DNS_SERVER |
|
|
151 |
GAA_FLAG_SKIP_MULTICAST;
|
|
152 |
ULONG retval = ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize);
|
|
153 |
if (retval == ERROR_BUFFER_OVERFLOW) {
|
|
154 |
// need more memory
|
|
155 |
pAdapter = (IP_ADAPTER_ADDRESSES *)qMalloc(bufSize);
|
|
156 |
if (!pAdapter)
|
|
157 |
return interfaces;
|
|
158 |
// try again
|
|
159 |
if (ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) {
|
|
160 |
qFree(pAdapter);
|
|
161 |
return interfaces;
|
|
162 |
}
|
|
163 |
} else if (retval != ERROR_SUCCESS) {
|
|
164 |
// error
|
|
165 |
return interfaces;
|
|
166 |
}
|
|
167 |
|
|
168 |
// iterate over the list and add the entries to our listing
|
|
169 |
for (PIP_ADAPTER_ADDRESSES ptr = pAdapter; ptr; ptr = ptr->Next) {
|
|
170 |
QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
|
|
171 |
interfaces << iface;
|
|
172 |
|
|
173 |
iface->index = 0;
|
|
174 |
if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, Ipv6IfIndex) && ptr->Ipv6IfIndex != 0)
|
|
175 |
iface->index = ptr->Ipv6IfIndex;
|
|
176 |
else if (ptr->IfIndex != 0)
|
|
177 |
iface->index = ptr->IfIndex;
|
|
178 |
|
|
179 |
iface->flags = QNetworkInterface::CanBroadcast;
|
|
180 |
if (ptr->OperStatus == IfOperStatusUp)
|
|
181 |
iface->flags |= QNetworkInterface::IsUp | QNetworkInterface::IsRunning;
|
|
182 |
if ((ptr->Flags & IP_ADAPTER_NO_MULTICAST) == 0)
|
|
183 |
iface->flags |= QNetworkInterface::CanMulticast;
|
|
184 |
|
|
185 |
iface->name = QString::fromLocal8Bit(ptr->AdapterName);
|
|
186 |
iface->friendlyName = QString::fromWCharArray(ptr->FriendlyName);
|
|
187 |
if (ptr->PhysicalAddressLength)
|
|
188 |
iface->hardwareAddress = iface->makeHwAddress(ptr->PhysicalAddressLength,
|
|
189 |
ptr->PhysicalAddress);
|
|
190 |
else
|
|
191 |
// loopback if it has no address
|
|
192 |
iface->flags |= QNetworkInterface::IsLoopBack;
|
|
193 |
|
|
194 |
// The GetAdaptersAddresses call has an interesting semantic:
|
|
195 |
// It can return a number N of addresses and a number M of prefixes.
|
|
196 |
// But if you have IPv6 addresses, generally N > M.
|
|
197 |
// I cannot find a way to relate the Address to the Prefix, aside from stopping
|
|
198 |
// the iteration at the last Prefix entry and assume that it applies to all addresses
|
|
199 |
// from that point on.
|
|
200 |
PIP_ADAPTER_PREFIX pprefix = 0;
|
|
201 |
if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, FirstPrefix))
|
|
202 |
pprefix = ptr->FirstPrefix;
|
|
203 |
for (PIP_ADAPTER_UNICAST_ADDRESS addr = ptr->FirstUnicastAddress; addr; addr = addr->Next) {
|
|
204 |
QNetworkAddressEntry entry;
|
|
205 |
entry.setIp(addressFromSockaddr(addr->Address.lpSockaddr));
|
|
206 |
if (pprefix) {
|
|
207 |
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
|
208 |
entry.setNetmask(ipv4netmasks[entry.ip()]);
|
|
209 |
|
|
210 |
// broadcast address is set on postProcess()
|
|
211 |
} else { //IPV6
|
|
212 |
entry.setPrefixLength(pprefix->PrefixLength);
|
|
213 |
}
|
|
214 |
pprefix = pprefix->Next ? pprefix->Next : pprefix;
|
|
215 |
}
|
|
216 |
iface->addressEntries << entry;
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
if (pAdapter != staticBuf)
|
|
221 |
qFree(pAdapter);
|
|
222 |
|
|
223 |
return interfaces;
|
|
224 |
}
|
|
225 |
|
|
226 |
static QList<QNetworkInterfacePrivate *> interfaceListingWin2k()
|
|
227 |
{
|
|
228 |
QList<QNetworkInterfacePrivate *> interfaces;
|
|
229 |
IP_ADAPTER_INFO staticBuf[2]; // 2 is arbitrary
|
|
230 |
PIP_ADAPTER_INFO pAdapter = staticBuf;
|
|
231 |
ULONG bufSize = sizeof staticBuf;
|
|
232 |
|
|
233 |
DWORD retval = ptrGetAdaptersInfo(pAdapter, &bufSize);
|
|
234 |
if (retval == ERROR_BUFFER_OVERFLOW) {
|
|
235 |
// need more memory
|
|
236 |
pAdapter = (IP_ADAPTER_INFO *)qMalloc(bufSize);
|
|
237 |
if (!pAdapter)
|
|
238 |
return interfaces;
|
|
239 |
// try again
|
|
240 |
if (ptrGetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) {
|
|
241 |
qFree(pAdapter);
|
|
242 |
return interfaces;
|
|
243 |
}
|
|
244 |
} else if (retval != ERROR_SUCCESS) {
|
|
245 |
// error
|
|
246 |
return interfaces;
|
|
247 |
}
|
|
248 |
|
|
249 |
// iterate over the list and add the entries to our listing
|
|
250 |
for (PIP_ADAPTER_INFO ptr = pAdapter; ptr; ptr = ptr->Next) {
|
|
251 |
QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
|
|
252 |
interfaces << iface;
|
|
253 |
|
|
254 |
iface->index = ptr->Index;
|
|
255 |
iface->flags = QNetworkInterface::IsUp | QNetworkInterface::IsRunning;
|
|
256 |
if (ptr->Type == MIB_IF_TYPE_PPP)
|
|
257 |
iface->flags |= QNetworkInterface::IsPointToPoint;
|
|
258 |
else
|
|
259 |
iface->flags |= QNetworkInterface::CanBroadcast;
|
|
260 |
iface->name = QString::fromLocal8Bit(ptr->AdapterName);
|
|
261 |
iface->hardwareAddress = QNetworkInterfacePrivate::makeHwAddress(ptr->AddressLength,
|
|
262 |
ptr->Address);
|
|
263 |
|
|
264 |
for (PIP_ADDR_STRING addr = &ptr->IpAddressList; addr; addr = addr->Next) {
|
|
265 |
QNetworkAddressEntry entry;
|
|
266 |
entry.setIp(QHostAddress(QLatin1String(addr->IpAddress.String)));
|
|
267 |
entry.setNetmask(QHostAddress(QLatin1String(addr->IpMask.String)));
|
|
268 |
// broadcast address is set on postProcess()
|
|
269 |
|
|
270 |
iface->addressEntries << entry;
|
|
271 |
}
|
|
272 |
}
|
|
273 |
|
|
274 |
if (pAdapter != staticBuf)
|
|
275 |
qFree(pAdapter);
|
|
276 |
|
|
277 |
return interfaces;
|
|
278 |
}
|
|
279 |
|
|
280 |
static QList<QNetworkInterfacePrivate *> interfaceListing()
|
|
281 |
{
|
|
282 |
resolveLibs();
|
|
283 |
if (ptrGetAdaptersAddresses != NULL)
|
|
284 |
return interfaceListingWinXP();
|
|
285 |
else if (ptrGetAdaptersInfo != NULL)
|
|
286 |
return interfaceListingWin2k();
|
|
287 |
|
|
288 |
// failed
|
|
289 |
return QList<QNetworkInterfacePrivate *>();
|
|
290 |
}
|
|
291 |
|
|
292 |
QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan()
|
|
293 |
{
|
|
294 |
return interfaceListing();
|
|
295 |
}
|
|
296 |
|
|
297 |
QString QHostInfo::localDomainName()
|
|
298 |
{
|
|
299 |
resolveLibs();
|
|
300 |
if (ptrGetNetworkParams == NULL)
|
|
301 |
return QString(); // couldn't resolve
|
|
302 |
|
|
303 |
FIXED_INFO info, *pinfo;
|
|
304 |
ULONG bufSize = sizeof info;
|
|
305 |
pinfo = &info;
|
|
306 |
if (ptrGetNetworkParams(pinfo, &bufSize) == ERROR_BUFFER_OVERFLOW) {
|
|
307 |
pinfo = (FIXED_INFO *)qMalloc(bufSize);
|
|
308 |
if (!pinfo)
|
|
309 |
return QString();
|
|
310 |
// try again
|
|
311 |
if (ptrGetNetworkParams(pinfo, &bufSize) != ERROR_SUCCESS) {
|
|
312 |
qFree(pinfo);
|
|
313 |
return QString(); // error
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
QString domainName = QUrl::fromAce(pinfo->DomainName);
|
|
318 |
|
|
319 |
if (pinfo != &info)
|
|
320 |
qFree(pinfo);
|
|
321 |
|
|
322 |
return domainName;
|
|
323 |
}
|
|
324 |
|
|
325 |
QT_END_NAMESPACE
|
|
326 |
|
|
327 |
#endif // QT_NO_NETWORKINTERFACE
|