2020-10-17 23:53:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/nonoo/kappanhang/log"
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
type streamCommon struct {
|
2020-10-18 11:17:40 +02:00
|
|
|
name string
|
2020-10-17 23:53:33 +02:00
|
|
|
conn *net.UDPConn
|
|
|
|
|
localSID uint32
|
|
|
|
|
remoteSID uint32
|
|
|
|
|
sendSeq uint16
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
func (s *streamCommon) read() ([]byte, error) {
|
|
|
|
|
err := s.conn.SetReadDeadline(time.Now().Add(time.Second))
|
2020-10-17 23:53:33 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b := make([]byte, 1500)
|
2020-10-18 11:01:53 +02:00
|
|
|
n, _, err := s.conn.ReadFromUDP(b)
|
2020-10-17 23:53:33 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return b[:n], err
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
func (s *streamCommon) reader(c chan []byte) {
|
2020-10-18 10:53:16 +02:00
|
|
|
var errCount int
|
|
|
|
|
for {
|
2020-10-18 11:01:53 +02:00
|
|
|
r, err := s.read()
|
2020-10-18 10:53:16 +02:00
|
|
|
if err == nil {
|
|
|
|
|
c <- r
|
|
|
|
|
} else {
|
|
|
|
|
errCount++
|
|
|
|
|
if errCount > 5 {
|
2020-10-18 11:17:40 +02:00
|
|
|
log.Fatal(s.name + "/timeout")
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
2020-10-18 11:17:40 +02:00
|
|
|
log.Error(s.name + "/stream break detected")
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
|
|
|
|
errCount = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
func (s *streamCommon) expect(packetLength int, b []byte) []byte {
|
2020-10-17 23:53:33 +02:00
|
|
|
var r []byte
|
|
|
|
|
expectStart := time.Now()
|
|
|
|
|
for {
|
2020-10-18 11:01:53 +02:00
|
|
|
r, _ = s.read()
|
2020-10-17 23:53:33 +02:00
|
|
|
if len(r) == packetLength && bytes.Equal(r[:len(b)], b) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if time.Since(expectStart) > time.Second {
|
|
|
|
|
log.Fatal("expect timeout")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
laddr := net.UDPAddr{
|
|
|
|
|
Port: portNumber,
|
|
|
|
|
}
|
2020-10-18 11:01:53 +02:00
|
|
|
s.conn, err = net.DialUDP("udp", &laddr, raddr)
|
2020-10-17 23:53:33 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
s.localSID = uint32(time.Now().Unix())
|
2020-10-18 11:17:40 +02:00
|
|
|
log.Debugf(s.name+"/using session id %.8x", s.localSID)
|
2020-10-17 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
func (p *streamCommon) sendPkt3() {
|
2020-10-17 23:53:33 +02:00
|
|
|
p.send([]byte{0x10, 0x00, 0x00, 0x00, 0x03, 0x00, byte(p.sendSeq), byte(p.sendSeq >> 8),
|
|
|
|
|
byte(p.localSID >> 24), byte(p.localSID >> 16), byte(p.localSID >> 8), byte(p.localSID),
|
|
|
|
|
byte(p.remoteSID >> 24), byte(p.remoteSID >> 16), byte(p.remoteSID >> 8), byte(p.remoteSID)})
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
func (p *streamCommon) sendPkt6() {
|
2020-10-17 23:53:33 +02:00
|
|
|
p.send([]byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00,
|
|
|
|
|
byte(p.localSID >> 24), byte(p.localSID >> 16), byte(p.localSID >> 8), byte(p.localSID),
|
|
|
|
|
byte(p.remoteSID >> 24), byte(p.remoteSID >> 16), byte(p.remoteSID >> 8), byte(p.remoteSID)})
|
|
|
|
|
}
|