File indexing completed on 2023-09-24 04:08:44
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2000 Alex Zepeda <zipzippy@sonic.net> 0004 SPDX-FileCopyrightText: 2001 George Staikos <staikos@kde.org> 0005 SPDX-FileCopyrightText: 2001 Dawit Alemayehu <adawit@kde.org> 0006 SPDX-FileCopyrightText: 2007, 2008 Andreas Hartmetz <ahartmetz@gmail.com> 0007 0008 SPDX-License-Identifier: LGPL-2.0-or-later 0009 */ 0010 0011 #ifndef _TCP_SLAVEBASE_H 0012 #define _TCP_SLAVEBASE_H 0013 0014 #include <stdio.h> 0015 #include <sys/types.h> 0016 0017 #include "kiocore_export.h" 0018 #include <kio/slavebase.h> 0019 0020 #include <memory> 0021 0022 class QIODevice; 0023 class QSslSocket; 0024 0025 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 101) 0026 0027 namespace KIO 0028 { 0029 0030 /** 0031 * @class KIO::TCPSlaveBase tcpslavebase.h <KIO/TCPSlaveBase> 0032 * 0033 * A SlaveBase with convenience methods for TCP-connected storages. 0034 * 0035 * @deprecated Since 5.99, use TCPWorkerBase. 0036 */ 0037 class KIOCORE_EXPORT TCPSlaveBase : public SlaveBase 0038 { 0039 public: 0040 /** 0041 * Constructor. 0042 * 0043 * @param autoSsl if true, will automatically invoke startSsl() right after 0044 * connecting. In the absence of errors the use of SSL will 0045 * therefore be transparent to higher layers. 0046 */ 0047 KIOCORE_DEPRECATED_VERSION_BELATED(5, 101, 5, 99, "Use TCPWorkerBase") 0048 TCPSlaveBase(const QByteArray &protocol, const QByteArray &poolSocket, const QByteArray &appSocket, bool autoSsl = false); 0049 0050 ~TCPSlaveBase() override; 0051 0052 protected: 0053 enum SslResultDetail { 0054 ResultOk = 1, 0055 ResultOverridden = 2, 0056 ResultFailed = 4, 0057 ResultFailedEarly = 8, 0058 }; 0059 friend class QFlags<KIO::TCPSlaveBase::SslResultDetail>; 0060 0061 public: 0062 Q_DECLARE_FLAGS(SslResult, SslResultDetail) 0063 protected: 0064 /** 0065 * Send data to the remote host. 0066 * 0067 * @param data data to be sent to remote machine 0068 * @param len the length (in bytes) of the data to be sent 0069 * 0070 * @return the actual size of the data that was sent 0071 */ 0072 using SlaveBase::write; // Silence incompatible virtual override warning 0073 ssize_t write(const char *data, ssize_t len); 0074 0075 /** 0076 * Read incoming data from the remote host. 0077 * 0078 * @param data storage for the data read from server 0079 * @param len length of the data (in bytes) to read from the server 0080 * 0081 * @return the actual size of data that was obtained 0082 */ 0083 using SlaveBase::read; 0084 ssize_t read(char *data, ssize_t len); 0085 0086 /** 0087 * Same as read() except it reads data one line at a time. 0088 */ 0089 ssize_t readLine(char *data, ssize_t len); 0090 0091 /** 0092 * Performs the initial TCP connection stuff and/or 0093 * SSL handshaking as necessary. 0094 * 0095 * @param protocol the protocol being used 0096 * @param host hostname 0097 * @param port port number 0098 * 0099 * @return on success, true is returned. 0100 * on failure, false is returned and an appropriate 0101 * error message is sent to the application. 0102 */ 0103 bool connectToHost(const QString &protocol, const QString &host, quint16 port); 0104 0105 /** 0106 * Connects to the specified host and port. 0107 * 0108 * @param host host name 0109 * @param port port number 0110 * @param errorString if not nullptr, this string will contain error information 0111 * on why the connection request failed. 0112 * 0113 * @return on success, 0 is returned. on failure, a KIO::Error code is returned. 0114 * @ref errorString, if not nullptr, will contain the appropriate error message 0115 * that can be sent back to the client. 0116 * 0117 * @since 4.7.2 0118 */ 0119 int connectToHost(const QString &host, quint16 port, QString *errorString = nullptr); 0120 0121 /** 0122 * the current port for this service 0123 * 0124 */ 0125 quint16 port() const; 0126 0127 /** 0128 * Will start SSL after connecting? 0129 * 0130 * @return if so, true is returned. 0131 * if not, false is returned. 0132 */ 0133 bool isAutoSsl() const; 0134 0135 /** 0136 * Is the current connection using SSL? 0137 * 0138 * @return if so, true is returned. 0139 * if not, false is returned. 0140 */ 0141 bool isUsingSsl() const; 0142 0143 /** 0144 * Start using SSL on the connection. You can use it right after connecting 0145 * for classic, transparent to the protocol SSL. Calling it later can be 0146 * used to implement e.g. SMTP's STARTTLS feature. 0147 * 0148 * @return on success, true is returned. 0149 * on failure, false is returned. 0150 */ 0151 bool startSsl(); 0152 0153 /** 0154 * Close the connection and forget non-permanent data like the peer host. 0155 */ 0156 void disconnectFromHost(); 0157 0158 /** 0159 * Returns true when end of data is reached. 0160 */ 0161 bool atEnd() const; 0162 0163 /** 0164 * Determines whether or not we are still connected 0165 * to the remote machine. 0166 * 0167 * return @p true if the socket is still active or 0168 * false otherwise. 0169 */ 0170 bool isConnected() const; 0171 0172 /** 0173 * Wait for incoming data on the socket 0174 * for the period specified by @p t. 0175 * 0176 * @param t length of time in seconds that we should monitor the 0177 * socket before timing out. 0178 * 0179 * @return true if any data arrived on the socket before the 0180 * timeout value was reached, false otherwise. 0181 */ 0182 bool waitForResponse(int t); 0183 0184 /** 0185 * Sets the mode of the connection to blocking or non-blocking. 0186 * 0187 * Be sure to call this function before calling connectToHost. 0188 * Otherwise, this setting will not have any effect until the next 0189 * @p connectToHost. 0190 * 0191 * @param b true to make the connection a blocking one, false otherwise. 0192 */ 0193 void setBlocking(bool b); 0194 0195 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 83) 0196 /** 0197 * Return the socket object, if the class ever needs to do anything to it. 0198 * 0199 * @deprecated since 5.83, use @c tcpSocket() instead. 0200 */ 0201 KIOCORE_DEPRECATED_VERSION(5, 83, "Use tcpSocket() instead.") 0202 QIODevice *socket() const; 0203 #endif 0204 0205 /** 0206 * Returns the socket object, useful if sub-classes need to do anything 0207 * with it (e.g. connect to any of the signals emitted by @c QAbstractSocket). 0208 * 0209 * @since 5.83 0210 */ 0211 QAbstractSocket *tcpSocket() const; 0212 0213 protected: 0214 void virtual_hook(int id, void *data) override; 0215 0216 private: 0217 // For the certificate verification code 0218 SslResult verifyServerCertificate(); 0219 0220 class TcpSlaveBasePrivate; 0221 std::unique_ptr<TcpSlaveBasePrivate> const d; 0222 }; 0223 0224 } 0225 0226 #endif 0227 0228 #endif