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