Skip to content

Commit

Permalink
wol: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Aug 7, 2015
0 parents commit b6f1c39
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: go
go:
- 1.4.2
- tip
before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
before_script:
- go get -d ./...
script:
- go test -v ./...
- if ! $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN; then echo "Coveralls not available."; fi
10 changes: 10 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MIT License
===========

Copyright (C) 2015 Matt Layher

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wol [![Build Status](https://travis-ci.org/mdlayher/wol.svg?branch=master)](https://travis-ci.org/mdlayher/wol) [![Coverage Status](https://coveralls.io/repos/mdlayher/wol/badge.svg?branch=master)](https://coveralls.io/r/mdlayher/wol?branch=master) [![GoDoc](http://godoc.org/github.com/mdlayher/wol?status.svg)](http://godoc.org/github.com/mdlayher/wol)
===

Package `wol` implements a Wake-on-LAN client. MIT Licensed.
137 changes: 137 additions & 0 deletions wol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Package wol implements a Wake-on-LAN client.
package wol

import (
"bytes"
"errors"
"io"
"net"

"github.com/mdlayher/ethernet"
)

const (
// EtherType is the registered EtherType for Wake-on-LAN over Ethernet.
EtherType ethernet.EtherType = 0x0842
)

var (
// ErrInvalidPassword is returned if a MagicPacket's Password field is
// not exactly 0 (empty), 4, or 6 bytes in length.
ErrInvalidPassword = errors.New("invalid password length")

// ErrInvalidSyncStream is returned if a MagicPacket's synchronization
// stream is incorrect.
ErrInvalidSyncStream = errors.New("invalid synchronization stream")

// ErrInvalidTarget is returned if a MagicPacket does not contain the
// same target hardware address repeated 16 times.
ErrInvalidTarget = errors.New("invalid hardware address target")
)

var (
// syncStream is a 6 byte slice which always occurs at the beginning of a
// Wake-on-LAN magic packet.
syncStream = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
)

// A MagicPacket is a Wake-on-LAN packet. It specifies a target hardware
// address to wake, and optionally, a password used to authenticate the
// MagicPacket wake request.
type MagicPacket struct {
// Target specifies the hardware address of a LAN device to wake using
// this MagicPacket.
Target net.HardwareAddr

// Password specifies an optional password for this MagicPacket. Password
// must be exactly 0 (empty), 4, or 6 bytes in length.
Password []byte
}

// MarshalBinary allocates a byte slice and marshals a MagicPacket into binary
// form.
//
// If p.Target is not exactly 6 bytes in length, ErrInvalidTarget is returned.
//
// If p.Password is not exactly 0 (empty), 4, or 6 bytes in length,
// ErrInvalidPassword is returned.
func (p *MagicPacket) MarshalBinary() ([]byte, error) {
// Must be 6 byte ethernet hardware address
if len(p.Target) != 6 {
return nil, ErrInvalidTarget
}

// Verify password is correct length
if pl := len(p.Password); pl != 0 && pl != 4 && pl != 6 {
return nil, ErrInvalidPassword
}

// 6 bytes: synchronization stream
// 6*16 bytes: repeated target ethernet hardware address
// N bytes: password
b := make([]byte, 6+(len(p.Target)*16)+len(p.Password))

// Synchronization stream must always be present first
copy(b[0:6], syncStream)

// Place repeated target hardware address 16 times
hl := len(p.Target)
for i := 0; i < 16; i++ {
copy(b[6+(hl*i):6+(hl*i)+hl], p.Target)
}

// Add password at end of slice
copy(b[len(b)-len(p.Password):], p.Password)

return b, nil
}

// UnmarshalBinary unmarshals a byte slice into a MagicPacket.
//
// If the byte slice does not contain enough data to unmarshal a valid
// MagicPacket, io.ErrUnexpectedEOF is returned.
//
// If the initial 6 bytes of the slice do not contain the synchronization
// stream value, ErrInvalidSyncStream is returned.
//
// If any of the 16 repeated target hardware addresses in the slice do not
// match, ErrInvalidTarget is returned.
//
// If the password in the slice is not exactly 0 (empty), 4, or 6 bytes in
// length, ErrInvalidPassword is returned.
func (p *MagicPacket) UnmarshalBinary(b []byte) error {
// Must contain sync stream and 16 repeated targets
if len(b) < 6+(6*16) {
return io.ErrUnexpectedEOF
}

// Sync stream must be correct
if !bytes.Equal(b[0:6], syncStream) {
return ErrInvalidSyncStream
}

// Hardware address must correctly repeat 16 times
for i := 0; i < 16; i++ {
if !bytes.Equal(b[6:12], b[6+(6*i):6+(6*i)+6]) {
return ErrInvalidTarget
}
}

// Password must be 0 (empty), 4, or 6 bytes in length
pl := len(b[6+(6*16):])
if pl != 0 && pl != 4 && pl != 6 {
return ErrInvalidPassword
}

// Allocate a single byte slice for target and password, and
// reslice it to store fields
bb := make([]byte, 6+pl)

copy(bb[0:6], b[6:12])
p.Target = bb[0:6]

copy(bb[6:], b[len(b)-pl:])
p.Password = bb[6:]

return nil
}
Loading

0 comments on commit b6f1c39

Please sign in to comment.