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 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? // Existing peer?
ra := udpAddrFromNet(raddr) ra := udpAddrFromNet(raddr)
@ -480,20 +480,8 @@ func main() {
continue continue
} }
outFrame, err := ethertalk.AppleTalk(myHWAddr, *ddpkt) if err := rooter.SendEtherTalkDDP(dstEth, ddpkt); err != nil {
if err != nil { log.Printf("DDP/AURP: couldn't send Ethertalk out: %v", err)
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)
} }
continue continue

View file

@ -22,7 +22,6 @@ import (
"gitea.drjosh.dev/josh/jrouter/atalk/aep" "gitea.drjosh.dev/josh/jrouter/atalk/aep"
"github.com/sfiera/multitalk/pkg/ddp" "github.com/sfiera/multitalk/pkg/ddp"
"github.com/sfiera/multitalk/pkg/ethernet" "github.com/sfiera/multitalk/pkg/ethernet"
"github.com/sfiera/multitalk/pkg/ethertalk"
) )
func (rtr *Router) HandleAEP(src ethernet.Addr, ddpkt *ddp.ExtPacket) error { 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.DstSocket, ddpkt.SrcSocket = ddpkt.SrcSocket, ddpkt.DstSocket
ddpkt.Data[0] = byte(aep.EchoReply) ddpkt.Data[0] = byte(aep.EchoReply)
ethFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, *ddpkt) return rtr.SendEtherTalkDDP(src, ddpkt)
if err != nil {
return err
}
ethFrame.Dst = src
ethFrameRaw, err := ethertalk.Marshal(*ethFrame)
if err != nil {
return err
}
return rtr.PcapHandle.WritePacketData(ethFrameRaw)
default: default:
return fmt.Errorf("invalid AEP function %d", ep.Function) 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 return err
} }
log.Print("NBP: Replying to LkUp with LkUp-Reply for myself") log.Print("NBP: Replying to LkUp with LkUp-Reply for myself")
return rtr.sendEtherTalkDDP(srcHWAddr, outDDP) return rtr.SendEtherTalkDDP(srcHWAddr, outDDP)
case nbp.FunctionBrRq: case nbp.FunctionBrRq:
// There must be 1! // 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.DstNode = 0xFF // Broadcast node address within the dest network
outDDP.Data = nbpRaw outDDP.Data = nbpRaw
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, outDDP) if err := rtr.SendEtherTalkDDP(ethDst, &outDDP); err != nil {
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 {
return err return err
} }
@ -102,7 +93,7 @@ func (rtr *Router) HandleNBP(srcHWAddr ethernet.Addr, ddpkt *ddp.ExtPacket) erro
continue continue
} }
log.Print("NBP: Replying to BrRq with LkUp-Reply for myself") 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 return err
} }

View file

@ -71,7 +71,7 @@ func (rtr *Router) HandleNBPInAURP(peer *Peer, ddpkt *ddp.ExtPacket) error {
if tuple.Zone != "*" && tuple.Zone != "" { if tuple.Zone != "*" && tuple.Zone != "" {
dstEth = atalk.MulticastAddr(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 return err
} }

View file

@ -32,7 +32,7 @@ type Router struct {
ZoneTable *ZoneTable 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) outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, *pkt)
if err != nil { if err != nil {
return err return err

View file

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