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

[vidalia-svn] r2577: upnpcontrolthread.* wants svn:eol-style and svn:keywords pro (in vidalia: . branches/upnp/src/vidalia/config)



Author: edmanm
Date: 2008-05-17 15:18:48 -0400 (Sat, 17 May 2008)
New Revision: 2577

Modified:
   vidalia/
   vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp
   vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h
Log:
 r313@thebe:  edmanm | 2008-05-17 15:19:24 -0400
 upnpcontrolthread.* wants svn:eol-style and svn:keywords properties too.



Property changes on: vidalia
___________________________________________________________________
 svk:merge ticket from /local/vidalia [r313] on 45a62a8a-8088-484c-baad-c7b3e776dd32

Modified: vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp	2008-05-16 15:14:34 UTC (rev 2576)
+++ vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp	2008-05-17 19:18:48 UTC (rev 2577)
@@ -1,298 +1,298 @@
-/*
-**  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 <QWaitCondition>
-#include <QMutex>
-#include <QTime>
-#include <QTextStream>
-#include <QString>
-#include <QMessageBox>
-#include <vidalia.h>
-
-#ifdef Q_OS_WIN32
-#include <winsock2.h>
-#endif
-
-#include "upnpcontrol.h"
-
-#define UPNPCONTROL_DEBUG 1
-#define UPNPCONTROL_REINIT_MSEC 300000 // 5 minutes
-#define UPNPCONTROL_MAX_WAIT_MSEC 60000 // 1 minute
-
-UPNPControlThread::UPNPControlThread(UPNPControl *control)
-{
-  _upnpInitialized = QTime();
-  _keepRunning = true;
-  _control = control;
-
-  _dirPort = 0;
-  _orPort = 0;
-
-  _waitCondition = new QWaitCondition();
-  _waitMutex = new QMutex();
-}
-
-UPNPControlThread::~UPNPControlThread()
-{
-  delete _waitCondition;
-  delete _waitMutex;
-}
-
-void
-UPNPControlThread::run()
-{
-  bool shouldExit = false;
-
-  forever {
-    /* TODO: Check for switching OR/Dir port */
-    /* TODO: Check for router losing state */
-    
-    configurePorts();
-
-    /* Wait for something to happen */
-    _waitMutex->lock();
-    if (_keepRunning) {
-      /* We should continue */ 
-      _waitCondition->wait(_waitMutex, UPNPCONTROL_MAX_WAIT_MSEC);
-      /* Maybe we were asked to exit while waiting */
-      shouldExit = !_keepRunning;
-      _waitMutex->unlock();
-      if (shouldExit)
-        break;
-    } else {
-      /* We should exit */
-      _waitMutex->unlock();
-      break;
-    }
-  }
-
-  /* Remove the existing port forwards */
-  updatePort(_dirPort, 0);
-  updatePort(_orPort, 0);
-}
-
-void
-UPNPControlThread::configurePorts() {
-  quint16 desiredDirPort, desiredOrPort;
-  bool force_init = false;
-  int retval = 0;
-
-  vDebug("configurePorts()");
-
-  /* Get desired state */
-  _control->getDesiredState(&desiredDirPort, &desiredOrPort);
-
-  /* If it's been a while since we initialized the router, or time has gone
-     backward, then maybe the router has gone away or forgotten the forwards.
-     Reset UPnP state, and re-do the port forwarding */
-  if (_upnpInitialized.isNull() || // Is this the first time we have used UPNP?
-      _upnpInitialized>QTime::currentTime() || // Has time gone backwards?
-      _upnpInitialized.addMSecs(UPNPCONTROL_REINIT_MSEC)<QTime::currentTime() // Has it been REINIT_MSEC since initialization
-      ) {
-    _upnpInitialized = QTime();
-    force_init = true;
-  }
-  
-  if (!force_init) {
-    /* Configure DirPort */
-    if (desiredDirPort != _dirPort) {
-      retval = updatePort(_dirPort, desiredDirPort);
-      if (!retval)
-	_dirPort = desiredDirPort;
-    }
-    
-    /* Configure ORPort */
-    if (desiredOrPort != _orPort) {
-      retval = updatePort(_orPort, desiredOrPort);
-      if (!retval)
-        _orPort = desiredOrPort;
-    }
-  } else {
-    /* Add the mapping even if they exist already */
-    retval = updatePort(0, desiredDirPort);
-    if (!retval)
-      _dirPort = desiredDirPort;
-    
-    retval = updatePort(0, desiredOrPort);
-    if (!retval)
-      _orPort = desiredOrPort;
-  }
-
-  vInfo("configurePorts() result: %1").arg(retval);
-    
-  if (retval) {
-    QString message;
-    QTextStream(&message) << "Failed to configure automatic port forwarding (status: " << retval << ")";
-    QMessageBox::warning(0, QString("Automatic port forwarding"), message);
-  }
-}
-
-void
-UPNPControlThread::stop()
-{
-  /* Lock access to _keepRunning */
-  _waitMutex->lock();
-
-  /* Ask the thread to stop */
-  _keepRunning = false;
-
-  /* Wake it up if needed */
-  _waitCondition->wakeAll();
-
-  /* Unlock shared state */
-  _waitMutex->unlock();
-
-  /* Wait for it to finish */
-  wait();
-}
-
-void
-UPNPControlThread::wakeup()
-{
-  _waitMutex->lock();
-  _waitCondition->wakeAll();
-  _waitMutex->unlock();
-}
-
-int
-UPNPControlThread::updatePort(quint16 oldPort, quint16 newPort)
-{
-  int retval;
-
-#ifdef Q_OS_WIN32  
-  // Workaround from http://trolltech.com/developer/knowledgebase/579
-  WSAData wsadata;
-  if (WSAStartup(MAKEWORD(2,0), &wsadata)!=0) {
-    vWarn("WSAStartup failure while disabling UPnP port forwarding");
-  }
-#endif
-
-  if (_upnpInitialized.isNull() && (oldPort!=0 || newPort !=0)) {
-    retval = init_upnp();
-    if (0 == retval)
-      _upnpInitialized = QTime::currentTime();
-    else
-      _upnpInitialized = QTime();
-  } else {
-    retval = 0;
-  }
-
-  if (!retval && oldPort != 0)
-    retval = disablePort(oldPort);
-
-  if (!retval && newPort != 0)
-    retval = forwardPort(newPort);
-
-#ifdef Q_OS_WIN32
-  WSACleanup();
-#endif
-
-  return retval;
-}
-
-/** Based on http://miniupnp.free.fr/files/download.php?file=xchat-upnp20061022.patch */
-int
-UPNPControlThread::init_upnp()
-{
-  struct UPNPDev * devlist;
-  int retval;
-
-  memset(&urls, 0, sizeof(struct UPNPUrls));
-  memset(&data, 0, sizeof(struct IGDdatas));
-
-  devlist = upnpDiscover(2000, NULL, NULL);
-  if (NULL == devlist) {
-    vWarn("upnpDiscover returned: NULL");
-    return 5;
-  }
-
-  retval = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
-
-  vInfo("GetValidIGD returned: %1").arg(retval);
-
-  freeUPNPDevlist(devlist);
-
-  if (retval != 1 && retval != 2)
-    return 6;
-
-  return 0;
-}
-
-int
-UPNPControlThread::forwardPort(quint16 port)
-{
-  int retval;
-  
-  char sPort[6];
-  
-  char intClient[16];
-  char intPort[6];
-
-  // Convert the port number to a string
-  snprintf(sPort, sizeof(sPort), "%d", port);
-
-  // Add the port mapping of external:port -> internal:port
-  retval = UPNP_AddPortMapping(urls.controlURL, data.servicetype,
-                               sPort, sPort, lanaddr, "Tor server", "TCP");
-  if(UPNPCOMMAND_SUCCESS != retval) {
-    vWarn("AddPortMapping(%1, %2, %3) failed with code %4")
-            .arg(sPort).arg(sPort).arg(lanaddr).arg(retval);
-    return 1;
-  }
-  
-  // Check if the port mapping was accepted
-  retval = UPNP_GetSpecificPortMappingEntry(urls.controlURL, data.servicetype,
-                                            sPort, "TCP", intClient, intPort);
-  if(UPNPCOMMAND_SUCCESS != retval) {
-    vWarn("GetSpecificPortMappingEntry() failed with code %1").arg(retval);
-    return 2;
-  }
-  
-  if(! intClient[0]) {
-    vWarn("GetSpecificPortMappingEntry failed.");
-    return 3;
-  }
-  
-  // Output the mapping
-  vInfo("(external):%1 -> %2:%3").arg(sPort).arg(intClient).arg(intPort);
-
-  return 0;
-}
-
-int
-UPNPControlThread::disablePort(quint16 port)
-{
-  char sPort[6];
-  
-  // Convert the port number to a string
-  snprintf(sPort, sizeof(sPort), "%d", port);
-  
-  // Remove the mapping
-  int retval = UPNP_DeletePortMapping(urls.controlURL, data.servicetype,
-                                      sPort, "TCP");
-  if(UPNPCOMMAND_SUCCESS != retval) {
-    vWarn("DeletePortMapping() failed with code %1").arg(retval);
-    return 4;
-  }
-  
-  // Output the cancelled mapping
-  vInfo("(external):%1 -> <>").arg(sPort);
-  
-  return 0;
-}
-
+/*
+**  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$
+** \brief Thread for configuring UPnP in the background
+*/
+
+#include "upnpcontrolthread.h"
+
+#include <QWaitCondition>
+#include <QMutex>
+#include <QTime>
+#include <QTextStream>
+#include <QString>
+#include <QMessageBox>
+#include <vidalia.h>
+
+#ifdef Q_OS_WIN32
+#include <winsock2.h>
+#endif
+
+#include "upnpcontrol.h"
+
+#define UPNPCONTROL_DEBUG 1
+#define UPNPCONTROL_REINIT_MSEC 300000 // 5 minutes
+#define UPNPCONTROL_MAX_WAIT_MSEC 60000 // 1 minute
+
+UPNPControlThread::UPNPControlThread(UPNPControl *control)
+{
+  _upnpInitialized = QTime();
+  _keepRunning = true;
+  _control = control;
+
+  _dirPort = 0;
+  _orPort = 0;
+
+  _waitCondition = new QWaitCondition();
+  _waitMutex = new QMutex();
+}
+
+UPNPControlThread::~UPNPControlThread()
+{
+  delete _waitCondition;
+  delete _waitMutex;
+}
+
+void
+UPNPControlThread::run()
+{
+  bool shouldExit = false;
+
+  forever {
+    /* TODO: Check for switching OR/Dir port */
+    /* TODO: Check for router losing state */
+    
+    configurePorts();
+
+    /* Wait for something to happen */
+    _waitMutex->lock();
+    if (_keepRunning) {
+      /* We should continue */ 
+      _waitCondition->wait(_waitMutex, UPNPCONTROL_MAX_WAIT_MSEC);
+      /* Maybe we were asked to exit while waiting */
+      shouldExit = !_keepRunning;
+      _waitMutex->unlock();
+      if (shouldExit)
+        break;
+    } else {
+      /* We should exit */
+      _waitMutex->unlock();
+      break;
+    }
+  }
+
+  /* Remove the existing port forwards */
+  updatePort(_dirPort, 0);
+  updatePort(_orPort, 0);
+}
+
+void
+UPNPControlThread::configurePorts() {
+  quint16 desiredDirPort, desiredOrPort;
+  bool force_init = false;
+  int retval = 0;
+
+  vDebug("configurePorts()");
+
+  /* Get desired state */
+  _control->getDesiredState(&desiredDirPort, &desiredOrPort);
+
+  /* If it's been a while since we initialized the router, or time has gone
+     backward, then maybe the router has gone away or forgotten the forwards.
+     Reset UPnP state, and re-do the port forwarding */
+  if (_upnpInitialized.isNull() || // Is this the first time we have used UPNP?
+      _upnpInitialized>QTime::currentTime() || // Has time gone backwards?
+      _upnpInitialized.addMSecs(UPNPCONTROL_REINIT_MSEC)<QTime::currentTime() // Has it been REINIT_MSEC since initialization
+      ) {
+    _upnpInitialized = QTime();
+    force_init = true;
+  }
+  
+  if (!force_init) {
+    /* Configure DirPort */
+    if (desiredDirPort != _dirPort) {
+      retval = updatePort(_dirPort, desiredDirPort);
+      if (!retval)
+	_dirPort = desiredDirPort;
+    }
+    
+    /* Configure ORPort */
+    if (desiredOrPort != _orPort) {
+      retval = updatePort(_orPort, desiredOrPort);
+      if (!retval)
+        _orPort = desiredOrPort;
+    }
+  } else {
+    /* Add the mapping even if they exist already */
+    retval = updatePort(0, desiredDirPort);
+    if (!retval)
+      _dirPort = desiredDirPort;
+    
+    retval = updatePort(0, desiredOrPort);
+    if (!retval)
+      _orPort = desiredOrPort;
+  }
+
+  vInfo("configurePorts() result: %1").arg(retval);
+    
+  if (retval) {
+    QString message;
+    QTextStream(&message) << "Failed to configure automatic port forwarding (status: " << retval << ")";
+    QMessageBox::warning(0, QString("Automatic port forwarding"), message);
+  }
+}
+
+void
+UPNPControlThread::stop()
+{
+  /* Lock access to _keepRunning */
+  _waitMutex->lock();
+
+  /* Ask the thread to stop */
+  _keepRunning = false;
+
+  /* Wake it up if needed */
+  _waitCondition->wakeAll();
+
+  /* Unlock shared state */
+  _waitMutex->unlock();
+
+  /* Wait for it to finish */
+  wait();
+}
+
+void
+UPNPControlThread::wakeup()
+{
+  _waitMutex->lock();
+  _waitCondition->wakeAll();
+  _waitMutex->unlock();
+}
+
+int
+UPNPControlThread::updatePort(quint16 oldPort, quint16 newPort)
+{
+  int retval;
+
+#ifdef Q_OS_WIN32  
+  // Workaround from http://trolltech.com/developer/knowledgebase/579
+  WSAData wsadata;
+  if (WSAStartup(MAKEWORD(2,0), &wsadata)!=0) {
+    vWarn("WSAStartup failure while disabling UPnP port forwarding");
+  }
+#endif
+
+  if (_upnpInitialized.isNull() && (oldPort!=0 || newPort !=0)) {
+    retval = init_upnp();
+    if (0 == retval)
+      _upnpInitialized = QTime::currentTime();
+    else
+      _upnpInitialized = QTime();
+  } else {
+    retval = 0;
+  }
+
+  if (!retval && oldPort != 0)
+    retval = disablePort(oldPort);
+
+  if (!retval && newPort != 0)
+    retval = forwardPort(newPort);
+
+#ifdef Q_OS_WIN32
+  WSACleanup();
+#endif
+
+  return retval;
+}
+
+/** Based on http://miniupnp.free.fr/files/download.php?file=xchat-upnp20061022.patch */
+int
+UPNPControlThread::init_upnp()
+{
+  struct UPNPDev * devlist;
+  int retval;
+
+  memset(&urls, 0, sizeof(struct UPNPUrls));
+  memset(&data, 0, sizeof(struct IGDdatas));
+
+  devlist = upnpDiscover(2000, NULL, NULL);
+  if (NULL == devlist) {
+    vWarn("upnpDiscover returned: NULL");
+    return 5;
+  }
+
+  retval = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
+
+  vInfo("GetValidIGD returned: %1").arg(retval);
+
+  freeUPNPDevlist(devlist);
+
+  if (retval != 1 && retval != 2)
+    return 6;
+
+  return 0;
+}
+
+int
+UPNPControlThread::forwardPort(quint16 port)
+{
+  int retval;
+  
+  char sPort[6];
+  
+  char intClient[16];
+  char intPort[6];
+
+  // Convert the port number to a string
+  snprintf(sPort, sizeof(sPort), "%d", port);
+
+  // Add the port mapping of external:port -> internal:port
+  retval = UPNP_AddPortMapping(urls.controlURL, data.servicetype,
+                               sPort, sPort, lanaddr, "Tor server", "TCP");
+  if(UPNPCOMMAND_SUCCESS != retval) {
+    vWarn("AddPortMapping(%1, %2, %3) failed with code %4")
+            .arg(sPort).arg(sPort).arg(lanaddr).arg(retval);
+    return 1;
+  }
+  
+  // Check if the port mapping was accepted
+  retval = UPNP_GetSpecificPortMappingEntry(urls.controlURL, data.servicetype,
+                                            sPort, "TCP", intClient, intPort);
+  if(UPNPCOMMAND_SUCCESS != retval) {
+    vWarn("GetSpecificPortMappingEntry() failed with code %1").arg(retval);
+    return 2;
+  }
+  
+  if(! intClient[0]) {
+    vWarn("GetSpecificPortMappingEntry failed.");
+    return 3;
+  }
+  
+  // Output the mapping
+  vInfo("(external):%1 -> %2:%3").arg(sPort).arg(intClient).arg(intPort);
+
+  return 0;
+}
+
+int
+UPNPControlThread::disablePort(quint16 port)
+{
+  char sPort[6];
+  
+  // Convert the port number to a string
+  snprintf(sPort, sizeof(sPort), "%d", port);
+  
+  // Remove the mapping
+  int retval = UPNP_DeletePortMapping(urls.controlURL, data.servicetype,
+                                      sPort, "TCP");
+  if(UPNPCOMMAND_SUCCESS != retval) {
+    vWarn("DeletePortMapping() failed with code %1").arg(retval);
+    return 4;
+  }
+  
+  // Output the cancelled mapping
+  vInfo("(external):%1 -> <>").arg(sPort);
+  
+  return 0;
+}
+


