blob: e459eb3f873b3a91b46b314a3a2b3be4fa3e7269 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * 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 "ice_options.h"
20#include "multiplexed_socket.h"
Sébastien Blin34086512023-07-25 09:52:14 -040021#include "ice_transport_factory.h"
Sébastien Blin84bf4182023-07-21 14:18:39 -040022#include "turn_cache.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040023
24#include <opendht/dhtrunner.h>
25#include <opendht/infohash.h>
26#include <opendht/value.h>
27#include <opendht/default_types.h>
28#include <opendht/sockaddr.h>
29#include <opendht/logger.h>
30
31#include <memory>
32#include <vector>
33#include <string>
34
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040035namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040036
37class ChannelSocket;
38class ConnectionManager;
39namespace upnp {
40class Controller;
41}
42namespace tls {
43class CertificateStore;
44}
45
46/**
47 * A PeerConnectionRequest is a request which ask for an initial connection
48 * It contains the ICE request an ID and if it's an answer
49 * Transmitted via the UDP DHT
50 */
51struct PeerConnectionRequest : public dht::EncryptedValue<PeerConnectionRequest>
52{
53 static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
54 static constexpr const char* key_prefix = "peer:"; ///< base to compute the DHT listen key
55 dht::Value::Id id = dht::Value::INVALID_ID;
56 std::string ice_msg {};
57 bool isAnswer {false};
58 std::string connType {}; // Used for push notifications to know why we open a new connection
59 MSGPACK_DEFINE_MAP(id, ice_msg, isAnswer, connType)
60};
61
62/**
63 * Used to accept or not an incoming ICE connection (default accept)
64 */
65using onICERequestCallback = std::function<bool(const DeviceId&)>;
66/**
67 * Used to accept or decline an incoming channel request
68 */
69using ChannelRequestCallback = std::function<bool(const std::shared_ptr<dht::crypto::Certificate>&,
70 const std::string& /* name */)>;
71/**
72 * Used by connectDevice, when the socket is ready
73 */
74using ConnectCallback = std::function<void(const std::shared_ptr<ChannelSocket>&, const DeviceId&)>;
75/**
76 * Used when an incoming connection is ready
77 */
78using ConnectionReadyCallback = std::function<
79 void(const DeviceId&, const std::string& /* channel_name */, std::shared_ptr<ChannelSocket>)>;
80
81using iOSConnectedCallback
82 = std::function<bool(const std::string& /* connType */, dht::InfoHash /* peer_h */)>;
83
84/**
85 * Manages connections to other devices
86 * @note the account MUST be valid if ConnectionManager lives
87 */
88class ConnectionManager
89{
90public:
Adrien Béraudc8cd2c72023-07-21 13:15:58 -040091 struct Config;
Adrien Béraud612b55b2023-05-29 10:42:04 -040092
93 ConnectionManager(std::shared_ptr<Config> config_);
94 ~ConnectionManager();
95
96 /**
97 * Open a new channel between the account's device and another device
98 * This method will send a message on the account's DHT, wait a reply
99 * and then, create a Tls socket with remote peer.
100 * @param deviceId Remote device
101 * @param name Name of the channel
102 * @param cb Callback called when socket is ready ready
103 * @param noNewSocket Do not negotiate a new socket if there is none
104 * @param forceNewSocket Negotiate a new socket even if there is one // todo group with previous
105 * (enum)
106 * @param connType Type of the connection
107 */
108 void connectDevice(const DeviceId& deviceId,
109 const std::string& name,
110 ConnectCallback cb,
111 bool noNewSocket = false,
112 bool forceNewSocket = false,
113 const std::string& connType = "");
114 void connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
115 const std::string& name,
116 ConnectCallback cb,
117 bool noNewSocket = false,
118 bool forceNewSocket = false,
119 const std::string& connType = "");
120
121 /**
122 * Check if we are already connecting to a device with a specific name
123 * @param deviceId Remote device
124 * @param name Name of the channel
125 * @return if connecting
126 * @note isConnecting is not true just after connectDevice() as connectDevice is full async
127 */
128 bool isConnecting(const DeviceId& deviceId, const std::string& name) const;
129
130 /**
131 * Close all connections with a current device
132 * @param peerUri Peer URI
133 */
134 void closeConnectionsWith(const std::string& peerUri);
135
136 /**
137 * Method to call to listen to incoming requests
138 * @param deviceId Account's device
139 */
140 void onDhtConnected(const dht::crypto::PublicKey& devicePk);
141
142 /**
143 * Add a callback to decline or accept incoming ICE connections
144 * @param cb Callback to trigger
145 */
146 void onICERequest(onICERequestCallback&& cb);
147
148 /**
149 * Trigger cb on incoming peer channel
150 * @param cb Callback to trigger
151 * @note The callback is used to validate
152 * if the incoming request is accepted or not.
153 */
154 void onChannelRequest(ChannelRequestCallback&& cb);
155
156 /**
157 * Trigger cb when connection with peer is ready
158 * @param cb Callback to trigger
159 */
160 void onConnectionReady(ConnectionReadyCallback&& cb);
161
162 /**
163 * Trigger cb when connection with peer is ready for iOS devices
164 * @param cb Callback to trigger
165 */
166 void oniOSConnected(iOSConnectedCallback&& cb);
167
168 /**
169 * @return the number of active sockets
170 */
171 std::size_t activeSockets() const;
172
173 /**
174 * Log informations for all sockets
175 */
176 void monitor() const;
177
178 /**
179 * Send beacon on peers supporting it
180 */
181 void connectivityChanged();
182
183 /**
184 * Create and return ICE options.
185 */
186 void getIceOptions(std::function<void(IceTransportOptions&&)> cb) noexcept;
187 IceTransportOptions getIceOptions() const noexcept;
188
189 /**
190 * Get the published IP address, fallbacks to NAT if family is unspecified
191 * Prefers the usage of IPv4 if possible.
192 */
193 IpAddr getPublishedIpAddress(uint16_t family = PF_UNSPEC) const;
194
195 /**
196 * Set published IP address according to given family
197 */
198 void setPublishedAddress(const IpAddr& ip_addr);
199
200 /**
201 * Store the local/public addresses used to register
202 */
203 void storeActiveIpAddress(std::function<void()>&& cb = {});
204
205 std::shared_ptr<Config> getConfig();
206
207private:
208 ConnectionManager() = delete;
209 class Impl;
210 std::shared_ptr<Impl> pimpl_;
211};
212
213struct ConnectionManager::Config
214{
215 /**
216 * Determine if STUN public address resolution is required to register this account. In this
217 * case a STUN server hostname must be specified.
218 */
219 bool stunEnabled {false};
220
221 /**
222 * The STUN server hostname (optional), used to provide the public IP address in case the
223 * softphone stay behind a NAT.
224 */
225 std::string stunServer {};
226
227 /**
228 * Determine if TURN public address resolution is required to register this account. In this
229 * case a TURN server hostname must be specified.
230 */
231 bool turnEnabled {false};
232
233 /**
234 * The TURN server hostname (optional), used to provide the public IP address in case the
235 * softphone stay behind a NAT.
236 */
237 std::string turnServer;
238 std::string turnServerUserName;
239 std::string turnServerPwd;
240 std::string turnServerRealm;
241
Sébastien Blin84bf4182023-07-21 14:18:39 -0400242 std::shared_ptr<TurnCache> turnCache;
Adrien Béraud612b55b2023-05-29 10:42:04 -0400243
244 std::string cachePath {};
245
246 std::shared_ptr<asio::io_context> ioContext;
247 std::shared_ptr<dht::DhtRunner> dht;
248 dht::crypto::Identity id;
249
250 tls::CertificateStore* certStore;
251
Sébastien Blin34086512023-07-25 09:52:14 -0400252 dhtnet::IceTransportFactory* factory;
253
Adrien Béraud612b55b2023-05-29 10:42:04 -0400254 /**
255 * UPnP IGD controller and the mutex to access it
Sébastien Blin464bdff2023-07-19 08:02:53 -0400256 */
Adrien Béraud612b55b2023-05-29 10:42:04 -0400257 bool upnpEnabled;
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400258 std::shared_ptr<dhtnet::upnp::Controller> upnpCtrl;
Adrien Béraud612b55b2023-05-29 10:42:04 -0400259
260 std::shared_ptr<dht::log::Logger> logger;
261
262 /**
263 * returns whether or not UPnP is enabled and active
264 * ie: if it is able to make port mappings
265 */
266 bool getUPnPActive() const;
267};
268
Sébastien Blin464bdff2023-07-19 08:02:53 -0400269} // namespace dhtnet