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 Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 26 | upnpContext_->dispatch([c=upnpContext_, this]{ |
| 27 | c->registerController(this); |
| 28 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | Controller::~Controller() |
| 32 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | releaseAllMappings(); |
Adrien Beraud | a8731ac | 2023-08-17 12:19:39 -0400 | [diff] [blame] | 34 | upnpContext_->dispatch([c=upnpContext_, this]{ |
| 35 | c->unregisterController(this); |
| 36 | }); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void |
| 40 | Controller::setPublicAddress(const IpAddr& addr) |
| 41 | { |
| 42 | assert(upnpContext_); |
| 43 | |
| 44 | if (addr and addr.getFamily() == AF_INET) { |
| 45 | upnpContext_->setPublicAddress(addr); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | bool |
| 50 | Controller::isReady() const |
| 51 | { |
| 52 | assert(upnpContext_); |
| 53 | return upnpContext_->isReady(); |
| 54 | } |
| 55 | |
| 56 | IpAddr |
| 57 | Controller::getExternalIP() const |
| 58 | { |
| 59 | assert(upnpContext_); |
| 60 | if (upnpContext_->isReady()) { |
| 61 | return upnpContext_->getExternalIP(); |
| 62 | } |
| 63 | return {}; |
| 64 | } |
| 65 | |
| 66 | Mapping::sharedPtr_t |
| 67 | Controller::reserveMapping(uint16_t port, PortType type) |
| 68 | { |
| 69 | Mapping map(type, port, port); |
| 70 | return reserveMapping(map); |
| 71 | } |
| 72 | |
| 73 | Mapping::sharedPtr_t |
| 74 | Controller::reserveMapping(Mapping& requestedMap) |
| 75 | { |
| 76 | assert(upnpContext_); |
| 77 | |
| 78 | // Try to get a provisioned port |
| 79 | auto mapRes = upnpContext_->reserveMapping(requestedMap); |
| 80 | if (mapRes) |
| 81 | addLocalMap(*mapRes); |
| 82 | return mapRes; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | Controller::releaseMapping(const Mapping& map) |
| 87 | { |
| 88 | assert(upnpContext_); |
| 89 | |
| 90 | removeLocalMap(map); |
| 91 | return upnpContext_->releaseMapping(map); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | Controller::releaseAllMappings() |
| 96 | { |
| 97 | assert(upnpContext_); |
| 98 | |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 99 | std::lock_guard lk(mapListMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 100 | for (auto const& [_, map] : mappingList_) { |
| 101 | upnpContext_->releaseMapping(map); |
| 102 | } |
| 103 | mappingList_.clear(); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | Controller::addLocalMap(const Mapping& map) |
| 108 | { |
| 109 | if (map.getMapKey()) { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 110 | std::lock_guard lock(mapListMutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 111 | auto ret = mappingList_.emplace(map.getMapKey(), map); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | Controller::removeLocalMap(const Mapping& map) |
| 117 | { |
| 118 | assert(upnpContext_); |
| 119 | |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 120 | std::lock_guard lk(mapListMutex_); |
Sébastien Blin | 91cda3c | 2024-01-10 16:21:18 -0500 | [diff] [blame] | 121 | return mappingList_.erase(map.getMapKey()) == 1; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | uint16_t |
| 125 | Controller::generateRandomPort(PortType type) |
| 126 | { |
| 127 | return UPnPContext::generateRandomPort(type); |
| 128 | } |
| 129 | |
| 130 | } // namespace upnp |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 131 | } // namespace dhtnet |