2

我想对 LLDP 数据包中的信息进行解码:lldp.go 使用 decodeLinkLayerDiscovery 函数进行解码,但我不知道如何使用它。例如,我想获取 MgmtAddress 的值。我该怎么做 ?

func readLLDP(handle *pcap.Handle, iface *net.Interface, stop chan struct{}) {
    src := gopacket.NewPacketSource(handle, layers.LayerTypeEthernet)
    in := src.Packets()

    p := gopacket.PacketBuilder{}

    p.AddLayer()

    for {
        var packet gopacket.Packet
        select {
        case <-stop:
            return
        case packet = <-in:
            lldpLayer := packet.Layer(layers.LayerTypeLinkLayerDiscovery)
            if lldpLayer == nil {
                continue
            }
            lldp := lldpLayer.(*layers.LinkLayerDiscovery)

            var chassisID string
            switch lldp.ChassisID.Subtype {
            case layers.LLDPChassisIDSubTypeMACAddr:
                chassisID = net.HardwareAddr(lldp.ChassisID.ID).String()
            default:
                chassisID = string(lldp.ChassisID.ID)
            }
            log.Printf("[%v] ChassisID %v", iface.Name, chassisID)
        }
    }
}
4

1 回答 1

1

我找到了解决方案,我必须使用 LayerTypeLinkLayerDiscoveryInfo :

    lldpLayerInfos := packet.Layer(layers.LayerTypeLinkLayerDiscoveryInfo)
    if lldpLayerInfos == nil {
        continue
    }
    lldpInfos := lldpLayerInfos.(*layers.LinkLayerDiscoveryInfo)
    log.Printf("[%v] ************************************************", iface.Name)
    log.Printf("[%v] PortDescription : %s", iface.Name, lldpInfos.PortDescription)
    log.Printf("[%v] SysName : %s", iface.Name, lldpInfos.SysName)
    log.Printf("[%v] SysDescription : %s", iface.Name, lldpInfos.SysDescription)
    var mgmtAddress string
    switch lldpInfos.MgmtAddress.Subtype {
    case layers.IANAAddressFamilyIPV4:
        mgmtAddress = net.IP(lldpInfos.MgmtAddress.Address).String()
    default:
        mgmtAddress = string(lldpInfos.MgmtAddress.Address)
    }
    log.Printf("[%v] MgmtAddress : %s", iface.Name, mgmtAddress)
于 2021-11-17T12:44:57.187 回答