| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qnetworkinterface.h"
|
|---|
| 43 | #include "qnetworkinterface_p.h"
|
|---|
| 44 |
|
|---|
| 45 | #include "qdebug.h"
|
|---|
| 46 | #include "qendian.h"
|
|---|
| 47 |
|
|---|
| 48 | #ifndef QT_NO_NETWORKINTERFACE
|
|---|
| 49 |
|
|---|
| 50 | QT_BEGIN_NAMESPACE
|
|---|
| 51 |
|
|---|
| 52 | static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list)
|
|---|
| 53 | {
|
|---|
| 54 | // Some platforms report a netmask but don't report a broadcast address
|
|---|
| 55 | // Go through all available addresses and calculate the broadcast address
|
|---|
| 56 | // from the IP and the netmask
|
|---|
| 57 | //
|
|---|
| 58 | // This is an IPv4-only thing -- IPv6 has no concept of broadcasts
|
|---|
| 59 | // The math is:
|
|---|
| 60 | // broadcast = IP | ~netmask
|
|---|
| 61 |
|
|---|
| 62 | QList<QNetworkInterfacePrivate *>::Iterator it = list.begin();
|
|---|
| 63 | const QList<QNetworkInterfacePrivate *>::Iterator end = list.end();
|
|---|
| 64 | for ( ; it != end; ++it) {
|
|---|
| 65 | QList<QNetworkAddressEntry>::Iterator addr_it = (*it)->addressEntries.begin();
|
|---|
| 66 | const QList<QNetworkAddressEntry>::Iterator addr_end = (*it)->addressEntries.end();
|
|---|
| 67 | for ( ; addr_it != addr_end; ++addr_it) {
|
|---|
| 68 | if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol)
|
|---|
| 69 | continue;
|
|---|
| 70 |
|
|---|
| 71 | if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) {
|
|---|
| 72 | QHostAddress bcast = addr_it->ip();
|
|---|
| 73 | bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address());
|
|---|
| 74 | addr_it->setBroadcast(bcast);
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return list;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager)
|
|---|
| 83 |
|
|---|
| 84 | QNetworkInterfaceManager::QNetworkInterfaceManager()
|
|---|
| 85 | {
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | QNetworkInterfaceManager::~QNetworkInterfaceManager()
|
|---|
| 89 | {
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name)
|
|---|
| 93 | {
|
|---|
| 94 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
|
|---|
| 95 | QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
|
|---|
| 96 | for ( ; it != interfaceList.constEnd(); ++it)
|
|---|
| 97 | if ((*it)->name == name)
|
|---|
| 98 | return *it;
|
|---|
| 99 |
|
|---|
| 100 | return empty;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index)
|
|---|
| 104 | {
|
|---|
| 105 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
|
|---|
| 106 | QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
|
|---|
| 107 | for ( ; it != interfaceList.constEnd(); ++it)
|
|---|
| 108 | if ((*it)->index == index)
|
|---|
| 109 | return *it;
|
|---|
| 110 |
|
|---|
| 111 | return empty;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
|
|---|
| 115 | {
|
|---|
| 116 | QList<QNetworkInterfacePrivate *> list = postProcess(scan());
|
|---|
| 117 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;
|
|---|
| 118 |
|
|---|
| 119 | foreach (QNetworkInterfacePrivate *ptr, list)
|
|---|
| 120 | result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
|
|---|
| 121 |
|
|---|
| 122 | return result;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data)
|
|---|
| 126 | {
|
|---|
| 127 | QString result;
|
|---|
| 128 | for (int i = 0; i < len; ++i) {
|
|---|
| 129 | if (i)
|
|---|
| 130 | result += QLatin1Char(':');
|
|---|
| 131 |
|
|---|
| 132 | char buf[3];
|
|---|
| 133 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && defined(_MSC_VER) && _MSC_VER >= 1400
|
|---|
| 134 | sprintf_s(buf, 3, "%02hX", ushort(data[i]));
|
|---|
| 135 | #else
|
|---|
| 136 | sprintf(buf, "%02hX", ushort(data[i]));
|
|---|
| 137 | #endif
|
|---|
| 138 | result += QLatin1String(buf);
|
|---|
| 139 | }
|
|---|
| 140 | return result;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /*!
|
|---|
| 144 | \class QNetworkAddressEntry
|
|---|
| 145 | \brief The QNetworkAddressEntry class stores one IP address
|
|---|
| 146 | supported by a network interface, along with its associated
|
|---|
| 147 | netmask and broadcast address.
|
|---|
| 148 |
|
|---|
| 149 | \since 4.2
|
|---|
|
|---|