📄 qnetworkinterface.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtNetwork module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qnetworkinterface.h"#include "qnetworkinterface_p.h"#include "qdebug.h"static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list){ // Some platforms report a netmask but don't report a broadcast address // Go through all available addresses and calculate the broadcast address // from the IP and the netmask // // This is an IPv4-only thing -- IPv6 has no concept of broadcasts // The math is: // broadcast = IP | ~netmask QList<QNetworkInterfacePrivate *>::Iterator it = list.begin(); const QList<QNetworkInterfacePrivate *>::Iterator end = list.end(); for ( ; it != end; ++it) { QList<QNetworkAddressEntry>::Iterator addr_it = (*it)->addressEntries.begin(); const QList<QNetworkAddressEntry>::Iterator addr_end = (*it)->addressEntries.end(); for ( ; addr_it != addr_end; ++addr_it) { if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol) continue; if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) { QHostAddress bcast = addr_it->ip(); bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address()); addr_it->setBroadcast(bcast); } } } return list;}Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager)QNetworkInterfaceManager::QNetworkInterfaceManager(){}QNetworkInterfaceManager::~QNetworkInterfaceManager(){}QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name){ QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces(); QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin(); for ( ; it != interfaceList.constEnd(); ++it) if ((*it)->name == name) return *it; return empty;}QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index){ QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces(); QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin(); for ( ; it != interfaceList.constEnd(); ++it) if ((*it)->index == index) return *it; return empty;}QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces(){ QList<QNetworkInterfacePrivate *> list = postProcess(scan()); QList<QSharedDataPointer<QNetworkInterfacePrivate> > result; foreach (QNetworkInterfacePrivate *ptr, list) result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr); return result;}QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data){ QString result; for (int i = 0; i < len; ++i) { if (i) result += QLatin1Char(':'); char buf[3];#if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400 sprintf_s(buf, 3, "%02hX", ushort(data[i]));#else sprintf(buf, "%02hX", ushort(data[i]));#endif result += QLatin1String(buf); } return result;}/*! \class QNetworkAddressEntry \brief The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address. \since 4.2 \reentrant \ingroup io Each network interface can contain zero or more IP addresses, which in turn can be associated with a netmask and/or a broadcast address (depending on support from the operating system). This class represents one such group.*//*! Constructs an empty QNetworkAddressEntry object.*/QNetworkAddressEntry::QNetworkAddressEntry() : d(new QNetworkAddressEntryPrivate){}/*! Constructs a QNetworkAddressEntry object that is a copy of the object \a other.*/QNetworkAddressEntry::QNetworkAddressEntry(const QNetworkAddressEntry &other) : d(new QNetworkAddressEntryPrivate(*other.d)){}/*! Makes a copy of the QNetworkAddressEntry object \a other.*/QNetworkAddressEntry &QNetworkAddressEntry::operator=(const QNetworkAddressEntry &other){ *d = *other.d; return *this;}/*! Destroys this QNetworkAddressEntry object.*/QNetworkAddressEntry::~QNetworkAddressEntry(){ delete d;}/*! This function returns one IPv4 or IPv6 address found, that was found in a network interface.*/QHostAddress QNetworkAddressEntry::ip() const{ return d->address;}/*! Sets the IP address the QNetworkAddressEntry object contains to \a newIp.*/void QNetworkAddressEntry::setIp(const QHostAddress &newIp){ d->address = newIp;}/*! Returns the netmask associated with the IP address. The netmask is expressed in the form of an IP address, such as 255.255.0.0. For IPv6 addresses, the prefix length is converted to an address where the number of bits set to 1 is equal to the prefix length. For a prefix length of 64 bits (the most common value), the netmask will be expressed as a QHostAddress holding the address FFFF:FFFF:FFFF:FFFF::*/QHostAddress QNetworkAddressEntry::netmask() const{ return d->netmask;}/*! Sets the netmask that this QNetworkAddressEntry object contains to \a newNetmask.*/void QNetworkAddressEntry::setNetmask(const QHostAddress &newNetmask){ d->netmask = newNetmask;}/*! Returns the broadcast address associated with the IPv4 address and netmask. It can usually be derived from those two by setting to 1 the bits of the IP address where the netmask contains a 0. (In other words, by bitwise-OR'ing the IP address with the inverse of the netmask) This member is always empty for IPv6 addresses, since the concept of broadcast has been abandoned in that system in favor of multicast. In particular, the group of hosts corresponding to all the nodes in the local network can be reached by the "all-nodes" special multicast group (address FF02::1).*/QHostAddress QNetworkAddressEntry::broadcast() const{ return d->broadcast;}/*! Sets the broadcast IP address of this QNetworkAddressEntry object to \a newBroadcast.*/void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast){ d->broadcast = newBroadcast;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -