Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 17 | #include "upnp/upnp_context.h" |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 18 | #include "protocol/upnp_protocol.h" |
| 19 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 20 | #if HAVE_LIBNATPMP |
| 21 | #include "protocol/natpmp/nat_pmp.h" |
| 22 | #endif |
| 23 | #if HAVE_LIBUPNP |
| 24 | #include "protocol/pupnp/pupnp.h" |
| 25 | #endif |
Amna | 7cd813c | 2023-10-02 18:15:47 -0400 | [diff] [blame] | 26 | #include <asio.hpp> |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 27 | #include <asio/steady_timer.hpp> |
Adrien Béraud | 9d35096 | 2023-07-13 15:36:32 -0400 | [diff] [blame] | 28 | #if __has_include(<fmt/std.h>) |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 29 | #include <fmt/std.h> |
Adrien Béraud | 9d35096 | 2023-07-13 15:36:32 -0400 | [diff] [blame] | 30 | #else |
| 31 | #include <fmt/ostream.h> |
| 32 | #endif |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 34 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 35 | namespace upnp { |
| 36 | |
| 37 | constexpr static auto MAP_UPDATE_INTERVAL = std::chrono::seconds(30); |
| 38 | constexpr static int MAX_REQUEST_RETRIES = 20; |
| 39 | constexpr static int MAX_REQUEST_REMOVE_COUNT = 5; |
| 40 | |
| 41 | constexpr static uint16_t UPNP_TCP_PORT_MIN {10000}; |
| 42 | constexpr static uint16_t UPNP_TCP_PORT_MAX {UPNP_TCP_PORT_MIN + 5000}; |
| 43 | constexpr static uint16_t UPNP_UDP_PORT_MIN {20000}; |
| 44 | constexpr static uint16_t UPNP_UDP_PORT_MAX {UPNP_UDP_PORT_MIN + 5000}; |
| 45 | |
Sébastien Blin | 55abf07 | 2023-07-19 10:21:21 -0400 | [diff] [blame] | 46 | UPnPContext::UPnPContext(const std::shared_ptr<asio::io_context>& ioContext, const std::shared_ptr<dht::log::Logger>& logger) |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 47 | : ctx(createIoContext(ioContext, logger)) |
Adrien Béraud | 91fd4b6 | 2023-08-29 20:50:01 -0400 | [diff] [blame] | 48 | , logger_(logger) |
Adrien Béraud | 95219ef | 2023-08-17 21:55:37 -0400 | [diff] [blame] | 49 | , mappingListUpdateTimer_(*ctx) |
| 50 | , connectivityChangedTimer_(*ctx) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 51 | { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 52 | if (logger_) logger_->debug("Creating UPnPContext instance [{}]", fmt::ptr(this)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 53 | |
| 54 | // Set port ranges |
| 55 | portRange_.emplace(PortType::TCP, std::make_pair(UPNP_TCP_PORT_MIN, UPNP_TCP_PORT_MAX)); |
| 56 | portRange_.emplace(PortType::UDP, std::make_pair(UPNP_UDP_PORT_MIN, UPNP_UDP_PORT_MAX)); |
| 57 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 58 | ctx->post([this] { init(); }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Adrien Béraud | b04fbd7 | 2023-08-17 19:56:11 -0400 | [diff] [blame] | 61 | std::shared_ptr<asio::io_context> |
| 62 | UPnPContext::createIoContext(const std::shared_ptr<asio::io_context>& ctx, const std::shared_ptr<dht::log::Logger>& logger) { |
| 63 | if (ctx) { |
| 64 | return ctx; |
| 65 | } else { |
| 66 | if (logger) logger->debug("UPnPContext: starting dedicated io_context thread"); |
| 67 | auto ioCtx = std::make_shared<asio::io_context>(); |
| 68 | ioContextRunner_ = std::make_unique<std::thread>([ioCtx, l=logger]() { |
| 69 | try { |
| 70 | auto work = asio::make_work_guard(*ioCtx); |
| 71 | ioCtx->run(); |
| 72 | } catch (const std::exception& ex) { |
| 73 | if (l) l->error("Unexpected io_context thread exception: {}", ex.what()); |
| 74 | } |
| 75 | }); |
| 76 | return ioCtx; |
| 77 | } |
| 78 | } |
| 79 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 80 | void |
| 81 | UPnPContext::shutdown(std::condition_variable& cv) |
| 82 | { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 83 | if (logger_) logger_->debug("Shutdown UPnPContext instance [{}]", fmt::ptr(this)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 84 | |
| 85 | stopUpnp(true); |
| 86 | |
| 87 | for (auto const& [_, proto] : protocolList_) { |
| 88 | proto->terminate(); |
| 89 | } |
| 90 | |
Adrien Béraud | 91fd4b6 | 2023-08-29 20:50:01 -0400 | [diff] [blame] | 91 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 92 | mappingList_->clear(); |
| 93 | mappingListUpdateTimer_.cancel(); |
| 94 | controllerList_.clear(); |
| 95 | protocolList_.clear(); |
| 96 | shutdownComplete_ = true; |
| 97 | cv.notify_one(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | UPnPContext::shutdown() |
| 102 | { |
| 103 | std::unique_lock<std::mutex> lk(mappingMutex_); |
| 104 | std::condition_variable cv; |
| 105 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 106 | ctx->post([&, this] { shutdown(cv); }); |
Sébastien Blin | 91cda3c | 2024-01-10 16:21:18 -0500 | [diff] [blame] | 107 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 108 | if (logger_) logger_->debug("Waiting for shutdown ..."); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 109 | |
| 110 | if (cv.wait_for(lk, std::chrono::seconds(30), [this] { return shutdownComplete_; })) { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 111 | if (logger_) logger_->debug("Shutdown completed"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 112 | } else { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 113 | if (logger_) logger_->error("Shutdown timed-out"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 114 | } |
François-Simon Fauteux-Chapleau | 648907c | 2024-02-06 15:16:48 -0500 | [diff] [blame] | 115 | // NOTE: It's important to unlock mappingMutex_ here, otherwise we get a |
| 116 | // deadlock when the call to cv.wait_for() above times out before we return |
| 117 | // from proto->terminate() in shutdown(cv). |
| 118 | lk.unlock(); |
Adrien Béraud | 91fd4b6 | 2023-08-29 20:50:01 -0400 | [diff] [blame] | 119 | |
| 120 | if (ioContextRunner_) { |
Sébastien Blin | 91cda3c | 2024-01-10 16:21:18 -0500 | [diff] [blame] | 121 | if (logger_) logger_->debug("UPnPContext: stopping io_context thread {}", fmt::ptr(this)); |
Adrien Béraud | 91fd4b6 | 2023-08-29 20:50:01 -0400 | [diff] [blame] | 122 | ctx->stop(); |
| 123 | ioContextRunner_->join(); |
| 124 | ioContextRunner_.reset(); |
Sébastien Blin | 91cda3c | 2024-01-10 16:21:18 -0500 | [diff] [blame] | 125 | if (logger_) logger_->debug("UPnPContext: stopping io_context thread - finished {}", fmt::ptr(this)); |
Adrien Béraud | 91fd4b6 | 2023-08-29 20:50:01 -0400 | [diff] [blame] | 126 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | UPnPContext::~UPnPContext() |
| 130 | { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 131 | if (logger_) logger_->debug("UPnPContext instance [{}] destroyed", fmt::ptr(this)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
| 135 | UPnPContext::init() |
| 136 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 137 | #if HAVE_LIBNATPMP |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 138 | auto natPmp = std::make_shared<NatPmp>(ctx, logger_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 139 | natPmp->setObserver(this); |
| 140 | protocolList_.emplace(NatProtocolType::NAT_PMP, std::move(natPmp)); |
| 141 | #endif |
| 142 | |
| 143 | #if HAVE_LIBUPNP |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 144 | auto pupnp = std::make_shared<PUPnP>(ctx, logger_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 145 | pupnp->setObserver(this); |
| 146 | protocolList_.emplace(NatProtocolType::PUPNP, std::move(pupnp)); |
| 147 | #endif |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | UPnPContext::startUpnp() |
| 152 | { |
| 153 | assert(not controllerList_.empty()); |
| 154 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 155 | if (logger_) logger_->debug("Starting UPNP context"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 156 | |
| 157 | // Request a new IGD search. |
| 158 | for (auto const& [_, protocol] : protocolList_) { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 159 | ctx->dispatch([p=protocol] { p->searchForIgd(); }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | started_ = true; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | UPnPContext::stopUpnp(bool forceRelease) |
| 167 | { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 168 | if (logger_) logger_->debug("Stopping UPNP context"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 169 | |
| 170 | // Clear all current mappings if any. |
| 171 | |
| 172 | // Use a temporary list to avoid processing the mapping |
| 173 | // list while holding the lock. |
| 174 | std::list<Mapping::sharedPtr_t> toRemoveList; |
| 175 | { |
| 176 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 177 | |
| 178 | PortType types[2] {PortType::TCP, PortType::UDP}; |
| 179 | for (auto& type : types) { |
| 180 | auto& mappingList = getMappingList(type); |
| 181 | for (auto const& [_, map] : mappingList) { |
| 182 | toRemoveList.emplace_back(map); |
| 183 | } |
| 184 | } |
| 185 | // Invalidate the current IGDs. |
| 186 | preferredIgd_.reset(); |
| 187 | validIgdList_.clear(); |
| 188 | } |
| 189 | for (auto const& map : toRemoveList) { |
| 190 | requestRemoveMapping(map); |
| 191 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 192 | // Notify is not needed in updateState when |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 193 | // shutting down (hence set it to false). NotifyCallback |
| 194 | // would trigger a new SIP registration and create a |
| 195 | // false registered state upon program close. |
| 196 | // It's handled by upper layers. |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 197 | updateMappingState(map, MappingState::FAILED, false); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 198 | // We dont remove mappings with auto-update enabled, |
| 199 | // unless forceRelease is true. |
| 200 | if (not map->getAutoUpdate() or forceRelease) { |
| 201 | map->enableAutoUpdate(false); |
| 202 | unregisterMapping(map); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Clear all current IGDs. |
| 207 | for (auto const& [_, protocol] : protocolList_) { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 208 | ctx->dispatch([p=protocol]{ p->clearIgds(); }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | started_ = false; |
| 212 | } |
| 213 | |
| 214 | uint16_t |
| 215 | UPnPContext::generateRandomPort(PortType type, bool mustBeEven) |
| 216 | { |
| 217 | auto minPort = type == PortType::TCP ? UPNP_TCP_PORT_MIN : UPNP_UDP_PORT_MIN; |
| 218 | auto maxPort = type == PortType::TCP ? UPNP_TCP_PORT_MAX : UPNP_UDP_PORT_MAX; |
| 219 | |
| 220 | if (minPort >= maxPort) { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 221 | // if (logger_) logger_->error("Max port number ({}) must be greater than min port number ({})", maxPort, minPort); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 222 | // Must be called with valid range. |
| 223 | assert(false); |
| 224 | } |
| 225 | |
| 226 | int fact = mustBeEven ? 2 : 1; |
| 227 | if (mustBeEven) { |
| 228 | minPort /= fact; |
| 229 | maxPort /= fact; |
| 230 | } |
| 231 | |
| 232 | // Seed the generator. |
| 233 | static std::mt19937 gen(dht::crypto::getSeededRandomEngine()); |
| 234 | // Define the range. |
| 235 | std::uniform_int_distribution<uint16_t> dist(minPort, maxPort); |
| 236 | return dist(gen) * fact; |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | UPnPContext::connectivityChanged() |
| 241 | { |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 242 | // Debounce the connectivity change notification. |
| 243 | connectivityChangedTimer_.expires_after(std::chrono::milliseconds(50)); |
| 244 | connectivityChangedTimer_.async_wait(std::bind(&UPnPContext::_connectivityChanged, this, std::placeholders::_1)); |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | UPnPContext::_connectivityChanged(const asio::error_code& ec) |
| 249 | { |
| 250 | if (ec == asio::error::operation_aborted) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 251 | return; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 252 | |
| 253 | auto hostAddr = ip_utils::getLocalAddr(AF_INET); |
| 254 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 255 | if (logger_) logger_->debug("Connectivity change check: host address {}", hostAddr.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 256 | |
| 257 | auto restartUpnp = false; |
| 258 | |
| 259 | // On reception of "connectivity change" notification, the UPNP search |
| 260 | // will be restarted if either there is no valid IGD, or the IGD address |
| 261 | // changed. |
| 262 | |
| 263 | if (not isReady()) { |
| 264 | restartUpnp = true; |
| 265 | } else { |
| 266 | // Check if the host address changed. |
| 267 | for (auto const& [_, protocol] : protocolList_) { |
| 268 | if (protocol->isReady() and hostAddr != protocol->getHostAddress()) { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 269 | if (logger_) logger_->warn("Host address changed from {} to {}", |
| 270 | protocol->getHostAddress().toString(), |
| 271 | hostAddr.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 272 | protocol->clearIgds(); |
| 273 | restartUpnp = true; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // We have at least one valid IGD and the host address did |
| 280 | // not change, so no need to restart. |
| 281 | if (not restartUpnp) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | // No registered controller. A new search will be performed when |
| 286 | // a controller is registered. |
| 287 | if (controllerList_.empty()) |
| 288 | return; |
| 289 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 290 | if (logger_) logger_->debug("Connectivity changed. Clear the IGDs and restart"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 291 | |
| 292 | stopUpnp(); |
| 293 | startUpnp(); |
| 294 | |
| 295 | // Mapping with auto update enabled must be processed first. |
| 296 | processMappingWithAutoUpdate(); |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | UPnPContext::setPublicAddress(const IpAddr& addr) |
| 301 | { |
| 302 | if (not addr) |
| 303 | return; |
| 304 | |
| 305 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 306 | if (knownPublicAddress_ != addr) { |
| 307 | knownPublicAddress_ = std::move(addr); |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 308 | if (logger_) logger_->debug("Setting the known public address to {}", addr.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | bool |
| 313 | UPnPContext::isReady() const |
| 314 | { |
| 315 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 316 | return not validIgdList_.empty(); |
| 317 | } |
| 318 | |
| 319 | IpAddr |
| 320 | UPnPContext::getExternalIP() const |
| 321 | { |
| 322 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 323 | // Return the first IGD Ip available. |
| 324 | if (not validIgdList_.empty()) { |
| 325 | return (*validIgdList_.begin())->getPublicIp(); |
| 326 | } |
| 327 | return {}; |
| 328 | } |
| 329 | |
| 330 | Mapping::sharedPtr_t |
| 331 | UPnPContext::reserveMapping(Mapping& requestedMap) |
| 332 | { |
| 333 | auto desiredPort = requestedMap.getExternalPort(); |
| 334 | |
| 335 | if (desiredPort == 0) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 336 | if (logger_) logger_->debug("Desired port is not set, will provide the first available port for [{}]", |
| 337 | requestedMap.getTypeStr()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 338 | } else { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 339 | if (logger_) logger_->debug("Try to find mapping for port {:d} [{}]", desiredPort, requestedMap.getTypeStr()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | Mapping::sharedPtr_t mapRes; |
| 343 | |
| 344 | { |
| 345 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 346 | auto& mappingList = getMappingList(requestedMap.getType()); |
| 347 | |
| 348 | // We try to provide a mapping in "OPEN" state. If not found, |
| 349 | // we provide any available mapping. In this case, it's up to |
| 350 | // the caller to use it or not. |
| 351 | for (auto const& [_, map] : mappingList) { |
| 352 | // If the desired port is null, we pick the first available port. |
| 353 | if (map->isValid() and (desiredPort == 0 or map->getExternalPort() == desiredPort) |
| 354 | and map->isAvailable()) { |
| 355 | // Considere the first available mapping regardless of its |
| 356 | // state. A mapping with OPEN state will be used if found. |
| 357 | if (not mapRes) |
| 358 | mapRes = map; |
| 359 | |
| 360 | if (map->getState() == MappingState::OPEN) { |
| 361 | // Found an "OPEN" mapping. We are done. |
| 362 | mapRes = map; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Create a mapping if none was available. |
| 370 | if (not mapRes) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 371 | // JAMI_WARN("Did not find any available mapping. Will request one now"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 372 | mapRes = registerMapping(requestedMap); |
| 373 | } |
| 374 | |
| 375 | if (mapRes) { |
| 376 | // Make the mapping unavailable |
| 377 | mapRes->setAvailable(false); |
| 378 | // Copy attributes. |
| 379 | mapRes->setNotifyCallback(requestedMap.getNotifyCallback()); |
| 380 | mapRes->enableAutoUpdate(requestedMap.getAutoUpdate()); |
| 381 | // Notify the listener. |
| 382 | if (auto cb = mapRes->getNotifyCallback()) |
| 383 | cb(mapRes); |
| 384 | } |
| 385 | |
| 386 | updateMappingList(true); |
| 387 | |
| 388 | return mapRes; |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | UPnPContext::releaseMapping(const Mapping& map) |
| 393 | { |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 394 | ctx->dispatch([this, map] { |
Sébastien Blin | 91cda3c | 2024-01-10 16:21:18 -0500 | [diff] [blame] | 395 | if (shutdownComplete_) |
| 396 | return; |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 397 | auto mapPtr = getMappingWithKey(map.getMapKey()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 398 | |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 399 | if (not mapPtr) { |
| 400 | // Might happen if the mapping failed or was never granted. |
| 401 | if (logger_) logger_->debug("Mapping {} does not exist or was already removed", map.toString()); |
| 402 | return; |
| 403 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 404 | |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 405 | if (mapPtr->isAvailable()) { |
| 406 | if (logger_) logger_->warn("Trying to release an unused mapping {}", mapPtr->toString()); |
| 407 | return; |
| 408 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 409 | |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 410 | // Remove it. |
| 411 | requestRemoveMapping(mapPtr); |
| 412 | unregisterMapping(mapPtr); |
| 413 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void |
| 417 | UPnPContext::registerController(void* controller) |
| 418 | { |
| 419 | { |
| 420 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 421 | if (shutdownComplete_) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 422 | if (logger_) logger_->warn("UPnPContext already shut down"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 423 | return; |
| 424 | } |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 425 | auto ret = controllerList_.emplace(controller); |
| 426 | if (not ret.second) { |
| 427 | if (logger_) logger_->warn("Controller {} is already registered", fmt::ptr(controller)); |
| 428 | return; |
| 429 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 430 | } |
| 431 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 432 | if (logger_) logger_->debug("Successfully registered controller {}", fmt::ptr(controller)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 433 | if (not started_) |
| 434 | startUpnp(); |
| 435 | } |
| 436 | |
| 437 | void |
| 438 | UPnPContext::unregisterController(void* controller) |
| 439 | { |
Sébastien Blin | 91cda3c | 2024-01-10 16:21:18 -0500 | [diff] [blame] | 440 | if (shutdownComplete_) |
| 441 | return; |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 442 | std::unique_lock<std::mutex> lock(mappingMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 443 | if (controllerList_.erase(controller) == 1) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 444 | if (logger_) logger_->debug("Successfully unregistered controller {}", fmt::ptr(controller)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 445 | } else { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 446 | if (logger_) logger_->debug("Controller {} was already removed", fmt::ptr(controller)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (controllerList_.empty()) { |
Adrien Béraud | c36965c | 2023-08-17 21:50:27 -0400 | [diff] [blame] | 450 | lock.unlock(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 451 | stopUpnp(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | uint16_t |
| 456 | UPnPContext::getAvailablePortNumber(PortType type) |
| 457 | { |
| 458 | // Only return an availalable random port. No actual |
| 459 | // reservation is made here. |
| 460 | |
| 461 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 462 | auto& mappingList = getMappingList(type); |
| 463 | int tryCount = 0; |
| 464 | while (tryCount++ < MAX_REQUEST_RETRIES) { |
| 465 | uint16_t port = generateRandomPort(type); |
| 466 | Mapping map(type, port, port); |
| 467 | if (mappingList.find(map.getMapKey()) == mappingList.end()) |
| 468 | return port; |
| 469 | } |
| 470 | |
| 471 | // Very unlikely to get here. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 472 | if (logger_) logger_->error("Could not find an available port after %i trials", MAX_REQUEST_RETRIES); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | void |
| 477 | UPnPContext::requestMapping(const Mapping::sharedPtr_t& map) |
| 478 | { |
| 479 | assert(map); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 480 | auto const& igd = getPreferredIgd(); |
| 481 | // We must have at least a valid IGD pointer if we get here. |
| 482 | // Not this method is called only if there were a valid IGD, however, |
| 483 | // because the processing is asynchronous, it's possible that the IGD |
| 484 | // was invalidated when the this code executed. |
| 485 | if (not igd) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 486 | if (logger_) logger_->debug("No valid IGDs available"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 487 | return; |
| 488 | } |
| 489 | |
| 490 | map->setIgd(igd); |
| 491 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 492 | if (logger_) logger_->debug("Request mapping {} using protocol [{}] IGD [{}]", |
| 493 | map->toString(), |
| 494 | igd->getProtocolName(), |
| 495 | igd->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 496 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 497 | updateMappingState(map, MappingState::IN_PROGRESS); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 498 | |
| 499 | auto const& protocol = protocolList_.at(igd->getProtocol()); |
| 500 | protocol->requestMappingAdd(*map); |
| 501 | } |
| 502 | |
| 503 | bool |
| 504 | UPnPContext::provisionNewMappings(PortType type, int portCount) |
| 505 | { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 506 | if (logger_) logger_->debug("Provision {:d} new mappings of type [{}]", portCount, Mapping::getTypeStr(type)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 507 | |
| 508 | assert(portCount > 0); |
| 509 | |
| 510 | while (portCount > 0) { |
| 511 | auto port = getAvailablePortNumber(type); |
| 512 | if (port > 0) { |
| 513 | // Found an available port number |
| 514 | portCount--; |
| 515 | Mapping map(type, port, port, true); |
| 516 | registerMapping(map); |
| 517 | } else { |
| 518 | // Very unlikely to get here! |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 519 | if (logger_) logger_->error("Can not find any available port to provision!"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | bool |
| 528 | UPnPContext::deleteUnneededMappings(PortType type, int portCount) |
| 529 | { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 530 | if (logger_) logger_->debug("Remove {:d} unneeded mapping of type [{}]", portCount, Mapping::getTypeStr(type)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 531 | |
| 532 | assert(portCount > 0); |
| 533 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 534 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 535 | |
| 536 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 537 | auto& mappingList = getMappingList(type); |
| 538 | |
| 539 | for (auto it = mappingList.begin(); it != mappingList.end();) { |
| 540 | auto map = it->second; |
| 541 | assert(map); |
| 542 | |
| 543 | if (not map->isAvailable()) { |
| 544 | it++; |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | if (map->getState() == MappingState::OPEN and portCount > 0) { |
| 549 | // Close portCount mappings in "OPEN" state. |
| 550 | requestRemoveMapping(map); |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 551 | it = mappingList.erase(it); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 552 | portCount--; |
| 553 | } else if (map->getState() != MappingState::OPEN) { |
| 554 | // If this methods is called, it means there are more open |
| 555 | // mappings than required. So, all mappings in a state other |
| 556 | // than "OPEN" state (typically in in-progress state) will |
| 557 | // be deleted as well. |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 558 | it = mappingList.erase(it); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 559 | } else { |
| 560 | it++; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | UPnPContext::updatePreferredIgd() |
| 569 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 570 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 571 | |
| 572 | if (preferredIgd_ and preferredIgd_->isValid()) |
| 573 | return; |
| 574 | |
| 575 | // Reset and search for the best IGD. |
| 576 | preferredIgd_.reset(); |
| 577 | |
| 578 | for (auto const& [_, protocol] : protocolList_) { |
| 579 | if (protocol->isReady()) { |
| 580 | auto igdList = protocol->getIgdList(); |
| 581 | assert(not igdList.empty()); |
| 582 | auto const& igd = igdList.front(); |
| 583 | if (not igd->isValid()) |
| 584 | continue; |
| 585 | |
| 586 | // Prefer NAT-PMP over PUPNP. |
| 587 | if (preferredIgd_ and igd->getProtocol() != NatProtocolType::NAT_PMP) |
| 588 | continue; |
| 589 | |
| 590 | // Update. |
| 591 | preferredIgd_ = igd; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (preferredIgd_ and preferredIgd_->isValid()) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 596 | if (logger_) logger_->debug("Preferred IGD updated to [{}] IGD [{} {}] ", |
| 597 | preferredIgd_->getProtocolName(), |
| 598 | preferredIgd_->getUID(), |
| 599 | preferredIgd_->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | std::shared_ptr<IGD> |
| 604 | UPnPContext::getPreferredIgd() const |
| 605 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 606 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 607 | |
| 608 | return preferredIgd_; |
| 609 | } |
| 610 | |
| 611 | void |
| 612 | UPnPContext::updateMappingList(bool async) |
| 613 | { |
| 614 | // Run async if requested. |
| 615 | if (async) { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 616 | ctx->post([this] { updateMappingList(false); }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 617 | return; |
| 618 | } |
| 619 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 620 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 621 | |
| 622 | // Update the preferred IGD. |
| 623 | updatePreferredIgd(); |
| 624 | |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 625 | mappingListUpdateTimer_.cancel(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 626 | |
| 627 | // Skip if no controller registered. |
| 628 | if (controllerList_.empty()) |
| 629 | return; |
| 630 | |
| 631 | // Cancel the current timer (if any) and re-schedule. |
| 632 | std::shared_ptr<IGD> prefIgd = getPreferredIgd(); |
| 633 | if (not prefIgd) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 634 | if (logger_) logger_->debug("UPNP/NAT-PMP enabled, but no valid IGDs available"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 635 | // No valid IGD. Nothing to do. |
| 636 | return; |
| 637 | } |
| 638 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 639 | mappingListUpdateTimer_.expires_after(MAP_UPDATE_INTERVAL); |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 640 | mappingListUpdateTimer_.async_wait([this](asio::error_code const& ec) { |
| 641 | if (ec != asio::error::operation_aborted) |
| 642 | updateMappingList(false); |
| 643 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 644 | |
| 645 | // Process pending requests if any. |
| 646 | processPendingRequests(prefIgd); |
| 647 | |
| 648 | // Make new requests for mappings that failed and have |
| 649 | // the auto-update option enabled. |
| 650 | processMappingWithAutoUpdate(); |
| 651 | |
| 652 | PortType typeArray[2] = {PortType::TCP, PortType::UDP}; |
| 653 | |
| 654 | for (auto idx : {0, 1}) { |
| 655 | auto type = typeArray[idx]; |
| 656 | |
| 657 | MappingStatus status; |
| 658 | getMappingStatus(type, status); |
| 659 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 660 | if (logger_) logger_->debug("Mapping status [{}] - overall {:d}: {:d} open ({:d} ready + {:d} in use), {:d} pending, {:d} " |
| 661 | "in-progress, {:d} failed", |
| 662 | Mapping::getTypeStr(type), |
| 663 | status.sum(), |
| 664 | status.openCount_, |
| 665 | status.readyCount_, |
| 666 | status.openCount_ - status.readyCount_, |
| 667 | status.pendingCount_, |
| 668 | status.inProgressCount_, |
| 669 | status.failedCount_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 670 | |
| 671 | if (status.failedCount_ > 0) { |
| 672 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 673 | auto const& mappingList = getMappingList(type); |
| 674 | for (auto const& [_, map] : mappingList) { |
| 675 | if (map->getState() == MappingState::FAILED) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 676 | if (logger_) logger_->debug("Mapping status [{}] - Available [{}]", |
| 677 | map->toString(true), |
| 678 | map->isAvailable() ? "YES" : "NO"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | int toRequestCount = (int) minOpenPortLimit_[idx] |
| 684 | - (int) (status.readyCount_ + status.inProgressCount_ |
| 685 | + status.pendingCount_); |
| 686 | |
| 687 | // Provision/release mappings accordingly. |
| 688 | if (toRequestCount > 0) { |
| 689 | // Take into account the request in-progress when making |
| 690 | // requests for new mappings. |
| 691 | provisionNewMappings(type, toRequestCount); |
| 692 | } else if (status.readyCount_ > maxOpenPortLimit_[idx]) { |
| 693 | deleteUnneededMappings(type, status.readyCount_ - maxOpenPortLimit_[idx]); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | // Prune the mapping list if needed |
| 698 | if (protocolList_.at(NatProtocolType::PUPNP)->isReady()) { |
| 699 | #if HAVE_LIBNATPMP |
| 700 | // Dont perform if NAT-PMP is valid. |
| 701 | if (not protocolList_.at(NatProtocolType::NAT_PMP)->isReady()) |
| 702 | #endif |
| 703 | { |
| 704 | pruneMappingList(); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | #if HAVE_LIBNATPMP |
| 709 | // Renew nat-pmp allocations |
| 710 | if (protocolList_.at(NatProtocolType::NAT_PMP)->isReady()) |
| 711 | renewAllocations(); |
| 712 | #endif |
| 713 | } |
| 714 | |
| 715 | void |
| 716 | UPnPContext::pruneMappingList() |
| 717 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 718 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 719 | |
| 720 | MappingStatus status; |
| 721 | getMappingStatus(status); |
| 722 | |
| 723 | // Do not prune the list if there are pending/in-progress requests. |
| 724 | if (status.inProgressCount_ != 0 or status.pendingCount_ != 0) { |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | auto const& igd = getPreferredIgd(); |
| 729 | if (not igd or igd->getProtocol() != NatProtocolType::PUPNP) { |
| 730 | return; |
| 731 | } |
| 732 | auto protocol = protocolList_.at(NatProtocolType::PUPNP); |
| 733 | |
| 734 | auto remoteMapList = protocol->getMappingsListByDescr(igd, |
| 735 | Mapping::UPNP_MAPPING_DESCRIPTION_PREFIX); |
Adrien Beraud | a0683d1 | 2023-08-22 18:09:02 -0400 | [diff] [blame] | 736 | /*if (remoteMapList.empty()) { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 737 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 738 | if (not getMappingList(PortType::TCP).empty() or getMappingList(PortType::TCP).empty()) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 739 | // JAMI_WARN("We have provisionned mappings but the PUPNP IGD returned an empty list!"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 740 | } |
Adrien Beraud | a0683d1 | 2023-08-22 18:09:02 -0400 | [diff] [blame] | 741 | }*/ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 742 | |
| 743 | pruneUnMatchedMappings(igd, remoteMapList); |
| 744 | pruneUnTrackedMappings(igd, remoteMapList); |
| 745 | } |
| 746 | |
| 747 | void |
| 748 | UPnPContext::pruneUnMatchedMappings(const std::shared_ptr<IGD>& igd, |
| 749 | const std::map<Mapping::key_t, Mapping>& remoteMapList) |
| 750 | { |
| 751 | // Check/synchronize local mapping list with the list |
| 752 | // returned by the IGD. |
| 753 | |
Adrien Beraud | a0683d1 | 2023-08-22 18:09:02 -0400 | [diff] [blame] | 754 | for (auto type: {PortType::TCP, PortType::UDP}) { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 755 | // Use a temporary list to avoid processing mappings while holding the lock. |
| 756 | std::list<Mapping::sharedPtr_t> toRemoveList; |
| 757 | { |
| 758 | std::lock_guard<std::mutex> lock(mappingMutex_); |
Adrien Beraud | a0683d1 | 2023-08-22 18:09:02 -0400 | [diff] [blame] | 759 | for (auto const& [_, map] : getMappingList(type)) { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 760 | // Only check mappings allocated by UPNP protocol. |
| 761 | if (map->getProtocol() != NatProtocolType::PUPNP) { |
| 762 | continue; |
| 763 | } |
| 764 | // Set mapping as failed if not found in the list |
| 765 | // returned by the IGD. |
| 766 | if (map->getState() == MappingState::OPEN |
| 767 | and remoteMapList.find(map->getMapKey()) == remoteMapList.end()) { |
| 768 | toRemoveList.emplace_back(map); |
| 769 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 770 | if (logger_) logger_->warn("Mapping {} (IGD {}) marked as \"OPEN\" but not found in the " |
| 771 | "remote list. Mark as failed!", |
| 772 | map->toString(), |
| 773 | igd->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | for (auto const& map : toRemoveList) { |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 779 | updateMappingState(map, MappingState::FAILED); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 780 | unregisterMapping(map); |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void |
| 786 | UPnPContext::pruneUnTrackedMappings(const std::shared_ptr<IGD>& igd, |
| 787 | const std::map<Mapping::key_t, Mapping>& remoteMapList) |
| 788 | { |
| 789 | // Use a temporary list to avoid processing mappings while holding the lock. |
| 790 | std::list<Mapping> toRemoveList; |
| 791 | { |
| 792 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 793 | |
| 794 | for (auto const& [_, map] : remoteMapList) { |
| 795 | // Must has valid IGD pointer and use UPNP protocol. |
| 796 | assert(map.getIgd()); |
| 797 | assert(map.getIgd()->getProtocol() == NatProtocolType::PUPNP); |
| 798 | auto& mappingList = getMappingList(map.getType()); |
| 799 | auto it = mappingList.find(map.getMapKey()); |
| 800 | if (it == mappingList.end()) { |
| 801 | // Not present, request mapping remove. |
| 802 | toRemoveList.emplace_back(std::move(map)); |
| 803 | // Make only few remove requests at once. |
| 804 | if (toRemoveList.size() >= MAX_REQUEST_REMOVE_COUNT) |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | // Remove un-tracked mappings. |
| 811 | auto protocol = protocolList_.at(NatProtocolType::PUPNP); |
| 812 | for (auto const& map : toRemoveList) { |
| 813 | protocol->requestMappingRemove(map); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | void |
| 818 | UPnPContext::pruneMappingsWithInvalidIgds(const std::shared_ptr<IGD>& igd) |
| 819 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 820 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 821 | |
| 822 | // Use temporary list to avoid holding the lock while |
| 823 | // processing the mapping list. |
| 824 | std::list<Mapping::sharedPtr_t> toRemoveList; |
| 825 | { |
| 826 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 827 | |
| 828 | PortType types[2] {PortType::TCP, PortType::UDP}; |
| 829 | for (auto& type : types) { |
| 830 | auto& mappingList = getMappingList(type); |
| 831 | for (auto const& [_, map] : mappingList) { |
| 832 | if (map->getIgd() == igd) |
| 833 | toRemoveList.emplace_back(map); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | for (auto const& map : toRemoveList) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 839 | if (logger_) logger_->debug("Remove mapping {} (has an invalid IGD {} [{}])", |
| 840 | map->toString(), |
| 841 | igd->toString(), |
| 842 | igd->getProtocolName()); |
Adrien Béraud | 5674031 | 2023-08-23 08:38:28 -0400 | [diff] [blame] | 843 | updateMappingState(map, MappingState::FAILED); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 844 | unregisterMapping(map); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void |
| 849 | UPnPContext::processPendingRequests(const std::shared_ptr<IGD>& igd) |
| 850 | { |
| 851 | // This list holds the mappings to be requested. This is |
| 852 | // needed to avoid performing the requests while holding |
| 853 | // the lock. |
| 854 | std::list<Mapping::sharedPtr_t> requestsList; |
| 855 | |
| 856 | // Populate the list of requests to perform. |
| 857 | { |
| 858 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 859 | PortType typeArray[2] {PortType::TCP, PortType::UDP}; |
| 860 | |
| 861 | for (auto type : typeArray) { |
| 862 | auto& mappingList = getMappingList(type); |
| 863 | for (auto& [_, map] : mappingList) { |
| 864 | if (map->getState() == MappingState::PENDING) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 865 | if (logger_) logger_->debug("Send pending request for mapping {} to IGD {}", |
| 866 | map->toString(), |
| 867 | igd->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 868 | requestsList.emplace_back(map); |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | // Process the pending requests. |
| 875 | for (auto const& map : requestsList) { |
| 876 | requestMapping(map); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | void |
| 881 | UPnPContext::processMappingWithAutoUpdate() |
| 882 | { |
| 883 | // This list holds the mappings to be requested. This is |
| 884 | // needed to avoid performing the requests while holding |
| 885 | // the lock. |
| 886 | std::list<Mapping::sharedPtr_t> requestsList; |
| 887 | |
| 888 | // Populate the list of requests for mappings with auto-update enabled. |
| 889 | { |
| 890 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 891 | PortType typeArray[2] {PortType::TCP, PortType::UDP}; |
| 892 | |
| 893 | for (auto type : typeArray) { |
| 894 | auto& mappingList = getMappingList(type); |
| 895 | for (auto const& [_, map] : mappingList) { |
| 896 | if (map->getState() == MappingState::FAILED and map->getAutoUpdate()) { |
| 897 | requestsList.emplace_back(map); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | for (auto const& oldMap : requestsList) { |
| 904 | // Request a new mapping if auto-update is enabled. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 905 | if (logger_) logger_->debug("Mapping {} has auto-update enabled, a new mapping will be requested", |
| 906 | oldMap->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 907 | |
| 908 | // Reserve a new mapping. |
| 909 | Mapping newMapping(oldMap->getType()); |
| 910 | newMapping.enableAutoUpdate(true); |
| 911 | newMapping.setNotifyCallback(oldMap->getNotifyCallback()); |
| 912 | |
| 913 | auto const& mapPtr = reserveMapping(newMapping); |
| 914 | assert(mapPtr); |
| 915 | |
| 916 | // Release the old one. |
| 917 | oldMap->setAvailable(true); |
| 918 | oldMap->enableAutoUpdate(false); |
| 919 | oldMap->setNotifyCallback(nullptr); |
| 920 | unregisterMapping(oldMap); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | void |
| 925 | UPnPContext::onIgdUpdated(const std::shared_ptr<IGD>& igd, UpnpIgdEvent event) |
| 926 | { |
| 927 | assert(igd); |
| 928 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 929 | // Reset to start search for a new best IGD. |
| 930 | preferredIgd_.reset(); |
| 931 | |
| 932 | char const* IgdState = event == UpnpIgdEvent::ADDED ? "ADDED" |
| 933 | : event == UpnpIgdEvent::REMOVED ? "REMOVED" |
| 934 | : "INVALID"; |
| 935 | |
| 936 | auto const& igdLocalAddr = igd->getLocalIp(); |
| 937 | auto protocolName = igd->getProtocolName(); |
| 938 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 939 | if (logger_) logger_->debug("New event for IGD [{} {}] [{}]: [{}]", |
| 940 | igd->getUID(), |
| 941 | igd->toString(), |
| 942 | protocolName, |
| 943 | IgdState); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 944 | |
| 945 | // Check if the IGD has valid addresses. |
| 946 | if (not igdLocalAddr) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 947 | if (logger_) logger_->warn("[{}] IGD has an invalid local address", protocolName); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | |
| 951 | if (not igd->getPublicIp()) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 952 | if (logger_) logger_->warn("[{}] IGD has an invalid public address", protocolName); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 953 | return; |
| 954 | } |
| 955 | |
| 956 | if (knownPublicAddress_ and igd->getPublicIp() != knownPublicAddress_) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 957 | if (logger_) logger_->warn("[{}] IGD external address [{}] does not match known public address [{}]." |
| 958 | " The mapped addresses might not be reachable", |
| 959 | protocolName, |
| 960 | igd->getPublicIp().toString(), |
| 961 | knownPublicAddress_.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // The IGD was removed or is invalid. |
| 965 | if (event == UpnpIgdEvent::REMOVED or event == UpnpIgdEvent::INVALID_STATE) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 966 | if (logger_) logger_->warn("State of IGD [{} {}] [{}] changed to [{}]. Pruning the mapping list", |
| 967 | igd->getUID(), |
| 968 | igd->toString(), |
| 969 | protocolName, |
| 970 | IgdState); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 971 | |
| 972 | pruneMappingsWithInvalidIgds(igd); |
| 973 | |
| 974 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 975 | validIgdList_.erase(igd); |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | // Update the IGD list. |
| 980 | { |
| 981 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 982 | auto ret = validIgdList_.emplace(igd); |
| 983 | if (ret.second) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 984 | if (logger_) logger_->debug("IGD [{}] on address {} was added. Will process any pending requests", |
| 985 | protocolName, |
| 986 | igdLocalAddr.toString(true, true)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 987 | } else { |
| 988 | // Already in the list. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 989 | if (logger_) logger_->error("IGD [{}] on address {} already in the list", |
| 990 | protocolName, |
| 991 | igdLocalAddr.toString(true, true)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 992 | return; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // Update the provisionned mappings. |
| 997 | updateMappingList(false); |
| 998 | } |
| 999 | |
| 1000 | void |
| 1001 | UPnPContext::onMappingAdded(const std::shared_ptr<IGD>& igd, const Mapping& mapRes) |
| 1002 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 1003 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1004 | |
| 1005 | // Check if we have a pending request for this response. |
| 1006 | auto map = getMappingWithKey(mapRes.getMapKey()); |
| 1007 | if (not map) { |
| 1008 | // We may receive a response for a canceled request. Just ignore it. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1009 | if (logger_) logger_->debug("Response for mapping {} [IGD {}] [{}] does not have a local match", |
| 1010 | mapRes.toString(), |
| 1011 | igd->toString(), |
| 1012 | mapRes.getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | // The mapping request is new and successful. Update. |
| 1017 | map->setIgd(igd); |
| 1018 | map->setInternalAddress(mapRes.getInternalAddress()); |
| 1019 | map->setExternalPort(mapRes.getExternalPort()); |
| 1020 | |
| 1021 | // Update the state and report to the owner. |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 1022 | updateMappingState(map, MappingState::OPEN); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1023 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1024 | if (logger_) logger_->debug("Mapping {} (on IGD {} [{}]) successfully performed", |
| 1025 | map->toString(), |
| 1026 | igd->toString(), |
| 1027 | map->getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1028 | |
| 1029 | // Call setValid() to reset the errors counter. We need |
| 1030 | // to reset the counter on each successful response. |
| 1031 | igd->setValid(true); |
| 1032 | } |
| 1033 | |
| 1034 | #if HAVE_LIBNATPMP |
| 1035 | void |
| 1036 | UPnPContext::onMappingRenewed(const std::shared_ptr<IGD>& igd, const Mapping& map) |
| 1037 | { |
| 1038 | auto mapPtr = getMappingWithKey(map.getMapKey()); |
| 1039 | |
| 1040 | if (not mapPtr) { |
| 1041 | // We may receive a notification for a canceled request. Ignore it. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1042 | if (logger_) logger_->warn("Renewed mapping {} from IGD {} [{}] does not have a match in local list", |
| 1043 | map.toString(), |
| 1044 | igd->toString(), |
| 1045 | map.getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1046 | return; |
| 1047 | } |
| 1048 | if (mapPtr->getProtocol() != NatProtocolType::NAT_PMP or not mapPtr->isValid() |
| 1049 | or mapPtr->getState() != MappingState::OPEN) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1050 | if (logger_) logger_->warn("Renewed mapping {} from IGD {} [{}] is in unexpected state", |
| 1051 | mapPtr->toString(), |
| 1052 | igd->toString(), |
| 1053 | mapPtr->getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | mapPtr->setRenewalTime(map.getRenewalTime()); |
| 1058 | } |
| 1059 | #endif |
| 1060 | |
| 1061 | void |
| 1062 | UPnPContext::requestRemoveMapping(const Mapping::sharedPtr_t& map) |
| 1063 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 1064 | if (not map or not map->isValid()) { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1065 | // Silently ignore if the mapping is invalid |
| 1066 | return; |
| 1067 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1068 | auto protocol = protocolList_.at(map->getIgd()->getProtocol()); |
| 1069 | protocol->requestMappingRemove(*map); |
| 1070 | } |
| 1071 | |
| 1072 | void |
| 1073 | UPnPContext::deleteAllMappings(PortType type) |
| 1074 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1075 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 1076 | auto& mappingList = getMappingList(type); |
| 1077 | |
| 1078 | for (auto const& [_, map] : mappingList) { |
| 1079 | requestRemoveMapping(map); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | void |
| 1084 | UPnPContext::onMappingRemoved(const std::shared_ptr<IGD>& igd, const Mapping& mapRes) |
| 1085 | { |
| 1086 | if (not mapRes.isValid()) |
| 1087 | return; |
| 1088 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1089 | auto map = getMappingWithKey(mapRes.getMapKey()); |
| 1090 | // Notify the listener. |
| 1091 | if (map and map->getNotifyCallback()) |
| 1092 | map->getNotifyCallback()(map); |
| 1093 | } |
| 1094 | |
| 1095 | Mapping::sharedPtr_t |
| 1096 | UPnPContext::registerMapping(Mapping& map) |
| 1097 | { |
| 1098 | if (map.getExternalPort() == 0) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 1099 | // JAMI_DBG("Port number not set. Will set a random port number"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1100 | auto port = getAvailablePortNumber(map.getType()); |
| 1101 | map.setExternalPort(port); |
| 1102 | map.setInternalPort(port); |
| 1103 | } |
| 1104 | |
| 1105 | // Newly added mapping must be in pending state by default. |
| 1106 | map.setState(MappingState::PENDING); |
| 1107 | |
| 1108 | Mapping::sharedPtr_t mapPtr; |
| 1109 | |
| 1110 | { |
| 1111 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 1112 | auto& mappingList = getMappingList(map.getType()); |
| 1113 | |
| 1114 | auto ret = mappingList.emplace(map.getMapKey(), std::make_shared<Mapping>(map)); |
| 1115 | if (not ret.second) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1116 | if (logger_) logger_->warn("Mapping request for {} already added!", map.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1117 | return {}; |
| 1118 | } |
| 1119 | mapPtr = ret.first->second; |
| 1120 | assert(mapPtr); |
| 1121 | } |
| 1122 | |
| 1123 | // No available IGD. The pending mapping requests will be processed |
| 1124 | // when a IGD becomes available (in onIgdAdded() method). |
| 1125 | if (not isReady()) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1126 | if (logger_) logger_->warn("No IGD available. Mapping will be requested when an IGD becomes available"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1127 | } else { |
| 1128 | requestMapping(mapPtr); |
| 1129 | } |
| 1130 | |
| 1131 | return mapPtr; |
| 1132 | } |
| 1133 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1134 | void |
| 1135 | UPnPContext::unregisterMapping(const Mapping::sharedPtr_t& map) |
| 1136 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 1137 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1138 | |
| 1139 | if (not map) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 1140 | // JAMI_ERR("Mapping pointer is null"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | if (map->getAutoUpdate()) { |
| 1145 | // Dont unregister mappings with auto-update enabled. |
| 1146 | return; |
| 1147 | } |
| 1148 | auto& mappingList = getMappingList(map->getType()); |
| 1149 | |
| 1150 | if (mappingList.erase(map->getMapKey()) == 1) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1151 | if (logger_) logger_->debug("Unregistered mapping {}", map->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1152 | } else { |
| 1153 | // The mapping may already be un-registered. Just ignore it. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1154 | if (logger_) logger_->debug("Mapping {} [{}] does not have a local match", |
| 1155 | map->toString(), |
| 1156 | map->getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | std::map<Mapping::key_t, Mapping::sharedPtr_t>& |
| 1161 | UPnPContext::getMappingList(PortType type) |
| 1162 | { |
| 1163 | unsigned typeIdx = type == PortType::TCP ? 0 : 1; |
| 1164 | return mappingList_[typeIdx]; |
| 1165 | } |
| 1166 | |
| 1167 | Mapping::sharedPtr_t |
| 1168 | UPnPContext::getMappingWithKey(Mapping::key_t key) |
| 1169 | { |
| 1170 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 1171 | auto const& mappingList = getMappingList(Mapping::getTypeFromMapKey(key)); |
| 1172 | auto it = mappingList.find(key); |
| 1173 | if (it == mappingList.end()) |
| 1174 | return nullptr; |
| 1175 | return it->second; |
| 1176 | } |
| 1177 | |
| 1178 | void |
| 1179 | UPnPContext::getMappingStatus(PortType type, MappingStatus& status) |
| 1180 | { |
| 1181 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 1182 | auto& mappingList = getMappingList(type); |
| 1183 | |
| 1184 | for (auto const& [_, map] : mappingList) { |
| 1185 | switch (map->getState()) { |
| 1186 | case MappingState::PENDING: { |
| 1187 | status.pendingCount_++; |
| 1188 | break; |
| 1189 | } |
| 1190 | case MappingState::IN_PROGRESS: { |
| 1191 | status.inProgressCount_++; |
| 1192 | break; |
| 1193 | } |
| 1194 | case MappingState::FAILED: { |
| 1195 | status.failedCount_++; |
| 1196 | break; |
| 1197 | } |
| 1198 | case MappingState::OPEN: { |
| 1199 | status.openCount_++; |
| 1200 | if (map->isAvailable()) |
| 1201 | status.readyCount_++; |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | default: |
| 1206 | // Must not get here. |
| 1207 | assert(false); |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | void |
| 1214 | UPnPContext::getMappingStatus(MappingStatus& status) |
| 1215 | { |
| 1216 | getMappingStatus(PortType::TCP, status); |
| 1217 | getMappingStatus(PortType::UDP, status); |
| 1218 | } |
| 1219 | |
| 1220 | void |
| 1221 | UPnPContext::onMappingRequestFailed(const Mapping& mapRes) |
| 1222 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1223 | auto const& map = getMappingWithKey(mapRes.getMapKey()); |
| 1224 | if (not map) { |
| 1225 | // We may receive a response for a removed request. Just ignore it. |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1226 | if (logger_) logger_->debug("Mapping {} [IGD {}] does not have a local match", |
| 1227 | mapRes.toString(), |
| 1228 | mapRes.getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | auto igd = map->getIgd(); |
| 1233 | if (not igd) { |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1234 | if (logger_) logger_->error("IGD pointer is null"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1235 | return; |
| 1236 | } |
| 1237 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 1238 | updateMappingState(map, MappingState::FAILED); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1239 | unregisterMapping(map); |
| 1240 | |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 1241 | if (logger_) logger_->warn("Mapping request for {} failed on IGD {} [{}]", |
| 1242 | map->toString(), |
| 1243 | igd->toString(), |
| 1244 | igd->getProtocolName()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1245 | } |
| 1246 | |
Adrien Beraud | 3bd61c9 | 2023-08-17 16:57:37 -0400 | [diff] [blame] | 1247 | void |
| 1248 | UPnPContext::updateMappingState(const Mapping::sharedPtr_t& map, MappingState newState, bool notify) |
| 1249 | { |
| 1250 | // CHECK_VALID_THREAD(); |
| 1251 | |
| 1252 | assert(map); |
| 1253 | |
| 1254 | // Ignore if the state did not change. |
| 1255 | if (newState == map->getState()) { |
| 1256 | // JAMI_DBG("Mapping %s already in state %s", map->toString().c_str(), map->getStateStr()); |
| 1257 | return; |
| 1258 | } |
| 1259 | |
| 1260 | // Update the state. |
| 1261 | map->setState(newState); |
| 1262 | |
| 1263 | // Notify the listener if set. |
| 1264 | if (notify and map->getNotifyCallback()) |
| 1265 | map->getNotifyCallback()(map); |
| 1266 | } |
| 1267 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1268 | #if HAVE_LIBNATPMP |
| 1269 | void |
| 1270 | UPnPContext::renewAllocations() |
| 1271 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 1272 | //CHECK_VALID_THREAD(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1273 | |
| 1274 | // Check if the we have valid PMP IGD. |
| 1275 | auto pmpProto = protocolList_.at(NatProtocolType::NAT_PMP); |
| 1276 | |
| 1277 | auto now = sys_clock::now(); |
| 1278 | std::vector<Mapping::sharedPtr_t> toRenew; |
| 1279 | |
| 1280 | for (auto type : {PortType::TCP, PortType::UDP}) { |
| 1281 | std::lock_guard<std::mutex> lock(mappingMutex_); |
| 1282 | auto mappingList = getMappingList(type); |
| 1283 | for (auto const& [_, map] : mappingList) { |
| 1284 | if (not map->isValid()) |
| 1285 | continue; |
| 1286 | if (map->getProtocol() != NatProtocolType::NAT_PMP) |
| 1287 | continue; |
| 1288 | if (map->getState() != MappingState::OPEN) |
| 1289 | continue; |
| 1290 | if (now < map->getRenewalTime()) |
| 1291 | continue; |
| 1292 | |
| 1293 | toRenew.emplace_back(map); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | // Quit if there are no mapping to renew |
| 1298 | if (toRenew.empty()) |
| 1299 | return; |
| 1300 | |
| 1301 | for (auto const& map : toRenew) { |
| 1302 | pmpProto->requestMappingRenew(*map); |
| 1303 | } |
| 1304 | } |
| 1305 | #endif |
| 1306 | |
| 1307 | } // namespace upnp |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 1308 | } // namespace dhtnet |