Vidalia  0.3.1
UPNPControlThread.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file UPNPControlThread.h
13 ** \brief Thread for configuring UPnP in the background
14 */
15 
16 #ifndef _UPNPCONTROLTHREAD_H
17 #define _UPNPCONTROLTHREAD_H
18 
19 #include "UPNPControl.h"
20 
21 #define STATICLIB
22 #include <miniupnpc/miniwget.h>
23 #include <miniupnpc/miniupnpc.h>
24 #include <miniupnpc/upnpcommands.h>
25 #undef STATICLIB
26 
27 #include <QThread>
28 #include <QMutex>
29 #include <QWaitCondition>
30 #include <QTime>
31 
32 
33 class UPNPControlThread : public QThread
34 {
35  Q_OBJECT
36 
37 public:
38  /** Specifies the number of milliseconds to wait for devices to respond
39  * when attempting to discover UPnP-enabled IGDs. */
40  static const int UPNPCONTROL_DISCOVER_TIMEOUT = 2000;
41 
42  /** Constructor. <b>control</b> will be used for retrieving the desired port
43  * forwarding state. */
45  /** Destructor. The UPnP control thread must be stopped prior to destroying
46  * this object. */
48  /** Terminates the UPnP control thread's run() loop. */
49  void stop();
50  /** Wakes up the UPnP control thread's run() loop. */
51  void wakeup();
52 
53 protected:
54  /** Thread entry point. The thread has a main loop that periodically wakes
55  * up and updates the configured port mappings. Upon exiting, all port
56  * mappings will be removed. */
57  void run();
58 
59 private:
60  /** Sets up port forwarding according the previously-configured desired
61  * state. The desired state is set using UPNPControl's setDesiredState()
62  * method. */
63  void configurePorts();
64  /** Discovers UPnP-enabled IGDs on the network. This method will block for
65  * UPNPCONTROL_DISCOVER_TIMEOUT milliseconds. */
67  /** Updates the port mapping for <b>oldPort</b>, changing it to
68  * <b>newPort</b>. */
69  UPNPControl::UPNPError updatePort(quint16 oldPort, quint16 newPort);
70  /** Adds a port forwarding mapping from external:<b>port</b> to
71  * internal:<b>port</b>. Returns 0 on success, or non-zero on failure. */
72  UPNPControl::UPNPError forwardPort(quint16 port);
73  /** Removes the port mapping for <b>port</b>. Returns 0 on success or
74  * non-zero on failure. */
75  UPNPControl::UPNPError disablePort(quint16 port);
76 
77  QTime _upnpInitialized; /**< Time at which the UPnP state was last set. */
78  bool _keepRunning; /**< True if the control thread should keep running. */
79  UPNPControl *_control; /**< Stores desired UPnP state. */
80  QWaitCondition *_waitCondition; /**< Used to wake up the control thread. */
81  QMutex *_waitMutex; /**< Mutex around shared variables. */
82  quint16 _dirPort; /**< Desired DirPort. */
83  quint16 _orPort; /**< Desired ORPort. */
84 
85  /* Used by miniupnpc library */
86  struct UPNPUrls urls;
87  struct IGDdatas data;
88  char lanaddr[16];
89 };
90 #endif
91 
struct IGDdatas data
QWaitCondition * _waitCondition
UPNPControl::UPNPError disablePort(quint16 port)
UPNPControl::UPNPError initializeUPNP()
UPNPControlThread(UPNPControl *control)
UPNPControl * _control
static const int UPNPCONTROL_DISCOVER_TIMEOUT
UPNPControl::UPNPError updatePort(quint16 oldPort, quint16 newPort)
UPNPControl::UPNPError forwardPort(quint16 port)
struct UPNPUrls urls