blob: 0db11b2fc9938354d467b60617285008b0fb9188 [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;
Amna4e52b162024-01-14 21:16:57 -050066 enum class CommunicationState { METADATA, DATA };
Amna4325f0f2024-01-22 16:11:00 -050067 std::shared_ptr<tls::TrustStore> trustStore;
Amna4e52b162024-01-14 21:16:57 -050068};
69
70class DvpnServer : public Dvpn
71{
72public:
73 // Build a server
Amnac75ffe92024-02-08 17:23:29 -050074 DvpnServer(dht::crypto::Identity identity,
Amna4e52b162024-01-14 21:16:57 -050075 const std::string& bootstrap,
76 const std::string& turn_host,
77 const std::string& turn_user,
78 const std::string& turn_pass,
79 const std::string& turn_realm,
Amna4325f0f2024-01-22 16:11:00 -050080 const std::string& configuration_file,
81 bool anonymous);
Amna4e52b162024-01-14 21:16:57 -050082};
83
84class DvpnClient : public Dvpn
85{
86public:
87 // Build a client
88 DvpnClient(dht::InfoHash peer_id,
Amna4e52b162024-01-14 21:16:57 -050089 dht::crypto::Identity identity,
90 const std::string& bootstrap,
91 const std::string& turn_host,
92 const std::string& turn_user,
93 const std::string& turn_pass,
94 const std::string& turn_realm,
95 const std::string& configuration_file);
96
97private:
98 msgpack::unpacker pac_ {};
99 CommunicationState connection_state = CommunicationState::METADATA;
100 int tun_fd;
101 char tun_device[IFNAMSIZ] = {0}; // IFNAMSIZ is typically the maximum size for interface names
102 std::shared_ptr<asio::posix::stream_descriptor> tun_stream;
Amna4e52b162024-01-14 21:16:57 -0500103};
104
105} // namespace dhtnet