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