| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSOCKS5SOCKETENGINE_P_H |
| 5 | #define QSOCKS5SOCKETENGINE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
| 19 | |
| 20 | #include <QtNetwork/qnetworkproxy.h> |
| 21 | |
| 22 | #include "qabstractsocketengine_p.h" |
| 23 | |
| 24 | QT_REQUIRE_CONFIG(socks5); |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QSocks5SocketEnginePrivate; |
| 29 | |
| 30 | class Q_AUTOTEST_EXPORT QSocks5SocketEngine : public QAbstractSocketEngine |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | public: |
| 34 | QSocks5SocketEngine(QObject *parent = nullptr); |
| 35 | ~QSocks5SocketEngine(); |
| 36 | |
| 37 | bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) override; |
| 38 | bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) override; |
| 39 | |
| 40 | void setProxy(const QNetworkProxy &networkProxy); |
| 41 | |
| 42 | qintptr socketDescriptor() const override; |
| 43 | |
| 44 | bool isValid() const override; |
| 45 | |
| 46 | bool connectInternal(); |
| 47 | bool connectToHost(const QHostAddress &address, quint16 port) override; |
| 48 | bool connectToHostByName(const QString &name, quint16 port) override; |
| 49 | bool bind(const QHostAddress &address, quint16 port) override; |
| 50 | bool listen(int backlog) override; |
| 51 | qintptr accept() override; |
| 52 | void close() override; |
| 53 | |
| 54 | qint64 bytesAvailable() const override; |
| 55 | |
| 56 | qint64 read(char *data, qint64 maxlen) override; |
| 57 | qint64 write(const char *data, qint64 len) override; |
| 58 | |
| 59 | #ifndef QT_NO_UDPSOCKET |
| 60 | #ifndef QT_NO_NETWORKINTERFACE |
| 61 | bool joinMulticastGroup(const QHostAddress &groupAddress, |
| 62 | const QNetworkInterface &interface) override; |
| 63 | bool leaveMulticastGroup(const QHostAddress &groupAddress, |
| 64 | const QNetworkInterface &interface) override; |
| 65 | QNetworkInterface multicastInterface() const override; |
| 66 | bool setMulticastInterface(const QNetworkInterface &iface) override; |
| 67 | #endif // QT_NO_NETWORKINTERFACE |
| 68 | |
| 69 | bool hasPendingDatagrams() const override; |
| 70 | qint64 pendingDatagramSize() const override; |
| 71 | #endif // QT_NO_UDPSOCKET |
| 72 | |
| 73 | qint64 (char *data, qint64 maxlen, QIpPacketHeader * = nullptr, |
| 74 | PacketHeaderOptions = WantNone) override; |
| 75 | qint64 (const char *data, qint64 len, const QIpPacketHeader &) override; |
| 76 | qint64 bytesToWrite() const override; |
| 77 | |
| 78 | int option(SocketOption option) const override; |
| 79 | bool setOption(SocketOption option, int value) override; |
| 80 | |
| 81 | bool waitForRead(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
| 82 | bool *timedOut = nullptr) override; |
| 83 | bool waitForWrite(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
| 84 | bool *timedOut = nullptr) override; |
| 85 | bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
| 86 | bool checkRead, bool checkWrite, |
| 87 | QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
| 88 | bool *timedOut = nullptr) override; |
| 89 | |
| 90 | bool isReadNotificationEnabled() const override; |
| 91 | void setReadNotificationEnabled(bool enable) override; |
| 92 | bool isWriteNotificationEnabled() const override; |
| 93 | void setWriteNotificationEnabled(bool enable) override; |
| 94 | bool isExceptionNotificationEnabled() const override; |
| 95 | void setExceptionNotificationEnabled(bool enable) override; |
| 96 | |
| 97 | private: |
| 98 | Q_DECLARE_PRIVATE(QSocks5SocketEngine) |
| 99 | Q_DISABLE_COPY_MOVE(QSocks5SocketEngine) |
| 100 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketConnected()) |
| 101 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketReadNotification()) |
| 102 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketErrorOccurred(QAbstractSocket::SocketError)) |
| 103 | #ifndef QT_NO_UDPSOCKET |
| 104 | Q_PRIVATE_SLOT(d_func(), void _q_udpSocketReadNotification()) |
| 105 | #endif |
| 106 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketBytesWritten()) |
| 107 | Q_PRIVATE_SLOT(d_func(), void _q_emitPendingReadNotification()) |
| 108 | Q_PRIVATE_SLOT(d_func(), void _q_emitPendingWriteNotification()) |
| 109 | Q_PRIVATE_SLOT(d_func(), void _q_emitPendingConnectionNotification()) |
| 110 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketDisconnected()) |
| 111 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketStateChanged(QAbstractSocket::SocketState)) |
| 112 | |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | class QTcpSocket; |
| 117 | |
| 118 | class QSocks5Authenticator |
| 119 | { |
| 120 | public: |
| 121 | QSocks5Authenticator(); |
| 122 | virtual ~QSocks5Authenticator(); |
| 123 | virtual char methodId(); |
| 124 | virtual bool beginAuthenticate(QTcpSocket *socket, bool *completed); |
| 125 | virtual bool continueAuthenticate(QTcpSocket *socket, bool *completed); |
| 126 | |
| 127 | bool seal(const QByteArray &buf, QByteArray *sealedBuf); |
| 128 | bool unSeal(const QByteArray &sealedBuf, QByteArray *buf); |
| 129 | bool unSeal(QTcpSocket *sealedSocket, QByteArray *buf); |
| 130 | |
| 131 | virtual QString errorString() { return QString(); } |
| 132 | }; |
| 133 | |
| 134 | class QSocks5PasswordAuthenticator : public QSocks5Authenticator |
| 135 | { |
| 136 | public: |
| 137 | QSocks5PasswordAuthenticator(const QString &userName, const QString &password); |
| 138 | char methodId() override; |
| 139 | bool beginAuthenticate(QTcpSocket *socket, bool *completed) override; |
| 140 | bool continueAuthenticate(QTcpSocket *socket, bool *completed) override; |
| 141 | |
| 142 | QString errorString() override; |
| 143 | |
| 144 | private: |
| 145 | QString userName; |
| 146 | QString password; |
| 147 | }; |
| 148 | |
| 149 | struct QSocks5Data; |
| 150 | struct QSocks5ConnectData; |
| 151 | struct QSocks5UdpAssociateData; |
| 152 | struct QSocks5BindData; |
| 153 | |
| 154 | class QSocks5SocketEnginePrivate : public QAbstractSocketEnginePrivate |
| 155 | { |
| 156 | Q_DECLARE_PUBLIC(QSocks5SocketEngine) |
| 157 | public: |
| 158 | QSocks5SocketEnginePrivate(); |
| 159 | ~QSocks5SocketEnginePrivate(); |
| 160 | |
| 161 | enum Socks5State |
| 162 | { |
| 163 | Uninitialized = 0, |
| 164 | ConnectError, |
| 165 | AuthenticationMethodsSent, |
| 166 | Authenticating, |
| 167 | AuthenticatingError, |
| 168 | RequestMethodSent, |
| 169 | RequestError, |
| 170 | Connected, |
| 171 | UdpAssociateSuccess, |
| 172 | BindSuccess, |
| 173 | ControlSocketError, |
| 174 | SocksError, |
| 175 | HostNameLookupError |
| 176 | }; |
| 177 | Socks5State socks5State; |
| 178 | |
| 179 | enum Socks5Mode |
| 180 | { |
| 181 | NoMode, |
| 182 | ConnectMode, |
| 183 | BindMode, |
| 184 | UdpAssociateMode |
| 185 | }; |
| 186 | Socks5Mode mode; |
| 187 | |
| 188 | enum Socks5Error |
| 189 | { |
| 190 | SocksFailure = 0x01, |
| 191 | ConnectionNotAllowed = 0x02, |
| 192 | NetworkUnreachable = 0x03, |
| 193 | HostUnreachable = 0x04, |
| 194 | ConnectionRefused = 0x05, |
| 195 | TTLExpired = 0x06, |
| 196 | CommandNotSupported = 0x07, |
| 197 | AddressTypeNotSupported = 0x08, |
| 198 | LastKnownError = AddressTypeNotSupported, |
| 199 | UnknownError |
| 200 | }; |
| 201 | |
| 202 | void initialize(Socks5Mode socks5Mode); |
| 203 | |
| 204 | void setErrorState(Socks5State state, const QString & = QString()); |
| 205 | void setErrorState(Socks5State state, Socks5Error socks5error); |
| 206 | |
| 207 | void reauthenticate(); |
| 208 | void parseAuthenticationMethodReply(); |
| 209 | void parseAuthenticatingReply(); |
| 210 | void sendRequestMethod(); |
| 211 | void parseRequestMethodReply(); |
| 212 | void parseNewConnection(); |
| 213 | |
| 214 | bool waitForConnected(QDeadlineTimer deadline, bool *timedOut); |
| 215 | |
| 216 | void _q_controlSocketConnected(); |
| 217 | void _q_controlSocketReadNotification(); |
| 218 | void _q_controlSocketErrorOccurred(QAbstractSocket::SocketError); |
| 219 | #ifndef QT_NO_UDPSOCKET |
| 220 | void _q_udpSocketReadNotification(); |
| 221 | #endif |
| 222 | void _q_controlSocketBytesWritten(); |
| 223 | void _q_controlSocketDisconnected(); |
| 224 | void _q_controlSocketStateChanged(QAbstractSocket::SocketState); |
| 225 | |
| 226 | QNetworkProxy proxyInfo; |
| 227 | |
| 228 | bool readNotificationEnabled, writeNotificationEnabled, exceptNotificationEnabled; |
| 229 | |
| 230 | qintptr socketDescriptor; |
| 231 | |
| 232 | QSocks5Data *data; |
| 233 | QSocks5ConnectData *connectData; |
| 234 | #ifndef QT_NO_UDPSOCKET |
| 235 | QSocks5UdpAssociateData *udpData; |
| 236 | #endif |
| 237 | QSocks5BindData *bindData; |
| 238 | QString peerName; |
| 239 | QByteArray ; |
| 240 | |
| 241 | mutable bool readNotificationActivated; |
| 242 | mutable bool writeNotificationActivated; |
| 243 | |
| 244 | bool readNotificationPending; |
| 245 | void _q_emitPendingReadNotification(); |
| 246 | void emitReadNotification(); |
| 247 | bool writeNotificationPending; |
| 248 | void _q_emitPendingWriteNotification(); |
| 249 | void emitWriteNotification(); |
| 250 | bool connectionNotificationPending; |
| 251 | void _q_emitPendingConnectionNotification(); |
| 252 | void emitConnectionNotification(); |
| 253 | }; |
| 254 | |
| 255 | class Q_AUTOTEST_EXPORT QSocks5SocketEngineHandler : public QSocketEngineHandler |
| 256 | { |
| 257 | public: |
| 258 | virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, |
| 259 | const QNetworkProxy &, QObject *parent) override; |
| 260 | virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent) override; |
| 261 | }; |
| 262 | |
| 263 | QT_END_NAMESPACE |
| 264 | |
| 265 | #endif // QSOCKS5SOCKETENGINE_H |
| 266 | |