2020-10-17 23:53:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2020-10-18 12:50:09 +02:00
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/binary"
|
2020-10-19 09:45:49 +02:00
|
|
|
"errors"
|
2020-10-17 23:53:33 +02:00
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/nonoo/kappanhang/log"
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-18 18:34:22 +02:00
|
|
|
const expectTimeoutDuration = time.Second
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
type streamCommon struct {
|
2020-10-18 19:41:19 +02:00
|
|
|
name string
|
|
|
|
|
conn *net.UDPConn
|
|
|
|
|
localSID uint32
|
|
|
|
|
remoteSID uint32
|
|
|
|
|
gotRemoteSID bool
|
|
|
|
|
readChan chan []byte
|
|
|
|
|
readerClosedChan chan bool
|
2020-10-18 12:50:09 +02:00
|
|
|
|
2020-10-18 14:21:58 +02:00
|
|
|
pkt7 pkt7Type
|
2020-10-17 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
func (s *streamCommon) send(d []byte) {
|
|
|
|
|
_, err := s.conn.Write(d)
|
2020-10-17 23:53:33 +02:00
|
|
|
if err != nil {
|
2020-10-19 09:45:49 +02:00
|
|
|
exit(err)
|
2020-10-17 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 19:41:19 +02:00
|
|
|
func (s *streamCommon) read() ([]byte, error) {
|
2020-10-17 23:53:33 +02:00
|
|
|
b := make([]byte, 1500)
|
2020-10-18 11:01:53 +02:00
|
|
|
n, _, err := s.conn.ReadFromUDP(b)
|
2020-10-18 19:41:19 +02:00
|
|
|
return b[:n], err
|
2020-10-17 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 13:02:41 +02:00
|
|
|
func (s *streamCommon) reader() {
|
2020-10-18 10:53:16 +02:00
|
|
|
for {
|
2020-10-18 19:41:19 +02:00
|
|
|
r, err := s.read()
|
|
|
|
|
if err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
2020-10-18 18:34:22 +02:00
|
|
|
if s.pkt7.isPkt7(r) {
|
|
|
|
|
s.pkt7.handle(s, r)
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
2020-10-18 18:34:22 +02:00
|
|
|
|
|
|
|
|
s.readChan <- r
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
2020-10-18 19:41:19 +02:00
|
|
|
s.readerClosedChan <- true
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 18:34:22 +02:00
|
|
|
func (s *streamCommon) tryReceivePacket(timeout time.Duration, packetLength, matchStartByte int, b []byte) []byte {
|
2020-10-17 23:53:33 +02:00
|
|
|
var r []byte
|
2020-10-18 19:41:19 +02:00
|
|
|
timer := time.NewTimer(timeout)
|
2020-10-17 23:53:33 +02:00
|
|
|
for {
|
2020-10-18 19:41:19 +02:00
|
|
|
select {
|
|
|
|
|
case r = <-s.readChan:
|
|
|
|
|
case <-timer.C:
|
|
|
|
|
return nil
|
2020-10-18 18:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(r) == packetLength && bytes.Equal(r[matchStartByte:len(b)+matchStartByte], b) {
|
2020-10-17 23:53:33 +02:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 18:34:22 +02:00
|
|
|
func (s *streamCommon) expect(packetLength int, b []byte) []byte {
|
|
|
|
|
r := s.tryReceivePacket(expectTimeoutDuration, packetLength, 0, b)
|
|
|
|
|
if r == nil {
|
2020-10-19 09:45:49 +02:00
|
|
|
exit(errors.New(s.name + "/expect timeout"))
|
2020-10-18 18:34:22 +02:00
|
|
|
}
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:17:40 +02:00
|
|
|
func (s *streamCommon) open(name string, portNumber int) {
|
|
|
|
|
s.name = name
|
2020-10-17 23:53:33 +02:00
|
|
|
hostPort := fmt.Sprint(connectAddress, ":", portNumber)
|
2020-10-18 11:17:40 +02:00
|
|
|
log.Print(s.name+"/connecting to ", hostPort)
|
2020-10-17 23:53:33 +02:00
|
|
|
raddr, err := net.ResolveUDPAddr("udp", hostPort)
|
|
|
|
|
if err != nil {
|
2020-10-19 09:45:49 +02:00
|
|
|
exit(err)
|
2020-10-17 23:53:33 +02:00
|
|
|
}
|
2020-10-18 16:48:57 +02:00
|
|
|
|
2020-10-21 14:29:15 +02:00
|
|
|
s.conn, err = net.DialUDP("udp", nil, raddr)
|
2020-10-17 23:53:33 +02:00
|
|
|
if err != nil {
|
2020-10-19 09:45:49 +02:00
|
|
|
exit(err)
|
2020-10-17 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 12:50:09 +02:00
|
|
|
// Constructing the local session ID by combining the local IP address and port.
|
|
|
|
|
laddr := s.conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
|
s.localSID = binary.BigEndian.Uint32(laddr.IP[len(laddr.IP)-4:])<<16 | uint32(laddr.Port&0xffff)
|
|
|
|
|
|
2020-10-18 18:59:11 +02:00
|
|
|
_, err = rand.Read(s.pkt7.randIDBytes[:])
|
2020-10-18 12:50:09 +02:00
|
|
|
if err != nil {
|
2020-10-19 09:45:49 +02:00
|
|
|
exit(err)
|
2020-10-18 12:50:09 +02:00
|
|
|
}
|
2020-10-18 13:02:41 +02:00
|
|
|
|
|
|
|
|
s.readChan = make(chan []byte)
|
2020-10-18 19:41:19 +02:00
|
|
|
s.readerClosedChan = make(chan bool)
|
2020-10-18 13:02:41 +02:00
|
|
|
go s.reader()
|
2020-10-18 19:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *streamCommon) close() {
|
|
|
|
|
s.conn.Close()
|
|
|
|
|
|
|
|
|
|
// Depleting the read channel.
|
|
|
|
|
var finished bool
|
|
|
|
|
for !finished {
|
|
|
|
|
select {
|
|
|
|
|
case <-s.readChan:
|
|
|
|
|
default:
|
|
|
|
|
finished = true
|
|
|
|
|
}
|
2020-10-18 18:34:22 +02:00
|
|
|
}
|
2020-10-18 19:41:19 +02:00
|
|
|
|
|
|
|
|
// Waiting for the reader to finish.
|
|
|
|
|
<-s.readerClosedChan
|
2020-10-18 12:50:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *streamCommon) sendPkt3() {
|
|
|
|
|
s.send([]byte{0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
|
|
|
|
|
byte(s.localSID >> 24), byte(s.localSID >> 16), byte(s.localSID >> 8), byte(s.localSID),
|
|
|
|
|
byte(s.remoteSID >> 24), byte(s.remoteSID >> 16), byte(s.remoteSID >> 8), byte(s.remoteSID)})
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 13:52:38 +02:00
|
|
|
func (s *streamCommon) waitForPkt4Answer() {
|
|
|
|
|
log.Debug(s.name + "/expecting a pkt4 answer")
|
|
|
|
|
// Example answer from radio: 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x8c, 0x7d, 0x45, 0x7a, 0x1d, 0xf6, 0xe9, 0x0b
|
|
|
|
|
r := s.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00})
|
|
|
|
|
s.remoteSID = binary.BigEndian.Uint32(r[8:12])
|
2020-10-18 18:34:22 +02:00
|
|
|
s.gotRemoteSID = true
|
2020-10-18 13:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 12:50:09 +02:00
|
|
|
func (s *streamCommon) sendPkt6() {
|
|
|
|
|
s.send([]byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00,
|
|
|
|
|
byte(s.localSID >> 24), byte(s.localSID >> 16), byte(s.localSID >> 8), byte(s.localSID),
|
|
|
|
|
byte(s.remoteSID >> 24), byte(s.remoteSID >> 16), byte(s.remoteSID >> 8), byte(s.remoteSID)})
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 13:52:38 +02:00
|
|
|
func (s *streamCommon) waitForPkt6Answer() {
|
2020-10-18 14:21:58 +02:00
|
|
|
log.Debug(s.name + "/expecting pkt6 answer")
|
2020-10-18 13:52:38 +02:00
|
|
|
// Example answer from radio: 0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0xe8, 0xd0, 0x44, 0x50, 0xa0, 0x61, 0x39, 0xbe
|
|
|
|
|
s.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00})
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 13:19:52 +02:00
|
|
|
func (s *streamCommon) sendDisconnect() {
|
2020-10-19 09:53:49 +02:00
|
|
|
if !s.gotRemoteSID || s.conn == nil {
|
2020-10-18 18:34:22 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 09:53:49 +02:00
|
|
|
log.Print(s.name + "/disconnecting")
|
2020-10-18 13:19:52 +02:00
|
|
|
s.send([]byte{0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
|
|
|
|
|
byte(s.localSID >> 24), byte(s.localSID >> 16), byte(s.localSID >> 8), byte(s.localSID),
|
|
|
|
|
byte(s.remoteSID >> 24), byte(s.remoteSID >> 16), byte(s.remoteSID >> 8), byte(s.remoteSID)})
|
|
|
|
|
}
|