blob: a971e7f3d6722e1f6957c903094aa585a80626e8 [file] [log] [blame]
Adrien Bérauda1d294f2023-07-17 22:42:13 -04001/*
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éraud2a4e73d2023-08-27 12:53:55 -040031#include <filesystem>
Adrien Bérauda1d294f2023-07-17 22:42:13 -040032
33namespace dht {
34namespace log {
Adrien Béraud9132a812023-07-21 11:20:40 -040035struct Logger;
Adrien Bérauda1d294f2023-07-17 22:42:13 -040036}
37}
38
39namespace dhtnet {
40
41using Logger = dht::log::Logger;
42
43class TurnTransport;
44
45class TurnCache : public std::enable_shared_from_this<TurnCache>
46{
47public:
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
67private:
68 std::string accountId_;
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040069 std::filesystem::path cachePath_;
Adrien Bérauda1d294f2023-07-17 22:42:13 -040070 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 Blin464bdff2023-07-19 08:02:53 -0400112} // namespace dhtnet