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 | #pragma once |
| 18 | |
Adrien Béraud | 33a03ba | 2023-06-21 11:35:54 -0400 | [diff] [blame] | 19 | #include "mapping.h" |
Adrien Béraud | dbb0686 | 2023-07-08 09:18:39 -0400 | [diff] [blame] | 20 | #include "../ip_utils.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
| 23 | #include <chrono> |
| 24 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 25 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 26 | class IpAddr; |
| 27 | } |
| 28 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 29 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 30 | namespace upnp { |
| 31 | |
| 32 | class UPnPContext; |
| 33 | |
Adrien Beraud | 753c819 | 2023-08-17 17:21:24 -0400 | [diff] [blame] | 34 | class Controller : std::enable_shared_from_this<Controller> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 35 | { |
| 36 | public: |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 37 | Controller(const std::shared_ptr<UPnPContext>& ctx); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 38 | ~Controller(); |
| 39 | |
| 40 | // Set known public address |
| 41 | void setPublicAddress(const IpAddr& addr); |
| 42 | // Checks if a valid IGD is available. |
| 43 | bool isReady() const; |
| 44 | // Gets the external ip of the first valid IGD in the list. |
| 45 | IpAddr getExternalIP() const; |
| 46 | |
| 47 | // Request port mapping. |
| 48 | // Returns a shared pointer on the allocated mapping. The shared |
| 49 | // pointer may point to nothing on failure. |
| 50 | Mapping::sharedPtr_t reserveMapping(Mapping& map); |
| 51 | Mapping::sharedPtr_t reserveMapping(uint16_t port, PortType type); |
| 52 | |
| 53 | // Remove port mapping. |
| 54 | void releaseMapping(const Mapping& map); |
| 55 | static uint16_t generateRandomPort(PortType); |
| 56 | |
Adrien Beraud | c161aaa | 2023-08-17 17:27:13 -0400 | [diff] [blame] | 57 | std::shared_ptr<UPnPContext> upnpContext() const { return upnpContext_; } |
| 58 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 59 | private: |
| 60 | // Adds a mapping locally to the list. |
| 61 | void addLocalMap(const Mapping& map); |
| 62 | // Removes a mapping from the local list. |
| 63 | bool removeLocalMap(const Mapping& map); |
| 64 | // Removes all mappings of the given type. |
| 65 | void releaseAllMappings(); |
| 66 | |
| 67 | std::shared_ptr<UPnPContext> upnpContext_; |
| 68 | |
| 69 | mutable std::mutex mapListMutex_; |
| 70 | std::map<Mapping::key_t, Mapping> mappingList_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace upnp |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 74 | } // namespace dhtnet |