Convert Figma logo to code with AI

google logogopacket

Provides packet processing capabilities for Go

6,271
1,123
6,271
350

Top Related Projects

Stenographer is a packet capture solution which aims to quickly spool all packets to disk, then provide simple, fast access to subsets of those packets. Discussion/announcements at stenographer@googlegroups.com

This repository contains a Go module to interact with Linux nftables (the iptables successor).

IPv4 and IPv6 userland network stack

10,663

Scapy: the Python-based interactive packet manipulation program & library.

Read-only mirror of Wireshark's Git repository at https://gitlab.com/wireshark/wireshark. ⚠️ GitHub won't let us disable pull requests. ⚠️ THEY WILL BE IGNORED HERE ⚠️ Upload them at GitLab instead.

Quick Overview

Google's gopacket is a Go library for packet processing. It provides a robust set of tools for capturing, decoding, and analyzing network packets, supporting various protocols and network layers. The library is designed to be efficient and flexible, making it suitable for a wide range of network-related applications.

Pros

  • High performance and efficient packet processing
  • Extensive protocol support, including both common and custom protocols
  • Flexible API allowing for easy customization and extension
  • Well-documented with comprehensive examples and tutorials

Cons

  • Steep learning curve for beginners due to its extensive feature set
  • Requires root/admin privileges for live packet capture on most systems
  • May require additional dependencies for certain features (e.g., libpcap)
  • Limited cross-platform support for some advanced features

Code Examples

  1. Capturing packets from a network interface:
package main

import (
    "fmt"
    "github.com/google/gopacket"
    "github.com/google/gopacket/pcap"
    "log"
)

func main() {
    handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever)
    if err != nil {
        log.Fatal(err)
    }
    defer handle.Close()

    packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
    for packet := range packetSource.Packets() {
        fmt.Println(packet)
    }
}
  1. Decoding a specific layer (e.g., TCP):
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
    tcp, _ := tcpLayer.(*layers.TCP)
    fmt.Printf("From port %d to %d\n", tcp.SrcPort, tcp.DstPort)
}
  1. Creating and sending a custom packet:
buffer := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
gopacket.SerializeLayers(buffer, opts,
    &layers.Ethernet{SrcMAC: srcMAC, DstMAC: dstMAC},
    &layers.IPv4{SrcIP: srcIP, DstIP: dstIP, Protocol: layers.IPProtocolTCP},
    &layers.TCP{SrcPort: srcPort, DstPort: dstPort},
    gopacket.Payload([]byte("Hello, World!")),
)
outgoingPacket := buffer.Bytes()

Getting Started

To start using gopacket, follow these steps:

  1. Install Go on your system if you haven't already.
  2. Install libpcap development files (e.g., libpcap-dev on Ubuntu).
  3. Install gopacket:
    go get github.com/google/gopacket
    
  4. Import the necessary packages in your Go code:
    import (
        "github.com/google/gopacket"
        "github.com/google/gopacket/pcap"
    )
    
  5. Start using gopacket functions in your code (refer to the examples above).

Note: Some features may require root/admin privileges to run.

Competitor Comparisons

Stenographer is a packet capture solution which aims to quickly spool all packets to disk, then provide simple, fast access to subsets of those packets. Discussion/announcements at stenographer@googlegroups.com

Pros of Stenographer

  • Designed for high-speed packet capture and storage
  • Efficient indexing and retrieval of packet data
  • Better suited for long-term packet storage and analysis

Cons of Stenographer

  • More complex setup and configuration
  • Limited to packet capture and storage functionality
  • Less flexible for general-purpose packet manipulation

Code Comparison

Stenographer (packet writing):

func (w *Writer) Write(pkt *base.Packet) error {
    w.mu.Lock()
    defer w.mu.Unlock()
    return w.write(pkt)
}

Gopacket (packet creation):

packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
    tcp, _ := tcpLayer.(*layers.TCP)
    fmt.Printf("From port %d to %d\n", tcp.SrcPort, tcp.DstPort)
}

Summary

Stenographer is optimized for high-speed packet capture and storage, making it ideal for long-term analysis. However, it's more complex to set up and less flexible than Gopacket. Gopacket offers a more versatile toolkit for packet manipulation and analysis, but may not be as efficient for large-scale packet capture and storage. The choice between the two depends on specific use cases and requirements.

This repository contains a Go module to interact with Linux nftables (the iptables successor).

Pros of nftables

  • Focuses specifically on netfilter firewall manipulation, providing a more specialized toolset for network filtering and packet classification
  • Offers a more modern and flexible approach to firewall configuration compared to iptables
  • Provides a higher-level abstraction for firewall rules, making it easier to manage complex configurations

