packet

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveGatewayMAC added in v1.5.0

func ResolveGatewayMAC(
	ctx context.Context,
	iface *net.Interface,
	localIP, gwIP net.IP,
) (net.HardwareAddr, error)

ResolveGatewayMAC resolves the MAC address of gwIP via ARP on iface. It opens a temporary pcap handle that is closed before returning.

Types

type BPFInstruction

type BPFInstruction struct {
	Op uint16
	Jt uint8
	Jf uint8
	K  uint32
}

type Handle

type Handle interface {
	// ReadPacketData reads the next packet from the wire.
	ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error)

	// WritePacketData sends a raw packet.
	WritePacketData(data []byte) error

	SetBPFRawInstructionFilter(filters []BPFInstruction) error

	ClearBPF() error

	LinkType() layers.LinkType

	// Close closes the handle.
	Close()
}

Handle is a common interface for both afpacket (Linux) and pcap (Others)

func NewHandle

func NewHandle(iface *net.Interface) (Handle, error)

NewHandle opens a raw socket using unix package.

type LinuxHandle

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

LinuxHandle uses standard syscalls (via x/sys/unix) to capture/inject packets. This ensures compatibility across all Linux architectures (including 386/MIPS).

func (*LinuxHandle) ClearBPF

func (h *LinuxHandle) ClearBPF() error

ClearBPF detaches the filter

func (*LinuxHandle) Close

func (h *LinuxHandle) Close()

func (*LinuxHandle) LinkType

func (h *LinuxHandle) LinkType() layers.LinkType

func (*LinuxHandle) ReadPacketData

func (h *LinuxHandle) ReadPacketData() ([]byte, gopacket.CaptureInfo, error)

ReadPacketData reads using unix.Recvfrom

func (*LinuxHandle) SetBPFRawInstructionFilter

func (h *LinuxHandle) SetBPFRawInstructionFilter(raw []BPFInstruction) error

SetBPFRawInstructionFilter attaches BPF using unix helper

func (*LinuxHandle) WritePacketData

func (h *LinuxHandle) WritePacketData(data []byte) error

WritePacketData injects packet using unix.Sendto

type Sniffer

type Sniffer interface {
	StartCapturing()
	Register(addrs []net.IP)
	GetOptimalTTL(key netutil.IPKey) uint8
}

type TCPSniffer

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

func NewTCPSniffer

func NewTCPSniffer(
	logger zerolog.Logger,
	handle Handle,
	cache cache.Cache[netutil.IPKey, uint8],
	cfg *config.RuntimeConfig,
) *TCPSniffer

func (*TCPSniffer) GetOptimalTTL

func (ts *TCPSniffer) GetOptimalTTL(key netutil.IPKey) uint8

GetOptimalTTL retrieves the estimated hop count for a given key from the cache.

func (*TCPSniffer) Register added in v1.5.2

func (ts *TCPSniffer) Register(addrs []net.IP)

Register registers new IP addresses for hop-count learning. Addresses that are already being tracked are ignored.

func (*TCPSniffer) StartCapturing

func (ts *TCPSniffer) StartCapturing()

StartCapturing begins monitoring for SYN/ACK packets in a background goroutine.

type TCPWriter

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

func NewTCPWriter

func NewTCPWriter(
	logger zerolog.Logger,
	handle Handle,
	iface *net.Interface,
	gatewayMAC net.HardwareAddr,
) *TCPWriter

func (*TCPWriter) WriteCraftedPacket

func (tw *TCPWriter) WriteCraftedPacket(
	ctx context.Context,
	src net.Addr,
	dst net.Addr,
	ttl uint8,
	payload []byte,
) (int, error)

WriteCraftedPacket crafts and injects a full TCP packet from a payload. It uses the pre-configured gateway MAC address.

type UDPSniffer

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

func NewUDPSniffer

func NewUDPSniffer(
	logger zerolog.Logger,
	handle Handle,
	cache cache.Cache[netutil.IPKey, uint8],
	cfg *config.RuntimeConfig,
) *UDPSniffer

func (*UDPSniffer) GetOptimalTTL

func (us *UDPSniffer) GetOptimalTTL(key netutil.IPKey) uint8

GetOptimalTTL retrieves the estimated hop count for a given key from the cache.

func (*UDPSniffer) Register added in v1.5.2

func (us *UDPSniffer) Register(addrs []net.IP)

Register registers new IP addresses for hop-count learning. Addresses that are already being tracked are ignored.

func (*UDPSniffer) StartCapturing

func (us *UDPSniffer) StartCapturing()

StartCapturing begins monitoring for UDP packets in a background goroutine.

type UDPWriter

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

func NewUDPWriter

func NewUDPWriter(
	logger zerolog.Logger,
	handle Handle,
	iface *net.Interface,
	gatewayMAC net.HardwareAddr,
) *UDPWriter

func (*UDPWriter) WriteCraftedPacket

func (uw *UDPWriter) WriteCraftedPacket(
	ctx context.Context,
	src net.Addr,
	dst net.Addr,
	ttl uint8,
	payload []byte,
) (int, error)

WriteCraftedPacket crafts and injects a full UDP packet from a payload. It uses the pre-configured gateway MAC address.

type Writer

type Writer interface {
	WriteCraftedPacket(
		ctx context.Context,
		src net.Addr,
		dst net.Addr,
		ttl uint8,
		payload []byte,
	) (int, error)
}

Jump to

Keyboard shortcuts

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