src/network/kernel/qhostaddress.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 #ifndef QHOSTADDRESS_H
       
    43 #define QHOSTADDRESS_H
       
    44 
       
    45 #include <QtCore/qpair.h>
       
    46 #include <QtCore/qstring.h>
       
    47 #include <QtCore/qscopedpointer.h>
       
    48 #include <QtNetwork/qabstractsocket.h>
       
    49 
       
    50 struct sockaddr;
       
    51 
       
    52 QT_BEGIN_HEADER
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 QT_MODULE(Network)
       
    57 
       
    58 class QHostAddressPrivate;
       
    59 
       
    60 class Q_NETWORK_EXPORT QIPv6Address
       
    61 {
       
    62 public:
       
    63     inline quint8 &operator [](int index) { return c[index]; }
       
    64     inline quint8 operator [](int index) const { return c[index]; }
       
    65     quint8 c[16];
       
    66 };
       
    67 
       
    68 typedef QIPv6Address Q_IPV6ADDR;
       
    69 
       
    70 class Q_NETWORK_EXPORT QHostAddress
       
    71 {
       
    72 public:
       
    73     enum SpecialAddress {
       
    74         Null,
       
    75         Broadcast,
       
    76         LocalHost,
       
    77         LocalHostIPv6,
       
    78         Any,
       
    79         AnyIPv6
       
    80     };
       
    81 
       
    82     QHostAddress();
       
    83     explicit QHostAddress(quint32 ip4Addr);
       
    84     explicit QHostAddress(quint8 *ip6Addr);
       
    85     explicit QHostAddress(const Q_IPV6ADDR &ip6Addr);
       
    86     explicit QHostAddress(const sockaddr *sockaddr);
       
    87     explicit QHostAddress(const QString &address);
       
    88     QHostAddress(const QHostAddress &copy);
       
    89     QHostAddress(SpecialAddress address);
       
    90     ~QHostAddress();
       
    91 
       
    92     QHostAddress &operator=(const QHostAddress &other);
       
    93     QHostAddress &operator=(const QString &address);
       
    94 
       
    95     void setAddress(quint32 ip4Addr);
       
    96     void setAddress(quint8 *ip6Addr);
       
    97     void setAddress(const Q_IPV6ADDR &ip6Addr);
       
    98     void setAddress(const sockaddr *sockaddr);
       
    99     bool setAddress(const QString &address);
       
   100 
       
   101     QAbstractSocket::NetworkLayerProtocol protocol() const;
       
   102     quint32 toIPv4Address() const;
       
   103     Q_IPV6ADDR toIPv6Address() const;
       
   104 
       
   105     QString toString() const;
       
   106 
       
   107     QString scopeId() const;
       
   108     void setScopeId(const QString &id);
       
   109 
       
   110     bool operator ==(const QHostAddress &address) const;
       
   111     bool operator ==(SpecialAddress address) const;
       
   112     inline bool operator !=(const QHostAddress &address) const
       
   113     { return !operator==(address); }
       
   114     inline bool operator !=(SpecialAddress address) const
       
   115     { return !operator==(address); }
       
   116     bool isNull() const;
       
   117     void clear();
       
   118 
       
   119 #ifdef QT3_SUPPORT
       
   120     inline QT3_SUPPORT quint32 ip4Addr() const { return toIPv4Address(); }
       
   121     inline QT3_SUPPORT bool isIPv4Address() const { return protocol() == QAbstractSocket::IPv4Protocol
       
   122                                                       || protocol() == QAbstractSocket::UnknownNetworkLayerProtocol; }
       
   123     inline QT3_SUPPORT bool isIp4Addr() const  { return protocol() == QAbstractSocket::IPv4Protocol
       
   124                                                       || protocol() == QAbstractSocket::UnknownNetworkLayerProtocol; }
       
   125     inline QT3_SUPPORT bool isIPv6Address() const { return protocol() == QAbstractSocket::IPv6Protocol; }
       
   126 #endif
       
   127 
       
   128     bool isInSubnet(const QHostAddress &subnet, int netmask) const;
       
   129     bool isInSubnet(const QPair<QHostAddress, int> &subnet) const;
       
   130 
       
   131     static QPair<QHostAddress, int> parseSubnet(const QString &subnet);
       
   132 
       
   133 protected:
       
   134     QScopedPointer<QHostAddressPrivate> d;
       
   135 };
       
   136 
       
   137 inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2)
       
   138 { return address2 == address1; }
       
   139 
       
   140 #ifndef QT_NO_DEBUG_STREAM
       
   141 Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &);
       
   142 #endif
       
   143 
       
   144 
       
   145 Q_NETWORK_EXPORT uint qHash(const QHostAddress &key);
       
   146 
       
   147 #ifndef QT_NO_DATASTREAM
       
   148 Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &);
       
   149 Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &);
       
   150 #endif
       
   151 
       
   152 QT_END_NAMESPACE
       
   153 
       
   154 QT_END_HEADER
       
   155 #endif // QHOSTADDRESS_H