CodeSampleX

Sample

github.com/google/gopacket v1.1.19: serialize and decode network packets with Ethernet IPv4 and TCP layers

Verified sample for golang github.com/google/gopacket v1.1.19: serialize and decode network packets with Ethernet IPv4 and TCP layers. The contract ran on go…

sha256:c82aa308ab9c316041078264511809dd4dffcd6eaaafaeaef4768243b5e91862

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

Case

HOW
Goal
serialize and decode network packets with Ethernet IPv4 and TCP layers
Packages
Symbols
  • gopacket.NewPacket
  • gopacket.SerializeLayers
  • layers.Ethernet
  • layers.IPv4
  • layers.TCP
Created
2026-08-28T01:04:29Z

Contract

  1. gopacket.SerializeLayers serializes Ethernet, IPv4, TCP layers and payload into packet bytes
  2. gopacket.NewPacket decodes serialized packet bytes and reconstructs packet layers
  3. layers.Ethernet layer contains valid source MAC, destination MAC, and EthernetType
  4. layers.IPv4 layer contains valid source IP, destination IP, and IPProtocol
  5. layers.TCP layer contains valid source port, destination port, sequence number, and flags
  6. gopacket.ApplicationLayer contains the decoded payload bytes

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract_test.go

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: serialize and decode network packets with Ethernet IPv4 and TCP layers
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/gopacket@v1.1.19
Demonstrate these symbols/APIs:
  - gopacket.NewPacket
  - gopacket.SerializeLayers
  - layers.Ethernet
  - layers.IPv4
  - layers.TCP

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:7614c70b9933acbae9648cf3bb152d8b247a3988a4aa90e584dfc292ad385c18","contract":["gopacket.SerializeLayers serializes Ethernet, IPv4, TCP layers and payload into packet bytes","gopacket.NewPacket decodes serialized packet bytes and reconstructs packet layers","layers.Ethernet layer contains valid source MAC, destination MAC, and EthernetType","layers.IPv4 layer contains valid source IP, destination IP, and IPProtocol","layers.TCP layer contains valid source port, destination port, sequence number, and flags","gopacket.ApplicationLayer contains the decoded payload bytes"],"goal":"serialize and decode network packets with Ethernet IPv4 and TCP layers","kind":"HOW","packages":["pkg:golang/github.com/google/gopacket@v1.1.19"],"schemaVersion":1,"symbols":["gopacket.NewPacket","gopacket.SerializeLayers","layers.Ethernet","layers.IPv4","layers.TCP"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/gopacket@v1.1.19"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/gopacket@v1.1.19","symbols":["gopacket.NewPacket","gopacket.SerializeLayers","layers.Ethernet","layers.IPv4","layers.TCP"],"verifierAdapter":"golang@1"}
go.mod
module example.com/packet

go 1.26.6

require github.com/google/gopacket v1.1.19
go.sum
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
main.go
package main

