jrouter/router/rtmp.go

289 lines
7.8 KiB
Go
Raw Permalink Normal View History

2024-04-08 09:21:35 +10:00
/*
Copyright 2024 Josh Deprez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2024-04-19 14:57:25 +10:00
package router
2024-04-07 15:14:54 +10:00
import (
"context"
2024-04-07 17:01:26 +10:00
"fmt"
2024-04-07 15:14:54 +10:00
"log"
"time"
2024-10-18 13:20:34 +11:00
"drjosh.dev/jrouter/atalk"
"drjosh.dev/jrouter/atalk/rtmp"
"drjosh.dev/jrouter/atalk/zip"
"drjosh.dev/jrouter/status"
2024-04-07 15:14:54 +10:00
"github.com/sfiera/multitalk/pkg/ddp"
)
// RTMPMachine implements RTMP on an AppleTalk network attached to the router.
2024-05-04 14:48:47 +10:00
func (port *EtherTalkPort) HandleRTMP(ctx context.Context, pkt *ddp.ExtPacket) error {
switch pkt.Proto {
case ddp.ProtoRTMPReq:
// I can answer RTMP requests!
req, err := rtmp.UnmarshalRequestPacket(pkt.Data)
if err != nil {
return fmt.Errorf("unmarshal Request packet: %w", err)
}
2024-05-03 16:13:59 +10:00
2024-05-04 14:48:47 +10:00
switch req.Function {
case rtmp.FunctionRequest:
// Respond with RTMP Response
respPkt := &rtmp.ResponsePacket{
SenderAddr: port.MyAddr,
Extended: true,
RangeStart: port.NetStart,
RangeEnd: port.NetEnd,
}
respPktRaw, err := respPkt.Marshal()
if err != nil {
return fmt.Errorf("marshal RTMP Response packet: %w", err)
}
ddpPkt := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(respPktRaw)) + atalk.DDPExtHeaderSize,
Cksum: 0,
DstNet: pkt.SrcNet,
DstNode: pkt.SrcNode,
DstSocket: 1, // the RTMP socket
SrcNet: port.MyAddr.Network,
SrcNode: port.MyAddr.Node,
SrcSocket: 1, // the RTMP socket
Proto: ddp.ProtoRTMPResp,
},
Data: respPktRaw,
}
2024-05-04 15:36:33 +10:00
if err := port.Router.Output(ctx, ddpPkt); err != nil {
2024-05-04 14:48:47 +10:00
return fmt.Errorf("send Response: %w", err)
}
case rtmp.FunctionRDRSplitHorizon, rtmp.FunctionRDRComplete:
// Like the Data broadcast, but solicited by a request (RDR).
splitHorizon := req.Function == rtmp.FunctionRDRSplitHorizon
for _, dataPkt := range port.rtmpDataPackets(splitHorizon) {
dataPktRaw, err := dataPkt.Marshal()
if err != nil {
return fmt.Errorf("marshal RTMP Data packet: %w", err)
}
ddpPkt := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(dataPktRaw)) + atalk.DDPExtHeaderSize,
Cksum: 0,
DstNet: pkt.SrcNet,
DstNode: pkt.SrcNode,
DstSocket: 1, // the RTMP socket
SrcNet: port.MyAddr.Network,
SrcNode: port.MyAddr.Node,
SrcSocket: 1, // the RTMP socket
Proto: ddp.ProtoRTMPResp,
},
Data: dataPktRaw,
}
2024-05-03 16:13:59 +10:00
2024-05-04 15:36:33 +10:00
if err := port.Router.Output(ctx, ddpPkt); err != nil {
2024-05-04 14:48:47 +10:00
return fmt.Errorf("send Data: %w", err)
}
}
case rtmp.FunctionLoopProbe:
2024-05-05 18:13:40 +10:00
return fmt.Errorf("TODO: handle Loop Probes")
2024-05-04 14:48:47 +10:00
}
case ddp.ProtoRTMPResp:
// It's a peer router on the AppleTalk network!
log.Print("RTMP: Got Response or Data")
dataPkt, err := rtmp.UnmarshalDataPacket(pkt.Data)
if err != nil {
2024-05-05 17:59:49 +10:00
return fmt.Errorf("unmarshal RTMP Data packet: %w", err)
2024-05-04 14:48:47 +10:00
}
peer := &EtherTalkPeer{
Port: port,
PeerAddr: dataPkt.RouterAddr,
}
2024-05-05 17:59:49 +10:00
var noZones []ddp.Network
for _, nt := range dataPkt.NetworkTuples {
route, err := port.Router.RouteTable.UpsertEtherTalkRoute(peer, nt.Extended, nt.RangeStart, nt.RangeEnd, nt.Distance+1)
if err != nil {
2024-05-05 18:13:40 +10:00
return fmt.Errorf("upsert EtherTalk route: %v", err)
2024-05-04 14:48:47 +10:00
}
2024-05-05 17:59:49 +10:00
if len(route.ZoneNames) == 0 {
noZones = append(noZones, route.NetStart)
}
}
if len(noZones) > 0 {
// Send a ZIP Query for all networks we don't have zone names for.
// TODO: split networks to fit in multiple packets as needed
qryPkt, err := (&zip.QueryPacket{Networks: noZones}).Marshal()
if err != nil {
return fmt.Errorf("marshal ZIP Query packet: %w", err)
}
outDDP := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(qryPkt)) + atalk.DDPExtHeaderSize,
Cksum: 0,
SrcNet: port.MyAddr.Network,
SrcNode: port.MyAddr.Node,
SrcSocket: 6,
DstNet: pkt.SrcNet,
DstNode: pkt.SrcNode,
DstSocket: 6, // ZIP socket
Proto: ddp.ProtoZIP,
},
Data: qryPkt,
}
2024-05-05 18:13:40 +10:00
if err := port.Send(ctx, outDDP); err != nil {
return fmt.Errorf("sending ZIP Query: %w", err)
}
2024-05-04 14:48:47 +10:00
}
default:
2024-05-05 18:13:40 +10:00
return fmt.Errorf("invalid DDP type %d on socket 1", pkt.Proto)
2024-05-03 16:13:59 +10:00
}
2024-05-04 14:48:47 +10:00
return nil
2024-04-07 15:14:54 +10:00
}
2024-05-04 14:48:47 +10:00
// RunRTMP makes periodic RTMP Data broadcasts on this port.
func (port *EtherTalkPort) RunRTMP(ctx context.Context) (err error) {
2024-05-03 16:13:59 +10:00
ctx, setStatus, _ := status.AddSimpleItem(ctx, "RTMP")
defer func() {
setStatus(fmt.Sprintf("Run loop stopped! Return: %v", err))
}()
2024-04-23 14:17:41 +10:00
setStatus("Awaiting DDP address assignment")
2024-04-07 17:01:26 +10:00
// Await local address assignment before doing anything
2024-05-04 14:48:47 +10:00
<-port.AARPMachine.Assigned()
2024-04-07 17:01:26 +10:00
2024-04-23 14:17:41 +10:00
setStatus("Initial RTMP Data broadcast")
2024-04-07 17:04:30 +10:00
// Initial broadcast
2024-05-04 14:48:47 +10:00
if err := port.broadcastRTMPData(); err != nil {
2024-04-07 17:04:30 +10:00
log.Printf("RTMP: Couldn't broadcast Data: %v", err)
}
2024-05-04 15:36:33 +10:00
setStatus("Starting broadcast loop")
2024-04-23 14:17:41 +10:00
2024-04-07 15:14:54 +10:00
bcastTicker := time.NewTicker(10 * time.Second)
defer bcastTicker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-bcastTicker.C:
2024-05-03 16:13:59 +10:00
setStatus("Broadcasting RTMP Data")
2024-05-04 14:48:47 +10:00
if err := port.broadcastRTMPData(); err != nil {
2024-05-04 15:36:33 +10:00
st := fmt.Sprintf("Couldn't broadcast Data: %v", err)
setStatus(st)
log.Print(st)
2024-04-07 15:14:54 +10:00
}
}
}
}
2024-04-07 16:31:19 +10:00
2024-05-04 14:48:47 +10:00
func (port *EtherTalkPort) broadcastRTMPData() error {
for _, dataPkt := range port.rtmpDataPackets(true) {
dataPktRaw, err := dataPkt.Marshal()
if err != nil {
return fmt.Errorf("marshal Data packet: %v", err)
}
2024-04-07 17:04:30 +10:00
ddpPkt := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(dataPktRaw)) + atalk.DDPExtHeaderSize,
Cksum: 0,
2024-05-04 14:48:47 +10:00
DstNet: 0x0000, // this network
DstNode: 0xff, // broadcast packet
DstSocket: 1, // the RTMP socket
SrcNet: port.MyAddr.Network,
SrcNode: port.MyAddr.Node,
SrcSocket: 1, // the RTMP socket
Proto: ddp.ProtoRTMPResp,
},
Data: dataPktRaw,
}
2024-04-07 17:04:30 +10:00
2024-05-04 14:48:47 +10:00
if err := port.Broadcast(ddpPkt); err != nil {
return err
}
2024-04-07 17:04:30 +10:00
}
return nil
2024-04-07 17:04:30 +10:00
}
2024-05-04 14:48:47 +10:00
func (port *EtherTalkPort) rtmpDataPackets(splitHorizon bool) []*rtmp.DataPacket {
// Build up a slice of routing tuples.
2024-05-04 14:48:47 +10:00
routes := port.Router.RouteTable.ValidRoutes()
tuples := make([]rtmp.NetworkTuple, 0, len(routes))
for _, rt := range routes {
2024-05-04 14:48:47 +10:00
if rt.EtherTalkDirect == port {
// If the route is actually a direct connection to this port,
// don't include it.
// (It's manually set as the first tuple anyway.)
continue
}
2024-05-04 16:16:17 +10:00
if splitHorizon && rt.EtherTalkPeer != nil && rt.EtherTalkPeer.Port == port {
2024-05-04 14:48:47 +10:00
// If the route is through a peer accessible on this port, don't
// include it.
continue
}
tuples = append(tuples, rtmp.NetworkTuple{
2024-04-14 17:13:12 +10:00
Extended: rt.Extended,
RangeStart: rt.NetStart,
RangeEnd: rt.NetEnd,
2024-04-26 13:22:48 +10:00
Distance: rt.Distance,
2024-04-13 15:47:58 +10:00
})
}
// "The first tuple in RTMP Data packets sent on extended
// networks ... indicates the network number range assigned
// to that network."
// TODO: support non-extended local networks (LocalTalk)
first := rtmp.NetworkTuple{
Extended: true,
2024-05-04 14:48:47 +10:00
RangeStart: port.NetStart,
RangeEnd: port.NetEnd,
Distance: 0,
}
var packets []*rtmp.DataPacket
rem := tuples
for len(rem) > 0 {
chunk := []rtmp.NetworkTuple{first}
size := 10 // router network + 1 + router node ID + first tuple
for _, nt := range rem {
size += nt.Size()
if size > atalk.DDPMaxDataSize {
break
}
chunk = append(chunk, nt)
}
rem = rem[len(chunk)-1:]
packets = append(packets, &rtmp.DataPacket{
2024-05-04 14:48:47 +10:00
RouterAddr: port.MyAddr,
Extended: true,
NetworkTuples: chunk,
})
}
return packets
2024-04-07 16:31:19 +10:00
}