pcap

package
v0.78.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package pcap implements a pure-Go libpcap classic-format writer and reader. It is deliberately minimal: no CGo, no gopacket, stdlib only. The output is compatible with Wireshark, aircrack-ng, and hashcat.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinkType

type LinkType uint32

LinkType identifies the data-link encapsulation of frames in the pcap.

const (
	// LinkTypeEthernet is IEEE 802.3 Ethernet.
	LinkTypeEthernet LinkType = 1

	// LinkTypeIEEE802_11 is raw 802.11 without any prefix header.
	// This is what most Marauder packet dumps emit.
	LinkTypeIEEE802_11 LinkType = 105

	// LinkTypeIEEE802_11Radiotap is 802.11 preceded by a radiotap header.
	// Preferred for hashcat / aircrack-ng because it carries channel and RSSI.
	LinkTypeIEEE802_11Radiotap LinkType = 127
)

type RadiotapHeader

type RadiotapHeader struct {
	// Channel is the centre frequency in MHz (e.g. 2412 for channel 1,
	// 5180 for channel 36). Zero is treated as unknown / unset.
	Channel uint16

	// Flags is the radiotap Flags field (bit 1 in the present bitmap).
	// 0x10 signals that an FCS is appended to the frame payload.
	Flags uint8

	// SignalDBM is the receive signal strength in dBm (e.g. -65).
	// Zero means unknown.
	SignalDBM int8

	// Rate is the data rate in units of 500 kbps (e.g. 2 = 1 Mbit/s).
	// Zero means unknown.
	Rate uint8
}

RadiotapHeader is a minimal radiotap prefix for 802.11 frames. Prepend the result of Bytes() before the raw 802.11 frame when writing with LinkTypeIEEE802_11Radiotap.

func (RadiotapHeader) Bytes

func (h RadiotapHeader) Bytes() []byte

Bytes returns the 16-byte radiotap-encoded prefix to prepend to a bare 802.11 frame before handing it to Writer.WritePacket when the Writer was created with LinkTypeIEEE802_11Radiotap.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads a libpcap classic file emitted by Writer. It supports both little-endian (magic 0xa1b2c3d4) and big-endian (magic 0xd4c3b2a1) files, though Writer only produces little-endian.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader reads and validates the 24-byte global pcap header from r. Returns an error if the magic is unrecognised or the version is not 2.4.

func (*Reader) LinkType

func (r *Reader) LinkType() LinkType

LinkType returns the file's link-type field from the global header.

func (*Reader) Next

func (r *Reader) Next() (time.Time, []byte, error)

Next returns the next packet's timestamp and raw frame bytes, or io.EOF when the file is exhausted. Any other error indicates a malformed or truncated file.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer streams packets to an io.Writer in libpcap classic format.

Concurrency: not safe for concurrent WritePacket; serialise externally.

Usage:

w, err := pcap.NewWriter(file, pcap.LinkTypeIEEE802_11Radiotap)
if err != nil { ... }
w.WritePacket(time.Now(), frameBytes)
// closing the underlying file is the caller's responsibility

func NewWriter

func NewWriter(w io.Writer, linkType LinkType) (*Writer, error)

NewWriter writes the 24-byte global header to w and returns a Writer ready for WritePacket calls. Returns an error if the global header cannot be written.

func (*Writer) BytesWritten

func (w *Writer) BytesWritten() int64

BytesWritten returns the total bytes written, including the global header and all per-packet record headers.

func (*Writer) PacketsWritten

func (w *Writer) PacketsWritten() int

PacketsWritten returns the count of successful WritePacket calls.

func (*Writer) WritePacket

func (w *Writer) WritePacket(ts time.Time, data []byte) error

WritePacket appends one packet record. ts is the capture timestamp; data is the raw frame bytes (already including any radiotap header when the Writer was created with LinkTypeIEEE802_11Radiotap).

If data is nil an error is returned and no bytes are written. If a previous write already failed, WritePacket attempts the write anyway (no poison-pill behaviour).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL