blob: 3dde4abd13406627215e6ea76db269a9b751f5cb [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#pragma once
18
Adrien Béraud370257c2023-08-15 20:53:09 -040019#include "./igd.h"
20#include "upnp/upnp_context.h"
Adrien Béraud25c30c42023-07-05 13:46:54 -040021#include "upnp/mapping.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040022#include "ip_utils.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040023
24#include <map>
25#include <string>
26#include <chrono>
27#include <functional>
28#include <condition_variable>
29#include <list>
30
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040031namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040032namespace upnp {
33
34// UPnP device descriptions.
35constexpr static const char* UPNP_ROOT_DEVICE = "upnp:rootdevice";
36constexpr static const char* UPNP_IGD_DEVICE
37 = "urn:schemas-upnp-org:device:InternetGatewayDevice:1";
38constexpr static const char* UPNP_WAN_DEVICE = "urn:schemas-upnp-org:device:WANDevice:1";
39constexpr static const char* UPNP_WANCON_DEVICE
40 = "urn:schemas-upnp-org:device:WANConnectionDevice:1";
41constexpr static const char* UPNP_WANIP_SERVICE = "urn:schemas-upnp-org:service:WANIPConnection:1";
42constexpr static const char* UPNP_WANPPP_SERVICE
43 = "urn:schemas-upnp-org:service:WANPPPConnection:1";
44
Adrien Béraud612b55b2023-05-29 10:42:04 -040045
46// Pure virtual interface class that UPnPContext uses to call protocol functions.
47class UPnPProtocol : public std::enable_shared_from_this<UPnPProtocol>//, protected UpnpThreadUtil
48{
49public:
50 enum class UpnpError : int { INVALID_ERR = -1, ERROR_OK, CONFLICT_IN_MAPPING };
51
Adrien Béraud370257c2023-08-15 20:53:09 -040052 UPnPProtocol(const std::shared_ptr<dht::log::Logger>& logger) : logger_(logger) {};
Adrien Béraud612b55b2023-05-29 10:42:04 -040053 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
80 // Sends a request to add a mapping.
81 virtual void requestMappingAdd(const Mapping& map) = 0;
82
83 // Renew an allocated mapping.
84 virtual void requestMappingRenew(const Mapping& mapping) = 0;
85
86 // Sends a request to remove a mapping.
87 virtual void requestMappingRemove(const Mapping& igdMapping) = 0;
88
89 // Set the user callbacks.
90 virtual void setObserver(UpnpMappingObserver* obs) = 0;
91
92 // Get the current host (local) address
93 virtual const IpAddr getHostAddress() const = 0;
94
95 // Terminate
96 virtual void terminate() = 0;
Adrien Béraud370257c2023-08-15 20:53:09 -040097
98 std::shared_ptr<dht::log::Logger> logger_;
Adrien Béraud612b55b2023-05-29 10:42:04 -040099};
100
101} // namespace upnp
Sébastien Blin464bdff2023-07-19 08:02:53 -0400102} // namespace dhtnet