From 8bb5edc1c9054d835ef79c9f1ab3e021b0693303 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 26 Mar 2011 13:01:27 +0100 Subject: Allow UPnP mapping when USE_UPNP is defined and miniupnpc is installed at build time(statically linked). Thanks joepie91 for the translation of the new copyright notices into Dutch. Thanks sipa for the translation of the new copyright notices into French. Thanks megu for the translation of the new copyright notices into Spanish. Thanks justmoon/Blitzboom for the translation of the new copyright notices into German. Thanks Joozero for the translation of the new copyright notices into Italian. Remaining translations were provided by Google Translate. --- net.cpp | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) (limited to 'net.cpp') diff --git a/net.cpp b/net.cpp index 372402f51f..2e8d4f48a0 100644 --- a/net.cpp +++ b/net.cpp @@ -4,11 +4,21 @@ #include "headers.h" +#ifdef USE_UPNP +#include +#include +#include +#include +#endif + static const int MAX_OUTBOUND_CONNECTIONS = 8; void ThreadMessageHandler2(void* parg); void ThreadSocketHandler2(void* parg); void ThreadOpenConnections2(void* parg); +#ifdef USE_UPNP +void ThreadMapPort2(void* parg); +#endif bool OpenNetworkConnection(const CAddress& addrConnect); @@ -857,6 +867,109 @@ void ThreadSocketHandler2(void* parg) +#ifdef USE_UPNP +void ThreadMapPort(void* parg) +{ + IMPLEMENT_RANDOMIZE_STACK(ThreadMapPort(parg)); + try + { + vnThreadsRunning[5]++; + ThreadMapPort2(parg); + vnThreadsRunning[5]--; + } + catch (std::exception& e) { + vnThreadsRunning[5]--; + PrintException(&e, "ThreadMapPort()"); + } catch (...) { + vnThreadsRunning[5]--; + PrintException(NULL, "ThreadMapPort()"); + } + printf("ThreadMapPort exiting\n"); +} + +void ThreadMapPort2(void* parg) +{ + printf("ThreadMapPort started\n"); + + char port[6]; + sprintf(port, "%d", ntohs(GetDefaultPort())); + + const char * rootdescurl = 0; + const char * multicastif = 0; + const char * minissdpdpath = 0; + struct UPNPDev * devlist = 0; + char lanaddr[64]; + + devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0); + + struct UPNPUrls urls; + struct IGDdatas data; + int r; + + if (UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr)) == 1) + { + char intClient[16]; + char intPort[6]; + +#ifndef __WXMSW__ + r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, + port, port, lanaddr, 0, "TCP", 0); +#else + r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, + port, port, lanaddr, 0, "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"); + loop { + if (fShutdown || !fUseUPnP) + { + r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port, "TCP", 0); + printf("UPNP_DeletePortMapping() returned : %d\n", r); + freeUPNPDevlist(devlist); devlist = 0; + FreeUPNPUrls(&urls); + return; + } + Sleep(2000); + } + } else { + printf("No valid UPnP IGDs found\n"); + freeUPNPDevlist(devlist); devlist = 0; + FreeUPNPUrls(&urls); + loop { + if (fShutdown) + return; + Sleep(2000); + } + } +} + +void MapPort(bool fMapPort) +{ + if (fUseUPnP != fMapPort) + { + fUseUPnP = fMapPort; + CWalletDB().WriteSetting("fUseUPnP", fUseUPnP); + } + if (fUseUPnP && vnThreadsRunning[5] < 1) + { + if (!CreateThread(ThreadMapPort, NULL)) + printf("Error: ThreadMapPort(ThreadMapPort) failed\n"); + } +} +#endif + + + + + + + + + + static const char *strDNSSeed[] = { "bitseed.xf2.org", }; @@ -1409,6 +1522,10 @@ void StartNode(void* parg) // Start threads // + // Map ports with UPnP + if (fHaveUPnP) + MapPort(fUseUPnP); + // Get addresses from IRC and advertise ours if (!CreateThread(ThreadIRCSeed, NULL)) printf("Error: CreateThread(ThreadIRCSeed) failed\n"); @@ -1434,7 +1551,11 @@ bool StopNode() fShutdown = true; nTransactionsUpdated++; int64 nStart = GetTime(); - while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0) + while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 +#ifdef USE_UPNP + || vnThreadsRunning[5] > 0 +#endif + ) { if (GetTime() - nStart > 20) break; @@ -1445,6 +1566,7 @@ bool StopNode() if (vnThreadsRunning[2] > 0) printf("ThreadMessageHandler still running\n"); if (vnThreadsRunning[3] > 0) printf("ThreadBitcoinMiner still running\n"); if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n"); + if (fHaveUPnP && vnThreadsRunning[5] > 0) printf("ThreadMapPort still running\n"); while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0) Sleep(20); Sleep(50); -- cgit v1.2.3