This commit is contained in:
Josh Deprez 2024-04-23 11:56:15 +10:00
parent 1b33388e2a
commit 74936edbdd
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw

31
main.go
View file

@ -127,15 +127,16 @@ func main() {
pcapHandle.Close() pcapHandle.Close()
log.Fatalf("Couldn't set BPF filter on packet capture: %v", err) log.Fatalf("Couldn't set BPF filter on packet capture: %v", err)
} }
defer pcapHandle.Close() // defer pcapHandle.Close()
// Wait until all peer handlers have finished before closing the port // Wait until all peer handlers have finished before closing the port
var handlersWG sync.WaitGroup var handlersWG sync.WaitGroup
defer func() { defer handlersWG.Wait()
log.Print("Waiting for handlers to return...") // defer func() {
handlersWG.Wait() // // log.Print("Waiting for handlers to return...")
ln.Close() // // handlersWG.Wait()
}() // // ln.Close()
// }()
goPeerHandler := func(p *router.Peer) { goPeerHandler := func(p *router.Peer) {
handlersWG.Add(1) handlersWG.Add(1)
go func() { go func() {
@ -244,9 +245,9 @@ func main() {
} }
rawPkt, _, err := pcapHandle.ReadPacketData() rawPkt, _, err := pcapHandle.ReadPacketData()
if errors.Is(err, pcap.NextErrorTimeoutExpired) { // if errors.Is(err, pcap.NextErrorTimeoutExpired) {
continue // continue
} // }
if errors.Is(err, io.EOF) || errors.Is(err, pcap.NextErrorNoMorePackets) { if errors.Is(err, io.EOF) || errors.Is(err, pcap.NextErrorNoMorePackets) {
return return
} }
@ -357,18 +358,19 @@ func main() {
}() }()
// ----------------------------- AURP inbound ----------------------------- // ----------------------------- AURP inbound -----------------------------
go func() {
for { for {
if ctx.Err() != nil { if ctx.Err() != nil {
return return
} }
ln.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) // ln.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
pktbuf := make([]byte, 4096) pktbuf := make([]byte, 4096)
pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf) pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf)
var operr *net.OpError // var operr *net.OpError
if errors.As(readErr, &operr) && operr.Timeout() { // if errors.As(readErr, &operr) && operr.Timeout() {
continue // continue
} // }
// log.Printf("AURP: Received packet of length %d from %v", pktlen, raddr) // log.Printf("AURP: Received packet of length %d from %v", pktlen, raddr)
@ -483,6 +485,7 @@ func main() {
log.Printf("AURP: Got unknown packet type %v", dh.PacketType) log.Printf("AURP: Got unknown packet type %v", dh.PacketType)
} }
} }
}()
} }
// Hashable net.UDPAddr // Hashable net.UDPAddr