File indexing completed on 2025-01-05 04:37:28

0001 /*
0002     SPDX-FileCopyrightText: 2005-2007 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef KTUPNPROUTER_H
0008 #define KTUPNPROUTER_H
0009 
0010 #include <QStringList>
0011 #include <QUrl>
0012 
0013 #include <ktorrent_export.h>
0014 #include <net/portlist.h>
0015 
0016 class KJob;
0017 
0018 namespace bt
0019 {
0020 class HTTPRequest;
0021 class WaitJob;
0022 
0023 /**
0024  * Structure describing a UPnP service found in an xml file.
0025  */
0026 struct KTORRENT_EXPORT UPnPService {
0027     QString serviceid;
0028     QString servicetype;
0029     QString controlurl;
0030     QString eventsuburl;
0031     QString scpdurl;
0032 
0033     UPnPService();
0034     UPnPService(const UPnPService &s);
0035 
0036     /**
0037      * Set a property of the service.
0038      * @param name Name of the property (matches to variable names)
0039      * @param value Value of the property
0040      */
0041     void setProperty(const QString &name, const QString &value);
0042 
0043     /**
0044      * Set all strings to empty.
0045      */
0046     void clear();
0047 
0048     /**
0049      * Assignment operator
0050      * @param s The service to copy
0051      * @return *this
0052      */
0053     UPnPService &operator=(const UPnPService &s);
0054 };
0055 
0056 /**
0057  *  Struct to hold the description of a device
0058  */
0059 struct KTORRENT_EXPORT UPnPDeviceDescription {
0060     QString friendlyName;
0061     QString manufacturer;
0062     QString modelDescription;
0063     QString modelName;
0064     QString modelNumber;
0065 
0066     /**
0067      * Set a property of the description
0068      * @param name Name of the property (matches to variable names)
0069      * @param value Value of the property
0070      */
0071     void setProperty(const QString &name, const QString &value);
0072 };
0073 
0074 /**
0075  * @author Joris Guisson
0076  *
0077  * Class representing a UPnP enabled router. This class is also used to communicate
0078  * with the router.
0079  */
0080 class KTORRENT_EXPORT UPnPRouter : public QObject
0081 {
0082     Q_OBJECT
0083 public:
0084     /**
0085      * Construct a router.
0086      * @param server The name of the router
0087      * @param location The location of it's xml description file
0088      * @param verbose Print lots of debug info
0089      */
0090     UPnPRouter(const QString &server, const QUrl &location, bool verbose = false);
0091     ~UPnPRouter() override;
0092 
0093     /// Disable or enable verbose logging
0094     void setVerbose(bool v);
0095 
0096     /// Get the name  of the server
0097     QString getServer() const;
0098 
0099     /// Get the location of it's xml description
0100     QUrl getLocation() const;
0101 
0102     /// Get the device description
0103     UPnPDeviceDescription &getDescription();
0104 
0105     /// Get the device description (const version)
0106     const UPnPDeviceDescription &getDescription() const;
0107 
0108     /// Get the current error (null string if there is none)
0109     QString getError() const;
0110 
0111     /// Get the router's external IP
0112     QString getExternalIP() const;
0113 
0114     /**
0115      * Download the XML File of the router.
0116      */
0117     void downloadXMLFile();
0118 
0119     /**
0120      * Add a service to the router.
0121      * @param s The service
0122      */
0123     void addService(UPnPService s);
0124 
0125 #if 0
0126     /**
0127      * See if a port is forwarded
0128      * @param port The Port
0129      */
0130     void isPortForwarded(const net::Port & port);
0131 #endif
0132 
0133     /**
0134      * Forward a local port
0135      * @param port The local port to forward
0136      */
0137     void forward(const net::Port &port);
0138 
0139     /**
0140      * Undo forwarding
0141      * @param port The port
0142      * @param waitjob When this is set the jobs needs to be added to the waitjob,
0143      * so we can wait for their completeion at exit
0144      */
0145     void undoForward(const net::Port &port, bt::WaitJob *waitjob = nullptr);
0146 
0147     /**
0148         In order to iterate over all forwardings, the visitor pattern must be used.
0149     */
0150     class Visitor
0151     {
0152     public:
0153         virtual ~Visitor()
0154         {
0155         }
0156 
0157         /**
0158             Called for each forwarding.
0159             @param port The Port
0160             @param pending When set to true, the forwarding is not completed yet
0161             @param service The UPnPService this forwarding is for
0162         */
0163         virtual void forwarding(const net::Port &port, bool pending, const UPnPService *service) = 0;
0164     };
0165 
0166     /**
0167         Visit all forwardings
0168         @param visitor The Visitor
0169     */
0170     void visit(Visitor *visitor) const;
0171 
0172 private:
0173     void forwardResult(HTTPRequest *r);
0174     void undoForwardResult(HTTPRequest *r);
0175     void getExternalIPResult(HTTPRequest *r);
0176     void downloadFinished(KJob *j);
0177 
0178 Q_SIGNALS:
0179     /**
0180      * Internal state has changed, a forwarding succeeded or failed, or an undo forwarding succeeded or failed.
0181      */
0182     void stateChanged();
0183 
0184     /**
0185      * Signal which indicates that the XML was downloaded successfully or not.
0186      * @param r The router which emitted the signal
0187      * @param success Whether or not it succeeded
0188      */
0189     void xmlFileDownloaded(UPnPRouter *r, bool success);
0190 
0191 private:
0192     class UPnPRouterPrivate;
0193     UPnPRouterPrivate *d;
0194 };
0195 
0196 }
0197 
0198 #endif