diff options
author | fx <[email protected]> | 2023-10-09 13:07:54 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-09 13:07:54 +0200 |
commit | bc5f721de8996b48550b5069f5592caf2968e822 (patch) | |
tree | fe7bf485cda800d6fff595c0573b9bca4a4c1dd0 /src/wol.rs | |
parent | 159cb1b3c940440ebe03e5042c361be563324978 (diff) | |
download | webol-bc5f721de8996b48550b5069f5592caf2968e822.tar webol-bc5f721de8996b48550b5069f5592caf2968e822.tar.gz webol-bc5f721de8996b48550b5069f5592caf2968e822.zip |
added wol func and bad auth
Diffstat (limited to 'src/wol.rs')
-rw-r--r-- | src/wol.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/wol.rs b/src/wol.rs new file mode 100644 index 0000000..80b66cd --- /dev/null +++ b/src/wol.rs | |||
@@ -0,0 +1,29 @@ | |||
1 | use std::net::{SocketAddr, UdpSocket}; | ||
2 | use std::num::ParseIntError; | ||
3 | |||
4 | /// Creates the magic packet from a mac address | ||
5 | /// | ||
6 | /// # Panics | ||
7 | /// | ||
8 | /// Panics if `mac_addr` is an invalid mac | ||
9 | pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, ParseIntError> { | ||
10 | let mut mac = Vec::new(); | ||
11 | let sp = mac_addr.split(':'); | ||
12 | for f in sp { | ||
13 | mac.push(u8::from_str_radix(f, 16)?); | ||
14 | }; | ||
15 | let mut buf = vec![255; 6]; | ||
16 | for _ in 0..16 { | ||
17 | for i in &mac { | ||
18 | buf.push(*i); | ||
19 | } | ||
20 | } | ||
21 | Ok(buf) | ||
22 | } | ||
23 | |||
24 | /// Sends a buffer on UDP broadcast | ||
25 | pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: Vec<u8>) -> Result<usize, std::io::Error> { | ||
26 | let socket = UdpSocket::bind(bind_addr)?; | ||
27 | socket.set_broadcast(true)?; | ||
28 | socket.send_to(&buffer, broadcast_addr) | ||
29 | } \ No newline at end of file | ||