Property changes on: vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h
===================================================================
--- vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h	2008-05-16 15:14:34 UTC (rev 2576)
+++ vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h	2008-05-17 19:18:48 UTC (rev 2577)
@@ -1,65 +1,65 @@
-/*
-**  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>
-#include <QMutex>
-#include <QWaitCondition>
-#include <QTime>
-
-#define STATICLIB
-#include <miniupnpc/miniwget.h>
-#include <miniupnpc/miniupnpc.h>
-#include <miniupnpc/upnpcommands.h>
-
-/* Forward declaration to make it build */
-class UPNPControl;
-
-class UPNPControlThread : public QThread
-{
-  Q_OBJECT
-
-public:
-  UPNPControlThread(UPNPControl *control);
-  ~UPNPControlThread();
-  void stop();
-  void wakeup();
-
-protected:
-  void run();
-
-private:
-  QTime _upnpInitialized;
-  bool _keepRunning;
-  UPNPControl *_control;
-  QWaitCondition *_waitCondition;
-  QMutex *_waitMutex;
-  quint16 _dirPort, _orPort;
-
-  /** Used by miniupnpc library */
-  struct UPNPUrls urls;
-  struct IGDdatas data;
-  char lanaddr[16];
-
-  void configurePorts();
-  int init_upnp();
-  int updatePort(quint16 oldPort, quint16 newPort);
-  int forwardPort(quint16 port);
-  int disablePort(quint16 port);
-};
-#endif 
+/*
+**  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$
+** \brief Thread for configuring UPnP in the background
+*/
+
+#ifndef _UPNPCONTROLTHREAD_H
+#define _UPNPCONTROLTHREAD_H
+
+#include <QThread>
+#include <QMutex>
+#include <QWaitCondition>
+#include <QTime>
+
+#define STATICLIB
+#include <miniupnpc/miniwget.h>
+#include <miniupnpc/miniupnpc.h>
+#include <miniupnpc/upnpcommands.h>
+
+/* Forward declaration to make it build */
+class UPNPControl;
+
+class UPNPControlThread : public QThread
+{
+  Q_OBJECT
+
+public:
+  UPNPControlThread(UPNPControl *control);
+  ~UPNPControlThread();
+  void stop();
+  void wakeup();
+
+protected:
+  void run();
+
+private:
+  QTime _upnpInitialized;
+  bool _keepRunning;
+  UPNPControl *_control;
+  QWaitCondition *_waitCondition;
+  QMutex *_waitMutex;
+  quint16 _dirPort, _orPort;
+
+  /** Used by miniupnpc library */
+  struct UPNPUrls urls;
+  struct IGDdatas data;
+  char lanaddr[16];
+
+  void configurePorts();
+  int init_upnp();
+  int updatePort(quint16 oldPort, quint16 newPort);
+  int forwardPort(quint16 port);
+  int disablePort(quint16 port);
+};
+#endif 


Property changes on: vidalia/branches/upnp/src/vidalia/config/upnpcontrolthread.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native