Qt 4.8
qgenericengine.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 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, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qgenericengine.h"
43 #include "../qnetworksession_impl.h"
44 
45 #include <QtNetwork/private/qnetworkconfiguration_p.h>
46 
47 #include <QtCore/qthread.h>
48 #include <QtCore/qmutex.h>
49 #include <QtCore/qcoreapplication.h>
50 #include <QtCore/qstringlist.h>
51 
52 #include <QtCore/qdebug.h>
53 #include <QtCore/private/qcoreapplication_p.h>
54 
55 #ifdef Q_OS_WIN
56 #include "../platformdefs_win.h"
57 #endif
58 
59 #ifdef Q_OS_LINUX
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <unistd.h>
65 #endif
66 
67 #ifndef QT_NO_BEARERMANAGEMENT
68 
70 
71 #ifndef QT_NO_NETWORKINTERFACE
73 {
74 #ifdef Q_OS_WIN32
75  unsigned long oid;
76  DWORD bytesWritten;
77 
78  NDIS_MEDIUM medium;
79  NDIS_PHYSICAL_MEDIUM physicalMedium;
80 
81  HANDLE handle = CreateFile((TCHAR *)QString::fromLatin1("\\\\.\\%1").arg(interface).utf16(), 0,
82  FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
83  if (handle == INVALID_HANDLE_VALUE)
85 
87  bytesWritten = 0;
88  bool result = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid),
89  &medium, sizeof(medium), &bytesWritten, 0);
90  if (!result) {
91  CloseHandle(handle);
93  }
94 
96  bytesWritten = 0;
97  result = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid),
98  &physicalMedium, sizeof(physicalMedium), &bytesWritten, 0);
99  if (!result) {
100  CloseHandle(handle);
101 
102  if (medium == NdisMedium802_3)
104  else
106  }
107 
108  CloseHandle(handle);
109 
110  if (medium == NdisMedium802_3) {
111  switch (physicalMedium) {
118  default:
119 #ifdef BEARER_MANAGEMENT_DEBUG
120  qDebug() << "Physical Medium" << physicalMedium;
121 #endif
123  }
124  }
125 
126 #ifdef BEARER_MANAGEMENT_DEBUG
127  qDebug() << medium << physicalMedium;
128 #endif
129 #elif defined(Q_OS_LINUX)
130  int sock = socket(AF_INET, SOCK_DGRAM, 0);
131 
132  ifreq request;
133  strncpy(request.ifr_name, interface.toLocal8Bit().data(), sizeof(request.ifr_name));
134  int result = ioctl(sock, SIOCGIFHWADDR, &request);
135  close(sock);
136 
137  if (result >= 0 && request.ifr_hwaddr.sa_family == ARPHRD_ETHER)
139 #else
140  Q_UNUSED(interface);
141 #endif
142 
144 }
145 #endif
146 
148 : QBearerEngineImpl(parent)
149 {
150  //workaround for deadlock in __cxa_guard_acquire with webkit on macos x
151  //initialise the Q_GLOBAL_STATIC in same thread as the AtomicallyInitializedStatic
153 }
154 
156 {
157 }
158 
160 {
161  QMutexLocker locker(&mutex);
162 
163  return configurationInterface.value(id);
164 }
165 
167 {
168  QMutexLocker locker(&mutex);
169 
170  return configurationInterface.contains(id);
171 }
172 
174 {
176 }
177 
179 {
181 }
182 
184 {
185  doRequestUpdate();
186 }
187 
189 {
190  doRequestUpdate();
191 }
192 
194 {
195 #ifndef QT_NO_NETWORKINTERFACE
196  QMutexLocker locker(&mutex);
197 
198  // Immediately after connecting with a wireless access point
199  // QNetworkInterface::allInterfaces() will sometimes return an empty list. Calling it again a
200  // second time results in a non-empty list. If we loose interfaces we will end up removing
201  // network configurations which will break current sessions.
203  if (interfaces.isEmpty())
204  interfaces = QNetworkInterface::allInterfaces();
205 
206  QStringList previous = accessPointConfigurations.keys();
207 
208  // create configuration for each interface
209  while (!interfaces.isEmpty()) {
210  QNetworkInterface interface = interfaces.takeFirst();
211 
212  if (!interface.isValid())
213  continue;
214 
215  // ignore loopback interface
216  if (interface.flags() & QNetworkInterface::IsLoopBack)
217  continue;
218 
219  // ignore WLAN interface handled in separate engine
220  if (qGetInterfaceType(interface.name()) == QNetworkConfiguration::BearerWLAN)
221  continue;
222 
223  uint identifier;
224  if (interface.index())
225  identifier = qHash(QLatin1String("generic:") + QString::number(interface.index()));
226  else
227  identifier = qHash(QLatin1String("generic:") + interface.hardwareAddress());
228 
229  const QString id = QString::number(identifier);
230 
231  previous.removeAll(id);
232 
233  QString name = interface.humanReadableName();
234  if (name.isEmpty())
235  name = interface.name();
236 
237  QNetworkConfiguration::StateFlags state = QNetworkConfiguration::Defined;
238  if ((interface.flags() & QNetworkInterface::IsRunning) && !interface.addressEntries().isEmpty())
240 
241  if (accessPointConfigurations.contains(id)) {
243 
244  bool changed = false;
245 
246  ptr->mutex.lock();
247 
248  if (!ptr->isValid) {
249  ptr->isValid = true;
250  changed = true;
251  }
252 
253  if (ptr->name != name) {
254  ptr->name = name;
255  changed = true;
256  }
257 
258  if (ptr->id != id) {
259  ptr->id = id;
260  changed = true;
261  }
262 
263  if (ptr->state != state) {
264  ptr->state = state;
265  changed = true;
266  }
267 
268  ptr->mutex.unlock();
269 
270  if (changed) {
271  locker.unlock();
273  locker.relock();
274  }
275  } else {
277 
278  ptr->name = name;
279  ptr->isValid = true;
280  ptr->id = id;
281  ptr->state = state;
283  ptr->bearerType = qGetInterfaceType(interface.name());
284 
285  accessPointConfigurations.insert(id, ptr);
286  configurationInterface.insert(id, interface.name());
287 
288  locker.unlock();
290  locker.relock();
291  }
292  }
293 
294  while (!previous.isEmpty()) {
296  accessPointConfigurations.take(previous.takeFirst());
297 
299 
300  locker.unlock();
302  locker.relock();
303  }
304 
305  locker.unlock();
306 #endif
307 
309 }
310 
312 {
313  QMutexLocker locker(&mutex);
314 
316 
317  if (!ptr)
319 
320  QMutexLocker configLocker(&ptr->mutex);
321 
322  if (!ptr->isValid) {
326  } else if ((ptr->state & QNetworkConfiguration::Discovered) ==
331  } else if ((ptr->state & QNetworkConfiguration::Undefined) ==
334  }
335 
337 }
338 
339 QNetworkConfigurationManager::Capabilities QGenericEngine::capabilities() const
340 {
342 }
343 
345 {
346  return new QNetworkSessionPrivateImpl;
347 }
348 
350 {
352 }
353 
354 
356 {
357  return true;
358 }
359 
361 
362 #endif // QT_NO_BEARERMANAGEMENT
BearerType
Specifies the type of bearer used by a configuration.
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
uint qHash(const QProcEnvKey &key)
Definition: qprocess_p.h:96
bool requiresPolling() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void lock()
Locks the mutex.
Definition: qmutex.cpp:151
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define IOCTL_NDIS_QUERY_GLOBAL_STATS
QNetworkConfiguration::StateFlags state
static QNetworkConfiguration::BearerType qGetInterfaceType(const QString &interface)
void configurationChanged(QNetworkConfigurationPrivatePointer config)
void unlock()
Unlocks this mutex locker.
Definition: qmutex.h:117
void updateCompleted()
QNetworkSession::State sessionStateForId(const QString &id)
State
This enum describes the connectivity state of the session.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QString getInterfaceFromId(const QString &id)
The QString class provides a Unicode character string.
Definition: qstring.h:83
QNetworkSessionPrivate * createSessionBackend()
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
NDIS_PHYSICAL_MEDIUM
#define OID_GEN_PHYSICAL_MEDIUM
QNetworkConfiguration::BearerType bearerType
Q_INVOKABLE void requestUpdate()
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void relock()
Relocks an unlocked mutex locker.
Definition: qmutex.h:125
Q_CORE_EXPORT void qDebug(const char *,...)
bool hasIdentifier(const QString &id)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QHash< QString, QNetworkConfigurationPrivatePointer > accessPointConfigurations
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void disconnectFromId(const QString &id)
const char * name
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
#define emit
Definition: qobjectdefs.h:76
QGenericEngine(QObject *parent=0)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void configurationAdded(QNetworkConfigurationPrivatePointer config)
unsigned int uint
Definition: qglobal.h:996
QNetworkConfigurationManager::Capabilities capabilities() const
const T * ptr(const T &t)
void * HANDLE
Definition: qnamespace.h:1671
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
void unlock()
Unlocks the mutex.
Definition: qmutex.cpp:296
NDIS_MEDIUM
Q_INVOKABLE void initialize()
static QNetworkInterface interfaceFromIndex(int index)
Returns a QNetworkInterface object for the interface whose internal ID is index.
QNetworkConfigurationPrivatePointer defaultConfiguration()
int remove(const Key &key)
Removes all the items that have the key key from the map.
Definition: qmap.h:662
void connectToId(const QString &id)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QExplicitlySharedDataPointer< QNetworkConfigurationPrivate > QNetworkConfigurationPrivatePointer
QNetworkConfiguration::Type type
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
static QList< QNetworkInterface > allInterfaces()
Returns a listing of all the network interfaces found on the host machine.
void configurationRemoved(QNetworkConfigurationPrivatePointer config)
bool contains(const Key &key) const
Returns true if the map contains an item with key key; otherwise returns false.
Definition: qmap.h:553
#define OID_GEN_MEDIA_SUPPORTED
void connectionError(const QString &id, QBearerEngineImpl::ConnectionError error)
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QMap< QString, QString > configurationInterface
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
int removeAll(const T &t)
Removes all occurrences of value in the list and returns the number of entries removed.
Definition: qlist.h:770