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-04-15 10:16:57 +10:00
|
|
|
"gitea.drjosh.dev/josh/jrouter/atalk"
|
2024-04-07 15:14:54 +10:00
|
|
|
"gitea.drjosh.dev/josh/jrouter/atalk/rtmp"
|
2024-04-23 14:17:41 +10:00
|
|
|
"gitea.drjosh.dev/josh/jrouter/status"
|
2024-04-07 15:14:54 +10:00
|
|
|
"github.com/google/gopacket/pcap"
|
2024-04-07 17:04:30 +10:00
|
|
|
"github.com/sfiera/multitalk/pkg/aarp"
|
2024-04-07 15:14:54 +10:00
|
|
|
"github.com/sfiera/multitalk/pkg/ddp"
|
2024-04-07 16:31:19 +10:00
|
|
|
"github.com/sfiera/multitalk/pkg/ethernet"
|
2024-04-07 15:14:54 +10:00
|
|
|
"github.com/sfiera/multitalk/pkg/ethertalk"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RTMPMachine implements RTMP on an AppleTalk network attached to the router.
|
|
|
|
type RTMPMachine struct {
|
2024-04-19 14:57:25 +10:00
|
|
|
AARP *AARPMachine
|
|
|
|
Config *Config
|
|
|
|
PcapHandle *pcap.Handle
|
|
|
|
RoutingTable *RoutingTable
|
2024-04-07 15:14:54 +10:00
|
|
|
}
|
|
|
|
|
2024-04-07 17:01:26 +10:00
|
|
|
// Run executes the machine.
|
2024-04-07 15:41:27 +10:00
|
|
|
func (m *RTMPMachine) Run(ctx context.Context, incomingCh <-chan *ddp.ExtPacket) error {
|
2024-04-23 14:17:41 +10:00
|
|
|
ctx, setStatus, done := status.AddSimpleItem(ctx, "RTMP")
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
setStatus("Awaiting DDP address assignment")
|
|
|
|
|
2024-04-07 17:01:26 +10:00
|
|
|
// Await local address assignment before doing anything
|
2024-04-19 14:57:25 +10:00
|
|
|
<-m.AARP.Assigned()
|
|
|
|
myAddr, ok := m.AARP.Address()
|
2024-04-07 17:01:26 +10:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("AARP machine closed Assigned channel but Address is not valid")
|
|
|
|
}
|
|
|
|
|
2024-04-23 14:17:41 +10:00
|
|
|
setStatus("Initial RTMP Data broadcast")
|
|
|
|
|
2024-04-07 17:04:30 +10:00
|
|
|
// Initial broadcast
|
|
|
|
if err := m.broadcastData(myAddr); err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't broadcast Data: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-23 14:17:41 +10:00
|
|
|
setStatus("Packet loop")
|
|
|
|
|
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-04-07 17:04:30 +10:00
|
|
|
if err := m.broadcastData(myAddr); err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't broadcast Data: %v", err)
|
2024-04-07 15:14:54 +10:00
|
|
|
}
|
2024-04-07 15:41:27 +10:00
|
|
|
|
|
|
|
case pkt := <-incomingCh:
|
|
|
|
switch pkt.Proto {
|
|
|
|
case ddp.ProtoRTMPReq:
|
|
|
|
// I can answer RTMP requests!
|
|
|
|
req, err := rtmp.UnmarshalRequestPacket(pkt.Data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't unmarshal Request packet: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-07 16:31:19 +10:00
|
|
|
// should be in the cache...
|
2024-04-19 14:57:25 +10:00
|
|
|
theirHWAddr, err := m.AARP.Resolve(ctx, ddp.Addr{Network: pkt.SrcNet, Node: pkt.SrcNode})
|
2024-04-07 16:31:19 +10:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't resolve %d.%d to a hardware address: %v", pkt.SrcNet, pkt.SrcNode, err)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-07 15:41:27 +10:00
|
|
|
|
2024-04-07 16:31:19 +10:00
|
|
|
switch req.Function {
|
2024-04-19 16:25:39 +10:00
|
|
|
case rtmp.FunctionRequest:
|
2024-04-07 16:31:19 +10:00
|
|
|
// Respond with RTMP Response
|
|
|
|
respPkt := &rtmp.ResponsePacket{
|
|
|
|
SenderAddr: myAddr.Proto,
|
|
|
|
Extended: true,
|
2024-04-19 14:57:25 +10:00
|
|
|
RangeStart: m.Config.EtherTalk.NetStart,
|
|
|
|
RangeEnd: m.Config.EtherTalk.NetEnd,
|
2024-04-07 16:31:19 +10:00
|
|
|
}
|
|
|
|
respPktRaw, err := respPkt.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't marshal RTMP Response packet: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ddpPkt := &ddp.ExtPacket{
|
|
|
|
ExtHeader: ddp.ExtHeader{
|
2024-04-15 10:16:57 +10:00
|
|
|
Size: uint16(len(respPktRaw)) + atalk.DDPExtHeaderSize,
|
2024-04-07 16:31:19 +10:00
|
|
|
Cksum: 0,
|
|
|
|
DstNet: pkt.SrcNet,
|
|
|
|
DstNode: pkt.SrcNode,
|
|
|
|
DstSocket: 1, // the RTMP socket
|
|
|
|
SrcNet: myAddr.Proto.Network,
|
|
|
|
SrcNode: myAddr.Proto.Node,
|
|
|
|
SrcSocket: 1, // the RTMP socket
|
|
|
|
Proto: ddp.ProtoRTMPResp,
|
|
|
|
},
|
|
|
|
Data: respPktRaw,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.send(myAddr.Hardware, theirHWAddr, ddpPkt); err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't send Data broadcast: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-19 16:25:39 +10:00
|
|
|
case rtmp.FunctionRDRSplitHorizon, rtmp.FunctionRDRComplete:
|
2024-04-07 16:31:19 +10:00
|
|
|
// Like the Data broadcast, but solicited by a request (RDR).
|
|
|
|
// TODO: handle split-horizon processing
|
2024-04-19 15:36:47 +10:00
|
|
|
for _, dataPkt := range m.dataPackets(myAddr.Proto) {
|
|
|
|
dataPktRaw, err := dataPkt.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't marshal Data packet: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
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: myAddr.Proto.Network,
|
|
|
|
SrcNode: myAddr.Proto.Node,
|
|
|
|
SrcSocket: 1, // the RTMP socket
|
|
|
|
Proto: ddp.ProtoRTMPResp,
|
|
|
|
},
|
|
|
|
Data: dataPktRaw,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.send(myAddr.Hardware, theirHWAddr, ddpPkt); err != nil {
|
|
|
|
log.Printf("RTMP: Couldn't send Data response: %v", err)
|
|
|
|
break
|
|
|
|
}
|
2024-04-07 16:31:19 +10:00
|
|
|
}
|
2024-04-19 16:25:39 +10:00
|
|
|
|
|
|
|
case rtmp.FunctionLoopProbe:
|
|
|
|
log.Printf("RTMP: TODO: handle Loop Probes")
|
|
|
|
|
2024-04-07 15:41:27 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
case ddp.ProtoRTMPResp:
|
|
|
|
// It's a peer router on the AppleTalk network!
|
2024-04-19 15:36:47 +10:00
|
|
|
// TODO: integrate this information with the routing table?
|
2024-04-07 16:31:19 +10:00
|
|
|
log.Print("RTMP: Got Response or Data")
|
2024-04-07 15:41:27 +10:00
|
|
|
|
2024-04-07 17:01:26 +10:00
|
|
|
default:
|
|
|
|
log.Printf("RTMP: invalid DDP type %d on socket 1", pkt.Proto)
|
2024-04-07 15:41:27 +10:00
|
|
|
}
|
|
|
|
|
2024-04-07 15:14:54 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-07 16:31:19 +10:00
|
|
|
|
|
|
|
func (m *RTMPMachine) send(src, dst ethernet.Addr, ddpPkt *ddp.ExtPacket) error {
|
|
|
|
ethFrame, err := ethertalk.AppleTalk(src, *ddpPkt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ethFrame.Dst = dst
|
|
|
|
|
|
|
|
ethFrameRaw, err := ethertalk.Marshal(*ethFrame)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-19 14:57:25 +10:00
|
|
|
return m.PcapHandle.WritePacketData(ethFrameRaw)
|
2024-04-07 16:31:19 +10:00
|
|
|
}
|
|
|
|
|
2024-04-07 17:04:30 +10:00
|
|
|
func (m *RTMPMachine) broadcastData(myAddr aarp.AddrPair) error {
|
2024-04-19 15:36:47 +10:00
|
|
|
for _, dataPkt := range m.dataPackets(myAddr.Proto) {
|
|
|
|
dataPktRaw, err := dataPkt.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal Data packet: %v", err)
|
|
|
|
}
|
2024-04-07 17:04:30 +10:00
|
|
|
|
2024-04-19 15:36:47 +10:00
|
|
|
ddpPkt := &ddp.ExtPacket{
|
|
|
|
ExtHeader: ddp.ExtHeader{
|
|
|
|
Size: uint16(len(dataPktRaw)) + atalk.DDPExtHeaderSize,
|
|
|
|
Cksum: 0,
|
|
|
|
DstNet: 0, // this network
|
|
|
|
DstNode: 0xff, // broadcast packet
|
|
|
|
DstSocket: 1, // the RTMP socket
|
|
|
|
SrcNet: myAddr.Proto.Network,
|
|
|
|
SrcNode: myAddr.Proto.Node,
|
|
|
|
SrcSocket: 1, // the RTMP socket
|
|
|
|
Proto: ddp.ProtoRTMPResp,
|
|
|
|
},
|
|
|
|
Data: dataPktRaw,
|
|
|
|
}
|
2024-04-07 17:04:30 +10:00
|
|
|
|
2024-04-19 15:36:47 +10:00
|
|
|
if err := m.send(myAddr.Hardware, ethertalk.AppleTalkBroadcast, ddpPkt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-07 17:04:30 +10:00
|
|
|
}
|
2024-04-19 15:36:47 +10:00
|
|
|
return nil
|
2024-04-07 17:04:30 +10:00
|
|
|
}
|
|
|
|
|
2024-04-19 15:36:47 +10:00
|
|
|
func (m *RTMPMachine) dataPackets(myAddr ddp.Addr) []*rtmp.DataPacket {
|
|
|
|
// Build up a slice of routing tuples.
|
|
|
|
routes := m.RoutingTable.ValidRoutes()
|
|
|
|
tuples := make([]rtmp.NetworkTuple, 0, len(routes))
|
|
|
|
for _, rt := range routes {
|
|
|
|
tuples = append(tuples, rtmp.NetworkTuple{
|
2024-04-14 17:13:12 +10:00
|
|
|
Extended: rt.Extended,
|
|
|
|
RangeStart: rt.NetStart,
|
|
|
|
RangeEnd: rt.NetEnd,
|
|
|
|
Distance: rt.Distance + 1,
|
2024-04-13 15:47:58 +10:00
|
|
|
})
|
|
|
|
}
|
2024-04-19 15:36:47 +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,
|
|
|
|
RangeStart: m.Config.EtherTalk.NetStart,
|
|
|
|
RangeEnd: m.Config.EtherTalk.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{
|
|
|
|
RouterAddr: myAddr,
|
|
|
|
Extended: true,
|
|
|
|
NetworkTuples: chunk,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return packets
|
2024-04-07 16:31:19 +10:00
|
|
|
}
|