Warning, file /frameworks/kio/src/core/tcpworkerbase.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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