packets

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package packets is a generated GoMock package.

Package packets has packet capture/emitting/filtering logic

Package packets is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var ErrAttachBPFNotSupported = errors.New("attaching BPF filters is not supported on this platform")

ErrAttachBPFNotSupported indicates that attaching classic BPF filters is not supported

Functions

func AllocPacketID

func AllocPacketID(maxTTL uint8) uint16

AllocPacketID allocates a new packet ID range, and returns the start of the range. This is used to avoid collisions when multiple traceroutes are running in parallel.

func MakeRawConn

func MakeRawConn(ctx context.Context, lc *net.ListenConfig, network string, localAddr netip.Addr) (*ipv4.RawConn, error)

MakeRawConn returns an ipv4.RawConn for the provided network/address

func RandomizePacketIDBase

func RandomizePacketIDBase()

RandomizePacketIDBase randomizes the packet ID. Only used in tests to avoid the same packet ID being used every time.

func ReadAndParse

func ReadAndParse(source Source, buffer []byte, parser *FrameParser) error

ReadAndParse reads from the given source into the buffer, and parses it with parser

func RemoveBPF

func RemoveBPF(c syscall.RawConn) error

func SerializeTCPFirstBytes

func SerializeTCPFirstBytes(tcp TCPInfo) []byte

SerializeTCPFirstBytes serializes the first 8 bytes of a TCP packet, used for testing

func SetBPF

func SetBPF(c syscall.RawConn, filter []bpf.RawInstruction) error

SetBPF attaches a BPF filter to the underlying socket

func SetBPFAndDrain

func SetBPFAndDrain(c syscall.RawConn, filter []bpf.RawInstruction) error

SetBPFAndDrain sets the filter for a raw socket and drains old data, so that new packets are guaranteed to match the filter

func StartDriver added in v0.1.26

func StartDriver() error

StartDriver starts the driver as there is no driver for this platform, this is a no-op

func WriteUDPFirstBytes

func WriteUDPFirstBytes(udp UDPInfo) []byte

WriteUDPFirstBytes writes the first 8 bytes of a UDP packet

Types

type FilterConfig added in v0.1.2

type FilterConfig struct {
	Src netip.AddrPort
	Dst netip.AddrPort
}

FilterConfig is the config for GenerateTCP4Filter

func (FilterConfig) GenerateTCP4Filter added in v0.1.2

func (c FilterConfig) GenerateTCP4Filter() ([]bpf.RawInstruction, error)

GenerateTCP4Filter creates a classic BPF filter for TCP SOCK_RAW sockets. It will only allow packets whose tuple matches the given config.

type FrameParser

type FrameParser struct {
	IP4     layers.IPv4
	IP6     layers.IPv6
	TCP     layers.TCP
	ICMP4   layers.ICMPv4
	ICMP6   layers.ICMPv6
	Payload gopacket.Payload
	Layers  []gopacket.LayerType
	// contains filtered or unexported fields
}

FrameParser parses traceroute responses using gopacket.

func NewFrameParser

func NewFrameParser() *FrameParser

NewFrameParser constructs a new FrameParser

func (*FrameParser) GetICMPInfo

func (p *FrameParser) GetICMPInfo() (ICMPInfo, error)

GetICMPInfo gets the ICMP details relevant to traceroutes from an ICMP response

func (*FrameParser) GetIPLayer

func (p *FrameParser) GetIPLayer() gopacket.LayerType

GetIPLayer gets the layer type of the IP layer (right now, only IPv4)

func (*FrameParser) GetIPPair

func (p *FrameParser) GetIPPair() (IPPair, error)

GetIPPair gets the IPPair of the IP layer

func (*FrameParser) GetTransportLayer

func (p *FrameParser) GetTransportLayer() gopacket.LayerType

GetTransportLayer gets the layer type of the transport layer (e.g. TCP, ICMP)

func (*FrameParser) IsDestinationUnreachable

func (p *FrameParser) IsDestinationUnreachable() bool

IsDestinationUnreachable returns true if the packet is a Destination Unreachable ICMP response

func (*FrameParser) IsTTLExceeded

func (p *FrameParser) IsTTLExceeded() bool

IsTTLExceeded returns true if the packet is a TTL exceeded ICMP response

func (*FrameParser) Parse

func (p *FrameParser) Parse(buffer []byte) error

Parse parses an ethernet packet

type ICMPInfo

type ICMPInfo struct {
	// IPPair is the source/dest IPs from the IP layer
	IPPair IPPair
	// WrappedPacketID is the packet ID from the wrapped IP payload
	WrappedPacketID uint16
	// ICMPPair is the source/dest IPs from the wrapped IP payload
	ICMPPair IPPair
	// Payload is the payload from within the wrapped IP packet, typically containing the first 8 bytes of TCP/UDP.
	Payload []byte
}

ICMPInfo encodes the information relevant to traceroutes from an ICMP response

type IPPair

type IPPair struct {
	SrcAddr netip.Addr
	DstAddr netip.Addr
}

IPPair combines a source/dest IP into a struct

func (IPPair) Flipped

func (p IPPair) Flipped() IPPair

Flipped returns an IPPair with the source/dest swapped

type MockSink

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

MockSink is a mock of Sink interface.

func NewMockSink

func NewMockSink(ctrl *gomock.Controller) *MockSink

NewMockSink creates a new mock instance.

func (*MockSink) Close

func (m *MockSink) Close() error

Close mocks base method.

func (*MockSink) EXPECT

func (m *MockSink) EXPECT() *MockSinkMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockSink) WriteTo

func (m *MockSink) WriteTo(buf []byte, addrPort netip.AddrPort) error

WriteTo mocks base method.

type MockSinkMockRecorder

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

MockSinkMockRecorder is the mock recorder for MockSink.

