dissertation-2-code/udp/flow.go

227 lines
4.0 KiB
Go
Raw Normal View History

2020-11-25 19:35:31 +00:00
package udp
import (
2020-11-26 22:10:37 +00:00
"errors"
"fmt"
"log"
2020-11-25 19:35:31 +00:00
"mpbl3p/proxy"
2020-11-26 22:10:37 +00:00
"mpbl3p/shared"
2020-11-25 19:35:31 +00:00
"net"
2020-11-26 18:55:29 +00:00
"sync"
2020-11-26 22:10:37 +00:00
"time"
2020-11-25 19:35:31 +00:00
)
type PacketWriter interface {
2020-11-26 22:39:07 +00:00
Write(b []byte) (int, error)
2020-11-26 22:10:37 +00:00
LocalAddr() net.Addr
2020-11-25 19:35:31 +00:00
}
type PacketConn interface {
PacketWriter
ReadFromUDP(b []byte) (int, *net.UDPAddr, error)
}
2020-11-26 18:55:29 +00:00
type InitiatedFlow struct {
2020-11-26 22:10:37 +00:00
Local string
2020-11-26 18:55:29 +00:00
Remote string
2020-11-25 19:35:31 +00:00
2020-11-26 22:10:37 +00:00
g proxy.MacGenerator
keepalive time.Duration
2020-11-26 18:55:29 +00:00
mu sync.RWMutex
Flow
2020-11-25 19:35:31 +00:00
}
2020-11-26 22:10:37 +00:00
func (f *InitiatedFlow) String() string {
return fmt.Sprintf("UdpOutbound{%v -> %v}", f.Local, f.Remote)
}
2020-11-25 19:35:31 +00:00
type Flow struct {
writer PacketWriter
raddr net.UDPAddr
isAlive bool
congestion Congestion
2020-11-26 18:55:29 +00:00
2020-11-26 22:15:22 +00:00
v proxy.MacVerifier
2020-11-26 18:55:29 +00:00
inboundDatagrams chan []byte
}
2020-11-26 22:10:37 +00:00
func (f Flow) String() string {
return fmt.Sprintf("UdpInbound{%v -> %v}", f.raddr, f.writer.LocalAddr())
}
func InitiateFlow(
local, remote string,
2020-11-26 22:15:22 +00:00
v proxy.MacVerifier,
2020-11-26 22:10:37 +00:00
g proxy.MacGenerator,
c Congestion,
keepalive time.Duration,
) (*InitiatedFlow, error) {
f := InitiatedFlow{
Local: local,
Remote: remote,
2020-11-26 22:15:22 +00:00
Flow: newFlow(c, v),
2020-11-26 22:10:37 +00:00
g: g,
keepalive: keepalive,
2020-11-26 18:55:29 +00:00
}
2020-11-26 22:10:37 +00:00
return &f, nil
2020-11-26 18:55:29 +00:00
}
2020-11-26 22:15:22 +00:00
func newFlow(c Congestion, v proxy.MacVerifier) Flow {
2020-11-26 22:10:37 +00:00
return Flow{
2020-11-26 18:55:29 +00:00
inboundDatagrams: make(chan []byte),
2020-11-26 22:10:37 +00:00
congestion: c,
2020-11-26 22:15:22 +00:00
v: v,
2020-11-26 18:55:29 +00:00
}
}
2020-11-26 22:10:37 +00:00
func (f *InitiatedFlow) Reconnect() error {
f.mu.Lock()
defer f.mu.Unlock()
if f.isAlive {
return nil
2020-11-26 18:55:29 +00:00
}
2020-11-26 22:10:37 +00:00
localAddr, err := net.ResolveUDPAddr("udp", f.Local)
if err != nil {
return err
}
remoteAddr, err := net.ResolveUDPAddr("udp", f.Remote)
if err != nil {
return err
}
conn, err := net.DialUDP("udp", localAddr, remoteAddr)
if err != nil {
return err
}
f.writer = conn
f.isAlive = true
go func() {
for {
buf := make([]byte, 6000)
n, _, err := conn.ReadFromUDP(buf)
if err != nil {
panic(err)
}
f.inboundDatagrams <- buf[:n]
}
}()
go func() {
var err error
for !errors.Is(err, shared.ErrDeadConnection) {
f.congestion.AwaitEarlyUpdate(f.keepalive)
err = f.Consume(proxy.NewSimplePacket(nil), f.g)
}
}()
return nil
}
func (f *InitiatedFlow) Consume(p proxy.Packet, g proxy.MacGenerator) error {
f.mu.RLock()
defer f.mu.RUnlock()
return f.Flow.Consume(p, g)
}
func (f *InitiatedFlow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
f.mu.RLock()
defer f.mu.RUnlock()
return f.Flow.Produce(v)
2020-11-25 19:35:31 +00:00
}
func (f *Flow) IsAlive() bool {
return f.isAlive
}
func (f *Flow) Consume(pp proxy.Packet, g proxy.MacGenerator) error {
2020-11-26 22:10:37 +00:00
if !f.isAlive {
return shared.ErrDeadConnection
}
// Sequence is the congestion controllers opportunity to block
2020-11-25 19:35:31 +00:00
p := Packet{
seq: f.congestion.Sequence(),
data: pp,
}
p.ack = f.congestion.NextAck()
p.nack = f.congestion.NextNack()
2020-11-26 18:55:29 +00:00
b := p.Marshal()
b = proxy.AppendMac(b, g)
2020-11-26 22:39:07 +00:00
_, err := f.writer.Write(b)
2020-11-26 18:55:29 +00:00
return err
2020-11-25 19:35:31 +00:00
}
func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
2020-11-26 22:10:37 +00:00
if !f.isAlive {
return nil, shared.ErrDeadConnection
}
2020-11-26 18:55:29 +00:00
b, err := proxy.StripMac(<-f.inboundDatagrams, v)
if err != nil {
return nil, err
}
p, err := UnmarshalPacket(b)
if err != nil {
return nil, err
}
// schedule an ack for this sequence number
f.congestion.ReceivedPacket(p.seq)
// adjust our sending congestion control based on their acks
if p.ack != 0 {
f.congestion.ReceivedAck(p.ack)
}
// adjust our sending congestion control based on their nacks
if p.nack != 0 {
f.congestion.ReceivedNack(p.nack)
}
return p, nil
2020-11-25 19:35:31 +00:00
}
func (f *Flow) handleDatagram(p []byte) {
2020-11-26 22:10:37 +00:00
// TODO: Fix with security
// 12 bytes for header + the MAC + a timestamp
2020-11-26 22:15:22 +00:00
if len(p) == 12+f.v.CodeLength()+8 {
b, err := proxy.StripMac(<-f.inboundDatagrams, f.v)
2020-11-26 22:10:37 +00:00
if err != nil {
log.Println(err)
return
}
p, err := UnmarshalPacket(b)
if err != nil {
log.Println(err)
return
}
// TODO: Decide whether to use this line. It means an ACK loop will start, but also is a packet loss.
f.congestion.ReceivedPacket(p.seq)
if p.ack != 0 {
f.congestion.ReceivedAck(p.ack)
}
if p.nack != 0 {
f.congestion.ReceivedNack(p.nack)
}
} else {
f.inboundDatagrams <- p
}
2020-11-25 19:35:31 +00:00
}