Add LLDP packet capture and logging

This commit is contained in:
Ian Gulliver
2025-11-28 15:28:47 -08:00
parent 095a70b311
commit 1073d0a267
3 changed files with 67 additions and 1 deletions

View File

@@ -2,9 +2,14 @@ package tendrils
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
type Tendrils struct {
@@ -89,5 +94,46 @@ func (t *Tendrils) updateGoroutines(interfaces []net.Interface) {
}
func (t *Tendrils) handleInterface(ctx context.Context, iface net.Interface) {
<-ctx.Done()
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()
bpfFilter := fmt.Sprintf("ether proto 0x88cc")
if err := handle.SetBPFFilter(bpfFilter); 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)
}