aboutsummaryrefslogtreecommitdiff
path: root/src/wol.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-11-06 11:09:34 +0100
committerGitHub <[email protected]>2023-11-06 11:09:34 +0100
commit1cd2a8e4aecfaad2a8385a6bea61580209b86398 (patch)
treec357bcaca0681caf9a6742c857bb494dc4315900 /src/wol.rs
parentd9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff)
parent32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (diff)
downloadwebol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar
webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar.gz
webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.zip
Merge pull request #6 from FxQnLr/ping
Ping
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}