Multi-port refactor #1

Merged
josh merged 8 commits from multi-port-refactor into main 2024-05-04 17:06:20 +10:00
5 changed files with 19 additions and 12 deletions
Showing only changes of commit 298ae0935f - Show all commits

View file

@ -47,7 +47,7 @@ func (rtr *Router) HandleAEP(ctx context.Context, ddpkt *ddp.ExtPacket) error {
ddpkt.DstSocket, ddpkt.SrcSocket = ddpkt.SrcSocket, ddpkt.DstSocket ddpkt.DstSocket, ddpkt.SrcSocket = ddpkt.SrcSocket, ddpkt.DstSocket
ddpkt.Data[0] = byte(aep.EchoReply) ddpkt.Data[0] = byte(aep.EchoReply)
return rtr.Forward(ctx, ddpkt) return rtr.Output(ctx, ddpkt)
default: default:
return fmt.Errorf("invalid AEP function %d", ep.Function) return fmt.Errorf("invalid AEP function %d", ep.Function)

View file

@ -65,7 +65,7 @@ func (rtr *Router) handleNBPFwdReq(ctx context.Context, ddpkt *ddp.ExtPacket, nb
if err != nil || outDDP == nil { if err != nil || outDDP == nil {
return err return err
} }
if err := rtr.Forward(ctx, outDDP); err != nil { if err := rtr.Output(ctx, outDDP); err != nil {
return err return err
} }
} }
@ -200,7 +200,7 @@ func (port *EtherTalkPort) handleNBPBrRq(ctx context.Context, ddpkt *ddp.ExtPack
Data: nbpRaw, Data: nbpRaw,
} }
if err := port.Router.Forward(ctx, outDDP); err != nil { if err := port.Router.Output(ctx, outDDP); err != nil {
return err return err
} }
} }

View file

@ -142,7 +142,7 @@ func (rt *RouteTable) UpdateAURPRouteDistance(peer *AURPPeer, network ddp.Networ
} }
} }
func (rt *RouteTable) UpsertEthRoute(peer *EtherTalkPeer, extended bool, netStart, netEnd ddp.Network, metric uint8) error { func (rt *RouteTable) UpsertEtherTalkRoute(peer *EtherTalkPeer, extended bool, netStart, netEnd ddp.Network, metric uint8) error {
if netStart > netEnd { if netStart > netEnd {
return fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd) return fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd)
} }

View file

@ -30,9 +30,8 @@ type Router struct {
Ports []*EtherTalkPort Ports []*EtherTalkPort
} }
// Forward routes a packet towards the right destination. // Forward increments the hop count, then outputs the packet in the direction
// It increments the hop count, then looks up the best route for the network, // of the destination.
// then transmits the packet according to the route.
func (rtr *Router) Forward(ctx context.Context, ddpkt *ddp.ExtPacket) error { func (rtr *Router) Forward(ctx context.Context, ddpkt *ddp.ExtPacket) error {
// Check and adjust the Hop Count // Check and adjust the Hop Count
// Note the ddp package doesn't make this simple // Note the ddp package doesn't make this simple
@ -44,6 +43,12 @@ func (rtr *Router) Forward(ctx context.Context, ddpkt *ddp.ExtPacket) error {
ddpkt.Size &^= 0x3C00 ddpkt.Size &^= 0x3C00
ddpkt.Size |= hopCount << 10 ddpkt.Size |= hopCount << 10
return rtr.Output(ctx, ddpkt)
}
// Output outputs the packet in the direction of the destination.
// (It does not check or adjust the hop count.)
func (rtr *Router) Output(ctx context.Context, ddpkt *ddp.ExtPacket) error {
switch route := rtr.RouteTable.LookupRoute(ddpkt.DstNet); { switch route := rtr.RouteTable.LookupRoute(ddpkt.DstNet); {
case route == nil: case route == nil:
return fmt.Errorf("no route for packet (dstnet %d); dropping packet", ddpkt.DstNet) return fmt.Errorf("no route for packet (dstnet %d); dropping packet", ddpkt.DstNet)

View file

@ -67,7 +67,7 @@ func (port *EtherTalkPort) HandleRTMP(ctx context.Context, pkt *ddp.ExtPacket) e
Data: respPktRaw, Data: respPktRaw,
} }
if err := port.Router.Forward(ctx, ddpPkt); err != nil { if err := port.Router.Output(ctx, ddpPkt); err != nil {
return fmt.Errorf("send Response: %w", err) return fmt.Errorf("send Response: %w", err)
} }
@ -95,7 +95,7 @@ func (port *EtherTalkPort) HandleRTMP(ctx context.Context, pkt *ddp.ExtPacket) e
Data: dataPktRaw, Data: dataPktRaw,
} }
if err := port.Router.Forward(ctx, ddpPkt); err != nil { if err := port.Router.Output(ctx, ddpPkt); err != nil {
return fmt.Errorf("send Data: %w", err) return fmt.Errorf("send Data: %w", err)
} }
} }
@ -119,7 +119,7 @@ func (port *EtherTalkPort) HandleRTMP(ctx context.Context, pkt *ddp.ExtPacket) e
} }
for _, rt := range dataPkt.NetworkTuples { for _, rt := range dataPkt.NetworkTuples {
if err := port.Router.RouteTable.UpsertEthRoute(peer, rt.Extended, rt.RangeStart, rt.RangeEnd, rt.Distance+1); err != nil { if err := port.Router.RouteTable.UpsertEtherTalkRoute(peer, rt.Extended, rt.RangeStart, rt.RangeEnd, rt.Distance+1); err != nil {
log.Printf("RTMP: Couldn't upsert EtherTalk route: %v", err) log.Printf("RTMP: Couldn't upsert EtherTalk route: %v", err)
} }
} }
@ -150,7 +150,7 @@ func (port *EtherTalkPort) RunRTMP(ctx context.Context) (err error) {
log.Printf("RTMP: Couldn't broadcast Data: %v", err) log.Printf("RTMP: Couldn't broadcast Data: %v", err)
} }
setStatus("Starting packet loop") setStatus("Starting broadcast loop")
bcastTicker := time.NewTicker(10 * time.Second) bcastTicker := time.NewTicker(10 * time.Second)
defer bcastTicker.Stop() defer bcastTicker.Stop()
@ -163,7 +163,9 @@ func (port *EtherTalkPort) RunRTMP(ctx context.Context) (err error) {
case <-bcastTicker.C: case <-bcastTicker.C:
setStatus("Broadcasting RTMP Data") setStatus("Broadcasting RTMP Data")
if err := port.broadcastRTMPData(); err != nil { if err := port.broadcastRTMPData(); err != nil {
log.Printf("RTMP: Couldn't broadcast Data: %v", err) st := fmt.Sprintf("Couldn't broadcast Data: %v", err)
setStatus(st)
log.Print(st)
} }
} }
} }