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