pcapfile

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package pcapfile checks the structure of a classic pcap capture file with bounded reads. It never interprets the packets: a structurally valid file is not evidence that the intended traffic was captured, that nothing was dropped, or that any protocol message in it is valid.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{MaxFileBytes: 1 << 30, MaxRecordBytes: 262144}

DefaultLimits read up to 1 GiB, with records up to 256 KiB: libpcap's largest default snaplen.

Functions

This section is empty.

Types

type Limits

type Limits struct {
	// MaxFileBytes bounds the bytes read from the file.
	MaxFileBytes int64
	// MaxRecordBytes bounds one record's captured length, independent of
	// the snaplen that the file declares.
	MaxRecordBytes uint32
}

Limits bound one check. Zero fields take the defaults.

type Report

type Report struct {
	Status Status `json:"status"`
	// Reason explains any status other than Valid.
	Reason string `json:"reason,omitempty"`
	// ByteOrder is "little" or "big"; TimestampUnit is "microsecond" or
	// "nanosecond". Both are empty when the header is not classic pcap.
	ByteOrder     string `json:"byte_order,omitempty"`
	TimestampUnit string `json:"timestamp_unit,omitempty"`
	VersionMajor  uint16 `json:"version_major,omitempty"`
	VersionMinor  uint16 `json:"version_minor,omitempty"`
	SnapLen       uint32 `json:"snaplen,omitempty"`
	// LinkType is the header's link-layer field as written.
	LinkType uint32 `json:"link_type,omitempty"`
	Packets  int    `json:"packets"`
	// TruncatedPackets counts records whose captured length is smaller than
	// their original length: snaplen truncation, not a malformed file.
	TruncatedPackets int `json:"truncated_packets"`
	// BytesRead is how much of the input was read and SHA256 is of exactly
	// those bytes. Complete reports that reading reached the end of the
	// input within the bound, so that SHA256 identifies the whole file. An
	// early verdict, a read error or the bound leave it false: the digest
	// then covers only the prefix that was read.
	BytesRead int64  `json:"bytes_read"`
	SHA256    string `json:"sha256"`
	Complete  bool   `json:"complete"`
}

Report is what one check found. Counts cover the records that were read completely before the verdict.

func Validate

func Validate(r io.Reader, limits Limits) (Report, error)

Validate checks a classic pcap stream. It reads at most limits.MaxFileBytes+1 bytes and never allocates a record's declared size. The error reports only a failure of r; structural findings, including input that ends early, are in the report.

type Status

type Status string

Status is the structural verdict on one file.

const (
	// Valid means every record header and its declared data fit the file.
	// A valid file can hold zero packets.
	Valid Status = "valid"
	// Unsupported means a recognized format or variant that this package
	// does not check, such as pcapng or another classic pcap version.
	Unsupported Status = "unsupported"
	// Malformed means the input is not a classic pcap file or breaks its
	// structure, including a final record that was cut off.
	Malformed Status = "malformed"
	// LimitExceeded means the file is larger than the requested bound; the
	// part that was read is reported, the rest was not checked.
	LimitExceeded Status = "limit_exceeded"
)

Jump to

Keyboard shortcuts

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