如何在lldp.go中使用decodeLinkLayerDiscovery函数?

huangapple go评论152阅读模式
英文:

How to use decodeLinkLayerDiscovery in lldp.go?

问题

我想解码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)
        }
    }
}

以上是一个读取LLDP数据包的示例代码。在代码中,使用lldpLayer := packet.Layer(layers.LayerTypeLinkLayerDiscovery)来获取LLDP层。然后,可以通过lldp对象来访问LLDP的各个字段,例如lldp.MgmtAddress来获取MgmtAddress的值。

英文:

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 &lt;-stop:
			return
		case packet = &lt;-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(&quot;[%v] ChassisID %v&quot;, iface.Name, chassisID)
		}
	}
}

答案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)

英文:

I found solution, I must use LayerTypeLinkLayerDiscoveryInfo :

	lldpLayerInfos := packet.Layer(layers.LayerTypeLinkLayerDiscoveryInfo)
	if lldpLayerInfos == nil {
		continue
	}
	lldpInfos := lldpLayerInfos.(*layers.LinkLayerDiscoveryInfo)
	log.Printf(&quot;[%v] ************************************************&quot;, iface.Name)
	log.Printf(&quot;[%v] PortDescription : %s&quot;, iface.Name, lldpInfos.PortDescription)
	log.Printf(&quot;[%v] SysName : %s&quot;, iface.Name, lldpInfos.SysName)
	log.Printf(&quot;[%v] SysDescription : %s&quot;, 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(&quot;[%v] MgmtAddress : %s&quot;, iface.Name, mgmtAddress)

huangapple
  • 本文由 发表于 2021年11月17日 18:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/70002868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定