import (
	"fmt"
	"net"

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

func main() {
	ethLayer := &layers.Ethernet{
		SrcMAC:       net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
		DstMAC:       net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
		EthernetType: layers.EthernetTypeIPv4,
	}

	ipLayer := &layers.IPv4{
		Version:  4,
		IHL:      5,
		TTL:      64,
		Protocol: layers.IPProtocolTCP,
		SrcIP:    net.IP{192, 168, 1, 10},
		DstIP:    net.IP{192, 168, 1, 20},
	}

	tcpLayer := &layers.TCP{
		SrcPort: layers.TCPPort(8080),
		DstPort: layers.TCPPort(80),
		SYN:     true,
		Seq:     1000,
	}
	_ = tcpLayer.SetNetworkLayerForChecksum(ipLayer)

	payload := gopacket.Payload([]byte("hello gopacket"))

	buf := gopacket.NewSerializeBuffer()
	opts := gopacket.SerializeOptions{
		FixLengths:       true,
		ComputeChecksums: true,
	}
	err := gopacket.SerializeLayers(buf, opts, ethLayer, ipLayer, tcpLayer, payload)
	if err != nil {
		panic(err)
	}

	packetData := buf.Bytes()
	fmt.Printf("Serialized packet length: %d bytes\n", len(packetData))

	packet := gopacket.NewPacket(packetData, layers.LayerTypeEthernet, gopacket.Default)

	if eth := packet.Layer(layers.LayerTypeEthernet); eth != nil {
		decodedEth, _ := eth.(*layers.Ethernet)
		fmt.Printf("Ethernet: %s -> %s\n", decodedEth.SrcMAC, decodedEth.DstMAC)
	}

	if ip := packet.Layer(layers.LayerTypeIPv4); ip != nil {
		decodedIP, _ := ip.(*layers.IPv4)
		fmt.Printf("IPv4: %s -> %s\n", decodedIP.SrcIP, decodedIP.DstIP)
	}

	if tcp := packet.Layer(layers.LayerTypeTCP); tcp != nil {
		decodedTCP, _ := tcp.(*layers.TCP)
		fmt.Printf("TCP: %d -> %d (SYN=%t)\n", decodedTCP.SrcPort, decodedTCP.DstPort, decodedTCP.SYN)
	}

	if app := packet.ApplicationLayer(); app != nil {
		fmt.Printf("Payload: %s\n", string(app.Payload()))
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "serialize and decode network packets with Ethernet IPv4 and TCP layers",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/gopacket@v1.1.19"
  ],
  "symbols": [
    "gopacket.NewPacket",
    "gopacket.SerializeLayers",
    "layers.Ethernet",
    "layers.IPv4",
    "layers.TCP"
  ]
}
test/contract_test.go
package test

import (
	"bytes"
	"net"
	"testing"

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

func TestSerializeAndDecodePacket(t *testing.T) {
	srcMAC := net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
	dstMAC := net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
	srcIP := net.IP{192, 168, 1, 10}
	dstIP := net.IP{192, 168, 1, 20}
	srcPort := layers.TCPPort(8080)
	dstPort := layers.TCPPort(80)
	payloadData := []byte("hello gopacket")

	ethLayer := &layers.Ethernet{
		SrcMAC:       srcMAC,
		DstMAC:       dstMAC,
		EthernetType: layers.EthernetTypeIPv4,
	}

	ipLayer := &layers.IPv4{
		Version:  4,
		IHL:      5,
		TTL:      64,
		Protocol: layers.IPProtocolTCP,
		SrcIP:    srcIP,
		DstIP:    dstIP,
	}

	tcpLayer := &layers.TCP{
		SrcPort: srcPort,
		DstPort: dstPort,
		SYN:     true,
		Seq:     1000,
	}
	if err := tcpLayer.SetNetworkLayerForChecksum(ipLayer); err != nil {
		t.Fatalf("failed to set network layer for checksum: %v", err)
	}

	payload := gopacket.Payload(payloadData)

	buf := gopacket.NewSerializeBuffer()
	opts := gopacket.SerializeOptions{
		FixLengths:       true,
		ComputeChecksums: true,
	}
	err := gopacket.SerializeLayers(buf, opts, ethLayer, ipLayer, tcpLayer, payload)
	if err != nil {
		t.Fatalf("gopacket.SerializeLayers failed: %v", err)
	}

	packetBytes := buf.Bytes()
	if len(packetBytes) == 0 {
		t.Fatal("serialized packetBytes is empty")
	}

	packet := gopacket.NewPacket(packetBytes, layers.LayerTypeEthernet, gopacket.Default)
	if packet.ErrorLayer() != nil {
		t.Fatalf("packet decoding error: %v", packet.ErrorLayer().Error())
	}

	// Verify Ethernet layer
	eth := packet.Layer(layers.LayerTypeEthernet)
	if eth == nil {
		t.Fatal("missing Ethernet layer")
	}
	ethDecoded, ok := eth.(*layers.Ethernet)
	if !ok {
		t.Fatal("layer is not *layers.Ethernet")
	}
	if !bytes.Equal(ethDecoded.SrcMAC, srcMAC) {
		t.Errorf("SrcMAC = %v, want %v", ethDecoded.SrcMAC, srcMAC)
	}
	if !bytes.Equal(ethDecoded.DstMAC, dstMAC) {
		t.Errorf("DstMAC = %v, want %v", ethDecoded.DstMAC, dstMAC)
	}
	if ethDecoded.EthernetType != layers.EthernetTypeIPv4 {
		t.Errorf("EthernetType = %v, want %v", ethDecoded.EthernetType, layers.EthernetTypeIPv4)
	}

	// Verify IPv4 layer
	ip := packet.Layer(layers.LayerTypeIPv4)
	if ip == nil {
		t.Fatal("missing IPv4 layer")
	}
	ipDecoded, ok := ip.(*layers.IPv4)
	if !ok {
		t.Fatal("layer is not *layers.IPv4")
	}
	if !ipDecoded.SrcIP.Equal(srcIP) {
		t.Errorf("SrcIP = %v, want %v", ipDecoded.SrcIP, srcIP)
	}
	if !ipDecoded.DstIP.Equal(dstIP) {
		t.Errorf("DstIP = %v, want %v", ipDecoded.DstIP, dstIP)
	}
	if ipDecoded.Protocol != layers.IPProtocolTCP {
		t.Errorf("Protocol = %v, want %v", ipDecoded.Protocol, layers.IPProtocolTCP)
	}

	// Verify TCP layer
	tcp := packet.Layer(layers.LayerTypeTCP)
	if tcp == nil {
		t.Fatal("missing TCP layer")
	}
	tcpDecoded, ok := tcp.(*layers.TCP)
	if !ok {
		t.Fatal("layer is not *layers.TCP")
	}
	if tcpDecoded.SrcPort != srcPort {
		t.Errorf("SrcPort = %v, want %v", tcpDecoded.SrcPort, srcPort)
	}
	if tcpDecoded.DstPort != dstPort {
		t.Errorf("DstPort = %v, want %v", tcpDecoded.DstPort, dstPort)
	}
	if !tcpDecoded.SYN {
		t.Errorf("SYN = %v, want true", tcpDecoded.SYN)
	}
	if tcpDecoded.Seq != 1000 {
		t.Errorf("Seq = %v, want 1000", tcpDecoded.Seq)
	}

	// Verify ApplicationLayer payload
	app := packet.ApplicationLayer()
	if app == nil {
		t.Fatal("missing application layer")
	}
	if !bytes.Equal(app.Payload(), payloadData) {
		t.Errorf("Payload = %q, want %q", string(app.Payload()), string(payloadData))
	}
}

func TestSerializeEmptyLayers(t *testing.T) {
	buf := gopacket.NewSerializeBuffer()
	opts := gopacket.SerializeOptions{}
	err := gopacket.SerializeLayers(buf, opts)
	if err != nil {
		t.Fatalf("SerializeLayers with no layers returned error: %v", err)
	}
	if len(buf.Bytes()) != 0 {
		t.Fatalf("expected 0 bytes for empty layers, got %d", len(buf.Bytes()))
	}
}

Origin Seeder

anonymous