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

[vidalia-svn] r2475: Initial merge of sjmurdoch's patch to add UPnP support using (in vidalia/trunk: . src/vidalia src/vidalia/config)



Author: edmanm
Date: 2008-04-01 22:00:32 -0400 (Tue, 01 Apr 2008)
New Revision: 2475

Added:
   vidalia/trunk/src/vidalia/config/upnpcontrol.cpp
   vidalia/trunk/src/vidalia/config/upnpcontrol.h
Modified:
   vidalia/trunk/
   vidalia/trunk/CREDITS
   vidalia/trunk/INSTALL
   vidalia/trunk/src/vidalia/CMakeLists.txt
   vidalia/trunk/src/vidalia/config/serversettings.cpp
   vidalia/trunk/src/vidalia/config/serversettings.h
Log:
 r289@lysithea:  edmanm | 2008-04-01 20:23:53 -0400
 Initial merge of sjmurdoch's patch to add UPnP support using the MiniUPnPc
 library.



Property changes on: vidalia/trunk
___________________________________________________________________
 svk:merge ticket from /local/vidalia/trunk [r289] on 90112fd6-a33b-4cea-8d39-48ff1d78625c

Modified: vidalia/trunk/CREDITS
===================================================================
--- vidalia/trunk/CREDITS	2008-04-02 02:00:24 UTC (rev 2474)
+++ vidalia/trunk/CREDITS	2008-04-02 02:00:32 UTC (rev 2475)
@@ -33,7 +33,8 @@
 
   Steven J. Murdoch <http://www.cl.cam.ac.uk/users/sjm217/> wrote the code
   to launch a web browser when Tor has built a circuit and close Vidalia when
-  the browser has exited.
+  the browser has exited. He also implemented UPnP support using the MiniUPNPc
+  library.
 
   Brandon Nase <http://www.students.dsu.edu/naseb/> designed and built 
   the Vidalia-Project website.

Modified: vidalia/trunk/INSTALL
===================================================================
--- vidalia/trunk/INSTALL	2008-04-02 02:00:24 UTC (rev 2474)
+++ vidalia/trunk/INSTALL	2008-04-02 02:00:32 UTC (rev 2475)
@@ -206,3 +206,8 @@
 'cmake --help' or 'man cmake' (on non-Windows platforms) for more information
 about supported generators and configuration options.
 
+Including UPnP support
+----------------------
+
+cmake -G "MSYS Makefiles" -DMINIUPNPC_LIBRARY_DIR="/usr/local/lib" -DMINIUPNPC_INCLUDE_DIR="/usr/local/include" .
+

Modified: vidalia/trunk/src/vidalia/CMakeLists.txt
===================================================================
--- vidalia/trunk/src/vidalia/CMakeLists.txt	2008-04-02 02:00:24 UTC (rev 2474)
+++ vidalia/trunk/src/vidalia/CMakeLists.txt	2008-04-02 02:00:32 UTC (rev 2475)
@@ -16,6 +16,7 @@
   ${CMAKE_CURRENT_SOURCE_DIR}
   ${CMAKE_CURRENT_SOURCE_DIR}/config
   ${CMAKE_CURRENT_SOURCE_DIR}/help/browser
+  ${MINIUPNPC_INCLUDE_DIR}
 )
 configure_file(
   ${CMAKE_CURRENT_SOURCE_DIR}/res/vidalia_win.rc.in
@@ -61,6 +62,7 @@
   config/portvalidator.cpp
   config/serverpage.cpp
   config/serversettings.cpp
+  config/upnpcontrol.cpp
   config/torsettings.cpp
   config/vidaliasettings.cpp
   config/vsettings.cpp
@@ -80,6 +82,7 @@
   config/portvalidator.h
   config/serverpage.h
   config/serversettings.h
+  config/upnpcontrol.h
   config/torsettings.h
   config/vidaliasettings.h
   config/vsettings.h
@@ -234,9 +237,16 @@
 endif(APPLE)
 add_dependencies(${vidalia_BIN} translations)
 
+## Link in miniupnpc
+find_library(MINIUPNPC
+    NAMES miniupnpc
+    PATHS ${MINIUPNPC_LIBRARY_DIR}
+)
+
 ## Link to the Qt libraries and other libraries built as a part of Vidalia
 target_link_libraries(${vidalia_BIN}
   ${QT_LIBRARIES}
+  ${MINIUPNPC}
   torcontrol
   util
 )

Modified: vidalia/trunk/src/vidalia/config/serversettings.cpp
===================================================================
--- vidalia/trunk/src/vidalia/config/serversettings.cpp	2008-04-02 02:00:24 UTC (rev 2474)
+++ vidalia/trunk/src/vidalia/config/serversettings.cpp	2008-04-02 02:00:32 UTC (rev 2475)
@@ -20,6 +20,7 @@
 
 #include "serversettings.h"
 #include "torsettings.h"
+#include "upnpcontrol.h"
 
 /** Define the set of characters that are valid in a nickname. */
 #define VALID_NICKNAME_CHARS \
@@ -130,6 +131,9 @@
   bool rc;
 
   if (isServerEnabled()) {
+    /* Configure UPnP device to forward DirPort and OrPort */
+    /* TODO: does isServerEnabled() return true when a server is just set up? */
+    configurePortForwarding();
     rc = torControl()->setConf(confValues(), errmsg);
   } else { 
     QStringList resetKeys;
@@ -152,6 +156,17 @@
   return rc;
 }
 
+/* TODO: We should call this periodically, in case the router gets rebooted or forgets its UPnP settings */
+/* TODO: Remove port forwarding when Tor is shutdown or the ORPort changes */
+/* TODO: init_upnp() will block for up to 2 seconds. We should fire off a thread */
+/** Configure UPnP device to forward DirPort and ORPort */
+void
+ServerSettings::configurePortForwarding()
+{
+  UPNPControl *pUNPControl = UPNPControl::Instance();
+  pUNPControl->forwardPort(getORPort());
+}
+
 /** Virtual method called when we retrieve a server-related setting from Tor.
  * Currently this just translates BandwidthFoo to RelayBandwidthFoo when
  * appropriate. */

Modified: vidalia/trunk/src/vidalia/config/serversettings.h
===================================================================
--- vidalia/trunk/src/vidalia/config/serversettings.h	2008-04-02 02:00:24 UTC (rev 2474)
+++ vidalia/trunk/src/vidalia/config/serversettings.h	2008-04-02 02:00:32 UTC (rev 2475)
@@ -91,6 +91,9 @@
 private:
   /** Returns Tor-recognizable configuration keys and current values. */
   QHash<QString,QString> confValues();
+
+  /** Configure UPnP device to forward DirPort and ORPort */
+  void configurePortForwarding();
 };
 
 #endif

