Some refactoring

This commit is contained in:
Josh Deprez 2024-04-21 18:58:24 +10:00
parent 6dc5246303
commit 1c274665d6
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
6 changed files with 20 additions and 83 deletions

18
main.go
View file

@ -382,7 +382,7 @@ func main() {
return
}
log.Printf("AURP: Got %T from %v", pkt, dh.SourceDI)
log.Printf("AURP: Got %T from %v (%v)", pkt, raddr, dh.SourceDI)
// Existing peer?
ra := udpAddrFromNet(raddr)
@ -480,20 +480,8 @@ func main() {
continue
}
outFrame, err := ethertalk.AppleTalk(myHWAddr, *ddpkt)
if err != nil {
log.Printf("DDP/AURP: couldn't create output frame: %v", err)
continue
}
outFrame.Dst = dstEth
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
log.Printf("DDP/AURP: couldn't marshal output frame: %v", err)
continue
}
if err := pcapHandle.WritePacketData(outFrameRaw); err != nil {
log.Printf("DDP/AURP: couldn't write output frame to device: %v", err)
if err := rooter.SendEtherTalkDDP(dstEth, ddpkt); err != nil {
log.Printf("DDP/AURP: couldn't send Ethertalk out: %v", err)
}
continue

View file

@ -22,7 +22,6 @@ import (
"gitea.drjosh.dev/josh/jrouter/atalk/aep"
"github.com/sfiera/multitalk/pkg/ddp"
"github.com/sfiera/multitalk/pkg/ethernet"
"github.com/sfiera/multitalk/pkg/ethertalk"
)
func (rtr *Router) HandleAEP(src ethernet.Addr, ddpkt *ddp.ExtPacket) error {
@ -48,16 +47,7 @@ func (rtr *Router) HandleAEP(src ethernet.Addr, ddpkt *ddp.ExtPacket) error {
ddpkt.DstSocket, ddpkt.SrcSocket = ddpkt.SrcSocket, ddpkt.DstSocket
ddpkt.Data[0] = byte(aep.EchoReply)
ethFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, *ddpkt)
if err != nil {
return err
}
ethFrame.Dst = src
ethFrameRaw, err := ethertalk.Marshal(*ethFrame)
if err != nil {
return err
}
return rtr.PcapHandle.WritePacketData(ethFrameRaw)
return rtr.SendEtherTalkDDP(src, ddpkt)
default:
return fmt.Errorf("invalid AEP function %d", ep.Function)

View file

@ -47,7 +47,7 @@ func (rtr *Router) HandleNBP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
return err
}
log.Print("NBP: Replying to LkUp with LkUp-Reply for myself")
return rtr.sendEtherTalkDDP(srcHWAddr, outDDP)
return rtr.SendEtherTalkDDP(srcHWAddr, outDDP)
case nbp.FunctionBrRq:
// There must be 1!
@ -80,16 +80,7 @@ func (rtr *Router) HandleNBP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
outDDP.DstNode = 0xFF // Broadcast node address within the dest network
outDDP.Data = nbpRaw
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, outDDP)
if err != nil {
return err
}
outFrame.Dst = ethDst
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
return err
}
if err := rtr.PcapHandle.WritePacketData(outFrameRaw); err != nil {
if err := rtr.SendEtherTalkDDP(ethDst, &outDDP); err != nil {
return err
}
@ -102,7 +93,7 @@ func (rtr *Router) HandleNBP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
continue
}
log.Print("NBP: Replying to BrRq with LkUp-Reply for myself")
if err := rtr.sendEtherTalkDDP(srcHWAddr, outDDP2); err != nil {
if err := rtr.SendEtherTalkDDP(srcHWAddr, outDDP2); err != nil {
return err
}

View file

@ -71,7 +71,7 @@ func (rtr *Router) HandleNBPInAURP(peer *Peer, ddpkt *ddp.ExtPacket) error {
if tuple.Zone != "*" && tuple.Zone != "" {
dstEth = atalk.MulticastAddr(tuple.Zone)
}
if err := rtr.sendEtherTalkDDP(dstEth, ddpkt); err != nil {
if err := rtr.SendEtherTalkDDP(dstEth, ddpkt); err != nil {
return err
}

View file

@ -32,7 +32,7 @@ type Router struct {
ZoneTable *ZoneTable
}
func (rtr *Router) sendEtherTalkDDP(dstEth ethernet.Addr, pkt *ddp.ExtPacket) error {
func (rtr *Router) SendEtherTalkDDP(dstEth ethernet.Addr, pkt *ddp.ExtPacket) error {
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, *pkt)
if err != nil {
return err

View file

@ -92,7 +92,7 @@ func (rtr *Router) HandleZIP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
if err != nil {
return err
}
respDDP := ddp.ExtPacket{
respDDP := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(ddpBody)) + atalk.DDPExtHeaderSize,
Cksum: 0,
@ -106,16 +106,7 @@ func (rtr *Router) HandleZIP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
},
Data: ddpBody,
}
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, respDDP)
if err != nil {
return err
}
outFrame.Dst = srcHWAddr
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
return err
}
return rtr.PcapHandle.WritePacketData(outFrameRaw)
return rtr.SendEtherTalkDDP(srcHWAddr, respDDP)
case *atp.TResp:
return fmt.Errorf("TODO: support handling ZIP ATP replies?")
@ -140,7 +131,7 @@ func (rtr *Router) HandleZIP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
if err != nil {
return fmt.Errorf("couldn't marshal %T: %w", resp, err)
}
outDDP := ddp.ExtPacket{
outDDP := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(respRaw)) + atalk.DDPExtHeaderSize,
Cksum: 0,
@ -154,21 +145,7 @@ func (rtr *Router) HandleZIP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
},
Data: respRaw,
}
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, outDDP)
if err != nil {
return fmt.Errorf("couldn't create EtherTalk frame: %w", err)
}
// Unicast reply.
outFrame.Dst = srcHWAddr
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
return fmt.Errorf("couldn't marshal EtherTalk frame: %w", err)
}
if err := rtr.PcapHandle.WritePacketData(outFrameRaw); err != nil {
return fmt.Errorf("couldn't write packet data: %w", err)
}
return nil
return rtr.SendEtherTalkDDP(srcHWAddr, outDDP)
}
// Inside AppleTalk SE, pp 8-11:
@ -268,7 +245,7 @@ func (rtr *Router) HandleZIP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
// assigned to the node's network. In these cases,
// if the request was sent via a broadcast, the
// routers should respond with a broadcast."
outDDP := ddp.ExtPacket{
outDDP := &ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(respRaw)) + atalk.DDPExtHeaderSize,
Cksum: 0,
@ -289,22 +266,13 @@ func (rtr *Router) HandleZIP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
outDDP.DstNode = 0xFF
}
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, outDDP)
if err != nil {
return fmt.Errorf("couldn't create EtherTalk frame: %w", err)
// If it's a broadcast packet, broadcast it. Otherwise don't
dstEth := ethertalk.AppleTalkBroadcast
if outDDP.DstNode != 0xFF {
dstEth = srcHWAddr
}
// If it's a broadcast packet, broadcast it. Otherwise don't?
if outDDP.DstNet != 0x0000 || outDDP.DstNode != 0xFF {
outFrame.Dst = srcHWAddr
}
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
return fmt.Errorf("couldn't marshal EtherTalk frame: %w", err)
}
if err := rtr.PcapHandle.WritePacketData(outFrameRaw); err != nil {
return fmt.Errorf("couldn't write packet data: %w", err)
}
return nil
return rtr.SendEtherTalkDDP(dstEth, outDDP)
default:
return fmt.Errorf("TODO: handle type %T", zipkt)