blob: 6331907b86b34a2dd195cff9703f4316129f8e69 [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:
51 Dvpn(const std::filesystem::path& path,
52 dht::crypto::Identity identity,
53 const std::string& bootstrap,
54 const std::string& turn_host,
55 const std::string& turn_user,
56 const std::string& turn_pass,
57 const std::string& turn_realm,
58 const std::string& configuration_file);
59 ~Dvpn();
60 void run();
61
62 std::unique_ptr<ConnectionManager> connectionManager;
63 std::shared_ptr<Logger> logger;
64 std::shared_ptr<tls::CertificateStore> certStore;
65 std::shared_ptr<IceTransportFactory> iceFactory;
66 std::shared_ptr<asio::io_context> ioContext;
67 std::thread ioContextRunner;
68 enum class CommunicationState { METADATA, DATA };
Amna4325f0f2024-01-22 16:11:00 -050069 std::shared_ptr<tls::TrustStore> trustStore;
Amna4e52b162024-01-14 21:16:57 -050070};
71
72class DvpnServer : public Dvpn
73{
74public:
75 // Build a server
76 DvpnServer(const std::filesystem::path& path,
77 dht::crypto::Identity identity,
78 const std::string& bootstrap,
79 const std::string& turn_host,
80 const std::string& turn_user,
81 const std::string& turn_pass,
82 const std::string& turn_realm,
Amna4325f0f2024-01-22 16:11:00 -050083 const std::string& configuration_file,
84 bool anonymous);
Amna4e52b162024-01-14 21:16:57 -050085};
86
87class DvpnClient : public Dvpn
88{
89public:
90 // Build a client
91 DvpnClient(dht::InfoHash peer_id,
92 const std::filesystem::path& path,
93 dht::crypto::Identity identity,
94 const std::string& bootstrap,
95 const std::string& turn_host,
96 const std::string& turn_user,
97 const std::string& turn_pass,
98 const std::string& turn_realm,
99 const std::string& configuration_file);
100
101private:
102 msgpack::unpacker pac_ {};
103 CommunicationState connection_state = CommunicationState::METADATA;
104 int tun_fd;
105 char tun_device[IFNAMSIZ] = {0}; // IFNAMSIZ is typically the maximum size for interface names
106 std::shared_ptr<asio::posix::stream_descriptor> tun_stream;
Amna4e52b162024-01-14 21:16:57 -0500107};
108
109} // namespace dhtnet