Split LLDP code into separate file and refactor interface tracking

This commit is contained in:
Ian Gulliver
2025-11-28 15:44:28 -08:00
parent 1073d0a267
commit b85925f644
3 changed files with 70 additions and 58 deletions

56
lldp.go Normal file
View File

@@ -0,0 +1,56 @@
package tendrils
import (
"context"
"log"
"net"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func (t *Tendrils) listenLLDP(ctx context.Context, iface net.Interface) {
handle, err := pcap.OpenLive(iface.Name, 65536, true, 5*time.Second)
if err != nil {
log.Printf("[ERROR] failed to open interface %s: %v", iface.Name, err)
return
}
defer handle.Close()
if err := handle.SetBPFFilter("ether proto 0x88cc"); err != nil {
log.Printf("[ERROR] failed to set BPF filter on %s: %v", iface.Name, err)
return
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
packets := packetSource.Packets()
for {
select {
case <-ctx.Done():
return
case packet, ok := <-packets:
if !ok {
return
}
t.handleLLDPPacket(iface.Name, packet)
}
}
}
func (t *Tendrils) handleLLDPPacket(ifaceName string, packet gopacket.Packet) {
lldpLayer := packet.Layer(layers.LayerTypeLinkLayerDiscovery)
if lldpLayer == nil {
return
}
lldp, ok := lldpLayer.(*layers.LinkLayerDiscovery)
if !ok {
return
}
log.Printf("[%s] lldp packet received: ChassisID=%x PortID=%s TTL=%d",
ifaceName, lldp.ChassisID.ID, lldp.PortID.ID, lldp.TTL)
}