summaryrefslogtreecommitdiff
path: root/src/wol.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-24 01:15:22 +0200
committerfx <[email protected]>2023-10-24 01:15:22 +0200
commitdcfb83fb2069bfcf4642b03453253e35479bf3da (patch)
treeccdacf40274227e86a8294558347aeea43b1724f /src/wol.rs
parentd9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff)
downloadwebol-dcfb83fb2069bfcf4642b03453253e35479bf3da.tar
webol-dcfb83fb2069bfcf4642b03453253e35479bf3da.tar.gz
webol-dcfb83fb2069bfcf4642b03453253e35479bf3da.zip
first ping impl baseline, doesnt work
Diffstat (limited to 'src/wol.rs')
-rw-r--r--src/wol.rs17
1 files changed, 9 insertions, 8 deletions
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 @@
1use std::net::{SocketAddr, UdpSocket}; 1use std::net::{SocketAddr, UdpSocket};
2use std::num::ParseIntError; 2
3use crate::error::WebolError;
3 4
4/// Creates the magic packet from a mac address 5/// Creates the magic packet from a mac address
5/// 6///
6/// # Panics 7/// # Panics
7/// 8///
8/// Panics if `mac_addr` is an invalid mac 9/// Panics if `mac_addr` is an invalid mac
9pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, ParseIntError> { 10pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, WebolError> {
10 let mut mac = Vec::new(); 11 let mut mac = Vec::new();
11 let sp = mac_addr.split(':'); 12 let sp = mac_addr.split(':');
12 for f in sp { 13 for f in sp {
13 mac.push(u8::from_str_radix(f, 16)?); 14 mac.push(u8::from_str_radix(f, 16).map_err(WebolError::BufferParse)?)
14 }; 15 };
15 let mut buf = vec![255; 6]; 16 let mut buf = vec![255; 6];
16 for _ in 0..16 { 17 for _ in 0..16 {
@@ -22,8 +23,8 @@ pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, ParseIntError> {
22} 23}
23 24
24/// Sends a buffer on UDP broadcast 25/// Sends a buffer on UDP broadcast
25pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec<u8>) -> Result<usize, std::io::Error> { 26pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec<u8>) -> Result<usize, WebolError> {
26 let socket = UdpSocket::bind(bind_addr)?; 27 let socket = UdpSocket::bind(bind_addr).map_err(WebolError::Broadcast)?;
27 socket.set_broadcast(true)?; 28 socket.set_broadcast(true).map_err(WebolError::Broadcast)?;
28 socket.send_to(&buffer, broadcast_addr) 29 socket.send_to(&buffer, broadcast_addr).map_err(WebolError::Broadcast)
29} \ No newline at end of file 30}