Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (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éraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
| 17 | #pragma once |
| 18 | |
| 19 | #include <mutex> |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 20 | #include <atomic> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 21 | |
| 22 | #include "ip_utils.h" |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 23 | #include "upnp/mapping.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 24 | |
| 25 | #ifdef _MSC_VER |
| 26 | typedef uint16_t in_port_t; |
| 27 | #endif |
| 28 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 29 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 30 | namespace upnp { |
| 31 | |
| 32 | enum class NatProtocolType { UNKNOWN, PUPNP, NAT_PMP }; |
| 33 | |
| 34 | class IGD |
| 35 | { |
| 36 | public: |
| 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 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 53 | std::lock_guard lock(mutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 54 | return localIp_; |
| 55 | } |
| 56 | IpAddr getPublicIp() const |
| 57 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 58 | std::lock_guard lock(mutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 59 | return publicIp_; |
| 60 | } |
| 61 | void setLocalIp(const IpAddr& addr) |
| 62 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 63 | std::lock_guard lock(mutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 64 | localIp_ = addr; |
| 65 | } |
| 66 | void setPublicIp(const IpAddr& addr) |
| 67 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 68 | std::lock_guard lock(mutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 69 | publicIp_ = addr; |
| 70 | } |
| 71 | void setUID(const std::string& uid) |
| 72 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 73 | std::lock_guard lock(mutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 74 | uid_ = uid; |
| 75 | } |
| 76 | std::string getUID() const |
| 77 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 78 | std::lock_guard lock(mutex_); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 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 | |
| 89 | protected: |
| 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 | |
| 99 | private: |
| 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 Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 107 | } // namespace dhtnet |