summaryrefslogtreecommitdiff
path: root/src/wol.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-15 17:17:30 +0100
committerFxQnLr <[email protected]>2024-02-15 17:17:30 +0100
commit3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 (patch)
tree48a38a52ebfc41fadab439b9a87fae3d43f78790 /src/wol.rs
parentc663810817183c8f92a4279236ca84d271365088 (diff)
downloadwebol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar
webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar.gz
webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.zip
Closes #10 & #12. Added `thiserror` crate and changed to `IntoSocketAddr` for easier usage and error handling
Diffstat (limited to 'src/wol.rs')
-rw-r--r--src/wol.rs18
1 files changed, 11 insertions, 7 deletions
diff --git a/src/wol.rs b/src/wol.rs
index 83c0ee6..31cf350 100644
--- a/src/wol.rs
+++ b/src/wol.rs
@@ -1,4 +1,4 @@
1use std::net::{SocketAddr, UdpSocket}; 1use std::net::{ToSocketAddrs, UdpSocket};
2 2
3use crate::error::Error; 3use crate::error::Error;
4 4
@@ -11,8 +11,8 @@ pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, Error> {
11 let mut mac = Vec::new(); 11 let mut mac = Vec::new();
12 let sp = mac_addr.split(':'); 12 let sp = mac_addr.split(':');
13 for f in sp { 13 for f in sp {
14 mac.push(u8::from_str_radix(f, 16).map_err(Error::BufferParse)?); 14 mac.push(u8::from_str_radix(f, 16)?);
15 }; 15 }
16 let mut buf = vec![255; 6]; 16 let mut buf = vec![255; 6];
17 for _ in 0..16 { 17 for _ in 0..16 {
18 for i in &mac { 18 for i in &mac {
@@ -23,8 +23,12 @@ pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, Error> {
23} 23}
24 24
25/// Sends a buffer on UDP broadcast 25/// Sends a buffer on UDP broadcast
26pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: &[u8]) -> Result<usize, Error> { 26pub fn send_packet<A: ToSocketAddrs>(
27 let socket = UdpSocket::bind(bind_addr).map_err(Error::Broadcast)?; 27 bind_addr: A,
28 socket.set_broadcast(true).map_err(Error::Broadcast)?; 28 broadcast_addr: A,
29 socket.send_to(buffer, broadcast_addr).map_err(Error::Broadcast) 29 buffer: &[u8],
30) -> Result<usize, Error> {
31 let socket = UdpSocket::bind(bind_addr)?;
32 socket.set_broadcast(true)?;
33 Ok(socket.send_to(buffer, broadcast_addr)?)
30} 34}