diff --git a/README.md b/README.md index 954b9dc..35ef4c4 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,31 @@ woly aa:bb:cc:dd:ee:ff Magic packet is constructed as follows -FF x 6; Mac Address x 15 +FF x 6; Mac Address x 16 eg. ``` FF FF FF FF FF FF AA BB CC DD EE FF AA BB CC DD EE FF -... +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF +AA BB CC DD EE FF ``` ## Target Destination The magic packet shall be sent to limited broadcast IP - `255.255.255.255` and the Discard Protocol Port `9`. -While sending it to targeted IP, for eg. 192.168.0.2 bound to desired mac address of the device might work, it is not reliable as sometimes the ARP cache will not be updated by router due to device being in low power mode and will drop this packet. \ No newline at end of file +While sending it to targeted IP, for eg. 192.168.0.2 bound to desired mac address of the device might work, it is not reliable as sometimes the ARP cache will not be updated by router due to device being in low power mode and will drop this packet. diff --git a/src/main.rs b/src/main.rs index 392b93a..485a41f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,64 @@ use std::env::args; use std::net::UdpSocket; -fn get_magic_pkt(mac: &String) -> [u8; 102] { +fn get_magic_pkt(mac: &str, magic_bytes: &mut [u8; 102]) { let mut mac_bytes = [0u8; 6]; - let mut magic_bytes = [0u8; 102]; - magic_bytes[..6].fill(0xFF); - for (i, part) in mac.split(':').enumerate() { - mac_bytes[i] = u8::from_str_radix(part, 16).unwrap(); + mac_bytes[i] = u8::from_str_radix(part, 16) + .expect(&format!("'{}' is not a valid hex between 00-ff", part)); } + + magic_bytes[..6].fill(0xFF); for i in 0..16 { let start = 6 + i * 6; magic_bytes[start..start + 6].copy_from_slice(&mac_bytes); } - - magic_bytes } fn main() { - let mac = args().nth(1).expect("no mac given; Usage woly aa:bb:cc:dd:ee:ff"); - let magic_bytes = get_magic_pkt(&mac); + let mac = args() + .nth(1) + .expect("no mac given; Usage woly aa:bb:cc:dd:ee:ff"); + + let mut magic_bytes = [0u8; 102]; + get_magic_pkt(&mac, &mut magic_bytes); let socket = UdpSocket::bind("0.0.0.0:0").expect("couldnt' bind socket"); socket.set_broadcast(true).expect("couldn't set broadcast"); - socket.send_to(&magic_bytes, "255.255.255.255:9").expect("couldn't send data"); + socket + .send_to(&magic_bytes, "255.255.255.255:9") + .expect("couldn't send data"); println!("Magic packet sent to 255.255.255.255:9 for Mac {:?}", mac); } + +#[cfg(test)] +mod tests { + use crate::get_magic_pkt; + + #[test] + fn test_magic_bytes_for_mac() { + let mut magic_bytes = [0u8; 102]; + get_magic_pkt("32:1a:ff:3e:10:ef", &mut magic_bytes); + let expected = [ + 255, 255, 255, 255, 255, 255, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + 50, 26, 255, 62, 16, 239, + ]; + + assert_eq!(magic_bytes, expected, "Magic bytes are not created correctly"); + } +}