2

I would like to decode informations in a LLDP packet : lldp.go do it with decodeLinkLayerDiscovery function but I don't know how to use it. For example, I want to get value of MgmtAddress. How can I do it ?

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)
        }
    }
}
ipStack
  • 381
  • 1
  • 12

1 Answers1

1

I found solution, I must use 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)
ipStack
  • 381
  • 1
  • 12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 17 '21 at 13:14