From dcfb83fb2069bfcf4642b03453253e35479bf3da Mon Sep 17 00:00:00 2001 From: fx Date: Tue, 24 Oct 2023 01:15:22 +0200 Subject: first ping impl baseline, doesnt work --- src/wol.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/wol.rs') diff --git a/src/wol.rs b/src/wol.rs index 80b66cd..0cdcae3 100644 --- a/src/wol.rs +++ b/src/wol.rs @@ -1,16 +1,17 @@ use std::net::{SocketAddr, UdpSocket}; -use std::num::ParseIntError; + +use crate::error::WebolError; /// Creates the magic packet from a mac address /// /// # Panics /// /// Panics if `mac_addr` is an invalid mac -pub fn create_buffer(mac_addr: &str) -> Result, ParseIntError> { +pub fn create_buffer(mac_addr: &str) -> Result, WebolError> { let mut mac = Vec::new(); let sp = mac_addr.split(':'); for f in sp { - mac.push(u8::from_str_radix(f, 16)?); + mac.push(u8::from_str_radix(f, 16).map_err(WebolError::BufferParse)?) }; let mut buf = vec![255; 6]; for _ in 0..16 { @@ -22,8 +23,8 @@ pub fn create_buffer(mac_addr: &str) -> Result, ParseIntError> { } /// Sends a buffer on UDP broadcast -pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec) -> Result { - let socket = UdpSocket::bind(bind_addr)?; - socket.set_broadcast(true)?; - socket.send_to(&buffer, broadcast_addr) -} \ No newline at end of file +pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec) -> Result { + let socket = UdpSocket::bind(bind_addr).map_err(WebolError::Broadcast)?; + socket.set_broadcast(true).map_err(WebolError::Broadcast)?; + socket.send_to(&buffer, broadcast_addr).map_err(WebolError::Broadcast) +} -- cgit v1.2.3