Adrien BĂ©raud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Eden Abitbol <eden.abitbol@savoirfairelinux.com> |
| 5 | * Author: Mohamed Chibani <mohamed.chibani@savoirfairelinux.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #pragma once |
| 23 | |
| 24 | #include "igd.h" |
| 25 | #include "mapping.h" |
| 26 | #include "ip_utils.h" |
| 27 | //#include "upnp/upnp_thread_util.h" |
| 28 | |
| 29 | #include <map> |
| 30 | #include <string> |
| 31 | #include <chrono> |
| 32 | #include <functional> |
| 33 | #include <condition_variable> |
| 34 | #include <list> |
| 35 | |
| 36 | namespace jami { |
| 37 | namespace upnp { |
| 38 | |
| 39 | // UPnP device descriptions. |
| 40 | constexpr static const char* UPNP_ROOT_DEVICE = "upnp:rootdevice"; |
| 41 | constexpr static const char* UPNP_IGD_DEVICE |
| 42 | = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"; |
| 43 | constexpr static const char* UPNP_WAN_DEVICE = "urn:schemas-upnp-org:device:WANDevice:1"; |
| 44 | constexpr static const char* UPNP_WANCON_DEVICE |
| 45 | = "urn:schemas-upnp-org:device:WANConnectionDevice:1"; |
| 46 | constexpr static const char* UPNP_WANIP_SERVICE = "urn:schemas-upnp-org:service:WANIPConnection:1"; |
| 47 | constexpr static const char* UPNP_WANPPP_SERVICE |
| 48 | = "urn:schemas-upnp-org:service:WANPPPConnection:1"; |
| 49 | |
| 50 | enum class UpnpIgdEvent { ADDED, REMOVED, INVALID_STATE }; |
| 51 | |
| 52 | // Interface used to report mapping event from the protocol implementations. |
| 53 | // This interface is meant to be implemented only by UPnPConext class. Sincce |
| 54 | // this class is a singleton, it's assumed that it out-lives the protocol |
| 55 | // implementations. In other words, the observer is always assumed to point to a |
| 56 | // valid instance. |
| 57 | class UpnpMappingObserver |
| 58 | { |
| 59 | public: |
| 60 | UpnpMappingObserver() {}; |
| 61 | virtual ~UpnpMappingObserver() {}; |
| 62 | |
| 63 | virtual void onIgdUpdated(const std::shared_ptr<IGD>& igd, UpnpIgdEvent event) = 0; |
| 64 | virtual void onMappingAdded(const std::shared_ptr<IGD>& igd, const Mapping& map) = 0; |
| 65 | virtual void onMappingRequestFailed(const Mapping& map) = 0; |
| 66 | #if HAVE_LIBNATPMP |
| 67 | virtual void onMappingRenewed(const std::shared_ptr<IGD>& igd, const Mapping& map) = 0; |
| 68 | #endif |
| 69 | virtual void onMappingRemoved(const std::shared_ptr<IGD>& igd, const Mapping& map) = 0; |
| 70 | }; |
| 71 | |
| 72 | // Pure virtual interface class that UPnPContext uses to call protocol functions. |
| 73 | class UPnPProtocol : public std::enable_shared_from_this<UPnPProtocol>//, protected UpnpThreadUtil |
| 74 | { |
| 75 | public: |
| 76 | enum class UpnpError : int { INVALID_ERR = -1, ERROR_OK, CONFLICT_IN_MAPPING }; |
| 77 | |
| 78 | UPnPProtocol() {}; |
| 79 | virtual ~UPnPProtocol() {}; |
| 80 | |
| 81 | // Get protocol type. |
| 82 | virtual NatProtocolType getProtocol() const = 0; |
| 83 | |
| 84 | // Get protocol type as string. |
| 85 | virtual char const* getProtocolName() const = 0; |
| 86 | |
| 87 | // Clear all known IGDs. |
| 88 | virtual void clearIgds() = 0; |
| 89 | |
| 90 | // Search for IGD. |
| 91 | virtual void searchForIgd() = 0; |
| 92 | |
| 93 | // Get the IGD instance. |
| 94 | virtual std::list<std::shared_ptr<IGD>> getIgdList() const = 0; |
| 95 | |
| 96 | // Return true if it has at least one valid IGD. |
| 97 | virtual bool isReady() const = 0; |
| 98 | |
| 99 | // Get the list of already allocated mappings if any. |
| 100 | virtual std::map<Mapping::key_t, Mapping> getMappingsListByDescr(const std::shared_ptr<IGD>&, |
| 101 | const std::string&) const |
| 102 | { |
| 103 | return {}; |
| 104 | } |
| 105 | |
| 106 | // Sends a request to add a mapping. |
| 107 | virtual void requestMappingAdd(const Mapping& map) = 0; |
| 108 | |
| 109 | // Renew an allocated mapping. |
| 110 | virtual void requestMappingRenew(const Mapping& mapping) = 0; |
| 111 | |
| 112 | // Sends a request to remove a mapping. |
| 113 | virtual void requestMappingRemove(const Mapping& igdMapping) = 0; |
| 114 | |
| 115 | // Set the user callbacks. |
| 116 | virtual void setObserver(UpnpMappingObserver* obs) = 0; |
| 117 | |
| 118 | // Get the current host (local) address |
| 119 | virtual const IpAddr getHostAddress() const = 0; |
| 120 | |
| 121 | // Terminate |
| 122 | virtual void terminate() = 0; |
| 123 | }; |
| 124 | |
| 125 | } // namespace upnp |
| 126 | } // namespace jami |