|
| 1 | +// Copyright (c) 2011-2020 The Bitcoin Core developers |
| 2 | +// Copyright (c) 2021 The PIVX developers |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +#if defined(HAVE_CONFIG_H) |
| 7 | +#include "config/pivx-config.h" |
| 8 | +#endif |
| 9 | + |
| 10 | +#include "mapport.h" |
| 11 | + |
| 12 | +#include "clientversion.h" |
| 13 | +#include "logging.h" |
| 14 | +#include "net.h" |
| 15 | +#include "netaddress.h" |
| 16 | +#include "netbase.h" |
| 17 | +#include "threadinterrupt.h" |
| 18 | +#include "util/system.h" |
| 19 | + |
| 20 | +#ifdef USE_UPNP |
| 21 | +#include <miniupnpc/miniupnpc.h> |
| 22 | +#include <miniupnpc/upnpcommands.h> |
| 23 | +#include <miniupnpc/upnperrors.h> |
| 24 | +// The minimum supported miniUPnPc API version is set to 10. This keeps compatibility |
| 25 | +// with Ubuntu 16.04 LTS and Debian 8 libminiupnpc-dev packages. |
| 26 | +static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"); |
| 27 | +#endif |
| 28 | + |
| 29 | +#include <cassert> |
| 30 | +#include <chrono> |
| 31 | +#include <functional> |
| 32 | +#include <string> |
| 33 | +#include <thread> |
| 34 | + |
| 35 | +#ifdef USE_UPNP |
| 36 | +static CThreadInterrupt g_upnp_interrupt; |
| 37 | +static std::thread g_upnp_thread; |
| 38 | +void ThreadMapPort() |
| 39 | +{ |
| 40 | + std::string port = strprintf("%u", GetListenPort()); |
| 41 | + const char* multicastif = 0; |
| 42 | + const char* minissdpdpath = 0; |
| 43 | + struct UPNPDev* devlist = 0; |
| 44 | + char lanaddr[64]; |
| 45 | + |
| 46 | + int error = 0; |
| 47 | +#if MINIUPNPC_API_VERSION < 14 |
| 48 | + devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error); |
| 49 | +#else |
| 50 | + devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error); |
| 51 | +#endif |
| 52 | + |
| 53 | + struct UPNPUrls urls; |
| 54 | + struct IGDdatas data; |
| 55 | + int r; |
| 56 | + |
| 57 | + r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr)); |
| 58 | + if (r == 1) { |
| 59 | + if (fDiscover) { |
| 60 | + char externalIPAddress[40]; |
| 61 | + r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress); |
| 62 | + if (r != UPNPCOMMAND_SUCCESS) { |
| 63 | + LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r); |
| 64 | + } else { |
| 65 | + if (externalIPAddress[0]) { |
| 66 | + CNetAddr resolved; |
| 67 | + if (LookupHost(externalIPAddress, resolved, false)) { |
| 68 | + LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str()); |
| 69 | + AddLocal(resolved, LOCAL_UPNP); |
| 70 | + } |
| 71 | + } else { |
| 72 | + LogPrintf("UPnP: GetExternalIPAddress failed.\n"); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + std::string strDesc = PACKAGE_NAME " " + FormatFullVersion(); |
| 78 | + |
| 79 | + do { |
| 80 | + r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0"); |
| 81 | + |
| 82 | + if (r != UPNPCOMMAND_SUCCESS) { |
| 83 | + LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n", port, port, lanaddr, r, strupnperror(r)); |
| 84 | + } else { |
| 85 | + LogPrintf("UPnP Port Mapping successful.\n"); |
| 86 | + } |
| 87 | + } while(g_upnp_interrupt.sleep_for(std::chrono::minutes(20))); |
| 88 | + |
| 89 | + r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0); |
| 90 | + LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r); |
| 91 | + freeUPNPDevlist(devlist); devlist = nullptr; |
| 92 | + FreeUPNPUrls(&urls); |
| 93 | + } else { |
| 94 | + LogPrintf("No valid UPnP IGDs found\n"); |
| 95 | + freeUPNPDevlist(devlist); |
| 96 | + devlist = 0; |
| 97 | + if (r != 0) |
| 98 | + FreeUPNPUrls(&urls); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void StartMapPort() |
| 103 | +{ |
| 104 | + if (!g_upnp_thread.joinable()) { |
| 105 | + assert(!g_upnp_interrupt); |
| 106 | + g_upnp_thread = std::thread(std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void InterruptMapPort() |
| 111 | +{ |
| 112 | + if (g_upnp_thread.joinable()) { |
| 113 | + g_upnp_interrupt(); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void StopMapPort() |
| 118 | +{ |
| 119 | + if (g_upnp_thread.joinable()) { |
| 120 | + g_upnp_thread.join(); |
| 121 | + g_upnp_interrupt.reset(); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#else |
| 126 | +void StartMapPort() |
| 127 | +{ |
| 128 | + // Intentionally left blank. |
| 129 | +} |
| 130 | +void InterruptMapPort() |
| 131 | +{ |
| 132 | + // Intentionally left blank. |
| 133 | +} |
| 134 | +void StopMapPort() |
| 135 | +{ |
| 136 | + // Intentionally left blank. |
| 137 | +} |
| 138 | +#endif |
0 commit comments