00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If 00004 ** you did not receive the LICENSE file with this file, you may obtain it 00005 ** from the Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.vidalia-project.net/. No part of Vidalia, including this file, 00007 ** may be copied, modified, propagated, or distributed except according to 00008 ** the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file upnpcontrolthread.h 00013 ** \version $Id: upnpcontrolthread.h 2638 2008-06-01 23:42:53Z edmanm $ 00014 ** \brief Thread for configuring UPnP in the background 00015 */ 00016 00017 #ifndef _UPNPCONTROLTHREAD_H 00018 #define _UPNPCONTROLTHREAD_H 00019 00020 #include <QThread> 00021 #include <QMutex> 00022 #include <QWaitCondition> 00023 #include <QTime> 00024 #include "upnpcontrol.h" 00025 00026 #define STATICLIB 00027 #include <miniupnpc/miniwget.h> 00028 #include <miniupnpc/miniupnpc.h> 00029 #include <miniupnpc/upnpcommands.h> 00030 00031 00032 class UPNPControlThread : public QThread 00033 { 00034 Q_OBJECT 00035 00036 public: 00037 /** Specifies the number of milliseconds to wait for devices to respond 00038 * when attempting to discover UPnP-enabled IGDs. */ 00039 static const int UPNPCONTROL_DISCOVER_TIMEOUT = 2000; 00040 00041 /** Constructor. <b>control</b> will be used for retrieving the desired port 00042 * forwarding state. */ 00043 UPNPControlThread(UPNPControl *control); 00044 /** Destructor. The UPnP control thread must be stopped prior to destroying 00045 * this object. */ 00046 ~UPNPControlThread(); 00047 /** Terminates the UPnP control thread's run() loop. */ 00048 void stop(); 00049 /** Wakes up the UPnP control thread's run() loop. */ 00050 void wakeup(); 00051 00052 protected: 00053 /** Thread entry point. The thread has a main loop that periodically wakes 00054 * up and updates the configured port mappings. Upon exiting, all port 00055 * mappings will be removed. */ 00056 void run(); 00057 00058 private: 00059 /** Sets up port forwarding according the previously-configured desired 00060 * state. The desired state is set using UPNPControl's setDesiredState() 00061 * method. */ 00062 void configurePorts(); 00063 /** Discovers UPnP-enabled IGDs on the network. This method will block for 00064 * UPNPCONTROL_DISCOVER_TIMEOUT milliseconds. */ 00065 UPNPControl::UPNPError initializeUPNP(); 00066 /** Updates the port mapping for <b>oldPort</b>, changing it to 00067 * <b>newPort</b>. */ 00068 UPNPControl::UPNPError updatePort(quint16 oldPort, quint16 newPort); 00069 /** Adds a port forwarding mapping from external:<b>port</b> to 00070 * internal:<b>port</b>. Returns 0 on success, or non-zero on failure. */ 00071 UPNPControl::UPNPError forwardPort(quint16 port); 00072 /** Removes the port mapping for <b>port</b>. Returns 0 on success or 00073 * non-zero on failure. */ 00074 UPNPControl::UPNPError disablePort(quint16 port); 00075 00076 QTime _upnpInitialized; /**< Time at which the UPnP state was last set. */ 00077 bool _keepRunning; /**< True if the control thread should keep running. */ 00078 UPNPControl *_control; /**< Stores desired UPnP state. */ 00079 QWaitCondition *_waitCondition; /**< Used to wake up the control thread. */ 00080 QMutex *_waitMutex; /**< Mutex around shared variables. */ 00081 quint16 _dirPort; /**< Desired DirPort. */ 00082 quint16 _orPort; /**< Desired ORPort. */ 00083 00084 /* Used by miniupnpc library */ 00085 struct UPNPUrls urls; 00086 struct IGDdatas data; 00087 char lanaddr[16]; 00088 }; 00089 #endif 00090