2024-03-25 19:30:06 +11:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-03-30 14:13:34 +11:00
|
|
|
"context"
|
2024-03-25 19:30:06 +11:00
|
|
|
"log"
|
|
|
|
"net"
|
2024-03-30 14:13:34 +11:00
|
|
|
"time"
|
2024-03-25 19:30:06 +11:00
|
|
|
|
|
|
|
"gitea.drjosh.dev/josh/jrouter/aurp"
|
|
|
|
)
|
|
|
|
|
2024-03-30 16:43:14 +11:00
|
|
|
const (
|
|
|
|
// TODO: check these parameters
|
|
|
|
lastHeardFromTimer = 10 * time.Second
|
|
|
|
lastHeardFromRetryLimit = 10
|
|
|
|
sendRetryTimer = 10 * time.Second
|
|
|
|
sendRetryLimit = 5
|
|
|
|
)
|
|
|
|
|
2024-03-30 16:14:18 +11:00
|
|
|
type receiverState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
receiverStateUnconnected receiverState = iota
|
|
|
|
receiverStateConnected
|
|
|
|
receiverStateWaitForOpenRsp
|
|
|
|
receiverStateWaitForRIRsp
|
|
|
|
receiverStateWaitForTickleAck
|
|
|
|
)
|
|
|
|
|
|
|
|
type senderState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
senderStateUnconnected senderState = iota
|
|
|
|
senderStateConnected
|
|
|
|
senderStateWaitForRIAck1
|
|
|
|
senderStateWaitForRIAck2
|
|
|
|
senderStateWaitForRIAck3
|
|
|
|
)
|
|
|
|
|
2024-03-25 19:30:06 +11:00
|
|
|
type peer struct {
|
|
|
|
tr *aurp.Transport
|
|
|
|
conn *net.UDPConn
|
|
|
|
raddr *net.UDPAddr
|
|
|
|
recv chan aurp.Packet
|
|
|
|
}
|
|
|
|
|
|
|
|
// send encodes and sends pkt to the remote host.
|
|
|
|
func (p *peer) send(pkt aurp.Packet) (int, error) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := pkt.WriteTo(&b); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-03-30 16:43:14 +11:00
|
|
|
log.Printf("Sending %T (len %d) to %v", pkt, b.Len(), p.raddr)
|
2024-03-25 19:30:06 +11:00
|
|
|
return p.conn.WriteToUDP(b.Bytes(), p.raddr)
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:13:34 +11:00
|
|
|
func (p *peer) handle(ctx context.Context) error {
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2024-03-30 15:26:23 +11:00
|
|
|
lastHeardFrom := time.Now()
|
2024-03-30 16:43:14 +11:00
|
|
|
lastSend := time.Now()
|
|
|
|
sendRetries := 0
|
2024-03-30 14:24:16 +11:00
|
|
|
|
2024-03-30 16:14:18 +11:00
|
|
|
rstate := receiverStateUnconnected
|
|
|
|
sstate := senderStateUnconnected
|
|
|
|
|
2024-03-25 19:30:06 +11:00
|
|
|
// Write an Open-Req packet
|
2024-03-30 16:43:14 +11:00
|
|
|
if _, err := p.send(p.tr.NewOpenReqPacket(nil)); err != nil {
|
2024-03-25 19:30:06 +11:00
|
|
|
log.Printf("Couldn't send Open-Req packet: %v", err)
|
2024-03-30 14:13:34 +11:00
|
|
|
return err
|
2024-03-25 19:30:06 +11:00
|
|
|
}
|
|
|
|
|
2024-03-30 16:14:18 +11:00
|
|
|
rstate = receiverStateWaitForOpenRsp
|
|
|
|
|
2024-03-30 14:13:34 +11:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-03-30 16:48:47 +11:00
|
|
|
if sstate == senderStateUnconnected {
|
2024-03-30 16:14:18 +11:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
2024-03-30 14:24:16 +11:00
|
|
|
// Send a best-effort Router Down before returning
|
|
|
|
if _, err := p.send(p.tr.NewRDPacket(aurp.ErrCodeNormalClose)); err != nil {
|
|
|
|
log.Printf("Couldn't send RD packet: %v", err)
|
|
|
|
}
|
2024-03-30 14:13:34 +11:00
|
|
|
return ctx.Err()
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2024-03-30 16:43:14 +11:00
|
|
|
switch rstate {
|
|
|
|
case receiverStateWaitForOpenRsp:
|
|
|
|
if time.Since(lastSend) <= sendRetryTimer {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if sendRetries >= sendRetryLimit {
|
|
|
|
log.Printf("Send retry limit reached while waiting for Open-Rsp, closing connection")
|
|
|
|
rstate = receiverStateUnconnected
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send another Open-Req
|
|
|
|
sendRetries++
|
2024-03-30 16:48:47 +11:00
|
|
|
lastSend = time.Now()
|
2024-03-30 16:43:14 +11:00
|
|
|
if _, err := p.send(p.tr.NewOpenReqPacket(nil)); err != nil {
|
|
|
|
log.Printf("Couldn't send Open-Req packet: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case receiverStateConnected:
|
2024-03-30 16:14:18 +11:00
|
|
|
// Check LHFT, send tickle?
|
2024-03-30 16:43:14 +11:00
|
|
|
if time.Since(lastHeardFrom) > lastHeardFromTimer {
|
2024-03-30 16:14:18 +11:00
|
|
|
if _, err := p.send(p.tr.NewTicklePacket()); err != nil {
|
|
|
|
log.Printf("Couldn't send Tickle: %v", err)
|
|
|
|
}
|
2024-03-30 14:13:34 +11:00
|
|
|
}
|
2024-03-30 16:14:18 +11:00
|
|
|
rstate = receiverStateWaitForTickleAck
|
2024-03-25 19:30:06 +11:00
|
|
|
}
|
|
|
|
|
2024-03-30 14:13:34 +11:00
|
|
|
case pkt := <-p.recv:
|
2024-03-30 15:26:23 +11:00
|
|
|
lastHeardFrom = time.Now()
|
|
|
|
|
2024-03-30 14:13:34 +11:00
|
|
|
switch pkt := pkt.(type) {
|
|
|
|
case *aurp.OpenReqPacket:
|
2024-03-30 16:14:18 +11:00
|
|
|
if sstate != senderStateUnconnected {
|
|
|
|
log.Printf("Open-Req received but sender state is not Unconnected (was %d); ignoring packet", sstate)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:13:34 +11:00
|
|
|
// The peer tells us their connection ID in Open-Req.
|
|
|
|
p.tr.RemoteConnID = pkt.ConnectionID
|
|
|
|
|
|
|
|
// Formulate a response.
|
|
|
|
var orsp *aurp.OpenRspPacket
|
|
|
|
switch {
|
|
|
|
case pkt.Version != 1:
|
|
|
|
// Respond with Open-Rsp with unknown version error.
|
|
|
|
orsp = p.tr.NewOpenRspPacket(0, int16(aurp.ErrCodeInvalidVersion), nil)
|
|
|
|
|
|
|
|
case len(pkt.Options) > 0:
|
|
|
|
// Options? OPTIONS? We don't accept no stinkin' _options_
|
|
|
|
orsp = p.tr.NewOpenRspPacket(0, int16(aurp.ErrCodeOptionNegotiation), nil)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Accept it I guess.
|
|
|
|
orsp = p.tr.NewOpenRspPacket(0, 1, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Responding with %T", orsp)
|
|
|
|
|
|
|
|
if _, err := p.send(orsp); err != nil {
|
|
|
|
log.Printf("Couldn't send Open-Rsp: %v", err)
|
|
|
|
}
|
2024-03-30 16:14:18 +11:00
|
|
|
if orsp.RateOrErrCode >= 0 {
|
|
|
|
sstate = senderStateConnected
|
|
|
|
} else {
|
|
|
|
sstate = senderStateUnconnected
|
|
|
|
}
|
2024-03-30 14:13:34 +11:00
|
|
|
|
|
|
|
case *aurp.OpenRspPacket:
|
2024-03-30 16:14:18 +11:00
|
|
|
if rstate != receiverStateWaitForOpenRsp {
|
|
|
|
log.Printf("Received Open-Rsp but was not waiting for one (receiver state was %d)", rstate)
|
|
|
|
}
|
2024-03-30 14:13:34 +11:00
|
|
|
if pkt.RateOrErrCode < 0 {
|
|
|
|
// It's an error code.
|
|
|
|
log.Printf("Open-Rsp error code from peer %v: %d", p.raddr.IP, pkt.RateOrErrCode)
|
|
|
|
// Close the connection
|
2024-03-30 16:14:18 +11:00
|
|
|
rstate = receiverStateUnconnected
|
2024-03-30 14:13:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Make other requests
|
|
|
|
|
|
|
|
case *aurp.RIReqPacket:
|
|
|
|
// TODO: Respond with RI-Rsp
|
|
|
|
|
|
|
|
case *aurp.RIRspPacket:
|
|
|
|
// TODO: Repsond with RI-Ack
|
|
|
|
// TODO: Integrate info into route table
|
|
|
|
|
|
|
|
case *aurp.RIAckPacket:
|
|
|
|
// TODO: Continue sending next RI-Rsp (streamed)
|
|
|
|
// TODO: If SZI flag is set, send ZI-Rsp (transaction)
|
|
|
|
|
|
|
|
case *aurp.RIUpdPacket:
|
|
|
|
// TODO: Integrate info into route table
|
|
|
|
|
|
|
|
case *aurp.RDPacket:
|
2024-03-30 16:16:46 +11:00
|
|
|
// TODO: Remove router from route tables
|
|
|
|
|
2024-03-30 14:13:34 +11:00
|
|
|
log.Printf("Router Down: error code %d %s", pkt.ErrorCode, pkt.ErrorCode)
|
2024-03-30 16:14:18 +11:00
|
|
|
// Respond with RI-Ack
|
|
|
|
if _, err := p.send(p.tr.NewRIAckPacket(pkt.ConnectionID, pkt.Sequence, 0)); err != nil {
|
|
|
|
log.Printf("Couldn't send RI-Ack: %v", err)
|
|
|
|
}
|
2024-03-30 16:16:46 +11:00
|
|
|
// Connection closed
|
2024-03-30 16:14:18 +11:00
|
|
|
rstate = receiverStateUnconnected
|
2024-03-30 14:13:34 +11:00
|
|
|
|
|
|
|
case *aurp.ZIReqPacket:
|
|
|
|
// TODO: Respond with ZI-Rsp
|
|
|
|
|
|
|
|
case *aurp.ZIRspPacket:
|
|
|
|
// TODO: Integrate info into zone table
|
|
|
|
|
|
|
|
case *aurp.TicklePacket:
|
2024-03-30 15:26:23 +11:00
|
|
|
// Immediately respond with Tickle-Ack
|
2024-03-30 14:13:34 +11:00
|
|
|
if _, err := p.send(p.tr.NewTickleAckPacket()); err != nil {
|
|
|
|
log.Printf("Couldn't send Tickle-Ack: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *aurp.TickleAckPacket:
|
2024-03-30 16:14:18 +11:00
|
|
|
if rstate != receiverStateWaitForTickleAck {
|
|
|
|
log.Printf("Received Tickle-Ack but was not waiting for one (receever state was %d)", rstate)
|
|
|
|
}
|
|
|
|
rstate = receiverStateConnected
|
|
|
|
|
2024-03-25 19:30:06 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|