Vidalia  0.2.21
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 {
44  _forwardedORPort = 0;
47  _state = IdleState;
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. */
67 void
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. */
77 void
78 UPNPControl::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. */
88 void
89 UPNPControl::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. */
101 void
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. */
113 void
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. */
132 QString
134 {
135  UPNPError error = this->error();
136 
137  switch (error) {
138  case Success:
139  return tr("Success");
140  case NoUPNPDevicesFound:
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. */
159 int
161 {
163 }
164