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