Kaushik .p
Kaushik .p

Reputation: 35

Reading ngap messages from pcap file

I have a Pcap file and Im trying to read ngap messages from it using go lang,so far Im able to read sctp and gtp messages but im facing issue to find the library which can read ngap messages or even decode sctp messages as ngap message.

I wanted to know if theres any library that I might have missed out or some other method to capture ngap messages.

`package main`
 import (
    "fmt"
    "log"
    "os"
    "os/signal"
    "syscall"
    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
    "github.com/google/gopacket/pcap"
)

func main() {
    // Check if file argument is provided
    if len(os.Args) < 2 {
        fmt.Println("Please provide a pcap file to read")
        os.Exit(1)
    }

    // Open up the pcap file for reading
    handle, err := pcap.OpenOffline(os.Args[1])
    if err != nil {
        log.Fatal(err)
    }
    defer handle.Close()

    // Create a channel to capture interrupt signal
    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

    // Start capturing packets
    packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
    for {
        select {
        case packet := <-packetSource.Packets():
            if packet == nil {
                continue // Skip processing if packet is nil
            }

            // Extract and check for SCTP layer
            sctpLayer := packet.Layer(layers.LayerTypeSCTP)
            if sctpLayer != nil {
                fmt.Println("SCTP Packet:")
                fmt.Println(packet.String())
                continue // Skip further processing for this packet
            }

            // Extract and check for NGAP layer (GTPv1U)
            gtpLayer := packet.Layer(layers.LayerTypeGTPv1U)
            if gtpLayer != nil {
                fmt.Println("GTPv1U Message:")
                fmt.Println(packet.String())
                continue // Skip further processing for this packet
            }
        case <-interrupt:
            fmt.Println("Exiting... you can call an exit func here")
            return
        }
    }
}

Upvotes: 1

Views: 111

Answers (0)

Related Questions