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 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #include "nat_pmp.h" |
| 18 | |
| 19 | #if HAVE_LIBNATPMP |
Andreas Traczyk | 8f7e1bd | 2024-04-04 16:26:58 -0400 | [diff] [blame] | 20 | #ifdef _WIN32 |
| 21 | // On Windows we assume WSAStartup is called during DHT initialization |
| 22 | #include <winsock2.h> |
| 23 | #include <ws2tcpip.h> |
| 24 | #else |
Sébastien Blin | baa52d9 | 2024-03-25 13:28:30 -0400 | [diff] [blame] | 25 | #include <poll.h> |
Andreas Traczyk | 8f7e1bd | 2024-04-04 16:26:58 -0400 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #ifdef _WIN32 |
| 29 | #define _poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) |
| 30 | #else |
| 31 | #define _poll(fds, nfds, timeout) poll(fds, nfds, timeout) |
| 32 | #endif |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 34 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 35 | namespace upnp { |
| 36 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 37 | NatPmp::NatPmp(const std::shared_ptr<asio::io_context>& ctx, const std::shared_ptr<dht::log::Logger>& logger) |
| 38 | : UPnPProtocol(logger), ioContext(ctx), searchForIgdTimer_(*ctx) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 39 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 40 | // JAMI_DBG("NAT-PMP: Instance [%p] created", this); |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 41 | ioContext->dispatch([this] { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 42 | igd_ = std::make_shared<PMPIGD>(); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | NatPmp::~NatPmp() |
| 47 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 48 | // JAMI_DBG("NAT-PMP: Instance [%p] destroyed", this); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void |
| 52 | NatPmp::initNatPmp() |
| 53 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 54 | initialized_ = false; |
| 55 | |
| 56 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 57 | std::lock_guard lock(natpmpMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 58 | hostAddress_ = ip_utils::getLocalAddr(AF_INET); |
| 59 | } |
| 60 | |
| 61 | // Local address must be valid. |
| 62 | if (not getHostAddress() or getHostAddress().isLoopback()) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 63 | if (logger_) logger_->warn("NAT-PMP: Does not have a valid local address!"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | |
| 67 | assert(igd_); |
| 68 | if (igd_->isValid()) { |
| 69 | igd_->setValid(false); |
| 70 | processIgdUpdate(UpnpIgdEvent::REMOVED); |
| 71 | } |
| 72 | |
| 73 | igd_->setLocalIp(IpAddr()); |
| 74 | igd_->setPublicIp(IpAddr()); |
| 75 | igd_->setUID(""); |
| 76 | |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 77 | if (logger_) logger_->debug("NAT-PMP: Trying to initialize IGD"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 78 | |
| 79 | int err = initnatpmp(&natpmpHdl_, 0, 0); |
| 80 | |
| 81 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 82 | if (logger_) logger_->warn("NAT-PMP: Initializing IGD using default gateway failed!"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 83 | const auto& localGw = ip_utils::getLocalGateway(); |
| 84 | if (not localGw) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 85 | if (logger_) logger_->warn("NAT-PMP: Couldn't find valid gateway on local host"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 86 | err = NATPMP_ERR_CANNOTGETGATEWAY; |
| 87 | } else { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 88 | if (logger_) logger_->warn("NAT-PMP: Trying to initialize using detected gateway {}", |
| 89 | localGw.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 90 | struct in_addr inaddr; |
| 91 | inet_pton(AF_INET, localGw.toString().c_str(), &inaddr); |
| 92 | err = initnatpmp(&natpmpHdl_, 1, inaddr.s_addr); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 97 | if (logger_) logger_->error("NAT-PMP: Can't initialize libnatpmp -> {}", getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | char addrbuf[INET_ADDRSTRLEN]; |
| 102 | inet_ntop(AF_INET, &natpmpHdl_.gateway, addrbuf, sizeof(addrbuf)); |
| 103 | IpAddr igdAddr(addrbuf); |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 104 | if (logger_) logger_->debug("NAT-PMP: Initialized on gateway {}", igdAddr.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 105 | |
| 106 | // Set the local (gateway) address. |
| 107 | igd_->setLocalIp(igdAddr); |
| 108 | // NAT-PMP protocol does not have UID, but we will set generic |
| 109 | // one debugging purposes. |
| 110 | igd_->setUID("NAT-PMP Gateway"); |
| 111 | |
| 112 | // Search and set the public address. |
| 113 | getIgdPublicAddress(); |
| 114 | |
| 115 | // Update and notify. |
| 116 | if (igd_->isValid()) { |
| 117 | initialized_ = true; |
| 118 | processIgdUpdate(UpnpIgdEvent::ADDED); |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | NatPmp::setObserver(UpnpMappingObserver* obs) |
| 124 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 125 | observer_ = obs; |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | NatPmp::terminate(std::condition_variable& cv) |
| 130 | { |
Adrien Béraud | 7a82bee | 2023-08-30 10:26:45 -0400 | [diff] [blame] | 131 | if (logger_) logger_->debug("NAT-PMP: Terminate instance {}", fmt::ptr(this)); |
| 132 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 133 | initialized_ = false; |
| 134 | observer_ = nullptr; |
| 135 | |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 136 | std::lock_guard lock(natpmpMutex_); |
Adrien Béraud | 7a82bee | 2023-08-30 10:26:45 -0400 | [diff] [blame] | 137 | shutdownComplete_ = true; |
| 138 | cv.notify_one(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | NatPmp::terminate() |
| 143 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 144 | std::condition_variable cv {}; |
| 145 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 146 | ioContext->dispatch([&] { |
| 147 | terminate(cv); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 148 | }); |
| 149 | |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 150 | std::unique_lock lk(natpmpMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 151 | if (cv.wait_for(lk, std::chrono::seconds(10), [this] { return shutdownComplete_; })) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 152 | if (logger_) logger_->debug("NAT-PMP: Shutdown completed"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 153 | } else { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 154 | if (logger_) logger_->error("NAT-PMP: Shutdown timed-out"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | const IpAddr |
| 159 | NatPmp::getHostAddress() const |
| 160 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 161 | std::lock_guard lock(natpmpMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 162 | return hostAddress_; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | NatPmp::clearIgds() |
| 167 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 168 | bool do_close = false; |
| 169 | |
| 170 | if (igd_) { |
| 171 | if (igd_->isValid()) { |
| 172 | do_close = true; |
| 173 | } |
| 174 | igd_->setValid(false); |
| 175 | } |
| 176 | |
| 177 | initialized_ = false; |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 178 | searchForIgdTimer_.cancel(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 179 | |
| 180 | igdSearchCounter_ = 0; |
| 181 | |
| 182 | if (do_close) { |
| 183 | closenatpmp(&natpmpHdl_); |
| 184 | memset(&natpmpHdl_, 0, sizeof(natpmpHdl_)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | NatPmp::searchForIgd() |
| 190 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 191 | if (not initialized_) { |
| 192 | initNatPmp(); |
| 193 | } |
| 194 | |
| 195 | // Schedule a retry in case init failed. |
| 196 | if (not initialized_) { |
| 197 | if (igdSearchCounter_++ < MAX_RESTART_SEARCH_RETRIES) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 198 | if (logger_) logger_->debug("NAT-PMP: Start search for IGDs. Attempt {}", igdSearchCounter_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 199 | |
| 200 | // Cancel the current timer (if any) and re-schedule. |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 201 | searchForIgdTimer_.expires_after(NATPMP_SEARCH_RETRY_UNIT * igdSearchCounter_); |
Sébastien Blin | d1e018c | 2023-09-01 09:15:12 -0400 | [diff] [blame] | 202 | searchForIgdTimer_.async_wait([w=weak()](const asio::error_code& ec) { |
| 203 | if (!ec) { |
| 204 | if (auto shared = w.lock()) |
| 205 | shared->searchForIgd(); |
| 206 | } |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 207 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 208 | } else { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 209 | if (logger_) logger_->warn("NAT-PMP: Setup failed after {} trials. NAT-PMP will be disabled!", |
| 210 | MAX_RESTART_SEARCH_RETRIES); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | std::list<std::shared_ptr<IGD>> |
| 216 | NatPmp::getIgdList() const |
| 217 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 218 | std::lock_guard lock(natpmpMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 219 | std::list<std::shared_ptr<IGD>> igdList; |
| 220 | if (igd_->isValid()) |
| 221 | igdList.emplace_back(igd_); |
| 222 | return igdList; |
| 223 | } |
| 224 | |
| 225 | bool |
| 226 | NatPmp::isReady() const |
| 227 | { |
| 228 | if (observer_ == nullptr) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 229 | if (logger_) logger_->error("NAT-PMP: the observer is not set!"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
| 233 | // Must at least have a valid local address. |
| 234 | if (not getHostAddress() or getHostAddress().isLoopback()) |
| 235 | return false; |
| 236 | |
| 237 | return igd_ and igd_->isValid(); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | NatPmp::incrementErrorsCounter(const std::shared_ptr<IGD>& igdIn) |
| 242 | { |
| 243 | if (not validIgdInstance(igdIn)) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (not igd_->isValid()) { |
| 248 | // Already invalid. Nothing to do. |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | if (not igd_->incrementErrorsCounter()) { |
| 253 | // Disable this IGD. |
| 254 | igd_->setValid(false); |
| 255 | // Notify the listener. |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 256 | if (logger_) logger_->warn("NAT-PMP: No more valid IGD!"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 257 | |
| 258 | processIgdUpdate(UpnpIgdEvent::INVALID_STATE); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | NatPmp::requestMappingAdd(const Mapping& mapping) |
| 264 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 265 | Mapping map(mapping); |
| 266 | assert(map.getIgd()); |
| 267 | auto err = addPortMapping(map); |
| 268 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 269 | if (logger_) logger_->warn("NAT-PMP: Request for mapping {} on {} failed with error {:d}: {}", |
| 270 | map.toString(), |
| 271 | igd_->toString(), |
| 272 | err, |
| 273 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 274 | |
| 275 | if (isErrorFatal(err)) { |
| 276 | // Fatal error, increment the counter. |
| 277 | incrementErrorsCounter(igd_); |
| 278 | } |
| 279 | // Notify the listener. |
| 280 | processMappingRequestFailed(std::move(map)); |
| 281 | } else { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 282 | if (logger_) logger_->debug("NAT-PMP: Request for mapping {:s} on {:s} succeeded", |
| 283 | map.toString(), |
| 284 | igd_->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 285 | // Notify the listener. |
| 286 | processMappingAdded(std::move(map)); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | NatPmp::requestMappingRenew(const Mapping& mapping) |
| 292 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 293 | Mapping map(mapping); |
| 294 | auto err = addPortMapping(map); |
| 295 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 296 | if (logger_) logger_->warn("NAT-PMP: Renewal request for mapping {} on {} failed with error {:d}: {}", |
François-Simon Fauteux-Chapleau | fd29c1d | 2024-05-30 16:48:26 -0400 | [diff] [blame] | 297 | map.toString(), |
| 298 | igd_->toString(), |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 299 | err, |
| 300 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 301 | // Notify the listener. |
| 302 | processMappingRequestFailed(std::move(map)); |
| 303 | |
| 304 | if (isErrorFatal(err)) { |
| 305 | // Fatal error, increment the counter. |
| 306 | incrementErrorsCounter(igd_); |
| 307 | } |
| 308 | } else { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 309 | if (logger_) logger_->debug("NAT-PMP: Renewal request for mapping {} on {} succeeded", |
François-Simon Fauteux-Chapleau | fd29c1d | 2024-05-30 16:48:26 -0400 | [diff] [blame] | 310 | map.toString(), |
| 311 | igd_->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 312 | // Notify the listener. |
| 313 | processMappingRenewed(map); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | int |
| 318 | NatPmp::readResponse(natpmp_t& handle, natpmpresp_t& response) |
| 319 | { |
| 320 | int err = 0; |
| 321 | unsigned readRetriesCounter = 0; |
| 322 | |
| 323 | while (true) { |
| 324 | if (readRetriesCounter++ > MAX_READ_RETRIES) { |
| 325 | err = NATPMP_ERR_SOCKETERROR; |
| 326 | break; |
| 327 | } |
| 328 | |
Sébastien Blin | 4cf587e | 2024-03-08 16:00:18 -0500 | [diff] [blame] | 329 | struct pollfd fds; |
| 330 | fds.fd = handle.s; |
| 331 | fds.events = POLLIN; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 332 | struct timeval timeout; |
Sébastien Blin | 7733109 | 2024-05-17 10:58:23 -0400 | [diff] [blame] | 333 | err = getnatpmprequesttimeout(&handle, &timeout); |
| 334 | int millis = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000); |
| 335 | // Note, getnatpmprequesttimeout can be negative if previous deadline is passed. |
| 336 | if (err != 0 || millis < 0) |
| 337 | millis = 50; |
Andreas Traczyk | 8f7e1bd | 2024-04-04 16:26:58 -0400 | [diff] [blame] | 338 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 339 | // Wait for data. |
Andreas Traczyk | 8f7e1bd | 2024-04-04 16:26:58 -0400 | [diff] [blame] | 340 | if (_poll(&fds, 1, millis) == -1) { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 341 | err = NATPMP_ERR_SOCKETERROR; |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | // Read the data. |
| 346 | err = readnatpmpresponseorretry(&handle, &response); |
| 347 | |
| 348 | if (err == NATPMP_TRYAGAIN) { |
| 349 | std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT_BEFORE_READ_RETRY)); |
| 350 | } else { |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | int |
François-Simon Fauteux-Chapleau | fd29c1d | 2024-05-30 16:48:26 -0400 | [diff] [blame] | 359 | NatPmp::sendMappingRequest(Mapping& mapping, uint32_t& lifetime) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 360 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 361 | int err = sendnewportmappingrequest(&natpmpHdl_, |
| 362 | mapping.getType() == PortType::UDP ? NATPMP_PROTOCOL_UDP |
| 363 | : NATPMP_PROTOCOL_TCP, |
| 364 | mapping.getInternalPort(), |
| 365 | mapping.getExternalPort(), |
| 366 | lifetime); |
| 367 | |
| 368 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 369 | if (logger_) logger_->error("NAT-PMP: Send mapping request failed with error {} {:d}", |
| 370 | getNatPmpErrorStr(err), |
| 371 | errno); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 372 | return err; |
| 373 | } |
| 374 | |
| 375 | unsigned readRetriesCounter = 0; |
| 376 | |
| 377 | while (readRetriesCounter++ < MAX_READ_RETRIES) { |
| 378 | // Read the response |
| 379 | natpmpresp_t response; |
| 380 | err = readResponse(natpmpHdl_, response); |
| 381 | |
| 382 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 383 | if (logger_) logger_->warn("NAT-PMP: Read response on IGD {} failed with error {}", |
| 384 | igd_->toString(), |
| 385 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 386 | } else if (response.type != NATPMP_RESPTYPE_TCPPORTMAPPING |
| 387 | and response.type != NATPMP_RESPTYPE_UDPPORTMAPPING) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 388 | if (logger_) logger_->error("NAT-PMP: Unexpected response type ({:d}) for mapping {} from IGD {}.", |
| 389 | response.type, |
| 390 | mapping.toString(), |
| 391 | igd_->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 392 | // Try to read again. |
| 393 | continue; |
| 394 | } |
| 395 | |
François-Simon Fauteux-Chapleau | fd29c1d | 2024-05-30 16:48:26 -0400 | [diff] [blame] | 396 | uint16_t newExternalPort = response.pnu.newportmapping.mappedpublicport; |
| 397 | uint32_t newLifetime = response.pnu.newportmapping.lifetime; |
| 398 | if (lifetime > 0) { |
| 399 | // We requested the creation/renewal of a mapping and didn't get an error, so at this point |
| 400 | // newExternalPort and newLifetime should both be nonzero. However, that's not always the case |
| 401 | // in practice (presumably because some routers don't implement NAT-PMP correctly). |
| 402 | if (newExternalPort == 0 || newLifetime == 0) { |
| 403 | if (logger_) logger_->warn("NAT-PMP: mapping request returned without error but the response" |
| 404 | " contains invalid data (external port: {}, lifetime: {})", |
| 405 | newExternalPort, |
| 406 | newLifetime); |
| 407 | err = NATPMP_ERR_INVALIDARGS; |
| 408 | } else { |
| 409 | lifetime = newLifetime; |
| 410 | mapping.setExternalPort(newExternalPort); |
| 411 | } |
| 412 | } |
| 413 | break; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return err; |
| 417 | } |
| 418 | |
| 419 | int |
| 420 | NatPmp::addPortMapping(Mapping& mapping) |
| 421 | { |
| 422 | auto const& igdIn = mapping.getIgd(); |
| 423 | assert(igdIn); |
| 424 | assert(igdIn->getProtocol() == NatProtocolType::NAT_PMP); |
| 425 | |
| 426 | if (not igdIn->isValid() or not validIgdInstance(igdIn)) { |
| 427 | mapping.setState(MappingState::FAILED); |
| 428 | return NATPMP_ERR_INVALIDARGS; |
| 429 | } |
| 430 | |
| 431 | mapping.setInternalAddress(getHostAddress().toString()); |
| 432 | |
| 433 | uint32_t lifetime = MAPPING_ALLOCATION_LIFETIME; |
| 434 | int err = sendMappingRequest(mapping, lifetime); |
| 435 | |
| 436 | if (err < 0) { |
| 437 | mapping.setState(MappingState::FAILED); |
| 438 | return err; |
| 439 | } |
| 440 | |
| 441 | // Set the renewal time and update. |
François-Simon Fauteux-Chapleau | fd29c1d | 2024-05-30 16:48:26 -0400 | [diff] [blame] | 442 | mapping.setRenewalTime(sys_clock::now() + std::chrono::seconds(lifetime / 2)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 443 | mapping.setState(MappingState::OPEN); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | NatPmp::requestMappingRemove(const Mapping& mapping) |
| 450 | { |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 451 | ioContext->dispatch([w = weak(), mapping] { |
| 452 | if (auto pmpThis = w.lock()) { |
| 453 | Mapping map {mapping}; |
| 454 | pmpThis->removePortMapping(map); |
| 455 | } |
| 456 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void |
| 460 | NatPmp::removePortMapping(Mapping& mapping) |
| 461 | { |
| 462 | auto igdIn = mapping.getIgd(); |
| 463 | assert(igdIn); |
| 464 | if (not igdIn->isValid()) { |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | if (not validIgdInstance(igdIn)) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | Mapping mapToRemove(mapping); |
| 473 | |
| 474 | uint32_t lifetime = 0; |
| 475 | int err = sendMappingRequest(mapping, lifetime); |
| 476 | |
| 477 | if (err < 0) { |
| 478 | // Nothing to do if the request fails, just log the error. |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 479 | if (logger_) logger_->warn("NAT-PMP: Send remove request failed with error {}. Ignoring", |
| 480 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Update and notify the listener. |
| 484 | mapToRemove.setState(MappingState::FAILED); |
| 485 | processMappingRemoved(std::move(mapToRemove)); |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | NatPmp::getIgdPublicAddress() |
| 490 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 491 | // Set the public address for this IGD if it does not |
| 492 | // have one already. |
| 493 | if (igd_->getPublicIp()) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 494 | if (logger_) logger_->warn("NAT-PMP: IGD {} already have a public address ({})", |
| 495 | igd_->toString(), |
| 496 | igd_->getPublicIp().toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | assert(igd_->getProtocol() == NatProtocolType::NAT_PMP); |
| 500 | |
| 501 | int err = sendpublicaddressrequest(&natpmpHdl_); |
| 502 | |
| 503 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 504 | if (logger_) logger_->error("NAT-PMP: send public address request on IGD {} failed with error: {}", |
| 505 | igd_->toString(), |
| 506 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 507 | |
| 508 | if (isErrorFatal(err)) { |
| 509 | // Fatal error, increment the counter. |
| 510 | incrementErrorsCounter(igd_); |
| 511 | } |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | natpmpresp_t response; |
| 516 | err = readResponse(natpmpHdl_, response); |
| 517 | |
| 518 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 519 | if (logger_) logger_->warn("NAT-PMP: Read response on IGD {} failed - {}", |
| 520 | igd_->toString(), |
| 521 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | |
| 525 | if (response.type != NATPMP_RESPTYPE_PUBLICADDRESS) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 526 | if (logger_) logger_->error("NAT-PMP: Unexpected response type ({:d}) for public address request from IGD {}.", |
| 527 | response.type, |
| 528 | igd_->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | |
| 532 | IpAddr publicAddr(response.pnu.publicaddress.addr); |
| 533 | |
| 534 | if (not publicAddr) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 535 | if (logger_) logger_->error("NAT-PMP: IGD {} returned an invalid public address {}", |
| 536 | igd_->toString(), |
| 537 | publicAddr.toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Update. |
| 541 | igd_->setPublicIp(publicAddr); |
| 542 | igd_->setValid(true); |
| 543 | |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 544 | if (logger_) logger_->debug("NAT-PMP: Setting IGD {} public address to {}", |
| 545 | igd_->toString(), |
| 546 | igd_->getPublicIp().toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | void |
| 550 | NatPmp::removeAllMappings() |
| 551 | { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 552 | if (logger_) logger_->warn("NAT-PMP: Send request to close all existing mappings to IGD {}", |
| 553 | igd_->toString().c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 554 | |
| 555 | int err = sendnewportmappingrequest(&natpmpHdl_, NATPMP_PROTOCOL_TCP, 0, 0, 0); |
| 556 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 557 | if (logger_) logger_->warn("NAT-PMP: Send close all TCP mappings request failed with error {}", |
| 558 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 559 | } |
| 560 | err = sendnewportmappingrequest(&natpmpHdl_, NATPMP_PROTOCOL_UDP, 0, 0, 0); |
| 561 | if (err < 0) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 562 | if (logger_) logger_->warn("NAT-PMP: Send close all UDP mappings request failed with error {}", |
| 563 | getNatPmpErrorStr(err)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | const char* |
| 568 | NatPmp::getNatPmpErrorStr(int errorCode) const |
| 569 | { |
| 570 | #ifdef ENABLE_STRNATPMPERR |
| 571 | return strnatpmperr(errorCode); |
| 572 | #else |
| 573 | switch (errorCode) { |
| 574 | case NATPMP_ERR_INVALIDARGS: |
| 575 | return "INVALIDARGS"; |
| 576 | break; |
| 577 | case NATPMP_ERR_SOCKETERROR: |
| 578 | return "SOCKETERROR"; |
| 579 | break; |
| 580 | case NATPMP_ERR_CANNOTGETGATEWAY: |
| 581 | return "CANNOTGETGATEWAY"; |
| 582 | break; |
| 583 | case NATPMP_ERR_CLOSEERR: |
| 584 | return "CLOSEERR"; |
| 585 | break; |
| 586 | case NATPMP_ERR_RECVFROM: |
| 587 | return "RECVFROM"; |
| 588 | break; |
| 589 | case NATPMP_ERR_NOPENDINGREQ: |
| 590 | return "NOPENDINGREQ"; |
| 591 | break; |
| 592 | case NATPMP_ERR_NOGATEWAYSUPPORT: |
| 593 | return "NOGATEWAYSUPPORT"; |
| 594 | break; |
| 595 | case NATPMP_ERR_CONNECTERR: |
| 596 | return "CONNECTERR"; |
| 597 | break; |
| 598 | case NATPMP_ERR_WRONGPACKETSOURCE: |
| 599 | return "WRONGPACKETSOURCE"; |
| 600 | break; |
| 601 | case NATPMP_ERR_SENDERR: |
| 602 | return "SENDERR"; |
| 603 | break; |
| 604 | case NATPMP_ERR_FCNTLERROR: |
| 605 | return "FCNTLERROR"; |
| 606 | break; |
| 607 | case NATPMP_ERR_GETTIMEOFDAYERR: |
| 608 | return "GETTIMEOFDAYERR"; |
| 609 | break; |
| 610 | case NATPMP_ERR_UNSUPPORTEDVERSION: |
| 611 | return "UNSUPPORTEDVERSION"; |
| 612 | break; |
| 613 | case NATPMP_ERR_UNSUPPORTEDOPCODE: |
| 614 | return "UNSUPPORTEDOPCODE"; |
| 615 | break; |
| 616 | case NATPMP_ERR_UNDEFINEDERROR: |
| 617 | return "UNDEFINEDERROR"; |
| 618 | break; |
| 619 | case NATPMP_ERR_NOTAUTHORIZED: |
| 620 | return "NOTAUTHORIZED"; |
| 621 | break; |
| 622 | case NATPMP_ERR_NETWORKFAILURE: |
| 623 | return "NETWORKFAILURE"; |
| 624 | break; |
| 625 | case NATPMP_ERR_OUTOFRESOURCES: |
| 626 | return "OUTOFRESOURCES"; |
| 627 | break; |
| 628 | case NATPMP_TRYAGAIN: |
| 629 | return "TRYAGAIN"; |
| 630 | break; |
| 631 | default: |
| 632 | return "UNKNOWNERR"; |
| 633 | break; |
| 634 | } |
| 635 | #endif |
| 636 | } |
| 637 | |
| 638 | bool |
| 639 | NatPmp::isErrorFatal(int error) |
| 640 | { |
| 641 | switch (error) { |
| 642 | case NATPMP_ERR_INVALIDARGS: |
| 643 | case NATPMP_ERR_SOCKETERROR: |
| 644 | case NATPMP_ERR_CANNOTGETGATEWAY: |
| 645 | case NATPMP_ERR_CLOSEERR: |
| 646 | case NATPMP_ERR_RECVFROM: |
| 647 | case NATPMP_ERR_NOGATEWAYSUPPORT: |
| 648 | case NATPMP_ERR_CONNECTERR: |
| 649 | case NATPMP_ERR_SENDERR: |
| 650 | case NATPMP_ERR_UNDEFINEDERROR: |
| 651 | case NATPMP_ERR_UNSUPPORTEDVERSION: |
| 652 | case NATPMP_ERR_UNSUPPORTEDOPCODE: |
| 653 | case NATPMP_ERR_NOTAUTHORIZED: |
| 654 | case NATPMP_ERR_NETWORKFAILURE: |
| 655 | case NATPMP_ERR_OUTOFRESOURCES: |
| 656 | return true; |
| 657 | default: |
| 658 | return false; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | bool |
| 663 | NatPmp::validIgdInstance(const std::shared_ptr<IGD>& igdIn) |
| 664 | { |
| 665 | if (igd_.get() != igdIn.get()) { |
Adrien Béraud | 17765bd | 2023-08-22 21:02:38 -0400 | [diff] [blame] | 666 | if (logger_) logger_->error("NAT-PMP: IGD ({}) does not match local instance ({})", |
| 667 | igdIn->toString(), |
| 668 | igd_->toString()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | void |
| 676 | NatPmp::processIgdUpdate(UpnpIgdEvent event) |
| 677 | { |
| 678 | if (igd_->isValid()) { |
| 679 | // Remove all current mappings if any. |
| 680 | removeAllMappings(); |
| 681 | } |
| 682 | |
| 683 | if (observer_ == nullptr) |
| 684 | return; |
| 685 | // Process the response on the context thread. |
Sébastien Blin | f6baf4b | 2024-01-03 15:51:36 -0500 | [diff] [blame] | 686 | ioContext->post([w = weak(), event] { |
| 687 | if (auto shared = w.lock()) { |
| 688 | if (!shared->shutdownComplete_) { |
| 689 | shared->observer_->onIgdUpdated(shared->igd_, event); |
| 690 | } |
| 691 | } |
| 692 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void |
| 696 | NatPmp::processMappingAdded(const Mapping& map) |
| 697 | { |
| 698 | if (observer_ == nullptr) |
| 699 | return; |
| 700 | |
| 701 | // Process the response on the context thread. |
Sébastien Blin | f6baf4b | 2024-01-03 15:51:36 -0500 | [diff] [blame] | 702 | ioContext->post([w=weak(), map] { |
| 703 | if (auto shared = w.lock()) { |
| 704 | if (!shared->shutdownComplete_) { |
| 705 | shared->observer_->onMappingAdded(shared->igd_, map); |
| 706 | } |
| 707 | } |
| 708 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void |
| 712 | NatPmp::processMappingRequestFailed(const Mapping& map) |
| 713 | { |
| 714 | if (observer_ == nullptr) |
| 715 | return; |
| 716 | |
| 717 | // Process the response on the context thread. |
Sébastien Blin | f6baf4b | 2024-01-03 15:51:36 -0500 | [diff] [blame] | 718 | ioContext->post([w=weak(), map] { |
| 719 | if (auto shared = w.lock()) { |
| 720 | if (!shared->shutdownComplete_) { |
| 721 | shared->observer_->onMappingRequestFailed(map); |
| 722 | } |
| 723 | } |
| 724 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void |
| 728 | NatPmp::processMappingRenewed(const Mapping& map) |
| 729 | { |
| 730 | if (observer_ == nullptr) |
| 731 | return; |
| 732 | |
| 733 | // Process the response on the context thread. |
Sébastien Blin | f6baf4b | 2024-01-03 15:51:36 -0500 | [diff] [blame] | 734 | ioContext->post([w=weak(), map] { |
| 735 | if (auto shared = w.lock()) { |
| 736 | if (!shared->shutdownComplete_) { |
| 737 | shared->observer_->onMappingRenewed(shared->igd_, map); |
| 738 | } |
| 739 | } |
| 740 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void |
| 744 | NatPmp::processMappingRemoved(const Mapping& map) |
| 745 | { |
| 746 | if (observer_ == nullptr) |
| 747 | return; |
| 748 | |
| 749 | // Process the response on the context thread. |
Sébastien Blin | f6baf4b | 2024-01-03 15:51:36 -0500 | [diff] [blame] | 750 | ioContext->post([w=weak(), map] { |
| 751 | if (auto shared = w.lock()) { |
| 752 | if (!shared->shutdownComplete_) { |
| 753 | shared->observer_->onMappingRemoved(shared->igd_, map); |
| 754 | } |
| 755 | } |
| 756 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | } // namespace upnp |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 760 | } // namespace dhtnet |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 761 | |
| 762 | #endif //-- #if HAVE_LIBNATPMP |