[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2549: Do UPnP operations in the background (Vidalia now hangs on e (in vidalia/branches/upnp/src/vidalia: . config)



Author: sjmurdoch
Date: 2008-04-30 09:04:26 -0400 (Wed, 30 Apr 2008)
New Revision: 2549

Added:
   vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp
   vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h
Modified:
   vidalia/branches/upnp/src/vidalia/CMakeLists.txt
   vidalia/branches/upnp/src/vidalia/config/serversettings.cpp
   vidalia/branches/upnp/src/vidalia/config/serversettings.h
   vidalia/branches/upnp/src/vidalia/config/upnpcontrol.cpp
   vidalia/branches/upnp/src/vidalia/config/upnpcontrol.h
   vidalia/branches/upnp/src/vidalia/mainwindow.cpp
Log:
Do UPnP operations in the background (Vidalia now hangs on exit)

Modified: vidalia/branches/upnp/src/vidalia/CMakeLists.txt
===================================================================
--- vidalia/branches/upnp/src/vidalia/CMakeLists.txt	2008-04-29 18:22:43 UTC (rev 2548)
+++ vidalia/branches/upnp/src/vidalia/CMakeLists.txt	2008-04-30 13:04:26 UTC (rev 2549)
@@ -93,9 +93,11 @@
   include_directories(${MINIUPNPC_INCLUDE_DIR})
   set(vidalia_SRCS ${vidalia_SRCS}
     config/upnpcontrol.cpp
+    config/upnpcontrolthread.cpp
   )
   qt4_wrap_cpp(vidalia_SRCS
     config/upnpcontrol.h
+    config/upnpcontrolthread.h
   )
 endif(USE_MINIUPNPC)
 

Modified: vidalia/branches/upnp/src/vidalia/config/serversettings.cpp
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/serversettings.cpp	2008-04-29 18:22:43 UTC (rev 2548)
+++ vidalia/branches/upnp/src/vidalia/config/serversettings.cpp	2008-04-30 13:04:26 UTC (rev 2549)
@@ -137,7 +137,7 @@
   if (isServerEnabled()) {
     /* Configure UPnP device to forward DirPort and OrPort */
     /* TODO: does isServerEnabled() return true when a server is just set up? */
-    configurePortForwarding(true);
+    configurePortForwarding(true, true);
     rc = torControl()->setConf(confValues(), errmsg);
   } else { 
     QStringList resetKeys;
@@ -156,7 +156,7 @@
                 << SETTING_BANDWIDTH_BURST;
     }
     rc = torControl()->resetConf(resetKeys, errmsg);
-    configurePortForwarding(false);
+    configurePortForwarding(false, true);
   }
   return rc;
 }
@@ -169,42 +169,23 @@
 true, will forward ORPort and DirPort; otherwise will remove exising
 port mappings */
 void
-ServerSettings::configurePortForwarding(bool enable)
+ServerSettings::configurePortForwarding(bool enable, bool async)
 {
 #ifdef USE_MINIUPNPC
+  quint16 ORPort, DirPort;
+  bool enableORPort, enableDirPort;
 
-#ifdef WIN32  
-  // Workaround from http://trolltech.com/developer/knowledgebase/579
-  WSAData wsadata;
-  if (WSAStartup(MAKEWORD(2,0), &wsadata)!=0) {
-    qFatal("WSAStartup failure while disabling UPnP port forwarding");
-  }
-#endif
+  ORPort = getORPort();
+  DirPort = getDirPort();
 
-  UPNPControl *pUNPControl = UPNPControl::Instance();
+  enableORPort = isServerEnabled();
+  enableDirPort = enableORPort && isDirectoryMirror();
 
-  // Remove all port forwards
-  if (!enable) {
-    pUNPControl->disableForwarding();
-    return;
-  }
-
-  // Configure port forwards, if Tor is using them
-  if (isServerEnabled())
-    pUNPControl->forwardORPort(getORPort());
+  if (async)
+    UPNPControl::configurePortForwardingAsync(enable, enableORPort, enableDirPort, ORPort, DirPort);
   else
-    pUNPControl->disableORPort();
-
-  if (isServerEnabled() && isDirectoryMirror())
-    pUNPControl->forwardDirPort(getDirPort());
-  else
-    pUNPControl->disableDirPort();
-
-#ifdef WIN32
-  WSACleanup();
+    UPNPControl::configurePortForwardingSync(enable, enableORPort, enableDirPort, ORPort, DirPort);
 #endif
-
-#endif
 }
 
 /** Virtual method called when we retrieve a server-related setting from Tor.

Modified: vidalia/branches/upnp/src/vidalia/config/serversettings.h
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/serversettings.h	2008-04-29 18:22:43 UTC (rev 2548)
+++ vidalia/branches/upnp/src/vidalia/config/serversettings.h	2008-04-30 13:04:26 UTC (rev 2549)
@@ -83,7 +83,7 @@
   quint32 getBandwidthBurstRate();
 
   /** If enable is true, configure UPnP device to forward ORPort, otherwise remove mapping */
