blob: deeb9197a8da7aba92f75b29a2b08667d7b420b7 [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>
31
32namespace dht {
33namespace log {
Adrien Béraud9132a812023-07-21 11:20:40 -040034struct Logger;
Adrien Bérauda1d294f2023-07-17 22:42:13 -040035}
36}
37
38namespace dhtnet {
39
40using Logger = dht::log::Logger;
41
42class TurnTransport;
43
44class TurnCache : public std::enable_shared_from_this<TurnCache>
45{
46public:
47 TurnCache(const std::string& accountId,
48 const std::string& cachePath,
49 const std::shared_ptr<asio::io_context>& io_context,
50 const std::shared_ptr<Logger>& logger,
51 const TurnTransportParams& params,
52 bool enabled);
53 ~TurnCache();
54
55 std::optional<IpAddr> getResolvedTurn(uint16_t family = AF_INET) const;
56 /**
57 * Pass a new configuration for the cache
58 * @param param The new configuration
59 */
60 void reconfigure(const TurnTransportParams& params, bool enabled);
61 /**
62 * Refresh cache from current configuration
63 */
64 void refresh(const asio::error_code& ec = {});
65
66private:
67 std::string accountId_;
68 std::string cachePath_;
69 TurnTransportParams params_;
70 std::atomic_bool enabled_ {false};
71 /**
72 * Avoid to refresh the cache multiple times
73 */
74 std::atomic_bool isRefreshing_ {false};
75 /**
76 * This will cache the turn server resolution each time we launch
77 * Jami, or for each connectivityChange()
78 */
79 void testTurn(IpAddr server);
80 std::unique_ptr<TurnTransport> testTurnV4_;
81 std::unique_ptr<TurnTransport> testTurnV6_;
82
83 // Used to detect if a turn server is down.
84 void refreshTurnDelay(bool scheduleNext);
85 std::chrono::seconds turnRefreshDelay_ {std::chrono::seconds(10)};
86
87 // Store resoved turn addresses
88 mutable std::mutex cachedTurnMutex_ {};
89 std::unique_ptr<IpAddr> cacheTurnV4_ {};
90 std::unique_ptr<IpAddr> cacheTurnV6_ {};
91
92 void onConnected(const asio::error_code& ec, bool ok, IpAddr server);
93
94 // io
95 std::shared_ptr<asio::io_context> io_context;
96 std::unique_ptr<asio::steady_timer> refreshTimer_;
97 std::unique_ptr<asio::steady_timer> onConnectedTimer_;
98
99 std::mutex shutdownMtx_;
100
101 std::shared_ptr<Logger> logger_;
102
103 // Asio :(
104 // https://stackoverflow.com/questions/35507956/is-it-safe-to-destroy-boostasio-timer-from-its-handler-or-handler-dtor
105 std::weak_ptr<TurnCache> weak()
106 {
107 return std::static_pointer_cast<TurnCache>(shared_from_this());
108 }
109};
110
Sébastien Blin464bdff2023-07-19 08:02:53 -0400111} // namespace dhtnet