Fix UPnP by reannouncing every 20 minutes.
authorMatt Corallo <matt@bluematt.me>
Tue, 31 Jan 2012 22:36:25 +0000 (17:36 -0500)
committerLuke Dashjr <luke-jr+git@utopios.org>
Thu, 2 Feb 2012 22:44:42 +0000 (17:44 -0500)
src/net.cpp

index b35a79d..f995ea9 100644 (file)
@@ -1105,11 +1105,11 @@ void ThreadMapPort2(void* parg)
     {
         string strDesc = "Bitcoin " + FormatFullVersion();
 #ifndef UPNPDISCOVER_SUCCESS
-    /* miniupnpc 1.5 */
+        /* miniupnpc 1.5 */
         r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
                             port, port, lanaddr, strDesc.c_str(), "TCP", 0);
 #else
-    /* miniupnpc 1.6 */
+        /* miniupnpc 1.6 */
         r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
                             port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
 #endif
@@ -1119,6 +1119,7 @@ void ThreadMapPort2(void* parg)
                 port, port, lanaddr, r, strupnperror(r));
         else
             printf("UPnP Port Mapping successful.\n");
+        int i = 1;
         loop {
             if (fShutdown || !fUseUPnP)
             {
@@ -1128,7 +1129,26 @@ void ThreadMapPort2(void* parg)
                 FreeUPNPUrls(&urls);
                 return;
             }
+            if (i % 600 == 0) // Refresh every 20 minutes
+            {
+#ifndef UPNPDISCOVER_SUCCESS
+                /* miniupnpc 1.5 */
+                r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
+                                    port, port, lanaddr, strDesc.c_str(), "TCP", 0);
+#else
+                /* miniupnpc 1.6 */
+                r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
+                                    port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
+#endif
+
+                if(r!=UPNPCOMMAND_SUCCESS)
+                    printf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
+                        port, port, lanaddr, r, strupnperror(r));
+                else
+                    printf("UPnP Port Mapping successful.\n");;
+            }
             Sleep(2000);
+            i++;
         }
     } else {
         printf("No valid UPnP IGDs found\n");