implement shutdown

This commit is contained in:
cool-mist
2026-07-11 21:10:24 +05:30
parent d1ff5c0b10
commit a5403a5de3
8 changed files with 285 additions and 119 deletions
+20 -7
View File
@@ -1,7 +1,9 @@
use std::net::UdpSocket;
use std::{
io::Write,
net::{TcpStream, UdpSocket},
};
use crate::{
arp,
config::Config,
error::{WError, WResult},
mac::Mac,
@@ -55,10 +57,18 @@ impl Wolz {
Ok(())
}
pub fn shut_down(&self, mac_or_nick: &str) -> WResult<()> {
// TODO: Fallback to full network scan if /proc/net/arp file lookup fails
fn shut_down(&self, mac_or_nick: &str) -> WResult<()> {
let mac = self.config.get_mac(mac_or_nick)?;
let ip_from_arp = netif::find_local_networks_with_mac(mac)?;
todo!()
let ip = netif::find_ip_with_mac(mac)?;
println!("Sending shutdown signal to {}:21367", ip);
let mut stream = TcpStream::connect(format!("{}:21367", ip)).map_err(WError::TcpSocketBindError)?;
stream
.write("shutdown".as_bytes())
.map_err(WError::TcpSocketWriteError)?;
Ok(())
}
}
@@ -67,10 +77,13 @@ fn try_send_magic_bytes(magic_bytes: &[u8; 102], mac: &Mac, local_network: &Loca
let broadcast_addr = format!("{}:9", local_network.subnet_broadcast);
match send_magic_bytes(&magic_bytes, &send_addr, &broadcast_addr) {
Ok(()) => {
println!("Sent magic pkt {} -> {} for Mac {}", send_addr, broadcast_addr, mac);
println!(
"Sent WoL magic packet {} -> {} for mac {}",
send_addr, broadcast_addr, mac
);
}
Err(e) => println!(
"Unable to send magic pkt {} -> {} for Mac {}, because {}",
"Unable to send WoL magic packet {} -> {} for mac {}, because {}",
send_addr, broadcast_addr, mac, e
),
}