| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtNetwork module 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QABSTRACTSOCKET_H |
| 41 | #define QABSTRACTSOCKET_H |
| 42 | |
| 43 | #include <QtNetwork/qtnetworkglobal.h> |
| 44 | #include <QtCore/qiodevice.h> |
| 45 | #include <QtCore/qobject.h> |
| 46 | #ifndef QT_NO_DEBUG_STREAM |
| 47 | #include <QtCore/qdebug.h> |
| 48 | #endif |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | |
| 53 | class QHostAddress; |
| 54 | #ifndef QT_NO_NETWORKPROXY |
| 55 | class QNetworkProxy; |
| 56 | #endif |
| 57 | class QAbstractSocketPrivate; |
| 58 | class QAuthenticator; |
| 59 | |
| 60 | class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | Q_MOC_INCLUDE(<QtNetwork/qauthenticator.h>) |
| 64 | |
| 65 | public: |
| 66 | enum SocketType { |
| 67 | TcpSocket, |
| 68 | UdpSocket, |
| 69 | SctpSocket, |
| 70 | UnknownSocketType = -1 |
| 71 | }; |
| 72 | Q_ENUM(SocketType) |
| 73 | enum NetworkLayerProtocol { |
| 74 | IPv4Protocol, |
| 75 | IPv6Protocol, |
| 76 | AnyIPProtocol, |
| 77 | UnknownNetworkLayerProtocol = -1 |
| 78 | }; |
| 79 | Q_ENUM(NetworkLayerProtocol) |
| 80 | enum SocketError { |
| 81 | ConnectionRefusedError, |
| 82 | RemoteHostClosedError, |
| 83 | HostNotFoundError, |
| 84 | SocketAccessError, |
| 85 | SocketResourceError, |
| 86 | SocketTimeoutError, /* 5 */ |
| 87 | DatagramTooLargeError, |
| 88 | NetworkError, |
| 89 | AddressInUseError, |
| 90 | SocketAddressNotAvailableError, |
| 91 | UnsupportedSocketOperationError, /* 10 */ |
| 92 | UnfinishedSocketOperationError, |
| 93 | ProxyAuthenticationRequiredError, |
| 94 | SslHandshakeFailedError, |
| 95 | ProxyConnectionRefusedError, |
| 96 | ProxyConnectionClosedError, /* 15 */ |
| 97 | ProxyConnectionTimeoutError, |
| 98 | ProxyNotFoundError, |
| 99 | ProxyProtocolError, |
| 100 | OperationError, |
| 101 | SslInternalError, /* 20 */ |
| 102 | SslInvalidUserDataError, |
| 103 | TemporaryError, |
| 104 | |
| 105 | UnknownSocketError = -1 |
| 106 | }; |
| 107 | Q_ENUM(SocketError) |
| 108 | enum SocketState { |
| 109 | UnconnectedState, |
| 110 | HostLookupState, |
| 111 | ConnectingState, |
| 112 | ConnectedState, |
| 113 | BoundState, |
| 114 | ListeningState, |
| 115 | ClosingState |
| 116 | }; |
| 117 | Q_ENUM(SocketState) |
| 118 | enum SocketOption { |
| 119 | LowDelayOption, // TCP_NODELAY |
| 120 | KeepAliveOption, // SO_KEEPALIVE |
| 121 | MulticastTtlOption, // IP_MULTICAST_TTL |
| 122 | MulticastLoopbackOption, // IP_MULTICAST_LOOPBACK |
| 123 | TypeOfServiceOption, //IP_TOS |
| 124 | SendBufferSizeSocketOption, //SO_SNDBUF |
| 125 | ReceiveBufferSizeSocketOption, //SO_RCVBUF |
| 126 | PathMtuSocketOption // IP_MTU |
| 127 | }; |
| 128 | Q_ENUM(SocketOption) |
| 129 | enum BindFlag { |
| 130 | DefaultForPlatform = 0x0, |
| 131 | ShareAddress = 0x1, |
| 132 | DontShareAddress = 0x2, |
| 133 | ReuseAddressHint = 0x4 |
| 134 | }; |
| 135 | Q_DECLARE_FLAGS(BindMode, BindFlag) |
| 136 | enum PauseMode { |
| 137 | PauseNever = 0x0, |
| 138 | PauseOnSslErrors = 0x1 |
| 139 | }; |
| 140 | Q_DECLARE_FLAGS(PauseModes, PauseMode) |
| 141 | |
| 142 | QAbstractSocket(SocketType socketType, QObject *parent); |
| 143 | virtual ~QAbstractSocket(); |
| 144 | |
| 145 | virtual void resume(); // to continue after proxy authentication required, SSL errors etc. |
| 146 | PauseModes pauseMode() const; |
| 147 | void setPauseMode(PauseModes pauseMode); |
| 148 | |
| 149 | virtual bool bind(const QHostAddress &address, quint16 port = 0, |
| 150 | BindMode mode = DefaultForPlatform); |
| 151 | bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform); |
| 152 | |
| 153 | virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol); |
| 154 | void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite); |
| 155 | virtual void disconnectFromHost(); |
| 156 | |
| 157 | bool isValid() const; |
| 158 | |
| 159 | qint64 bytesAvailable() const override; |
| 160 | qint64 bytesToWrite() const override; |
| 161 | |
| 162 | quint16 localPort() const; |
| 163 | QHostAddress localAddress() const; |
| 164 | quint16 peerPort() const; |
| 165 | QHostAddress peerAddress() const; |
| 166 | QString peerName() const; |
| 167 | |
| 168 | qint64 readBufferSize() const; |
| 169 | virtual void setReadBufferSize(qint64 size); |
| 170 | |
| 171 | void abort(); |
| 172 | |
| 173 | virtual qintptr socketDescriptor() const; |
| 174 | virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState, |
| 175 | OpenMode openMode = ReadWrite); |
| 176 | |
| 177 | virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value); |
| 178 | virtual QVariant socketOption(QAbstractSocket::SocketOption option); |
| 179 | |
| 180 | SocketType socketType() const; |
| 181 | SocketState state() const; |
| 182 | SocketError error() const; |
| 183 | |
| 184 | // from QIODevice |
| 185 | void close() override; |
| 186 | bool isSequential() const override; |
| 187 | bool flush(); |
| 188 | |
| 189 | // for synchronous access |
| 190 | virtual bool waitForConnected(int msecs = 30000); |
| 191 | bool waitForReadyRead(int msecs = 30000) override; |
| 192 | bool waitForBytesWritten(int msecs = 30000) override; |
| 193 | virtual bool waitForDisconnected(int msecs = 30000); |
| 194 | |
| 195 | #ifndef QT_NO_NETWORKPROXY |
| 196 | void setProxy(const QNetworkProxy &networkProxy); |
| 197 | QNetworkProxy proxy() const; |
| 198 | QString protocolTag() const; |
| 199 | void setProtocolTag(const QString &tag); |
| 200 | #endif |
| 201 | |
| 202 | Q_SIGNALS: |
| 203 | void hostFound(); |
| 204 | void connected(); |
| 205 | void disconnected(); |
| 206 | void stateChanged(QAbstractSocket::SocketState); |
| 207 | void errorOccurred(QAbstractSocket::SocketError); |
| 208 | #ifndef QT_NO_NETWORKPROXY |
| 209 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator); |
| 210 | #endif |
| 211 | |
| 212 | protected: |
| 213 | qint64 readData(char *data, qint64 maxlen) override; |
| 214 | qint64 readLineData(char *data, qint64 maxlen) override; |
| 215 | qint64 skipData(qint64 maxSize) override; |
| 216 | qint64 writeData(const char *data, qint64 len) override; |
| 217 | |
| 218 | void setSocketState(SocketState state); |
| 219 | void setSocketError(SocketError socketError); |
| 220 | void setLocalPort(quint16 port); |
| 221 | void setLocalAddress(const QHostAddress &address); |
| 222 | void setPeerPort(quint16 port); |
| 223 | void setPeerAddress(const QHostAddress &address); |
| 224 | void setPeerName(const QString &name); |
| 225 | |
| 226 | QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = nullptr); |
| 227 | |
| 228 | private: |
| 229 | Q_DECLARE_PRIVATE(QAbstractSocket) |
| 230 | Q_DISABLE_COPY_MOVE(QAbstractSocket) |
| 231 | |
| 232 | Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress()) |
| 233 | Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &)) |
| 234 | Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt()) |
| 235 | Q_PRIVATE_SLOT(d_func(), void _q_testConnection()) |
| 236 | }; |
| 237 | |
| 238 | |
| 239 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::BindMode) |
| 240 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::PauseModes) |
| 241 | |
| 242 | #ifndef QT_NO_DEBUG_STREAM |
| 243 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError); |
| 244 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState); |
| 245 | #endif |
| 246 | |
| 247 | QT_END_NAMESPACE |
| 248 | |
| 249 | Q_DECLARE_METATYPE(QAbstractSocket::SocketState) |
| 250 | Q_DECLARE_METATYPE(QAbstractSocket::SocketError) |
| 251 | |
| 252 | #endif // QABSTRACTSOCKET_H |
| 253 | |