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

[vidalia-svn] r2499: Patch from sjmurdoch to delete the UPnP mapping when the use (in vidalia/trunk: . src/vidalia/config)



Author: edmanm
Date: 2008-04-09 17:01:49 -0400 (Wed, 09 Apr 2008)
New Revision: 2499

Modified:
   vidalia/trunk/
   vidalia/trunk/src/vidalia/config/serversettings.cpp
   vidalia/trunk/src/vidalia/config/serversettings.h
   vidalia/trunk/src/vidalia/config/upnpcontrol.cpp
   vidalia/trunk/src/vidalia/config/upnpcontrol.h
Log:
 r336@lysithea:  edmanm | 2008-04-09 16:59:05 -0400
 Patch from sjmurdoch to delete the UPnP mapping when the user is no longer a
 relay.



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

Modified: vidalia/trunk/src/vidalia/config/serversettings.cpp
===================================================================
--- vidalia/trunk/src/vidalia/config/serversettings.cpp	2008-04-09 02:08:22 UTC (rev 2498)
+++ vidalia/trunk/src/vidalia/config/serversettings.cpp	2008-04-09 21:01:49 UTC (rev 2499)
@@ -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();
+    configurePortForwarding(true);
     rc = torControl()->setConf(confValues(), errmsg);
   } else { 
     QStringList resetKeys;
@@ -156,6 +156,7 @@
                 << SETTING_BANDWIDTH_BURST;
     }
     rc = torControl()->resetConf(resetKeys, errmsg);
+    configurePortForwarding(false);
   }
   return rc;
 }
@@ -165,11 +166,16 @@
 /* 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()
+ServerSettings::configurePortForwarding(boolean enable)
 {
 #ifdef USE_MINIUPNPC
   UPNPControl *pUNPControl = UPNPControl::Instance();
-  pUNPControl->forwardPort(getORPort());
+
+  if (enable) {
+    pUNPControl->forwardPort(getORPort());
+  } else {
+    pUNPControl->disableForwarding();
+  }
 #endif
 }
 

Modified: vidalia/trunk/src/vidalia/config/serversettings.h
===================================================================
--- vidalia/trunk/src/vidalia/config/serversettings.h	2008-04-09 02:08:22 UTC (rev 2498)
+++ vidalia/trunk/src/vidalia/config/serversettings.h	2008-04-09 21:01:49 UTC (rev 2499)
@@ -93,7 +93,7 @@
   QHash<QString,QString> confValues();
 
   /** Configure UPnP device to forward DirPort and ORPort */
-  void configurePortForwarding();
+  void configurePortForwarding(boolean enable);
 };
 
 #endif

Modified: vidalia/trunk/src/vidalia/config/upnpcontrol.cpp
===================================================================
--- vidalia/trunk/src/vidalia/config/upnpcontrol.cpp	2008-04-09 02:08:22 UTC (rev 2498)
+++ vidalia/trunk/src/vidalia/config/upnpcontrol.cpp	2008-04-09 21:01:49 UTC (rev 2499)
@@ -27,6 +27,7 @@
 UPNPControl::UPNPControl()
 {
   init_upnp();
+  forwardedPort = 0;
 }
 
 int
@@ -68,9 +69,42 @@
   
   // Output the mapping
   printf("(external):%s -> %s:%s\n", sPort, intClient, intPort);
+  fflush(stdout);
+
+  // Save the mapping
+  forwardedPort = port;
+
   return 0;
 }
 
+int
+UPNPControl::disableForwarding()
+{
+  char sPort[6];
+
+  if (0 == forwardedPort)
+    return 0;
+
+  // Convert the port number to a string
+  snprintf(sPort, sizeof(sPort), "%d", forwardedPort);
+
+  int retval = UPNP_DeletePortMapping(urls.controlURL, data.servicetype, sPort, "TCP");
+  if(UPNPCOMMAND_SUCCESS != retval) {
+    printf("DeletePortMapping() failed with code %d\n", retval);
+    return 1;
+  }
+
+  // Output the cancelled mapping
+  printf("(external):%s -> <>\n", sPort);
+  fflush(stdout);
+
+  // Save the mapping
+  forwardedPort = 0;
+
+  return 0;
+}
+
+
 /** Based on http://miniupnp.free.fr/files/download.php?file=xchat-upnp20061022.patch */
 void
 UPNPControl::init_upnp()
@@ -78,52 +112,13 @@
   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);
+  printf("GetValidIGD returned: %d\n", retval);
+  fflush(stdout);
 
   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");
-}

Modified: vidalia/trunk/src/vidalia/config/upnpcontrol.h
===================================================================
--- vidalia/trunk/src/vidalia/config/upnpcontrol.h	2008-04-09 02:08:22 UTC (rev 2498)
+++ vidalia/trunk/src/vidalia/config/upnpcontrol.h	2008-04-09 21:01:49 UTC (rev 2499)
@@ -31,6 +31,7 @@
 public:
   static UPNPControl* Instance();
   int forwardPort(quint16 port);
+  int disableForwarding();
 protected:
   UPNPControl();
 private:
@@ -43,6 +44,9 @@
   void init_upnp();
   void upnp_add_redir (const char * addr, int port);
   void upnp_rem_redir(int port);
+
+  /* Currently forwarded port */
+  quint16 forwardedPort;
 };
 
 #endif