-  void configurePortForwarding(bool enable);
+  void configurePortForwarding(bool enable, bool async);
 
 protected:
   /** Virtual method called when we retrieve a server-related setting from Tor.

Modified: vidalia/branches/upnp/src/vidalia/config/upnpcontrol.cpp
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/upnpcontrol.cpp	2008-04-29 18:22:43 UTC (rev 2548)
+++ vidalia/branches/upnp/src/vidalia/config/upnpcontrol.cpp	2008-04-30 13:04:26 UTC (rev 2549)
@@ -16,6 +16,12 @@
 
 #include "upnpcontrol.h"
 
+#ifdef WIN32
+#include <winsock2.h>
+#endif
+
+#include "upnpcontrolthread.h"
+
 #define UPNPCONTROL_DEBUG 1
 
 UPNPControl* UPNPControl::pInstance = 0;
@@ -229,3 +235,46 @@
 
   freeUPNPDevlist(devlist);
 }
+
+void
+UPNPControl::configurePortForwardingAsync(bool enable, bool enableORPort, bool enableDirPort, quint16 ORPort, quint16 DirPort)
+{
+  UPNPControlThread *thread = new UPNPControlThread(enable, enableORPort, enableDirPort, ORPort, DirPort);
+  thread->start();
+  //TODO: How to de-allocate memory? What about locking?
+}
+
+void
+UPNPControl::configurePortForwardingSync(bool enable, bool enableORPort, bool enableDirPort, quint16 ORPort, quint16 DirPort)
+{
+#ifdef WIN32  
+  // Workaround from http://trolltech.com/developer/knowledgebase/579
+  WSAData wsadata;
+  if (WSAStartup(MAKEWORD(2,0), &wsadata)!=0) {
+    qFatal("WSAStartup failure while disabling UPnP port forwarding");
+  }
+#endif
+
+  UPNPControl *pUNPControl = UPNPControl::Instance();
+
+  // Remove all port forwards
+  if (!enable) {
+    pUNPControl->disableForwarding();
+    return;
+  }
+
+  // Configure port forwards, if Tor is using them
+  if (enableORPort)
+    pUNPControl->forwardORPort(ORPort);
+  else
+    pUNPControl->disableORPort();
+
+  if (enableDirPort)
+    pUNPControl->forwardDirPort(DirPort);
+  else
+    pUNPControl->disableDirPort();
+
+#ifdef WIN32
+  WSACleanup();
+#endif
+}

Modified: vidalia/branches/upnp/src/vidalia/config/upnpcontrol.h
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/upnpcontrol.h	2008-04-29 18:22:43 UTC (rev 2548)
+++ vidalia/branches/upnp/src/vidalia/config/upnpcontrol.h	2008-04-30 13:04:26 UTC (rev 2549)
@@ -38,6 +38,10 @@
   int forwardORPort(quint16 port);
   int disableDirPort();
   int disableORPort();
+
+  static void configurePortForwardingAsync(bool enable, bool enableORPort, bool enableDirPort, quint16 ORPort, quint16 DirPort);
+  static void configurePortForwardingSync(bool enable, bool enableORPort, bool enableDirPort, quint16 ORPort, quint16 DirPort);
+
 protected:
   UPNPControl();
 private:

Added: vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp	                        (rev 0)
+++ vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp	2008-04-30 13:04:26 UTC (rev 2549)
@@ -0,0 +1,33 @@
+/*
+**  This file is part of Vidalia, and is subject to the license terms in the
+**  LICENSE file, found in the top level directory of this distribution. If 
+**  you did not receive the LICENSE file with this file, you may obtain it
+**  from the Vidalia source package distributed by the Vidalia Project at
+**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+**  may be copied, modified, propagated, or distributed except according to
+**  the terms described in the LICENSE file.
+*/
+
+/* 
+** \file upnpcontrol.h
+** \version $Id: upnpcontrol.h 2499 2008-04-09 21:01:49Z edmanm $
+** \brief Thread for configuring UPnP in the background
+*/
+
+#include "upnpcontrolthread.h"
+#include "upnpcontrol.h"
+
+UPNPControlThread::UPNPControlThread(bool enable, bool enableORPort, bool enableDirPort, quint16 ORPort, quint16 DirPort)
+{
+  _enable = enable;
+  _enableORPort = enableORPort;
+  _enableDirPort = enableDirPort;
+  _ORPort = ORPort;
+  _DirPort = DirPort;
+}
+
+void
+UPNPControlThread::run()
+{
+  UPNPControl::configurePortForwardingSync(_enable, _enableORPort, _enableDirPort, _ORPort, _DirPort);
+}

