blob: 42382aa57fb221e008a3dc0e7a953ed5cf2fd42c [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 };
69
70};
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,
83 const std::string& configuration_file);
84};
85
86class DvpnClient : public Dvpn
87{
88public:
89 // Build a client
90 DvpnClient(dht::InfoHash peer_id,
91 const std::filesystem::path& path,
92 dht::crypto::Identity identity,
93 const std::string& bootstrap,
94 const std::string& turn_host,
95 const std::string& turn_user,
96 const std::string& turn_pass,
97 const std::string& turn_realm,
98 const std::string& configuration_file);
99
100private:
101 msgpack::unpacker pac_ {};
102 CommunicationState connection_state = CommunicationState::METADATA;
103 int tun_fd;
104 char tun_device[IFNAMSIZ] = {0}; // IFNAMSIZ is typically the maximum size for interface names
105 std::shared_ptr<asio::posix::stream_descriptor> tun_stream;
106
107};
108
109} // namespace dhtnet