34 lines
1019 B
Rust
34 lines
1019 B
Rust
use std::net::UdpSocket;
|
|
|
|
use crate::{
|
|
config::Config,
|
|
error::{WError, WResult},
|
|
};
|
|
|
|
pub struct Wolz {
|
|
config: Config,
|
|
}
|
|
|
|
impl Wolz {
|
|
pub fn create() -> WResult<Self> {
|
|
let config = Config::create()?;
|
|
Ok(Self { config })
|
|
}
|
|
|
|
pub fn wake_up(&self, mac_or_nick: &str) -> WResult<()> {
|
|
let mac = self.config.get_mac(mac_or_nick)?;
|
|
let mut magic_bytes = [0u8; 102];
|
|
mac.copy_magic_bytes(&mut magic_bytes);
|
|
|
|
let socket = UdpSocket::bind("0.0.0.0:0")
|
|
.map_err(|e| WError::UdpSocketError(format!("couldn't bind to socket because {}", e)))?;
|
|
socket.set_broadcast(true)
|
|
.map_err(|e| WError::UdpSocketError(format!("couldn't set broadcast because {}", e)))?;
|
|
socket
|
|
.send_to(&magic_bytes, "255.255.255.255:9")
|
|
.map_err(|e| WError::UdpSocketError(format!("couldn't send data because {}", e)))?;
|
|
println!("Magic packet sent to 255.255.255.255:9 for Mac {:}", mac);
|
|
Ok(())
|
|
}
|
|
}
|