-
Notifications
You must be signed in to change notification settings - Fork 8
/
wol.go
53 lines (42 loc) · 1.14 KB
/
wol.go
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
package gowol
import (
"errors"
"net"
)
// MagicPacket is a slice of 102 bytes containing the magic packet data.
type MagicPacket [102]byte
// NewMagicPacket allocates a new MagicPacket with the specified MAC.
func NewMagicPacket(macAddr string) (packet MagicPacket, err error) {
mac, err := net.ParseMAC(macAddr)
if err != nil {
return packet, err
}
if len(mac) != 6 {
return packet, errors.New("invalid EUI-48 MAC address")
}
// write magic bytes to packet
copy(packet[0:], []byte{255, 255, 255, 255, 255, 255})
offset := 6
for i := 0; i < 16; i++ {
copy(packet[offset:], mac)
offset += 6
}
return packet, nil
}
func sendUDPPacket(mp MagicPacket, addr string) (err error) {
conn, err := net.Dial("udp", addr)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write(mp[:])
return err
}
// Send writes the MagicPacket to the specified address on port 9.
func (mp MagicPacket) Send(addr string) error {
return sendUDPPacket(mp, addr+":9")
}
// SendPort writes the MagicPacket to the specified address and port.
func (mp MagicPacket) SendPort(addr string, port string) error {
return sendUDPPacket(mp, addr+":"+port)
}