diff options
author | Ethel Morgan <eth@ethulhu.co.uk> | 2020-06-19 23:50:49 +0100 |
---|---|---|
committer | Ethel Morgan <eth@ethulhu.co.uk> | 2020-06-19 23:50:49 +0100 |
commit | 029f90de6895b68b5f3d1999858b09d055429679 (patch) | |
tree | 091541f5cb5d9e3d948ae69be5a5ac53aa7954c0 /wakeonlan | |
parent | d544da3d08be66807831c03bf0f421c0addd8e9f (diff) |
basic wake-on-lan actuator
Diffstat (limited to '')
-rw-r--r-- | wakeonlan/wakeonlan.go | 43 | ||||
-rw-r--r-- | wakeonlan/wakeonlan_test.go | 42 |
2 files changed, 85 insertions, 0 deletions
diff --git a/wakeonlan/wakeonlan.go b/wakeonlan/wakeonlan.go new file mode 100644 index 0000000..803d27f --- /dev/null +++ b/wakeonlan/wakeonlan.go @@ -0,0 +1,43 @@ +// Package wakeonlan implements Wake-On-Lan. +package wakeonlan + +import ( + "bytes" + "encoding/binary" + "fmt" + "net" +) + +var ( + header = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} + + broadcastAddr = &net.UDPAddr{ + IP: net.IPv4bcast, + Port: 9, + } +) + +// Wake sends the Magic Packet to wake the computer with the given MAC address. +func Wake(mac net.HardwareAddr) error { + conn, err := net.DialUDP("udp", nil, broadcastAddr) + if err != nil { + return fmt.Errorf("could not create UDP broadcast: %w", err) + } + defer conn.Close() + + payload := packet(mac) + + if _, err := conn.Write(payload); err != nil { + return fmt.Errorf("could not send Magic Packet: %w", err) + } + return nil +} + +func packet(mac net.HardwareAddr) []byte { + var buf bytes.Buffer + binary.Write(&buf, binary.BigEndian, header) + for i := 0; i < 16; i++ { + binary.Write(&buf, binary.BigEndian, mac) + } + return buf.Bytes() +} diff --git a/wakeonlan/wakeonlan_test.go b/wakeonlan/wakeonlan_test.go new file mode 100644 index 0000000..245d580 --- /dev/null +++ b/wakeonlan/wakeonlan_test.go @@ -0,0 +1,42 @@ +package wakeonlan + +import ( + "encoding/hex" + "net" + "reflect" + "testing" +) + +func TestPacket(t *testing.T) { + tests := []struct { + mac string + packet string + }{ + { + "a8:23:22:ad:be:c7", + "ffffffffffffa82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7a82322adbec7", + }, + } + + for i, test := range tests { + mac, err := net.ParseMAC(test.mac) + if err != nil { + t.Fatalf("invalid MAC address %q: %v", test.mac, err) + } + + if len(test.packet) != 102 { + if err != nil { + t.Fatalf("invalid packet length address: found %v, want 102", len(test.packet)) + } + } + want, err := hex.DecodeString(test.packet) + if err != nil { + t.Fatalf("invalid hex string: %v", err) + } + + got := packet(mac) + if !reflect.DeepEqual(want, got) { + t.Errorf("[test %d]\nwanted %v\nlen %d\ngot %v\nlen %d", i, want, len(want), got, len(got)) + } + } +} |