| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef USE_UPNP |
| 5 | |
| 6 | #include "Common/UPnP.h" |
| 7 | |
| 8 | #include "Common/Logging/Log.h" |
| 9 | |
| 10 | #include <array> |
| 11 | #include <cstdlib> |
| 12 | #include <cstring> |
| 13 | #include <miniupnpc.h> |
| 14 | #include <miniwget.h> |
| 15 | #include <string> |
| 16 | #include <thread> |
| 17 | #include <upnpcommands.h> |
| 18 | #include <upnperrors.h> |
| 19 | |
| 20 | static UPNPUrls s_urls; |
| 21 | static IGDdatas s_data; |
| 22 | static std::array<char, 20> s_our_ip; |
| 23 | static u16 s_mapped = 0; |
| 24 | static std::thread s_thread; |
| 25 | |
| 26 | // called from ---UPnP--- thread |
| 27 | // discovers the IGD |
| 28 | static bool InitUPnP() |
| 29 | { |
| 30 | static bool s_inited = false; |
| 31 | static bool s_error = false; |
| 32 | |
| 33 | // Don't init if already inited |
| 34 | if (s_inited) |
| 35 | return true; |
| 36 | |
| 37 | // Don't init if it failed before |
| 38 | if (s_error) |
| 39 | return false; |
| 40 | |
| 41 | s_urls = {}; |
| 42 | s_data = {}; |
| 43 | |
| 44 | // Find all UPnP devices |
| 45 | int upnperror = 0; |
| 46 | std::unique_ptr<UPNPDev, decltype(&freeUPNPDevlist)> devlist(nullptr, freeUPNPDevlist); |
| 47 | #if MINIUPNPC_API_VERSION >= 14 |
| 48 | devlist.reset(upnpDiscover(2000, nullptr, nullptr, 0, 0, 2, &upnperror)); |
| 49 | #else |
| 50 | devlist.reset(upnpDiscover(2000, nullptr, nullptr, 0, 0, &upnperror)); |
| 51 | #endif |
| 52 | if (!devlist) |
| 53 | { |
| 54 | if (upnperror == UPNPDISCOVER_SUCCESS) |
| 55 | { |
| 56 | WARN_LOG_FMT(NETPLAY, "No UPnP devices could be found."); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | WARN_LOG_FMT(NETPLAY, "An error occurred trying to discover UPnP devices: {}", |
| 61 | strupnperror(upnperror)); |
| 62 | } |
| 63 | |
| 64 | s_error = true; |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // Look for the IGD |
| 70 | bool found_valid_igd = false; |
| 71 | for (UPNPDev* dev = devlist.get(); dev; dev = dev->pNext) |
| 72 | { |
| 73 | if (!std::strstr(dev->st, "InternetGatewayDevice")) |
| 74 | continue; |
| 75 | |
| 76 | int desc_xml_size = 0; |
| 77 | std::unique_ptr<char, decltype(&std::free)> desc_xml(nullptr, std::free); |
| 78 | int statusCode = 200; |
| 79 | #if MINIUPNPC_API_VERSION >= 16 |
| 80 | desc_xml.reset( |
| 81 | static_cast<char*>(miniwget_getaddr(dev->descURL, &desc_xml_size, s_our_ip.data(), |
| 82 | static_cast<int>(s_our_ip.size()), 0, &statusCode))); |
| 83 | #else |
| 84 | desc_xml.reset(static_cast<char*>(miniwget_getaddr( |
| 85 | dev->descURL, &desc_xml_size, s_our_ip.data(), static_cast<int>(s_our_ip.size()), 0))); |
| 86 | #endif |
| 87 | if (desc_xml && statusCode == 200) |
| 88 | { |
| 89 | parserootdesc(desc_xml.get(), desc_xml_size, &s_data); |
| 90 | GetUPNPUrls(&s_urls, &s_data, dev->descURL, 0); |
| 91 | |
| 92 | found_valid_igd = true; |
| 93 | NOTICE_LOG_FMT(NETPLAY, "Got info from IGD at {}.", dev->descURL); |
| 94 | break; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | WARN_LOG_FMT(NETPLAY, "Error getting info from IGD at {}.", dev->descURL); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (!found_valid_igd) |
| 103 | WARN_LOG_FMT(NETPLAY, "Could not find a valid IGD in the discovered UPnP devices."); |
| 104 | |
| 105 | s_inited = true; |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // called from ---UPnP--- thread |
| 111 | // Attempt to stop portforwarding. |
| 112 | // -- |
| 113 | // NOTE: It is important that this happens! A few very crappy routers |
| 114 | // apparently do not delete UPnP mappings on their own, so if you leave them |
| 115 | // hanging, the NVRAM will fill with portmappings, and eventually all UPnP |
| 116 | // requests will fail silently, with the only recourse being a factory reset. |
| 117 | // -- |
| 118 | static bool UnmapPort(const u16 port) |
| 119 | { |
| 120 | std::string port_str = std::to_string(port); |
| 121 | UPNP_DeletePortMapping(s_urls.controlURL, s_data.first.servicetype, port_str.c_str(), "UDP", |
| 122 | nullptr); |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | // called from ---UPnP--- thread |
| 128 | // Attempt to portforward! |
| 129 | static bool MapPort(const char* addr, const u16 port) |
| 130 | { |
| 131 | if (s_mapped > 0) |
| 132 | UnmapPort(s_mapped); |
| 133 | |
| 134 | std::string port_str = std::to_string(port); |
| 135 | int result = UPNP_AddPortMapping( |
| 136 | s_urls.controlURL, s_data.first.servicetype, port_str.c_str(), port_str.c_str(), addr, |
| 137 | (std::string("dolphin-emu UDP on ") + addr).c_str(), "UDP", nullptr, nullptr); |
| 138 | |
| 139 | if (result != 0) |
| 140 | return false; |
| 141 | |
| 142 | s_mapped = port; |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | // UPnP thread: try to map a port |
| 148 | static void MapPortThread(const u16 port) |
| 149 | { |
| 150 | if (InitUPnP() && MapPort(s_our_ip.data(), port)) |
| 151 | { |
| 152 | NOTICE_LOG_FMT(NETPLAY, "Successfully mapped port {} to {}.", port, s_our_ip.data()); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | WARN_LOG_FMT(NETPLAY, "Failed to map port {} to {}.", port, s_our_ip.data()); |
| 157 | } |
| 158 | |
| 159 | // UPnP thread: try to unmap a port |
| 160 | static void UnmapPortThread() |
| 161 | { |
| 162 | if (s_mapped > 0) |
| 163 | UnmapPort(s_mapped); |
| 164 | } |
| 165 | |
| 166 | void Common::UPnP::TryPortmapping(u16 port) |
| 167 | { |
| 168 | if (s_thread.joinable()) |
| 169 | s_thread.join(); |
| 170 | s_thread = std::thread(&MapPortThread, port); |
| 171 | } |
| 172 | |
| 173 | void Common::UPnP::StopPortmapping() |
| 174 | { |
| 175 | if (s_thread.joinable()) |
| 176 | s_thread.join(); |
| 177 | s_thread = std::thread(&UnmapPortThread); |
| 178 | s_thread.join(); |
| 179 | } |
| 180 | |
| 181 | #endif |