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_control.h" |
| 18 | #include "upnp/upnp_context.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 19 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 20 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 21 | namespace upnp { |
| 22 | |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 23 | Controller::Controller(const std::shared_ptr<UPnPContext>& ctx) |
| 24 | : upnpContext_(ctx) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 25 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 26 | upnpContext_->registerController(this); |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 27 | // JAMI_DBG("Controller@%p: Created UPnP Controller session", this); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | Controller::~Controller() |
| 31 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 32 | // JAMI_DBG("Controller@%p: Destroying UPnP Controller session", this); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | |
| 34 | releaseAllMappings(); |
| 35 | upnpContext_->unregisterController(this); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | Controller::setPublicAddress(const IpAddr& addr) |
| 40 | { |
| 41 | assert(upnpContext_); |
| 42 | |
| 43 | if (addr and addr.getFamily() == AF_INET) { |
| 44 | upnpContext_->setPublicAddress(addr); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | bool |
| 49 | Controller::isReady() const |
| 50 | { |
| 51 | assert(upnpContext_); |
| 52 | return upnpContext_->isReady(); |
| 53 | } |
| 54 | |
| 55 | IpAddr |
| 56 | Controller::getExternalIP() const |
| 57 | { |
| 58 | assert(upnpContext_); |
| 59 | if (upnpContext_->isReady()) { |
| 60 | return upnpContext_->getExternalIP(); |
| 61 | } |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | Mapping::sharedPtr_t |
| 66 | Controller::reserveMapping(uint16_t port, PortType type) |
| 67 | { |
| 68 | Mapping map(type, port, port); |
| 69 | return reserveMapping(map); |
| 70 | } |
| 71 | |
| 72 | Mapping::sharedPtr_t |
| 73 | Controller::reserveMapping(Mapping& requestedMap) |
| 74 | { |
| 75 | assert(upnpContext_); |
| 76 | |
| 77 | // Try to get a provisioned port |
| 78 | auto mapRes = upnpContext_->reserveMapping(requestedMap); |
| 79 | if (mapRes) |
| 80 | addLocalMap(*mapRes); |
| 81 | return mapRes; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | Controller::releaseMapping(const Mapping& map) |
| 86 | { |
| 87 | assert(upnpContext_); |
| 88 | |
| 89 | removeLocalMap(map); |
| 90 | return upnpContext_->releaseMapping(map); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | Controller::releaseAllMappings() |
| 95 | { |
| 96 | assert(upnpContext_); |
| 97 | |
| 98 | std::lock_guard<std::mutex> lk(mapListMutex_); |
| 99 | for (auto const& [_, map] : mappingList_) { |
| 100 | upnpContext_->releaseMapping(map); |
| 101 | } |
| 102 | mappingList_.clear(); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Controller::addLocalMap(const Mapping& map) |
| 107 | { |
| 108 | if (map.getMapKey()) { |
| 109 | std::lock_guard<std::mutex> lock(mapListMutex_); |
| 110 | auto ret = mappingList_.emplace(map.getMapKey(), map); |
| 111 | if (not ret.second) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 112 | // JAMI_WARN("Mapping request for %s already in the list!", map.toString().c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | bool |
| 118 | Controller::removeLocalMap(const Mapping& map) |
| 119 | { |
| 120 | assert(upnpContext_); |
| 121 | |
| 122 | std::lock_guard<std::mutex> lk(mapListMutex_); |
| 123 | if (mappingList_.erase(map.getMapKey()) != 1) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 124 | // JAMI_ERR("Failed to remove mapping %s from local list", map.getTypeStr()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | uint16_t |
| 132 | Controller::generateRandomPort(PortType type) |
| 133 | { |
| 134 | return UPnPContext::generateRandomPort(type); |
| 135 | } |
| 136 | |
| 137 | } // namespace upnp |
| 138 | } // namespace jami |