func (*MockSinkMockRecorder) Close

func (mr *MockSinkMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockSinkMockRecorder) WriteTo

func (mr *MockSinkMockRecorder) WriteTo(buf, addrPort interface{}) *gomock.Call

WriteTo indicates an expected call of WriteTo.

type MockSource

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

MockSource is a mock of Source interface.

func NewMockSource

func NewMockSource(ctrl *gomock.Controller) *MockSource

NewMockSource creates a new mock instance.

func (*MockSource) Close

func (m *MockSource) Close() error

Close mocks base method.

func (*MockSource) EXPECT

func (m *MockSource) EXPECT() *MockSourceMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockSource) Read

func (m *MockSource) Read(buf []byte) (int, error)

Read mocks base method.

func (*MockSource) SetPacketFilter

func (m *MockSource) SetPacketFilter(spec PacketFilterSpec) error

SetPacketFilter mocks base method.

func (*MockSource) SetReadDeadline

func (m *MockSource) SetReadDeadline(t time.Time) error

SetReadDeadline mocks base method.

type MockSourceMockRecorder

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

MockSourceMockRecorder is the mock recorder for MockSource.

func (*MockSourceMockRecorder) Close

func (mr *MockSourceMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockSourceMockRecorder) Read

func (mr *MockSourceMockRecorder) Read(buf interface{}) *gomock.Call

Read indicates an expected call of Read.

func (*MockSourceMockRecorder) SetPacketFilter

func (mr *MockSourceMockRecorder) SetPacketFilter(spec interface{}) *gomock.Call

SetPacketFilter indicates an expected call of SetPacketFilter.

func (*MockSourceMockRecorder) SetReadDeadline

func (mr *MockSourceMockRecorder) SetReadDeadline(t interface{}) *gomock.Call

SetReadDeadline indicates an expected call of SetReadDeadline.

type PacketFilterSpec

type PacketFilterSpec struct {
	// FilterType is which kind of packet filter to enable
	FilterType PacketFilterType
	// FilterConfig contains the 4-tuple of source/dest.
	// Some fields may be unused for certain PacketFilterTypes -- refer
	// to the PacketFilterType documentation
	FilterConfig FilterConfig
}

PacketFilterSpec defines how a packet Source should filter packets.

type PacketFilterType

type PacketFilterType int

PacketFilterType is which kind of packet filter to enable

const (
	// FilterTypeNone indicates no filter (all packets).
	FilterTypeNone PacketFilterType = iota
	// FilterTypeICMP indicates only ICMP packets.
	FilterTypeICMP
	// FilterTypeUDP indicates only ICMP and UDP packets.
	// FilterConfig will contain the UDP source/destination 4-tuple.
	FilterTypeUDP
	// FilterTypeTCP indicates only ICMP and TCP packets.
	// FilterConfig will contain the TCP source/destination 4-tuple.
	FilterTypeTCP
	// FilterTypeSYNACK indicates only TCP SYNACK packets.
	// FilterConfig will contain the source addr/port, but NOT the destination.
	// This is used by SACK traceroute.
	FilterTypeSYNACK
)

type Sink

type Sink interface {
	// WriteTo writes the given packet (buffer starts at the IP layer) to addrPort.
	// (the port is required for compatibility with Windows)
	WriteTo(buf []byte, addrPort netip.AddrPort) error
	// Close closes the socket
	Close() error
}

Sink is an interface which sends IP packets

func NewSinkLinux

func NewSinkLinux(addr netip.Addr) (Sink, error)

NewSinkLinux returns a new sinkLinux implementing packet sink

type Source

type Source interface {
	// SetReadDeadline sets the deadline for when a Read() call must finish
	SetReadDeadline(t time.Time) error
	// Read reads a packet (starting with the IP frame)
	Read(buf []byte) (int, error)
	// Close closes the socket
	Close() error
	// SetPacketFilter sets this Source to only return certain packets.
	// This is purely a performance optimization -- on some platforms SetPacketFilter
	// may be a no-op.
	SetPacketFilter(spec PacketFilterSpec) error
}

Source is an interface representing ethernet packet capture

func NewAFPacketSource

func NewAFPacketSource() (Source, error)

NewAFPacketSource creates a new AFPacketSource

type SourceSinkHandle

type SourceSinkHandle struct {
	Source Source
	Sink   Sink
	// MustClosePort means the traceroute must close the handle they used to reserve a port.
	// It's a Windows-specific hack -- on Windows, you can't actually capture all
	// packets with a raw socket.  By reserving a port, packets go to that socket instead of your
	// raw socket. This can only be addressed using a Windows driver.
	MustClosePort bool
}

SourceSinkHandle contains a platform's Source and Sink implementation

func NewSourceSink

func NewSourceSink(addr netip.Addr, useDriver bool) (SourceSinkHandle, error)

NewSourceSink returns a Source and Sink implementation for this platform

type TCPInfo

type TCPInfo struct {
	SrcPort uint16
	DstPort uint16
	Seq     uint32
}

TCPInfo is the info we get back from ICMP exceeded payload in a TCP probe.

func ParseTCPFirstBytes

func ParseTCPFirstBytes(buffer []byte) (TCPInfo, error)

ParseTCPFirstBytes parses the first 8 bytes an ICMP response is expected to have, as TCP

type UDPInfo

type UDPInfo struct {
	SrcPort  uint16
	DstPort  uint16
	Length   uint16
	Checksum uint16
}

UDPInfo is the info we get back from ICMP exceeded payload in a UDP probe.

func ParseUDPFirstBytes

func ParseUDPFirstBytes(buffer []byte) (UDPInfo, error)

ParseUDPFirstBytes parses the first 8 bytes an ICMP response is expected to have, as UDP

Jump to

Keyboard shortcuts

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