Vidalia 0.2.10
|
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 3735 2009-04-28 20:28:01Z edmanm $ 00014 ** \brief Thread for configuring UPnP in the background 00015 */ 00016 00017 #ifndef _UPNPCONTROLTHREAD_H 00018 #define _UPNPCONTROLTHREAD_H 00019 00020 #include "UPNPControl.h" 00021 00022 #define STATICLIB 00023 #include <miniupnpc/miniwget.h> 00024 #include <miniupnpc/miniupnpc.h> 00025 #include <miniupnpc/upnpcommands.h> 00026 #undef STATICLIB 00027 00028 #include <QThread> 00029 #include <QMutex> 00030 #include <QWaitCondition> 00031 #include <QTime> 00032 00033 00034 class UPNPControlThread : public QThread 00035 { 00036 Q_OBJECT 00037 00038 public: 00039 /** Specifies the number of milliseconds to wait for devices to respond 00040 * when attempting to discover UPnP-enabled IGDs. */ 00041 static const int UPNPCONTROL_DISCOVER_TIMEOUT = 2000; 00042 00043 /** Constructor. <b>control</b> will be used for retrieving the desired port 00044 * forwarding state. */ 00045 UPNPControlThread(UPNPControl *control); 00046 /** Destructor. The UPnP control thread must be stopped prior to destroying 00047 * this object. */ 00048 ~UPNPControlThread(); 00049 /** Terminates the UPnP control thread's run() loop. */ 00050 void stop(); 00051 /** Wakes up the UPnP control thread's run() loop. */ 00052 void wakeup(); 00053 00054 protected: 00055 /** Thread entry point. The thread has a main loop that periodically wakes 00056 * up and updates the configured port mappings. Upon exiting, all port 00057 * mappings will be removed. */ 00058 void run(); 00059 00060 private: 00061 /** Sets up port forwarding according the previously-configured desired 00062 * state. The desired state is set using UPNPControl's setDesiredState() 00063 * method. */ 00064 void configurePorts(); 00065 /** Discovers UPnP-enabled IGDs on the network. This method will block for 00066 * UPNPCONTROL_DISCOVER_TIMEOUT milliseconds. */ 00067 UPNPControl::UPNPError initializeUPNP(); 00068 /** Updates the port mapping for <b>oldPort</b>, changing it to 00069 * <b>newPort</b>. */ 00070 UPNPControl::UPNPError updatePort(quint16 oldPort, quint16 newPort); 00071 /** Adds a port forwarding mapping from external:<b>port</b> to 00072 * internal:<b>port</b>. Returns 0 on success, or non-zero on failure. */ 00073 UPNPControl::UPNPError forwardPort(quint16 port); 00074 /** Removes the port mapping for <b>port</b>. Returns 0 on success or 00075 * non-zero on failure. */ 00076 UPNPControl::UPNPError disablePort(quint16 port); 00077 00078 QTime _upnpInitialized; /**< Time at which the UPnP state was last set. */ 00079 bool _keepRunning; /**< True if the control thread should keep running. */ 00080 UPNPControl *_control; /**< Stores desired UPnP state. */ 00081 QWaitCondition *_waitCondition; /**< Used to wake up the control thread. */ 00082 QMutex *_waitMutex; /**< Mutex around shared variables. */ 00083 quint16 _dirPort; /**< Desired DirPort. */ 00084 quint16 _orPort; /**< Desired ORPort. */ 00085 00086 /* Used by miniupnpc library */ 00087 struct UPNPUrls urls; 00088 struct IGDdatas data; 00089 char lanaddr[16]; 00090 }; 00091 #endif 00092