diff options
author | FxQnLr <[email protected]> | 2024-02-25 15:53:04 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-02-25 15:53:04 +0100 |
commit | f0dc13f907a72ffef44f89b5e197567db129b020 (patch) | |
tree | d560273df2eece276cbda021cb4e95c044bb19df /src/wol.rs | |
parent | c663810817183c8f92a4279236ca84d271365088 (diff) | |
parent | 91cd665671d564620bce13e693cd7ecaad697db9 (diff) | |
download | webol-f0dc13f907a72ffef44f89b5e197567db129b020.tar webol-f0dc13f907a72ffef44f89b5e197567db129b020.tar.gz webol-f0dc13f907a72ffef44f89b5e197567db129b020.zip |
Merge pull request #17 from FxQnLr/0.3.2
0.3.2
Diffstat (limited to 'src/wol.rs')
-rw-r--r-- | src/wol.rs | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -1,4 +1,4 @@ | |||
1 | use std::net::{SocketAddr, UdpSocket}; | 1 | use std::net::{ToSocketAddrs, UdpSocket}; |
2 | 2 | ||
3 | use crate::error::Error; | 3 | use 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 |
26 | pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: &[u8]) -> Result<usize, Error> { | 26 | pub 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 | } |