dsjuimtq920056 2019-01-26 16:20
浏览 48

如何使用Gopacket访问相同类型的所有堆叠式标头的值

Can anybody guide me to extract all stacked header of same type. i.e interface receive Ethernet->Dot1Q->Dot1Q->IP->... in which how can I access both inner and outer Dot1Q Layer. I have tried with below code but didn't workout as expected.

package main

import (
    "fmt"
    "log"
    "os"
    "time"

    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
    "github.com/google/gopacket/pcap"
)

func main() {

    var (
        device = os.Args[1]
        //device             = "enp2s0"
        snapshot_len int32 = 1024
        promiscuous  bool  = false
        err          error
        timeout      time.Duration = 1 * time.Second
        handle       *pcap.Handle
        ethLayer     layers.Ethernet
        q1Layer      layers.Dot1Q
        q2Layer      layers.Dot1Q
        ipLayer      layers.IPv4
        udpLayer     layers.UDP
        dnsLayer     layers.DNS
    )

    handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
    if err != nil {
        log.Fatal(err)
    }
    defer handle.Close()

    packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
    for packet := range packetSource.Packets() {
        parser := gopacket.NewDecodingLayerParser(
            layers.LayerTypeEthernet,
            &ethLayer,
            &q1Layer,
            &q2Layer,
            &ipLayer,
            &udpLayer,
            &dnsLayer,
        )
        fmt.Println(packet)
        //fmt.Println(packet)
        foundLayerTypes := []gopacket.LayerType{}

        err := parser.DecodeLayers(packet.Data(), &foundLayerTypes)
        if err != nil {
            fmt.Println("Trouble decoding layers: ", err)
        }
        for _, layerType := range foundLayerTypes {
            fmt.Println(layerType)
        }
        fmt.Println("VLANq1 type: ", q1Layer.Type)
        fmt.Println("VLANq1 id: ", q1Layer.VLANIdentifier)
        fmt.Println("VLANq2 type: ", q2Layer.Type)
        fmt.Println("VLANq2 id: ", q2Layer.VLANIdentifier)

    }

}

==========================================================================

PACKET: 38 bytes, wire length 38 cap length 38 @ 2019-01-26 15:59:07.324935 +0000 UTC
- Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..24..] SrcMAC=00:04:00:00:00:00 DstMAC=00:04:00:00:00:01 EthernetType=Dot1Q Length=0}
- Layer 2 (04 bytes) = Dot1Q    {Contents=[0, 10, 129, 0] Payload=[..20..] Priority=0 DropEligible=false VLANIdentifier=10 Type=Dot1Q}
- Layer 3 (04 bytes) = Dot1Q    {Contents=[0, 100, 136, 100] Payload=[..16..] Priority=0 DropEligible=false VLANIdentifier=100 Type=PPPoESession}
- Layer 4 (06 bytes) = PPPoE    {Contents=[..6..] Payload=[..10..] Version=1 Type=1 Code=PPP SessionId=1 Length=10}
- Layer 5 (02 bytes) = PPP  {Contents=[192, 33] Payload=[..8..] PPPType=UnknownPPPType HasPPTPHeader=false}
- Layer 6 (08 bytes) = DecodeFailure    Packet decoding error: Unable to decode PPPType 49185

Trouble decoding layers:  No decoder for layer type PPPoE
Ethernet
Dot1Q
Dot1Q
VLANq1 type:  LLC
VLANq1 id:  0
VLANq2 type:  PPPoESession
VLANq2 id:  100

==========================================================================

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据