capture

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package capture provides utilities for reading packets from network interfaces or files.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoSource is returned when neither a network interface nor a file
	// is specified to New.
	ErrNoSource = errors.New("must specify a network interface or file")
	// ErrAmbiguousSource is returned when both a network interface and a file
	// are specified to New.
	ErrAmbiguousSource = errors.New("cannot specify both network interface and file")
)
View Source
var (
	// ErrBytesFull is returned if the buffer cannot hold enough
	// data bytes to store this block.
	ErrBytesFull = errors.New("buffer out of space for more bytes")
	// ErrBlocksFull is returned if the buffer cannot hold another block.
	ErrBlocksFull = errors.New("buffer out of space for more packets")
)

Functions

This section is empty.

Types

type BlockBuffer

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

BlockBuffer stores a sequence of byte slices in compact form.

func NewBlockBuffer

func NewBlockBuffer(maxBlocks, maxBytes int) BlockBuffer

NewBlockBuffer creates a new BlockBuffer that can hold a maximum of maxBlocks slices or a total of at most maxBytes.

func (*BlockBuffer) Append

func (b *BlockBuffer) Append(data []byte) error

Append adds data to the buffer as a new block. Makes a copy of data, which may be modified freely after Append returns. Returns ErrBytesFull or ErrBlocksFull if there is insufficient space.

func (*BlockBuffer) Block

func (b *BlockBuffer) Block(n int) []byte

Block returns a slice referencing block n, indexed from 0. Any modifications to the returned slice modify the contents of the buffer.

func (*BlockBuffer) BlockLen

func (b *BlockBuffer) BlockLen() int

BlockLen returns the number of blocks currently stored in the buffer.

func (*BlockBuffer) BytesRemaining

func (b *BlockBuffer) BytesRemaining() int

BytesRemaining returns the number of additional bytes this buffer can hold.

func (*BlockBuffer) Clear

func (b *BlockBuffer) Clear()

Clear removes all blocks.

type PacketBuffer

type PacketBuffer struct {
	BlockBuffer
	// contains filtered or unexported fields
}

PacketBuffer stores captured packets in a compact format.

func NewPacketBuffer

func NewPacketBuffer(maxPackets, maxBytes int) *PacketBuffer

NewPacketBuffer creates a PacketBuffer with the specified limits.

func (*PacketBuffer) Append

func (pb *PacketBuffer) Append(pd PacketData) error

Append adds a packet to the PacketBuffer. Makes a copy of pd.Data, which may be modified after Append returns. Returns ErrBytesFull or ErrBlocksFull if there is insufficient space.

func (*PacketBuffer) Clear

func (pb *PacketBuffer) Clear()

Clear removes all packets.

func (*PacketBuffer) Packet

func (pb *PacketBuffer) Packet(n int) PacketData

Packet returns the packet at the specified index, which must be less than PacketLen. The Data field of the returned PacketData points into this PacketBuffer and must not be modified.

func (*PacketBuffer) PacketCap

func (pb *PacketBuffer) PacketCap() int

PacketCap returns the capacity for packets in the PacketBuffer.

func (*PacketBuffer) PacketLen

func (pb *PacketBuffer) PacketLen() int

PacketLen returns the number of packets in the PacketBuffer.

type PacketData

type PacketData struct {
	Info gopacket.CaptureInfo
	Data []byte
}

PacketData represents a single packet's data plus metadata indicating when the packet was captured.

type PacketSource

type PacketSource interface {
	// CollectPackets fills pb with packets.
	CollectPackets(pb *PacketBuffer) error
	// DiscardPacket reads a single packet and discards its contents.
	DiscardPacket() error
	StatProvider
}

PacketSource is an abstract source of network packets.

func New

func New(netInterface string, infile string, bufferSize int, noDelay bool, ports []int) (PacketSource, error)

New creates a PacketSource bound to the specified network interface or pcap file.

bufferSize determines the amount of kernel memory (in MiB) to allocate for temporary storage. A larger bufferSize can reduce dropped packets as revealed by Stats, but use caution as kernel memory is a precious resource.

type StatProvider

type StatProvider interface {
	// Stats returns statistics compatible with those from a pcap handle.
	Stats() (*pcap.Stats, error)
}

StatProvider provides statistics on packet capture.

Jump to

Keyboard shortcuts

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