From bc5f721de8996b48550b5069f5592caf2968e822 Mon Sep 17 00:00:00 2001 From: fx Date: Mon, 9 Oct 2023 13:07:54 +0200 Subject: added wol func and bad auth --- src/wol.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/wol.rs (limited to 'src/wol.rs') diff --git a/src/wol.rs b/src/wol.rs new file mode 100644 index 0000000..80b66cd --- /dev/null +++ b/src/wol.rs @@ -0,0 +1,29 @@ +use std::net::{SocketAddr, UdpSocket}; +use std::num::ParseIntError; + +/// Creates the magic packet from a mac address +/// +/// # Panics +/// +/// Panics if `mac_addr` is an invalid mac +pub fn create_buffer(mac_addr: &str) -> Result, ParseIntError> { + let mut mac = Vec::new(); + let sp = mac_addr.split(':'); + for f in sp { + mac.push(u8::from_str_radix(f, 16)?); + }; + let mut buf = vec![255; 6]; + for _ in 0..16 { + for i in &mac { + buf.push(*i); + } + } + Ok(buf) +} + +/// Sends a buffer on UDP broadcast +pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec) -> Result { + let socket = UdpSocket::bind(bind_addr)?; + socket.set_broadcast(true)?; + socket.send_to(&buffer, broadcast_addr) +} \ No newline at end of file -- cgit v1.2.3