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 | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 19 | #include "./igd.h" |
| 20 | #include "upnp/upnp_context.h" |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 21 | #include "upnp/mapping.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 22 | #include "ip_utils.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 23 | |
| 24 | #include <map> |
| 25 | #include <string> |
| 26 | #include <chrono> |
| 27 | #include <functional> |
| 28 | #include <condition_variable> |
| 29 | #include <list> |
| 30 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 31 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 32 | namespace upnp { |
| 33 | |
| 34 | // UPnP device descriptions. |
| 35 | constexpr static const char* UPNP_ROOT_DEVICE = "upnp:rootdevice"; |
| 36 | constexpr static const char* UPNP_IGD_DEVICE |
| 37 | = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"; |
| 38 | constexpr static const char* UPNP_WAN_DEVICE = "urn:schemas-upnp-org:device:WANDevice:1"; |
| 39 | constexpr static const char* UPNP_WANCON_DEVICE |
| 40 | = "urn:schemas-upnp-org:device:WANConnectionDevice:1"; |
| 41 | constexpr static const char* UPNP_WANIP_SERVICE = "urn:schemas-upnp-org:service:WANIPConnection:1"; |
| 42 | constexpr static const char* UPNP_WANPPP_SERVICE |
| 43 | = "urn:schemas-upnp-org:service:WANPPPConnection:1"; |
| 44 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 45 | |
| 46 | // Pure virtual interface class that UPnPContext uses to call protocol functions. |
| 47 | class UPnPProtocol : public std::enable_shared_from_this<UPnPProtocol>//, protected UpnpThreadUtil |
| 48 | { |
| 49 | public: |
| 50 | enum class UpnpError : int { INVALID_ERR = -1, ERROR_OK, CONFLICT_IN_MAPPING }; |
| 51 | |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 52 | UPnPProtocol(const std::shared_ptr<dht::log::Logger>& logger) : logger_(logger) {}; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 53 | virtual ~UPnPProtocol() {}; |
| 54 | |
| 55 | // Get protocol type. |
| 56 | virtual NatProtocolType getProtocol() const = 0; |
| 57 | |
| 58 | // Get protocol type as string. |
| 59 | virtual char const* getProtocolName() const = 0; |
| 60 | |
| 61 | // Clear all known IGDs. |
| 62 | virtual void clearIgds() = 0; |
| 63 | |
| 64 | // Search for IGD. |
| 65 | virtual void searchForIgd() = 0; |
| 66 | |
| 67 | // Get the IGD instance. |
| 68 | virtual std::list<std::shared_ptr<IGD>> getIgdList() const = 0; |
| 69 | |
| 70 | // Return true if it has at least one valid IGD. |
| 71 | virtual bool isReady() const = 0; |
| 72 | |
| 73 | // Get the list of already allocated mappings if any. |
| 74 | virtual std::map<Mapping::key_t, Mapping> getMappingsListByDescr(const std::shared_ptr<IGD>&, |
| 75 | const std::string&) const |
| 76 | { |
| 77 | return {}; |
| 78 | } |
| 79 | |
François-Simon Fauteux-Chapleau | 826f0ba | 2024-05-29 15:22:21 -0400 | [diff] [blame] | 80 | // Get information about all existing port mappings on the given IGD |
| 81 | virtual std::vector<MappingInfo> getMappingsInfo(const std::shared_ptr<IGD>& igd) const |
| 82 | { |
| 83 | return {}; |
| 84 | } |
| 85 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 86 | // Sends a request to add a mapping. |
| 87 | virtual void requestMappingAdd(const Mapping& map) = 0; |
| 88 | |
| 89 | // Renew an allocated mapping. |
| 90 | virtual void requestMappingRenew(const Mapping& mapping) = 0; |
| 91 | |
| 92 | // Sends a request to remove a mapping. |
| 93 | virtual void requestMappingRemove(const Mapping& igdMapping) = 0; |
| 94 | |
| 95 | // Set the user callbacks. |
| 96 | virtual void setObserver(UpnpMappingObserver* obs) = 0; |
| 97 | |
| 98 | // Get the current host (local) address |
| 99 | virtual const IpAddr getHostAddress() const = 0; |
| 100 | |
| 101 | // Terminate |
| 102 | virtual void terminate() = 0; |
Adrien Béraud | 370257c | 2023-08-15 20:53:09 -0400 | [diff] [blame] | 103 | |
| 104 | std::shared_ptr<dht::log::Logger> logger_; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace upnp |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 108 | } // namespace dhtnet |