#1160·gopacket

Capturing NGAP messages

Author: mrramachariCreated Mar 25, 2024Updated Jan 6, 2026

I wanted to capture ngap messages using this library,I wanted to know if that's possible using this library if yes I wanted some examples. So far I've managed to read sctp and gtp messages.

`package main
import (
	"fmt" // Import the fmt package to print messages to the console.
	"log" // Import the log package to log errors to the console.
	"os"

	"github.com/google/gopacket"        // Import the gopacket package to decode packets.
	"github.com/google/gopacket/layers" // Import the layers package to access the various network layers.
	"github.com/google/gopacket/pcap"   // Import the pcap package to capture packets.
)

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()

	// Loop through packets in file
	packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
	for packet := range packetSource.Packets() {

		// Print the packet details
		// fmt.Println(packet.String())

		// // Extract and print the Ethernet layer
		// ethLayer := packet.Layer(layers.LayerTypeEthernet)
		// if ethLayer != nil {
		// 	ethPacket, _ := ethLayer.(*layers.Ethernet)
		// 	fmt.Println("Ethernet source MAC address:", ethPacket.SrcMAC)
		// 	fmt.Println("Ethernet destination MAC address:", ethPacket.DstMAC)
		// }

		// // Extract and print the IP layer
		// ipLayer := packet.Layer(layers.LayerTypeIPv4)
		// if ipLayer != nil {
		// 	ipPacket, _ := ipLayer.(*layers.IPv4)
		// 	fmt.Println("IP source address:", ipPacket.SrcIP)
		// 	fmt.Println("IP destination address:", ipPacket.DstIP)
		// }

		// 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 GTP layer
		// gtpLayer := packet.Layer(layers.LayerTypeGTP)
		// if gtpLayer != nil {
		// 	fmt.Println("GTP Packet:")
		// 	fmt.Println(packet.String())
		// 	continue // Skip further processing for this packet
		// }
		ngapLayer := packet.Layer(layers.LayerTypeGTPv1U)
		if ngapLayer != nil {
			fmt.Println("GTPv1U Message:")
			fmt.Println(packet.String())
			continue // Skip further processing for this packet
		}
	}
}`