Cons of nftables

  • Has a narrower scope compared to gopacket, which offers broader packet processing capabilities
  • May have a steeper learning curve for users familiar with traditional iptables syntax
  • Less mature ecosystem and community support compared to gopacket

Code Comparison

nftables:

conn, err := nftables.New()
table := &nftables.Table{Name: "filter", Family: nftables.TableFamilyIPv4}
chain := &nftables.Chain{Name: "input", Table: table}
conn.AddRule(&nftables.Rule{
    Table: table,
    Chain: chain,
    Exprs: []expr.Any{
        // Rule expressions
    },
})

gopacket:

handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever)
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
    // Process packet
}

The code snippets demonstrate the different focus areas of the two libraries. nftables is centered around firewall rule configuration, while gopacket provides more general packet capture and processing capabilities.

IPv4 and IPv6 userland network stack

Pros of netstack

  • Provides a complete network stack implementation, including TCP/IP
  • Designed for use in non-OS environments, like embedded systems
  • Offers more control over network protocols and behavior

Cons of netstack

  • Less actively maintained compared to gopacket
  • Steeper learning curve due to its comprehensive nature
  • May be overkill for simple packet processing tasks

Code Comparison

netstack:

// Creating a new network stack
s := stack.New(stack.Options{
    NetworkProtocols:   []stack.NetworkProtocol{ipv4.NewProtocol()},
    TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
})

gopacket:

// Reading packets from a pcap file
handle, _ := pcap.OpenOffline("file.pcap")
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
    // Process packet
}

netstack focuses on building a complete network stack, while gopacket is primarily used for packet capture and analysis. netstack provides more control over network protocols but requires more setup. gopacket offers simpler packet processing capabilities and is better suited for tasks like network monitoring or packet inspection. The choice between the two depends on the specific requirements of your project.

10,663

Scapy: the Python-based interactive packet manipulation program & library.

Pros of Scapy

  • Written in Python, offering easier scripting and rapid prototyping
  • More extensive protocol support out-of-the-box
  • Powerful packet manipulation and crafting capabilities

Cons of Scapy

  • Slower performance compared to GoPacket's compiled Go code
  • Less suitable for high-performance, production-grade applications
  • Steeper learning curve due to its extensive features

Code Comparison

Scapy (Python):

from scapy.all import *
packet = IP(dst="8.8.8.8")/TCP(dport=80)
send(packet)

GoPacket (Go):

import "github.com/google/gopacket"
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
ipLayer := packet.Layer(layers.LayerTypeIPv4)

Summary

Scapy is a versatile Python-based packet manipulation tool, ideal for rapid prototyping and complex packet crafting. It offers extensive protocol support but may have performance limitations. GoPacket, written in Go, provides better performance and is more suitable for production environments, but with a potentially steeper learning curve for those unfamiliar with Go.

Read-only mirror of Wireshark's Git repository at https://gitlab.com/wireshark/wireshark. ⚠️ GitHub won't let us disable pull requests. ⚠️ THEY WILL BE IGNORED HERE ⚠️ Upload them at GitLab instead.

Pros of Wireshark

  • More comprehensive and feature-rich GUI for packet analysis
  • Extensive protocol support and decoding capabilities
  • Large community and extensive documentation

Cons of Wireshark

  • Heavier resource usage and slower performance for large captures
  • Steeper learning curve for advanced features

Code Comparison

Wireshark (C):

void
packet_list_queue_draw(PacketList *packet_list)
{
    gtk_widget_queue_draw(packet_list->view);
}

Gopacket (Go):

func (p *Packet) Layer(layerType LayerType) Layer {
    for _, layer := range p.Layers() {
        if layer.LayerType() == layerType {
            return layer
        }
    }
    return nil
}

Key Differences

  • Wireshark is a full-featured GUI application, while Gopacket is a library for packet processing in Go
  • Wireshark offers more extensive protocol analysis, while Gopacket focuses on efficient packet parsing and manipulation
  • Wireshark has a larger codebase and longer development history, whereas Gopacket is more lightweight and easier to integrate into Go projects

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

GoPacket

This library provides packet decoding capabilities for Go. See godoc for more details.

Build Status GoDoc

Minimum Go version required is 1.5 except for pcapgo/EthernetHandle, afpacket, and bsdbpf which need at least 1.9 due to x/sys/unix dependencies.

Originally forked from the gopcap project written by Andreas Krennmair ak@synflood.at (http://github.com/akrennmair/gopcap).