Added: vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h	                        (rev 0)
+++ vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h	2008-04-30 13:04:26 UTC (rev 2549)
@@ -0,0 +1,36 @@
+/*
+**  This file is part of Vidalia, and is subject to the license terms in the
+**  LICENSE file, found in the top level directory of this distribution. If 
+**  you did not receive the LICENSE file with this file, you may obtain it
+**  from the Vidalia source package distributed by the Vidalia Project at
+**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+**  may be copied, modified, propagated, or distributed except according to
+**  the terms described in the LICENSE file.
+*/
+
+/* 
+** \file upnpcontrolthread.h
+** \version $Id: upnpcontrolthread.h 2499 2008-04-09 21:01:49Z edmanm $
+** \brief Thread for configuring UPnP in the background
+*/
+
+#ifndef _UPNPCONTROLTHREAD_H
+#define _UPNPCONTROLTHREAD_H
+
+#include <QThread>
+
+class UPNPControlThread : public QThread
+{
+  Q_OBJECT
+
+public:
+  UPNPControlThread(bool enable, bool enableORPort, bool enableDirPort, quint16 ORPort, quint16 DirPort);
+
+protected:
+  void run();
+
+private:
+  bool _enable, _enableORPort, _enableDirPort;
+  quint16 _ORPort, _DirPort;
+};
+#endif 

Modified: vidalia/branches/upnp/src/vidalia/mainwindow.cpp
===================================================================
--- vidalia/branches/upnp/src/vidalia/mainwindow.cpp	2008-04-29 18:22:43 UTC (rev 2548)
+++ vidalia/branches/upnp/src/vidalia/mainwindow.cpp	2008-04-30 13:04:26 UTC (rev 2549)
@@ -228,7 +228,7 @@
 
   /* Disable port forwarding */
   ServerSettings settings(_torControl);
-  settings.configurePortForwarding(false);
+  settings.configurePortForwarding(false, false);
 
   if (_proxyProcess->state() != QProcess::NotRunning) {
     /* Close the proxy server (Polipo ignores the WM_CLOSE event sent by
@@ -913,7 +913,7 @@
   }
 
   /* Configure UPnP port forwarding if needed */
-  serverSettings.configurePortForwarding(true);
+  serverSettings.configurePortForwarding(true, true);
 
   /* Check if Tor has a circuit established */
   if (_torControl->circuitEstablished())