WIP
This commit is contained in:
19
Cargo.lock
generated
19
Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -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"] }
|
||||
|
||||
@@ -1,10 +1,34 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
buf = [0u8; Whod::MAX_SIZE];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::array;
|
||||
|
||||
use bytes::Buf;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[repr(C)]
|
||||
pub struct Outmp {
|
||||
@@ -25,6 +27,10 @@ pub struct Whoent {
|
||||
pub we_idle: i32,
|
||||
}
|
||||
|
||||
impl Whoent {
|
||||
pub const SIZE: usize = std::mem::size_of::<Self>();
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[repr(C)]
|
||||
pub struct Whod {
|
||||
@@ -47,7 +53,9 @@ pub struct Whod {
|
||||
}
|
||||
|
||||
impl Whod {
|
||||
pub const SIZE: usize = std::mem::size_of::<Whod>();
|
||||
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::<Self>();
|
||||
|
||||
pub const MAX_HOSTNAME_LEN: usize = 32;
|
||||
pub const MAX_WHOENTRIES: usize = 1024 / std::mem::size_of::<Whoent>();
|
||||
|
||||
@@ -56,13 +64,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::<Whod>()] {
|
||||
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::<Whod>()]) -> Self {
|
||||
unsafe { std::mem::transmute_copy(bytes) }
|
||||
pub fn from_bytes(input: &[u8]) -> anyhow::Result<Self> {
|
||||
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) % Whoent::SIZE != 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::<Whoent>() {
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
Reference in New Issue
Block a user