From a8854301d2fe3d09b41ca055a713500edcead000 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Mon, 26 Aug 2024 20:17:33 +0200 Subject: Working on remote io tcp proxies and tls --- modules/io-tls/tls.cpp | 52 ++++++++++++++++++++-- modules/io-tls/tls.hpp | 26 ----------- modules/remote-io/.nix/derivation.nix | 43 ++++++++++++++++++ modules/remote-io/SConstruct | 82 +++++++++++++++++++++++++++++++++++ modules/remote-io/c++/SConscript | 38 ++++++++++++++++ modules/remote-io/c++/remote.hpp | 51 ++++++++++++++++++++++ modules/remote-io/examples/SConscript | 32 ++++++++++++++ modules/remote-io/tests/SConscript | 31 +++++++++++++ 8 files changed, 326 insertions(+), 29 deletions(-) create mode 100644 modules/remote-io/.nix/derivation.nix create mode 100644 modules/remote-io/SConstruct create mode 100644 modules/remote-io/c++/SConscript create mode 100644 modules/remote-io/c++/remote.hpp create mode 100644 modules/remote-io/examples/SConscript create mode 100644 modules/remote-io/tests/SConscript diff --git a/modules/io-tls/tls.cpp b/modules/io-tls/tls.cpp index 57406e3..981aa08 100644 --- a/modules/io-tls/tls.cpp +++ b/modules/io-tls/tls.cpp @@ -41,10 +41,15 @@ tls::impl &tls::get_impl() { return *impl_; } class tls_io_stream final : public io_stream { private: own internal; + gnutls_certificate_credentials_t xcred_; gnutls_session_t session_handle; public: - tls_io_stream(own internal_) : internal{std::move(internal_)} {} + tls_io_stream(own internal_, gnutls_certificate_credentials_t xcred__, gnutls_session_t session_handle__): + internal{std::move(internal_)}, + xcred_{xcred__}, + session_handle_{session_handle__} + {} ~tls_io_stream() { gnutls_bye(session_handle, GNUTLS_SHUT_RDWR); } @@ -89,6 +94,43 @@ public: gnutls_session_t &session() { return session_handle; } }; +class tls_server final : public server { +private: + own internal_; + gnutls_certificate_credentials_t xcred_; + gnutls_session_t session_handle_; + +public: + tls_server(own internal__, gnutls_certificate_credentials_t xcred__): + internal_{std::move(internal__)} + {} + + ~tls_server() { + gnutls_bye(session_handle_, GNUTLS_SHUT_RDWR); + gnutls_certificate_free_credentials(xcred_); + } + + conveyor> accept() override { + return make_error(); + } +}; + +class tls_network final : public network { +private: + tls& tls_; + network &internal; +public: + tls_network(tls& tls_, network &network_); + + conveyor> resolve_address(const std::string &addr, uint16_t port = 0) override; + + own listen(network_address& address) override; + + conveyor> connect(network_address& address) override; + + own datagram(network_address& address) override; +}; + tls_server::tls_server(own srv) : internal{std::move(srv)} {} conveyor> tls_server::accept() { @@ -157,8 +199,12 @@ public: }; } -own tls_network::listen(network_address& address) { - return heap(internal.listen(address)); +own tls_network::listen(const network_address& address) { + gnutls_certificate_credentials_t x509_cred; + gnutls_certificate_allocate_credentials(&x509_cred); + auto int_srv = internal.listen(address); + + return heap(int_srv, x509_cred); } conveyor> tls_network::connect(network_address& address) { diff --git a/modules/io-tls/tls.hpp b/modules/io-tls/tls.hpp index 5313bf7..a04598d 100644 --- a/modules/io-tls/tls.hpp +++ b/modules/io-tls/tls.hpp @@ -9,32 +9,6 @@ namespace saw { class tls; -class tls_server final : public server { -private: - own internal; - -public: - tls_server(own srv); - - conveyor> accept() override; -}; - -class tls_network final : public network { -private: - tls& tls_; - network &internal; -public: - tls_network(tls& tls_, network &network_); - - conveyor> resolve_address(const std::string &addr, uint16_t port = 0) override; - - own listen(network_address& address) override; - - conveyor> connect(network_address& address) override; - - own datagram(network_address& address) override; -}; - /** * tls context class. * Provides tls network class which ensures the usage of tls encrypted connections diff --git a/modules/remote-io/.nix/derivation.nix b/modules/remote-io/.nix/derivation.nix new file mode 100644 index 0000000..917789a --- /dev/null +++ b/modules/remote-io/.nix/derivation.nix @@ -0,0 +1,43 @@ +{ lib +, stdenv +, scons +, clang-tools +, version +, forstio +, build_examples ? "false" +}: + +stdenv.mkDerivation { + pname = "forstio-remote-thread"; + inherit version; + src = ./..; + + enableParallelBuilding = true; + + nativeBuildInputs = [ + scons + clang-tools + ]; + + buildInputs = [ + forstio.core + forstio.async + forstio.io + forstio.codec + forstio.io_codec + forstio.remote + ]; + + outputs = [ + "out" + "dev" + ]; + + buildPhase = '' + scons build_examples=${build_examples} + ''; + + installPhase = '' + scons prefix=$out build_examples=${build_examples} install + ''; +} diff --git a/modules/remote-io/SConstruct b/modules/remote-io/SConstruct new file mode 100644 index 0000000..fe16748 --- /dev/null +++ b/modules/remote-io/SConstruct @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add( + BoolVariable('build_examples', + help='Build examples', + default=False + ) +); + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +); + +env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], + CXX=['c++'], + CPPDEFINES=['SAW_UNIX'], + CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], + LIBS=[ + 'forstio-core', + 'forstio-async', + 'forstio-io', + 'forstio-codec', + 'pthread' + ] +); +env.__class__.add_source_files = add_kel_source_files +env.Tool('compilation_db'); +env.cdb = env.CompilationDatabase('compile_commands.json'); + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('c++/SConscript') +SConscript('examples/SConscript') + +env.Alias('cdb', env.cdb); +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') diff --git a/modules/remote-io/c++/SConscript b/modules/remote-io/c++/SConscript new file mode 100644 index 0000000..6194362 --- /dev/null +++ b/modules/remote-io/c++/SConscript @@ -0,0 +1,38 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +thread_env = env.Clone(); + +thread_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +thread_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += thread_env.sources; +env.headers += thread_env.headers; + +## Shared lib +objects_shared = [] +thread_env.add_source_files(objects_shared, thread_env.sources, shared=True); +env.library_shared = thread_env.SharedLibrary('#build/forstio-thread', [objects_shared]); + +## Static lib +objects_static = [] +thread_env.add_source_files(objects_static, thread_env.sources, shared=False); +env.library_static = thread_env.StaticLibrary('#build/forstio-thread', [objects_static]); + +# Set Alias +env.Alias('library_thread', [env.library_shared, env.library_static]); + +env.targets += ['library_thread']; + +# Install +env.Install('$prefix/lib/', [env.library_shared, env.library_static]); +env.Install('$prefix/include/forstio/thread/', [thread_env.headers]); diff --git a/modules/remote-io/c++/remote.hpp b/modules/remote-io/c++/remote.hpp new file mode 100644 index 0000000..fe2804d --- /dev/null +++ b/modules/remote-io/c++/remote.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include + +namespace saw { +/** + */ +namespace rmt { +struct IoTcp {}; +} + +template<> +class remote_address final { +private: + +public: + remote_address() = default; +}; + +/** + * A device representing a remote server. Technically it's + * a logical distinction and not a physical. + */ +template<> +class remote final { +private: + ref net_; +public: + remote(network& net__): + net_{net__} + {} + + conveyor>> resolve_address(){ + return heap>(); + } + + error_or>> parse_address(){ + return heap>(); + } + + template + error_or>> data_listen(const remote_address& addr){ + return make_error(); + } + + template + conveyor>> data_connect(const remote_address& addr){ + return make_error(); + } +}; +} diff --git a/modules/remote-io/examples/SConscript b/modules/remote-io/examples/SConscript new file mode 100644 index 0000000..df8e0c6 --- /dev/null +++ b/modules/remote-io/examples/SConscript @@ -0,0 +1,32 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +examples_env = env.Clone(); + +examples_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +examples_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += examples_env.sources; +env.headers += examples_env.headers; + +objects_static = [] + +# Set Alias +env.examples = [ +#, examples_env.echo_server +]; +env.Alias('examples', env.examples); + +if env["build_examples"]: + env.targets += ['examples']; + env.Install('$prefix/bin/', env.examples); +#endif diff --git a/modules/remote-io/tests/SConscript b/modules/remote-io/tests/SConscript new file mode 100644 index 0000000..f8ffc92 --- /dev/null +++ b/modules/remote-io/tests/SConscript @@ -0,0 +1,31 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +test_cases_env = env.Clone(); + +test_cases_env.Append(LIBS=['forstio-test']); + +test_cases_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +test_cases_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += test_cases_env.sources; +env.headers += test_cases_env.headers; + +objects_static = [] +test_cases_env.add_source_files(objects_static, test_cases_env.sources, shared=False); +test_cases_env.program = test_cases_env.Program('#bin/tests', [objects_static, env.library_static]); + +# Set Alias +env.Alias('test', test_cases_env.program); +env.Alias('check', test_cases_env.program); + +env.targets += ['test','check']; -- cgit v1.2.3