From d5178777202d057a69d76f3ec0c1aa43a55e2503 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 18 Sep 2023 12:28:04 +0500 Subject: [PATCH] Packet-byte handling, protocol struct to handle protocol states --- srt/intake.go | 6 ++++-- srt/packet.go | 8 ++++++++ srt/protocol.go | 20 ++++++++++++++++++++ srt/tunnel.go | 34 +++++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 srt/protocol.go diff --git a/srt/intake.go b/srt/intake.go index ab0b0e1..28cd985 100644 --- a/srt/intake.go +++ b/srt/intake.go @@ -27,7 +27,7 @@ func (intake *Intake) NewTunnel(l net.PacketConn, peer net.Addr) (*Tunnel) { tunnel := new(Tunnel) tunnel.socket = l tunnel.peer = peer - tunnel.queue = make(chan *Packet, 10) + tunnel.queue = make(chan []byte, 10) return tunnel } return nil @@ -61,5 +61,7 @@ func (intake *Intake) Read() { return } fmt.Println(string(intake.buffer[:n])) - tunnel.queue <- &(Packet{intake.buffer[:n]}) + pkt := make([]byte, n) + copy(pkt, intake.buffer[:n]) + tunnel.queue <- pkt } diff --git a/srt/packet.go b/srt/packet.go index 512f481..4503151 100644 --- a/srt/packet.go +++ b/srt/packet.go @@ -6,3 +6,11 @@ import ( type Packet struct { raw []byte } + +func MarshallPacket(packet *Packet) ([]byte, error) { + return packet.raw, nil +} + +func ParsePacket(buffer []byte) (*Packet, error) { + return &Packet{buffer}, nil +} diff --git a/srt/protocol.go b/srt/protocol.go new file mode 100644 index 0000000..fabcda2 --- /dev/null +++ b/srt/protocol.go @@ -0,0 +1,20 @@ +package srt + +import ( + "fmt" +) + +const ( + INDUCTION uint8 = iota + CONCLUSION + DATA_LOOP +) + +type SRTManager struct { + state uint8 +} + +func (proto *SRTManager) Decide(packet *Packet) (*Packet, error) { + fmt.Println(*packet) + return nil, nil +} diff --git a/srt/tunnel.go b/srt/tunnel.go index e2427ee..8c4e4ff 100644 --- a/srt/tunnel.go +++ b/srt/tunnel.go @@ -7,6 +7,38 @@ import ( type Tunnel struct { socket net.PacketConn peer net.Addr - queue chan *Packet + queue chan []byte broken bool + state *SRTManager +} + +func (tunnel *Tunnel) Start() { + tunnel.state = new(SRTManager) + for { + packet, err := tunnel.ReadPacket() + if err != nil { + tunnel.broken = true + } + response, err := tunnel.state.Decide(packet) + if err != nil { + tunnel.broken = true + } + if response != nil { + tunnel.WritePacket(response) + } + } +} + +func (tunnel *Tunnel) WritePacket(packet *Packet) { + buffer, err := MarshallPacket(packet) + if err != nil { + tunnel.broken = true + return + } + tunnel.socket.WriteTo(buffer, tunnel.peer) +} + +func (tunnel *Tunnel) ReadPacket() (*Packet, error) { + packet := <- tunnel.queue + return ParsePacket(packet) }