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

时间:2019-01-26 16:20:43

标签: go gopacket

任何人都可以引导我提取相同类型的所有堆叠式标头。即接口接收以太网-> Dot1Q-> Dot1Q-> IP-> ...在其中如何访问内部和外部Dot1Q层。我尝试使用下面的代码,但没有按预期进行锻炼。

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 个答案:

没有答案