Adrien Béraud | a1d294f | 2023-07-17 22:42:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 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 |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #pragma once |
| 18 | |
| 19 | #include "ip_utils.h" |
| 20 | #include "turn_params.h" |
| 21 | |
| 22 | #include <asio.hpp> |
| 23 | |
| 24 | #include <atomic> |
| 25 | #include <chrono> |
| 26 | #include <functional> |
| 27 | #include <memory> |
| 28 | #include <mutex> |
| 29 | #include <optional> |
| 30 | #include <string> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 31 | #include <filesystem> |
Adrien Béraud | a1d294f | 2023-07-17 22:42:13 -0400 | [diff] [blame] | 32 | |
| 33 | namespace dht { |
| 34 | namespace log { |
Adrien Béraud | 9132a81 | 2023-07-21 11:20:40 -0400 | [diff] [blame] | 35 | struct Logger; |
Adrien Béraud | a1d294f | 2023-07-17 22:42:13 -0400 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | namespace dhtnet { |
| 40 | |
| 41 | using Logger = dht::log::Logger; |
| 42 | |
| 43 | class TurnTransport; |
| 44 | |
| 45 | class TurnCache : public std::enable_shared_from_this<TurnCache> |
| 46 | { |
| 47 | public: |
| 48 | TurnCache(const std::string& accountId, |
| 49 | const std::string& cachePath, |
| 50 | const std::shared_ptr<asio::io_context>& io_context, |
| 51 | const std::shared_ptr<Logger>& logger, |
| 52 | const TurnTransportParams& params, |
| 53 | bool enabled); |
| 54 | ~TurnCache(); |
| 55 | |
| 56 | std::optional<IpAddr> getResolvedTurn(uint16_t family = AF_INET) const; |
| 57 | /** |
| 58 | * Pass a new configuration for the cache |
| 59 | * @param param The new configuration |
| 60 | */ |
| 61 | void reconfigure(const TurnTransportParams& params, bool enabled); |
| 62 | /** |
| 63 | * Refresh cache from current configuration |
| 64 | */ |
| 65 | void refresh(const asio::error_code& ec = {}); |
| 66 | |
| 67 | private: |
| 68 | std::string accountId_; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 69 | std::filesystem::path cachePath_; |
Adrien Béraud | a1d294f | 2023-07-17 22:42:13 -0400 | [diff] [blame] | 70 | TurnTransportParams params_; |
| 71 | std::atomic_bool enabled_ {false}; |
| 72 | /** |
| 73 | * Avoid to refresh the cache multiple times |
| 74 | */ |
| 75 | std::atomic_bool isRefreshing_ {false}; |
| 76 | /** |
| 77 | * This will cache the turn server resolution each time we launch |
| 78 | * Jami, or for each connectivityChange() |
| 79 | */ |
| 80 | void testTurn(IpAddr server); |
| 81 | std::unique_ptr<TurnTransport> testTurnV4_; |
| 82 | std::unique_ptr<TurnTransport> testTurnV6_; |
| 83 | |
| 84 | // Used to detect if a turn server is down. |
| 85 | void refreshTurnDelay(bool scheduleNext); |
| 86 | std::chrono::seconds turnRefreshDelay_ {std::chrono::seconds(10)}; |
| 87 | |
| 88 | // Store resoved turn addresses |
| 89 | mutable std::mutex cachedTurnMutex_ {}; |
| 90 | std::unique_ptr<IpAddr> cacheTurnV4_ {}; |
| 91 | std::unique_ptr<IpAddr> cacheTurnV6_ {}; |
| 92 | |
| 93 | void onConnected(const asio::error_code& ec, bool ok, IpAddr server); |
| 94 | |
| 95 | // io |
| 96 | std::shared_ptr<asio::io_context> io_context; |
| 97 | std::unique_ptr<asio::steady_timer> refreshTimer_; |
| 98 | std::unique_ptr<asio::steady_timer> onConnectedTimer_; |
| 99 | |
| 100 | std::mutex shutdownMtx_; |
| 101 | |
| 102 | std::shared_ptr<Logger> logger_; |
| 103 | |
| 104 | // Asio :( |
| 105 | // https://stackoverflow.com/questions/35507956/is-it-safe-to-destroy-boostasio-timer-from-its-handler-or-handler-dtor |
| 106 | std::weak_ptr<TurnCache> weak() |
| 107 | { |
| 108 | return std::static_pointer_cast<TurnCache>(shared_from_this()); |
| 109 | } |
| 110 | }; |
| 111 | |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 112 | } // namespace dhtnet |