Merge pull request #1 from cool-mist/master

Improve perf slightly and add test
This commit is contained in:
C Ganesh Sundar
2026-06-29 17:54:06 +05:30
committed by GitHub
2 changed files with 62 additions and 13 deletions
+16 -3
View File
@@ -12,18 +12,31 @@ woly aa:bb:cc:dd:ee:ff
Magic packet is constructed as follows Magic packet is constructed as follows
FF x 6; Mac Address x 15 FF x 6; Mac Address x 16
eg. eg.
``` ```
FF FF FF FF FF FF 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
AA BB CC DD EE FF
AA BB CC DD EE FF
``` ```
## Target Destination ## Target Destination
The magic packet shall be sent to limited broadcast IP - `255.255.255.255` and the Discard Protocol Port `9`. 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. 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.
+46 -10
View File
@@ -1,28 +1,64 @@
use std::env::args; use std::env::args;
use std::net::UdpSocket; 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 mac_bytes = [0u8; 6];
let mut magic_bytes = [0u8; 102];
magic_bytes[..6].fill(0xFF);
for (i, part) in mac.split(':').enumerate() { 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 { for i in 0..16 {
let start = 6 + i * 6; let start = 6 + i * 6;
magic_bytes[start..start + 6].copy_from_slice(&mac_bytes); magic_bytes[start..start + 6].copy_from_slice(&mac_bytes);
} }
magic_bytes
} }
fn main() { fn main() {
let mac = args().nth(1).expect("no mac given; Usage woly aa:bb:cc:dd:ee:ff"); let mac = args()
let magic_bytes = get_magic_pkt(&mac); .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"); let socket = UdpSocket::bind("0.0.0.0:0").expect("couldnt' bind socket");
socket.set_broadcast(true).expect("couldn't set broadcast"); 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); 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");
}
}