diff options
Diffstat (limited to 'src/wol.rs')
-rw-r--r-- | src/wol.rs | 29 |
1 files changed, 29 insertions, 0 deletions
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 @@ | |||
1 | use std::net::{SocketAddr, UdpSocket}; | ||
2 | use std::num::ParseIntError; | ||
3 | |||
4 | /// Creates the magic packet from a mac address | ||
5 | /// | ||
6 | /// # Panics | ||
7 | /// | ||
8 | /// Panics if `mac_addr` is an invalid mac | ||
9 | pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, ParseIntError> { | ||
10 | let mut mac = Vec::new(); | ||
11 | let sp = mac_addr.split(':'); | ||
12 | for f in sp { | ||
13 | mac.push(u8::from_str_radix(f, 16)?); | ||
14 | }; | ||
15 | let mut buf = vec![255; 6]; | ||
16 | for _ in 0..16 { | ||
17 | for i in &mac { | ||
18 | buf.push(*i); | ||
19 | } | ||
20 | } | ||
21 | Ok(buf) | ||
22 | } | ||
23 | |||
24 | /// Sends a buffer on UDP broadcast | ||
25 | pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec<u8>) -> Result<usize, std::io::Error> { | ||
26 | let socket = UdpSocket::bind(bind_addr)?; | ||
27 | socket.set_broadcast(true)?; | ||
28 | socket.send_to(&buffer, broadcast_addr) | ||
29 | } \ No newline at end of file | ||