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/mapping.h" |
| 18 | // #include "logger.h" |
Adrien Béraud | 33a03ba | 2023-06-21 11:35:54 -0400 | [diff] [blame] | 19 | #include "igd.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 20 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 21 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 22 | namespace upnp { |
| 23 | |
| 24 | Mapping::Mapping(PortType type, uint16_t portExternal, uint16_t portInternal, bool available) |
| 25 | : type_(type) |
| 26 | , externalPort_(portExternal) |
| 27 | , internalPort_(portInternal) |
| 28 | , internalAddr_() |
| 29 | , igd_() |
| 30 | , available_(available) |
| 31 | , state_(MappingState::PENDING) |
| 32 | , notifyCb_(nullptr) |
| 33 | , autoUpdate_(false) |
| 34 | #if HAVE_LIBNATPMP |
| 35 | , renewalTime_(sys_clock::now()) |
| 36 | #endif |
| 37 | {} |
| 38 | |
| 39 | Mapping::Mapping(const Mapping& other) |
| 40 | { |
| 41 | std::lock_guard<std::mutex> lock(other.mutex_); |
| 42 | |
| 43 | internalAddr_ = other.internalAddr_; |
| 44 | internalPort_ = other.internalPort_; |
| 45 | externalPort_ = other.externalPort_; |
| 46 | type_ = other.type_; |
| 47 | igd_ = other.igd_; |
| 48 | available_ = other.available_; |
| 49 | state_ = other.state_; |
| 50 | notifyCb_ = other.notifyCb_; |
| 51 | autoUpdate_ = other.autoUpdate_; |
| 52 | #if HAVE_LIBNATPMP |
| 53 | renewalTime_ = other.renewalTime_; |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | Mapping::updateFrom(const Mapping::sharedPtr_t& other) |
| 59 | { |
| 60 | updateFrom(*other); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Mapping::updateFrom(const Mapping& other) |
| 65 | { |
| 66 | if (type_ != other.type_) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 67 | // JAMI_ERR("The source and destination types must match"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
| 71 | internalAddr_ = std::move(other.internalAddr_); |
| 72 | internalPort_ = other.internalPort_; |
| 73 | externalPort_ = other.externalPort_; |
| 74 | igd_ = other.igd_; |
| 75 | state_ = other.state_; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | Mapping::setAvailable(bool val) |
| 80 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 81 | // JAMI_DBG("Changing mapping %s state from %s to %s", |
| 82 | // toString().c_str(), |
| 83 | // available_ ? "AVAILABLE" : "UNAVAILABLE", |
| 84 | // val ? "AVAILABLE" : "UNAVAILABLE"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 85 | |
| 86 | std::lock_guard<std::mutex> lock(mutex_); |
| 87 | available_ = val; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | Mapping::setState(const MappingState& state) |
| 92 | { |
| 93 | std::lock_guard<std::mutex> lock(mutex_); |
| 94 | state_ = state; |
| 95 | } |
| 96 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame^] | 97 | void |
| 98 | Mapping::updateState(const MappingState& newState, bool notify) |
| 99 | { |
| 100 | std::unique_lock<std::mutex> lock(mutex_); |
| 101 | if (newState == state_) |
| 102 | return; |
| 103 | state_ = newState; |
| 104 | |
| 105 | if (notify && notifyCb_) { |
| 106 | lock.unlock(); |
| 107 | notifyCb_(shared_from_this()); |
| 108 | } |
| 109 | } |
| 110 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 111 | const char* |
| 112 | Mapping::getStateStr() const |
| 113 | { |
| 114 | std::lock_guard<std::mutex> lock(mutex_); |
| 115 | return getStateStr(state_); |
| 116 | } |
| 117 | |
| 118 | std::string |
| 119 | Mapping::toString(bool extraInfo) const |
| 120 | { |
| 121 | std::lock_guard<std::mutex> lock(mutex_); |
| 122 | std::ostringstream descr; |
| 123 | descr << UPNP_MAPPING_DESCRIPTION_PREFIX << "-" << getTypeStr(type_); |
| 124 | descr << ":" << std::to_string(internalPort_); |
| 125 | |
| 126 | if (extraInfo) { |
| 127 | descr << " (state=" << getStateStr(state_) |
| 128 | << ", auto-update=" << (autoUpdate_ ? "YES" : "NO") << ")"; |
| 129 | } |
| 130 | |
| 131 | return descr.str(); |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | Mapping::isValid() const |
| 136 | { |
| 137 | std::lock_guard<std::mutex> lock(mutex_); |
| 138 | if (state_ == MappingState::FAILED) |
| 139 | return false; |
| 140 | if (internalPort_ == 0) |
| 141 | return false; |
| 142 | if (externalPort_ == 0) |
| 143 | return false; |
| 144 | if (not igd_ or not igd_->isValid()) |
| 145 | return false; |
| 146 | IpAddr intAddr(internalAddr_); |
| 147 | return intAddr and not intAddr.isLoopback(); |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | Mapping::hasValidHostAddress() const |
| 152 | { |
| 153 | std::lock_guard<std::mutex> lock(mutex_); |
| 154 | |
| 155 | IpAddr intAddr(internalAddr_); |
| 156 | return intAddr and not intAddr.isLoopback(); |
| 157 | } |
| 158 | |
| 159 | bool |
| 160 | Mapping::hasPublicAddress() const |
| 161 | { |
| 162 | std::lock_guard<std::mutex> lock(mutex_); |
| 163 | |
| 164 | return igd_ and igd_->getPublicIp() and not igd_->getPublicIp().isPrivate(); |
| 165 | } |
| 166 | |
| 167 | Mapping::key_t |
| 168 | Mapping::getMapKey() const |
| 169 | { |
| 170 | std::lock_guard<std::mutex> lock(mutex_); |
| 171 | |
| 172 | key_t mapKey = internalPort_; |
| 173 | if (type_ == PortType::UDP) |
| 174 | mapKey |= 1 << (sizeof(uint16_t) * 8); |
| 175 | return mapKey; |
| 176 | } |
| 177 | |
| 178 | PortType |
| 179 | Mapping::getTypeFromMapKey(key_t key) |
| 180 | { |
| 181 | return (key >> (sizeof(uint16_t) * 8)) ? PortType::UDP : PortType::TCP; |
| 182 | } |
| 183 | |
| 184 | std::string |
| 185 | Mapping::getExternalAddress() const |
| 186 | { |
| 187 | std::lock_guard<std::mutex> lock(mutex_); |
| 188 | if (igd_) |
| 189 | return igd_->getPublicIp().toString(); |
| 190 | return {}; |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | Mapping::setExternalPort(uint16_t port) |
| 195 | { |
| 196 | std::lock_guard<std::mutex> lock(mutex_); |
| 197 | externalPort_ = port; |
| 198 | } |
| 199 | |
| 200 | uint16_t |
| 201 | Mapping::getExternalPort() const |
| 202 | { |
| 203 | std::lock_guard<std::mutex> lock(mutex_); |
| 204 | return externalPort_; |
| 205 | } |
| 206 | |
| 207 | std::string |
| 208 | Mapping::getExternalPortStr() const |
| 209 | { |
| 210 | std::lock_guard<std::mutex> lock(mutex_); |
| 211 | return std::to_string(externalPort_); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | Mapping::setInternalAddress(const std::string& addr) |
| 216 | { |
| 217 | std::lock_guard<std::mutex> lock(mutex_); |
| 218 | internalAddr_ = addr; |
| 219 | } |
| 220 | |
| 221 | std::string |
| 222 | Mapping::getInternalAddress() const |
| 223 | { |
| 224 | std::lock_guard<std::mutex> lock(mutex_); |
| 225 | return internalAddr_; |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | Mapping::setInternalPort(uint16_t port) |
| 230 | { |
| 231 | std::lock_guard<std::mutex> lock(mutex_); |
| 232 | internalPort_ = port; |
| 233 | } |
| 234 | |
| 235 | uint16_t |
| 236 | Mapping::getInternalPort() const |
| 237 | { |
| 238 | std::lock_guard<std::mutex> lock(mutex_); |
| 239 | return internalPort_; |
| 240 | } |
| 241 | |
| 242 | std::string |
| 243 | Mapping::getInternalPortStr() const |
| 244 | { |
| 245 | std::lock_guard<std::mutex> lock(mutex_); |
| 246 | return std::to_string(internalPort_); |
| 247 | } |
| 248 | |
| 249 | PortType |
| 250 | Mapping::getType() const |
| 251 | { |
| 252 | std::lock_guard<std::mutex> lock(mutex_); |
| 253 | return type_; |
| 254 | } |
| 255 | |
| 256 | const char* |
| 257 | Mapping::getTypeStr() const |
| 258 | { |
| 259 | std::lock_guard<std::mutex> lock(mutex_); |
| 260 | return getTypeStr(type_); |
| 261 | } |
| 262 | |
| 263 | bool |
| 264 | Mapping::isAvailable() const |
| 265 | { |
| 266 | std::lock_guard<std::mutex> lock(mutex_); |
| 267 | return available_; |
| 268 | } |
| 269 | |
| 270 | std::shared_ptr<IGD> |
| 271 | Mapping::getIgd() const |
| 272 | { |
| 273 | std::lock_guard<std::mutex> lock(mutex_); |
| 274 | return igd_; |
| 275 | } |
| 276 | |
| 277 | NatProtocolType |
| 278 | Mapping::getProtocol() const |
| 279 | { |
| 280 | std::lock_guard<std::mutex> lock(mutex_); |
| 281 | if (igd_) |
| 282 | return igd_->getProtocol(); |
| 283 | return NatProtocolType::UNKNOWN; |
| 284 | } |
| 285 | const char* |
| 286 | Mapping::getProtocolName() const |
| 287 | { |
| 288 | if (igd_) { |
| 289 | if (igd_->getProtocol() == NatProtocolType::NAT_PMP) |
| 290 | return "NAT-PMP"; |
| 291 | if (igd_->getProtocol() == NatProtocolType::PUPNP) |
| 292 | return "PUPNP"; |
| 293 | } |
| 294 | return "UNKNOWN"; |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | Mapping::setIgd(const std::shared_ptr<IGD>& igd) |
| 299 | { |
| 300 | std::lock_guard<std::mutex> lock(mutex_); |
| 301 | igd_ = igd; |
| 302 | } |
| 303 | |
| 304 | MappingState |
| 305 | Mapping::getState() const |
| 306 | { |
| 307 | std::lock_guard<std::mutex> lock(mutex_); |
| 308 | return state_; |
| 309 | } |
| 310 | |
| 311 | Mapping::NotifyCallback |
| 312 | Mapping::getNotifyCallback() const |
| 313 | { |
| 314 | std::lock_guard<std::mutex> lock(mutex_); |
| 315 | return notifyCb_; |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | Mapping::setNotifyCallback(NotifyCallback cb) |
| 320 | { |
| 321 | std::lock_guard<std::mutex> lock(mutex_); |
| 322 | notifyCb_ = std::move(cb); |
| 323 | } |
| 324 | |
| 325 | void |
| 326 | Mapping::enableAutoUpdate(bool enable) |
| 327 | { |
| 328 | std::lock_guard<std::mutex> lock(mutex_); |
| 329 | autoUpdate_ = enable; |
| 330 | } |
| 331 | |
| 332 | bool |
| 333 | Mapping::getAutoUpdate() const |
| 334 | { |
| 335 | std::lock_guard<std::mutex> lock(mutex_); |
| 336 | return autoUpdate_; |
| 337 | } |
| 338 | |
| 339 | #if HAVE_LIBNATPMP |
| 340 | sys_clock::time_point |
| 341 | Mapping::getRenewalTime() const |
| 342 | { |
| 343 | std::lock_guard<std::mutex> lock(mutex_); |
| 344 | return renewalTime_; |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | Mapping::setRenewalTime(sys_clock::time_point time) |
| 349 | { |
| 350 | std::lock_guard<std::mutex> lock(mutex_); |
| 351 | renewalTime_ = time; |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | } // namespace upnp |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 356 | } // namespace dhtnet |