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