Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | namespace 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 | |
| 39 | struct 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 | |
| 48 | class Dvpn |
| 49 | { |
| 50 | public: |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 51 | Dvpn(dht::crypto::Identity identity, |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 52 | 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 }; |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 68 | std::shared_ptr<tls::TrustStore> trustStore; |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | class DvpnServer : public Dvpn |
| 72 | { |
| 73 | public: |
| 74 | // Build a server |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 75 | DvpnServer(dht::crypto::Identity identity, |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 76 | 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, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 81 | const std::string& configuration_file, |
| 82 | bool anonymous); |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | class DvpnClient : public Dvpn |
| 86 | { |
| 87 | public: |
| 88 | // Build a client |
| 89 | DvpnClient(dht::InfoHash peer_id, |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 90 | 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 | |
| 98 | private: |
| 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; |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace dhtnet |