/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include "crypto.h" #include "wivrn_serialization.h" #include #include #include #include #include #include #include #include #include #include namespace wivrn { class socket_shutdown : public std::exception { public: const char * what() const noexcept override; }; class invalid_packet : public std::exception { public: const char * what() const noexcept override; }; template struct index_of_type {}; template struct index_of_type : std::integral_constant {}; template struct index_of_type : std::integral_constant::value> {}; static_assert(index_of_type::value == 0); static_assert(index_of_type::value == 1); class fd_base { protected: int fd = -1; fd_base(const fd_base &) = delete; std::atomic bytes_sent_ = 0; std::atomic bytes_received_ = 0; public: fd_base() = default; fd_base(int fd) : fd{fd} {} fd_base(fd_base &&); fd_base & operator=(fd_base &&); ~fd_base(); int get_fd() const { return fd; } operator bool() const { return fd != -1; } uint64_t bytes_sent() const { return bytes_sent_; } uint64_t bytes_received() const { return bytes_received_; } }; class UDP : public fd_base { std::shared_ptr buffer; std::vector> messages; crypto::decrypt_context decrypter; static thread_local crypto::encrypt_context encrypter; static std::atomic iv_counter; static_assert(sizeof(iv_counter) == 8); bool encrypted = false; std::array key; std::array recv_iv_header; std::array send_iv_header; public: UDP(); explicit UDP(int fd); deserialization_packet receive_raw(); deserialization_packet receive_pending(); std::pair receive_from_raw(); void send_raw(serialization_packet && packet); void send_many_raw(std::span packets); void connect(in6_addr address, int port); void connect(in_addr address, int port); void bind(sockaddr_in6 address); void subscribe_multicast(in6_addr address); void unsubscribe_multicast(in6_addr address); void set_receive_buffer_size(int size); void set_send_buffer_size(int size); void set_tos(int type_of_service); void set_aes_key_and_ivs(std::span key, std::span recv_iv_header, std::span send_iv_header); }; class TCP : public fd_base { std::shared_ptr buffer; ssize_t capacity_left = 0; std::span data; std::unique_ptr mutex; void init(); crypto::decrypt_context decrypter; crypto::encrypt_context encrypter; public: TCP() = default; TCP(in6_addr address, int port); TCP(in_addr address, int port); explicit TCP(int fd); deserialization_packet receive_raw(); deserialization_packet receive_pending(); void send_raw(serialization_packet && packet); void send_many_raw(std::span packets); void set_aes_key_and_ivs(std::span key, std::span recv_iv, std::span send_iv); }; using UnixDatagram = UDP; class TCPListener : public fd_base { public: TCPListener() = default; TCPListener(int port); template std::pair accept() { assert(fd != -1); sockaddr_in6 addr{}; socklen_t addrlen = sizeof(addr); int fd2 = ::accept(fd, (sockaddr *)&addr, &addrlen); fcntl(fd, F_SETFD, FD_CLOEXEC); if (fd2 < 0) throw std::system_error{errno, std::generic_category()}; return {T{fd2}, addr}; } }; template class typed_socket; namespace details { template struct Index; template struct Index> { static const std::size_t value = 0; }; template struct Index> { static const std::size_t value = 1 + Index>::value; }; template concept not_lvalue_reference = !std::is_lvalue_reference_v; } // namespace details template class typed_socket> : public Socket { public: template typed_socket(Args &&... args) : Socket(std::forward(args)...) {} std::optional receive_pending() { deserialization_packet packet = ((Socket *)this)->receive_pending(); if (packet.empty()) return {}; return packet.deserialize(); } std::optional receive() { deserialization_packet packet = this->receive_raw(); if (packet.empty()) return {}; return packet.deserialize(); } // WARNING: serialization packet keeps references to data template static void serialize(serialization_packet & p, const T & data) { p.clear(); uint8_t index = details::Index, std::tuple>::value; p.serialize(index); p.serialize(data); } template void send(T && data) { thread_local serialization_packet p; serialize(p, data); this->send_raw(std::move(p)); } void send(std::span packets) { this->send_many_raw(packets); } }; } // namespace wivrn