From 72e2b31629964affae2d8b205d3ed516f998373c Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Sat, 21 May 2022 15:21:43 +0100 Subject: [PATCH] tcp connection listener --- examples/tls/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++++ examples/tls/spec.json | 35 +++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 examples/tls/main.rs create mode 100644 examples/tls/spec.json diff --git a/examples/tls/main.rs b/examples/tls/main.rs new file mode 100644 index 0000000..124bbd5 --- /dev/null +++ b/examples/tls/main.rs @@ -0,0 +1,58 @@ +use std::net::TcpListener; + +fn main() { + let mut args = std::env::args(); + + let _bin = args.next(); + let entrypoint = args.next(); + + match entrypoint { + Some(s) => match s.as_str() { + "connection_listener" => connection_listener_entrypoint(), + + _ => unimplemented!(), + }, + None => unimplemented!(), + } +} + +fn connection_listener_entrypoint() { + // imports + use std::os::unix::io::{FromRawFd, RawFd}; + + // argument parsing + let mut args = std::env::args(); + + let _bin = args.next(); + let _entrypoint = args.next(); + + let tcp_listener = args.next(); + let tcp_listener: RawFd = tcp_listener + .expect("tcp listener required") + .parse() + .expect("tcp listener should be a file descriptor"); + let tcp_listener = unsafe { TcpListener::from_raw_fd(tcp_listener) }; + + // actual function body + fn connection_listener(tcp_listener: TcpListener) -> i32 { + println!("connection_listener entered"); + + // handle incoming connections + for stream in tcp_listener.incoming() { + let _stream = match stream { + Ok(s) => s, + Err(e) => { + println!("connection listener: error: {}", e); + return 1; + } + }; + + println!("received a new connection"); + } + + exitcode::OK + } + + // run function + std::process::exit(connection_listener(tcp_listener)); +} diff --git a/examples/tls/spec.json b/examples/tls/spec.json new file mode 100644 index 0000000..c2ce4ca --- /dev/null +++ b/examples/tls/spec.json @@ -0,0 +1,35 @@ +{ + "entrypoints": { + "connection_listener": { + "args": [ + "BinaryName", + "Entrypoint", + { + "TcpListener": { + "addr": "0.0.0.0:8443" + } + } + ], + "environment": [ + { + "Filesystem": { + "host_path": "/lib/x86_64-linux-gnu/libgcc_s.so.1", + "environment_path": "/lib/libgcc_s.so.1" + } + }, + { + "Filesystem": { + "host_path": "/lib/x86_64-linux-gnu/libc.so.6", + "environment_path": "/lib/libc.so.6" + } + }, + { + "Filesystem": { + "host_path": "/lib64/ld-linux-x86-64.so.2", + "environment_path": "/lib64/ld-linux-x86-64.so.2" + } + } + ] + } + } +} \ No newline at end of file