From 4d3afef6ab6d6b96bc4b73a09a7ca076853a8cd5 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 4 Jan 2026 20:16:59 +0900 Subject: [PATCH] WIP --- Cargo.lock | 19 ++++++++ Cargo.toml | 3 +- src/bin/roowhod.rs | 40 ++++++++++++---- src/proto/finger_protocol.rs | 1 + src/proto/rusers_protocol.rs | 1 + src/proto/rwhod_protocol.rs | 88 ++++++++++++++++++++++++++++++++++-- src/proto/write_protocol.rs | 1 + 7 files changed, 138 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 773742f..0ba2fc1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,6 +85,12 @@ version = "3.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" +[[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" + [[package]] name = "cc" version = "1.2.51" @@ -556,6 +562,7 @@ name = "roowho2" version = "0.1.0" dependencies = [ "anyhow", + "bytes", "chrono", "clap", "nix", @@ -753,9 +760,21 @@ dependencies = [ "mio", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.61.2", ] +[[package]] +name = "tokio-macros" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "type-map" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 84f7492..adec9f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,11 @@ autolib = false [dependencies] anyhow = "1.0.100" +bytes = "1.11.0" chrono = { version = "0.4.42", features = ["serde"] } clap = { version = "4.5.53", features = ["derive"] } nix = { version = "0.30.1", features = ["hostname", "net"] } -tokio = { version = "1.49.0", features = ["net", "rt-multi-thread"] } +tokio = { version = "1.49.0", features = ["macros", "net", "rt-multi-thread"] } # onc-rpc = "0.3.2" # sd-notify = "0.4.5" # serde = { version = "1.0.228", features = ["derive"] } diff --git a/src/bin/roowhod.rs b/src/bin/roowhod.rs index 0b8f907..473ffca 100644 --- a/src/bin/roowhod.rs +++ b/src/bin/roowhod.rs @@ -1,10 +1,32 @@ -fn main() { - println!( - "{:#?}", - roowho2_lib::server::rwhod::generate_rwhod_status_update(), - ); - println!( - "{:#?}", - roowho2_lib::server::rwhod::determine_relevant_interfaces(), - ); +use std::net::SocketAddrV4; + +use roowho2_lib::proto::Whod; + +const RWHOD_BROADCAST_PORT: u16 = 513; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let addr = SocketAddrV4::new(std::net::Ipv4Addr::UNSPECIFIED, RWHOD_BROADCAST_PORT); + let socket = tokio::net::UdpSocket::bind(addr).await?; + socket.set_broadcast(true)?; + + let mut buf = [0u8; Whod::MAX_SIZE]; + loop { + let (len, src) = socket.recv_from(&mut buf).await?; + if len < Whod::HEADER_SIZE { + eprintln!( + "Received too short packet from {src}: {len} bytes (needs to be at least {} bytes)", + Whod::HEADER_SIZE + ); + continue; + } + match Whod::from_bytes(&buf[..len]) { + Ok(whod) => { + println!("Received whod packet from {src}:\n{whod:#?}"); + } + Err(err) => { + eprintln!("Failed to parse whod packet from {src}: {err}"); + } + } + } } diff --git a/src/proto/finger_protocol.rs b/src/proto/finger_protocol.rs index e69de29..8b13789 100644 --- a/src/proto/finger_protocol.rs +++ b/src/proto/finger_protocol.rs @@ -0,0 +1 @@ + diff --git a/src/proto/rusers_protocol.rs b/src/proto/rusers_protocol.rs index e69de29..8b13789 100644 --- a/src/proto/rusers_protocol.rs +++ b/src/proto/rusers_protocol.rs @@ -0,0 +1 @@ + diff --git a/src/proto/rwhod_protocol.rs b/src/proto/rwhod_protocol.rs index 93bceba..d7db8a3 100644 --- a/src/proto/rwhod_protocol.rs +++ b/src/proto/rwhod_protocol.rs @@ -1,5 +1,7 @@ use std::array; +use bytes::Buf; + #[derive(Debug, Clone)] #[repr(C)] pub struct Outmp { @@ -47,7 +49,9 @@ pub struct Whod { } impl Whod { - pub const SIZE: usize = std::mem::size_of::(); + pub const HEADER_SIZE: usize = 1 + 1 + 2 + 4 + 4 + Self::MAX_HOSTNAME_LEN + 4 * 3 + 4; + pub const MAX_SIZE: usize = std::mem::size_of::(); + pub const MAX_HOSTNAME_LEN: usize = 32; pub const MAX_WHOENTRIES: usize = 1024 / std::mem::size_of::(); @@ -56,13 +60,87 @@ impl Whod { // NOTE: there was probably meant to be more packet types, but only status is defined. pub const WHODTYPE_STATUS: u8 = 1; - pub fn to_bytes(&self) -> [u8; std::mem::size_of::()] { + pub fn to_bytes(&self) -> [u8; Whod::MAX_SIZE] { unsafe { std::mem::transmute_copy(self) } } - // TODO: we should probably make a safer parser. - pub fn from_bytes(bytes: &[u8; std::mem::size_of::()]) -> Self { - unsafe { std::mem::transmute_copy(bytes) } + pub fn from_bytes(input: &[u8]) -> anyhow::Result { + if input.len() < Self::HEADER_SIZE { + return Err(anyhow::anyhow!( + "Not enough bytes to parse Whod header: {} < {}", + input.len(), + Self::HEADER_SIZE + )); + } + + if input.len() > Self::MAX_SIZE { + return Err(anyhow::anyhow!( + "Too many bytes to parse Whod: {} > {}", + input.len(), + Self::MAX_SIZE + )); + } + + if input.len() - Self::HEADER_SIZE % std::mem::size_of::() != 0 { + return Err(anyhow::anyhow!( + "Invalid Whod bytes length: {} (not aligned with Whoent entries)", + input.len() + )); + } + + let mut bytes = bytes::Bytes::copy_from_slice(input); + + let wd_vers = bytes.get_u8(); + let wd_type = bytes.get_u8(); + bytes.advance(2); // skip wd_pad + let wd_sendtime = bytes.get_i32(); + let wd_recvtime = bytes.get_i32(); + let mut wd_hostname = [0u8; Self::MAX_HOSTNAME_LEN]; + bytes.copy_to_slice(&mut wd_hostname); + let wd_loadav = [bytes.get_i32(), bytes.get_i32(), bytes.get_i32()]; + let wd_boottime = bytes.get_i32(); + + debug_assert!(bytes.remaining() + Self::HEADER_SIZE == input.len()); + + let mut wd_we = array::from_fn(|_| Whoent { + we_utmp: Outmp { + out_line: [0u8; Outmp::MAX_TTY_NAME_LEN], + out_name: [0u8; Outmp::MAX_USER_ID_LEN], + out_time: 0, + }, + we_idle: 0, + }); + + for whoent in &mut wd_we { + if bytes.remaining() < std::mem::size_of::() { + break; + } + + let mut out_line = [0u8; Outmp::MAX_TTY_NAME_LEN]; + bytes.copy_to_slice(&mut out_line); + let mut out_name = [0u8; Outmp::MAX_USER_ID_LEN]; + bytes.copy_to_slice(&mut out_name); + let out_time = bytes.get_i32(); + + whoent.we_utmp = Outmp { + out_line, + out_name, + out_time, + }; + whoent.we_idle = bytes.get_i32(); + } + + Ok(Whod { + wd_vers, + wd_type, + wd_pad: [0u8; 2], + wd_sendtime, + wd_recvtime, + wd_hostname, + wd_loadav, + wd_boottime, + wd_we, + }) } } diff --git a/src/proto/write_protocol.rs b/src/proto/write_protocol.rs index e69de29..8b13789 100644 --- a/src/proto/write_protocol.rs +++ b/src/proto/write_protocol.rs @@ -0,0 +1 @@ +