Vidalia 0.3.1
UPNPControl.cpp
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 UPNPControl.cpp
13** \brief Singleton object for interacting with UPNP device
14*/
15
16#include "UPNPControl.h"
17#include "UPNPControlThread.h"
18
19#include <QMutex>
20#include <QMetaType>
21
22#ifdef Q_OS_WIN32
23#include <winsock2.h>
24#endif
25
26
27/** UPNPControl singleton instance. */
29
30/** Returns a pointer to this object's singleton instance. */
32{
33 if (0 == _instance) {
34 _instance = new UPNPControl();
35 _instance->_controlThread->start();
36 }
37 return _instance;
38}
39
40/** Constructor. Initializes and starts a thread in which all blocking UPnP
41 * operations will be performed. */
43{
48
49 qRegisterMetaType<UPNPControl::UPNPError>("UPNPControl::UPNPError");
50 qRegisterMetaType<UPNPControl::UPNPState>("UPNPControl::UPNPState");
51
52 _mutex = new QMutex();
54}
55
56/** Destructor. cleanup() should be called before the object is destroyed.
57 * \sa cleanup()
58 */
60{
61 delete _mutex;
62 delete _controlThread;
63}
64
65/** Terminates the UPnP control thread and frees memory allocated to this
66 * object's singleton instance. */
67void
69{
71 delete _instance;
72 _instance = 0;
73}
74
75/** Sets <b>desiredDirPort</b> and <b>desiredOrPort</b> to the currently
76 * forwarded DirPort and ORPort values. */
77void
78UPNPControl::getDesiredState(quint16 *desiredDirPort, quint16 *desiredOrPort)
79{
80 _mutex->lock();
81 *desiredDirPort = _forwardedDirPort;
82 *desiredOrPort = _forwardedORPort;
83 _mutex->unlock();
84}
85
86/** Sets the desired DirPort and ORPort port mappings to <b>desiredDirPort</b>
87 * and <b>desiredOrPort</b>, respectively. */
88void
89UPNPControl::setDesiredState(quint16 desiredDirPort, quint16 desiredOrPort)
90{
91 _mutex->lock();
92 _forwardedDirPort = desiredDirPort;
93 _forwardedORPort = desiredOrPort;
94 _mutex->unlock();
95
97}
98
99/** Sets the most recent UPnP-related error to <b>error</b> and emits the
100 * error() signal. */
101void
103{
104 _mutex->lock();
105 _error = upnpError;
106 _mutex->unlock();
107
108 emit error(upnpError);
109}
110
111/** Sets the current UPnP state to <b>state</b> and emits the stateChanged()
112 * signal. */
113void
115{
116 _mutex->lock();
117 _state = state;
118 _mutex->unlock();
119
120 emit stateChanged(state);
121}
122
123/** Returns the type of error that occurred last. */
126{
127 QMutexLocker locker(_mutex);
128 return _error;
129}
130
131/** Returns a QString describing the type of error that occurred last. */
132QString
134{
135 UPNPError error = this->error();
136
137 switch (error) {
138 case Success:
139 return tr("Success");
141 return tr("No UPnP-enabled devices found");
142 case NoValidIGDsFound:
143 return tr("No valid UPnP-enabled Internet gateway devices found");
144 case WSAStartupFailed:
145 return tr("WSAStartup failed");
147 return tr("Failed to add a port mapping");
149 return tr("Failed to retrieve a port mapping");
151 return tr("Failed to remove a port mapping");
152 default:
153 return tr("Unknown error");
154 }
155}
156
157/** Returns the number of milliseconds to wait for devices to respond
158 * when attempting to discover UPnP-enabled IGDs. */
159int
161{
163}
164
static void cleanup()
Definition: UPNPControl.cpp:68
void setState(UPNPState state)
void setDesiredState(quint16 desiredDirPort, quint16 desiredOrPort)
Definition: UPNPControl.cpp:89
quint16 _forwardedORPort
Definition: UPNPControl.h:102
UPNPError error() const
QString errorString() const
static UPNPControl * instance()
Definition: UPNPControl.cpp:31
void stateChanged(UPNPControl::UPNPState state)
UPNPState _state
Definition: UPNPControl.h:106
void getDesiredState(quint16 *desiredDirPort, quint16 *desiredOrPort)
Definition: UPNPControl.cpp:78
int discoverTimeout() const
QMutex * _mutex
Definition: UPNPControl.h:104
quint16 _forwardedDirPort
Definition: UPNPControl.h:103
void setError(UPNPError error)
@ AddPortMappingFailed
Definition: UPNPControl.h:37
@ GetPortMappingFailed
Definition: UPNPControl.h:38
@ NoUPNPDevicesFound
Definition: UPNPControl.h:34
@ DeletePortMappingFailed
Definition: UPNPControl.h:39
@ NoValidIGDsFound
Definition: UPNPControl.h:35
@ WSAStartupFailed
Definition: UPNPControl.h:36
UPNPError _error
Definition: UPNPControl.h:105
UPNPControlThread * _controlThread
Definition: UPNPControl.h:109
friend class UPNPControlThread
Definition: UPNPControl.h:108
static UPNPControl * _instance
Definition: UPNPControl.h:100
static const int UPNPCONTROL_DISCOVER_TIMEOUT