blob: e17775aac043a57a4e63ea1e6aa584e743946db5 [file] [log] [blame]
Amna4e52b162024-01-14 21:16:57 -05001/*
2 * Copyright (C) 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
18
19#pragma once
20#include "connectionmanager.h"
21#include "multiplexed_socket.h"
22#include "ice_transport_factory.h"
23#include "certstore.h"
24
25#include <asio.hpp>
26
27namespace dhtnet {
28
29/*
30 Both the client and the server have a TUN interface.
31 The server creates a a TUN interface for each client.
32 The client needs to know the server TUN address (peer address in the TUN configuration).
33 The server send its TUN addresses to the client in the first packet.
34 Two states are used to handle this:
35 - METADATA: the first packet is sent by the server and contains its TUN address
36 - DATA: the actual data
37*/
38
39struct MetaData
40{
41 std::string addrClient;
42 std::string addrServer;
43 std::string addrClientIpv6;
44 std::string addrServerIpv6;
45 MSGPACK_DEFINE_MAP(addrClient, addrServer, addrClientIpv6, addrServerIpv6);
46};
47
48class Dvpn
49{
50public:
Amnac75ffe92024-02-08 17:23:29 -050051 Dvpn(dht::crypto::Identity identity,
Amna4e52b162024-01-14 21:16:57 -050052 const std::string& bootstrap,
53 const std::string& turn_host,
54 const std::string& turn_user,
55 const std::string& turn_pass,
56 const std::string& turn_realm,
57 const std::string& configuration_file);
58 ~Dvpn();
59 void run();
60
61 std::unique_ptr<ConnectionManager> connectionManager;
62 std::shared_ptr<Logger> logger;
63 std::shared_ptr<tls::CertificateStore> certStore;
64 std::shared_ptr<IceTransportFactory> iceFactory;
65 std::shared_ptr<asio::io_context> ioContext;
66 std::thread ioContextRunner;
67 enum class CommunicationState { METADATA, DATA };
Amna4325f0f2024-01-22 16:11:00 -050068 std::shared_ptr<tls::TrustStore> trustStore;
Amna4e52b162024-01-14 21:16:57 -050069};
70
71class DvpnServer : public Dvpn
72{
73public:
74 // Build a server
Amnac75ffe92024-02-08 17:23:29 -050075 DvpnServer(dht::crypto::Identity identity,
Amna4e52b162024-01-14 21:16:57 -050076 const std::string& bootstrap,
77 const std::string& turn_host,
78 const std::string& turn_user,
79 const std::string& turn_pass,
80 const std::string& turn_realm,
Amna4325f0f2024-01-22 16:11:00 -050081 const std::string& configuration_file,
82 bool anonymous);
Amna4e52b162024-01-14 21:16:57 -050083};
84
85class DvpnClient : public Dvpn
86{
87public:
88 // Build a client
89 DvpnClient(dht::InfoHash peer_id,
Amna4e52b162024-01-14 21:16:57 -050090 dht::crypto::Identity identity,
91 const std::string& bootstrap,
92 const std::string& turn_host,
93 const std::string& turn_user,
94 const std::string& turn_pass,
95 const std::string& turn_realm,
96 const std::string& configuration_file);
97
98private:
99 msgpack::unpacker pac_ {};
100 CommunicationState connection_state = CommunicationState::METADATA;
101 int tun_fd;
102 char tun_device[IFNAMSIZ] = {0}; // IFNAMSIZ is typically the maximum size for interface names
103 std::shared_ptr<asio::posix::stream_descriptor> tun_stream;
Amna4e52b162024-01-14 21:16:57 -0500104};
105
106} // namespace dhtnet