qhostaddress.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtNetwork module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QHOSTADDRESS_H
00043 #define QHOSTADDRESS_H
00044 
00045 #include <QtCore/qpair.h>
00046 #include <QtCore/qstring.h>
00047 #include <QtCore/qscopedpointer.h>
00048 #include <QtNetwork/qabstractsocket.h>
00049 
00050 struct sockaddr;
00051 
00052 QT_BEGIN_HEADER
00053 
00054 QT_BEGIN_NAMESPACE
00055 
00056 QT_MODULE(Network)
00057 
00058 class QHostAddressPrivate;
00059 
00060 class Q_NETWORK_EXPORT QIPv6Address
00061 {
00062 public:
00063     inline quint8 &operator [](int index) { return c[index]; }
00064     inline quint8 operator [](int index) const { return c[index]; }
00065     quint8 c[16];
00066 };
00067 
00068 typedef QIPv6Address Q_IPV6ADDR;
00069 
00070 class Q_NETWORK_EXPORT QHostAddress
00071 {
00072 public:
00073     enum SpecialAddress {
00074         Null,
00075         Broadcast,
00076         LocalHost,
00077         LocalHostIPv6,
00078         Any,
00079         AnyIPv6
00080     };
00081 
00082     QHostAddress();
00083     explicit QHostAddress(quint32 ip4Addr);
00084     explicit QHostAddress(quint8 *ip6Addr);
00085     explicit QHostAddress(const Q_IPV6ADDR &ip6Addr);
00086     explicit QHostAddress(const sockaddr *sockaddr);
00087     explicit QHostAddress(const QString &address);
00088     QHostAddress(const QHostAddress &copy);
00089     QHostAddress(SpecialAddress address);
00090     ~QHostAddress();
00091 
00092     QHostAddress &operator=(const QHostAddress &other);
00093     QHostAddress &operator=(const QString &address);
00094 
00095     void setAddress(quint32 ip4Addr);
00096     void setAddress(quint8 *ip6Addr);
00097     void setAddress(const Q_IPV6ADDR &ip6Addr);
00098     void setAddress(const sockaddr *sockaddr);
00099     bool setAddress(const QString &address);
00100 
00101     QAbstractSocket::NetworkLayerProtocol protocol() const;
00102     quint32 toIPv4Address() const;
00103     Q_IPV6ADDR toIPv6Address() const;
00104 
00105     QString toString() const;
00106 
00107     QString scopeId() const;
00108     void setScopeId(const QString &id);
00109 
00110     bool operator ==(const QHostAddress &address) const;
00111     bool operator ==(SpecialAddress address) const;
00112     inline bool operator !=(const QHostAddress &address) const
00113     { return !operator==(address); }
00114     inline bool operator !=(SpecialAddress address) const
00115     { return !operator==(address); }
00116     bool isNull() const;
00117     void clear();
00118 
00119 #ifdef QT3_SUPPORT
00120     inline QT3_SUPPORT quint32 ip4Addr() const { return toIPv4Address(); }
00121     inline QT3_SUPPORT bool isIPv4Address() const { return protocol() == QAbstractSocket::IPv4Protocol
00122                                                       || protocol() == QAbstractSocket::UnknownNetworkLayerProtocol; }
00123     inline QT3_SUPPORT bool isIp4Addr() const  { return protocol() == QAbstractSocket::IPv4Protocol
00124                                                       || protocol() == QAbstractSocket::UnknownNetworkLayerProtocol; }
00125     inline QT3_SUPPORT bool isIPv6Address() const { return protocol() == QAbstractSocket::IPv6Protocol; }
00126 #endif
00127 
00128     bool isInSubnet(const QHostAddress &subnet, int netmask) const;
00129     bool isInSubnet(const QPair<QHostAddress, int> &subnet) const;
00130 
00131     static QPair<QHostAddress, int> parseSubnet(const QString &subnet);
00132 
00133 protected:
00134     QScopedPointer<QHostAddressPrivate> d;
00135 };
00136 
00137 inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2)
00138 { return address2 == address1; }
00139 
00140 #ifndef QT_NO_DEBUG_STREAM
00141 Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &);
00142 #endif
00143 
00144 
00145 Q_NETWORK_EXPORT uint qHash(const QHostAddress &key);
00146 
00147 #ifndef QT_NO_DATASTREAM
00148 Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &);
00149 Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &);
00150 #endif
00151 
00152 QT_END_NAMESPACE
00153 
00154 QT_END_HEADER
00155 #endif // QHOSTADDRESS_H