1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::net::{Ipv4Addr, UdpSocket};

use eui48::{MacAddress, ParseError};
use thiserror::Error;
use tracing::{info, instrument};

type Result<T> = std::result::Result<T, WololoError>;

#[derive(Debug, Error)]
pub enum WololoError {
    #[error("Invalid MAC address")]
    ParseError(#[from] ParseError),
    #[error("Could not send packet")]
    SocketError(#[from] std::io::Error),
}

#[derive(Debug, Copy, Clone)]
pub struct Packet {
    bytes: [u8; 6],
}

impl Packet {
    #[instrument]
    pub fn new(bytes: [u8; 6]) -> Self {
        Self { bytes }
    }

    #[instrument]
    pub fn from_str(mac_address: &str) -> Result<Self> {
        let mac = MacAddress::parse_str(mac_address)?;
        Ok(Self {
            bytes: mac.to_array(),
        })
    }

    #[instrument]
    pub fn from_mac(mac_address: &MacAddress) -> Self {
        Self::new(mac_address.to_array())
    }

    #[instrument]
    pub fn to_macaddress(&self) -> Result<MacAddress> {
        MacAddress::from_bytes(&self.bytes).map_err(WololoError::ParseError)
    }

    #[instrument]
    pub fn to_bytes(&self) -> [u8; 102] {
        let mut payload = [0u8; 102];
        payload[0..6].copy_from_slice(&[0xff; 6]);
        for n in 1..17 {
            let start = n * 6;
            let end = start + 6;
            payload[start..end].copy_from_slice(&self.bytes);
        }

        payload
    }

    #[instrument]
    pub fn send(&self) -> Result<()> {
        info!("Waking host with MAC address {}", self.to_macaddress()?);
        let socket = UdpSocket::bind((Ipv4Addr::new(0, 0, 0, 0), 0))?;
        socket.set_broadcast(true)?;
        socket.send_to(&self.to_bytes(), (Ipv4Addr::new(255, 255, 255, 255), 9))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::Packet;

    const MAC_ADDRESS: [u8; 6] = [0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1];

    #[rustfmt::skip]
    const PACKET: [u8; 102] = [
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
        0x48, 0xdb, 0x6b, 0xe8, 0xdd, 0xa1,
    ];

    #[test]
    fn test_serialize_packet() {
        let pkt = Packet::new(MAC_ADDRESS);

        assert_eq!(pkt.to_bytes(), PACKET);
    }
}