socket

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package socket implements AF_PACKET sockets / file descriptors (both for `afring` and plain `afpacket` modes). In addition, allocated sockets provide access to packet capture statistics for the underlying network interface during capture.

Index

Constants

View Source
const (
	TPacketVersion = unix.TPACKET_V3 // TPacketVersion : The TPacket header version to use
)

Variables

View Source
var (
	// ErrInvalidSocket denotes that the underlying socket is invalid
	ErrInvalidSocket = errors.New("invalid socket")

	// ErrIgnoreVLANonVLANIface denotes an (invalid) attempt at ignoring VLAN packets on a VLAN interface
	ErrIgnoreVLANonVLANIface = errors.New("cannot ignore VLAN packets on VLAN interface")
)

Functions

This section is empty.

Types

type FileDescriptor

type FileDescriptor int

FileDescriptor denotes a generic system level file descriptor (an int)

func New

func New(iface *link.Link) (FileDescriptor, error)

New instantiates a new file decriptor

func (FileDescriptor) Close

func (sd FileDescriptor) Close() error

Close closes the file descriptor

func (FileDescriptor) GetSocketStats

func (sd FileDescriptor) GetSocketStats() (ss TPacketStats, err error)

GetSocketStats returns (and resets) socket / traffic statistics

func (FileDescriptor) IsOpen

func (sd FileDescriptor) IsOpen() bool

IsOpen determines if the file descriptor is open / valid

func (FileDescriptor) SetSocketOptions

func (sd FileDescriptor) SetSocketOptions(iface *link.Link, snapLen int, options Options) error

SetSocketOptions sets several socket options on the underlying file descriptor required to perform AF_PACKET capture and retrieval of socket / traffic statistics

func (FileDescriptor) SetupRingBuffer

func (sd FileDescriptor) SetupRingBuffer(val unsafe.Pointer, vallen uintptr) error

SetupRingBuffer peforms a call via setsockopt() to prepare a mmmap'ed ring buffer

type MockFileDescriptor

type MockFileDescriptor struct {
	FileDescriptor
	// contains filtered or unexported fields
}

MockFileDescriptor denotes a mock file descriptor mimicking the behavior of an AF_PACKET socket by means of using a simple event file descriptor instead

func NewMock

func NewMock() (MockFileDescriptor, error)

NewMock instantiates a new mock file descriptor

func (*MockFileDescriptor) Get

Get fetches a single packets from the (buffered) mock file descriptor

func (*MockFileDescriptor) GetSocketStats

func (m *MockFileDescriptor) GetSocketStats() (ss TPacketStats, err error)

GetSocketStats returns (and resets) socket / traffic statistics

func (*MockFileDescriptor) GetSocketStatsNoReset

func (m *MockFileDescriptor) GetSocketStatsNoReset() (ss TPacketStats, err error)

GetSocketStatsNoReset returns socket / traffic statistics (without resetting the counters)

func (*MockFileDescriptor) HasPackets

func (m *MockFileDescriptor) HasPackets() bool

HasPackets returns if there are currently any packets in the mock buffer

func (*MockFileDescriptor) IncrementPacketCount

func (m *MockFileDescriptor) IncrementPacketCount(delta uint64)

IncrementPacketCount allows for simulation of packet / traffic statistics by means of manual counting (to be used during population of a mock data source)

func (*MockFileDescriptor) LastPoll

func (m *MockFileDescriptor) LastPoll() int64

LastPoll return the timestamp of the last poll on the FileDescriptor

func (*MockFileDescriptor) Put

func (m *MockFileDescriptor) Put(pkt capture.Packet)

Put sends a single packets via the (buffered) mock file descriptor

func (*MockFileDescriptor) ReleaseSemaphore

func (m *MockFileDescriptor) ReleaseSemaphore() (errno unix.Errno)

ReleaseSemaphore consumes from the event fd, releasing the semaphore and indicating that the next event can be sent

func (*MockFileDescriptor) SetNoRelease

func (m *MockFileDescriptor) SetNoRelease(enable bool) *MockFileDescriptor

SetNoRelease disables reading from the eventFD after data has been consumed (thereby not releasing the block which has to be handled elsewhere instead)

type Options added in v1.0.10

type Options struct {

	// Promiscuous mode
	Promiscuous bool

	// Ignore handling of VLANs
	IgnoreVLANs bool

	// Disable automatic BPF filter setup based on link type
	DisableAutoBPF bool

	// Extra BPF instructions to append to the auto-generated BPF filter
	ExtraBPFInstr []bpf.RawInstruction
}

Options denotes the available socket options

type TPacketStats

type TPacketStats struct {
	Packets      uint32
	Drops        uint32
	QueueFreezes uint32
}

TPacketStats denotes the V3 tpacket_stats structure, c.f. https://github.com/torvalds/linux/blob/master/include/uapi/linux/if_packet.h

Jump to

Keyboard shortcuts

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