From 7bb0dafaf6e5d1cb9109ad651368689d36d91e1f Mon Sep 17 00:00:00 2001 From: Wijnand Modderman-Lenstra Date: Mon, 21 Dec 2015 11:05:08 +0100 Subject: [PATCH] homebrew: split off peer --- homebrew/homebrew.go | 37 +---------------------------------- homebrew/peer.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 homebrew/peer.go diff --git a/homebrew/homebrew.go b/homebrew/homebrew.go index 46a6465..04d10ba 100644 --- a/homebrew/homebrew.go +++ b/homebrew/homebrew.go @@ -4,7 +4,6 @@ package homebrew import ( "bytes" "crypto/rand" - "crypto/sha256" "encoding/binary" "encoding/hex" "errors" @@ -66,41 +65,6 @@ var ( SendInterval = time.Millisecond * 30 ) -// Peer is a remote repeater that also speaks the Homebrew protocol -type Peer struct { - ID uint32 - Addr *net.UDPAddr - AuthKey []byte - Status AuthStatus - Nonce []byte - Token []byte - Incoming bool - UnlinkOnAuthFailure bool - PacketReceived dmr.PacketFunc - Last struct { - PacketSent time.Time - PacketReceived time.Time - PingSent time.Time - PingReceived time.Time - PongReceived time.Time - } - - // Packed repeater ID - id []byte -} - -func (p *Peer) CheckRepeaterID(id []byte) bool { - return id != nil && p.id != nil && bytes.Equal(id, p.id) -} - -func (p *Peer) UpdateToken(nonce []byte) { - p.Nonce = nonce - hash := sha256.New() - hash.Write(p.Nonce) - hash.Write(p.AuthKey) - p.Token = []byte(hex.EncodeToString(hash.Sum(nil))) -} - // Homebrew is implements the Homebrew IPSC DMR Air Interface protocol type Homebrew struct { Config *RepeaterConfiguration @@ -135,6 +99,7 @@ func New(config *RepeaterConfiguration, addr *net.UDPAddr) (*Homebrew, error) { PeerID: make(map[uint32]*Peer), id: packRepeaterID(config.ID), mutex: &sync.Mutex{}, + rxtx: &sync.Mutex{}, queue: make([]*dmr.Packet, 0), } if h.conn, err = net.ListenUDP("udp", addr); err != nil { diff --git a/homebrew/peer.go b/homebrew/peer.go new file mode 100644 index 0000000..95af1a9 --- /dev/null +++ b/homebrew/peer.go @@ -0,0 +1,46 @@ +package homebrew + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "net" + "time" + + "github.com/pd0mz/go-dmr" +) + +// Peer is a remote repeater that also speaks the Homebrew protocol +type Peer struct { + ID uint32 + Addr *net.UDPAddr + AuthKey []byte + Status AuthStatus + Nonce []byte + Token []byte + Incoming bool + UnlinkOnAuthFailure bool + PacketReceived dmr.PacketFunc + Last struct { + PacketSent time.Time + PacketReceived time.Time + PingSent time.Time + PingReceived time.Time + PongReceived time.Time + } + + // Packed repeater ID + id []byte +} + +func (p *Peer) CheckRepeaterID(id []byte) bool { + return id != nil && p.id != nil && bytes.Equal(id, p.id) +} + +func (p *Peer) UpdateToken(nonce []byte) { + p.Nonce = nonce + hash := sha256.New() + hash.Write(p.Nonce) + hash.Write(p.AuthKey) + p.Token = []byte(hex.EncodeToString(hash.Sum(nil))) +}