blob: 8e8e5a92ae238a6d5bf5119a55bac0efae95e81c [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 */
17#pragma once
18
19#include <mutex>
Morteza Namvar5f639522023-07-04 17:08:58 -040020#include <atomic>
Adrien Béraud612b55b2023-05-29 10:42:04 -040021
22#include "ip_utils.h"
Morteza Namvar5f639522023-07-04 17:08:58 -040023#include "upnp/mapping.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040024
25#ifdef _MSC_VER
26typedef uint16_t in_port_t;
27#endif
28
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040029namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040030namespace upnp {
31
32enum class NatProtocolType { UNKNOWN, PUPNP, NAT_PMP };
33
34class IGD
35{
36public:
37 // Max error before moving the IGD to invalid state.
38 constexpr static int MAX_ERRORS_COUNT = 10;
39
40 IGD(NatProtocolType prot);
41 virtual ~IGD() = default;
42 bool operator==(IGD& other) const;
43
44 NatProtocolType getProtocol() const { return protocol_; }
45
46 char const* getProtocolName() const
47 {
48 return protocol_ == NatProtocolType::NAT_PMP ? "NAT-PMP" : "UPNP";
49 };
50
51 IpAddr getLocalIp() const
52 {
53 std::lock_guard<std::mutex> lock(mutex_);
54 return localIp_;
55 }
56 IpAddr getPublicIp() const
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 return publicIp_;
60 }
61 void setLocalIp(const IpAddr& addr)
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 localIp_ = addr;
65 }
66 void setPublicIp(const IpAddr& addr)
67 {
68 std::lock_guard<std::mutex> lock(mutex_);
69 publicIp_ = addr;
70 }
71 void setUID(const std::string& uid)
72 {
73 std::lock_guard<std::mutex> lock(mutex_);
74 uid_ = uid;
75 }
76 std::string getUID() const
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 return uid_;
80 }
81
82 void setValid(bool valid);
83 bool isValid() const { return valid_; }
84 bool incrementErrorsCounter();
85 int getErrorsCount() const;
86
87 virtual const std::string toString() const = 0;
88
89protected:
90 const NatProtocolType protocol_ {NatProtocolType::UNKNOWN};
91 std::atomic_bool valid_ {false};
92 std::atomic<int> errorsCounter_ {0};
93
94 mutable std::mutex mutex_;
95 IpAddr localIp_ {}; // Local IP of the IGD (typically the same as the gateway).
96 IpAddr publicIp_ {}; // External/public IP of IGD.
97 std::string uid_ {};
98
99private:
100 IGD(IGD&& other) = delete;
101 IGD(IGD& other) = delete;
102 IGD& operator=(IGD&& other) = delete;
103 IGD& operator=(IGD& other) = delete;
104};
105
106} // namespace upnp
Sébastien Blin464bdff2023-07-19 08:02:53 -0400107} // namespace dhtnet