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