Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
| 5 | * Author: Eden Abitbol <eden.abitbol@savoirfairelinux.com> |
| 6 | * Author: Mohamed Chibani <mohamed.chibani@savoirfairelinux.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 23 | #include "upnp/mapping.h" |
| 24 | // #include "logger.h" |
Adrien Béraud | 33a03ba | 2023-06-21 11:35:54 -0400 | [diff] [blame] | 25 | #include "igd.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 26 | |
| 27 | namespace jami { |
| 28 | namespace upnp { |
| 29 | |
| 30 | Mapping::Mapping(PortType type, uint16_t portExternal, uint16_t portInternal, bool available) |
| 31 | : type_(type) |
| 32 | , externalPort_(portExternal) |
| 33 | , internalPort_(portInternal) |
| 34 | , internalAddr_() |
| 35 | , igd_() |
| 36 | , available_(available) |
| 37 | , state_(MappingState::PENDING) |
| 38 | , notifyCb_(nullptr) |
| 39 | , autoUpdate_(false) |
| 40 | #if HAVE_LIBNATPMP |
| 41 | , renewalTime_(sys_clock::now()) |
| 42 | #endif |
| 43 | {} |
| 44 | |
| 45 | Mapping::Mapping(const Mapping& other) |
| 46 | { |
| 47 | std::lock_guard<std::mutex> lock(other.mutex_); |
| 48 | |
| 49 | internalAddr_ = other.internalAddr_; |
| 50 | internalPort_ = other.internalPort_; |
| 51 | externalPort_ = other.externalPort_; |
| 52 | type_ = other.type_; |
| 53 | igd_ = other.igd_; |
| 54 | available_ = other.available_; |
| 55 | state_ = other.state_; |
| 56 | notifyCb_ = other.notifyCb_; |
| 57 | autoUpdate_ = other.autoUpdate_; |
| 58 | #if HAVE_LIBNATPMP |
| 59 | renewalTime_ = other.renewalTime_; |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Mapping::updateFrom(const Mapping::sharedPtr_t& other) |
| 65 | { |
| 66 | updateFrom(*other); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | Mapping::updateFrom(const Mapping& other) |
| 71 | { |
| 72 | if (type_ != other.type_) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 73 | // JAMI_ERR("The source and destination types must match"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | internalAddr_ = std::move(other.internalAddr_); |
| 78 | internalPort_ = other.internalPort_; |
| 79 | externalPort_ = other.externalPort_; |
| 80 | igd_ = other.igd_; |
| 81 | state_ = other.state_; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | Mapping::setAvailable(bool val) |
| 86 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 87 | // JAMI_DBG("Changing mapping %s state from %s to %s", |
| 88 | // toString().c_str(), |
| 89 | // available_ ? "AVAILABLE" : "UNAVAILABLE", |
| 90 | // val ? "AVAILABLE" : "UNAVAILABLE"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 91 | |
| 92 | std::lock_guard<std::mutex> lock(mutex_); |
| 93 | available_ = val; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | Mapping::setState(const MappingState& state) |
| 98 | { |
| 99 | std::lock_guard<std::mutex> lock(mutex_); |
| 100 | state_ = state; |
| 101 | } |
| 102 | |
| 103 | const char* |
| 104 | Mapping::getStateStr() const |
| 105 | { |
| 106 | std::lock_guard<std::mutex> lock(mutex_); |
| 107 | return getStateStr(state_); |
| 108 | } |
| 109 | |
| 110 | std::string |
| 111 | Mapping::toString(bool extraInfo) const |
| 112 | { |
| 113 | std::lock_guard<std::mutex> lock(mutex_); |
| 114 | std::ostringstream descr; |
| 115 | descr << UPNP_MAPPING_DESCRIPTION_PREFIX << "-" << getTypeStr(type_); |
| 116 | descr << ":" << std::to_string(internalPort_); |
| 117 | |
| 118 | if (extraInfo) { |
| 119 | descr << " (state=" << getStateStr(state_) |
| 120 | << ", auto-update=" << (autoUpdate_ ? "YES" : "NO") << ")"; |
| 121 | } |
| 122 | |
| 123 | return descr.str(); |
| 124 | } |
| 125 | |
| 126 | bool |
| 127 | Mapping::isValid() const |
| 128 | { |
| 129 | std::lock_guard<std::mutex> lock(mutex_); |
| 130 | if (state_ == MappingState::FAILED) |
| 131 | return false; |
| 132 | if (internalPort_ == 0) |
| 133 | return false; |
| 134 | if (externalPort_ == 0) |
| 135 | return false; |
| 136 | if (not igd_ or not igd_->isValid()) |
| 137 | return false; |
| 138 | IpAddr intAddr(internalAddr_); |
| 139 | return intAddr and not intAddr.isLoopback(); |
| 140 | } |
| 141 | |
| 142 | bool |
| 143 | Mapping::hasValidHostAddress() const |
| 144 | { |
| 145 | std::lock_guard<std::mutex> lock(mutex_); |
| 146 | |
| 147 | IpAddr intAddr(internalAddr_); |
| 148 | return intAddr and not intAddr.isLoopback(); |
| 149 | } |
| 150 | |
| 151 | bool |
| 152 | Mapping::hasPublicAddress() const |
| 153 | { |
| 154 | std::lock_guard<std::mutex> lock(mutex_); |
| 155 | |
| 156 | return igd_ and igd_->getPublicIp() and not igd_->getPublicIp().isPrivate(); |
| 157 | } |
| 158 | |
| 159 | Mapping::key_t |
| 160 | Mapping::getMapKey() const |
| 161 | { |
| 162 | std::lock_guard<std::mutex> lock(mutex_); |
| 163 | |
| 164 | key_t mapKey = internalPort_; |
| 165 | if (type_ == PortType::UDP) |
| 166 | mapKey |= 1 << (sizeof(uint16_t) * 8); |
| 167 | return mapKey; |
| 168 | } |
| 169 | |
| 170 | PortType |
| 171 | Mapping::getTypeFromMapKey(key_t key) |
| 172 | { |
| 173 | return (key >> (sizeof(uint16_t) * 8)) ? PortType::UDP : PortType::TCP; |
| 174 | } |
| 175 | |
| 176 | std::string |
| 177 | Mapping::getExternalAddress() const |
| 178 | { |
| 179 | std::lock_guard<std::mutex> lock(mutex_); |
| 180 | if (igd_) |
| 181 | return igd_->getPublicIp().toString(); |
| 182 | return {}; |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | Mapping::setExternalPort(uint16_t port) |
| 187 | { |
| 188 | std::lock_guard<std::mutex> lock(mutex_); |
| 189 | externalPort_ = port; |
| 190 | } |
| 191 | |
| 192 | uint16_t |
| 193 | Mapping::getExternalPort() const |
| 194 | { |
| 195 | std::lock_guard<std::mutex> lock(mutex_); |
| 196 | return externalPort_; |
| 197 | } |
| 198 | |
| 199 | std::string |
| 200 | Mapping::getExternalPortStr() const |
| 201 | { |
| 202 | std::lock_guard<std::mutex> lock(mutex_); |
| 203 | return std::to_string(externalPort_); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | Mapping::setInternalAddress(const std::string& addr) |
| 208 | { |
| 209 | std::lock_guard<std::mutex> lock(mutex_); |
| 210 | internalAddr_ = addr; |
| 211 | } |
| 212 | |
| 213 | std::string |
| 214 | Mapping::getInternalAddress() const |
| 215 | { |
| 216 | std::lock_guard<std::mutex> lock(mutex_); |
| 217 | return internalAddr_; |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | Mapping::setInternalPort(uint16_t port) |
| 222 | { |
| 223 | std::lock_guard<std::mutex> lock(mutex_); |
| 224 | internalPort_ = port; |
| 225 | } |
| 226 | |
| 227 | uint16_t |
| 228 | Mapping::getInternalPort() const |
| 229 | { |
| 230 | std::lock_guard<std::mutex> lock(mutex_); |
| 231 | return internalPort_; |
| 232 | } |
| 233 | |
| 234 | std::string |
| 235 | Mapping::getInternalPortStr() const |
| 236 | { |
| 237 | std::lock_guard<std::mutex> lock(mutex_); |
| 238 | return std::to_string(internalPort_); |
| 239 | } |
| 240 | |
| 241 | PortType |
| 242 | Mapping::getType() const |
| 243 | { |
| 244 | std::lock_guard<std::mutex> lock(mutex_); |
| 245 | return type_; |
| 246 | } |
| 247 | |
| 248 | const char* |
| 249 | Mapping::getTypeStr() const |
| 250 | { |
| 251 | std::lock_guard<std::mutex> lock(mutex_); |
| 252 | return getTypeStr(type_); |
| 253 | } |
| 254 | |
| 255 | bool |
| 256 | Mapping::isAvailable() const |
| 257 | { |
| 258 | std::lock_guard<std::mutex> lock(mutex_); |
| 259 | return available_; |
| 260 | } |
| 261 | |
| 262 | std::shared_ptr<IGD> |
| 263 | Mapping::getIgd() const |
| 264 | { |
| 265 | std::lock_guard<std::mutex> lock(mutex_); |
| 266 | return igd_; |
| 267 | } |
| 268 | |
| 269 | NatProtocolType |
| 270 | Mapping::getProtocol() const |
| 271 | { |
| 272 | std::lock_guard<std::mutex> lock(mutex_); |
| 273 | if (igd_) |
| 274 | return igd_->getProtocol(); |
| 275 | return NatProtocolType::UNKNOWN; |
| 276 | } |
| 277 | const char* |
| 278 | Mapping::getProtocolName() const |
| 279 | { |
| 280 | if (igd_) { |
| 281 | if (igd_->getProtocol() == NatProtocolType::NAT_PMP) |
| 282 | return "NAT-PMP"; |
| 283 | if (igd_->getProtocol() == NatProtocolType::PUPNP) |
| 284 | return "PUPNP"; |
| 285 | } |
| 286 | return "UNKNOWN"; |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | Mapping::setIgd(const std::shared_ptr<IGD>& igd) |
| 291 | { |
| 292 | std::lock_guard<std::mutex> lock(mutex_); |
| 293 | igd_ = igd; |
| 294 | } |
| 295 | |
| 296 | MappingState |
| 297 | Mapping::getState() const |
| 298 | { |
| 299 | std::lock_guard<std::mutex> lock(mutex_); |
| 300 | return state_; |
| 301 | } |
| 302 | |
| 303 | Mapping::NotifyCallback |
| 304 | Mapping::getNotifyCallback() const |
| 305 | { |
| 306 | std::lock_guard<std::mutex> lock(mutex_); |
| 307 | return notifyCb_; |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | Mapping::setNotifyCallback(NotifyCallback cb) |
| 312 | { |
| 313 | std::lock_guard<std::mutex> lock(mutex_); |
| 314 | notifyCb_ = std::move(cb); |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | Mapping::enableAutoUpdate(bool enable) |
| 319 | { |
| 320 | std::lock_guard<std::mutex> lock(mutex_); |
| 321 | autoUpdate_ = enable; |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | Mapping::getAutoUpdate() const |
| 326 | { |
| 327 | std::lock_guard<std::mutex> lock(mutex_); |
| 328 | return autoUpdate_; |
| 329 | } |
| 330 | |
| 331 | #if HAVE_LIBNATPMP |
| 332 | sys_clock::time_point |
| 333 | Mapping::getRenewalTime() const |
| 334 | { |
| 335 | std::lock_guard<std::mutex> lock(mutex_); |
| 336 | return renewalTime_; |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | Mapping::setRenewalTime(sys_clock::time_point time) |
| 341 | { |
| 342 | std::lock_guard<std::mutex> lock(mutex_); |
| 343 | renewalTime_ = time; |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | } // namespace upnp |
| 348 | } // namespace jami |