aboutsummaryrefslogtreecommitdiff
path: root/src/wol.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wol.rs')
-rw-r--r--src/wol.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/wol.rs b/src/wol.rs
index 6392366..1176b28 100644
--- a/src/wol.rs
+++ b/src/wol.rs
@@ -1,7 +1,29 @@
1use std::net::{ToSocketAddrs, UdpSocket}; 1use std::net::{ToSocketAddrs, UdpSocket};
2 2
3use tracing::trace;
4
3use crate::error::Error; 5use crate::error::Error;
4 6
7/// Creates the magic packet from a mac address
8///
9/// # Panics
10///
11/// Panics if `mac_addr` is an invalid mac
12pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, Error> {
13 let mut mac = Vec::new();
14 let sp = mac_addr.split(':');
15 for f in sp {
16 mac.push(u8::from_str_radix(f, 16)?);
17 }
18 let mut buf = vec![255; 6];
19 for _ in 0..16 {
20 for i in &mac {
21 buf.push(*i);
22 }
23 }
24 Ok(buf)
25}
26
5/// Sends a buffer on UDP broadcast 27/// Sends a buffer on UDP broadcast
6pub fn send_packet<A: ToSocketAddrs>( 28pub fn send_packet<A: ToSocketAddrs>(
7 bind_addr: A, 29 bind_addr: A,
@@ -10,5 +32,6 @@ pub fn send_packet<A: ToSocketAddrs>(
10) -> Result<usize, Error> { 32) -> Result<usize, Error> {
11 let socket = UdpSocket::bind(bind_addr)?; 33 let socket = UdpSocket::bind(bind_addr)?;
12 socket.set_broadcast(true)?; 34 socket.set_broadcast(true)?;
35 trace!(?buffer ,"start with");
13 Ok(socket.send_to(buffer, broadcast_addr)?) 36 Ok(socket.send_to(buffer, broadcast_addr)?)
14} 37}