Stub for libFenrir

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2023-02-03 19:18:34 +01:00
parent e46fbdb669
commit 104cbb6126
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
6 changed files with 163 additions and 11 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
[*.md]
trim_trailing_whitespace = false

View File

@ -1,8 +1,44 @@
[package]
name = "libFenrir"
name = "fenrir"
version = "0.1.0"
edition = "2021"
# Fenrir won't be ready for a while,
# we might as well use async fn in trait, which is nightly
# remember to update this
rust-version = "1.67.0"
homepage = "https://git.runesauth.com/RunesAuth/libFenrir"
repository = "https://git.runesauth.com/RunesAuth/libFenrir"
license = "Apache-2.0"
keywords = [ "Fenrir", "libFenrir", "authentication" ]
categories = [ "authentication", "cryptography", "network-programming" ]
# keep this until the PoC works
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate_type = [ "lib", "cdylib", "staticlib" ]
[dependencies]
thiserror = { version = "^1.0" }
tokio = { version = "^1", features = ["full"] }
# PERF: todo linux-only, behind "iouring" feature
#tokio-uring = { version = "^0.4" }
tracing = { version = "^0.1" }
[profile.dev]
opt-level = 0
debug = true
debug-assertions = true
overflow-checks = true
lto = false
panic = 'unwind'
incremental = true
codegen-units = 256
rpath = false

View File

@ -26,10 +26,16 @@
pkg-config
exa
fd
(rust-bin.stable.latest.default.override {
#(rust-bin.stable.latest.default.override {
# go with nightly to have async fn in traits
(rust-bin.nightly."2023-02-01".default.override {
#extensions = [ "rust-src" ];
#targets = [ "arm-unknown-linux-gnueabihf" ];
})
clippy
lld
cargo-watch
rustfmt
];
shellHook = ''
# use zsh or other custom shell

8
rustfmt.toml Normal file
View File

@ -0,0 +1,8 @@
edition = "2021"
unstable_features = true
format_strings = true
max_width = 80
# next ones do not work on stable yet
wrap_comments = true
comment_width = 80

23
src/config/mod.rs Normal file
View File

@ -0,0 +1,23 @@
//!
//! Configuration to initialize the Fenrir networking library
use ::std::{net::SocketAddr, num::NonZeroUsize, option::Option, vec::Vec};
/// Main config for libFenrir
#[derive(Clone, Debug)]
pub struct Config {
/// number of threads that libFenrir will use
pub threads: Option<NonZeroUsize>,
/// List of ipv4 or ipv6 UDP inet socket to listen on
/// If empty, libFenrir will listen on a random UDP port on `0.0.0.0`
pub listen: Vec<SocketAddr>,
}
impl Default for Config {
fn default() -> Self {
Config {
threads: None,
listen: Vec::new(),
}
}
}

View File

@ -1,14 +1,81 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications
)]
//!
//! libFenrir is the official rust library implementing the Fenrir protocol
use ::std::{
io::Result,
net::{IpAddr, Ipv4Addr, SocketAddr},
};
use ::tokio::{net::UdpSocket, runtime};
use ::tracing::error;
mod config;
pub use config::Config;
/// Instance of a fenrir endpoint
#[allow(missing_copy_implementations, missing_debug_implementations)]
pub struct Fenrir {
/// library Configuration
cfg: Config,
/// internal runtime
rt: ::tokio::runtime::Runtime,
}
#[cfg(test)]
mod tests {
use super::*;
/// Initialize a new Fenrir endpoint
pub fn init(config: &Config) -> Result<Fenrir> {
// PERF: linux iouring
// PERF: multithread pinning with hwloc2 for topology
let f = Fenrir::new(config.clone())?;
f.add_listener();
Ok(f)
}
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
impl Fenrir {
/// Create a new Fenrir endpoint
fn new(config: Config) -> Result<Self> {
let mut builder = runtime::Builder::new_multi_thread();
if let Some(threads) = config.threads {
builder.worker_threads(threads.get());
}
let rt = match builder.thread_name("libFenrir").build() {
Ok(rt) => rt,
Err(e) => {
::tracing::error!("Can't initialize: {}", e);
return Err(e);
}
};
Ok(Fenrir {
cfg: config,
rt: rt,
})
}
/// Add an UDP listener
fn add_listener(&self) {
if self.cfg.listen.len() == 0 {
self.rt.spawn(listen_udp(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
0, // 0 means random port
)));
} else {
for sock in self.cfg.listen.iter() {
self.rt.spawn(listen_udp(sock.clone()));
}
}
}
}
/// Add an async udp listener
async fn listen_udp(sock: SocketAddr) -> Result<()> {
UdpSocket::bind(sock).await?;
Ok(())
}