Added: vidalia/trunk/src/vidalia/config/upnpcontrol.cpp
===================================================================
--- vidalia/trunk/src/vidalia/config/upnpcontrol.cpp	                        (rev 0)
+++ vidalia/trunk/src/vidalia/config/upnpcontrol.cpp	2008-04-02 02:00:32 UTC (rev 2475)
@@ -0,0 +1,129 @@
+/*
+**  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.cpp
+** \version $Id$
+** \brief Singleton object for interacting with UPNP device
+*/
+
+#include "upnpcontrol.h"
+
+UPNPControl* UPNPControl::pInstance = 0;
+UPNPControl* UPNPControl::Instance()
+{
+  if (0 == pInstance)
+    pInstance = new UPNPControl;
+  return pInstance;
+}
+
+UPNPControl::UPNPControl()
+{
+  init_upnp();
+}
+
+int
+UPNPControl::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) {
+    printf("AddPortMapping(%s, %s, %s) failed with code %d\n",
+	   sPort, sPort, lanaddr, 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) {
+    printf("GetSpecificPortMappingEntry() failed with code %d\n", retval);
+    return 2;
+  }
+  
+  if(! intClient[0]) {
+    printf("GetSpecificPortMappingEntry failed.\n");
+    return 3;
+  }
+  
+  // Output the mapping
+  printf("(external):%s -> %s:%s\n", sPort, intClient, intPort);
+  return 0;
+}
+
+/** Based on http://miniupnp.free.fr/files/download.php?file=xchat-upnp20061022.patch */
+void
+UPNPControl::init_upnp()
+{
+  struct UPNPDev * devlist;
+  int retval;
+
+  printf("TB : init_upnp()\n");
+
+  memset(&urls, 0, sizeof(struct UPNPUrls));
+  memset(&data, 0, sizeof(struct IGDdatas));
+
+  devlist = upnpDiscover(2000, NULL, NULL);
+  retval = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
+  printf("UPNP: %d", retval);
+
+  freeUPNPDevlist(devlist);
+}
+
+/** Based on http://miniupnp.free.fr/files/download.php?file=xchat-upnp20061022.patch */
+void
+UPNPControl::upnp_add_redir(const char * addr, int port)
+{
+	char port_str[16];
+	int r;
+	printf("TB : upnp_add_redir (%s, %d)\n", addr, port);
+	if(urls.controlURL[0] == '\0')
+	{
+		printf("TB : the init was not done !\n");
+		fflush(stdout);
+		return;
+	}
+	
+	r = UPNP_AddPortMapping(urls.controlURL, data.servicetype,
+	                        port_str, port_str, addr, 0, "TCP");
+	if(r==0)
+		printf("AddPortMapping(%s, %s, %s) failed\n", port_str, port_str, addr);
+	fflush(stdout);
+}
+
+/** Based on http://miniupnp.free.fr/files/download.php?file=xchat-upnp20061022.patch */
+void
+UPNPControl::upnp_rem_redir(int port)
+{
+	char port_str[16];
+	int t;
+	printf("TB : upnp_rem_redir (%d)\n", port);
+	if(urls.controlURL[0] == '\0')
+	{
+		printf("TB : the init was not done !\n");
+		fflush(stdout);
+		return;
+	}
+	sprintf(port_str, "%d", port);
+	UPNP_DeletePortMapping(urls.controlURL, data.servicetype, port_str, "TCP");
+}


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

Added: vidalia/trunk/src/vidalia/config/upnpcontrol.h
===================================================================
--- vidalia/trunk/src/vidalia/config/upnpcontrol.h	                        (rev 0)
+++ vidalia/trunk/src/vidalia/config/upnpcontrol.h	2008-04-02 02:00:32 UTC (rev 2475)
@@ -0,0 +1,48 @@
+/*
+**  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 Singleton object for interacting with UPNP device
+*/
+
+#ifndef _UPNPCONTROL_H
+#define _UPNPCONTROL_H
+
+#include <QObject>
+
+#define STATICLIB
+#include <miniupnpc/miniwget.h>
+#include <miniupnpc/miniupnpc.h>
+#include <miniupnpc/upnpcommands.h>
+
+class UPNPControl : public QObject
+{
+  Q_OBJECT
+
+public:
+  static UPNPControl* Instance();
+  int forwardPort(quint16 port);
+protected:
+  UPNPControl();
+private:
+  static UPNPControl* pInstance;
+
+  /** Used by miniupnpc library */
+  struct UPNPUrls urls;
+  struct IGDdatas data;
+  char lanaddr[16];
+  void init_upnp();
+  void upnp_add_redir (const char * addr, int port);
+  void upnp_rem_redir(int port);
+};
+
+#endif 


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